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" 2783c9089eSPaolo Bonzini #include "monitor/monitor.h" 28737e150eSPaolo Bonzini #include "block/block_int.h" 29737e150eSPaolo Bonzini #include "block/blockjob.h" 301de7afc9SPaolo Bonzini #include "qemu/module.h" 317b1b5d19SPaolo Bonzini #include "qapi/qmp/qjson.h" 329c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 331de7afc9SPaolo Bonzini #include "qemu/notify.h" 34737e150eSPaolo Bonzini #include "block/coroutine.h" 35b2023818SLuiz Capitulino #include "qmp-commands.h" 361de7afc9SPaolo Bonzini #include "qemu/timer.h" 37fc01f7e7Sbellard 3871e72a19SJuan Quintela #ifdef CONFIG_BSD 397674e7bfSbellard #include <sys/types.h> 407674e7bfSbellard #include <sys/stat.h> 417674e7bfSbellard #include <sys/ioctl.h> 4272cf2d4fSBlue Swirl #include <sys/queue.h> 43c5e97233Sblueswir1 #ifndef __DragonFly__ 447674e7bfSbellard #include <sys/disk.h> 457674e7bfSbellard #endif 46c5e97233Sblueswir1 #endif 477674e7bfSbellard 4849dc768dSaliguori #ifdef _WIN32 4949dc768dSaliguori #include <windows.h> 5049dc768dSaliguori #endif 5149dc768dSaliguori 521c9805a3SStefan Hajnoczi #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 531c9805a3SStefan Hajnoczi 54470c0504SStefan Hajnoczi typedef enum { 55470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ = 0x1, 56f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE = 0x2, 57470c0504SStefan Hajnoczi } BdrvRequestFlags; 58470c0504SStefan Hajnoczi 597d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); 60f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 61f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 62c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 63f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 64f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 65ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 66f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 67f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 68f9f05dc5SKevin Wolf QEMUIOVector *iov); 69f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 70f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 71f9f05dc5SKevin Wolf QEMUIOVector *iov); 72c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 73470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 74470c0504SStefan Hajnoczi BdrvRequestFlags flags); 751c9805a3SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 76f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 77f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags); 78b2a61371SStefan Hajnoczi static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 79b2a61371SStefan Hajnoczi int64_t sector_num, 80b2a61371SStefan Hajnoczi QEMUIOVector *qiov, 81b2a61371SStefan Hajnoczi int nb_sectors, 82b2a61371SStefan Hajnoczi BlockDriverCompletionFunc *cb, 83b2a61371SStefan Hajnoczi void *opaque, 848c5873d6SStefan Hajnoczi bool is_write); 85b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque); 86621f0589SKevin Wolf static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 87621f0589SKevin Wolf int64_t sector_num, int nb_sectors); 88ec530c81Sbellard 8998f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 9098f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait); 9198f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 9298f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait); 9398f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 9498f90dbaSZhi Yong Wu bool is_write, int64_t *wait); 9598f90dbaSZhi Yong Wu 961b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 971b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 987ee930d0Sblueswir1 998a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 1008a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 101ea2384d3Sbellard 102eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 103eb852011SMarkus Armbruster static int use_bdrv_whitelist; 104eb852011SMarkus Armbruster 1059e0b22f4SStefan Hajnoczi #ifdef _WIN32 1069e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 1079e0b22f4SStefan Hajnoczi { 1089e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 1099e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 1109e0b22f4SStefan Hajnoczi filename[1] == ':'); 1119e0b22f4SStefan Hajnoczi } 1129e0b22f4SStefan Hajnoczi 1139e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 1149e0b22f4SStefan Hajnoczi { 1159e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 1169e0b22f4SStefan Hajnoczi filename[2] == '\0') 1179e0b22f4SStefan Hajnoczi return 1; 1189e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1199e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1209e0b22f4SStefan Hajnoczi return 1; 1219e0b22f4SStefan Hajnoczi return 0; 1229e0b22f4SStefan Hajnoczi } 1239e0b22f4SStefan Hajnoczi #endif 1249e0b22f4SStefan Hajnoczi 1250563e191SZhi Yong Wu /* throttling disk I/O limits */ 12698f90dbaSZhi Yong Wu void bdrv_io_limits_disable(BlockDriverState *bs) 12798f90dbaSZhi Yong Wu { 12898f90dbaSZhi Yong Wu bs->io_limits_enabled = false; 12998f90dbaSZhi Yong Wu 13098f90dbaSZhi Yong Wu while (qemu_co_queue_next(&bs->throttled_reqs)); 13198f90dbaSZhi Yong Wu 13298f90dbaSZhi Yong Wu if (bs->block_timer) { 13398f90dbaSZhi Yong Wu qemu_del_timer(bs->block_timer); 13498f90dbaSZhi Yong Wu qemu_free_timer(bs->block_timer); 13598f90dbaSZhi Yong Wu bs->block_timer = NULL; 13698f90dbaSZhi Yong Wu } 13798f90dbaSZhi Yong Wu 13898f90dbaSZhi Yong Wu bs->slice_start = 0; 13998f90dbaSZhi Yong Wu bs->slice_end = 0; 14098f90dbaSZhi Yong Wu } 14198f90dbaSZhi Yong Wu 1420563e191SZhi Yong Wu static void bdrv_block_timer(void *opaque) 1430563e191SZhi Yong Wu { 1440563e191SZhi Yong Wu BlockDriverState *bs = opaque; 1450563e191SZhi Yong Wu 1460563e191SZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 1470563e191SZhi Yong Wu } 1480563e191SZhi Yong Wu 1490563e191SZhi Yong Wu void bdrv_io_limits_enable(BlockDriverState *bs) 1500563e191SZhi Yong Wu { 1510563e191SZhi Yong Wu qemu_co_queue_init(&bs->throttled_reqs); 1520563e191SZhi Yong Wu bs->block_timer = qemu_new_timer_ns(vm_clock, bdrv_block_timer, bs); 1530563e191SZhi Yong Wu bs->io_limits_enabled = true; 1540563e191SZhi Yong Wu } 1550563e191SZhi Yong Wu 1560563e191SZhi Yong Wu bool bdrv_io_limits_enabled(BlockDriverState *bs) 1570563e191SZhi Yong Wu { 1580563e191SZhi Yong Wu BlockIOLimit *io_limits = &bs->io_limits; 1590563e191SZhi Yong Wu return io_limits->bps[BLOCK_IO_LIMIT_READ] 1600563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_WRITE] 1610563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_TOTAL] 1620563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_READ] 1630563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_WRITE] 1640563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_TOTAL]; 1650563e191SZhi Yong Wu } 1660563e191SZhi Yong Wu 16798f90dbaSZhi Yong Wu static void bdrv_io_limits_intercept(BlockDriverState *bs, 16898f90dbaSZhi Yong Wu bool is_write, int nb_sectors) 16998f90dbaSZhi Yong Wu { 17098f90dbaSZhi Yong Wu int64_t wait_time = -1; 17198f90dbaSZhi Yong Wu 17298f90dbaSZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 17398f90dbaSZhi Yong Wu qemu_co_queue_wait(&bs->throttled_reqs); 17498f90dbaSZhi Yong Wu } 17598f90dbaSZhi Yong Wu 17698f90dbaSZhi Yong Wu /* In fact, we hope to keep each request's timing, in FIFO mode. The next 17798f90dbaSZhi Yong Wu * throttled requests will not be dequeued until the current request is 17898f90dbaSZhi Yong Wu * allowed to be serviced. So if the current request still exceeds the 17998f90dbaSZhi Yong Wu * limits, it will be inserted to the head. All requests followed it will 18098f90dbaSZhi Yong Wu * be still in throttled_reqs queue. 18198f90dbaSZhi Yong Wu */ 18298f90dbaSZhi Yong Wu 18398f90dbaSZhi Yong Wu while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) { 18498f90dbaSZhi Yong Wu qemu_mod_timer(bs->block_timer, 18598f90dbaSZhi Yong Wu wait_time + qemu_get_clock_ns(vm_clock)); 18698f90dbaSZhi Yong Wu qemu_co_queue_wait_insert_head(&bs->throttled_reqs); 18798f90dbaSZhi Yong Wu } 18898f90dbaSZhi Yong Wu 18998f90dbaSZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 19098f90dbaSZhi Yong Wu } 19198f90dbaSZhi Yong Wu 1929e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 1939e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 1949e0b22f4SStefan Hajnoczi { 195947995c0SPaolo Bonzini const char *p; 196947995c0SPaolo Bonzini 1979e0b22f4SStefan Hajnoczi #ifdef _WIN32 1989e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 1999e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2009e0b22f4SStefan Hajnoczi return 0; 2019e0b22f4SStefan Hajnoczi } 202947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 203947995c0SPaolo Bonzini #else 204947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2059e0b22f4SStefan Hajnoczi #endif 2069e0b22f4SStefan Hajnoczi 207947995c0SPaolo Bonzini return *p == ':'; 2089e0b22f4SStefan Hajnoczi } 2099e0b22f4SStefan Hajnoczi 21083f64091Sbellard int path_is_absolute(const char *path) 21183f64091Sbellard { 21221664424Sbellard #ifdef _WIN32 21321664424Sbellard /* specific case for names like: "\\.\d:" */ 214f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 21521664424Sbellard return 1; 216f53f4da9SPaolo Bonzini } 217f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 2183b9f94e1Sbellard #else 219f53f4da9SPaolo Bonzini return (*path == '/'); 2203b9f94e1Sbellard #endif 22183f64091Sbellard } 22283f64091Sbellard 22383f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 22483f64091Sbellard path to it by considering it is relative to base_path. URL are 22583f64091Sbellard supported. */ 22683f64091Sbellard void path_combine(char *dest, int dest_size, 22783f64091Sbellard const char *base_path, 22883f64091Sbellard const char *filename) 22983f64091Sbellard { 23083f64091Sbellard const char *p, *p1; 23183f64091Sbellard int len; 23283f64091Sbellard 23383f64091Sbellard if (dest_size <= 0) 23483f64091Sbellard return; 23583f64091Sbellard if (path_is_absolute(filename)) { 23683f64091Sbellard pstrcpy(dest, dest_size, filename); 23783f64091Sbellard } else { 23883f64091Sbellard p = strchr(base_path, ':'); 23983f64091Sbellard if (p) 24083f64091Sbellard p++; 24183f64091Sbellard else 24283f64091Sbellard p = base_path; 2433b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2443b9f94e1Sbellard #ifdef _WIN32 2453b9f94e1Sbellard { 2463b9f94e1Sbellard const char *p2; 2473b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2483b9f94e1Sbellard if (!p1 || p2 > p1) 2493b9f94e1Sbellard p1 = p2; 2503b9f94e1Sbellard } 2513b9f94e1Sbellard #endif 25283f64091Sbellard if (p1) 25383f64091Sbellard p1++; 25483f64091Sbellard else 25583f64091Sbellard p1 = base_path; 25683f64091Sbellard if (p1 > p) 25783f64091Sbellard p = p1; 25883f64091Sbellard len = p - base_path; 25983f64091Sbellard if (len > dest_size - 1) 26083f64091Sbellard len = dest_size - 1; 26183f64091Sbellard memcpy(dest, base_path, len); 26283f64091Sbellard dest[len] = '\0'; 26383f64091Sbellard pstrcat(dest, dest_size, filename); 26483f64091Sbellard } 26583f64091Sbellard } 26683f64091Sbellard 267dc5a1371SPaolo Bonzini void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) 268dc5a1371SPaolo Bonzini { 269dc5a1371SPaolo Bonzini if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { 270dc5a1371SPaolo Bonzini pstrcpy(dest, sz, bs->backing_file); 271dc5a1371SPaolo Bonzini } else { 272dc5a1371SPaolo Bonzini path_combine(dest, sz, bs->filename, bs->backing_file); 273dc5a1371SPaolo Bonzini } 274dc5a1371SPaolo Bonzini } 275dc5a1371SPaolo Bonzini 2765efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 277ea2384d3Sbellard { 2788c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 2798c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 280f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 281f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 282f9f05dc5SKevin Wolf 283f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 284f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 285f8c35c1dSStefan Hajnoczi */ 286f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 28783f64091Sbellard /* add AIO emulation layer */ 288f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 289f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 29083f64091Sbellard } 291f9f05dc5SKevin Wolf } 292b2e12bc6SChristoph Hellwig 2938a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 294ea2384d3Sbellard } 295b338082bSbellard 296b338082bSbellard /* create a new block device (by default it is empty) */ 297b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 298fc01f7e7Sbellard { 2991b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 300b338082bSbellard 3017267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 302b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 303ea2384d3Sbellard if (device_name[0] != '\0') { 3041b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 305ea2384d3Sbellard } 30628a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 307d7d512f6SPaolo Bonzini notifier_list_init(&bs->close_notifiers); 308d616b224SStefan Hajnoczi notifier_with_return_list_init(&bs->before_write_notifiers); 309d7d512f6SPaolo Bonzini 310b338082bSbellard return bs; 311b338082bSbellard } 312b338082bSbellard 313d7d512f6SPaolo Bonzini void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) 314d7d512f6SPaolo Bonzini { 315d7d512f6SPaolo Bonzini notifier_list_add(&bs->close_notifiers, notify); 316d7d512f6SPaolo Bonzini } 317d7d512f6SPaolo Bonzini 318ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 319ea2384d3Sbellard { 320ea2384d3Sbellard BlockDriver *drv1; 3218a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3228a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 323ea2384d3Sbellard return drv1; 324ea2384d3Sbellard } 3258a22f02aSStefan Hajnoczi } 326ea2384d3Sbellard return NULL; 327ea2384d3Sbellard } 328ea2384d3Sbellard 329b64ec4e4SFam Zheng static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) 330eb852011SMarkus Armbruster { 331b64ec4e4SFam Zheng static const char *whitelist_rw[] = { 332b64ec4e4SFam Zheng CONFIG_BDRV_RW_WHITELIST 333b64ec4e4SFam Zheng }; 334b64ec4e4SFam Zheng static const char *whitelist_ro[] = { 335b64ec4e4SFam Zheng CONFIG_BDRV_RO_WHITELIST 336eb852011SMarkus Armbruster }; 337eb852011SMarkus Armbruster const char **p; 338eb852011SMarkus Armbruster 339b64ec4e4SFam Zheng if (!whitelist_rw[0] && !whitelist_ro[0]) { 340eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 341b64ec4e4SFam Zheng } 342eb852011SMarkus Armbruster 343b64ec4e4SFam Zheng for (p = whitelist_rw; *p; p++) { 344eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 345eb852011SMarkus Armbruster return 1; 346eb852011SMarkus Armbruster } 347eb852011SMarkus Armbruster } 348b64ec4e4SFam Zheng if (read_only) { 349b64ec4e4SFam Zheng for (p = whitelist_ro; *p; p++) { 350b64ec4e4SFam Zheng if (!strcmp(drv->format_name, *p)) { 351b64ec4e4SFam Zheng return 1; 352b64ec4e4SFam Zheng } 353b64ec4e4SFam Zheng } 354b64ec4e4SFam Zheng } 355eb852011SMarkus Armbruster return 0; 356eb852011SMarkus Armbruster } 357eb852011SMarkus Armbruster 358b64ec4e4SFam Zheng BlockDriver *bdrv_find_whitelisted_format(const char *format_name, 359b64ec4e4SFam Zheng bool read_only) 360eb852011SMarkus Armbruster { 361eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 362b64ec4e4SFam Zheng return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL; 363eb852011SMarkus Armbruster } 364eb852011SMarkus Armbruster 3655b7e1542SZhi Yong Wu typedef struct CreateCo { 3665b7e1542SZhi Yong Wu BlockDriver *drv; 3675b7e1542SZhi Yong Wu char *filename; 3685b7e1542SZhi Yong Wu QEMUOptionParameter *options; 3695b7e1542SZhi Yong Wu int ret; 3705b7e1542SZhi Yong Wu } CreateCo; 3715b7e1542SZhi Yong Wu 3725b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 3735b7e1542SZhi Yong Wu { 3745b7e1542SZhi Yong Wu CreateCo *cco = opaque; 3755b7e1542SZhi Yong Wu assert(cco->drv); 3765b7e1542SZhi Yong Wu 3775b7e1542SZhi Yong Wu cco->ret = cco->drv->bdrv_create(cco->filename, cco->options); 3785b7e1542SZhi Yong Wu } 3795b7e1542SZhi Yong Wu 3800e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 3810e7e1989SKevin Wolf QEMUOptionParameter *options) 382ea2384d3Sbellard { 3835b7e1542SZhi Yong Wu int ret; 3840e7e1989SKevin Wolf 3855b7e1542SZhi Yong Wu Coroutine *co; 3865b7e1542SZhi Yong Wu CreateCo cco = { 3875b7e1542SZhi Yong Wu .drv = drv, 3885b7e1542SZhi Yong Wu .filename = g_strdup(filename), 3895b7e1542SZhi Yong Wu .options = options, 3905b7e1542SZhi Yong Wu .ret = NOT_DONE, 3915b7e1542SZhi Yong Wu }; 3925b7e1542SZhi Yong Wu 3935b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 39480168bffSLuiz Capitulino ret = -ENOTSUP; 39580168bffSLuiz Capitulino goto out; 3965b7e1542SZhi Yong Wu } 3975b7e1542SZhi Yong Wu 3985b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 3995b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 4005b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 4015b7e1542SZhi Yong Wu } else { 4025b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 4035b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 4045b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 4055b7e1542SZhi Yong Wu qemu_aio_wait(); 4065b7e1542SZhi Yong Wu } 4075b7e1542SZhi Yong Wu } 4085b7e1542SZhi Yong Wu 4095b7e1542SZhi Yong Wu ret = cco.ret; 4105b7e1542SZhi Yong Wu 41180168bffSLuiz Capitulino out: 41280168bffSLuiz Capitulino g_free(cco.filename); 4135b7e1542SZhi Yong Wu return ret; 414ea2384d3Sbellard } 415ea2384d3Sbellard 41684a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 41784a12e66SChristoph Hellwig { 41884a12e66SChristoph Hellwig BlockDriver *drv; 41984a12e66SChristoph Hellwig 420b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 42184a12e66SChristoph Hellwig if (drv == NULL) { 42216905d71SStefan Hajnoczi return -ENOENT; 42384a12e66SChristoph Hellwig } 42484a12e66SChristoph Hellwig 42584a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 42684a12e66SChristoph Hellwig } 42784a12e66SChristoph Hellwig 428eba25057SJim Meyering /* 429eba25057SJim Meyering * Create a uniquely-named empty temporary file. 430eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 431eba25057SJim Meyering */ 432eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 433eba25057SJim Meyering { 434d5249393Sbellard #ifdef _WIN32 4353b9f94e1Sbellard char temp_dir[MAX_PATH]; 436eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 437eba25057SJim Meyering have length MAX_PATH or greater. */ 438eba25057SJim Meyering assert(size >= MAX_PATH); 439eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 440eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 441eba25057SJim Meyering ? 0 : -GetLastError()); 442d5249393Sbellard #else 443ea2384d3Sbellard int fd; 4447ccfb2ebSblueswir1 const char *tmpdir; 4450badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 4460badc1eeSaurel32 if (!tmpdir) 4470badc1eeSaurel32 tmpdir = "/tmp"; 448eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 449eba25057SJim Meyering return -EOVERFLOW; 450ea2384d3Sbellard } 451eba25057SJim Meyering fd = mkstemp(filename); 452fe235a06SDunrong Huang if (fd < 0) { 453fe235a06SDunrong Huang return -errno; 454fe235a06SDunrong Huang } 455fe235a06SDunrong Huang if (close(fd) != 0) { 456fe235a06SDunrong Huang unlink(filename); 457eba25057SJim Meyering return -errno; 458eba25057SJim Meyering } 459eba25057SJim Meyering return 0; 460d5249393Sbellard #endif 461eba25057SJim Meyering } 462ea2384d3Sbellard 463f3a5d3f8SChristoph Hellwig /* 464f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 465f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 466f3a5d3f8SChristoph Hellwig */ 467f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 468f3a5d3f8SChristoph Hellwig { 469508c7cb3SChristoph Hellwig int score_max = 0, score; 470508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 471f3a5d3f8SChristoph Hellwig 4728a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 473508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 474508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 475508c7cb3SChristoph Hellwig if (score > score_max) { 476508c7cb3SChristoph Hellwig score_max = score; 477508c7cb3SChristoph Hellwig drv = d; 478f3a5d3f8SChristoph Hellwig } 479508c7cb3SChristoph Hellwig } 480f3a5d3f8SChristoph Hellwig } 481f3a5d3f8SChristoph Hellwig 482508c7cb3SChristoph Hellwig return drv; 483f3a5d3f8SChristoph Hellwig } 484f3a5d3f8SChristoph Hellwig 485b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 48684a12e66SChristoph Hellwig { 48784a12e66SChristoph Hellwig BlockDriver *drv1; 48884a12e66SChristoph Hellwig char protocol[128]; 48984a12e66SChristoph Hellwig int len; 49084a12e66SChristoph Hellwig const char *p; 49184a12e66SChristoph Hellwig 49266f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 49366f82ceeSKevin Wolf 49439508e7aSChristoph Hellwig /* 49539508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 49639508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 49739508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 49839508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 49939508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 50039508e7aSChristoph Hellwig */ 50184a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 50239508e7aSChristoph Hellwig if (drv1) { 50384a12e66SChristoph Hellwig return drv1; 50484a12e66SChristoph Hellwig } 50539508e7aSChristoph Hellwig 5069e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 50739508e7aSChristoph Hellwig return bdrv_find_format("file"); 50839508e7aSChristoph Hellwig } 5099e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 5109e0b22f4SStefan Hajnoczi assert(p != NULL); 51184a12e66SChristoph Hellwig len = p - filename; 51284a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 51384a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 51484a12e66SChristoph Hellwig memcpy(protocol, filename, len); 51584a12e66SChristoph Hellwig protocol[len] = '\0'; 51684a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 51784a12e66SChristoph Hellwig if (drv1->protocol_name && 51884a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 51984a12e66SChristoph Hellwig return drv1; 52084a12e66SChristoph Hellwig } 52184a12e66SChristoph Hellwig } 52284a12e66SChristoph Hellwig return NULL; 52384a12e66SChristoph Hellwig } 52484a12e66SChristoph Hellwig 525f500a6d3SKevin Wolf static int find_image_format(BlockDriverState *bs, const char *filename, 526f500a6d3SKevin Wolf BlockDriver **pdrv) 527ea2384d3Sbellard { 528f500a6d3SKevin Wolf int score, score_max; 529ea2384d3Sbellard BlockDriver *drv1, *drv; 53083f64091Sbellard uint8_t buf[2048]; 531f500a6d3SKevin Wolf int ret = 0; 532f8ea0b00SNicholas Bellinger 53308a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 5348e895599SPaolo Bonzini if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { 535c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 536c98ac35dSStefan Weil if (!drv) { 537c98ac35dSStefan Weil ret = -ENOENT; 538c98ac35dSStefan Weil } 539c98ac35dSStefan Weil *pdrv = drv; 540c98ac35dSStefan Weil return ret; 5411a396859SNicholas A. Bellinger } 542f8ea0b00SNicholas Bellinger 54383f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 544ea2384d3Sbellard if (ret < 0) { 545c98ac35dSStefan Weil *pdrv = NULL; 546c98ac35dSStefan Weil return ret; 547ea2384d3Sbellard } 548ea2384d3Sbellard 549ea2384d3Sbellard score_max = 0; 55084a12e66SChristoph Hellwig drv = NULL; 5518a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 55283f64091Sbellard if (drv1->bdrv_probe) { 553ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 554ea2384d3Sbellard if (score > score_max) { 555ea2384d3Sbellard score_max = score; 556ea2384d3Sbellard drv = drv1; 557ea2384d3Sbellard } 558ea2384d3Sbellard } 55983f64091Sbellard } 560c98ac35dSStefan Weil if (!drv) { 561c98ac35dSStefan Weil ret = -ENOENT; 562c98ac35dSStefan Weil } 563c98ac35dSStefan Weil *pdrv = drv; 564c98ac35dSStefan Weil return ret; 565ea2384d3Sbellard } 566ea2384d3Sbellard 56751762288SStefan Hajnoczi /** 56851762288SStefan Hajnoczi * Set the current 'total_sectors' value 56951762288SStefan Hajnoczi */ 57051762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 57151762288SStefan Hajnoczi { 57251762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 57351762288SStefan Hajnoczi 574396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 575396759adSNicholas Bellinger if (bs->sg) 576396759adSNicholas Bellinger return 0; 577396759adSNicholas Bellinger 57851762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 57951762288SStefan Hajnoczi if (drv->bdrv_getlength) { 58051762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 58151762288SStefan Hajnoczi if (length < 0) { 58251762288SStefan Hajnoczi return length; 58351762288SStefan Hajnoczi } 58451762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 58551762288SStefan Hajnoczi } 58651762288SStefan Hajnoczi 58751762288SStefan Hajnoczi bs->total_sectors = hint; 58851762288SStefan Hajnoczi return 0; 58951762288SStefan Hajnoczi } 59051762288SStefan Hajnoczi 591c3993cdcSStefan Hajnoczi /** 5929e8f1835SPaolo Bonzini * Set open flags for a given discard mode 5939e8f1835SPaolo Bonzini * 5949e8f1835SPaolo Bonzini * Return 0 on success, -1 if the discard mode was invalid. 5959e8f1835SPaolo Bonzini */ 5969e8f1835SPaolo Bonzini int bdrv_parse_discard_flags(const char *mode, int *flags) 5979e8f1835SPaolo Bonzini { 5989e8f1835SPaolo Bonzini *flags &= ~BDRV_O_UNMAP; 5999e8f1835SPaolo Bonzini 6009e8f1835SPaolo Bonzini if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { 6019e8f1835SPaolo Bonzini /* do nothing */ 6029e8f1835SPaolo Bonzini } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { 6039e8f1835SPaolo Bonzini *flags |= BDRV_O_UNMAP; 6049e8f1835SPaolo Bonzini } else { 6059e8f1835SPaolo Bonzini return -1; 6069e8f1835SPaolo Bonzini } 6079e8f1835SPaolo Bonzini 6089e8f1835SPaolo Bonzini return 0; 6099e8f1835SPaolo Bonzini } 6109e8f1835SPaolo Bonzini 6119e8f1835SPaolo Bonzini /** 612c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 613c3993cdcSStefan Hajnoczi * 614c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 615c3993cdcSStefan Hajnoczi */ 616c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 617c3993cdcSStefan Hajnoczi { 618c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 619c3993cdcSStefan Hajnoczi 620c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 621c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 62292196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 62392196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 624c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 625c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 626c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 627c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 628c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 629c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 630c3993cdcSStefan Hajnoczi /* this is the default */ 631c3993cdcSStefan Hajnoczi } else { 632c3993cdcSStefan Hajnoczi return -1; 633c3993cdcSStefan Hajnoczi } 634c3993cdcSStefan Hajnoczi 635c3993cdcSStefan Hajnoczi return 0; 636c3993cdcSStefan Hajnoczi } 637c3993cdcSStefan Hajnoczi 63853fec9d3SStefan Hajnoczi /** 63953fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 64053fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 64153fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 64253fec9d3SStefan Hajnoczi */ 64353fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 64453fec9d3SStefan Hajnoczi { 64553fec9d3SStefan Hajnoczi bs->copy_on_read++; 64653fec9d3SStefan Hajnoczi } 64753fec9d3SStefan Hajnoczi 64853fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 64953fec9d3SStefan Hajnoczi { 65053fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 65153fec9d3SStefan Hajnoczi bs->copy_on_read--; 65253fec9d3SStefan Hajnoczi } 65353fec9d3SStefan Hajnoczi 6547b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 6557b272452SKevin Wolf { 6567b272452SKevin Wolf int open_flags = flags | BDRV_O_CACHE_WB; 6577b272452SKevin Wolf 6587b272452SKevin Wolf /* 6597b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 6607b272452SKevin Wolf * image. 6617b272452SKevin Wolf */ 6627b272452SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 6637b272452SKevin Wolf 6647b272452SKevin Wolf /* 6657b272452SKevin Wolf * Snapshots should be writable. 6667b272452SKevin Wolf */ 6677b272452SKevin Wolf if (bs->is_temporary) { 6687b272452SKevin Wolf open_flags |= BDRV_O_RDWR; 6697b272452SKevin Wolf } 6707b272452SKevin Wolf 6717b272452SKevin Wolf return open_flags; 6727b272452SKevin Wolf } 6737b272452SKevin Wolf 674b6ce07aaSKevin Wolf /* 67557915332SKevin Wolf * Common part for opening disk images and files 676b6ad491aSKevin Wolf * 677b6ad491aSKevin Wolf * Removes all processed options from *options. 67857915332SKevin Wolf */ 679f500a6d3SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, 680035fccdfSKevin Wolf QDict *options, int flags, BlockDriver *drv) 68157915332SKevin Wolf { 68257915332SKevin Wolf int ret, open_flags; 683035fccdfSKevin Wolf const char *filename; 68457915332SKevin Wolf 68557915332SKevin Wolf assert(drv != NULL); 6866405875cSPaolo Bonzini assert(bs->file == NULL); 687707ff828SKevin Wolf assert(options != NULL && bs->options != options); 68857915332SKevin Wolf 68945673671SKevin Wolf if (file != NULL) { 69045673671SKevin Wolf filename = file->filename; 69145673671SKevin Wolf } else { 69245673671SKevin Wolf filename = qdict_get_try_str(options, "filename"); 69345673671SKevin Wolf } 69445673671SKevin Wolf 69545673671SKevin Wolf trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name); 69628dcee10SStefan Hajnoczi 6975d186eb0SKevin Wolf /* bdrv_open() with directly using a protocol as drv. This layer is already 6985d186eb0SKevin Wolf * opened, so assign it to bs (while file becomes a closed BlockDriverState) 6995d186eb0SKevin Wolf * and return immediately. */ 7005d186eb0SKevin Wolf if (file != NULL && drv->bdrv_file_open) { 7015d186eb0SKevin Wolf bdrv_swap(file, bs); 7025d186eb0SKevin Wolf return 0; 7035d186eb0SKevin Wolf } 7045d186eb0SKevin Wolf 70557915332SKevin Wolf bs->open_flags = flags; 70657915332SKevin Wolf bs->buffer_alignment = 512; 707b64ec4e4SFam Zheng open_flags = bdrv_open_flags(bs, flags); 708b64ec4e4SFam Zheng bs->read_only = !(open_flags & BDRV_O_RDWR); 709b64ec4e4SFam Zheng 710b64ec4e4SFam Zheng if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { 711b64ec4e4SFam Zheng return -ENOTSUP; 712b64ec4e4SFam Zheng } 71357915332SKevin Wolf 71453fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 715b64ec4e4SFam Zheng if (!bs->read_only && (flags & BDRV_O_COPY_ON_READ)) { 71653fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 71753fec9d3SStefan Hajnoczi } 71853fec9d3SStefan Hajnoczi 719c2ad1b0cSKevin Wolf if (filename != NULL) { 72057915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 721c2ad1b0cSKevin Wolf } else { 722c2ad1b0cSKevin Wolf bs->filename[0] = '\0'; 723c2ad1b0cSKevin Wolf } 72457915332SKevin Wolf 72557915332SKevin Wolf bs->drv = drv; 7267267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 72757915332SKevin Wolf 72803f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 729e7c63796SStefan Hajnoczi 73066f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 73166f82ceeSKevin Wolf if (drv->bdrv_file_open) { 7325d186eb0SKevin Wolf assert(file == NULL); 733c2ad1b0cSKevin Wolf assert(drv->bdrv_parse_filename || filename != NULL); 73456d1b4d2SKevin Wolf ret = drv->bdrv_file_open(bs, options, open_flags); 735f500a6d3SKevin Wolf } else { 7362af5ef70SKevin Wolf if (file == NULL) { 7372af5ef70SKevin Wolf qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't use '%s' as a " 7382af5ef70SKevin Wolf "block driver for the protocol level", 7392af5ef70SKevin Wolf drv->format_name); 7402af5ef70SKevin Wolf ret = -EINVAL; 7412af5ef70SKevin Wolf goto free_and_fail; 7422af5ef70SKevin Wolf } 743f500a6d3SKevin Wolf assert(file != NULL); 744f500a6d3SKevin Wolf bs->file = file; 745b6ad491aSKevin Wolf ret = drv->bdrv_open(bs, options, open_flags); 74666f82ceeSKevin Wolf } 74766f82ceeSKevin Wolf 74857915332SKevin Wolf if (ret < 0) { 74957915332SKevin Wolf goto free_and_fail; 75057915332SKevin Wolf } 75157915332SKevin Wolf 75251762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 75351762288SStefan Hajnoczi if (ret < 0) { 75451762288SStefan Hajnoczi goto free_and_fail; 75557915332SKevin Wolf } 75651762288SStefan Hajnoczi 75757915332SKevin Wolf #ifndef _WIN32 75857915332SKevin Wolf if (bs->is_temporary) { 759c2ad1b0cSKevin Wolf assert(filename != NULL); 76057915332SKevin Wolf unlink(filename); 76157915332SKevin Wolf } 76257915332SKevin Wolf #endif 76357915332SKevin Wolf return 0; 76457915332SKevin Wolf 76557915332SKevin Wolf free_and_fail: 76666f82ceeSKevin Wolf bs->file = NULL; 7677267c094SAnthony Liguori g_free(bs->opaque); 76857915332SKevin Wolf bs->opaque = NULL; 76957915332SKevin Wolf bs->drv = NULL; 77057915332SKevin Wolf return ret; 77157915332SKevin Wolf } 77257915332SKevin Wolf 77357915332SKevin Wolf /* 774b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 775787e4a85SKevin Wolf * 776787e4a85SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 777787e4a85SKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 778787e4a85SKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 779787e4a85SKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_file_open. 780b6ce07aaSKevin Wolf */ 781787e4a85SKevin Wolf int bdrv_file_open(BlockDriverState **pbs, const char *filename, 782787e4a85SKevin Wolf QDict *options, int flags) 783b338082bSbellard { 78483f64091Sbellard BlockDriverState *bs; 7856db95603SChristoph Hellwig BlockDriver *drv; 786c2ad1b0cSKevin Wolf const char *drvname; 78783f64091Sbellard int ret; 7883b0d4f61Sbellard 789707ff828SKevin Wolf /* NULL means an empty set of options */ 790707ff828SKevin Wolf if (options == NULL) { 791707ff828SKevin Wolf options = qdict_new(); 7923b0d4f61Sbellard } 793707ff828SKevin Wolf 794707ff828SKevin Wolf bs = bdrv_new(""); 795707ff828SKevin Wolf bs->options = options; 796707ff828SKevin Wolf options = qdict_clone_shallow(options); 797707ff828SKevin Wolf 798035fccdfSKevin Wolf /* Fetch the file name from the options QDict if necessary */ 799035fccdfSKevin Wolf if (!filename) { 800035fccdfSKevin Wolf filename = qdict_get_try_str(options, "filename"); 801035fccdfSKevin Wolf } else if (filename && !qdict_haskey(options, "filename")) { 802035fccdfSKevin Wolf qdict_put(options, "filename", qstring_from_str(filename)); 803035fccdfSKevin Wolf } else { 804035fccdfSKevin Wolf qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't specify 'file' and " 805035fccdfSKevin Wolf "'filename' options at the same time"); 806035fccdfSKevin Wolf ret = -EINVAL; 807035fccdfSKevin Wolf goto fail; 808035fccdfSKevin Wolf } 809035fccdfSKevin Wolf 810c2ad1b0cSKevin Wolf /* Find the right block driver */ 811c2ad1b0cSKevin Wolf drvname = qdict_get_try_str(options, "driver"); 812c2ad1b0cSKevin Wolf if (drvname) { 813b64ec4e4SFam Zheng drv = bdrv_find_whitelisted_format(drvname, !(flags & BDRV_O_RDWR)); 814c2ad1b0cSKevin Wolf qdict_del(options, "driver"); 815c2ad1b0cSKevin Wolf } else if (filename) { 816c2ad1b0cSKevin Wolf drv = bdrv_find_protocol(filename); 817c2ad1b0cSKevin Wolf } else { 818c2ad1b0cSKevin Wolf qerror_report(ERROR_CLASS_GENERIC_ERROR, 819c2ad1b0cSKevin Wolf "Must specify either driver or file"); 820c2ad1b0cSKevin Wolf drv = NULL; 821c2ad1b0cSKevin Wolf } 822c2ad1b0cSKevin Wolf 823c2ad1b0cSKevin Wolf if (!drv) { 824c2ad1b0cSKevin Wolf ret = -ENOENT; 825c2ad1b0cSKevin Wolf goto fail; 826c2ad1b0cSKevin Wolf } 827c2ad1b0cSKevin Wolf 828c2ad1b0cSKevin Wolf /* Parse the filename and open it */ 829c2ad1b0cSKevin Wolf if (drv->bdrv_parse_filename && filename) { 8306963a30dSKevin Wolf Error *local_err = NULL; 8316963a30dSKevin Wolf drv->bdrv_parse_filename(filename, options, &local_err); 8326963a30dSKevin Wolf if (error_is_set(&local_err)) { 8336963a30dSKevin Wolf qerror_report_err(local_err); 8346963a30dSKevin Wolf error_free(local_err); 8356963a30dSKevin Wolf ret = -EINVAL; 8366963a30dSKevin Wolf goto fail; 8376963a30dSKevin Wolf } 83856d1b4d2SKevin Wolf qdict_del(options, "filename"); 839c2ad1b0cSKevin Wolf } else if (!drv->bdrv_parse_filename && !filename) { 840c2ad1b0cSKevin Wolf qerror_report(ERROR_CLASS_GENERIC_ERROR, 841c2ad1b0cSKevin Wolf "The '%s' block driver requires a file name", 842c2ad1b0cSKevin Wolf drv->format_name); 843c2ad1b0cSKevin Wolf ret = -EINVAL; 844c2ad1b0cSKevin Wolf goto fail; 8456963a30dSKevin Wolf } 8466963a30dSKevin Wolf 847035fccdfSKevin Wolf ret = bdrv_open_common(bs, NULL, options, flags, drv); 848707ff828SKevin Wolf if (ret < 0) { 849707ff828SKevin Wolf goto fail; 850707ff828SKevin Wolf } 851707ff828SKevin Wolf 852707ff828SKevin Wolf /* Check if any unknown options were used */ 853707ff828SKevin Wolf if (qdict_size(options) != 0) { 854707ff828SKevin Wolf const QDictEntry *entry = qdict_first(options); 855707ff828SKevin Wolf qerror_report(ERROR_CLASS_GENERIC_ERROR, "Block protocol '%s' doesn't " 856707ff828SKevin Wolf "support the option '%s'", 857707ff828SKevin Wolf drv->format_name, entry->key); 858707ff828SKevin Wolf ret = -EINVAL; 859707ff828SKevin Wolf goto fail; 860707ff828SKevin Wolf } 861707ff828SKevin Wolf QDECREF(options); 862707ff828SKevin Wolf 86371d0770cSaliguori bs->growable = 1; 86483f64091Sbellard *pbs = bs; 86583f64091Sbellard return 0; 866707ff828SKevin Wolf 867707ff828SKevin Wolf fail: 868707ff828SKevin Wolf QDECREF(options); 869707ff828SKevin Wolf if (!bs->drv) { 870707ff828SKevin Wolf QDECREF(bs->options); 871707ff828SKevin Wolf } 872707ff828SKevin Wolf bdrv_delete(bs); 873707ff828SKevin Wolf return ret; 8743b0d4f61Sbellard } 8753b0d4f61Sbellard 87631ca6d07SKevin Wolf /* 87731ca6d07SKevin Wolf * Opens the backing file for a BlockDriverState if not yet open 87831ca6d07SKevin Wolf * 87931ca6d07SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 88031ca6d07SKevin Wolf * empty set of options. The reference to the QDict is transferred to this 88131ca6d07SKevin Wolf * function (even on failure), so if the caller intends to reuse the dictionary, 88231ca6d07SKevin Wolf * it needs to use QINCREF() before calling bdrv_file_open. 88331ca6d07SKevin Wolf */ 88431ca6d07SKevin Wolf int bdrv_open_backing_file(BlockDriverState *bs, QDict *options) 8859156df12SPaolo Bonzini { 8869156df12SPaolo Bonzini char backing_filename[PATH_MAX]; 8879156df12SPaolo Bonzini int back_flags, ret; 8889156df12SPaolo Bonzini BlockDriver *back_drv = NULL; 8899156df12SPaolo Bonzini 8909156df12SPaolo Bonzini if (bs->backing_hd != NULL) { 89131ca6d07SKevin Wolf QDECREF(options); 8929156df12SPaolo Bonzini return 0; 8939156df12SPaolo Bonzini } 8949156df12SPaolo Bonzini 89531ca6d07SKevin Wolf /* NULL means an empty set of options */ 89631ca6d07SKevin Wolf if (options == NULL) { 89731ca6d07SKevin Wolf options = qdict_new(); 89831ca6d07SKevin Wolf } 89931ca6d07SKevin Wolf 9009156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 9011cb6f506SKevin Wolf if (qdict_haskey(options, "file.filename")) { 9021cb6f506SKevin Wolf backing_filename[0] = '\0'; 9031cb6f506SKevin Wolf } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { 90431ca6d07SKevin Wolf QDECREF(options); 9059156df12SPaolo Bonzini return 0; 9069156df12SPaolo Bonzini } 9079156df12SPaolo Bonzini 9089156df12SPaolo Bonzini bs->backing_hd = bdrv_new(""); 9099156df12SPaolo Bonzini bdrv_get_full_backing_filename(bs, backing_filename, 9109156df12SPaolo Bonzini sizeof(backing_filename)); 9119156df12SPaolo Bonzini 9129156df12SPaolo Bonzini if (bs->backing_format[0] != '\0') { 9139156df12SPaolo Bonzini back_drv = bdrv_find_format(bs->backing_format); 9149156df12SPaolo Bonzini } 9159156df12SPaolo Bonzini 9169156df12SPaolo Bonzini /* backing files always opened read-only */ 9179156df12SPaolo Bonzini back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT); 9189156df12SPaolo Bonzini 91931ca6d07SKevin Wolf ret = bdrv_open(bs->backing_hd, 92031ca6d07SKevin Wolf *backing_filename ? backing_filename : NULL, options, 921de9c0cecSKevin Wolf back_flags, back_drv); 9229156df12SPaolo Bonzini if (ret < 0) { 9239156df12SPaolo Bonzini bdrv_delete(bs->backing_hd); 9249156df12SPaolo Bonzini bs->backing_hd = NULL; 9259156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 9269156df12SPaolo Bonzini return ret; 9279156df12SPaolo Bonzini } 9289156df12SPaolo Bonzini return 0; 9299156df12SPaolo Bonzini } 9309156df12SPaolo Bonzini 931707ff828SKevin Wolf static void extract_subqdict(QDict *src, QDict **dst, const char *start) 932707ff828SKevin Wolf { 933707ff828SKevin Wolf const QDictEntry *entry, *next; 934707ff828SKevin Wolf const char *p; 935707ff828SKevin Wolf 936707ff828SKevin Wolf *dst = qdict_new(); 937707ff828SKevin Wolf entry = qdict_first(src); 938707ff828SKevin Wolf 939707ff828SKevin Wolf while (entry != NULL) { 940707ff828SKevin Wolf next = qdict_next(src, entry); 941707ff828SKevin Wolf if (strstart(entry->key, start, &p)) { 942707ff828SKevin Wolf qobject_incref(entry->value); 943707ff828SKevin Wolf qdict_put_obj(*dst, p, entry->value); 944707ff828SKevin Wolf qdict_del(src, entry->key); 945707ff828SKevin Wolf } 946707ff828SKevin Wolf entry = next; 947707ff828SKevin Wolf } 948707ff828SKevin Wolf } 949707ff828SKevin Wolf 950b6ce07aaSKevin Wolf /* 951b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 952de9c0cecSKevin Wolf * 953de9c0cecSKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 954de9c0cecSKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 955de9c0cecSKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 956de9c0cecSKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_open. 957b6ce07aaSKevin Wolf */ 958de9c0cecSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, 959de9c0cecSKevin Wolf int flags, BlockDriver *drv) 960ea2384d3Sbellard { 961b6ce07aaSKevin Wolf int ret; 96289c9bc3dSStefan Weil /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 96389c9bc3dSStefan Weil char tmp_filename[PATH_MAX + 1]; 964f500a6d3SKevin Wolf BlockDriverState *file = NULL; 965707ff828SKevin Wolf QDict *file_options = NULL; 96633e3963eSbellard 967de9c0cecSKevin Wolf /* NULL means an empty set of options */ 968de9c0cecSKevin Wolf if (options == NULL) { 969de9c0cecSKevin Wolf options = qdict_new(); 970de9c0cecSKevin Wolf } 971de9c0cecSKevin Wolf 972de9c0cecSKevin Wolf bs->options = options; 973b6ad491aSKevin Wolf options = qdict_clone_shallow(options); 974de9c0cecSKevin Wolf 975de9c0cecSKevin Wolf /* For snapshot=on, create a temporary qcow2 overlay */ 97683f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 977ea2384d3Sbellard BlockDriverState *bs1; 978ea2384d3Sbellard int64_t total_size; 97991a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 98008b392e1SKevin Wolf QEMUOptionParameter *create_options; 981b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 98233e3963eSbellard 983c2ad1b0cSKevin Wolf if (qdict_size(options) != 0) { 984c2ad1b0cSKevin Wolf error_report("Can't use snapshot=on with driver-specific options"); 985c2ad1b0cSKevin Wolf ret = -EINVAL; 986c2ad1b0cSKevin Wolf goto fail; 987c2ad1b0cSKevin Wolf } 988c2ad1b0cSKevin Wolf assert(filename != NULL); 989c2ad1b0cSKevin Wolf 990ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 991ea2384d3Sbellard instead of opening 'filename' directly */ 992ea2384d3Sbellard 993ea2384d3Sbellard /* if there is a backing file, use it */ 994ea2384d3Sbellard bs1 = bdrv_new(""); 995de9c0cecSKevin Wolf ret = bdrv_open(bs1, filename, NULL, 0, drv); 99651d7c00cSaliguori if (ret < 0) { 997ea2384d3Sbellard bdrv_delete(bs1); 998de9c0cecSKevin Wolf goto fail; 999ea2384d3Sbellard } 10003e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 10017c96d46eSaliguori 1002ea2384d3Sbellard bdrv_delete(bs1); 1003ea2384d3Sbellard 1004eba25057SJim Meyering ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 1005eba25057SJim Meyering if (ret < 0) { 1006de9c0cecSKevin Wolf goto fail; 1007eba25057SJim Meyering } 10087c96d46eSaliguori 10097c96d46eSaliguori /* Real path is meaningless for protocols */ 10104d70655bSStefan Hajnoczi if (path_has_protocol(filename)) { 10117c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 10127c96d46eSaliguori "%s", filename); 1013de9c0cecSKevin Wolf } else if (!realpath(filename, backing_filename)) { 1014de9c0cecSKevin Wolf ret = -errno; 1015de9c0cecSKevin Wolf goto fail; 1016de9c0cecSKevin Wolf } 10177c96d46eSaliguori 101891a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 101908b392e1SKevin Wolf create_options = parse_option_parameters("", bdrv_qcow2->create_options, 102008b392e1SKevin Wolf NULL); 102191a073a9SKevin Wolf 102208b392e1SKevin Wolf set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); 102308b392e1SKevin Wolf set_option_parameter(create_options, BLOCK_OPT_BACKING_FILE, 102408b392e1SKevin Wolf backing_filename); 102591a073a9SKevin Wolf if (drv) { 102608b392e1SKevin Wolf set_option_parameter(create_options, BLOCK_OPT_BACKING_FMT, 102791a073a9SKevin Wolf drv->format_name); 102891a073a9SKevin Wolf } 102991a073a9SKevin Wolf 103008b392e1SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options); 103108b392e1SKevin Wolf free_option_parameters(create_options); 103251d7c00cSaliguori if (ret < 0) { 1033de9c0cecSKevin Wolf goto fail; 1034ea2384d3Sbellard } 103591a073a9SKevin Wolf 1036ea2384d3Sbellard filename = tmp_filename; 103791a073a9SKevin Wolf drv = bdrv_qcow2; 1038ea2384d3Sbellard bs->is_temporary = 1; 1039ea2384d3Sbellard } 1040ea2384d3Sbellard 1041f500a6d3SKevin Wolf /* Open image file without format layer */ 1042be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 1043be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 1044be028adcSJeff Cody } 1045be028adcSJeff Cody 1046707ff828SKevin Wolf extract_subqdict(options, &file_options, "file."); 1047707ff828SKevin Wolf 1048707ff828SKevin Wolf ret = bdrv_file_open(&file, filename, file_options, 104950b05b6fSKevin Wolf bdrv_open_flags(bs, flags | BDRV_O_UNMAP)); 1050f500a6d3SKevin Wolf if (ret < 0) { 1051de9c0cecSKevin Wolf goto fail; 1052f500a6d3SKevin Wolf } 1053f500a6d3SKevin Wolf 1054f500a6d3SKevin Wolf /* Find the right image format driver */ 1055f500a6d3SKevin Wolf if (!drv) { 1056f500a6d3SKevin Wolf ret = find_image_format(file, filename, &drv); 1057f500a6d3SKevin Wolf } 1058f500a6d3SKevin Wolf 1059f500a6d3SKevin Wolf if (!drv) { 1060f500a6d3SKevin Wolf goto unlink_and_fail; 1061f500a6d3SKevin Wolf } 1062f500a6d3SKevin Wolf 1063b6ce07aaSKevin Wolf /* Open the image */ 1064035fccdfSKevin Wolf ret = bdrv_open_common(bs, file, options, flags, drv); 1065b6ce07aaSKevin Wolf if (ret < 0) { 10666987307cSChristoph Hellwig goto unlink_and_fail; 10676987307cSChristoph Hellwig } 10686987307cSChristoph Hellwig 1069f500a6d3SKevin Wolf if (bs->file != file) { 1070f500a6d3SKevin Wolf bdrv_delete(file); 1071f500a6d3SKevin Wolf file = NULL; 1072f500a6d3SKevin Wolf } 1073f500a6d3SKevin Wolf 1074b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 10759156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 107631ca6d07SKevin Wolf QDict *backing_options; 107731ca6d07SKevin Wolf 107831ca6d07SKevin Wolf extract_subqdict(options, &backing_options, "backing."); 107931ca6d07SKevin Wolf ret = bdrv_open_backing_file(bs, backing_options); 1080b6ce07aaSKevin Wolf if (ret < 0) { 1081b6ad491aSKevin Wolf goto close_and_fail; 1082b6ce07aaSKevin Wolf } 1083b6ce07aaSKevin Wolf } 1084b6ce07aaSKevin Wolf 1085b6ad491aSKevin Wolf /* Check if any unknown options were used */ 1086b6ad491aSKevin Wolf if (qdict_size(options) != 0) { 1087b6ad491aSKevin Wolf const QDictEntry *entry = qdict_first(options); 1088b6ad491aSKevin Wolf qerror_report(ERROR_CLASS_GENERIC_ERROR, "Block format '%s' used by " 1089b6ad491aSKevin Wolf "device '%s' doesn't support the option '%s'", 1090b6ad491aSKevin Wolf drv->format_name, bs->device_name, entry->key); 1091b6ad491aSKevin Wolf 1092b6ad491aSKevin Wolf ret = -EINVAL; 1093b6ad491aSKevin Wolf goto close_and_fail; 1094b6ad491aSKevin Wolf } 1095b6ad491aSKevin Wolf QDECREF(options); 1096b6ad491aSKevin Wolf 1097b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 10987d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 1099b6ce07aaSKevin Wolf } 1100b6ce07aaSKevin Wolf 110198f90dbaSZhi Yong Wu /* throttling disk I/O limits */ 110298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 110398f90dbaSZhi Yong Wu bdrv_io_limits_enable(bs); 110498f90dbaSZhi Yong Wu } 110598f90dbaSZhi Yong Wu 1106b6ce07aaSKevin Wolf return 0; 1107b6ce07aaSKevin Wolf 1108b6ce07aaSKevin Wolf unlink_and_fail: 1109f500a6d3SKevin Wolf if (file != NULL) { 1110f500a6d3SKevin Wolf bdrv_delete(file); 1111f500a6d3SKevin Wolf } 1112b6ce07aaSKevin Wolf if (bs->is_temporary) { 1113b6ce07aaSKevin Wolf unlink(filename); 1114b6ce07aaSKevin Wolf } 1115de9c0cecSKevin Wolf fail: 1116de9c0cecSKevin Wolf QDECREF(bs->options); 1117b6ad491aSKevin Wolf QDECREF(options); 1118de9c0cecSKevin Wolf bs->options = NULL; 1119b6ad491aSKevin Wolf return ret; 1120de9c0cecSKevin Wolf 1121b6ad491aSKevin Wolf close_and_fail: 1122b6ad491aSKevin Wolf bdrv_close(bs); 1123b6ad491aSKevin Wolf QDECREF(options); 1124b6ce07aaSKevin Wolf return ret; 1125b6ce07aaSKevin Wolf } 1126b6ce07aaSKevin Wolf 1127e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 1128e971aa12SJeff Cody bool prepared; 1129e971aa12SJeff Cody BDRVReopenState state; 1130e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 1131e971aa12SJeff Cody } BlockReopenQueueEntry; 1132e971aa12SJeff Cody 1133e971aa12SJeff Cody /* 1134e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 1135e971aa12SJeff Cody * reopen of multiple devices. 1136e971aa12SJeff Cody * 1137e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 1138e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 1139e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 1140e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 1141e971aa12SJeff Cody * atomic 'set'. 1142e971aa12SJeff Cody * 1143e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 1144e971aa12SJeff Cody * 1145e971aa12SJeff Cody * flags contains the open flags for the associated bs 1146e971aa12SJeff Cody * 1147e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 1148e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 1149e971aa12SJeff Cody * 1150e971aa12SJeff Cody */ 1151e971aa12SJeff Cody BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 1152e971aa12SJeff Cody BlockDriverState *bs, int flags) 1153e971aa12SJeff Cody { 1154e971aa12SJeff Cody assert(bs != NULL); 1155e971aa12SJeff Cody 1156e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 1157e971aa12SJeff Cody if (bs_queue == NULL) { 1158e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 1159e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 1160e971aa12SJeff Cody } 1161e971aa12SJeff Cody 1162e971aa12SJeff Cody if (bs->file) { 1163e971aa12SJeff Cody bdrv_reopen_queue(bs_queue, bs->file, flags); 1164e971aa12SJeff Cody } 1165e971aa12SJeff Cody 1166e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 1167e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 1168e971aa12SJeff Cody 1169e971aa12SJeff Cody bs_entry->state.bs = bs; 1170e971aa12SJeff Cody bs_entry->state.flags = flags; 1171e971aa12SJeff Cody 1172e971aa12SJeff Cody return bs_queue; 1173e971aa12SJeff Cody } 1174e971aa12SJeff Cody 1175e971aa12SJeff Cody /* 1176e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 1177e971aa12SJeff Cody * 1178e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 1179e971aa12SJeff Cody * via bdrv_reopen_queue(). 1180e971aa12SJeff Cody * 1181e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 1182e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 1183e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 1184e971aa12SJeff Cody * data cleaned up. 1185e971aa12SJeff Cody * 1186e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 1187e971aa12SJeff Cody * to all devices. 1188e971aa12SJeff Cody * 1189e971aa12SJeff Cody */ 1190e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 1191e971aa12SJeff Cody { 1192e971aa12SJeff Cody int ret = -1; 1193e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 1194e971aa12SJeff Cody Error *local_err = NULL; 1195e971aa12SJeff Cody 1196e971aa12SJeff Cody assert(bs_queue != NULL); 1197e971aa12SJeff Cody 1198e971aa12SJeff Cody bdrv_drain_all(); 1199e971aa12SJeff Cody 1200e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1201e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 1202e971aa12SJeff Cody error_propagate(errp, local_err); 1203e971aa12SJeff Cody goto cleanup; 1204e971aa12SJeff Cody } 1205e971aa12SJeff Cody bs_entry->prepared = true; 1206e971aa12SJeff Cody } 1207e971aa12SJeff Cody 1208e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 1209e971aa12SJeff Cody * changes 1210e971aa12SJeff Cody */ 1211e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1212e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 1213e971aa12SJeff Cody } 1214e971aa12SJeff Cody 1215e971aa12SJeff Cody ret = 0; 1216e971aa12SJeff Cody 1217e971aa12SJeff Cody cleanup: 1218e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 1219e971aa12SJeff Cody if (ret && bs_entry->prepared) { 1220e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 1221e971aa12SJeff Cody } 1222e971aa12SJeff Cody g_free(bs_entry); 1223e971aa12SJeff Cody } 1224e971aa12SJeff Cody g_free(bs_queue); 1225e971aa12SJeff Cody return ret; 1226e971aa12SJeff Cody } 1227e971aa12SJeff Cody 1228e971aa12SJeff Cody 1229e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 1230e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1231e971aa12SJeff Cody { 1232e971aa12SJeff Cody int ret = -1; 1233e971aa12SJeff Cody Error *local_err = NULL; 1234e971aa12SJeff Cody BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); 1235e971aa12SJeff Cody 1236e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1237e971aa12SJeff Cody if (local_err != NULL) { 1238e971aa12SJeff Cody error_propagate(errp, local_err); 1239e971aa12SJeff Cody } 1240e971aa12SJeff Cody return ret; 1241e971aa12SJeff Cody } 1242e971aa12SJeff Cody 1243e971aa12SJeff Cody 1244e971aa12SJeff Cody /* 1245e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1246e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1247e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1248e971aa12SJeff Cody * 1249e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1250e971aa12SJeff Cody * flags are the new open flags 1251e971aa12SJeff Cody * queue is the reopen queue 1252e971aa12SJeff Cody * 1253e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1254e971aa12SJeff Cody * as well. 1255e971aa12SJeff Cody * 1256e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1257e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1258e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1259e971aa12SJeff Cody * 1260e971aa12SJeff Cody */ 1261e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1262e971aa12SJeff Cody Error **errp) 1263e971aa12SJeff Cody { 1264e971aa12SJeff Cody int ret = -1; 1265e971aa12SJeff Cody Error *local_err = NULL; 1266e971aa12SJeff Cody BlockDriver *drv; 1267e971aa12SJeff Cody 1268e971aa12SJeff Cody assert(reopen_state != NULL); 1269e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1270e971aa12SJeff Cody drv = reopen_state->bs->drv; 1271e971aa12SJeff Cody 1272e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 1273e971aa12SJeff Cody * to r/w */ 1274e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 1275e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 1276e971aa12SJeff Cody error_set(errp, QERR_DEVICE_IS_READ_ONLY, 1277e971aa12SJeff Cody reopen_state->bs->device_name); 1278e971aa12SJeff Cody goto error; 1279e971aa12SJeff Cody } 1280e971aa12SJeff Cody 1281e971aa12SJeff Cody 1282e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 1283e971aa12SJeff Cody if (ret) { 1284e971aa12SJeff Cody error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", 1285e971aa12SJeff Cody strerror(-ret)); 1286e971aa12SJeff Cody goto error; 1287e971aa12SJeff Cody } 1288e971aa12SJeff Cody 1289e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 1290e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 1291e971aa12SJeff Cody if (ret) { 1292e971aa12SJeff Cody if (local_err != NULL) { 1293e971aa12SJeff Cody error_propagate(errp, local_err); 1294e971aa12SJeff Cody } else { 1295d8b6895fSLuiz Capitulino error_setg(errp, "failed while preparing to reopen image '%s'", 1296e971aa12SJeff Cody reopen_state->bs->filename); 1297e971aa12SJeff Cody } 1298e971aa12SJeff Cody goto error; 1299e971aa12SJeff Cody } 1300e971aa12SJeff Cody } else { 1301e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 1302e971aa12SJeff Cody * handler for each supported drv. */ 1303e971aa12SJeff Cody error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 1304e971aa12SJeff Cody drv->format_name, reopen_state->bs->device_name, 1305e971aa12SJeff Cody "reopening of file"); 1306e971aa12SJeff Cody ret = -1; 1307e971aa12SJeff Cody goto error; 1308e971aa12SJeff Cody } 1309e971aa12SJeff Cody 1310e971aa12SJeff Cody ret = 0; 1311e971aa12SJeff Cody 1312e971aa12SJeff Cody error: 1313e971aa12SJeff Cody return ret; 1314e971aa12SJeff Cody } 1315e971aa12SJeff Cody 1316e971aa12SJeff Cody /* 1317e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 1318e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 1319e971aa12SJeff Cody * the active BlockDriverState contents. 1320e971aa12SJeff Cody */ 1321e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 1322e971aa12SJeff Cody { 1323e971aa12SJeff Cody BlockDriver *drv; 1324e971aa12SJeff Cody 1325e971aa12SJeff Cody assert(reopen_state != NULL); 1326e971aa12SJeff Cody drv = reopen_state->bs->drv; 1327e971aa12SJeff Cody assert(drv != NULL); 1328e971aa12SJeff Cody 1329e971aa12SJeff Cody /* If there are any driver level actions to take */ 1330e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 1331e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 1332e971aa12SJeff Cody } 1333e971aa12SJeff Cody 1334e971aa12SJeff Cody /* set BDS specific flags now */ 1335e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 1336e971aa12SJeff Cody reopen_state->bs->enable_write_cache = !!(reopen_state->flags & 1337e971aa12SJeff Cody BDRV_O_CACHE_WB); 1338e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 1339e971aa12SJeff Cody } 1340e971aa12SJeff Cody 1341e971aa12SJeff Cody /* 1342e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 1343e971aa12SJeff Cody * reopen_state 1344e971aa12SJeff Cody */ 1345e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 1346e971aa12SJeff Cody { 1347e971aa12SJeff Cody BlockDriver *drv; 1348e971aa12SJeff Cody 1349e971aa12SJeff Cody assert(reopen_state != NULL); 1350e971aa12SJeff Cody drv = reopen_state->bs->drv; 1351e971aa12SJeff Cody assert(drv != NULL); 1352e971aa12SJeff Cody 1353e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 1354e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 1355e971aa12SJeff Cody } 1356e971aa12SJeff Cody } 1357e971aa12SJeff Cody 1358e971aa12SJeff Cody 1359fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 1360fc01f7e7Sbellard { 13613e914655SPaolo Bonzini if (bs->job) { 13623e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 13633e914655SPaolo Bonzini } 1364*58fda173SStefan Hajnoczi bdrv_drain_all(); /* complete I/O */ 1365*58fda173SStefan Hajnoczi bdrv_flush(bs); 1366*58fda173SStefan Hajnoczi bdrv_drain_all(); /* in case flush left pending I/O */ 1367d7d512f6SPaolo Bonzini notifier_list_notify(&bs->close_notifiers, bs); 13687094f12fSKevin Wolf 13693cbc002cSPaolo Bonzini if (bs->drv) { 1370557df6acSStefan Hajnoczi if (bs->backing_hd) { 1371ea2384d3Sbellard bdrv_delete(bs->backing_hd); 1372557df6acSStefan Hajnoczi bs->backing_hd = NULL; 1373557df6acSStefan Hajnoczi } 1374ea2384d3Sbellard bs->drv->bdrv_close(bs); 13757267c094SAnthony Liguori g_free(bs->opaque); 1376ea2384d3Sbellard #ifdef _WIN32 1377ea2384d3Sbellard if (bs->is_temporary) { 1378ea2384d3Sbellard unlink(bs->filename); 1379ea2384d3Sbellard } 138067b915a5Sbellard #endif 1381ea2384d3Sbellard bs->opaque = NULL; 1382ea2384d3Sbellard bs->drv = NULL; 138353fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 1384a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 1385a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 13866405875cSPaolo Bonzini bs->total_sectors = 0; 13876405875cSPaolo Bonzini bs->encrypted = 0; 13886405875cSPaolo Bonzini bs->valid_key = 0; 13896405875cSPaolo Bonzini bs->sg = 0; 13906405875cSPaolo Bonzini bs->growable = 0; 1391de9c0cecSKevin Wolf QDECREF(bs->options); 1392de9c0cecSKevin Wolf bs->options = NULL; 1393b338082bSbellard 139466f82ceeSKevin Wolf if (bs->file != NULL) { 13950ac9377dSPaolo Bonzini bdrv_delete(bs->file); 13960ac9377dSPaolo Bonzini bs->file = NULL; 139766f82ceeSKevin Wolf } 13989ca11154SPavel Hrdina } 139966f82ceeSKevin Wolf 14007d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 140198f90dbaSZhi Yong Wu 140298f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 140398f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 140498f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 140598f90dbaSZhi Yong Wu } 1406b338082bSbellard } 1407b338082bSbellard 14082bc93fedSMORITA Kazutaka void bdrv_close_all(void) 14092bc93fedSMORITA Kazutaka { 14102bc93fedSMORITA Kazutaka BlockDriverState *bs; 14112bc93fedSMORITA Kazutaka 14122bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 14132bc93fedSMORITA Kazutaka bdrv_close(bs); 14142bc93fedSMORITA Kazutaka } 14152bc93fedSMORITA Kazutaka } 14162bc93fedSMORITA Kazutaka 1417922453bcSStefan Hajnoczi /* 1418922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 1419922453bcSStefan Hajnoczi * 1420922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 1421922453bcSStefan Hajnoczi * after calling this function. 14224c355d53SZhi Yong Wu * 14234c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 14244c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 14254c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 14264c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 14274c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 1428922453bcSStefan Hajnoczi */ 1429922453bcSStefan Hajnoczi void bdrv_drain_all(void) 1430922453bcSStefan Hajnoczi { 1431922453bcSStefan Hajnoczi BlockDriverState *bs; 14324c355d53SZhi Yong Wu bool busy; 1433922453bcSStefan Hajnoczi 14344c355d53SZhi Yong Wu do { 14354c355d53SZhi Yong Wu busy = qemu_aio_wait(); 14364c355d53SZhi Yong Wu 14374c355d53SZhi Yong Wu /* FIXME: We do not have timer support here, so this is effectively 14384c355d53SZhi Yong Wu * a busy wait. 14394c355d53SZhi Yong Wu */ 14404c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 14414c355d53SZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 14424c355d53SZhi Yong Wu qemu_co_queue_restart_all(&bs->throttled_reqs); 14434c355d53SZhi Yong Wu busy = true; 14444c355d53SZhi Yong Wu } 14454c355d53SZhi Yong Wu } 14464c355d53SZhi Yong Wu } while (busy); 1447922453bcSStefan Hajnoczi 1448922453bcSStefan Hajnoczi /* If requests are still pending there is a bug somewhere */ 1449922453bcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1450922453bcSStefan Hajnoczi assert(QLIST_EMPTY(&bs->tracked_requests)); 1451922453bcSStefan Hajnoczi assert(qemu_co_queue_empty(&bs->throttled_reqs)); 1452922453bcSStefan Hajnoczi } 1453922453bcSStefan Hajnoczi } 1454922453bcSStefan Hajnoczi 1455d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 1456d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1457d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1458d22b2f41SRyan Harper { 1459d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 1460d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 1461d22b2f41SRyan Harper } 1462d22b2f41SRyan Harper bs->device_name[0] = '\0'; 1463d22b2f41SRyan Harper } 1464d22b2f41SRyan Harper 1465e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1466e023b2e2SPaolo Bonzini { 1467e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1468e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1469e023b2e2SPaolo Bonzini } 1470e023b2e2SPaolo Bonzini } 1471e023b2e2SPaolo Bonzini 14724ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 14734ddc07caSPaolo Bonzini BlockDriverState *bs_src) 14744ddc07caSPaolo Bonzini { 14754ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 14764ddc07caSPaolo Bonzini bs_dest->open_flags = bs_src->open_flags; 14774ddc07caSPaolo Bonzini 14784ddc07caSPaolo Bonzini /* dev info */ 14794ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 14804ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 14814ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 14824ddc07caSPaolo Bonzini bs_dest->buffer_alignment = bs_src->buffer_alignment; 14834ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 14844ddc07caSPaolo Bonzini 14854ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 14864ddc07caSPaolo Bonzini 14874ddc07caSPaolo Bonzini /* i/o timing parameters */ 14884ddc07caSPaolo Bonzini bs_dest->slice_start = bs_src->slice_start; 14894ddc07caSPaolo Bonzini bs_dest->slice_end = bs_src->slice_end; 14905905fbc9SStefan Hajnoczi bs_dest->slice_submitted = bs_src->slice_submitted; 14914ddc07caSPaolo Bonzini bs_dest->io_limits = bs_src->io_limits; 14924ddc07caSPaolo Bonzini bs_dest->throttled_reqs = bs_src->throttled_reqs; 14934ddc07caSPaolo Bonzini bs_dest->block_timer = bs_src->block_timer; 14944ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 14954ddc07caSPaolo Bonzini 14964ddc07caSPaolo Bonzini /* r/w error */ 14974ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 14984ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 14994ddc07caSPaolo Bonzini 15004ddc07caSPaolo Bonzini /* i/o status */ 15014ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 15024ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 15034ddc07caSPaolo Bonzini 15044ddc07caSPaolo Bonzini /* dirty bitmap */ 15054ddc07caSPaolo Bonzini bs_dest->dirty_bitmap = bs_src->dirty_bitmap; 15064ddc07caSPaolo Bonzini 15074ddc07caSPaolo Bonzini /* job */ 15084ddc07caSPaolo Bonzini bs_dest->in_use = bs_src->in_use; 15094ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 15104ddc07caSPaolo Bonzini 15114ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 15124ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 15134ddc07caSPaolo Bonzini bs_src->device_name); 15144ddc07caSPaolo Bonzini bs_dest->list = bs_src->list; 15154ddc07caSPaolo Bonzini } 15164ddc07caSPaolo Bonzini 15174ddc07caSPaolo Bonzini /* 15184ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 15194ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 15204ddc07caSPaolo Bonzini * actually attached to a device. 15214ddc07caSPaolo Bonzini * 15224ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 15234ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 15244ddc07caSPaolo Bonzini * 15254ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 15264ddc07caSPaolo Bonzini * 15274ddc07caSPaolo Bonzini * This function does not create any image files. 15284ddc07caSPaolo Bonzini */ 15294ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 15304ddc07caSPaolo Bonzini { 15314ddc07caSPaolo Bonzini BlockDriverState tmp; 15324ddc07caSPaolo Bonzini 15334ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 15344ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 15354ddc07caSPaolo Bonzini assert(bs_new->dirty_bitmap == NULL); 15364ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 15374ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 15384ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 15394ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 15404ddc07caSPaolo Bonzini assert(bs_new->block_timer == NULL); 15414ddc07caSPaolo Bonzini 15424ddc07caSPaolo Bonzini tmp = *bs_new; 15434ddc07caSPaolo Bonzini *bs_new = *bs_old; 15444ddc07caSPaolo Bonzini *bs_old = tmp; 15454ddc07caSPaolo Bonzini 15464ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 15474ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 15484ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 15494ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 15504ddc07caSPaolo Bonzini 15514ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 15524ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 15534ddc07caSPaolo Bonzini 15544ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 15554ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 15564ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 15574ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 15584ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 15594ddc07caSPaolo Bonzini assert(bs_new->block_timer == NULL); 15604ddc07caSPaolo Bonzini 15614ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 15624ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 15634ddc07caSPaolo Bonzini } 15644ddc07caSPaolo Bonzini 15658802d1fdSJeff Cody /* 15668802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 15678802d1fdSJeff Cody * live, while keeping required fields on the top layer. 15688802d1fdSJeff Cody * 15698802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 15708802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 15718802d1fdSJeff Cody * 1572f6801b83SJeff Cody * bs_new is required to be anonymous. 1573f6801b83SJeff Cody * 15748802d1fdSJeff Cody * This function does not create any image files. 15758802d1fdSJeff Cody */ 15768802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 15778802d1fdSJeff Cody { 15784ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 15798802d1fdSJeff Cody 15808802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 15818802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 15824ddc07caSPaolo Bonzini bs_top->backing_hd = bs_new; 15834ddc07caSPaolo Bonzini bs_top->open_flags &= ~BDRV_O_NO_BACKING; 15844ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 15854ddc07caSPaolo Bonzini bs_new->filename); 15864ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 15874ddc07caSPaolo Bonzini bs_new->drv ? bs_new->drv->format_name : ""); 15888802d1fdSJeff Cody } 15898802d1fdSJeff Cody 1590b338082bSbellard void bdrv_delete(BlockDriverState *bs) 1591b338082bSbellard { 1592fa879d62SMarkus Armbruster assert(!bs->dev); 15933e914655SPaolo Bonzini assert(!bs->job); 15943e914655SPaolo Bonzini assert(!bs->in_use); 159518846deeSMarkus Armbruster 15961b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1597d22b2f41SRyan Harper bdrv_make_anon(bs); 159834c6f050Saurel32 1599b338082bSbellard bdrv_close(bs); 160066f82ceeSKevin Wolf 16017267c094SAnthony Liguori g_free(bs); 1602fc01f7e7Sbellard } 1603fc01f7e7Sbellard 1604fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1605fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 160618846deeSMarkus Armbruster { 1607fa879d62SMarkus Armbruster if (bs->dev) { 160818846deeSMarkus Armbruster return -EBUSY; 160918846deeSMarkus Armbruster } 1610fa879d62SMarkus Armbruster bs->dev = dev; 161128a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 161218846deeSMarkus Armbruster return 0; 161318846deeSMarkus Armbruster } 161418846deeSMarkus Armbruster 1615fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1616fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 161718846deeSMarkus Armbruster { 1618fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1619fa879d62SMarkus Armbruster abort(); 1620fa879d62SMarkus Armbruster } 1621fa879d62SMarkus Armbruster } 1622fa879d62SMarkus Armbruster 1623fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1624fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1625fa879d62SMarkus Armbruster { 1626fa879d62SMarkus Armbruster assert(bs->dev == dev); 1627fa879d62SMarkus Armbruster bs->dev = NULL; 16280e49de52SMarkus Armbruster bs->dev_ops = NULL; 16290e49de52SMarkus Armbruster bs->dev_opaque = NULL; 163029e05f20SMarkus Armbruster bs->buffer_alignment = 512; 163118846deeSMarkus Armbruster } 163218846deeSMarkus Armbruster 1633fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1634fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 163518846deeSMarkus Armbruster { 1636fa879d62SMarkus Armbruster return bs->dev; 163718846deeSMarkus Armbruster } 163818846deeSMarkus Armbruster 16390e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 16400e49de52SMarkus Armbruster void *opaque) 16410e49de52SMarkus Armbruster { 16420e49de52SMarkus Armbruster bs->dev_ops = ops; 16430e49de52SMarkus Armbruster bs->dev_opaque = opaque; 16440e49de52SMarkus Armbruster } 16450e49de52SMarkus Armbruster 164632c81a4aSPaolo Bonzini void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 164732c81a4aSPaolo Bonzini enum MonitorEvent ev, 16481ceee0d5SPaolo Bonzini BlockErrorAction action, bool is_read) 1649329c0a48SLuiz Capitulino { 1650329c0a48SLuiz Capitulino QObject *data; 1651329c0a48SLuiz Capitulino const char *action_str; 1652329c0a48SLuiz Capitulino 1653329c0a48SLuiz Capitulino switch (action) { 1654329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1655329c0a48SLuiz Capitulino action_str = "report"; 1656329c0a48SLuiz Capitulino break; 1657329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1658329c0a48SLuiz Capitulino action_str = "ignore"; 1659329c0a48SLuiz Capitulino break; 1660329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1661329c0a48SLuiz Capitulino action_str = "stop"; 1662329c0a48SLuiz Capitulino break; 1663329c0a48SLuiz Capitulino default: 1664329c0a48SLuiz Capitulino abort(); 1665329c0a48SLuiz Capitulino } 1666329c0a48SLuiz Capitulino 1667329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1668329c0a48SLuiz Capitulino bdrv->device_name, 1669329c0a48SLuiz Capitulino action_str, 1670329c0a48SLuiz Capitulino is_read ? "read" : "write"); 167132c81a4aSPaolo Bonzini monitor_protocol_event(ev, data); 1672329c0a48SLuiz Capitulino 1673329c0a48SLuiz Capitulino qobject_decref(data); 1674329c0a48SLuiz Capitulino } 1675329c0a48SLuiz Capitulino 16766f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 16776f382ed2SLuiz Capitulino { 16786f382ed2SLuiz Capitulino QObject *data; 16796f382ed2SLuiz Capitulino 16806f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 16816f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 16826f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 16836f382ed2SLuiz Capitulino 16846f382ed2SLuiz Capitulino qobject_decref(data); 16856f382ed2SLuiz Capitulino } 16866f382ed2SLuiz Capitulino 16877d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 16880e49de52SMarkus Armbruster { 1689145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 16906f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 16917d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 16926f382ed2SLuiz Capitulino if (tray_was_closed) { 16936f382ed2SLuiz Capitulino /* tray open */ 16946f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 16956f382ed2SLuiz Capitulino } 16966f382ed2SLuiz Capitulino if (load) { 16976f382ed2SLuiz Capitulino /* tray close */ 16986f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 16996f382ed2SLuiz Capitulino } 1700145feb17SMarkus Armbruster } 1701145feb17SMarkus Armbruster } 1702145feb17SMarkus Armbruster 17032c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 17042c6942faSMarkus Armbruster { 17052c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 17062c6942faSMarkus Armbruster } 17072c6942faSMarkus Armbruster 1708025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1709025ccaa7SPaolo Bonzini { 1710025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1711025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1712025ccaa7SPaolo Bonzini } 1713025ccaa7SPaolo Bonzini } 1714025ccaa7SPaolo Bonzini 1715e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1716e4def80bSMarkus Armbruster { 1717e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1718e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1719e4def80bSMarkus Armbruster } 1720e4def80bSMarkus Armbruster return false; 1721e4def80bSMarkus Armbruster } 1722e4def80bSMarkus Armbruster 1723145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1724145feb17SMarkus Armbruster { 1725145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1726145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 17270e49de52SMarkus Armbruster } 17280e49de52SMarkus Armbruster } 17290e49de52SMarkus Armbruster 1730f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1731f107639aSMarkus Armbruster { 1732f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1733f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1734f107639aSMarkus Armbruster } 1735f107639aSMarkus Armbruster return false; 1736f107639aSMarkus Armbruster } 1737f107639aSMarkus Armbruster 1738e97fc193Saliguori /* 1739e97fc193Saliguori * Run consistency checks on an image 1740e97fc193Saliguori * 1741e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1742a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1743e076f338SKevin Wolf * check are stored in res. 1744e97fc193Saliguori */ 17454534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 1746e97fc193Saliguori { 1747e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1748e97fc193Saliguori return -ENOTSUP; 1749e97fc193Saliguori } 1750e97fc193Saliguori 1751e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 17524534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 1753e97fc193Saliguori } 1754e97fc193Saliguori 17558a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 17568a426614SKevin Wolf 175733e3963eSbellard /* commit COW file into the raw image */ 175833e3963eSbellard int bdrv_commit(BlockDriverState *bs) 175933e3963eSbellard { 176019cb3738Sbellard BlockDriver *drv = bs->drv; 17618a426614SKevin Wolf int64_t sector, total_sectors; 17628a426614SKevin Wolf int n, ro, open_flags; 17630bce597dSJeff Cody int ret = 0; 17648a426614SKevin Wolf uint8_t *buf; 1765c2cba3d9SJim Meyering char filename[PATH_MAX]; 176633e3963eSbellard 176719cb3738Sbellard if (!drv) 176819cb3738Sbellard return -ENOMEDIUM; 176933e3963eSbellard 17704dca4b63SNaphtali Sprei if (!bs->backing_hd) { 17714dca4b63SNaphtali Sprei return -ENOTSUP; 17724dca4b63SNaphtali Sprei } 17734dca4b63SNaphtali Sprei 17742d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 17752d3735d3SStefan Hajnoczi return -EBUSY; 17762d3735d3SStefan Hajnoczi } 17772d3735d3SStefan Hajnoczi 17784dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 1779c2cba3d9SJim Meyering /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ 1780c2cba3d9SJim Meyering pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); 17814dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 17824dca4b63SNaphtali Sprei 17834dca4b63SNaphtali Sprei if (ro) { 17840bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 17850bce597dSJeff Cody return -EACCES; 17864dca4b63SNaphtali Sprei } 1787ea2384d3Sbellard } 1788ea2384d3Sbellard 17896ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 17907267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 17918a426614SKevin Wolf 17928a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 179305c4af54SStefan Hajnoczi if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 17948a426614SKevin Wolf 17958a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 17964dca4b63SNaphtali Sprei ret = -EIO; 17974dca4b63SNaphtali Sprei goto ro_cleanup; 179833e3963eSbellard } 179933e3963eSbellard 18008a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 18014dca4b63SNaphtali Sprei ret = -EIO; 18024dca4b63SNaphtali Sprei goto ro_cleanup; 180333e3963eSbellard } 180433e3963eSbellard } 180533e3963eSbellard } 180695389c86Sbellard 18071d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 18081d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 18091d44952fSChristoph Hellwig bdrv_flush(bs); 18101d44952fSChristoph Hellwig } 181195389c86Sbellard 18123f5075aeSChristoph Hellwig /* 18133f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 18143f5075aeSChristoph Hellwig * stable on disk. 18153f5075aeSChristoph Hellwig */ 18163f5075aeSChristoph Hellwig if (bs->backing_hd) 18173f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 18184dca4b63SNaphtali Sprei 18194dca4b63SNaphtali Sprei ro_cleanup: 18207267c094SAnthony Liguori g_free(buf); 18214dca4b63SNaphtali Sprei 18224dca4b63SNaphtali Sprei if (ro) { 18230bce597dSJeff Cody /* ignoring error return here */ 18240bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 18254dca4b63SNaphtali Sprei } 18264dca4b63SNaphtali Sprei 18271d44952fSChristoph Hellwig return ret; 182833e3963eSbellard } 182933e3963eSbellard 1830e8877497SStefan Hajnoczi int bdrv_commit_all(void) 18316ab4b5abSMarkus Armbruster { 18326ab4b5abSMarkus Armbruster BlockDriverState *bs; 18336ab4b5abSMarkus Armbruster 18346ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1835272d2d8eSJeff Cody if (bs->drv && bs->backing_hd) { 1836e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1837e8877497SStefan Hajnoczi if (ret < 0) { 1838e8877497SStefan Hajnoczi return ret; 18396ab4b5abSMarkus Armbruster } 18406ab4b5abSMarkus Armbruster } 1841272d2d8eSJeff Cody } 1842e8877497SStefan Hajnoczi return 0; 1843e8877497SStefan Hajnoczi } 18446ab4b5abSMarkus Armbruster 1845dbffbdcfSStefan Hajnoczi /** 1846dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1847dbffbdcfSStefan Hajnoczi * 1848dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1849dbffbdcfSStefan Hajnoczi */ 1850dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1851dbffbdcfSStefan Hajnoczi { 1852dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1853f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1854dbffbdcfSStefan Hajnoczi } 1855dbffbdcfSStefan Hajnoczi 1856dbffbdcfSStefan Hajnoczi /** 1857dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1858dbffbdcfSStefan Hajnoczi */ 1859dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1860dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1861dbffbdcfSStefan Hajnoczi int64_t sector_num, 1862dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1863dbffbdcfSStefan Hajnoczi { 1864dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1865dbffbdcfSStefan Hajnoczi .bs = bs, 1866dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1867dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1868dbffbdcfSStefan Hajnoczi .is_write = is_write, 18695f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1870dbffbdcfSStefan Hajnoczi }; 1871dbffbdcfSStefan Hajnoczi 1872f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1873f4658285SStefan Hajnoczi 1874dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1875dbffbdcfSStefan Hajnoczi } 1876dbffbdcfSStefan Hajnoczi 1877d83947acSStefan Hajnoczi /** 1878d83947acSStefan Hajnoczi * Round a region to cluster boundaries 1879d83947acSStefan Hajnoczi */ 1880343bded4SPaolo Bonzini void bdrv_round_to_clusters(BlockDriverState *bs, 1881d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 1882d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 1883d83947acSStefan Hajnoczi int *cluster_nb_sectors) 1884d83947acSStefan Hajnoczi { 1885d83947acSStefan Hajnoczi BlockDriverInfo bdi; 1886d83947acSStefan Hajnoczi 1887d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 1888d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 1889d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 1890d83947acSStefan Hajnoczi } else { 1891d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 1892d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 1893d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 1894d83947acSStefan Hajnoczi nb_sectors, c); 1895d83947acSStefan Hajnoczi } 1896d83947acSStefan Hajnoczi } 1897d83947acSStefan Hajnoczi 1898f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 1899f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 1900d83947acSStefan Hajnoczi /* aaaa bbbb */ 1901d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 1902d83947acSStefan Hajnoczi return false; 1903d83947acSStefan Hajnoczi } 1904d83947acSStefan Hajnoczi /* bbbb aaaa */ 1905d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 1906d83947acSStefan Hajnoczi return false; 1907d83947acSStefan Hajnoczi } 1908d83947acSStefan Hajnoczi return true; 1909f4658285SStefan Hajnoczi } 1910f4658285SStefan Hajnoczi 1911f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 1912f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 1913f4658285SStefan Hajnoczi { 1914f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 1915d83947acSStefan Hajnoczi int64_t cluster_sector_num; 1916d83947acSStefan Hajnoczi int cluster_nb_sectors; 1917f4658285SStefan Hajnoczi bool retry; 1918f4658285SStefan Hajnoczi 1919d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 1920d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 1921d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 1922d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 1923d83947acSStefan Hajnoczi * interleave between them. 1924d83947acSStefan Hajnoczi */ 1925343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 1926d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1927d83947acSStefan Hajnoczi 1928f4658285SStefan Hajnoczi do { 1929f4658285SStefan Hajnoczi retry = false; 1930f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 1931d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 1932d83947acSStefan Hajnoczi cluster_nb_sectors)) { 19335f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 19345f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 19355f8b6491SStefan Hajnoczi * never happen since it means deadlock. 19365f8b6491SStefan Hajnoczi */ 19375f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 19385f8b6491SStefan Hajnoczi 1939f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 1940f4658285SStefan Hajnoczi retry = true; 1941f4658285SStefan Hajnoczi break; 1942f4658285SStefan Hajnoczi } 1943f4658285SStefan Hajnoczi } 1944f4658285SStefan Hajnoczi } while (retry); 1945f4658285SStefan Hajnoczi } 1946f4658285SStefan Hajnoczi 1947756e6736SKevin Wolf /* 1948756e6736SKevin Wolf * Return values: 1949756e6736SKevin Wolf * 0 - success 1950756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 1951756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 1952756e6736SKevin Wolf * image file header 1953756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 1954756e6736SKevin Wolf */ 1955756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 1956756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1957756e6736SKevin Wolf { 1958756e6736SKevin Wolf BlockDriver *drv = bs->drv; 1959469ef350SPaolo Bonzini int ret; 1960756e6736SKevin Wolf 19615f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 19625f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 19635f377794SPaolo Bonzini return -EINVAL; 19645f377794SPaolo Bonzini } 19655f377794SPaolo Bonzini 1966756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 1967469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 1968756e6736SKevin Wolf } else { 1969469ef350SPaolo Bonzini ret = -ENOTSUP; 1970756e6736SKevin Wolf } 1971469ef350SPaolo Bonzini 1972469ef350SPaolo Bonzini if (ret == 0) { 1973469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1974469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1975469ef350SPaolo Bonzini } 1976469ef350SPaolo Bonzini return ret; 1977756e6736SKevin Wolf } 1978756e6736SKevin Wolf 19796ebdcee2SJeff Cody /* 19806ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 19816ebdcee2SJeff Cody * 19826ebdcee2SJeff Cody * active is the current topmost image. 19836ebdcee2SJeff Cody * 19846ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 19856ebdcee2SJeff Cody * or if active == bs. 19866ebdcee2SJeff Cody */ 19876ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 19886ebdcee2SJeff Cody BlockDriverState *bs) 19896ebdcee2SJeff Cody { 19906ebdcee2SJeff Cody BlockDriverState *overlay = NULL; 19916ebdcee2SJeff Cody BlockDriverState *intermediate; 19926ebdcee2SJeff Cody 19936ebdcee2SJeff Cody assert(active != NULL); 19946ebdcee2SJeff Cody assert(bs != NULL); 19956ebdcee2SJeff Cody 19966ebdcee2SJeff Cody /* if bs is the same as active, then by definition it has no overlay 19976ebdcee2SJeff Cody */ 19986ebdcee2SJeff Cody if (active == bs) { 19996ebdcee2SJeff Cody return NULL; 20006ebdcee2SJeff Cody } 20016ebdcee2SJeff Cody 20026ebdcee2SJeff Cody intermediate = active; 20036ebdcee2SJeff Cody while (intermediate->backing_hd) { 20046ebdcee2SJeff Cody if (intermediate->backing_hd == bs) { 20056ebdcee2SJeff Cody overlay = intermediate; 20066ebdcee2SJeff Cody break; 20076ebdcee2SJeff Cody } 20086ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 20096ebdcee2SJeff Cody } 20106ebdcee2SJeff Cody 20116ebdcee2SJeff Cody return overlay; 20126ebdcee2SJeff Cody } 20136ebdcee2SJeff Cody 20146ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 20156ebdcee2SJeff Cody BlockDriverState *bs; 20166ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 20176ebdcee2SJeff Cody } BlkIntermediateStates; 20186ebdcee2SJeff Cody 20196ebdcee2SJeff Cody 20206ebdcee2SJeff Cody /* 20216ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 20226ebdcee2SJeff Cody * above 'top' to have base as its backing file. 20236ebdcee2SJeff Cody * 20246ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 20256ebdcee2SJeff Cody * information in 'bs' can be properly updated. 20266ebdcee2SJeff Cody * 20276ebdcee2SJeff Cody * E.g., this will convert the following chain: 20286ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 20296ebdcee2SJeff Cody * 20306ebdcee2SJeff Cody * to 20316ebdcee2SJeff Cody * 20326ebdcee2SJeff Cody * bottom <- base <- active 20336ebdcee2SJeff Cody * 20346ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 20356ebdcee2SJeff Cody * 20366ebdcee2SJeff Cody * base <- intermediate <- top <- active 20376ebdcee2SJeff Cody * 20386ebdcee2SJeff Cody * to 20396ebdcee2SJeff Cody * 20406ebdcee2SJeff Cody * base <- active 20416ebdcee2SJeff Cody * 20426ebdcee2SJeff Cody * Error conditions: 20436ebdcee2SJeff Cody * if active == top, that is considered an error 20446ebdcee2SJeff Cody * 20456ebdcee2SJeff Cody */ 20466ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 20476ebdcee2SJeff Cody BlockDriverState *base) 20486ebdcee2SJeff Cody { 20496ebdcee2SJeff Cody BlockDriverState *intermediate; 20506ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 20516ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 20526ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 20536ebdcee2SJeff Cody int ret = -EIO; 20546ebdcee2SJeff Cody 20556ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 20566ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 20576ebdcee2SJeff Cody 20586ebdcee2SJeff Cody if (!top->drv || !base->drv) { 20596ebdcee2SJeff Cody goto exit; 20606ebdcee2SJeff Cody } 20616ebdcee2SJeff Cody 20626ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 20636ebdcee2SJeff Cody 20646ebdcee2SJeff Cody if (new_top_bs == NULL) { 20656ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 20666ebdcee2SJeff Cody goto exit; 20676ebdcee2SJeff Cody } 20686ebdcee2SJeff Cody 20696ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 20706ebdcee2SJeff Cody * to do, no intermediate images */ 20716ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 20726ebdcee2SJeff Cody ret = 0; 20736ebdcee2SJeff Cody goto exit; 20746ebdcee2SJeff Cody } 20756ebdcee2SJeff Cody 20766ebdcee2SJeff Cody intermediate = top; 20776ebdcee2SJeff Cody 20786ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 20796ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 20806ebdcee2SJeff Cody */ 20816ebdcee2SJeff Cody while (intermediate) { 20826ebdcee2SJeff Cody intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); 20836ebdcee2SJeff Cody intermediate_state->bs = intermediate; 20846ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 20856ebdcee2SJeff Cody 20866ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 20876ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 20886ebdcee2SJeff Cody break; 20896ebdcee2SJeff Cody } 20906ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 20916ebdcee2SJeff Cody } 20926ebdcee2SJeff Cody if (base_bs == NULL) { 20936ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 20946ebdcee2SJeff Cody * unravel everything, and exit with error */ 20956ebdcee2SJeff Cody goto exit; 20966ebdcee2SJeff Cody } 20976ebdcee2SJeff Cody 20986ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 20996ebdcee2SJeff Cody ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, 21006ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 21016ebdcee2SJeff Cody if (ret) { 21026ebdcee2SJeff Cody goto exit; 21036ebdcee2SJeff Cody } 21046ebdcee2SJeff Cody new_top_bs->backing_hd = base_bs; 21056ebdcee2SJeff Cody 21066ebdcee2SJeff Cody 21076ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 21086ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 21096ebdcee2SJeff Cody intermediate_state->bs->backing_hd = NULL; 21106ebdcee2SJeff Cody bdrv_delete(intermediate_state->bs); 21116ebdcee2SJeff Cody } 21126ebdcee2SJeff Cody ret = 0; 21136ebdcee2SJeff Cody 21146ebdcee2SJeff Cody exit: 21156ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 21166ebdcee2SJeff Cody g_free(intermediate_state); 21176ebdcee2SJeff Cody } 21186ebdcee2SJeff Cody return ret; 21196ebdcee2SJeff Cody } 21206ebdcee2SJeff Cody 21216ebdcee2SJeff Cody 212271d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 212371d0770cSaliguori size_t size) 212471d0770cSaliguori { 212571d0770cSaliguori int64_t len; 212671d0770cSaliguori 212771d0770cSaliguori if (!bdrv_is_inserted(bs)) 212871d0770cSaliguori return -ENOMEDIUM; 212971d0770cSaliguori 213071d0770cSaliguori if (bs->growable) 213171d0770cSaliguori return 0; 213271d0770cSaliguori 213371d0770cSaliguori len = bdrv_getlength(bs); 213471d0770cSaliguori 2135fbb7b4e0SKevin Wolf if (offset < 0) 2136fbb7b4e0SKevin Wolf return -EIO; 2137fbb7b4e0SKevin Wolf 2138fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 213971d0770cSaliguori return -EIO; 214071d0770cSaliguori 214171d0770cSaliguori return 0; 214271d0770cSaliguori } 214371d0770cSaliguori 214471d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 214571d0770cSaliguori int nb_sectors) 214671d0770cSaliguori { 2147eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 2148eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 214971d0770cSaliguori } 215071d0770cSaliguori 21511c9805a3SStefan Hajnoczi typedef struct RwCo { 21521c9805a3SStefan Hajnoczi BlockDriverState *bs; 21531c9805a3SStefan Hajnoczi int64_t sector_num; 21541c9805a3SStefan Hajnoczi int nb_sectors; 21551c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 21561c9805a3SStefan Hajnoczi bool is_write; 21571c9805a3SStefan Hajnoczi int ret; 21581c9805a3SStefan Hajnoczi } RwCo; 21591c9805a3SStefan Hajnoczi 21601c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 2161fc01f7e7Sbellard { 21621c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 2163fc01f7e7Sbellard 21641c9805a3SStefan Hajnoczi if (!rwco->is_write) { 21651c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 2166470c0504SStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 21671c9805a3SStefan Hajnoczi } else { 21681c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 2169f08f2ddaSStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 21701c9805a3SStefan Hajnoczi } 21711c9805a3SStefan Hajnoczi } 2172e7a8a783SKevin Wolf 21731c9805a3SStefan Hajnoczi /* 21748d3b1a2dSKevin Wolf * Process a vectored synchronous request using coroutines 21751c9805a3SStefan Hajnoczi */ 21768d3b1a2dSKevin Wolf static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, 21778d3b1a2dSKevin Wolf QEMUIOVector *qiov, bool is_write) 21781c9805a3SStefan Hajnoczi { 21791c9805a3SStefan Hajnoczi Coroutine *co; 21801c9805a3SStefan Hajnoczi RwCo rwco = { 21811c9805a3SStefan Hajnoczi .bs = bs, 21821c9805a3SStefan Hajnoczi .sector_num = sector_num, 21838d3b1a2dSKevin Wolf .nb_sectors = qiov->size >> BDRV_SECTOR_BITS, 21848d3b1a2dSKevin Wolf .qiov = qiov, 21851c9805a3SStefan Hajnoczi .is_write = is_write, 21861c9805a3SStefan Hajnoczi .ret = NOT_DONE, 21871c9805a3SStefan Hajnoczi }; 21888d3b1a2dSKevin Wolf assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0); 21891c9805a3SStefan Hajnoczi 2190498e386cSZhi Yong Wu /** 2191498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 2192498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 2193498e386cSZhi Yong Wu * if it has been enabled. 2194498e386cSZhi Yong Wu */ 2195498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 2196498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 2197498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 2198498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 2199498e386cSZhi Yong Wu } 2200498e386cSZhi Yong Wu 22011c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 22021c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 22031c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 22041c9805a3SStefan Hajnoczi } else { 22051c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 22061c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 22071c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 22081c9805a3SStefan Hajnoczi qemu_aio_wait(); 22091c9805a3SStefan Hajnoczi } 22101c9805a3SStefan Hajnoczi } 22111c9805a3SStefan Hajnoczi return rwco.ret; 2212e7a8a783SKevin Wolf } 2213e7a8a783SKevin Wolf 22148d3b1a2dSKevin Wolf /* 22158d3b1a2dSKevin Wolf * Process a synchronous request using coroutines 22168d3b1a2dSKevin Wolf */ 22178d3b1a2dSKevin Wolf static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 22188d3b1a2dSKevin Wolf int nb_sectors, bool is_write) 22198d3b1a2dSKevin Wolf { 22208d3b1a2dSKevin Wolf QEMUIOVector qiov; 22218d3b1a2dSKevin Wolf struct iovec iov = { 22228d3b1a2dSKevin Wolf .iov_base = (void *)buf, 22238d3b1a2dSKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 22248d3b1a2dSKevin Wolf }; 22258d3b1a2dSKevin Wolf 22268d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 22278d3b1a2dSKevin Wolf return bdrv_rwv_co(bs, sector_num, &qiov, is_write); 22288d3b1a2dSKevin Wolf } 22298d3b1a2dSKevin Wolf 22301c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 22311c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 22321c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 22331c9805a3SStefan Hajnoczi { 22341c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); 223583f64091Sbellard } 2236fc01f7e7Sbellard 223707d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 223807d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 223907d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 224007d27a44SMarkus Armbruster { 224107d27a44SMarkus Armbruster bool enabled; 224207d27a44SMarkus Armbruster int ret; 224307d27a44SMarkus Armbruster 224407d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 224507d27a44SMarkus Armbruster bs->io_limits_enabled = false; 224607d27a44SMarkus Armbruster ret = bdrv_read(bs, 0, buf, 1); 224707d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 224807d27a44SMarkus Armbruster return ret; 224907d27a44SMarkus Armbruster } 225007d27a44SMarkus Armbruster 225119cb3738Sbellard /* Return < 0 if error. Important errors are: 225219cb3738Sbellard -EIO generic I/O error (may happen for all errors) 225319cb3738Sbellard -ENOMEDIUM No media inserted. 225419cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 225519cb3738Sbellard -EACCES Trying to write a read-only device 225619cb3738Sbellard */ 2257fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 2258fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 2259fc01f7e7Sbellard { 22601c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); 226183f64091Sbellard } 226283f64091Sbellard 22638d3b1a2dSKevin Wolf int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov) 22648d3b1a2dSKevin Wolf { 22658d3b1a2dSKevin Wolf return bdrv_rwv_co(bs, sector_num, qiov, true); 22668d3b1a2dSKevin Wolf } 22678d3b1a2dSKevin Wolf 2268eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 2269eda578e5Saliguori void *buf, int count1) 227083f64091Sbellard { 22716ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 227283f64091Sbellard int len, nb_sectors, count; 227383f64091Sbellard int64_t sector_num; 22749a8c4cceSKevin Wolf int ret; 227583f64091Sbellard 227683f64091Sbellard count = count1; 227783f64091Sbellard /* first read to align to sector start */ 22786ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 227983f64091Sbellard if (len > count) 228083f64091Sbellard len = count; 22816ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 228283f64091Sbellard if (len > 0) { 22839a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 22849a8c4cceSKevin Wolf return ret; 22856ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 228683f64091Sbellard count -= len; 228783f64091Sbellard if (count == 0) 228883f64091Sbellard return count1; 228983f64091Sbellard sector_num++; 229083f64091Sbellard buf += len; 229183f64091Sbellard } 229283f64091Sbellard 229383f64091Sbellard /* read the sectors "in place" */ 22946ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 229583f64091Sbellard if (nb_sectors > 0) { 22969a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 22979a8c4cceSKevin Wolf return ret; 229883f64091Sbellard sector_num += nb_sectors; 22996ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 230083f64091Sbellard buf += len; 230183f64091Sbellard count -= len; 230283f64091Sbellard } 230383f64091Sbellard 230483f64091Sbellard /* add data from the last sector */ 230583f64091Sbellard if (count > 0) { 23069a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 23079a8c4cceSKevin Wolf return ret; 230883f64091Sbellard memcpy(buf, tmp_buf, count); 230983f64091Sbellard } 231083f64091Sbellard return count1; 231183f64091Sbellard } 231283f64091Sbellard 23138d3b1a2dSKevin Wolf int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov) 231483f64091Sbellard { 23156ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 231683f64091Sbellard int len, nb_sectors, count; 231783f64091Sbellard int64_t sector_num; 23189a8c4cceSKevin Wolf int ret; 231983f64091Sbellard 23208d3b1a2dSKevin Wolf count = qiov->size; 23218d3b1a2dSKevin Wolf 232283f64091Sbellard /* first write to align to sector start */ 23236ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 232483f64091Sbellard if (len > count) 232583f64091Sbellard len = count; 23266ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 232783f64091Sbellard if (len > 0) { 23289a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 23299a8c4cceSKevin Wolf return ret; 23308d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), 23318d3b1a2dSKevin Wolf len); 23329a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 23339a8c4cceSKevin Wolf return ret; 233483f64091Sbellard count -= len; 233583f64091Sbellard if (count == 0) 23368d3b1a2dSKevin Wolf return qiov->size; 233783f64091Sbellard sector_num++; 233883f64091Sbellard } 233983f64091Sbellard 234083f64091Sbellard /* write the sectors "in place" */ 23416ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 234283f64091Sbellard if (nb_sectors > 0) { 23438d3b1a2dSKevin Wolf QEMUIOVector qiov_inplace; 23448d3b1a2dSKevin Wolf 23458d3b1a2dSKevin Wolf qemu_iovec_init(&qiov_inplace, qiov->niov); 23468d3b1a2dSKevin Wolf qemu_iovec_concat(&qiov_inplace, qiov, len, 23478d3b1a2dSKevin Wolf nb_sectors << BDRV_SECTOR_BITS); 23488d3b1a2dSKevin Wolf ret = bdrv_writev(bs, sector_num, &qiov_inplace); 23498d3b1a2dSKevin Wolf qemu_iovec_destroy(&qiov_inplace); 23508d3b1a2dSKevin Wolf if (ret < 0) { 23519a8c4cceSKevin Wolf return ret; 23528d3b1a2dSKevin Wolf } 23538d3b1a2dSKevin Wolf 235483f64091Sbellard sector_num += nb_sectors; 23556ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 235683f64091Sbellard count -= len; 235783f64091Sbellard } 235883f64091Sbellard 235983f64091Sbellard /* add data from the last sector */ 236083f64091Sbellard if (count > 0) { 23619a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 23629a8c4cceSKevin Wolf return ret; 23638d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count); 23649a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 23659a8c4cceSKevin Wolf return ret; 236683f64091Sbellard } 23678d3b1a2dSKevin Wolf return qiov->size; 23688d3b1a2dSKevin Wolf } 23698d3b1a2dSKevin Wolf 23708d3b1a2dSKevin Wolf int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 23718d3b1a2dSKevin Wolf const void *buf, int count1) 23728d3b1a2dSKevin Wolf { 23738d3b1a2dSKevin Wolf QEMUIOVector qiov; 23748d3b1a2dSKevin Wolf struct iovec iov = { 23758d3b1a2dSKevin Wolf .iov_base = (void *) buf, 23768d3b1a2dSKevin Wolf .iov_len = count1, 23778d3b1a2dSKevin Wolf }; 23788d3b1a2dSKevin Wolf 23798d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 23808d3b1a2dSKevin Wolf return bdrv_pwritev(bs, offset, &qiov); 238183f64091Sbellard } 238283f64091Sbellard 2383f08145feSKevin Wolf /* 2384f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 2385f08145feSKevin Wolf * request (acts as a barrier) 2386f08145feSKevin Wolf * 2387f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 2388f08145feSKevin Wolf */ 2389f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 2390f08145feSKevin Wolf const void *buf, int count) 2391f08145feSKevin Wolf { 2392f08145feSKevin Wolf int ret; 2393f08145feSKevin Wolf 2394f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 2395f08145feSKevin Wolf if (ret < 0) { 2396f08145feSKevin Wolf return ret; 2397f08145feSKevin Wolf } 2398f08145feSKevin Wolf 2399f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 2400f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 2401f08145feSKevin Wolf bdrv_flush(bs); 2402f08145feSKevin Wolf } 2403f08145feSKevin Wolf 2404f08145feSKevin Wolf return 0; 2405f08145feSKevin Wolf } 2406f08145feSKevin Wolf 2407470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 2408ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2409ab185921SStefan Hajnoczi { 2410ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 2411ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 2412ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 2413ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 2414ab185921SStefan Hajnoczi */ 2415ab185921SStefan Hajnoczi void *bounce_buffer; 2416ab185921SStefan Hajnoczi 241779c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 2418ab185921SStefan Hajnoczi struct iovec iov; 2419ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 2420ab185921SStefan Hajnoczi int64_t cluster_sector_num; 2421ab185921SStefan Hajnoczi int cluster_nb_sectors; 2422ab185921SStefan Hajnoczi size_t skip_bytes; 2423ab185921SStefan Hajnoczi int ret; 2424ab185921SStefan Hajnoczi 2425ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 2426ab185921SStefan Hajnoczi * allocating cluster in the image file. 2427ab185921SStefan Hajnoczi */ 2428343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2429ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2430ab185921SStefan Hajnoczi 2431470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 2432ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 2433ab185921SStefan Hajnoczi 2434ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 2435ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 2436ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 2437ab185921SStefan Hajnoczi 243879c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 2439ab185921SStefan Hajnoczi &bounce_qiov); 2440ab185921SStefan Hajnoczi if (ret < 0) { 2441ab185921SStefan Hajnoczi goto err; 2442ab185921SStefan Hajnoczi } 2443ab185921SStefan Hajnoczi 244479c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 244579c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 2446621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 244779c053bdSStefan Hajnoczi cluster_nb_sectors); 244879c053bdSStefan Hajnoczi } else { 2449f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 2450f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 2451f05fa4adSPaolo Bonzini */ 245279c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 2453ab185921SStefan Hajnoczi &bounce_qiov); 245479c053bdSStefan Hajnoczi } 245579c053bdSStefan Hajnoczi 2456ab185921SStefan Hajnoczi if (ret < 0) { 2457ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 2458ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 2459ab185921SStefan Hajnoczi * Simply report it in all cases. 2460ab185921SStefan Hajnoczi */ 2461ab185921SStefan Hajnoczi goto err; 2462ab185921SStefan Hajnoczi } 2463ab185921SStefan Hajnoczi 2464ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 246503396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 2466ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 2467ab185921SStefan Hajnoczi 2468ab185921SStefan Hajnoczi err: 2469ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 2470ab185921SStefan Hajnoczi return ret; 2471ab185921SStefan Hajnoczi } 2472ab185921SStefan Hajnoczi 2473c5fbe571SStefan Hajnoczi /* 2474c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 2475c5fbe571SStefan Hajnoczi */ 2476c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 2477470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2478470c0504SStefan Hajnoczi BdrvRequestFlags flags) 2479da1fa91dSKevin Wolf { 2480da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 2481dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 2482dbffbdcfSStefan Hajnoczi int ret; 2483da1fa91dSKevin Wolf 2484da1fa91dSKevin Wolf if (!drv) { 2485da1fa91dSKevin Wolf return -ENOMEDIUM; 2486da1fa91dSKevin Wolf } 2487da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2488da1fa91dSKevin Wolf return -EIO; 2489da1fa91dSKevin Wolf } 2490da1fa91dSKevin Wolf 249198f90dbaSZhi Yong Wu /* throttling disk read I/O */ 249298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 249398f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, false, nb_sectors); 249498f90dbaSZhi Yong Wu } 249598f90dbaSZhi Yong Wu 2496f4658285SStefan Hajnoczi if (bs->copy_on_read) { 2497470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 2498470c0504SStefan Hajnoczi } 2499470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2500470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 2501470c0504SStefan Hajnoczi } 2502470c0504SStefan Hajnoczi 2503470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2504f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2505f4658285SStefan Hajnoczi } 2506f4658285SStefan Hajnoczi 2507dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 2508ab185921SStefan Hajnoczi 2509470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2510ab185921SStefan Hajnoczi int pnum; 2511ab185921SStefan Hajnoczi 2512ab185921SStefan Hajnoczi ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); 2513ab185921SStefan Hajnoczi if (ret < 0) { 2514ab185921SStefan Hajnoczi goto out; 2515ab185921SStefan Hajnoczi } 2516ab185921SStefan Hajnoczi 2517ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 2518470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 2519ab185921SStefan Hajnoczi goto out; 2520ab185921SStefan Hajnoczi } 2521ab185921SStefan Hajnoczi } 2522ab185921SStefan Hajnoczi 2523dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 2524ab185921SStefan Hajnoczi 2525ab185921SStefan Hajnoczi out: 2526dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2527470c0504SStefan Hajnoczi 2528470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2529470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 2530470c0504SStefan Hajnoczi } 2531470c0504SStefan Hajnoczi 2532dbffbdcfSStefan Hajnoczi return ret; 2533da1fa91dSKevin Wolf } 2534da1fa91dSKevin Wolf 2535c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 2536da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 2537da1fa91dSKevin Wolf { 2538c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 2539da1fa91dSKevin Wolf 2540470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 2541470c0504SStefan Hajnoczi } 2542470c0504SStefan Hajnoczi 2543470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 2544470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2545470c0504SStefan Hajnoczi { 2546470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 2547470c0504SStefan Hajnoczi 2548470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 2549470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 2550c5fbe571SStefan Hajnoczi } 2551c5fbe571SStefan Hajnoczi 2552f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 2553f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 2554f08f2ddaSStefan Hajnoczi { 2555f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 2556f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 2557f08f2ddaSStefan Hajnoczi struct iovec iov; 2558f08f2ddaSStefan Hajnoczi int ret; 2559f08f2ddaSStefan Hajnoczi 2560621f0589SKevin Wolf /* TODO Emulate only part of misaligned requests instead of letting block 2561621f0589SKevin Wolf * drivers return -ENOTSUP and emulate everything */ 2562621f0589SKevin Wolf 2563f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 2564f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 2565621f0589SKevin Wolf ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 2566621f0589SKevin Wolf if (ret != -ENOTSUP) { 2567621f0589SKevin Wolf return ret; 2568621f0589SKevin Wolf } 2569f08f2ddaSStefan Hajnoczi } 2570f08f2ddaSStefan Hajnoczi 2571f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 2572f08f2ddaSStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2573f08f2ddaSStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 2574f08f2ddaSStefan Hajnoczi memset(iov.iov_base, 0, iov.iov_len); 2575f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 2576f08f2ddaSStefan Hajnoczi 2577f08f2ddaSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 2578f08f2ddaSStefan Hajnoczi 2579f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 2580f08f2ddaSStefan Hajnoczi return ret; 2581f08f2ddaSStefan Hajnoczi } 2582f08f2ddaSStefan Hajnoczi 2583c5fbe571SStefan Hajnoczi /* 2584c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 2585c5fbe571SStefan Hajnoczi */ 2586c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 2587f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2588f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 2589c5fbe571SStefan Hajnoczi { 2590c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 2591dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 25926b7cb247SStefan Hajnoczi int ret; 2593da1fa91dSKevin Wolf 2594da1fa91dSKevin Wolf if (!bs->drv) { 2595da1fa91dSKevin Wolf return -ENOMEDIUM; 2596da1fa91dSKevin Wolf } 2597da1fa91dSKevin Wolf if (bs->read_only) { 2598da1fa91dSKevin Wolf return -EACCES; 2599da1fa91dSKevin Wolf } 2600da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2601da1fa91dSKevin Wolf return -EIO; 2602da1fa91dSKevin Wolf } 2603da1fa91dSKevin Wolf 260498f90dbaSZhi Yong Wu /* throttling disk write I/O */ 260598f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 260698f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, true, nb_sectors); 260798f90dbaSZhi Yong Wu } 260898f90dbaSZhi Yong Wu 2609470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2610f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2611f4658285SStefan Hajnoczi } 2612f4658285SStefan Hajnoczi 2613dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 2614dbffbdcfSStefan Hajnoczi 2615d616b224SStefan Hajnoczi ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); 2616d616b224SStefan Hajnoczi 2617d616b224SStefan Hajnoczi if (ret < 0) { 2618d616b224SStefan Hajnoczi /* Do nothing, write notifier decided to fail this request */ 2619d616b224SStefan Hajnoczi } else if (flags & BDRV_REQ_ZERO_WRITE) { 2620f08f2ddaSStefan Hajnoczi ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); 2621f08f2ddaSStefan Hajnoczi } else { 26226b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 2623f08f2ddaSStefan Hajnoczi } 26246b7cb247SStefan Hajnoczi 2625f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 2626f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 2627f05fa4adSPaolo Bonzini } 2628f05fa4adSPaolo Bonzini 2629da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 26301755da16SPaolo Bonzini bdrv_set_dirty(bs, sector_num, nb_sectors); 2631da1fa91dSKevin Wolf } 2632da1fa91dSKevin Wolf 2633da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2634da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2635da1fa91dSKevin Wolf } 2636da1fa91dSKevin Wolf 2637dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2638dbffbdcfSStefan Hajnoczi 26396b7cb247SStefan Hajnoczi return ret; 2640da1fa91dSKevin Wolf } 2641da1fa91dSKevin Wolf 2642c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 2643c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 2644c5fbe571SStefan Hajnoczi { 2645c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 2646c5fbe571SStefan Hajnoczi 2647f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 2648f08f2ddaSStefan Hajnoczi } 2649f08f2ddaSStefan Hajnoczi 2650f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 2651f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 2652f08f2ddaSStefan Hajnoczi { 2653f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 2654f08f2ddaSStefan Hajnoczi 2655f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 2656f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE); 2657c5fbe571SStefan Hajnoczi } 2658c5fbe571SStefan Hajnoczi 265983f64091Sbellard /** 266083f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 266183f64091Sbellard */ 266283f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 266383f64091Sbellard { 266483f64091Sbellard BlockDriver *drv = bs->drv; 266551762288SStefan Hajnoczi int ret; 266683f64091Sbellard if (!drv) 266719cb3738Sbellard return -ENOMEDIUM; 266883f64091Sbellard if (!drv->bdrv_truncate) 266983f64091Sbellard return -ENOTSUP; 267059f2689dSNaphtali Sprei if (bs->read_only) 267159f2689dSNaphtali Sprei return -EACCES; 26728591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 26738591675fSMarcelo Tosatti return -EBUSY; 267451762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 267551762288SStefan Hajnoczi if (ret == 0) { 267651762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2677145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 267851762288SStefan Hajnoczi } 267951762288SStefan Hajnoczi return ret; 268083f64091Sbellard } 268183f64091Sbellard 268283f64091Sbellard /** 26834a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 26844a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 26854a1d5e1fSFam Zheng */ 26864a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 26874a1d5e1fSFam Zheng { 26884a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 26894a1d5e1fSFam Zheng if (!drv) { 26904a1d5e1fSFam Zheng return -ENOMEDIUM; 26914a1d5e1fSFam Zheng } 26924a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 26934a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 26944a1d5e1fSFam Zheng } 26954a1d5e1fSFam Zheng if (bs->file) { 26964a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 26974a1d5e1fSFam Zheng } 26984a1d5e1fSFam Zheng return -ENOTSUP; 26994a1d5e1fSFam Zheng } 27004a1d5e1fSFam Zheng 27014a1d5e1fSFam Zheng /** 270283f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 270383f64091Sbellard */ 270483f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 270583f64091Sbellard { 270683f64091Sbellard BlockDriver *drv = bs->drv; 270783f64091Sbellard if (!drv) 270819cb3738Sbellard return -ENOMEDIUM; 270951762288SStefan Hajnoczi 27102c6942faSMarkus Armbruster if (bs->growable || bdrv_dev_has_removable_media(bs)) { 271146a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 271283f64091Sbellard return drv->bdrv_getlength(bs); 2713fc01f7e7Sbellard } 271446a4e4e6SStefan Hajnoczi } 271546a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 271646a4e4e6SStefan Hajnoczi } 2717fc01f7e7Sbellard 271819cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 271996b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2720fc01f7e7Sbellard { 272119cb3738Sbellard int64_t length; 272219cb3738Sbellard length = bdrv_getlength(bs); 272319cb3738Sbellard if (length < 0) 272419cb3738Sbellard length = 0; 272519cb3738Sbellard else 27266ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 272719cb3738Sbellard *nb_sectors_ptr = length; 2728fc01f7e7Sbellard } 2729cf98951bSbellard 27300563e191SZhi Yong Wu /* throttling disk io limits */ 27310563e191SZhi Yong Wu void bdrv_set_io_limits(BlockDriverState *bs, 27320563e191SZhi Yong Wu BlockIOLimit *io_limits) 27330563e191SZhi Yong Wu { 27340563e191SZhi Yong Wu bs->io_limits = *io_limits; 27350563e191SZhi Yong Wu bs->io_limits_enabled = bdrv_io_limits_enabled(bs); 27360563e191SZhi Yong Wu } 27370563e191SZhi Yong Wu 2738ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 2739ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 2740abd7f68dSMarkus Armbruster { 2741abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2742abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2743abd7f68dSMarkus Armbruster } 2744abd7f68dSMarkus Armbruster 27451ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 2746abd7f68dSMarkus Armbruster { 2747abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2748abd7f68dSMarkus Armbruster } 2749abd7f68dSMarkus Armbruster 27503e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 27513e1caa5fSPaolo Bonzini { 27523e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 27533e1caa5fSPaolo Bonzini 27543e1caa5fSPaolo Bonzini switch (on_err) { 27553e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 27563e1caa5fSPaolo Bonzini return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; 27573e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 27583e1caa5fSPaolo Bonzini return BDRV_ACTION_STOP; 27593e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 27603e1caa5fSPaolo Bonzini return BDRV_ACTION_REPORT; 27613e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 27623e1caa5fSPaolo Bonzini return BDRV_ACTION_IGNORE; 27633e1caa5fSPaolo Bonzini default: 27643e1caa5fSPaolo Bonzini abort(); 27653e1caa5fSPaolo Bonzini } 27663e1caa5fSPaolo Bonzini } 27673e1caa5fSPaolo Bonzini 27683e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 27693e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 27703e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 27713e1caa5fSPaolo Bonzini */ 27723e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 27733e1caa5fSPaolo Bonzini bool is_read, int error) 27743e1caa5fSPaolo Bonzini { 27753e1caa5fSPaolo Bonzini assert(error >= 0); 277632c81a4aSPaolo Bonzini bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); 27773e1caa5fSPaolo Bonzini if (action == BDRV_ACTION_STOP) { 27783e1caa5fSPaolo Bonzini vm_stop(RUN_STATE_IO_ERROR); 27793e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 27803e1caa5fSPaolo Bonzini } 27813e1caa5fSPaolo Bonzini } 27823e1caa5fSPaolo Bonzini 2783b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2784b338082bSbellard { 2785b338082bSbellard return bs->read_only; 2786b338082bSbellard } 2787b338082bSbellard 2788985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2789985a03b0Sths { 2790985a03b0Sths return bs->sg; 2791985a03b0Sths } 2792985a03b0Sths 2793e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2794e900a7b7SChristoph Hellwig { 2795e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2796e900a7b7SChristoph Hellwig } 2797e900a7b7SChristoph Hellwig 2798425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 2799425b0148SPaolo Bonzini { 2800425b0148SPaolo Bonzini bs->enable_write_cache = wce; 280155b110f2SJeff Cody 280255b110f2SJeff Cody /* so a reopen() will preserve wce */ 280355b110f2SJeff Cody if (wce) { 280455b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 280555b110f2SJeff Cody } else { 280655b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 280755b110f2SJeff Cody } 2808425b0148SPaolo Bonzini } 2809425b0148SPaolo Bonzini 2810ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2811ea2384d3Sbellard { 2812ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2813ea2384d3Sbellard return 1; 2814ea2384d3Sbellard return bs->encrypted; 2815ea2384d3Sbellard } 2816ea2384d3Sbellard 2817c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2818c0f4ce77Saliguori { 2819c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2820c0f4ce77Saliguori 2821c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2822c0f4ce77Saliguori return 1; 2823c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2824c0f4ce77Saliguori } 2825c0f4ce77Saliguori 2826ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2827ea2384d3Sbellard { 2828ea2384d3Sbellard int ret; 2829ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2830ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2831ea2384d3Sbellard if (ret < 0) 2832ea2384d3Sbellard return ret; 2833ea2384d3Sbellard if (!bs->encrypted) 2834ea2384d3Sbellard return 0; 2835ea2384d3Sbellard } 2836fd04a2aeSShahar Havivi if (!bs->encrypted) { 2837fd04a2aeSShahar Havivi return -EINVAL; 2838fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2839fd04a2aeSShahar Havivi return -ENOMEDIUM; 2840fd04a2aeSShahar Havivi } 2841c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2842bb5fc20fSaliguori if (ret < 0) { 2843bb5fc20fSaliguori bs->valid_key = 0; 2844bb5fc20fSaliguori } else if (!bs->valid_key) { 2845bb5fc20fSaliguori bs->valid_key = 1; 2846bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 28477d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 2848bb5fc20fSaliguori } 2849c0f4ce77Saliguori return ret; 2850ea2384d3Sbellard } 2851ea2384d3Sbellard 2852f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 2853ea2384d3Sbellard { 2854f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 2855ea2384d3Sbellard } 2856ea2384d3Sbellard 2857ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2858ea2384d3Sbellard void *opaque) 2859ea2384d3Sbellard { 2860ea2384d3Sbellard BlockDriver *drv; 2861ea2384d3Sbellard 28628a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2863ea2384d3Sbellard it(opaque, drv->format_name); 2864ea2384d3Sbellard } 2865ea2384d3Sbellard } 2866ea2384d3Sbellard 2867b338082bSbellard BlockDriverState *bdrv_find(const char *name) 2868b338082bSbellard { 2869b338082bSbellard BlockDriverState *bs; 2870b338082bSbellard 28711b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 28721b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 2873b338082bSbellard return bs; 2874b338082bSbellard } 28751b7bdbc1SStefan Hajnoczi } 2876b338082bSbellard return NULL; 2877b338082bSbellard } 2878b338082bSbellard 28792f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 28802f399b0aSMarkus Armbruster { 28812f399b0aSMarkus Armbruster if (!bs) { 28822f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 28832f399b0aSMarkus Armbruster } 28842f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 28852f399b0aSMarkus Armbruster } 28862f399b0aSMarkus Armbruster 288751de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 288881d0912dSbellard { 288981d0912dSbellard BlockDriverState *bs; 289081d0912dSbellard 28911b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 289251de9760Saliguori it(opaque, bs); 289381d0912dSbellard } 289481d0912dSbellard } 289581d0912dSbellard 2896ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 2897ea2384d3Sbellard { 2898ea2384d3Sbellard return bs->device_name; 2899ea2384d3Sbellard } 2900ea2384d3Sbellard 2901c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 2902c8433287SMarkus Armbruster { 2903c8433287SMarkus Armbruster return bs->open_flags; 2904c8433287SMarkus Armbruster } 2905c8433287SMarkus Armbruster 2906c6ca28d6Saliguori void bdrv_flush_all(void) 2907c6ca28d6Saliguori { 2908c6ca28d6Saliguori BlockDriverState *bs; 2909c6ca28d6Saliguori 29101b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2911c6ca28d6Saliguori bdrv_flush(bs); 2912c6ca28d6Saliguori } 29131b7bdbc1SStefan Hajnoczi } 2914c6ca28d6Saliguori 29153ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 29163ac21627SPeter Lieven { 29173ac21627SPeter Lieven return 1; 29183ac21627SPeter Lieven } 29193ac21627SPeter Lieven 2920f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2921f2feebbdSKevin Wolf { 2922f2feebbdSKevin Wolf assert(bs->drv); 2923f2feebbdSKevin Wolf 2924336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2925336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2926f2feebbdSKevin Wolf } 2927f2feebbdSKevin Wolf 29283ac21627SPeter Lieven /* safe default */ 29293ac21627SPeter Lieven return 0; 2930f2feebbdSKevin Wolf } 2931f2feebbdSKevin Wolf 2932376ae3f1SStefan Hajnoczi typedef struct BdrvCoIsAllocatedData { 2933376ae3f1SStefan Hajnoczi BlockDriverState *bs; 2934b35b2bbaSMiroslav Rezanina BlockDriverState *base; 2935376ae3f1SStefan Hajnoczi int64_t sector_num; 2936376ae3f1SStefan Hajnoczi int nb_sectors; 2937376ae3f1SStefan Hajnoczi int *pnum; 2938376ae3f1SStefan Hajnoczi int ret; 2939376ae3f1SStefan Hajnoczi bool done; 2940376ae3f1SStefan Hajnoczi } BdrvCoIsAllocatedData; 2941376ae3f1SStefan Hajnoczi 2942f58c7b35Sths /* 2943f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 2944f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 2945f58c7b35Sths * hence all their sectors are reported as allocated. 2946f58c7b35Sths * 2947bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 2948bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 2949bd9533e3SStefan Hajnoczi * 2950f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 2951f58c7b35Sths * the specified sector) that are known to be in the same 2952f58c7b35Sths * allocated/unallocated state. 2953f58c7b35Sths * 2954bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2955bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 2956f58c7b35Sths */ 2957060f51c9SStefan Hajnoczi int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, 2958060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 2959f58c7b35Sths { 2960f58c7b35Sths int64_t n; 2961bd9533e3SStefan Hajnoczi 29626aebab14SStefan Hajnoczi if (sector_num >= bs->total_sectors) { 29636aebab14SStefan Hajnoczi *pnum = 0; 29646aebab14SStefan Hajnoczi return 0; 29656aebab14SStefan Hajnoczi } 2966bd9533e3SStefan Hajnoczi 29676aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 2968bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 2969bd9533e3SStefan Hajnoczi nb_sectors = n; 2970bd9533e3SStefan Hajnoczi } 2971bd9533e3SStefan Hajnoczi 2972bd9533e3SStefan Hajnoczi if (!bs->drv->bdrv_co_is_allocated) { 2973bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 29746aebab14SStefan Hajnoczi return 1; 29756aebab14SStefan Hajnoczi } 29766aebab14SStefan Hajnoczi 2977060f51c9SStefan Hajnoczi return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum); 2978060f51c9SStefan Hajnoczi } 2979060f51c9SStefan Hajnoczi 2980060f51c9SStefan Hajnoczi /* Coroutine wrapper for bdrv_is_allocated() */ 2981060f51c9SStefan Hajnoczi static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) 2982060f51c9SStefan Hajnoczi { 2983060f51c9SStefan Hajnoczi BdrvCoIsAllocatedData *data = opaque; 2984060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 2985060f51c9SStefan Hajnoczi 2986060f51c9SStefan Hajnoczi data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, 2987060f51c9SStefan Hajnoczi data->pnum); 2988060f51c9SStefan Hajnoczi data->done = true; 2989060f51c9SStefan Hajnoczi } 2990060f51c9SStefan Hajnoczi 2991060f51c9SStefan Hajnoczi /* 2992060f51c9SStefan Hajnoczi * Synchronous wrapper around bdrv_co_is_allocated(). 2993060f51c9SStefan Hajnoczi * 2994060f51c9SStefan Hajnoczi * See bdrv_co_is_allocated() for details. 2995060f51c9SStefan Hajnoczi */ 2996060f51c9SStefan Hajnoczi int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 2997060f51c9SStefan Hajnoczi int *pnum) 2998060f51c9SStefan Hajnoczi { 2999376ae3f1SStefan Hajnoczi Coroutine *co; 3000376ae3f1SStefan Hajnoczi BdrvCoIsAllocatedData data = { 3001376ae3f1SStefan Hajnoczi .bs = bs, 3002376ae3f1SStefan Hajnoczi .sector_num = sector_num, 3003376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 3004376ae3f1SStefan Hajnoczi .pnum = pnum, 3005376ae3f1SStefan Hajnoczi .done = false, 3006376ae3f1SStefan Hajnoczi }; 3007376ae3f1SStefan Hajnoczi 3008376ae3f1SStefan Hajnoczi co = qemu_coroutine_create(bdrv_is_allocated_co_entry); 3009376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 3010376ae3f1SStefan Hajnoczi while (!data.done) { 3011376ae3f1SStefan Hajnoczi qemu_aio_wait(); 3012376ae3f1SStefan Hajnoczi } 3013376ae3f1SStefan Hajnoczi return data.ret; 3014376ae3f1SStefan Hajnoczi } 3015f58c7b35Sths 3016188a7bbfSPaolo Bonzini /* 3017188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 3018188a7bbfSPaolo Bonzini * 3019188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 3020188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 3021188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 3022188a7bbfSPaolo Bonzini * 3023188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 3024188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 3025188a7bbfSPaolo Bonzini * allocated/unallocated state. 3026188a7bbfSPaolo Bonzini * 3027188a7bbfSPaolo Bonzini */ 3028188a7bbfSPaolo Bonzini int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top, 3029188a7bbfSPaolo Bonzini BlockDriverState *base, 3030188a7bbfSPaolo Bonzini int64_t sector_num, 3031188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 3032188a7bbfSPaolo Bonzini { 3033188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 3034188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 3035188a7bbfSPaolo Bonzini 3036188a7bbfSPaolo Bonzini intermediate = top; 3037188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 3038188a7bbfSPaolo Bonzini int pnum_inter; 3039188a7bbfSPaolo Bonzini ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors, 3040188a7bbfSPaolo Bonzini &pnum_inter); 3041188a7bbfSPaolo Bonzini if (ret < 0) { 3042188a7bbfSPaolo Bonzini return ret; 3043188a7bbfSPaolo Bonzini } else if (ret) { 3044188a7bbfSPaolo Bonzini *pnum = pnum_inter; 3045188a7bbfSPaolo Bonzini return 1; 3046188a7bbfSPaolo Bonzini } 3047188a7bbfSPaolo Bonzini 3048188a7bbfSPaolo Bonzini /* 3049188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 3050188a7bbfSPaolo Bonzini * might have 3051188a7bbfSPaolo Bonzini * 3052188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 3053188a7bbfSPaolo Bonzini */ 305463ba17d3SVishvananda Ishaya if (n > pnum_inter && 305563ba17d3SVishvananda Ishaya (intermediate == top || 305663ba17d3SVishvananda Ishaya sector_num + pnum_inter < intermediate->total_sectors)) { 3057188a7bbfSPaolo Bonzini n = pnum_inter; 3058188a7bbfSPaolo Bonzini } 3059188a7bbfSPaolo Bonzini 3060188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 3061188a7bbfSPaolo Bonzini } 3062188a7bbfSPaolo Bonzini 3063188a7bbfSPaolo Bonzini *pnum = n; 3064188a7bbfSPaolo Bonzini return 0; 3065188a7bbfSPaolo Bonzini } 3066188a7bbfSPaolo Bonzini 3067b35b2bbaSMiroslav Rezanina /* Coroutine wrapper for bdrv_is_allocated_above() */ 3068b35b2bbaSMiroslav Rezanina static void coroutine_fn bdrv_is_allocated_above_co_entry(void *opaque) 3069b35b2bbaSMiroslav Rezanina { 3070b35b2bbaSMiroslav Rezanina BdrvCoIsAllocatedData *data = opaque; 3071b35b2bbaSMiroslav Rezanina BlockDriverState *top = data->bs; 3072b35b2bbaSMiroslav Rezanina BlockDriverState *base = data->base; 3073b35b2bbaSMiroslav Rezanina 3074b35b2bbaSMiroslav Rezanina data->ret = bdrv_co_is_allocated_above(top, base, data->sector_num, 3075b35b2bbaSMiroslav Rezanina data->nb_sectors, data->pnum); 3076b35b2bbaSMiroslav Rezanina data->done = true; 3077b35b2bbaSMiroslav Rezanina } 3078b35b2bbaSMiroslav Rezanina 3079b35b2bbaSMiroslav Rezanina /* 3080b35b2bbaSMiroslav Rezanina * Synchronous wrapper around bdrv_co_is_allocated_above(). 3081b35b2bbaSMiroslav Rezanina * 3082b35b2bbaSMiroslav Rezanina * See bdrv_co_is_allocated_above() for details. 3083b35b2bbaSMiroslav Rezanina */ 3084b35b2bbaSMiroslav Rezanina int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base, 3085b35b2bbaSMiroslav Rezanina int64_t sector_num, int nb_sectors, int *pnum) 3086b35b2bbaSMiroslav Rezanina { 3087b35b2bbaSMiroslav Rezanina Coroutine *co; 3088b35b2bbaSMiroslav Rezanina BdrvCoIsAllocatedData data = { 3089b35b2bbaSMiroslav Rezanina .bs = top, 3090b35b2bbaSMiroslav Rezanina .base = base, 3091b35b2bbaSMiroslav Rezanina .sector_num = sector_num, 3092b35b2bbaSMiroslav Rezanina .nb_sectors = nb_sectors, 3093b35b2bbaSMiroslav Rezanina .pnum = pnum, 3094b35b2bbaSMiroslav Rezanina .done = false, 3095b35b2bbaSMiroslav Rezanina }; 3096b35b2bbaSMiroslav Rezanina 3097b35b2bbaSMiroslav Rezanina co = qemu_coroutine_create(bdrv_is_allocated_above_co_entry); 3098b35b2bbaSMiroslav Rezanina qemu_coroutine_enter(co, &data); 3099b35b2bbaSMiroslav Rezanina while (!data.done) { 3100b35b2bbaSMiroslav Rezanina qemu_aio_wait(); 3101b35b2bbaSMiroslav Rezanina } 3102b35b2bbaSMiroslav Rezanina return data.ret; 3103b35b2bbaSMiroslav Rezanina } 3104b35b2bbaSMiroslav Rezanina 3105045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 3106045df330Saliguori { 3107045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 3108045df330Saliguori return bs->backing_file; 3109045df330Saliguori else if (bs->encrypted) 3110045df330Saliguori return bs->filename; 3111045df330Saliguori else 3112045df330Saliguori return NULL; 3113045df330Saliguori } 3114045df330Saliguori 311583f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 311683f64091Sbellard char *filename, int filename_size) 311783f64091Sbellard { 311883f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 311983f64091Sbellard } 312083f64091Sbellard 3121faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 3122faea38e7Sbellard const uint8_t *buf, int nb_sectors) 3123faea38e7Sbellard { 3124faea38e7Sbellard BlockDriver *drv = bs->drv; 3125faea38e7Sbellard if (!drv) 312619cb3738Sbellard return -ENOMEDIUM; 3127faea38e7Sbellard if (!drv->bdrv_write_compressed) 3128faea38e7Sbellard return -ENOTSUP; 3129fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 3130fbb7b4e0SKevin Wolf return -EIO; 31317cd1e32aSlirans@il.ibm.com 31321755da16SPaolo Bonzini assert(!bs->dirty_bitmap); 31337cd1e32aSlirans@il.ibm.com 3134faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 3135faea38e7Sbellard } 3136faea38e7Sbellard 3137faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 3138faea38e7Sbellard { 3139faea38e7Sbellard BlockDriver *drv = bs->drv; 3140faea38e7Sbellard if (!drv) 314119cb3738Sbellard return -ENOMEDIUM; 3142faea38e7Sbellard if (!drv->bdrv_get_info) 3143faea38e7Sbellard return -ENOTSUP; 3144faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 3145faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 3146faea38e7Sbellard } 3147faea38e7Sbellard 314845566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 314945566e9cSChristoph Hellwig int64_t pos, int size) 3150178e08a5Saliguori { 3151cf8074b3SKevin Wolf QEMUIOVector qiov; 3152cf8074b3SKevin Wolf struct iovec iov = { 3153cf8074b3SKevin Wolf .iov_base = (void *) buf, 3154cf8074b3SKevin Wolf .iov_len = size, 3155cf8074b3SKevin Wolf }; 3156cf8074b3SKevin Wolf 3157cf8074b3SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 3158cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs, &qiov, pos); 3159cf8074b3SKevin Wolf } 3160cf8074b3SKevin Wolf 3161cf8074b3SKevin Wolf int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) 3162cf8074b3SKevin Wolf { 3163178e08a5Saliguori BlockDriver *drv = bs->drv; 3164cf8074b3SKevin Wolf 3165cf8074b3SKevin Wolf if (!drv) { 3166178e08a5Saliguori return -ENOMEDIUM; 3167cf8074b3SKevin Wolf } else if (drv->bdrv_save_vmstate) { 3168cf8074b3SKevin Wolf return drv->bdrv_save_vmstate(bs, qiov, pos); 3169cf8074b3SKevin Wolf } else if (bs->file) { 3170cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs->file, qiov, pos); 3171cf8074b3SKevin Wolf } 3172cf8074b3SKevin Wolf 31737cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3174178e08a5Saliguori } 3175178e08a5Saliguori 317645566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 317745566e9cSChristoph Hellwig int64_t pos, int size) 3178178e08a5Saliguori { 3179178e08a5Saliguori BlockDriver *drv = bs->drv; 3180178e08a5Saliguori if (!drv) 3181178e08a5Saliguori return -ENOMEDIUM; 31827cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 318345566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 31847cdb1f6dSMORITA Kazutaka if (bs->file) 31857cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 31867cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3187178e08a5Saliguori } 3188178e08a5Saliguori 31898b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 31908b9b0cc2SKevin Wolf { 3191bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 31928b9b0cc2SKevin Wolf return; 31938b9b0cc2SKevin Wolf } 31948b9b0cc2SKevin Wolf 3195bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 319641c695c7SKevin Wolf } 31978b9b0cc2SKevin Wolf 319841c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 319941c695c7SKevin Wolf const char *tag) 320041c695c7SKevin Wolf { 320141c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 320241c695c7SKevin Wolf bs = bs->file; 320341c695c7SKevin Wolf } 320441c695c7SKevin Wolf 320541c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 320641c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 320741c695c7SKevin Wolf } 320841c695c7SKevin Wolf 320941c695c7SKevin Wolf return -ENOTSUP; 321041c695c7SKevin Wolf } 321141c695c7SKevin Wolf 321241c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 321341c695c7SKevin Wolf { 321441c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_resume) { 321541c695c7SKevin Wolf bs = bs->file; 321641c695c7SKevin Wolf } 321741c695c7SKevin Wolf 321841c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 321941c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 322041c695c7SKevin Wolf } 322141c695c7SKevin Wolf 322241c695c7SKevin Wolf return -ENOTSUP; 322341c695c7SKevin Wolf } 322441c695c7SKevin Wolf 322541c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 322641c695c7SKevin Wolf { 322741c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 322841c695c7SKevin Wolf bs = bs->file; 322941c695c7SKevin Wolf } 323041c695c7SKevin Wolf 323141c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 323241c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 323341c695c7SKevin Wolf } 323441c695c7SKevin Wolf 323541c695c7SKevin Wolf return false; 32368b9b0cc2SKevin Wolf } 32378b9b0cc2SKevin Wolf 3238199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 3239199630b6SBlue Swirl { 3240199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 3241199630b6SBlue Swirl } 3242199630b6SBlue Swirl 3243b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 3244b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 3245b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 3246b1b1d783SJeff Cody * the CWD rather than the chain. */ 3247e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 3248e8a6bb9cSMarcelo Tosatti const char *backing_file) 3249e8a6bb9cSMarcelo Tosatti { 3250b1b1d783SJeff Cody char *filename_full = NULL; 3251b1b1d783SJeff Cody char *backing_file_full = NULL; 3252b1b1d783SJeff Cody char *filename_tmp = NULL; 3253b1b1d783SJeff Cody int is_protocol = 0; 3254b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 3255b1b1d783SJeff Cody BlockDriverState *retval = NULL; 3256b1b1d783SJeff Cody 3257b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 3258e8a6bb9cSMarcelo Tosatti return NULL; 3259e8a6bb9cSMarcelo Tosatti } 3260e8a6bb9cSMarcelo Tosatti 3261b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 3262b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 3263b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 3264b1b1d783SJeff Cody 3265b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 3266b1b1d783SJeff Cody 3267b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 3268b1b1d783SJeff Cody 3269b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 3270b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 3271b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 3272b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 3273b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3274b1b1d783SJeff Cody break; 3275b1b1d783SJeff Cody } 3276e8a6bb9cSMarcelo Tosatti } else { 3277b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 3278b1b1d783SJeff Cody * image's filename path */ 3279b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3280b1b1d783SJeff Cody backing_file); 3281b1b1d783SJeff Cody 3282b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 3283b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 3284b1b1d783SJeff Cody continue; 3285b1b1d783SJeff Cody } 3286b1b1d783SJeff Cody 3287b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 3288b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 3289b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3290b1b1d783SJeff Cody curr_bs->backing_file); 3291b1b1d783SJeff Cody 3292b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 3293b1b1d783SJeff Cody continue; 3294b1b1d783SJeff Cody } 3295b1b1d783SJeff Cody 3296b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 3297b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3298b1b1d783SJeff Cody break; 3299b1b1d783SJeff Cody } 3300e8a6bb9cSMarcelo Tosatti } 3301e8a6bb9cSMarcelo Tosatti } 3302e8a6bb9cSMarcelo Tosatti 3303b1b1d783SJeff Cody g_free(filename_full); 3304b1b1d783SJeff Cody g_free(backing_file_full); 3305b1b1d783SJeff Cody g_free(filename_tmp); 3306b1b1d783SJeff Cody return retval; 3307e8a6bb9cSMarcelo Tosatti } 3308e8a6bb9cSMarcelo Tosatti 3309f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 3310f198fd1cSBenoît Canet { 3311f198fd1cSBenoît Canet if (!bs->drv) { 3312f198fd1cSBenoît Canet return 0; 3313f198fd1cSBenoît Canet } 3314f198fd1cSBenoît Canet 3315f198fd1cSBenoît Canet if (!bs->backing_hd) { 3316f198fd1cSBenoît Canet return 0; 3317f198fd1cSBenoît Canet } 3318f198fd1cSBenoît Canet 3319f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 3320f198fd1cSBenoît Canet } 3321f198fd1cSBenoît Canet 332279fac568SJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 332379fac568SJeff Cody { 332479fac568SJeff Cody BlockDriverState *curr_bs = NULL; 332579fac568SJeff Cody 332679fac568SJeff Cody if (!bs) { 332779fac568SJeff Cody return NULL; 332879fac568SJeff Cody } 332979fac568SJeff Cody 333079fac568SJeff Cody curr_bs = bs; 333179fac568SJeff Cody 333279fac568SJeff Cody while (curr_bs->backing_hd) { 333379fac568SJeff Cody curr_bs = curr_bs->backing_hd; 333479fac568SJeff Cody } 333579fac568SJeff Cody return curr_bs; 333679fac568SJeff Cody } 333779fac568SJeff Cody 3338ea2384d3Sbellard /**************************************************************/ 333983f64091Sbellard /* async I/Os */ 3340ea2384d3Sbellard 33413b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 3342f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 334383f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 3344ea2384d3Sbellard { 3345bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 3346bbf0a440SStefan Hajnoczi 3347b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 33488c5873d6SStefan Hajnoczi cb, opaque, false); 334983f64091Sbellard } 335083f64091Sbellard 3351f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 3352f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 335383f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 33547674e7bfSbellard { 3355bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 3356bbf0a440SStefan Hajnoczi 33571a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 33588c5873d6SStefan Hajnoczi cb, opaque, true); 335983f64091Sbellard } 336083f64091Sbellard 336140b4f539SKevin Wolf 336240b4f539SKevin Wolf typedef struct MultiwriteCB { 336340b4f539SKevin Wolf int error; 336440b4f539SKevin Wolf int num_requests; 336540b4f539SKevin Wolf int num_callbacks; 336640b4f539SKevin Wolf struct { 336740b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 336840b4f539SKevin Wolf void *opaque; 336940b4f539SKevin Wolf QEMUIOVector *free_qiov; 337040b4f539SKevin Wolf } callbacks[]; 337140b4f539SKevin Wolf } MultiwriteCB; 337240b4f539SKevin Wolf 337340b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 337440b4f539SKevin Wolf { 337540b4f539SKevin Wolf int i; 337640b4f539SKevin Wolf 337740b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 337840b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 33791e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 33801e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 33811e1ea48dSStefan Hajnoczi } 33827267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 338340b4f539SKevin Wolf } 338440b4f539SKevin Wolf } 338540b4f539SKevin Wolf 338640b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 338740b4f539SKevin Wolf { 338840b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 338940b4f539SKevin Wolf 33906d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 33916d519a5fSStefan Hajnoczi 3392cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 339340b4f539SKevin Wolf mcb->error = ret; 339440b4f539SKevin Wolf } 339540b4f539SKevin Wolf 339640b4f539SKevin Wolf mcb->num_requests--; 339740b4f539SKevin Wolf if (mcb->num_requests == 0) { 339840b4f539SKevin Wolf multiwrite_user_cb(mcb); 33997267c094SAnthony Liguori g_free(mcb); 340040b4f539SKevin Wolf } 340140b4f539SKevin Wolf } 340240b4f539SKevin Wolf 340340b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 340440b4f539SKevin Wolf { 340577be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 340677be4366SChristoph Hellwig 340777be4366SChristoph Hellwig /* 340877be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 340977be4366SChristoph Hellwig * here as that could overflow the return value. 341077be4366SChristoph Hellwig */ 341177be4366SChristoph Hellwig if (req1->sector > req2->sector) { 341277be4366SChristoph Hellwig return 1; 341377be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 341477be4366SChristoph Hellwig return -1; 341577be4366SChristoph Hellwig } else { 341677be4366SChristoph Hellwig return 0; 341777be4366SChristoph Hellwig } 341840b4f539SKevin Wolf } 341940b4f539SKevin Wolf 342040b4f539SKevin Wolf /* 342140b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 342240b4f539SKevin Wolf * requests that remain after merging. 342340b4f539SKevin Wolf */ 342440b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 342540b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 342640b4f539SKevin Wolf { 342740b4f539SKevin Wolf int i, outidx; 342840b4f539SKevin Wolf 342940b4f539SKevin Wolf // Sort requests by start sector 343040b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 343140b4f539SKevin Wolf 343240b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 343340b4f539SKevin Wolf // filling up gaps with zero sectors. 343440b4f539SKevin Wolf outidx = 0; 343540b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 343640b4f539SKevin Wolf int merge = 0; 343740b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 343840b4f539SKevin Wolf 3439b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 344040b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 344140b4f539SKevin Wolf merge = 1; 344240b4f539SKevin Wolf } 344340b4f539SKevin Wolf 3444e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3445e2a305fbSChristoph Hellwig merge = 0; 3446e2a305fbSChristoph Hellwig } 3447e2a305fbSChristoph Hellwig 344840b4f539SKevin Wolf if (merge) { 344940b4f539SKevin Wolf size_t size; 34507267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 345140b4f539SKevin Wolf qemu_iovec_init(qiov, 345240b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 345340b4f539SKevin Wolf 345440b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 345540b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 345640b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 34571b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 345840b4f539SKevin Wolf 3459b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3460b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 346140b4f539SKevin Wolf 346240b4f539SKevin Wolf // Add the second request 34631b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 346440b4f539SKevin Wolf 3465cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 346640b4f539SKevin Wolf reqs[outidx].qiov = qiov; 346740b4f539SKevin Wolf 346840b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 346940b4f539SKevin Wolf } else { 347040b4f539SKevin Wolf outidx++; 347140b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 347240b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 347340b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 347440b4f539SKevin Wolf } 347540b4f539SKevin Wolf } 347640b4f539SKevin Wolf 347740b4f539SKevin Wolf return outidx + 1; 347840b4f539SKevin Wolf } 347940b4f539SKevin Wolf 348040b4f539SKevin Wolf /* 348140b4f539SKevin Wolf * Submit multiple AIO write requests at once. 348240b4f539SKevin Wolf * 348340b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 348440b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 348540b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 348640b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 348740b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 348840b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 348940b4f539SKevin Wolf * 349040b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 349140b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 349240b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 349340b4f539SKevin Wolf */ 349440b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 349540b4f539SKevin Wolf { 349640b4f539SKevin Wolf MultiwriteCB *mcb; 349740b4f539SKevin Wolf int i; 349840b4f539SKevin Wolf 3499301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3500301db7c2SRyan Harper if (bs->drv == NULL) { 3501301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3502301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3503301db7c2SRyan Harper } 3504301db7c2SRyan Harper return -1; 3505301db7c2SRyan Harper } 3506301db7c2SRyan Harper 350740b4f539SKevin Wolf if (num_reqs == 0) { 350840b4f539SKevin Wolf return 0; 350940b4f539SKevin Wolf } 351040b4f539SKevin Wolf 351140b4f539SKevin Wolf // Create MultiwriteCB structure 35127267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 351340b4f539SKevin Wolf mcb->num_requests = 0; 351440b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 351540b4f539SKevin Wolf 351640b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 351740b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 351840b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 351940b4f539SKevin Wolf } 352040b4f539SKevin Wolf 352140b4f539SKevin Wolf // Check for mergable requests 352240b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 352340b4f539SKevin Wolf 35246d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 35256d519a5fSStefan Hajnoczi 3526df9309fbSPaolo Bonzini /* Run the aio requests. */ 3527df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 352840b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3529ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 353040b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 353140b4f539SKevin Wolf } 353240b4f539SKevin Wolf 353340b4f539SKevin Wolf return 0; 353440b4f539SKevin Wolf } 353540b4f539SKevin Wolf 353683f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 353783f64091Sbellard { 3538d7331bedSStefan Hajnoczi acb->aiocb_info->cancel(acb); 353983f64091Sbellard } 354083f64091Sbellard 354198f90dbaSZhi Yong Wu /* block I/O throttling */ 354298f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 354398f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait) 354498f90dbaSZhi Yong Wu { 354598f90dbaSZhi Yong Wu uint64_t bps_limit = 0; 3546ae29d6c6SStefan Hajnoczi uint64_t extension; 354798f90dbaSZhi Yong Wu double bytes_limit, bytes_base, bytes_res; 354898f90dbaSZhi Yong Wu double slice_time, wait_time; 354998f90dbaSZhi Yong Wu 355098f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 355198f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 355298f90dbaSZhi Yong Wu } else if (bs->io_limits.bps[is_write]) { 355398f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[is_write]; 355498f90dbaSZhi Yong Wu } else { 355598f90dbaSZhi Yong Wu if (wait) { 355698f90dbaSZhi Yong Wu *wait = 0; 355798f90dbaSZhi Yong Wu } 355898f90dbaSZhi Yong Wu 355998f90dbaSZhi Yong Wu return false; 356098f90dbaSZhi Yong Wu } 356198f90dbaSZhi Yong Wu 356298f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 356398f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 356498f90dbaSZhi Yong Wu bytes_limit = bps_limit * slice_time; 35655905fbc9SStefan Hajnoczi bytes_base = bs->slice_submitted.bytes[is_write]; 356698f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 35675905fbc9SStefan Hajnoczi bytes_base += bs->slice_submitted.bytes[!is_write]; 356898f90dbaSZhi Yong Wu } 356998f90dbaSZhi Yong Wu 357098f90dbaSZhi Yong Wu /* bytes_base: the bytes of data which have been read/written; and 357198f90dbaSZhi Yong Wu * it is obtained from the history statistic info. 357298f90dbaSZhi Yong Wu * bytes_res: the remaining bytes of data which need to be read/written. 357398f90dbaSZhi Yong Wu * (bytes_base + bytes_res) / bps_limit: used to calcuate 357498f90dbaSZhi Yong Wu * the total time for completing reading/writting all data. 357598f90dbaSZhi Yong Wu */ 357698f90dbaSZhi Yong Wu bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 357798f90dbaSZhi Yong Wu 357898f90dbaSZhi Yong Wu if (bytes_base + bytes_res <= bytes_limit) { 357998f90dbaSZhi Yong Wu if (wait) { 358098f90dbaSZhi Yong Wu *wait = 0; 358198f90dbaSZhi Yong Wu } 358298f90dbaSZhi Yong Wu 358398f90dbaSZhi Yong Wu return false; 358498f90dbaSZhi Yong Wu } 358598f90dbaSZhi Yong Wu 358698f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 358798f90dbaSZhi Yong Wu wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; 358898f90dbaSZhi Yong Wu 358998f90dbaSZhi Yong Wu /* When the I/O rate at runtime exceeds the limits, 359098f90dbaSZhi Yong Wu * bs->slice_end need to be extended in order that the current statistic 359198f90dbaSZhi Yong Wu * info can be kept until the timer fire, so it is increased and tuned 359298f90dbaSZhi Yong Wu * based on the result of experiment. 359398f90dbaSZhi Yong Wu */ 3594ae29d6c6SStefan Hajnoczi extension = wait_time * NANOSECONDS_PER_SECOND; 3595ae29d6c6SStefan Hajnoczi extension = DIV_ROUND_UP(extension, BLOCK_IO_SLICE_TIME) * 3596ae29d6c6SStefan Hajnoczi BLOCK_IO_SLICE_TIME; 3597ae29d6c6SStefan Hajnoczi bs->slice_end += extension; 359898f90dbaSZhi Yong Wu if (wait) { 35990775437fSStefan Hajnoczi *wait = wait_time * NANOSECONDS_PER_SECOND; 360098f90dbaSZhi Yong Wu } 360198f90dbaSZhi Yong Wu 360298f90dbaSZhi Yong Wu return true; 360398f90dbaSZhi Yong Wu } 360498f90dbaSZhi Yong Wu 360598f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 360698f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait) 360798f90dbaSZhi Yong Wu { 360898f90dbaSZhi Yong Wu uint64_t iops_limit = 0; 360998f90dbaSZhi Yong Wu double ios_limit, ios_base; 361098f90dbaSZhi Yong Wu double slice_time, wait_time; 361198f90dbaSZhi Yong Wu 361298f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 361398f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 361498f90dbaSZhi Yong Wu } else if (bs->io_limits.iops[is_write]) { 361598f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[is_write]; 361698f90dbaSZhi Yong Wu } else { 361798f90dbaSZhi Yong Wu if (wait) { 361898f90dbaSZhi Yong Wu *wait = 0; 361998f90dbaSZhi Yong Wu } 362098f90dbaSZhi Yong Wu 362198f90dbaSZhi Yong Wu return false; 362298f90dbaSZhi Yong Wu } 362398f90dbaSZhi Yong Wu 362498f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 362598f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 362698f90dbaSZhi Yong Wu ios_limit = iops_limit * slice_time; 36275905fbc9SStefan Hajnoczi ios_base = bs->slice_submitted.ios[is_write]; 362898f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 36295905fbc9SStefan Hajnoczi ios_base += bs->slice_submitted.ios[!is_write]; 363098f90dbaSZhi Yong Wu } 363198f90dbaSZhi Yong Wu 363298f90dbaSZhi Yong Wu if (ios_base + 1 <= ios_limit) { 363398f90dbaSZhi Yong Wu if (wait) { 363498f90dbaSZhi Yong Wu *wait = 0; 363598f90dbaSZhi Yong Wu } 363698f90dbaSZhi Yong Wu 363798f90dbaSZhi Yong Wu return false; 363898f90dbaSZhi Yong Wu } 363998f90dbaSZhi Yong Wu 36400775437fSStefan Hajnoczi /* Calc approx time to dispatch, in seconds */ 364198f90dbaSZhi Yong Wu wait_time = (ios_base + 1) / iops_limit; 364298f90dbaSZhi Yong Wu if (wait_time > elapsed_time) { 364398f90dbaSZhi Yong Wu wait_time = wait_time - elapsed_time; 364498f90dbaSZhi Yong Wu } else { 364598f90dbaSZhi Yong Wu wait_time = 0; 364698f90dbaSZhi Yong Wu } 364798f90dbaSZhi Yong Wu 3648ae29d6c6SStefan Hajnoczi /* Exceeded current slice, extend it by another slice time */ 3649ae29d6c6SStefan Hajnoczi bs->slice_end += BLOCK_IO_SLICE_TIME; 365098f90dbaSZhi Yong Wu if (wait) { 36510775437fSStefan Hajnoczi *wait = wait_time * NANOSECONDS_PER_SECOND; 365298f90dbaSZhi Yong Wu } 365398f90dbaSZhi Yong Wu 365498f90dbaSZhi Yong Wu return true; 365598f90dbaSZhi Yong Wu } 365698f90dbaSZhi Yong Wu 365798f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 365898f90dbaSZhi Yong Wu bool is_write, int64_t *wait) 365998f90dbaSZhi Yong Wu { 366098f90dbaSZhi Yong Wu int64_t now, max_wait; 366198f90dbaSZhi Yong Wu uint64_t bps_wait = 0, iops_wait = 0; 366298f90dbaSZhi Yong Wu double elapsed_time; 366398f90dbaSZhi Yong Wu int bps_ret, iops_ret; 366498f90dbaSZhi Yong Wu 366598f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 3666e660fb8bSStefan Hajnoczi if (now > bs->slice_end) { 366798f90dbaSZhi Yong Wu bs->slice_start = now; 3668ae29d6c6SStefan Hajnoczi bs->slice_end = now + BLOCK_IO_SLICE_TIME; 36695905fbc9SStefan Hajnoczi memset(&bs->slice_submitted, 0, sizeof(bs->slice_submitted)); 367098f90dbaSZhi Yong Wu } 367198f90dbaSZhi Yong Wu 367298f90dbaSZhi Yong Wu elapsed_time = now - bs->slice_start; 367398f90dbaSZhi Yong Wu elapsed_time /= (NANOSECONDS_PER_SECOND); 367498f90dbaSZhi Yong Wu 367598f90dbaSZhi Yong Wu bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, 367698f90dbaSZhi Yong Wu is_write, elapsed_time, &bps_wait); 367798f90dbaSZhi Yong Wu iops_ret = bdrv_exceed_iops_limits(bs, is_write, 367898f90dbaSZhi Yong Wu elapsed_time, &iops_wait); 367998f90dbaSZhi Yong Wu if (bps_ret || iops_ret) { 368098f90dbaSZhi Yong Wu max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; 368198f90dbaSZhi Yong Wu if (wait) { 368298f90dbaSZhi Yong Wu *wait = max_wait; 368398f90dbaSZhi Yong Wu } 368498f90dbaSZhi Yong Wu 368598f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 368698f90dbaSZhi Yong Wu if (bs->slice_end < now + max_wait) { 368798f90dbaSZhi Yong Wu bs->slice_end = now + max_wait; 368898f90dbaSZhi Yong Wu } 368998f90dbaSZhi Yong Wu 369098f90dbaSZhi Yong Wu return true; 369198f90dbaSZhi Yong Wu } 369298f90dbaSZhi Yong Wu 369398f90dbaSZhi Yong Wu if (wait) { 369498f90dbaSZhi Yong Wu *wait = 0; 369598f90dbaSZhi Yong Wu } 369698f90dbaSZhi Yong Wu 36975905fbc9SStefan Hajnoczi bs->slice_submitted.bytes[is_write] += (int64_t)nb_sectors * 36985905fbc9SStefan Hajnoczi BDRV_SECTOR_SIZE; 36995905fbc9SStefan Hajnoczi bs->slice_submitted.ios[is_write]++; 37005905fbc9SStefan Hajnoczi 370198f90dbaSZhi Yong Wu return false; 370298f90dbaSZhi Yong Wu } 370383f64091Sbellard 370483f64091Sbellard /**************************************************************/ 370583f64091Sbellard /* async block device emulation */ 370683f64091Sbellard 3707c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3708c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3709c16b5a2cSChristoph Hellwig QEMUBH *bh; 3710c16b5a2cSChristoph Hellwig int ret; 3711c16b5a2cSChristoph Hellwig /* vector translation state */ 3712c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3713c16b5a2cSChristoph Hellwig uint8_t *bounce; 3714c16b5a2cSChristoph Hellwig int is_write; 3715c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3716c16b5a2cSChristoph Hellwig 3717c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3718c16b5a2cSChristoph Hellwig { 3719b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3720b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 37216a7ad299SDor Laor qemu_bh_delete(acb->bh); 372236afc451SAvi Kivity acb->bh = NULL; 3723c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3724c16b5a2cSChristoph Hellwig } 3725c16b5a2cSChristoph Hellwig 3726d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_aiocb_info = { 3727c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3728c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3729c16b5a2cSChristoph Hellwig }; 3730c16b5a2cSChristoph Hellwig 373183f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3732beac80cdSbellard { 3733ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3734f141eafeSaliguori 3735f141eafeSaliguori if (!acb->is_write) 373603396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 3737ceb42de8Saliguori qemu_vfree(acb->bounce); 3738ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 37396a7ad299SDor Laor qemu_bh_delete(acb->bh); 374036afc451SAvi Kivity acb->bh = NULL; 3741ce1a14dcSpbrook qemu_aio_release(acb); 3742beac80cdSbellard } 3743beac80cdSbellard 3744f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3745f141eafeSaliguori int64_t sector_num, 3746f141eafeSaliguori QEMUIOVector *qiov, 3747f141eafeSaliguori int nb_sectors, 3748f141eafeSaliguori BlockDriverCompletionFunc *cb, 3749f141eafeSaliguori void *opaque, 3750f141eafeSaliguori int is_write) 3751f141eafeSaliguori 3752ea2384d3Sbellard { 3753ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 375483f64091Sbellard 3755d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); 3756f141eafeSaliguori acb->is_write = is_write; 3757f141eafeSaliguori acb->qiov = qiov; 3758e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3759ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3760f141eafeSaliguori 3761f141eafeSaliguori if (is_write) { 3762d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 37631ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3764f141eafeSaliguori } else { 37651ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3766f141eafeSaliguori } 3767f141eafeSaliguori 3768ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3769f141eafeSaliguori 3770ce1a14dcSpbrook return &acb->common; 37717a6cba61Spbrook } 37727a6cba61Spbrook 3773f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3774f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3775ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 377683f64091Sbellard { 3777f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 377883f64091Sbellard } 377983f64091Sbellard 3780f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3781f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3782f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3783f141eafeSaliguori { 3784f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3785f141eafeSaliguori } 3786f141eafeSaliguori 378768485420SKevin Wolf 378868485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 378968485420SKevin Wolf BlockDriverAIOCB common; 379068485420SKevin Wolf BlockRequest req; 379168485420SKevin Wolf bool is_write; 3792d318aea9SKevin Wolf bool *done; 379368485420SKevin Wolf QEMUBH* bh; 379468485420SKevin Wolf } BlockDriverAIOCBCoroutine; 379568485420SKevin Wolf 379668485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 379768485420SKevin Wolf { 3798d318aea9SKevin Wolf BlockDriverAIOCBCoroutine *acb = 3799d318aea9SKevin Wolf container_of(blockacb, BlockDriverAIOCBCoroutine, common); 3800d318aea9SKevin Wolf bool done = false; 3801d318aea9SKevin Wolf 3802d318aea9SKevin Wolf acb->done = &done; 3803d318aea9SKevin Wolf while (!done) { 3804d318aea9SKevin Wolf qemu_aio_wait(); 3805d318aea9SKevin Wolf } 380668485420SKevin Wolf } 380768485420SKevin Wolf 3808d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_co_aiocb_info = { 380968485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 381068485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 381168485420SKevin Wolf }; 381268485420SKevin Wolf 381335246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 381468485420SKevin Wolf { 381568485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 381668485420SKevin Wolf 381768485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 3818d318aea9SKevin Wolf 3819d318aea9SKevin Wolf if (acb->done) { 3820d318aea9SKevin Wolf *acb->done = true; 3821d318aea9SKevin Wolf } 3822d318aea9SKevin Wolf 382368485420SKevin Wolf qemu_bh_delete(acb->bh); 382468485420SKevin Wolf qemu_aio_release(acb); 382568485420SKevin Wolf } 382668485420SKevin Wolf 3827b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3828b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3829b2a61371SStefan Hajnoczi { 3830b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3831b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3832b2a61371SStefan Hajnoczi 3833b2a61371SStefan Hajnoczi if (!acb->is_write) { 3834b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 3835470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3836b2a61371SStefan Hajnoczi } else { 3837b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 3838f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3839b2a61371SStefan Hajnoczi } 3840b2a61371SStefan Hajnoczi 384135246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3842b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 3843b2a61371SStefan Hajnoczi } 3844b2a61371SStefan Hajnoczi 384568485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 384668485420SKevin Wolf int64_t sector_num, 384768485420SKevin Wolf QEMUIOVector *qiov, 384868485420SKevin Wolf int nb_sectors, 384968485420SKevin Wolf BlockDriverCompletionFunc *cb, 385068485420SKevin Wolf void *opaque, 38518c5873d6SStefan Hajnoczi bool is_write) 385268485420SKevin Wolf { 385368485420SKevin Wolf Coroutine *co; 385468485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 385568485420SKevin Wolf 3856d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 385768485420SKevin Wolf acb->req.sector = sector_num; 385868485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 385968485420SKevin Wolf acb->req.qiov = qiov; 386068485420SKevin Wolf acb->is_write = is_write; 3861d318aea9SKevin Wolf acb->done = NULL; 386268485420SKevin Wolf 38638c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 386468485420SKevin Wolf qemu_coroutine_enter(co, acb); 386568485420SKevin Wolf 386668485420SKevin Wolf return &acb->common; 386768485420SKevin Wolf } 386868485420SKevin Wolf 386907f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 3870b2e12bc6SChristoph Hellwig { 387107f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 387207f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 3873b2e12bc6SChristoph Hellwig 387407f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 387507f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3876b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 3877b2e12bc6SChristoph Hellwig } 3878b2e12bc6SChristoph Hellwig 387907f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 3880016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 3881016f5cf6SAlexander Graf { 388207f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 3883016f5cf6SAlexander Graf 388407f07615SPaolo Bonzini Coroutine *co; 388507f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 3886016f5cf6SAlexander Graf 3887d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 3888d318aea9SKevin Wolf acb->done = NULL; 3889d318aea9SKevin Wolf 389007f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 389107f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 3892016f5cf6SAlexander Graf 3893016f5cf6SAlexander Graf return &acb->common; 3894016f5cf6SAlexander Graf } 3895016f5cf6SAlexander Graf 38964265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 38974265d620SPaolo Bonzini { 38984265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 38994265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 39004265d620SPaolo Bonzini 39014265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 39024265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 39034265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 39044265d620SPaolo Bonzini } 39054265d620SPaolo Bonzini 39064265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 39074265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 39084265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 39094265d620SPaolo Bonzini { 39104265d620SPaolo Bonzini Coroutine *co; 39114265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 39124265d620SPaolo Bonzini 39134265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 39144265d620SPaolo Bonzini 3915d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 39164265d620SPaolo Bonzini acb->req.sector = sector_num; 39174265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 3918d318aea9SKevin Wolf acb->done = NULL; 39194265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 39204265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 39214265d620SPaolo Bonzini 39224265d620SPaolo Bonzini return &acb->common; 39234265d620SPaolo Bonzini } 39244265d620SPaolo Bonzini 3925ea2384d3Sbellard void bdrv_init(void) 3926ea2384d3Sbellard { 39275efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3928ea2384d3Sbellard } 3929ce1a14dcSpbrook 3930eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3931eb852011SMarkus Armbruster { 3932eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3933eb852011SMarkus Armbruster bdrv_init(); 3934eb852011SMarkus Armbruster } 3935eb852011SMarkus Armbruster 3936d7331bedSStefan Hajnoczi void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 39376bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 39386bbff9a0Saliguori { 3939ce1a14dcSpbrook BlockDriverAIOCB *acb; 3940ce1a14dcSpbrook 3941d7331bedSStefan Hajnoczi acb = g_slice_alloc(aiocb_info->aiocb_size); 3942d7331bedSStefan Hajnoczi acb->aiocb_info = aiocb_info; 3943ce1a14dcSpbrook acb->bs = bs; 3944ce1a14dcSpbrook acb->cb = cb; 3945ce1a14dcSpbrook acb->opaque = opaque; 3946ce1a14dcSpbrook return acb; 3947ce1a14dcSpbrook } 3948ce1a14dcSpbrook 3949ce1a14dcSpbrook void qemu_aio_release(void *p) 3950ce1a14dcSpbrook { 3951d37c975fSStefan Hajnoczi BlockDriverAIOCB *acb = p; 3952d7331bedSStefan Hajnoczi g_slice_free1(acb->aiocb_info->aiocb_size, acb); 3953ce1a14dcSpbrook } 395419cb3738Sbellard 395519cb3738Sbellard /**************************************************************/ 3956f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 3957f9f05dc5SKevin Wolf 3958f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 3959f9f05dc5SKevin Wolf Coroutine *coroutine; 3960f9f05dc5SKevin Wolf int ret; 3961f9f05dc5SKevin Wolf } CoroutineIOCompletion; 3962f9f05dc5SKevin Wolf 3963f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 3964f9f05dc5SKevin Wolf { 3965f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 3966f9f05dc5SKevin Wolf 3967f9f05dc5SKevin Wolf co->ret = ret; 3968f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 3969f9f05dc5SKevin Wolf } 3970f9f05dc5SKevin Wolf 3971f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 3972f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 3973f9f05dc5SKevin Wolf bool is_write) 3974f9f05dc5SKevin Wolf { 3975f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 3976f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 3977f9f05dc5SKevin Wolf }; 3978f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 3979f9f05dc5SKevin Wolf 3980f9f05dc5SKevin Wolf if (is_write) { 3981a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 3982f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3983f9f05dc5SKevin Wolf } else { 3984a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 3985f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3986f9f05dc5SKevin Wolf } 3987f9f05dc5SKevin Wolf 398859370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 3989f9f05dc5SKevin Wolf if (!acb) { 3990f9f05dc5SKevin Wolf return -EIO; 3991f9f05dc5SKevin Wolf } 3992f9f05dc5SKevin Wolf qemu_coroutine_yield(); 3993f9f05dc5SKevin Wolf 3994f9f05dc5SKevin Wolf return co.ret; 3995f9f05dc5SKevin Wolf } 3996f9f05dc5SKevin Wolf 3997f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 3998f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3999f9f05dc5SKevin Wolf QEMUIOVector *iov) 4000f9f05dc5SKevin Wolf { 4001f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 4002f9f05dc5SKevin Wolf } 4003f9f05dc5SKevin Wolf 4004f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 4005f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4006f9f05dc5SKevin Wolf QEMUIOVector *iov) 4007f9f05dc5SKevin Wolf { 4008f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 4009f9f05dc5SKevin Wolf } 4010f9f05dc5SKevin Wolf 401107f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 4012e7a8a783SKevin Wolf { 401307f07615SPaolo Bonzini RwCo *rwco = opaque; 401407f07615SPaolo Bonzini 401507f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 401607f07615SPaolo Bonzini } 401707f07615SPaolo Bonzini 401807f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 401907f07615SPaolo Bonzini { 4020eb489bb1SKevin Wolf int ret; 4021eb489bb1SKevin Wolf 402229cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 402307f07615SPaolo Bonzini return 0; 4024eb489bb1SKevin Wolf } 4025eb489bb1SKevin Wolf 4026ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 4027bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); 4028eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 4029eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 4030eb489bb1SKevin Wolf if (ret < 0) { 4031eb489bb1SKevin Wolf return ret; 4032eb489bb1SKevin Wolf } 4033eb489bb1SKevin Wolf } 4034eb489bb1SKevin Wolf 4035ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 4036ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 4037d4c82329SKevin Wolf goto flush_parent; 4038ca716364SKevin Wolf } 4039ca716364SKevin Wolf 4040bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); 4041eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 404229cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 404307f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 404407f07615SPaolo Bonzini BlockDriverAIOCB *acb; 4045e7a8a783SKevin Wolf CoroutineIOCompletion co = { 4046e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 4047e7a8a783SKevin Wolf }; 4048e7a8a783SKevin Wolf 404907f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 405007f07615SPaolo Bonzini if (acb == NULL) { 405129cdb251SPaolo Bonzini ret = -EIO; 405207f07615SPaolo Bonzini } else { 4053e7a8a783SKevin Wolf qemu_coroutine_yield(); 405429cdb251SPaolo Bonzini ret = co.ret; 4055e7a8a783SKevin Wolf } 405607f07615SPaolo Bonzini } else { 405707f07615SPaolo Bonzini /* 405807f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 405907f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 406007f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 406107f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 406207f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 406307f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 406407f07615SPaolo Bonzini * mode. 406507f07615SPaolo Bonzini * 406607f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 406707f07615SPaolo Bonzini */ 406829cdb251SPaolo Bonzini ret = 0; 406907f07615SPaolo Bonzini } 407029cdb251SPaolo Bonzini if (ret < 0) { 407129cdb251SPaolo Bonzini return ret; 407229cdb251SPaolo Bonzini } 407329cdb251SPaolo Bonzini 407429cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 407529cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 407629cdb251SPaolo Bonzini */ 4077d4c82329SKevin Wolf flush_parent: 407829cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 407907f07615SPaolo Bonzini } 408007f07615SPaolo Bonzini 40810f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 40820f15423cSAnthony Liguori { 40830f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 40840f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 40850f15423cSAnthony Liguori } 40860f15423cSAnthony Liguori } 40870f15423cSAnthony Liguori 40880f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 40890f15423cSAnthony Liguori { 40900f15423cSAnthony Liguori BlockDriverState *bs; 40910f15423cSAnthony Liguori 40920f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 40930f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 40940f15423cSAnthony Liguori } 40950f15423cSAnthony Liguori } 40960f15423cSAnthony Liguori 409707789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 409807789269SBenoît Canet { 409907789269SBenoît Canet BlockDriverState *bs; 410007789269SBenoît Canet 410107789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 410207789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 410307789269SBenoît Canet } 410407789269SBenoît Canet } 410507789269SBenoît Canet 410607f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 410707f07615SPaolo Bonzini { 410807f07615SPaolo Bonzini Coroutine *co; 410907f07615SPaolo Bonzini RwCo rwco = { 411007f07615SPaolo Bonzini .bs = bs, 411107f07615SPaolo Bonzini .ret = NOT_DONE, 411207f07615SPaolo Bonzini }; 411307f07615SPaolo Bonzini 411407f07615SPaolo Bonzini if (qemu_in_coroutine()) { 411507f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 411607f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 411707f07615SPaolo Bonzini } else { 411807f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 411907f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 412007f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 412107f07615SPaolo Bonzini qemu_aio_wait(); 412207f07615SPaolo Bonzini } 412307f07615SPaolo Bonzini } 412407f07615SPaolo Bonzini 412507f07615SPaolo Bonzini return rwco.ret; 412607f07615SPaolo Bonzini } 4127e7a8a783SKevin Wolf 41284265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 41294265d620SPaolo Bonzini { 41304265d620SPaolo Bonzini RwCo *rwco = opaque; 41314265d620SPaolo Bonzini 41324265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 41334265d620SPaolo Bonzini } 41344265d620SPaolo Bonzini 41354265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 41364265d620SPaolo Bonzini int nb_sectors) 41374265d620SPaolo Bonzini { 41384265d620SPaolo Bonzini if (!bs->drv) { 41394265d620SPaolo Bonzini return -ENOMEDIUM; 41404265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 41414265d620SPaolo Bonzini return -EIO; 41424265d620SPaolo Bonzini } else if (bs->read_only) { 41434265d620SPaolo Bonzini return -EROFS; 4144df702c9bSPaolo Bonzini } 4145df702c9bSPaolo Bonzini 4146df702c9bSPaolo Bonzini if (bs->dirty_bitmap) { 41478f0720ecSPaolo Bonzini bdrv_reset_dirty(bs, sector_num, nb_sectors); 4148df702c9bSPaolo Bonzini } 4149df702c9bSPaolo Bonzini 41509e8f1835SPaolo Bonzini /* Do nothing if disabled. */ 41519e8f1835SPaolo Bonzini if (!(bs->open_flags & BDRV_O_UNMAP)) { 41529e8f1835SPaolo Bonzini return 0; 41539e8f1835SPaolo Bonzini } 41549e8f1835SPaolo Bonzini 4155df702c9bSPaolo Bonzini if (bs->drv->bdrv_co_discard) { 41564265d620SPaolo Bonzini return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors); 41574265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 41584265d620SPaolo Bonzini BlockDriverAIOCB *acb; 41594265d620SPaolo Bonzini CoroutineIOCompletion co = { 41604265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 41614265d620SPaolo Bonzini }; 41624265d620SPaolo Bonzini 41634265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 41644265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 41654265d620SPaolo Bonzini if (acb == NULL) { 41664265d620SPaolo Bonzini return -EIO; 41674265d620SPaolo Bonzini } else { 41684265d620SPaolo Bonzini qemu_coroutine_yield(); 41694265d620SPaolo Bonzini return co.ret; 41704265d620SPaolo Bonzini } 41714265d620SPaolo Bonzini } else { 41724265d620SPaolo Bonzini return 0; 41734265d620SPaolo Bonzini } 41744265d620SPaolo Bonzini } 41754265d620SPaolo Bonzini 41764265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 41774265d620SPaolo Bonzini { 41784265d620SPaolo Bonzini Coroutine *co; 41794265d620SPaolo Bonzini RwCo rwco = { 41804265d620SPaolo Bonzini .bs = bs, 41814265d620SPaolo Bonzini .sector_num = sector_num, 41824265d620SPaolo Bonzini .nb_sectors = nb_sectors, 41834265d620SPaolo Bonzini .ret = NOT_DONE, 41844265d620SPaolo Bonzini }; 41854265d620SPaolo Bonzini 41864265d620SPaolo Bonzini if (qemu_in_coroutine()) { 41874265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 41884265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 41894265d620SPaolo Bonzini } else { 41904265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 41914265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 41924265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 41934265d620SPaolo Bonzini qemu_aio_wait(); 41944265d620SPaolo Bonzini } 41954265d620SPaolo Bonzini } 41964265d620SPaolo Bonzini 41974265d620SPaolo Bonzini return rwco.ret; 41984265d620SPaolo Bonzini } 41994265d620SPaolo Bonzini 4200f9f05dc5SKevin Wolf /**************************************************************/ 420119cb3738Sbellard /* removable device support */ 420219cb3738Sbellard 420319cb3738Sbellard /** 420419cb3738Sbellard * Return TRUE if the media is present 420519cb3738Sbellard */ 420619cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 420719cb3738Sbellard { 420819cb3738Sbellard BlockDriver *drv = bs->drv; 4209a1aff5bfSMarkus Armbruster 421019cb3738Sbellard if (!drv) 421119cb3738Sbellard return 0; 421219cb3738Sbellard if (!drv->bdrv_is_inserted) 4213a1aff5bfSMarkus Armbruster return 1; 4214a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 421519cb3738Sbellard } 421619cb3738Sbellard 421719cb3738Sbellard /** 42188e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 42198e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 422019cb3738Sbellard */ 422119cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 422219cb3738Sbellard { 422319cb3738Sbellard BlockDriver *drv = bs->drv; 422419cb3738Sbellard 42258e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 42268e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 42278e49ca46SMarkus Armbruster } 42288e49ca46SMarkus Armbruster return -ENOTSUP; 422919cb3738Sbellard } 423019cb3738Sbellard 423119cb3738Sbellard /** 423219cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 423319cb3738Sbellard */ 4234f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 423519cb3738Sbellard { 423619cb3738Sbellard BlockDriver *drv = bs->drv; 423719cb3738Sbellard 4238822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 4239822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 424019cb3738Sbellard } 42416f382ed2SLuiz Capitulino 42426f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 42436f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 42446f382ed2SLuiz Capitulino } 424519cb3738Sbellard } 424619cb3738Sbellard 424719cb3738Sbellard /** 424819cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 424919cb3738Sbellard * to eject it manually). 425019cb3738Sbellard */ 4251025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 425219cb3738Sbellard { 425319cb3738Sbellard BlockDriver *drv = bs->drv; 425419cb3738Sbellard 4255025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 4256b8c6d095SStefan Hajnoczi 4257025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 4258025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 425919cb3738Sbellard } 426019cb3738Sbellard } 4261985a03b0Sths 4262985a03b0Sths /* needed for generic scsi interface */ 4263985a03b0Sths 4264985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 4265985a03b0Sths { 4266985a03b0Sths BlockDriver *drv = bs->drv; 4267985a03b0Sths 4268985a03b0Sths if (drv && drv->bdrv_ioctl) 4269985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 4270985a03b0Sths return -ENOTSUP; 4271985a03b0Sths } 42727d780669Saliguori 4273221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 4274221f715dSaliguori unsigned long int req, void *buf, 42757d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 42767d780669Saliguori { 4277221f715dSaliguori BlockDriver *drv = bs->drv; 42787d780669Saliguori 4279221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 4280221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 4281221f715dSaliguori return NULL; 42827d780669Saliguori } 4283e268ca52Saliguori 42847b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 42857b6f9300SMarkus Armbruster { 42867b6f9300SMarkus Armbruster bs->buffer_alignment = align; 42877b6f9300SMarkus Armbruster } 42887cd1e32aSlirans@il.ibm.com 4289e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 4290e268ca52Saliguori { 4291e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 4292e268ca52Saliguori } 42937cd1e32aSlirans@il.ibm.com 4294c53b1c51SStefan Hajnoczi /* 4295c53b1c51SStefan Hajnoczi * Check if all memory in this vector is sector aligned. 4296c53b1c51SStefan Hajnoczi */ 4297c53b1c51SStefan Hajnoczi bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) 4298c53b1c51SStefan Hajnoczi { 4299c53b1c51SStefan Hajnoczi int i; 4300c53b1c51SStefan Hajnoczi 4301c53b1c51SStefan Hajnoczi for (i = 0; i < qiov->niov; i++) { 4302c53b1c51SStefan Hajnoczi if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { 4303c53b1c51SStefan Hajnoczi return false; 4304c53b1c51SStefan Hajnoczi } 4305c53b1c51SStefan Hajnoczi } 4306c53b1c51SStefan Hajnoczi 4307c53b1c51SStefan Hajnoczi return true; 4308c53b1c51SStefan Hajnoczi } 4309c53b1c51SStefan Hajnoczi 431050717e94SPaolo Bonzini void bdrv_set_dirty_tracking(BlockDriverState *bs, int granularity) 43117cd1e32aSlirans@il.ibm.com { 43127cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 4313a55eb92cSJan Kiszka 431450717e94SPaolo Bonzini assert((granularity & (granularity - 1)) == 0); 431550717e94SPaolo Bonzini 431650717e94SPaolo Bonzini if (granularity) { 431750717e94SPaolo Bonzini granularity >>= BDRV_SECTOR_BITS; 431850717e94SPaolo Bonzini assert(!bs->dirty_bitmap); 43198f0720ecSPaolo Bonzini bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); 432050717e94SPaolo Bonzini bs->dirty_bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); 43217cd1e32aSlirans@il.ibm.com } else { 4322c6d22830SJan Kiszka if (bs->dirty_bitmap) { 43238f0720ecSPaolo Bonzini hbitmap_free(bs->dirty_bitmap); 4324c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 43257cd1e32aSlirans@il.ibm.com } 43267cd1e32aSlirans@il.ibm.com } 43277cd1e32aSlirans@il.ibm.com } 43287cd1e32aSlirans@il.ibm.com 43297cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 43307cd1e32aSlirans@il.ibm.com { 43318f0720ecSPaolo Bonzini if (bs->dirty_bitmap) { 43328f0720ecSPaolo Bonzini return hbitmap_get(bs->dirty_bitmap, sector); 43337cd1e32aSlirans@il.ibm.com } else { 43347cd1e32aSlirans@il.ibm.com return 0; 43357cd1e32aSlirans@il.ibm.com } 43367cd1e32aSlirans@il.ibm.com } 43377cd1e32aSlirans@il.ibm.com 43388f0720ecSPaolo Bonzini void bdrv_dirty_iter_init(BlockDriverState *bs, HBitmapIter *hbi) 43391755da16SPaolo Bonzini { 43408f0720ecSPaolo Bonzini hbitmap_iter_init(hbi, bs->dirty_bitmap, 0); 43411755da16SPaolo Bonzini } 43421755da16SPaolo Bonzini 43431755da16SPaolo Bonzini void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 43441755da16SPaolo Bonzini int nr_sectors) 43451755da16SPaolo Bonzini { 43468f0720ecSPaolo Bonzini hbitmap_set(bs->dirty_bitmap, cur_sector, nr_sectors); 43471755da16SPaolo Bonzini } 43481755da16SPaolo Bonzini 43497cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 43507cd1e32aSlirans@il.ibm.com int nr_sectors) 43517cd1e32aSlirans@il.ibm.com { 43528f0720ecSPaolo Bonzini hbitmap_reset(bs->dirty_bitmap, cur_sector, nr_sectors); 43537cd1e32aSlirans@il.ibm.com } 4354aaa0eb75SLiran Schour 4355aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 4356aaa0eb75SLiran Schour { 43578f0720ecSPaolo Bonzini if (bs->dirty_bitmap) { 4358acc906c6SPaolo Bonzini return hbitmap_count(bs->dirty_bitmap); 43598f0720ecSPaolo Bonzini } else { 43608f0720ecSPaolo Bonzini return 0; 43618f0720ecSPaolo Bonzini } 4362aaa0eb75SLiran Schour } 4363f88e1a42SJes Sorensen 4364db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 4365db593f25SMarcelo Tosatti { 4366db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 4367db593f25SMarcelo Tosatti bs->in_use = in_use; 4368db593f25SMarcelo Tosatti } 4369db593f25SMarcelo Tosatti 4370db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 4371db593f25SMarcelo Tosatti { 4372db593f25SMarcelo Tosatti return bs->in_use; 4373db593f25SMarcelo Tosatti } 4374db593f25SMarcelo Tosatti 437528a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 437628a7282aSLuiz Capitulino { 4377d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 437858e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 437928a7282aSLuiz Capitulino } 438028a7282aSLuiz Capitulino 438128a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 438228a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 438328a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 438428a7282aSLuiz Capitulino { 4385d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 438692aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 438792aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 438892aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 438928a7282aSLuiz Capitulino } 439028a7282aSLuiz Capitulino 439128a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 439228a7282aSLuiz Capitulino { 4393d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 439428a7282aSLuiz Capitulino } 439528a7282aSLuiz Capitulino 439628a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 439728a7282aSLuiz Capitulino { 439828a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 439958e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 44003bd293c3SPaolo Bonzini if (bs->job) { 44013bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 44023bd293c3SPaolo Bonzini } 440328a7282aSLuiz Capitulino } 440428a7282aSLuiz Capitulino } 440528a7282aSLuiz Capitulino 440628a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 440728a7282aSLuiz Capitulino { 44083e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 44093e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 441058e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 441158e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 441228a7282aSLuiz Capitulino } 441328a7282aSLuiz Capitulino } 441428a7282aSLuiz Capitulino 4415a597e79cSChristoph Hellwig void 4416a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 4417a597e79cSChristoph Hellwig enum BlockAcctType type) 4418a597e79cSChristoph Hellwig { 4419a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 4420a597e79cSChristoph Hellwig 4421a597e79cSChristoph Hellwig cookie->bytes = bytes; 4422c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4423a597e79cSChristoph Hellwig cookie->type = type; 4424a597e79cSChristoph Hellwig } 4425a597e79cSChristoph Hellwig 4426a597e79cSChristoph Hellwig void 4427a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4428a597e79cSChristoph Hellwig { 4429a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4430a597e79cSChristoph Hellwig 4431a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4432a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4433c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4434a597e79cSChristoph Hellwig } 4435a597e79cSChristoph Hellwig 4436d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 4437f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 4438f382d43aSMiroslav Rezanina char *options, uint64_t img_size, int flags, 4439f382d43aSMiroslav Rezanina Error **errp, bool quiet) 4440f88e1a42SJes Sorensen { 4441f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4442d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4443f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 4444f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 444596df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4446f88e1a42SJes Sorensen int ret = 0; 4447f88e1a42SJes Sorensen 4448f88e1a42SJes Sorensen /* Find driver and parse its options */ 4449f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4450f88e1a42SJes Sorensen if (!drv) { 445171c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 4452d92ada22SLuiz Capitulino return; 4453f88e1a42SJes Sorensen } 4454f88e1a42SJes Sorensen 4455f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 4456f88e1a42SJes Sorensen if (!proto_drv) { 445771c79813SLuiz Capitulino error_setg(errp, "Unknown protocol '%s'", filename); 4458d92ada22SLuiz Capitulino return; 4459f88e1a42SJes Sorensen } 4460f88e1a42SJes Sorensen 4461f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4462f88e1a42SJes Sorensen drv->create_options); 4463f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4464f88e1a42SJes Sorensen proto_drv->create_options); 4465f88e1a42SJes Sorensen 4466f88e1a42SJes Sorensen /* Create parameter list with default values */ 4467f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4468f88e1a42SJes Sorensen 4469f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4470f88e1a42SJes Sorensen 4471f88e1a42SJes Sorensen /* Parse -o options */ 4472f88e1a42SJes Sorensen if (options) { 4473f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4474f88e1a42SJes Sorensen if (param == NULL) { 447571c79813SLuiz Capitulino error_setg(errp, "Invalid options for file format '%s'.", fmt); 4476f88e1a42SJes Sorensen goto out; 4477f88e1a42SJes Sorensen } 4478f88e1a42SJes Sorensen } 4479f88e1a42SJes Sorensen 4480f88e1a42SJes Sorensen if (base_filename) { 4481f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4482f88e1a42SJes Sorensen base_filename)) { 448371c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 448471c79813SLuiz Capitulino fmt); 4485f88e1a42SJes Sorensen goto out; 4486f88e1a42SJes Sorensen } 4487f88e1a42SJes Sorensen } 4488f88e1a42SJes Sorensen 4489f88e1a42SJes Sorensen if (base_fmt) { 4490f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 449171c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 449271c79813SLuiz Capitulino "format '%s'", fmt); 4493f88e1a42SJes Sorensen goto out; 4494f88e1a42SJes Sorensen } 4495f88e1a42SJes Sorensen } 4496f88e1a42SJes Sorensen 4497792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4498792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4499792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 450071c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 450171c79813SLuiz Capitulino "same filename as the backing file"); 4502792da93aSJes Sorensen goto out; 4503792da93aSJes Sorensen } 4504792da93aSJes Sorensen } 4505792da93aSJes Sorensen 4506f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4507f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 450896df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 450996df67d1SStefan Hajnoczi if (!backing_drv) { 451071c79813SLuiz Capitulino error_setg(errp, "Unknown backing file format '%s'", 451171c79813SLuiz Capitulino backing_fmt->value.s); 4512f88e1a42SJes Sorensen goto out; 4513f88e1a42SJes Sorensen } 4514f88e1a42SJes Sorensen } 4515f88e1a42SJes Sorensen 4516f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4517f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4518d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4519d220894eSKevin Wolf if (size && size->value.n == -1) { 4520f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4521f88e1a42SJes Sorensen uint64_t size; 4522f88e1a42SJes Sorensen char buf[32]; 452363090dacSPaolo Bonzini int back_flags; 452463090dacSPaolo Bonzini 452563090dacSPaolo Bonzini /* backing files always opened read-only */ 452663090dacSPaolo Bonzini back_flags = 452763090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 4528f88e1a42SJes Sorensen 4529f88e1a42SJes Sorensen bs = bdrv_new(""); 4530f88e1a42SJes Sorensen 4531de9c0cecSKevin Wolf ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags, 4532de9c0cecSKevin Wolf backing_drv); 4533f88e1a42SJes Sorensen if (ret < 0) { 453471c79813SLuiz Capitulino error_setg_errno(errp, -ret, "Could not open '%s'", 453571c79813SLuiz Capitulino backing_file->value.s); 4536f88e1a42SJes Sorensen goto out; 4537f88e1a42SJes Sorensen } 4538f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4539f88e1a42SJes Sorensen size *= 512; 4540f88e1a42SJes Sorensen 4541f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4542f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4543f88e1a42SJes Sorensen } else { 454471c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 4545f88e1a42SJes Sorensen goto out; 4546f88e1a42SJes Sorensen } 4547f88e1a42SJes Sorensen } 4548f88e1a42SJes Sorensen 4549f382d43aSMiroslav Rezanina if (!quiet) { 4550f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4551f88e1a42SJes Sorensen print_option_parameters(param); 4552f88e1a42SJes Sorensen puts(""); 4553f382d43aSMiroslav Rezanina } 4554f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 4555f88e1a42SJes Sorensen if (ret < 0) { 4556f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 455771c79813SLuiz Capitulino error_setg(errp,"Formatting or formatting option not supported for " 455871c79813SLuiz Capitulino "file format '%s'", fmt); 4559f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 4560f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 4561f3f4d2c0SKevin Wolf if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) { 4562f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 4563f3f4d2c0SKevin Wolf } 4564f3f4d2c0SKevin Wolf error_setg(errp, "The image size is too large for file format '%s'%s", 4565f3f4d2c0SKevin Wolf fmt, cluster_size_hint); 4566f88e1a42SJes Sorensen } else { 456771c79813SLuiz Capitulino error_setg(errp, "%s: error while creating %s: %s", filename, fmt, 456871c79813SLuiz Capitulino strerror(-ret)); 4569f88e1a42SJes Sorensen } 4570f88e1a42SJes Sorensen } 4571f88e1a42SJes Sorensen 4572f88e1a42SJes Sorensen out: 4573f88e1a42SJes Sorensen free_option_parameters(create_options); 4574f88e1a42SJes Sorensen free_option_parameters(param); 4575f88e1a42SJes Sorensen 4576f88e1a42SJes Sorensen if (bs) { 4577f88e1a42SJes Sorensen bdrv_delete(bs); 4578f88e1a42SJes Sorensen } 4579f88e1a42SJes Sorensen } 458085d126f3SStefan Hajnoczi 458185d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 458285d126f3SStefan Hajnoczi { 458385d126f3SStefan Hajnoczi /* Currently BlockDriverState always uses the main loop AioContext */ 458485d126f3SStefan Hajnoczi return qemu_get_aio_context(); 458585d126f3SStefan Hajnoczi } 4586d616b224SStefan Hajnoczi 4587d616b224SStefan Hajnoczi void bdrv_add_before_write_notifier(BlockDriverState *bs, 4588d616b224SStefan Hajnoczi NotifierWithReturn *notifier) 4589d616b224SStefan Hajnoczi { 4590d616b224SStefan Hajnoczi notifier_with_return_list_add(&bs->before_write_notifiers, notifier); 4591d616b224SStefan Hajnoczi } 4592