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 102f9092b10SMarkus Armbruster /* The device to use for VM snapshots */ 103f9092b10SMarkus Armbruster static BlockDriverState *bs_snapshots; 104f9092b10SMarkus Armbruster 105eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 106eb852011SMarkus Armbruster static int use_bdrv_whitelist; 107eb852011SMarkus Armbruster 1089e0b22f4SStefan Hajnoczi #ifdef _WIN32 1099e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 1109e0b22f4SStefan Hajnoczi { 1119e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 1129e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 1139e0b22f4SStefan Hajnoczi filename[1] == ':'); 1149e0b22f4SStefan Hajnoczi } 1159e0b22f4SStefan Hajnoczi 1169e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 1179e0b22f4SStefan Hajnoczi { 1189e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 1199e0b22f4SStefan Hajnoczi filename[2] == '\0') 1209e0b22f4SStefan Hajnoczi return 1; 1219e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1229e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1239e0b22f4SStefan Hajnoczi return 1; 1249e0b22f4SStefan Hajnoczi return 0; 1259e0b22f4SStefan Hajnoczi } 1269e0b22f4SStefan Hajnoczi #endif 1279e0b22f4SStefan Hajnoczi 1280563e191SZhi Yong Wu /* throttling disk I/O limits */ 12998f90dbaSZhi Yong Wu void bdrv_io_limits_disable(BlockDriverState *bs) 13098f90dbaSZhi Yong Wu { 13198f90dbaSZhi Yong Wu bs->io_limits_enabled = false; 13298f90dbaSZhi Yong Wu 13398f90dbaSZhi Yong Wu while (qemu_co_queue_next(&bs->throttled_reqs)); 13498f90dbaSZhi Yong Wu 13598f90dbaSZhi Yong Wu if (bs->block_timer) { 13698f90dbaSZhi Yong Wu qemu_del_timer(bs->block_timer); 13798f90dbaSZhi Yong Wu qemu_free_timer(bs->block_timer); 13898f90dbaSZhi Yong Wu bs->block_timer = NULL; 13998f90dbaSZhi Yong Wu } 14098f90dbaSZhi Yong Wu 14198f90dbaSZhi Yong Wu bs->slice_start = 0; 14298f90dbaSZhi Yong Wu bs->slice_end = 0; 14398f90dbaSZhi Yong Wu bs->slice_time = 0; 14498f90dbaSZhi Yong Wu memset(&bs->io_base, 0, sizeof(bs->io_base)); 14598f90dbaSZhi Yong Wu } 14698f90dbaSZhi Yong Wu 1470563e191SZhi Yong Wu static void bdrv_block_timer(void *opaque) 1480563e191SZhi Yong Wu { 1490563e191SZhi Yong Wu BlockDriverState *bs = opaque; 1500563e191SZhi Yong Wu 1510563e191SZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 1520563e191SZhi Yong Wu } 1530563e191SZhi Yong Wu 1540563e191SZhi Yong Wu void bdrv_io_limits_enable(BlockDriverState *bs) 1550563e191SZhi Yong Wu { 1560563e191SZhi Yong Wu qemu_co_queue_init(&bs->throttled_reqs); 1570563e191SZhi Yong Wu bs->block_timer = qemu_new_timer_ns(vm_clock, bdrv_block_timer, bs); 1580563e191SZhi Yong Wu bs->io_limits_enabled = true; 1590563e191SZhi Yong Wu } 1600563e191SZhi Yong Wu 1610563e191SZhi Yong Wu bool bdrv_io_limits_enabled(BlockDriverState *bs) 1620563e191SZhi Yong Wu { 1630563e191SZhi Yong Wu BlockIOLimit *io_limits = &bs->io_limits; 1640563e191SZhi Yong Wu return io_limits->bps[BLOCK_IO_LIMIT_READ] 1650563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_WRITE] 1660563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_TOTAL] 1670563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_READ] 1680563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_WRITE] 1690563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_TOTAL]; 1700563e191SZhi Yong Wu } 1710563e191SZhi Yong Wu 17298f90dbaSZhi Yong Wu static void bdrv_io_limits_intercept(BlockDriverState *bs, 17398f90dbaSZhi Yong Wu bool is_write, int nb_sectors) 17498f90dbaSZhi Yong Wu { 17598f90dbaSZhi Yong Wu int64_t wait_time = -1; 17698f90dbaSZhi Yong Wu 17798f90dbaSZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 17898f90dbaSZhi Yong Wu qemu_co_queue_wait(&bs->throttled_reqs); 17998f90dbaSZhi Yong Wu } 18098f90dbaSZhi Yong Wu 18198f90dbaSZhi Yong Wu /* In fact, we hope to keep each request's timing, in FIFO mode. The next 18298f90dbaSZhi Yong Wu * throttled requests will not be dequeued until the current request is 18398f90dbaSZhi Yong Wu * allowed to be serviced. So if the current request still exceeds the 18498f90dbaSZhi Yong Wu * limits, it will be inserted to the head. All requests followed it will 18598f90dbaSZhi Yong Wu * be still in throttled_reqs queue. 18698f90dbaSZhi Yong Wu */ 18798f90dbaSZhi Yong Wu 18898f90dbaSZhi Yong Wu while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) { 18998f90dbaSZhi Yong Wu qemu_mod_timer(bs->block_timer, 19098f90dbaSZhi Yong Wu wait_time + qemu_get_clock_ns(vm_clock)); 19198f90dbaSZhi Yong Wu qemu_co_queue_wait_insert_head(&bs->throttled_reqs); 19298f90dbaSZhi Yong Wu } 19398f90dbaSZhi Yong Wu 19498f90dbaSZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 19598f90dbaSZhi Yong Wu } 19698f90dbaSZhi Yong Wu 1979e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 1989e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 1999e0b22f4SStefan Hajnoczi { 200947995c0SPaolo Bonzini const char *p; 201947995c0SPaolo Bonzini 2029e0b22f4SStefan Hajnoczi #ifdef _WIN32 2039e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2049e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2059e0b22f4SStefan Hajnoczi return 0; 2069e0b22f4SStefan Hajnoczi } 207947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 208947995c0SPaolo Bonzini #else 209947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2109e0b22f4SStefan Hajnoczi #endif 2119e0b22f4SStefan Hajnoczi 212947995c0SPaolo Bonzini return *p == ':'; 2139e0b22f4SStefan Hajnoczi } 2149e0b22f4SStefan Hajnoczi 21583f64091Sbellard int path_is_absolute(const char *path) 21683f64091Sbellard { 21721664424Sbellard #ifdef _WIN32 21821664424Sbellard /* specific case for names like: "\\.\d:" */ 219f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 22021664424Sbellard return 1; 221f53f4da9SPaolo Bonzini } 222f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 2233b9f94e1Sbellard #else 224f53f4da9SPaolo Bonzini return (*path == '/'); 2253b9f94e1Sbellard #endif 22683f64091Sbellard } 22783f64091Sbellard 22883f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 22983f64091Sbellard path to it by considering it is relative to base_path. URL are 23083f64091Sbellard supported. */ 23183f64091Sbellard void path_combine(char *dest, int dest_size, 23283f64091Sbellard const char *base_path, 23383f64091Sbellard const char *filename) 23483f64091Sbellard { 23583f64091Sbellard const char *p, *p1; 23683f64091Sbellard int len; 23783f64091Sbellard 23883f64091Sbellard if (dest_size <= 0) 23983f64091Sbellard return; 24083f64091Sbellard if (path_is_absolute(filename)) { 24183f64091Sbellard pstrcpy(dest, dest_size, filename); 24283f64091Sbellard } else { 24383f64091Sbellard p = strchr(base_path, ':'); 24483f64091Sbellard if (p) 24583f64091Sbellard p++; 24683f64091Sbellard else 24783f64091Sbellard p = base_path; 2483b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2493b9f94e1Sbellard #ifdef _WIN32 2503b9f94e1Sbellard { 2513b9f94e1Sbellard const char *p2; 2523b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2533b9f94e1Sbellard if (!p1 || p2 > p1) 2543b9f94e1Sbellard p1 = p2; 2553b9f94e1Sbellard } 2563b9f94e1Sbellard #endif 25783f64091Sbellard if (p1) 25883f64091Sbellard p1++; 25983f64091Sbellard else 26083f64091Sbellard p1 = base_path; 26183f64091Sbellard if (p1 > p) 26283f64091Sbellard p = p1; 26383f64091Sbellard len = p - base_path; 26483f64091Sbellard if (len > dest_size - 1) 26583f64091Sbellard len = dest_size - 1; 26683f64091Sbellard memcpy(dest, base_path, len); 26783f64091Sbellard dest[len] = '\0'; 26883f64091Sbellard pstrcat(dest, dest_size, filename); 26983f64091Sbellard } 27083f64091Sbellard } 27183f64091Sbellard 272dc5a1371SPaolo Bonzini void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) 273dc5a1371SPaolo Bonzini { 274dc5a1371SPaolo Bonzini if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { 275dc5a1371SPaolo Bonzini pstrcpy(dest, sz, bs->backing_file); 276dc5a1371SPaolo Bonzini } else { 277dc5a1371SPaolo Bonzini path_combine(dest, sz, bs->filename, bs->backing_file); 278dc5a1371SPaolo Bonzini } 279dc5a1371SPaolo Bonzini } 280dc5a1371SPaolo Bonzini 2815efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 282ea2384d3Sbellard { 2838c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 2848c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 285f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 286f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 287f9f05dc5SKevin Wolf 288f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 289f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 290f8c35c1dSStefan Hajnoczi */ 291f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 29283f64091Sbellard /* add AIO emulation layer */ 293f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 294f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 29583f64091Sbellard } 296f9f05dc5SKevin Wolf } 297b2e12bc6SChristoph Hellwig 2988a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 299ea2384d3Sbellard } 300b338082bSbellard 301b338082bSbellard /* create a new block device (by default it is empty) */ 302b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 303fc01f7e7Sbellard { 3041b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 305b338082bSbellard 3067267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 307b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 308ea2384d3Sbellard if (device_name[0] != '\0') { 3091b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 310ea2384d3Sbellard } 31128a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 312d7d512f6SPaolo Bonzini notifier_list_init(&bs->close_notifiers); 313d7d512f6SPaolo Bonzini 314b338082bSbellard return bs; 315b338082bSbellard } 316b338082bSbellard 317d7d512f6SPaolo Bonzini void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) 318d7d512f6SPaolo Bonzini { 319d7d512f6SPaolo Bonzini notifier_list_add(&bs->close_notifiers, notify); 320d7d512f6SPaolo Bonzini } 321d7d512f6SPaolo Bonzini 322ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 323ea2384d3Sbellard { 324ea2384d3Sbellard BlockDriver *drv1; 3258a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3268a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 327ea2384d3Sbellard return drv1; 328ea2384d3Sbellard } 3298a22f02aSStefan Hajnoczi } 330ea2384d3Sbellard return NULL; 331ea2384d3Sbellard } 332ea2384d3Sbellard 333eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 334eb852011SMarkus Armbruster { 335eb852011SMarkus Armbruster static const char *whitelist[] = { 336eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 337eb852011SMarkus Armbruster }; 338eb852011SMarkus Armbruster const char **p; 339eb852011SMarkus Armbruster 340eb852011SMarkus Armbruster if (!whitelist[0]) 341eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 342eb852011SMarkus Armbruster 343eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 344eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 345eb852011SMarkus Armbruster return 1; 346eb852011SMarkus Armbruster } 347eb852011SMarkus Armbruster } 348eb852011SMarkus Armbruster return 0; 349eb852011SMarkus Armbruster } 350eb852011SMarkus Armbruster 351eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 352eb852011SMarkus Armbruster { 353eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 354eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 355eb852011SMarkus Armbruster } 356eb852011SMarkus Armbruster 3575b7e1542SZhi Yong Wu typedef struct CreateCo { 3585b7e1542SZhi Yong Wu BlockDriver *drv; 3595b7e1542SZhi Yong Wu char *filename; 3605b7e1542SZhi Yong Wu QEMUOptionParameter *options; 3615b7e1542SZhi Yong Wu int ret; 3625b7e1542SZhi Yong Wu } CreateCo; 3635b7e1542SZhi Yong Wu 3645b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 3655b7e1542SZhi Yong Wu { 3665b7e1542SZhi Yong Wu CreateCo *cco = opaque; 3675b7e1542SZhi Yong Wu assert(cco->drv); 3685b7e1542SZhi Yong Wu 3695b7e1542SZhi Yong Wu cco->ret = cco->drv->bdrv_create(cco->filename, cco->options); 3705b7e1542SZhi Yong Wu } 3715b7e1542SZhi Yong Wu 3720e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 3730e7e1989SKevin Wolf QEMUOptionParameter *options) 374ea2384d3Sbellard { 3755b7e1542SZhi Yong Wu int ret; 3760e7e1989SKevin Wolf 3775b7e1542SZhi Yong Wu Coroutine *co; 3785b7e1542SZhi Yong Wu CreateCo cco = { 3795b7e1542SZhi Yong Wu .drv = drv, 3805b7e1542SZhi Yong Wu .filename = g_strdup(filename), 3815b7e1542SZhi Yong Wu .options = options, 3825b7e1542SZhi Yong Wu .ret = NOT_DONE, 3835b7e1542SZhi Yong Wu }; 3845b7e1542SZhi Yong Wu 3855b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 38680168bffSLuiz Capitulino ret = -ENOTSUP; 38780168bffSLuiz Capitulino goto out; 3885b7e1542SZhi Yong Wu } 3895b7e1542SZhi Yong Wu 3905b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 3915b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 3925b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 3935b7e1542SZhi Yong Wu } else { 3945b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 3955b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 3965b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 3975b7e1542SZhi Yong Wu qemu_aio_wait(); 3985b7e1542SZhi Yong Wu } 3995b7e1542SZhi Yong Wu } 4005b7e1542SZhi Yong Wu 4015b7e1542SZhi Yong Wu ret = cco.ret; 4025b7e1542SZhi Yong Wu 40380168bffSLuiz Capitulino out: 40480168bffSLuiz Capitulino g_free(cco.filename); 4055b7e1542SZhi Yong Wu return ret; 406ea2384d3Sbellard } 407ea2384d3Sbellard 40884a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 40984a12e66SChristoph Hellwig { 41084a12e66SChristoph Hellwig BlockDriver *drv; 41184a12e66SChristoph Hellwig 412b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 41384a12e66SChristoph Hellwig if (drv == NULL) { 41416905d71SStefan Hajnoczi return -ENOENT; 41584a12e66SChristoph Hellwig } 41684a12e66SChristoph Hellwig 41784a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 41884a12e66SChristoph Hellwig } 41984a12e66SChristoph Hellwig 420eba25057SJim Meyering /* 421eba25057SJim Meyering * Create a uniquely-named empty temporary file. 422eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 423eba25057SJim Meyering */ 424eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 425eba25057SJim Meyering { 426d5249393Sbellard #ifdef _WIN32 4273b9f94e1Sbellard char temp_dir[MAX_PATH]; 428eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 429eba25057SJim Meyering have length MAX_PATH or greater. */ 430eba25057SJim Meyering assert(size >= MAX_PATH); 431eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 432eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 433eba25057SJim Meyering ? 0 : -GetLastError()); 434d5249393Sbellard #else 435ea2384d3Sbellard int fd; 4367ccfb2ebSblueswir1 const char *tmpdir; 4370badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 4380badc1eeSaurel32 if (!tmpdir) 4390badc1eeSaurel32 tmpdir = "/tmp"; 440eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 441eba25057SJim Meyering return -EOVERFLOW; 442ea2384d3Sbellard } 443eba25057SJim Meyering fd = mkstemp(filename); 444fe235a06SDunrong Huang if (fd < 0) { 445fe235a06SDunrong Huang return -errno; 446fe235a06SDunrong Huang } 447fe235a06SDunrong Huang if (close(fd) != 0) { 448fe235a06SDunrong Huang unlink(filename); 449eba25057SJim Meyering return -errno; 450eba25057SJim Meyering } 451eba25057SJim Meyering return 0; 452d5249393Sbellard #endif 453eba25057SJim Meyering } 454ea2384d3Sbellard 455f3a5d3f8SChristoph Hellwig /* 456f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 457f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 458f3a5d3f8SChristoph Hellwig */ 459f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 460f3a5d3f8SChristoph Hellwig { 461508c7cb3SChristoph Hellwig int score_max = 0, score; 462508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 463f3a5d3f8SChristoph Hellwig 4648a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 465508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 466508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 467508c7cb3SChristoph Hellwig if (score > score_max) { 468508c7cb3SChristoph Hellwig score_max = score; 469508c7cb3SChristoph Hellwig drv = d; 470f3a5d3f8SChristoph Hellwig } 471508c7cb3SChristoph Hellwig } 472f3a5d3f8SChristoph Hellwig } 473f3a5d3f8SChristoph Hellwig 474508c7cb3SChristoph Hellwig return drv; 475f3a5d3f8SChristoph Hellwig } 476f3a5d3f8SChristoph Hellwig 477b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 47884a12e66SChristoph Hellwig { 47984a12e66SChristoph Hellwig BlockDriver *drv1; 48084a12e66SChristoph Hellwig char protocol[128]; 48184a12e66SChristoph Hellwig int len; 48284a12e66SChristoph Hellwig const char *p; 48384a12e66SChristoph Hellwig 48466f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 48566f82ceeSKevin Wolf 48639508e7aSChristoph Hellwig /* 48739508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 48839508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 48939508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 49039508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 49139508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 49239508e7aSChristoph Hellwig */ 49384a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 49439508e7aSChristoph Hellwig if (drv1) { 49584a12e66SChristoph Hellwig return drv1; 49684a12e66SChristoph Hellwig } 49739508e7aSChristoph Hellwig 4989e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 49939508e7aSChristoph Hellwig return bdrv_find_format("file"); 50039508e7aSChristoph Hellwig } 5019e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 5029e0b22f4SStefan Hajnoczi assert(p != NULL); 50384a12e66SChristoph Hellwig len = p - filename; 50484a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 50584a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 50684a12e66SChristoph Hellwig memcpy(protocol, filename, len); 50784a12e66SChristoph Hellwig protocol[len] = '\0'; 50884a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 50984a12e66SChristoph Hellwig if (drv1->protocol_name && 51084a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 51184a12e66SChristoph Hellwig return drv1; 51284a12e66SChristoph Hellwig } 51384a12e66SChristoph Hellwig } 51484a12e66SChristoph Hellwig return NULL; 51584a12e66SChristoph Hellwig } 51684a12e66SChristoph Hellwig 517f500a6d3SKevin Wolf static int find_image_format(BlockDriverState *bs, const char *filename, 518f500a6d3SKevin Wolf BlockDriver **pdrv) 519ea2384d3Sbellard { 520f500a6d3SKevin Wolf int score, score_max; 521ea2384d3Sbellard BlockDriver *drv1, *drv; 52283f64091Sbellard uint8_t buf[2048]; 523f500a6d3SKevin Wolf int ret = 0; 524f8ea0b00SNicholas Bellinger 52508a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 5268e895599SPaolo Bonzini if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { 527c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 528c98ac35dSStefan Weil if (!drv) { 529c98ac35dSStefan Weil ret = -ENOENT; 530c98ac35dSStefan Weil } 531c98ac35dSStefan Weil *pdrv = drv; 532c98ac35dSStefan Weil return ret; 5331a396859SNicholas A. Bellinger } 534f8ea0b00SNicholas Bellinger 53583f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 536ea2384d3Sbellard if (ret < 0) { 537c98ac35dSStefan Weil *pdrv = NULL; 538c98ac35dSStefan Weil return ret; 539ea2384d3Sbellard } 540ea2384d3Sbellard 541ea2384d3Sbellard score_max = 0; 54284a12e66SChristoph Hellwig drv = NULL; 5438a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 54483f64091Sbellard if (drv1->bdrv_probe) { 545ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 546ea2384d3Sbellard if (score > score_max) { 547ea2384d3Sbellard score_max = score; 548ea2384d3Sbellard drv = drv1; 549ea2384d3Sbellard } 550ea2384d3Sbellard } 55183f64091Sbellard } 552c98ac35dSStefan Weil if (!drv) { 553c98ac35dSStefan Weil ret = -ENOENT; 554c98ac35dSStefan Weil } 555c98ac35dSStefan Weil *pdrv = drv; 556c98ac35dSStefan Weil return ret; 557ea2384d3Sbellard } 558ea2384d3Sbellard 55951762288SStefan Hajnoczi /** 56051762288SStefan Hajnoczi * Set the current 'total_sectors' value 56151762288SStefan Hajnoczi */ 56251762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 56351762288SStefan Hajnoczi { 56451762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 56551762288SStefan Hajnoczi 566396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 567396759adSNicholas Bellinger if (bs->sg) 568396759adSNicholas Bellinger return 0; 569396759adSNicholas Bellinger 57051762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 57151762288SStefan Hajnoczi if (drv->bdrv_getlength) { 57251762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 57351762288SStefan Hajnoczi if (length < 0) { 57451762288SStefan Hajnoczi return length; 57551762288SStefan Hajnoczi } 57651762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 57751762288SStefan Hajnoczi } 57851762288SStefan Hajnoczi 57951762288SStefan Hajnoczi bs->total_sectors = hint; 58051762288SStefan Hajnoczi return 0; 58151762288SStefan Hajnoczi } 58251762288SStefan Hajnoczi 583c3993cdcSStefan Hajnoczi /** 584c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 585c3993cdcSStefan Hajnoczi * 586c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 587c3993cdcSStefan Hajnoczi */ 588c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 589c3993cdcSStefan Hajnoczi { 590c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 591c3993cdcSStefan Hajnoczi 592c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 593c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 59492196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 59592196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 596c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 597c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 598c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 599c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 600c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 601c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 602c3993cdcSStefan Hajnoczi /* this is the default */ 603c3993cdcSStefan Hajnoczi } else { 604c3993cdcSStefan Hajnoczi return -1; 605c3993cdcSStefan Hajnoczi } 606c3993cdcSStefan Hajnoczi 607c3993cdcSStefan Hajnoczi return 0; 608c3993cdcSStefan Hajnoczi } 609c3993cdcSStefan Hajnoczi 61053fec9d3SStefan Hajnoczi /** 61153fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 61253fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 61353fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 61453fec9d3SStefan Hajnoczi */ 61553fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 61653fec9d3SStefan Hajnoczi { 61753fec9d3SStefan Hajnoczi bs->copy_on_read++; 61853fec9d3SStefan Hajnoczi } 61953fec9d3SStefan Hajnoczi 62053fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 62153fec9d3SStefan Hajnoczi { 62253fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 62353fec9d3SStefan Hajnoczi bs->copy_on_read--; 62453fec9d3SStefan Hajnoczi } 62553fec9d3SStefan Hajnoczi 6267b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 6277b272452SKevin Wolf { 6287b272452SKevin Wolf int open_flags = flags | BDRV_O_CACHE_WB; 6297b272452SKevin Wolf 6307b272452SKevin Wolf /* 6317b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 6327b272452SKevin Wolf * image. 6337b272452SKevin Wolf */ 6347b272452SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 6357b272452SKevin Wolf 6367b272452SKevin Wolf /* 6377b272452SKevin Wolf * Snapshots should be writable. 6387b272452SKevin Wolf */ 6397b272452SKevin Wolf if (bs->is_temporary) { 6407b272452SKevin Wolf open_flags |= BDRV_O_RDWR; 6417b272452SKevin Wolf } 6427b272452SKevin Wolf 6437b272452SKevin Wolf return open_flags; 6447b272452SKevin Wolf } 6457b272452SKevin Wolf 646b6ce07aaSKevin Wolf /* 64757915332SKevin Wolf * Common part for opening disk images and files 64857915332SKevin Wolf */ 649f500a6d3SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, 650f500a6d3SKevin Wolf const char *filename, 65157915332SKevin Wolf int flags, BlockDriver *drv) 65257915332SKevin Wolf { 65357915332SKevin Wolf int ret, open_flags; 65457915332SKevin Wolf 65557915332SKevin Wolf assert(drv != NULL); 6566405875cSPaolo Bonzini assert(bs->file == NULL); 65757915332SKevin Wolf 65828dcee10SStefan Hajnoczi trace_bdrv_open_common(bs, filename, flags, drv->format_name); 65928dcee10SStefan Hajnoczi 66057915332SKevin Wolf bs->open_flags = flags; 66157915332SKevin Wolf bs->buffer_alignment = 512; 66257915332SKevin Wolf 66353fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 66453fec9d3SStefan Hajnoczi if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) { 66553fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 66653fec9d3SStefan Hajnoczi } 66753fec9d3SStefan Hajnoczi 66857915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 66957915332SKevin Wolf 67057915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 67157915332SKevin Wolf return -ENOTSUP; 67257915332SKevin Wolf } 67357915332SKevin Wolf 67457915332SKevin Wolf bs->drv = drv; 6757267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 67657915332SKevin Wolf 67703f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 6787b272452SKevin Wolf open_flags = bdrv_open_flags(bs, flags); 67957915332SKevin Wolf 680be028adcSJeff Cody bs->read_only = !(open_flags & BDRV_O_RDWR); 681e7c63796SStefan Hajnoczi 68266f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 68366f82ceeSKevin Wolf if (drv->bdrv_file_open) { 684f500a6d3SKevin Wolf if (file != NULL) { 685f500a6d3SKevin Wolf bdrv_swap(file, bs); 686f500a6d3SKevin Wolf ret = 0; 68766f82ceeSKevin Wolf } else { 688f500a6d3SKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 68966f82ceeSKevin Wolf } 690f500a6d3SKevin Wolf } else { 691f500a6d3SKevin Wolf assert(file != NULL); 692f500a6d3SKevin Wolf bs->file = file; 693f500a6d3SKevin Wolf ret = drv->bdrv_open(bs, open_flags); 69466f82ceeSKevin Wolf } 69566f82ceeSKevin Wolf 69657915332SKevin Wolf if (ret < 0) { 69757915332SKevin Wolf goto free_and_fail; 69857915332SKevin Wolf } 69957915332SKevin Wolf 70051762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 70151762288SStefan Hajnoczi if (ret < 0) { 70251762288SStefan Hajnoczi goto free_and_fail; 70357915332SKevin Wolf } 70451762288SStefan Hajnoczi 70557915332SKevin Wolf #ifndef _WIN32 70657915332SKevin Wolf if (bs->is_temporary) { 70757915332SKevin Wolf unlink(filename); 70857915332SKevin Wolf } 70957915332SKevin Wolf #endif 71057915332SKevin Wolf return 0; 71157915332SKevin Wolf 71257915332SKevin Wolf free_and_fail: 71366f82ceeSKevin Wolf bs->file = NULL; 7147267c094SAnthony Liguori g_free(bs->opaque); 71557915332SKevin Wolf bs->opaque = NULL; 71657915332SKevin Wolf bs->drv = NULL; 71757915332SKevin Wolf return ret; 71857915332SKevin Wolf } 71957915332SKevin Wolf 72057915332SKevin Wolf /* 721b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 722b6ce07aaSKevin Wolf */ 72383f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 724b338082bSbellard { 72583f64091Sbellard BlockDriverState *bs; 7266db95603SChristoph Hellwig BlockDriver *drv; 72783f64091Sbellard int ret; 7283b0d4f61Sbellard 729b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 7306db95603SChristoph Hellwig if (!drv) { 7316db95603SChristoph Hellwig return -ENOENT; 7326db95603SChristoph Hellwig } 7336db95603SChristoph Hellwig 73483f64091Sbellard bs = bdrv_new(""); 735f500a6d3SKevin Wolf ret = bdrv_open_common(bs, NULL, filename, flags, drv); 73683f64091Sbellard if (ret < 0) { 73783f64091Sbellard bdrv_delete(bs); 73883f64091Sbellard return ret; 7393b0d4f61Sbellard } 74071d0770cSaliguori bs->growable = 1; 74183f64091Sbellard *pbs = bs; 74283f64091Sbellard return 0; 7433b0d4f61Sbellard } 7443b0d4f61Sbellard 7459156df12SPaolo Bonzini int bdrv_open_backing_file(BlockDriverState *bs) 7469156df12SPaolo Bonzini { 7479156df12SPaolo Bonzini char backing_filename[PATH_MAX]; 7489156df12SPaolo Bonzini int back_flags, ret; 7499156df12SPaolo Bonzini BlockDriver *back_drv = NULL; 7509156df12SPaolo Bonzini 7519156df12SPaolo Bonzini if (bs->backing_hd != NULL) { 7529156df12SPaolo Bonzini return 0; 7539156df12SPaolo Bonzini } 7549156df12SPaolo Bonzini 7559156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 7569156df12SPaolo Bonzini if (bs->backing_file[0] == '\0') { 7579156df12SPaolo Bonzini return 0; 7589156df12SPaolo Bonzini } 7599156df12SPaolo Bonzini 7609156df12SPaolo Bonzini bs->backing_hd = bdrv_new(""); 7619156df12SPaolo Bonzini bdrv_get_full_backing_filename(bs, backing_filename, 7629156df12SPaolo Bonzini sizeof(backing_filename)); 7639156df12SPaolo Bonzini 7649156df12SPaolo Bonzini if (bs->backing_format[0] != '\0') { 7659156df12SPaolo Bonzini back_drv = bdrv_find_format(bs->backing_format); 7669156df12SPaolo Bonzini } 7679156df12SPaolo Bonzini 7689156df12SPaolo Bonzini /* backing files always opened read-only */ 7699156df12SPaolo Bonzini back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT); 7709156df12SPaolo Bonzini 7719156df12SPaolo Bonzini ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 7729156df12SPaolo Bonzini if (ret < 0) { 7739156df12SPaolo Bonzini bdrv_delete(bs->backing_hd); 7749156df12SPaolo Bonzini bs->backing_hd = NULL; 7759156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 7769156df12SPaolo Bonzini return ret; 7779156df12SPaolo Bonzini } 7789156df12SPaolo Bonzini return 0; 7799156df12SPaolo Bonzini } 7809156df12SPaolo Bonzini 781b6ce07aaSKevin Wolf /* 782b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 783b6ce07aaSKevin Wolf */ 784d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 785ea2384d3Sbellard BlockDriver *drv) 786ea2384d3Sbellard { 787b6ce07aaSKevin Wolf int ret; 78889c9bc3dSStefan Weil /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 78989c9bc3dSStefan Weil char tmp_filename[PATH_MAX + 1]; 790f500a6d3SKevin Wolf BlockDriverState *file = NULL; 79133e3963eSbellard 79283f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 793ea2384d3Sbellard BlockDriverState *bs1; 794ea2384d3Sbellard int64_t total_size; 7957c96d46eSaliguori int is_protocol = 0; 79691a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 79791a073a9SKevin Wolf QEMUOptionParameter *options; 798b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 79933e3963eSbellard 800ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 801ea2384d3Sbellard instead of opening 'filename' directly */ 802ea2384d3Sbellard 803ea2384d3Sbellard /* if there is a backing file, use it */ 804ea2384d3Sbellard bs1 = bdrv_new(""); 805d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 80651d7c00cSaliguori if (ret < 0) { 807ea2384d3Sbellard bdrv_delete(bs1); 80851d7c00cSaliguori return ret; 809ea2384d3Sbellard } 8103e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 8117c96d46eSaliguori 8127c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 8137c96d46eSaliguori is_protocol = 1; 8147c96d46eSaliguori 815ea2384d3Sbellard bdrv_delete(bs1); 816ea2384d3Sbellard 817eba25057SJim Meyering ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 818eba25057SJim Meyering if (ret < 0) { 819eba25057SJim Meyering return ret; 820eba25057SJim Meyering } 8217c96d46eSaliguori 8227c96d46eSaliguori /* Real path is meaningless for protocols */ 8237c96d46eSaliguori if (is_protocol) 8247c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 8257c96d46eSaliguori "%s", filename); 826114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 827114cdfa9SKirill A. Shutemov return -errno; 8287c96d46eSaliguori 82991a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 83091a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 83191a073a9SKevin Wolf 8323e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 83391a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 83491a073a9SKevin Wolf if (drv) { 83591a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 83691a073a9SKevin Wolf drv->format_name); 83791a073a9SKevin Wolf } 83891a073a9SKevin Wolf 83991a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 840d748768cSJan Kiszka free_option_parameters(options); 84151d7c00cSaliguori if (ret < 0) { 84251d7c00cSaliguori return ret; 843ea2384d3Sbellard } 84491a073a9SKevin Wolf 845ea2384d3Sbellard filename = tmp_filename; 84691a073a9SKevin Wolf drv = bdrv_qcow2; 847ea2384d3Sbellard bs->is_temporary = 1; 848ea2384d3Sbellard } 849ea2384d3Sbellard 850f500a6d3SKevin Wolf /* Open image file without format layer */ 851be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 852be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 853be028adcSJeff Cody } 854be028adcSJeff Cody 855f500a6d3SKevin Wolf ret = bdrv_file_open(&file, filename, bdrv_open_flags(bs, flags)); 856f500a6d3SKevin Wolf if (ret < 0) { 857f500a6d3SKevin Wolf return ret; 858f500a6d3SKevin Wolf } 859f500a6d3SKevin Wolf 860f500a6d3SKevin Wolf /* Find the right image format driver */ 861f500a6d3SKevin Wolf if (!drv) { 862f500a6d3SKevin Wolf ret = find_image_format(file, filename, &drv); 863f500a6d3SKevin Wolf } 864f500a6d3SKevin Wolf 865f500a6d3SKevin Wolf if (!drv) { 866f500a6d3SKevin Wolf goto unlink_and_fail; 867f500a6d3SKevin Wolf } 868f500a6d3SKevin Wolf 869b6ce07aaSKevin Wolf /* Open the image */ 870f500a6d3SKevin Wolf ret = bdrv_open_common(bs, file, filename, flags, drv); 871b6ce07aaSKevin Wolf if (ret < 0) { 8726987307cSChristoph Hellwig goto unlink_and_fail; 8736987307cSChristoph Hellwig } 8746987307cSChristoph Hellwig 875f500a6d3SKevin Wolf if (bs->file != file) { 876f500a6d3SKevin Wolf bdrv_delete(file); 877f500a6d3SKevin Wolf file = NULL; 878f500a6d3SKevin Wolf } 879f500a6d3SKevin Wolf 880b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 8819156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 8829156df12SPaolo Bonzini ret = bdrv_open_backing_file(bs); 883b6ce07aaSKevin Wolf if (ret < 0) { 884b6ce07aaSKevin Wolf bdrv_close(bs); 885b6ce07aaSKevin Wolf return ret; 886b6ce07aaSKevin Wolf } 887b6ce07aaSKevin Wolf } 888b6ce07aaSKevin Wolf 889b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 8907d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 891b6ce07aaSKevin Wolf } 892b6ce07aaSKevin Wolf 89398f90dbaSZhi Yong Wu /* throttling disk I/O limits */ 89498f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 89598f90dbaSZhi Yong Wu bdrv_io_limits_enable(bs); 89698f90dbaSZhi Yong Wu } 89798f90dbaSZhi Yong Wu 898b6ce07aaSKevin Wolf return 0; 899b6ce07aaSKevin Wolf 900b6ce07aaSKevin Wolf unlink_and_fail: 901f500a6d3SKevin Wolf if (file != NULL) { 902f500a6d3SKevin Wolf bdrv_delete(file); 903f500a6d3SKevin Wolf } 904b6ce07aaSKevin Wolf if (bs->is_temporary) { 905b6ce07aaSKevin Wolf unlink(filename); 906b6ce07aaSKevin Wolf } 907b6ce07aaSKevin Wolf return ret; 908b6ce07aaSKevin Wolf } 909b6ce07aaSKevin Wolf 910e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 911e971aa12SJeff Cody bool prepared; 912e971aa12SJeff Cody BDRVReopenState state; 913e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 914e971aa12SJeff Cody } BlockReopenQueueEntry; 915e971aa12SJeff Cody 916e971aa12SJeff Cody /* 917e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 918e971aa12SJeff Cody * reopen of multiple devices. 919e971aa12SJeff Cody * 920e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 921e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 922e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 923e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 924e971aa12SJeff Cody * atomic 'set'. 925e971aa12SJeff Cody * 926e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 927e971aa12SJeff Cody * 928e971aa12SJeff Cody * flags contains the open flags for the associated bs 929e971aa12SJeff Cody * 930e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 931e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 932e971aa12SJeff Cody * 933e971aa12SJeff Cody */ 934e971aa12SJeff Cody BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 935e971aa12SJeff Cody BlockDriverState *bs, int flags) 936e971aa12SJeff Cody { 937e971aa12SJeff Cody assert(bs != NULL); 938e971aa12SJeff Cody 939e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 940e971aa12SJeff Cody if (bs_queue == NULL) { 941e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 942e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 943e971aa12SJeff Cody } 944e971aa12SJeff Cody 945e971aa12SJeff Cody if (bs->file) { 946e971aa12SJeff Cody bdrv_reopen_queue(bs_queue, bs->file, flags); 947e971aa12SJeff Cody } 948e971aa12SJeff Cody 949e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 950e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 951e971aa12SJeff Cody 952e971aa12SJeff Cody bs_entry->state.bs = bs; 953e971aa12SJeff Cody bs_entry->state.flags = flags; 954e971aa12SJeff Cody 955e971aa12SJeff Cody return bs_queue; 956e971aa12SJeff Cody } 957e971aa12SJeff Cody 958e971aa12SJeff Cody /* 959e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 960e971aa12SJeff Cody * 961e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 962e971aa12SJeff Cody * via bdrv_reopen_queue(). 963e971aa12SJeff Cody * 964e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 965e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 966e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 967e971aa12SJeff Cody * data cleaned up. 968e971aa12SJeff Cody * 969e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 970e971aa12SJeff Cody * to all devices. 971e971aa12SJeff Cody * 972e971aa12SJeff Cody */ 973e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 974e971aa12SJeff Cody { 975e971aa12SJeff Cody int ret = -1; 976e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 977e971aa12SJeff Cody Error *local_err = NULL; 978e971aa12SJeff Cody 979e971aa12SJeff Cody assert(bs_queue != NULL); 980e971aa12SJeff Cody 981e971aa12SJeff Cody bdrv_drain_all(); 982e971aa12SJeff Cody 983e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 984e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 985e971aa12SJeff Cody error_propagate(errp, local_err); 986e971aa12SJeff Cody goto cleanup; 987e971aa12SJeff Cody } 988e971aa12SJeff Cody bs_entry->prepared = true; 989e971aa12SJeff Cody } 990e971aa12SJeff Cody 991e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 992e971aa12SJeff Cody * changes 993e971aa12SJeff Cody */ 994e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 995e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 996e971aa12SJeff Cody } 997e971aa12SJeff Cody 998e971aa12SJeff Cody ret = 0; 999e971aa12SJeff Cody 1000e971aa12SJeff Cody cleanup: 1001e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 1002e971aa12SJeff Cody if (ret && bs_entry->prepared) { 1003e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 1004e971aa12SJeff Cody } 1005e971aa12SJeff Cody g_free(bs_entry); 1006e971aa12SJeff Cody } 1007e971aa12SJeff Cody g_free(bs_queue); 1008e971aa12SJeff Cody return ret; 1009e971aa12SJeff Cody } 1010e971aa12SJeff Cody 1011e971aa12SJeff Cody 1012e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 1013e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1014e971aa12SJeff Cody { 1015e971aa12SJeff Cody int ret = -1; 1016e971aa12SJeff Cody Error *local_err = NULL; 1017e971aa12SJeff Cody BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); 1018e971aa12SJeff Cody 1019e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1020e971aa12SJeff Cody if (local_err != NULL) { 1021e971aa12SJeff Cody error_propagate(errp, local_err); 1022e971aa12SJeff Cody } 1023e971aa12SJeff Cody return ret; 1024e971aa12SJeff Cody } 1025e971aa12SJeff Cody 1026e971aa12SJeff Cody 1027e971aa12SJeff Cody /* 1028e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1029e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1030e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1031e971aa12SJeff Cody * 1032e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1033e971aa12SJeff Cody * flags are the new open flags 1034e971aa12SJeff Cody * queue is the reopen queue 1035e971aa12SJeff Cody * 1036e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1037e971aa12SJeff Cody * as well. 1038e971aa12SJeff Cody * 1039e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1040e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1041e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1042e971aa12SJeff Cody * 1043e971aa12SJeff Cody */ 1044e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1045e971aa12SJeff Cody Error **errp) 1046e971aa12SJeff Cody { 1047e971aa12SJeff Cody int ret = -1; 1048e971aa12SJeff Cody Error *local_err = NULL; 1049e971aa12SJeff Cody BlockDriver *drv; 1050e971aa12SJeff Cody 1051e971aa12SJeff Cody assert(reopen_state != NULL); 1052e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1053e971aa12SJeff Cody drv = reopen_state->bs->drv; 1054e971aa12SJeff Cody 1055e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 1056e971aa12SJeff Cody * to r/w */ 1057e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 1058e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 1059e971aa12SJeff Cody error_set(errp, QERR_DEVICE_IS_READ_ONLY, 1060e971aa12SJeff Cody reopen_state->bs->device_name); 1061e971aa12SJeff Cody goto error; 1062e971aa12SJeff Cody } 1063e971aa12SJeff Cody 1064e971aa12SJeff Cody 1065e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 1066e971aa12SJeff Cody if (ret) { 1067e971aa12SJeff Cody error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", 1068e971aa12SJeff Cody strerror(-ret)); 1069e971aa12SJeff Cody goto error; 1070e971aa12SJeff Cody } 1071e971aa12SJeff Cody 1072e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 1073e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 1074e971aa12SJeff Cody if (ret) { 1075e971aa12SJeff Cody if (local_err != NULL) { 1076e971aa12SJeff Cody error_propagate(errp, local_err); 1077e971aa12SJeff Cody } else { 1078e971aa12SJeff Cody error_set(errp, QERR_OPEN_FILE_FAILED, 1079e971aa12SJeff Cody reopen_state->bs->filename); 1080e971aa12SJeff Cody } 1081e971aa12SJeff Cody goto error; 1082e971aa12SJeff Cody } 1083e971aa12SJeff Cody } else { 1084e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 1085e971aa12SJeff Cody * handler for each supported drv. */ 1086e971aa12SJeff Cody error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 1087e971aa12SJeff Cody drv->format_name, reopen_state->bs->device_name, 1088e971aa12SJeff Cody "reopening of file"); 1089e971aa12SJeff Cody ret = -1; 1090e971aa12SJeff Cody goto error; 1091e971aa12SJeff Cody } 1092e971aa12SJeff Cody 1093e971aa12SJeff Cody ret = 0; 1094e971aa12SJeff Cody 1095e971aa12SJeff Cody error: 1096e971aa12SJeff Cody return ret; 1097e971aa12SJeff Cody } 1098e971aa12SJeff Cody 1099e971aa12SJeff Cody /* 1100e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 1101e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 1102e971aa12SJeff Cody * the active BlockDriverState contents. 1103e971aa12SJeff Cody */ 1104e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 1105e971aa12SJeff Cody { 1106e971aa12SJeff Cody BlockDriver *drv; 1107e971aa12SJeff Cody 1108e971aa12SJeff Cody assert(reopen_state != NULL); 1109e971aa12SJeff Cody drv = reopen_state->bs->drv; 1110e971aa12SJeff Cody assert(drv != NULL); 1111e971aa12SJeff Cody 1112e971aa12SJeff Cody /* If there are any driver level actions to take */ 1113e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 1114e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 1115e971aa12SJeff Cody } 1116e971aa12SJeff Cody 1117e971aa12SJeff Cody /* set BDS specific flags now */ 1118e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 1119e971aa12SJeff Cody reopen_state->bs->enable_write_cache = !!(reopen_state->flags & 1120e971aa12SJeff Cody BDRV_O_CACHE_WB); 1121e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 1122e971aa12SJeff Cody } 1123e971aa12SJeff Cody 1124e971aa12SJeff Cody /* 1125e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 1126e971aa12SJeff Cody * reopen_state 1127e971aa12SJeff Cody */ 1128e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 1129e971aa12SJeff Cody { 1130e971aa12SJeff Cody BlockDriver *drv; 1131e971aa12SJeff Cody 1132e971aa12SJeff Cody assert(reopen_state != NULL); 1133e971aa12SJeff Cody drv = reopen_state->bs->drv; 1134e971aa12SJeff Cody assert(drv != NULL); 1135e971aa12SJeff Cody 1136e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 1137e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 1138e971aa12SJeff Cody } 1139e971aa12SJeff Cody } 1140e971aa12SJeff Cody 1141e971aa12SJeff Cody 1142fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 1143fc01f7e7Sbellard { 114480ccf93bSLiu Yuan bdrv_flush(bs); 11453e914655SPaolo Bonzini if (bs->job) { 11463e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 11473e914655SPaolo Bonzini } 11487094f12fSKevin Wolf bdrv_drain_all(); 1149d7d512f6SPaolo Bonzini notifier_list_notify(&bs->close_notifiers, bs); 11507094f12fSKevin Wolf 11513cbc002cSPaolo Bonzini if (bs->drv) { 1152f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 1153f9092b10SMarkus Armbruster bs_snapshots = NULL; 1154f9092b10SMarkus Armbruster } 1155557df6acSStefan Hajnoczi if (bs->backing_hd) { 1156ea2384d3Sbellard bdrv_delete(bs->backing_hd); 1157557df6acSStefan Hajnoczi bs->backing_hd = NULL; 1158557df6acSStefan Hajnoczi } 1159ea2384d3Sbellard bs->drv->bdrv_close(bs); 11607267c094SAnthony Liguori g_free(bs->opaque); 1161ea2384d3Sbellard #ifdef _WIN32 1162ea2384d3Sbellard if (bs->is_temporary) { 1163ea2384d3Sbellard unlink(bs->filename); 1164ea2384d3Sbellard } 116567b915a5Sbellard #endif 1166ea2384d3Sbellard bs->opaque = NULL; 1167ea2384d3Sbellard bs->drv = NULL; 116853fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 1169a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 1170a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 11716405875cSPaolo Bonzini bs->total_sectors = 0; 11726405875cSPaolo Bonzini bs->encrypted = 0; 11736405875cSPaolo Bonzini bs->valid_key = 0; 11746405875cSPaolo Bonzini bs->sg = 0; 11756405875cSPaolo Bonzini bs->growable = 0; 1176b338082bSbellard 117766f82ceeSKevin Wolf if (bs->file != NULL) { 11780ac9377dSPaolo Bonzini bdrv_delete(bs->file); 11790ac9377dSPaolo Bonzini bs->file = NULL; 118066f82ceeSKevin Wolf } 11819ca11154SPavel Hrdina } 118266f82ceeSKevin Wolf 11837d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 118498f90dbaSZhi Yong Wu 118598f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 118698f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 118798f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 118898f90dbaSZhi Yong Wu } 1189b338082bSbellard } 1190b338082bSbellard 11912bc93fedSMORITA Kazutaka void bdrv_close_all(void) 11922bc93fedSMORITA Kazutaka { 11932bc93fedSMORITA Kazutaka BlockDriverState *bs; 11942bc93fedSMORITA Kazutaka 11952bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 11962bc93fedSMORITA Kazutaka bdrv_close(bs); 11972bc93fedSMORITA Kazutaka } 11982bc93fedSMORITA Kazutaka } 11992bc93fedSMORITA Kazutaka 1200922453bcSStefan Hajnoczi /* 1201922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 1202922453bcSStefan Hajnoczi * 1203922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 1204922453bcSStefan Hajnoczi * after calling this function. 12054c355d53SZhi Yong Wu * 12064c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 12074c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 12084c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 12094c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 12104c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 1211922453bcSStefan Hajnoczi */ 1212922453bcSStefan Hajnoczi void bdrv_drain_all(void) 1213922453bcSStefan Hajnoczi { 1214922453bcSStefan Hajnoczi BlockDriverState *bs; 12154c355d53SZhi Yong Wu bool busy; 1216922453bcSStefan Hajnoczi 12174c355d53SZhi Yong Wu do { 12184c355d53SZhi Yong Wu busy = qemu_aio_wait(); 12194c355d53SZhi Yong Wu 12204c355d53SZhi Yong Wu /* FIXME: We do not have timer support here, so this is effectively 12214c355d53SZhi Yong Wu * a busy wait. 12224c355d53SZhi Yong Wu */ 12234c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 12244c355d53SZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 12254c355d53SZhi Yong Wu qemu_co_queue_restart_all(&bs->throttled_reqs); 12264c355d53SZhi Yong Wu busy = true; 12274c355d53SZhi Yong Wu } 12284c355d53SZhi Yong Wu } 12294c355d53SZhi Yong Wu } while (busy); 1230922453bcSStefan Hajnoczi 1231922453bcSStefan Hajnoczi /* If requests are still pending there is a bug somewhere */ 1232922453bcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1233922453bcSStefan Hajnoczi assert(QLIST_EMPTY(&bs->tracked_requests)); 1234922453bcSStefan Hajnoczi assert(qemu_co_queue_empty(&bs->throttled_reqs)); 1235922453bcSStefan Hajnoczi } 1236922453bcSStefan Hajnoczi } 1237922453bcSStefan Hajnoczi 1238d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 1239d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1240d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1241d22b2f41SRyan Harper { 1242d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 1243d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 1244d22b2f41SRyan Harper } 1245d22b2f41SRyan Harper bs->device_name[0] = '\0'; 1246d22b2f41SRyan Harper } 1247d22b2f41SRyan Harper 1248e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1249e023b2e2SPaolo Bonzini { 1250e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1251e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1252e023b2e2SPaolo Bonzini } 1253e023b2e2SPaolo Bonzini } 1254e023b2e2SPaolo Bonzini 12554ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 12564ddc07caSPaolo Bonzini BlockDriverState *bs_src) 12574ddc07caSPaolo Bonzini { 12584ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 12594ddc07caSPaolo Bonzini bs_dest->open_flags = bs_src->open_flags; 12604ddc07caSPaolo Bonzini 12614ddc07caSPaolo Bonzini /* dev info */ 12624ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 12634ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 12644ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 12654ddc07caSPaolo Bonzini bs_dest->buffer_alignment = bs_src->buffer_alignment; 12664ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 12674ddc07caSPaolo Bonzini 12684ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 12694ddc07caSPaolo Bonzini 12704ddc07caSPaolo Bonzini /* i/o timing parameters */ 12714ddc07caSPaolo Bonzini bs_dest->slice_time = bs_src->slice_time; 12724ddc07caSPaolo Bonzini bs_dest->slice_start = bs_src->slice_start; 12734ddc07caSPaolo Bonzini bs_dest->slice_end = bs_src->slice_end; 12744ddc07caSPaolo Bonzini bs_dest->io_limits = bs_src->io_limits; 12754ddc07caSPaolo Bonzini bs_dest->io_base = bs_src->io_base; 12764ddc07caSPaolo Bonzini bs_dest->throttled_reqs = bs_src->throttled_reqs; 12774ddc07caSPaolo Bonzini bs_dest->block_timer = bs_src->block_timer; 12784ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 12794ddc07caSPaolo Bonzini 12804ddc07caSPaolo Bonzini /* r/w error */ 12814ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 12824ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 12834ddc07caSPaolo Bonzini 12844ddc07caSPaolo Bonzini /* i/o status */ 12854ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 12864ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 12874ddc07caSPaolo Bonzini 12884ddc07caSPaolo Bonzini /* dirty bitmap */ 12894ddc07caSPaolo Bonzini bs_dest->dirty_bitmap = bs_src->dirty_bitmap; 12904ddc07caSPaolo Bonzini 12914ddc07caSPaolo Bonzini /* job */ 12924ddc07caSPaolo Bonzini bs_dest->in_use = bs_src->in_use; 12934ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 12944ddc07caSPaolo Bonzini 12954ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 12964ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 12974ddc07caSPaolo Bonzini bs_src->device_name); 12984ddc07caSPaolo Bonzini bs_dest->list = bs_src->list; 12994ddc07caSPaolo Bonzini } 13004ddc07caSPaolo Bonzini 13014ddc07caSPaolo Bonzini /* 13024ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 13034ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 13044ddc07caSPaolo Bonzini * actually attached to a device. 13054ddc07caSPaolo Bonzini * 13064ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 13074ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 13084ddc07caSPaolo Bonzini * 13094ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 13104ddc07caSPaolo Bonzini * 13114ddc07caSPaolo Bonzini * This function does not create any image files. 13124ddc07caSPaolo Bonzini */ 13134ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 13144ddc07caSPaolo Bonzini { 13154ddc07caSPaolo Bonzini BlockDriverState tmp; 13164ddc07caSPaolo Bonzini 13174ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 13184ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 13194ddc07caSPaolo Bonzini assert(bs_new->dirty_bitmap == NULL); 13204ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 13214ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 13224ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 13234ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 13244ddc07caSPaolo Bonzini assert(bs_new->block_timer == NULL); 13254ddc07caSPaolo Bonzini 13264ddc07caSPaolo Bonzini tmp = *bs_new; 13274ddc07caSPaolo Bonzini *bs_new = *bs_old; 13284ddc07caSPaolo Bonzini *bs_old = tmp; 13294ddc07caSPaolo Bonzini 13304ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 13314ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 13324ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 13334ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 13344ddc07caSPaolo Bonzini 13354ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 13364ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 13374ddc07caSPaolo Bonzini 13384ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 13394ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 13404ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 13414ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 13424ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 13434ddc07caSPaolo Bonzini assert(bs_new->block_timer == NULL); 13444ddc07caSPaolo Bonzini 13454ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 13464ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 13474ddc07caSPaolo Bonzini } 13484ddc07caSPaolo Bonzini 13498802d1fdSJeff Cody /* 13508802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 13518802d1fdSJeff Cody * live, while keeping required fields on the top layer. 13528802d1fdSJeff Cody * 13538802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 13548802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 13558802d1fdSJeff Cody * 1356f6801b83SJeff Cody * bs_new is required to be anonymous. 1357f6801b83SJeff Cody * 13588802d1fdSJeff Cody * This function does not create any image files. 13598802d1fdSJeff Cody */ 13608802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 13618802d1fdSJeff Cody { 13624ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 13638802d1fdSJeff Cody 13648802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 13658802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 13664ddc07caSPaolo Bonzini bs_top->backing_hd = bs_new; 13674ddc07caSPaolo Bonzini bs_top->open_flags &= ~BDRV_O_NO_BACKING; 13684ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 13694ddc07caSPaolo Bonzini bs_new->filename); 13704ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 13714ddc07caSPaolo Bonzini bs_new->drv ? bs_new->drv->format_name : ""); 13728802d1fdSJeff Cody } 13738802d1fdSJeff Cody 1374b338082bSbellard void bdrv_delete(BlockDriverState *bs) 1375b338082bSbellard { 1376fa879d62SMarkus Armbruster assert(!bs->dev); 13773e914655SPaolo Bonzini assert(!bs->job); 13783e914655SPaolo Bonzini assert(!bs->in_use); 137918846deeSMarkus Armbruster 13801b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1381d22b2f41SRyan Harper bdrv_make_anon(bs); 138234c6f050Saurel32 1383b338082bSbellard bdrv_close(bs); 138466f82ceeSKevin Wolf 1385f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 13867267c094SAnthony Liguori g_free(bs); 1387fc01f7e7Sbellard } 1388fc01f7e7Sbellard 1389fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1390fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 139118846deeSMarkus Armbruster { 1392fa879d62SMarkus Armbruster if (bs->dev) { 139318846deeSMarkus Armbruster return -EBUSY; 139418846deeSMarkus Armbruster } 1395fa879d62SMarkus Armbruster bs->dev = dev; 139628a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 139718846deeSMarkus Armbruster return 0; 139818846deeSMarkus Armbruster } 139918846deeSMarkus Armbruster 1400fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1401fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 140218846deeSMarkus Armbruster { 1403fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1404fa879d62SMarkus Armbruster abort(); 1405fa879d62SMarkus Armbruster } 1406fa879d62SMarkus Armbruster } 1407fa879d62SMarkus Armbruster 1408fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1409fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1410fa879d62SMarkus Armbruster { 1411fa879d62SMarkus Armbruster assert(bs->dev == dev); 1412fa879d62SMarkus Armbruster bs->dev = NULL; 14130e49de52SMarkus Armbruster bs->dev_ops = NULL; 14140e49de52SMarkus Armbruster bs->dev_opaque = NULL; 141529e05f20SMarkus Armbruster bs->buffer_alignment = 512; 141618846deeSMarkus Armbruster } 141718846deeSMarkus Armbruster 1418fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1419fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 142018846deeSMarkus Armbruster { 1421fa879d62SMarkus Armbruster return bs->dev; 142218846deeSMarkus Armbruster } 142318846deeSMarkus Armbruster 14240e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 14250e49de52SMarkus Armbruster void *opaque) 14260e49de52SMarkus Armbruster { 14270e49de52SMarkus Armbruster bs->dev_ops = ops; 14280e49de52SMarkus Armbruster bs->dev_opaque = opaque; 14292c6942faSMarkus Armbruster if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) { 14302c6942faSMarkus Armbruster bs_snapshots = NULL; 14312c6942faSMarkus Armbruster } 14320e49de52SMarkus Armbruster } 14330e49de52SMarkus Armbruster 143432c81a4aSPaolo Bonzini void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 143532c81a4aSPaolo Bonzini enum MonitorEvent ev, 14361ceee0d5SPaolo Bonzini BlockErrorAction action, bool is_read) 1437329c0a48SLuiz Capitulino { 1438329c0a48SLuiz Capitulino QObject *data; 1439329c0a48SLuiz Capitulino const char *action_str; 1440329c0a48SLuiz Capitulino 1441329c0a48SLuiz Capitulino switch (action) { 1442329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1443329c0a48SLuiz Capitulino action_str = "report"; 1444329c0a48SLuiz Capitulino break; 1445329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1446329c0a48SLuiz Capitulino action_str = "ignore"; 1447329c0a48SLuiz Capitulino break; 1448329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1449329c0a48SLuiz Capitulino action_str = "stop"; 1450329c0a48SLuiz Capitulino break; 1451329c0a48SLuiz Capitulino default: 1452329c0a48SLuiz Capitulino abort(); 1453329c0a48SLuiz Capitulino } 1454329c0a48SLuiz Capitulino 1455329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1456329c0a48SLuiz Capitulino bdrv->device_name, 1457329c0a48SLuiz Capitulino action_str, 1458329c0a48SLuiz Capitulino is_read ? "read" : "write"); 145932c81a4aSPaolo Bonzini monitor_protocol_event(ev, data); 1460329c0a48SLuiz Capitulino 1461329c0a48SLuiz Capitulino qobject_decref(data); 1462329c0a48SLuiz Capitulino } 1463329c0a48SLuiz Capitulino 14646f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 14656f382ed2SLuiz Capitulino { 14666f382ed2SLuiz Capitulino QObject *data; 14676f382ed2SLuiz Capitulino 14686f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 14696f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 14706f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 14716f382ed2SLuiz Capitulino 14726f382ed2SLuiz Capitulino qobject_decref(data); 14736f382ed2SLuiz Capitulino } 14746f382ed2SLuiz Capitulino 14757d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 14760e49de52SMarkus Armbruster { 1477145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 14786f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 14797d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 14806f382ed2SLuiz Capitulino if (tray_was_closed) { 14816f382ed2SLuiz Capitulino /* tray open */ 14826f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 14836f382ed2SLuiz Capitulino } 14846f382ed2SLuiz Capitulino if (load) { 14856f382ed2SLuiz Capitulino /* tray close */ 14866f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 14876f382ed2SLuiz Capitulino } 1488145feb17SMarkus Armbruster } 1489145feb17SMarkus Armbruster } 1490145feb17SMarkus Armbruster 14912c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 14922c6942faSMarkus Armbruster { 14932c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 14942c6942faSMarkus Armbruster } 14952c6942faSMarkus Armbruster 1496025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1497025ccaa7SPaolo Bonzini { 1498025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1499025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1500025ccaa7SPaolo Bonzini } 1501025ccaa7SPaolo Bonzini } 1502025ccaa7SPaolo Bonzini 1503e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1504e4def80bSMarkus Armbruster { 1505e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1506e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1507e4def80bSMarkus Armbruster } 1508e4def80bSMarkus Armbruster return false; 1509e4def80bSMarkus Armbruster } 1510e4def80bSMarkus Armbruster 1511145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1512145feb17SMarkus Armbruster { 1513145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1514145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 15150e49de52SMarkus Armbruster } 15160e49de52SMarkus Armbruster } 15170e49de52SMarkus Armbruster 1518f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1519f107639aSMarkus Armbruster { 1520f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1521f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1522f107639aSMarkus Armbruster } 1523f107639aSMarkus Armbruster return false; 1524f107639aSMarkus Armbruster } 1525f107639aSMarkus Armbruster 1526e97fc193Saliguori /* 1527e97fc193Saliguori * Run consistency checks on an image 1528e97fc193Saliguori * 1529e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1530a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1531e076f338SKevin Wolf * check are stored in res. 1532e97fc193Saliguori */ 15334534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 1534e97fc193Saliguori { 1535e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1536e97fc193Saliguori return -ENOTSUP; 1537e97fc193Saliguori } 1538e97fc193Saliguori 1539e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 15404534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 1541e97fc193Saliguori } 1542e97fc193Saliguori 15438a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 15448a426614SKevin Wolf 154533e3963eSbellard /* commit COW file into the raw image */ 154633e3963eSbellard int bdrv_commit(BlockDriverState *bs) 154733e3963eSbellard { 154819cb3738Sbellard BlockDriver *drv = bs->drv; 15498a426614SKevin Wolf int64_t sector, total_sectors; 15508a426614SKevin Wolf int n, ro, open_flags; 15510bce597dSJeff Cody int ret = 0; 15528a426614SKevin Wolf uint8_t *buf; 1553c2cba3d9SJim Meyering char filename[PATH_MAX]; 155433e3963eSbellard 155519cb3738Sbellard if (!drv) 155619cb3738Sbellard return -ENOMEDIUM; 155733e3963eSbellard 15584dca4b63SNaphtali Sprei if (!bs->backing_hd) { 15594dca4b63SNaphtali Sprei return -ENOTSUP; 15604dca4b63SNaphtali Sprei } 15614dca4b63SNaphtali Sprei 15622d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 15632d3735d3SStefan Hajnoczi return -EBUSY; 15642d3735d3SStefan Hajnoczi } 15652d3735d3SStefan Hajnoczi 15664dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 1567c2cba3d9SJim Meyering /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ 1568c2cba3d9SJim Meyering pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); 15694dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 15704dca4b63SNaphtali Sprei 15714dca4b63SNaphtali Sprei if (ro) { 15720bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 15730bce597dSJeff Cody return -EACCES; 15744dca4b63SNaphtali Sprei } 1575ea2384d3Sbellard } 1576ea2384d3Sbellard 15776ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 15787267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 15798a426614SKevin Wolf 15808a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 158105c4af54SStefan Hajnoczi if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 15828a426614SKevin Wolf 15838a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 15844dca4b63SNaphtali Sprei ret = -EIO; 15854dca4b63SNaphtali Sprei goto ro_cleanup; 158633e3963eSbellard } 158733e3963eSbellard 15888a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 15894dca4b63SNaphtali Sprei ret = -EIO; 15904dca4b63SNaphtali Sprei goto ro_cleanup; 159133e3963eSbellard } 159233e3963eSbellard } 159333e3963eSbellard } 159495389c86Sbellard 15951d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 15961d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 15971d44952fSChristoph Hellwig bdrv_flush(bs); 15981d44952fSChristoph Hellwig } 159995389c86Sbellard 16003f5075aeSChristoph Hellwig /* 16013f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 16023f5075aeSChristoph Hellwig * stable on disk. 16033f5075aeSChristoph Hellwig */ 16043f5075aeSChristoph Hellwig if (bs->backing_hd) 16053f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 16064dca4b63SNaphtali Sprei 16074dca4b63SNaphtali Sprei ro_cleanup: 16087267c094SAnthony Liguori g_free(buf); 16094dca4b63SNaphtali Sprei 16104dca4b63SNaphtali Sprei if (ro) { 16110bce597dSJeff Cody /* ignoring error return here */ 16120bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 16134dca4b63SNaphtali Sprei } 16144dca4b63SNaphtali Sprei 16151d44952fSChristoph Hellwig return ret; 161633e3963eSbellard } 161733e3963eSbellard 1618e8877497SStefan Hajnoczi int bdrv_commit_all(void) 16196ab4b5abSMarkus Armbruster { 16206ab4b5abSMarkus Armbruster BlockDriverState *bs; 16216ab4b5abSMarkus Armbruster 16226ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1623e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1624e8877497SStefan Hajnoczi if (ret < 0) { 1625e8877497SStefan Hajnoczi return ret; 16266ab4b5abSMarkus Armbruster } 16276ab4b5abSMarkus Armbruster } 1628e8877497SStefan Hajnoczi return 0; 1629e8877497SStefan Hajnoczi } 16306ab4b5abSMarkus Armbruster 1631dbffbdcfSStefan Hajnoczi struct BdrvTrackedRequest { 1632dbffbdcfSStefan Hajnoczi BlockDriverState *bs; 1633dbffbdcfSStefan Hajnoczi int64_t sector_num; 1634dbffbdcfSStefan Hajnoczi int nb_sectors; 1635dbffbdcfSStefan Hajnoczi bool is_write; 1636dbffbdcfSStefan Hajnoczi QLIST_ENTRY(BdrvTrackedRequest) list; 16375f8b6491SStefan Hajnoczi Coroutine *co; /* owner, used for deadlock detection */ 1638f4658285SStefan Hajnoczi CoQueue wait_queue; /* coroutines blocked on this request */ 1639dbffbdcfSStefan Hajnoczi }; 1640dbffbdcfSStefan Hajnoczi 1641dbffbdcfSStefan Hajnoczi /** 1642dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1643dbffbdcfSStefan Hajnoczi * 1644dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1645dbffbdcfSStefan Hajnoczi */ 1646dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1647dbffbdcfSStefan Hajnoczi { 1648dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1649f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1650dbffbdcfSStefan Hajnoczi } 1651dbffbdcfSStefan Hajnoczi 1652dbffbdcfSStefan Hajnoczi /** 1653dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1654dbffbdcfSStefan Hajnoczi */ 1655dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1656dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1657dbffbdcfSStefan Hajnoczi int64_t sector_num, 1658dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1659dbffbdcfSStefan Hajnoczi { 1660dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1661dbffbdcfSStefan Hajnoczi .bs = bs, 1662dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1663dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1664dbffbdcfSStefan Hajnoczi .is_write = is_write, 16655f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1666dbffbdcfSStefan Hajnoczi }; 1667dbffbdcfSStefan Hajnoczi 1668f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1669f4658285SStefan Hajnoczi 1670dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1671dbffbdcfSStefan Hajnoczi } 1672dbffbdcfSStefan Hajnoczi 1673d83947acSStefan Hajnoczi /** 1674d83947acSStefan Hajnoczi * Round a region to cluster boundaries 1675d83947acSStefan Hajnoczi */ 1676343bded4SPaolo Bonzini void bdrv_round_to_clusters(BlockDriverState *bs, 1677d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 1678d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 1679d83947acSStefan Hajnoczi int *cluster_nb_sectors) 1680d83947acSStefan Hajnoczi { 1681d83947acSStefan Hajnoczi BlockDriverInfo bdi; 1682d83947acSStefan Hajnoczi 1683d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 1684d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 1685d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 1686d83947acSStefan Hajnoczi } else { 1687d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 1688d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 1689d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 1690d83947acSStefan Hajnoczi nb_sectors, c); 1691d83947acSStefan Hajnoczi } 1692d83947acSStefan Hajnoczi } 1693d83947acSStefan Hajnoczi 1694f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 1695f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 1696d83947acSStefan Hajnoczi /* aaaa bbbb */ 1697d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 1698d83947acSStefan Hajnoczi return false; 1699d83947acSStefan Hajnoczi } 1700d83947acSStefan Hajnoczi /* bbbb aaaa */ 1701d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 1702d83947acSStefan Hajnoczi return false; 1703d83947acSStefan Hajnoczi } 1704d83947acSStefan Hajnoczi return true; 1705f4658285SStefan Hajnoczi } 1706f4658285SStefan Hajnoczi 1707f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 1708f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 1709f4658285SStefan Hajnoczi { 1710f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 1711d83947acSStefan Hajnoczi int64_t cluster_sector_num; 1712d83947acSStefan Hajnoczi int cluster_nb_sectors; 1713f4658285SStefan Hajnoczi bool retry; 1714f4658285SStefan Hajnoczi 1715d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 1716d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 1717d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 1718d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 1719d83947acSStefan Hajnoczi * interleave between them. 1720d83947acSStefan Hajnoczi */ 1721343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 1722d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1723d83947acSStefan Hajnoczi 1724f4658285SStefan Hajnoczi do { 1725f4658285SStefan Hajnoczi retry = false; 1726f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 1727d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 1728d83947acSStefan Hajnoczi cluster_nb_sectors)) { 17295f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 17305f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 17315f8b6491SStefan Hajnoczi * never happen since it means deadlock. 17325f8b6491SStefan Hajnoczi */ 17335f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 17345f8b6491SStefan Hajnoczi 1735f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 1736f4658285SStefan Hajnoczi retry = true; 1737f4658285SStefan Hajnoczi break; 1738f4658285SStefan Hajnoczi } 1739f4658285SStefan Hajnoczi } 1740f4658285SStefan Hajnoczi } while (retry); 1741f4658285SStefan Hajnoczi } 1742f4658285SStefan Hajnoczi 1743756e6736SKevin Wolf /* 1744756e6736SKevin Wolf * Return values: 1745756e6736SKevin Wolf * 0 - success 1746756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 1747756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 1748756e6736SKevin Wolf * image file header 1749756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 1750756e6736SKevin Wolf */ 1751756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 1752756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1753756e6736SKevin Wolf { 1754756e6736SKevin Wolf BlockDriver *drv = bs->drv; 1755469ef350SPaolo Bonzini int ret; 1756756e6736SKevin Wolf 17575f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 17585f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 17595f377794SPaolo Bonzini return -EINVAL; 17605f377794SPaolo Bonzini } 17615f377794SPaolo Bonzini 1762756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 1763469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 1764756e6736SKevin Wolf } else { 1765469ef350SPaolo Bonzini ret = -ENOTSUP; 1766756e6736SKevin Wolf } 1767469ef350SPaolo Bonzini 1768469ef350SPaolo Bonzini if (ret == 0) { 1769469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1770469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1771469ef350SPaolo Bonzini } 1772469ef350SPaolo Bonzini return ret; 1773756e6736SKevin Wolf } 1774756e6736SKevin Wolf 17756ebdcee2SJeff Cody /* 17766ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 17776ebdcee2SJeff Cody * 17786ebdcee2SJeff Cody * active is the current topmost image. 17796ebdcee2SJeff Cody * 17806ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 17816ebdcee2SJeff Cody * or if active == bs. 17826ebdcee2SJeff Cody */ 17836ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 17846ebdcee2SJeff Cody BlockDriverState *bs) 17856ebdcee2SJeff Cody { 17866ebdcee2SJeff Cody BlockDriverState *overlay = NULL; 17876ebdcee2SJeff Cody BlockDriverState *intermediate; 17886ebdcee2SJeff Cody 17896ebdcee2SJeff Cody assert(active != NULL); 17906ebdcee2SJeff Cody assert(bs != NULL); 17916ebdcee2SJeff Cody 17926ebdcee2SJeff Cody /* if bs is the same as active, then by definition it has no overlay 17936ebdcee2SJeff Cody */ 17946ebdcee2SJeff Cody if (active == bs) { 17956ebdcee2SJeff Cody return NULL; 17966ebdcee2SJeff Cody } 17976ebdcee2SJeff Cody 17986ebdcee2SJeff Cody intermediate = active; 17996ebdcee2SJeff Cody while (intermediate->backing_hd) { 18006ebdcee2SJeff Cody if (intermediate->backing_hd == bs) { 18016ebdcee2SJeff Cody overlay = intermediate; 18026ebdcee2SJeff Cody break; 18036ebdcee2SJeff Cody } 18046ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 18056ebdcee2SJeff Cody } 18066ebdcee2SJeff Cody 18076ebdcee2SJeff Cody return overlay; 18086ebdcee2SJeff Cody } 18096ebdcee2SJeff Cody 18106ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 18116ebdcee2SJeff Cody BlockDriverState *bs; 18126ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 18136ebdcee2SJeff Cody } BlkIntermediateStates; 18146ebdcee2SJeff Cody 18156ebdcee2SJeff Cody 18166ebdcee2SJeff Cody /* 18176ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 18186ebdcee2SJeff Cody * above 'top' to have base as its backing file. 18196ebdcee2SJeff Cody * 18206ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 18216ebdcee2SJeff Cody * information in 'bs' can be properly updated. 18226ebdcee2SJeff Cody * 18236ebdcee2SJeff Cody * E.g., this will convert the following chain: 18246ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 18256ebdcee2SJeff Cody * 18266ebdcee2SJeff Cody * to 18276ebdcee2SJeff Cody * 18286ebdcee2SJeff Cody * bottom <- base <- active 18296ebdcee2SJeff Cody * 18306ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 18316ebdcee2SJeff Cody * 18326ebdcee2SJeff Cody * base <- intermediate <- top <- active 18336ebdcee2SJeff Cody * 18346ebdcee2SJeff Cody * to 18356ebdcee2SJeff Cody * 18366ebdcee2SJeff Cody * base <- active 18376ebdcee2SJeff Cody * 18386ebdcee2SJeff Cody * Error conditions: 18396ebdcee2SJeff Cody * if active == top, that is considered an error 18406ebdcee2SJeff Cody * 18416ebdcee2SJeff Cody */ 18426ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 18436ebdcee2SJeff Cody BlockDriverState *base) 18446ebdcee2SJeff Cody { 18456ebdcee2SJeff Cody BlockDriverState *intermediate; 18466ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 18476ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 18486ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 18496ebdcee2SJeff Cody int ret = -EIO; 18506ebdcee2SJeff Cody 18516ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 18526ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 18536ebdcee2SJeff Cody 18546ebdcee2SJeff Cody if (!top->drv || !base->drv) { 18556ebdcee2SJeff Cody goto exit; 18566ebdcee2SJeff Cody } 18576ebdcee2SJeff Cody 18586ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 18596ebdcee2SJeff Cody 18606ebdcee2SJeff Cody if (new_top_bs == NULL) { 18616ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 18626ebdcee2SJeff Cody goto exit; 18636ebdcee2SJeff Cody } 18646ebdcee2SJeff Cody 18656ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 18666ebdcee2SJeff Cody * to do, no intermediate images */ 18676ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 18686ebdcee2SJeff Cody ret = 0; 18696ebdcee2SJeff Cody goto exit; 18706ebdcee2SJeff Cody } 18716ebdcee2SJeff Cody 18726ebdcee2SJeff Cody intermediate = top; 18736ebdcee2SJeff Cody 18746ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 18756ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 18766ebdcee2SJeff Cody */ 18776ebdcee2SJeff Cody while (intermediate) { 18786ebdcee2SJeff Cody intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); 18796ebdcee2SJeff Cody intermediate_state->bs = intermediate; 18806ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 18816ebdcee2SJeff Cody 18826ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 18836ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 18846ebdcee2SJeff Cody break; 18856ebdcee2SJeff Cody } 18866ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 18876ebdcee2SJeff Cody } 18886ebdcee2SJeff Cody if (base_bs == NULL) { 18896ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 18906ebdcee2SJeff Cody * unravel everything, and exit with error */ 18916ebdcee2SJeff Cody goto exit; 18926ebdcee2SJeff Cody } 18936ebdcee2SJeff Cody 18946ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 18956ebdcee2SJeff Cody ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, 18966ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 18976ebdcee2SJeff Cody if (ret) { 18986ebdcee2SJeff Cody goto exit; 18996ebdcee2SJeff Cody } 19006ebdcee2SJeff Cody new_top_bs->backing_hd = base_bs; 19016ebdcee2SJeff Cody 19026ebdcee2SJeff Cody 19036ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 19046ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 19056ebdcee2SJeff Cody intermediate_state->bs->backing_hd = NULL; 19066ebdcee2SJeff Cody bdrv_delete(intermediate_state->bs); 19076ebdcee2SJeff Cody } 19086ebdcee2SJeff Cody ret = 0; 19096ebdcee2SJeff Cody 19106ebdcee2SJeff Cody exit: 19116ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 19126ebdcee2SJeff Cody g_free(intermediate_state); 19136ebdcee2SJeff Cody } 19146ebdcee2SJeff Cody return ret; 19156ebdcee2SJeff Cody } 19166ebdcee2SJeff Cody 19176ebdcee2SJeff Cody 191871d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 191971d0770cSaliguori size_t size) 192071d0770cSaliguori { 192171d0770cSaliguori int64_t len; 192271d0770cSaliguori 192371d0770cSaliguori if (!bdrv_is_inserted(bs)) 192471d0770cSaliguori return -ENOMEDIUM; 192571d0770cSaliguori 192671d0770cSaliguori if (bs->growable) 192771d0770cSaliguori return 0; 192871d0770cSaliguori 192971d0770cSaliguori len = bdrv_getlength(bs); 193071d0770cSaliguori 1931fbb7b4e0SKevin Wolf if (offset < 0) 1932fbb7b4e0SKevin Wolf return -EIO; 1933fbb7b4e0SKevin Wolf 1934fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 193571d0770cSaliguori return -EIO; 193671d0770cSaliguori 193771d0770cSaliguori return 0; 193871d0770cSaliguori } 193971d0770cSaliguori 194071d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 194171d0770cSaliguori int nb_sectors) 194271d0770cSaliguori { 1943eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1944eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 194571d0770cSaliguori } 194671d0770cSaliguori 19471c9805a3SStefan Hajnoczi typedef struct RwCo { 19481c9805a3SStefan Hajnoczi BlockDriverState *bs; 19491c9805a3SStefan Hajnoczi int64_t sector_num; 19501c9805a3SStefan Hajnoczi int nb_sectors; 19511c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 19521c9805a3SStefan Hajnoczi bool is_write; 19531c9805a3SStefan Hajnoczi int ret; 19541c9805a3SStefan Hajnoczi } RwCo; 19551c9805a3SStefan Hajnoczi 19561c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 1957fc01f7e7Sbellard { 19581c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 1959fc01f7e7Sbellard 19601c9805a3SStefan Hajnoczi if (!rwco->is_write) { 19611c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 1962470c0504SStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 19631c9805a3SStefan Hajnoczi } else { 19641c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 1965f08f2ddaSStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 19661c9805a3SStefan Hajnoczi } 19671c9805a3SStefan Hajnoczi } 1968e7a8a783SKevin Wolf 19691c9805a3SStefan Hajnoczi /* 19701c9805a3SStefan Hajnoczi * Process a synchronous request using coroutines 19711c9805a3SStefan Hajnoczi */ 19721c9805a3SStefan Hajnoczi static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 19731c9805a3SStefan Hajnoczi int nb_sectors, bool is_write) 19741c9805a3SStefan Hajnoczi { 1975e7a8a783SKevin Wolf QEMUIOVector qiov; 1976e7a8a783SKevin Wolf struct iovec iov = { 1977e7a8a783SKevin Wolf .iov_base = (void *)buf, 1978e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 1979e7a8a783SKevin Wolf }; 19801c9805a3SStefan Hajnoczi Coroutine *co; 19811c9805a3SStefan Hajnoczi RwCo rwco = { 19821c9805a3SStefan Hajnoczi .bs = bs, 19831c9805a3SStefan Hajnoczi .sector_num = sector_num, 19841c9805a3SStefan Hajnoczi .nb_sectors = nb_sectors, 19851c9805a3SStefan Hajnoczi .qiov = &qiov, 19861c9805a3SStefan Hajnoczi .is_write = is_write, 19871c9805a3SStefan Hajnoczi .ret = NOT_DONE, 19881c9805a3SStefan Hajnoczi }; 1989e7a8a783SKevin Wolf 1990e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 19911c9805a3SStefan Hajnoczi 1992498e386cSZhi Yong Wu /** 1993498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 1994498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 1995498e386cSZhi Yong Wu * if it has been enabled. 1996498e386cSZhi Yong Wu */ 1997498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 1998498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 1999498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 2000498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 2001498e386cSZhi Yong Wu } 2002498e386cSZhi Yong Wu 20031c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 20041c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 20051c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 20061c9805a3SStefan Hajnoczi } else { 20071c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 20081c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 20091c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 20101c9805a3SStefan Hajnoczi qemu_aio_wait(); 20111c9805a3SStefan Hajnoczi } 20121c9805a3SStefan Hajnoczi } 20131c9805a3SStefan Hajnoczi return rwco.ret; 2014e7a8a783SKevin Wolf } 2015e7a8a783SKevin Wolf 20161c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 20171c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 20181c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 20191c9805a3SStefan Hajnoczi { 20201c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); 202183f64091Sbellard } 2022fc01f7e7Sbellard 202307d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 202407d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 202507d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 202607d27a44SMarkus Armbruster { 202707d27a44SMarkus Armbruster bool enabled; 202807d27a44SMarkus Armbruster int ret; 202907d27a44SMarkus Armbruster 203007d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 203107d27a44SMarkus Armbruster bs->io_limits_enabled = false; 203207d27a44SMarkus Armbruster ret = bdrv_read(bs, 0, buf, 1); 203307d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 203407d27a44SMarkus Armbruster return ret; 203507d27a44SMarkus Armbruster } 203607d27a44SMarkus Armbruster 203719cb3738Sbellard /* Return < 0 if error. Important errors are: 203819cb3738Sbellard -EIO generic I/O error (may happen for all errors) 203919cb3738Sbellard -ENOMEDIUM No media inserted. 204019cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 204119cb3738Sbellard -EACCES Trying to write a read-only device 204219cb3738Sbellard */ 2043fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 2044fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 2045fc01f7e7Sbellard { 20461c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); 204783f64091Sbellard } 204883f64091Sbellard 2049eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 2050eda578e5Saliguori void *buf, int count1) 205183f64091Sbellard { 20526ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 205383f64091Sbellard int len, nb_sectors, count; 205483f64091Sbellard int64_t sector_num; 20559a8c4cceSKevin Wolf int ret; 205683f64091Sbellard 205783f64091Sbellard count = count1; 205883f64091Sbellard /* first read to align to sector start */ 20596ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 206083f64091Sbellard if (len > count) 206183f64091Sbellard len = count; 20626ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 206383f64091Sbellard if (len > 0) { 20649a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 20659a8c4cceSKevin Wolf return ret; 20666ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 206783f64091Sbellard count -= len; 206883f64091Sbellard if (count == 0) 206983f64091Sbellard return count1; 207083f64091Sbellard sector_num++; 207183f64091Sbellard buf += len; 207283f64091Sbellard } 207383f64091Sbellard 207483f64091Sbellard /* read the sectors "in place" */ 20756ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 207683f64091Sbellard if (nb_sectors > 0) { 20779a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 20789a8c4cceSKevin Wolf return ret; 207983f64091Sbellard sector_num += nb_sectors; 20806ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 208183f64091Sbellard buf += len; 208283f64091Sbellard count -= len; 208383f64091Sbellard } 208483f64091Sbellard 208583f64091Sbellard /* add data from the last sector */ 208683f64091Sbellard if (count > 0) { 20879a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 20889a8c4cceSKevin Wolf return ret; 208983f64091Sbellard memcpy(buf, tmp_buf, count); 209083f64091Sbellard } 209183f64091Sbellard return count1; 209283f64091Sbellard } 209383f64091Sbellard 2094eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 2095eda578e5Saliguori const void *buf, int count1) 209683f64091Sbellard { 20976ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 209883f64091Sbellard int len, nb_sectors, count; 209983f64091Sbellard int64_t sector_num; 21009a8c4cceSKevin Wolf int ret; 210183f64091Sbellard 210283f64091Sbellard count = count1; 210383f64091Sbellard /* first write to align to sector start */ 21046ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 210583f64091Sbellard if (len > count) 210683f64091Sbellard len = count; 21076ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 210883f64091Sbellard if (len > 0) { 21099a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 21109a8c4cceSKevin Wolf return ret; 21116ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 21129a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 21139a8c4cceSKevin Wolf return ret; 211483f64091Sbellard count -= len; 211583f64091Sbellard if (count == 0) 211683f64091Sbellard return count1; 211783f64091Sbellard sector_num++; 211883f64091Sbellard buf += len; 211983f64091Sbellard } 212083f64091Sbellard 212183f64091Sbellard /* write the sectors "in place" */ 21226ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 212383f64091Sbellard if (nb_sectors > 0) { 21249a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 21259a8c4cceSKevin Wolf return ret; 212683f64091Sbellard sector_num += nb_sectors; 21276ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 212883f64091Sbellard buf += len; 212983f64091Sbellard count -= len; 213083f64091Sbellard } 213183f64091Sbellard 213283f64091Sbellard /* add data from the last sector */ 213383f64091Sbellard if (count > 0) { 21349a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 21359a8c4cceSKevin Wolf return ret; 213683f64091Sbellard memcpy(tmp_buf, buf, count); 21379a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 21389a8c4cceSKevin Wolf return ret; 213983f64091Sbellard } 214083f64091Sbellard return count1; 214183f64091Sbellard } 214283f64091Sbellard 2143f08145feSKevin Wolf /* 2144f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 2145f08145feSKevin Wolf * request (acts as a barrier) 2146f08145feSKevin Wolf * 2147f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 2148f08145feSKevin Wolf */ 2149f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 2150f08145feSKevin Wolf const void *buf, int count) 2151f08145feSKevin Wolf { 2152f08145feSKevin Wolf int ret; 2153f08145feSKevin Wolf 2154f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 2155f08145feSKevin Wolf if (ret < 0) { 2156f08145feSKevin Wolf return ret; 2157f08145feSKevin Wolf } 2158f08145feSKevin Wolf 2159f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 2160f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 2161f08145feSKevin Wolf bdrv_flush(bs); 2162f08145feSKevin Wolf } 2163f08145feSKevin Wolf 2164f08145feSKevin Wolf return 0; 2165f08145feSKevin Wolf } 2166f08145feSKevin Wolf 2167470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 2168ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2169ab185921SStefan Hajnoczi { 2170ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 2171ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 2172ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 2173ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 2174ab185921SStefan Hajnoczi */ 2175ab185921SStefan Hajnoczi void *bounce_buffer; 2176ab185921SStefan Hajnoczi 217779c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 2178ab185921SStefan Hajnoczi struct iovec iov; 2179ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 2180ab185921SStefan Hajnoczi int64_t cluster_sector_num; 2181ab185921SStefan Hajnoczi int cluster_nb_sectors; 2182ab185921SStefan Hajnoczi size_t skip_bytes; 2183ab185921SStefan Hajnoczi int ret; 2184ab185921SStefan Hajnoczi 2185ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 2186ab185921SStefan Hajnoczi * allocating cluster in the image file. 2187ab185921SStefan Hajnoczi */ 2188343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2189ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2190ab185921SStefan Hajnoczi 2191470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 2192ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 2193ab185921SStefan Hajnoczi 2194ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 2195ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 2196ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 2197ab185921SStefan Hajnoczi 219879c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 2199ab185921SStefan Hajnoczi &bounce_qiov); 2200ab185921SStefan Hajnoczi if (ret < 0) { 2201ab185921SStefan Hajnoczi goto err; 2202ab185921SStefan Hajnoczi } 2203ab185921SStefan Hajnoczi 220479c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 220579c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 2206621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 220779c053bdSStefan Hajnoczi cluster_nb_sectors); 220879c053bdSStefan Hajnoczi } else { 2209f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 2210f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 2211f05fa4adSPaolo Bonzini */ 221279c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 2213ab185921SStefan Hajnoczi &bounce_qiov); 221479c053bdSStefan Hajnoczi } 221579c053bdSStefan Hajnoczi 2216ab185921SStefan Hajnoczi if (ret < 0) { 2217ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 2218ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 2219ab185921SStefan Hajnoczi * Simply report it in all cases. 2220ab185921SStefan Hajnoczi */ 2221ab185921SStefan Hajnoczi goto err; 2222ab185921SStefan Hajnoczi } 2223ab185921SStefan Hajnoczi 2224ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 222503396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 2226ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 2227ab185921SStefan Hajnoczi 2228ab185921SStefan Hajnoczi err: 2229ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 2230ab185921SStefan Hajnoczi return ret; 2231ab185921SStefan Hajnoczi } 2232ab185921SStefan Hajnoczi 2233c5fbe571SStefan Hajnoczi /* 2234c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 2235c5fbe571SStefan Hajnoczi */ 2236c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 2237470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2238470c0504SStefan Hajnoczi BdrvRequestFlags flags) 2239da1fa91dSKevin Wolf { 2240da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 2241dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 2242dbffbdcfSStefan Hajnoczi int ret; 2243da1fa91dSKevin Wolf 2244da1fa91dSKevin Wolf if (!drv) { 2245da1fa91dSKevin Wolf return -ENOMEDIUM; 2246da1fa91dSKevin Wolf } 2247da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2248da1fa91dSKevin Wolf return -EIO; 2249da1fa91dSKevin Wolf } 2250da1fa91dSKevin Wolf 225198f90dbaSZhi Yong Wu /* throttling disk read I/O */ 225298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 225398f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, false, nb_sectors); 225498f90dbaSZhi Yong Wu } 225598f90dbaSZhi Yong Wu 2256f4658285SStefan Hajnoczi if (bs->copy_on_read) { 2257470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 2258470c0504SStefan Hajnoczi } 2259470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2260470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 2261470c0504SStefan Hajnoczi } 2262470c0504SStefan Hajnoczi 2263470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2264f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2265f4658285SStefan Hajnoczi } 2266f4658285SStefan Hajnoczi 2267dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 2268ab185921SStefan Hajnoczi 2269470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2270ab185921SStefan Hajnoczi int pnum; 2271ab185921SStefan Hajnoczi 2272ab185921SStefan Hajnoczi ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); 2273ab185921SStefan Hajnoczi if (ret < 0) { 2274ab185921SStefan Hajnoczi goto out; 2275ab185921SStefan Hajnoczi } 2276ab185921SStefan Hajnoczi 2277ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 2278470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 2279ab185921SStefan Hajnoczi goto out; 2280ab185921SStefan Hajnoczi } 2281ab185921SStefan Hajnoczi } 2282ab185921SStefan Hajnoczi 2283dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 2284ab185921SStefan Hajnoczi 2285ab185921SStefan Hajnoczi out: 2286dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2287470c0504SStefan Hajnoczi 2288470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2289470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 2290470c0504SStefan Hajnoczi } 2291470c0504SStefan Hajnoczi 2292dbffbdcfSStefan Hajnoczi return ret; 2293da1fa91dSKevin Wolf } 2294da1fa91dSKevin Wolf 2295c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 2296da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 2297da1fa91dSKevin Wolf { 2298c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 2299da1fa91dSKevin Wolf 2300470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 2301470c0504SStefan Hajnoczi } 2302470c0504SStefan Hajnoczi 2303470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 2304470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2305470c0504SStefan Hajnoczi { 2306470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 2307470c0504SStefan Hajnoczi 2308470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 2309470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 2310c5fbe571SStefan Hajnoczi } 2311c5fbe571SStefan Hajnoczi 2312f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 2313f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 2314f08f2ddaSStefan Hajnoczi { 2315f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 2316f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 2317f08f2ddaSStefan Hajnoczi struct iovec iov; 2318f08f2ddaSStefan Hajnoczi int ret; 2319f08f2ddaSStefan Hajnoczi 2320621f0589SKevin Wolf /* TODO Emulate only part of misaligned requests instead of letting block 2321621f0589SKevin Wolf * drivers return -ENOTSUP and emulate everything */ 2322621f0589SKevin Wolf 2323f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 2324f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 2325621f0589SKevin Wolf ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 2326621f0589SKevin Wolf if (ret != -ENOTSUP) { 2327621f0589SKevin Wolf return ret; 2328621f0589SKevin Wolf } 2329f08f2ddaSStefan Hajnoczi } 2330f08f2ddaSStefan Hajnoczi 2331f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 2332f08f2ddaSStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2333f08f2ddaSStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 2334f08f2ddaSStefan Hajnoczi memset(iov.iov_base, 0, iov.iov_len); 2335f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 2336f08f2ddaSStefan Hajnoczi 2337f08f2ddaSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 2338f08f2ddaSStefan Hajnoczi 2339f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 2340f08f2ddaSStefan Hajnoczi return ret; 2341f08f2ddaSStefan Hajnoczi } 2342f08f2ddaSStefan Hajnoczi 2343c5fbe571SStefan Hajnoczi /* 2344c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 2345c5fbe571SStefan Hajnoczi */ 2346c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 2347f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2348f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 2349c5fbe571SStefan Hajnoczi { 2350c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 2351dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 23526b7cb247SStefan Hajnoczi int ret; 2353da1fa91dSKevin Wolf 2354da1fa91dSKevin Wolf if (!bs->drv) { 2355da1fa91dSKevin Wolf return -ENOMEDIUM; 2356da1fa91dSKevin Wolf } 2357da1fa91dSKevin Wolf if (bs->read_only) { 2358da1fa91dSKevin Wolf return -EACCES; 2359da1fa91dSKevin Wolf } 2360da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2361da1fa91dSKevin Wolf return -EIO; 2362da1fa91dSKevin Wolf } 2363da1fa91dSKevin Wolf 236498f90dbaSZhi Yong Wu /* throttling disk write I/O */ 236598f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 236698f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, true, nb_sectors); 236798f90dbaSZhi Yong Wu } 236898f90dbaSZhi Yong Wu 2369470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2370f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2371f4658285SStefan Hajnoczi } 2372f4658285SStefan Hajnoczi 2373dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 2374dbffbdcfSStefan Hajnoczi 2375f08f2ddaSStefan Hajnoczi if (flags & BDRV_REQ_ZERO_WRITE) { 2376f08f2ddaSStefan Hajnoczi ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); 2377f08f2ddaSStefan Hajnoczi } else { 23786b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 2379f08f2ddaSStefan Hajnoczi } 23806b7cb247SStefan Hajnoczi 2381f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 2382f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 2383f05fa4adSPaolo Bonzini } 2384f05fa4adSPaolo Bonzini 2385da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 23861755da16SPaolo Bonzini bdrv_set_dirty(bs, sector_num, nb_sectors); 2387da1fa91dSKevin Wolf } 2388da1fa91dSKevin Wolf 2389da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2390da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2391da1fa91dSKevin Wolf } 2392da1fa91dSKevin Wolf 2393dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2394dbffbdcfSStefan Hajnoczi 23956b7cb247SStefan Hajnoczi return ret; 2396da1fa91dSKevin Wolf } 2397da1fa91dSKevin Wolf 2398c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 2399c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 2400c5fbe571SStefan Hajnoczi { 2401c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 2402c5fbe571SStefan Hajnoczi 2403f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 2404f08f2ddaSStefan Hajnoczi } 2405f08f2ddaSStefan Hajnoczi 2406f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 2407f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 2408f08f2ddaSStefan Hajnoczi { 2409f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 2410f08f2ddaSStefan Hajnoczi 2411f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 2412f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE); 2413c5fbe571SStefan Hajnoczi } 2414c5fbe571SStefan Hajnoczi 241583f64091Sbellard /** 241683f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 241783f64091Sbellard */ 241883f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 241983f64091Sbellard { 242083f64091Sbellard BlockDriver *drv = bs->drv; 242151762288SStefan Hajnoczi int ret; 242283f64091Sbellard if (!drv) 242319cb3738Sbellard return -ENOMEDIUM; 242483f64091Sbellard if (!drv->bdrv_truncate) 242583f64091Sbellard return -ENOTSUP; 242659f2689dSNaphtali Sprei if (bs->read_only) 242759f2689dSNaphtali Sprei return -EACCES; 24288591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 24298591675fSMarcelo Tosatti return -EBUSY; 243051762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 243151762288SStefan Hajnoczi if (ret == 0) { 243251762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2433145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 243451762288SStefan Hajnoczi } 243551762288SStefan Hajnoczi return ret; 243683f64091Sbellard } 243783f64091Sbellard 243883f64091Sbellard /** 24394a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 24404a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 24414a1d5e1fSFam Zheng */ 24424a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 24434a1d5e1fSFam Zheng { 24444a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 24454a1d5e1fSFam Zheng if (!drv) { 24464a1d5e1fSFam Zheng return -ENOMEDIUM; 24474a1d5e1fSFam Zheng } 24484a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 24494a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 24504a1d5e1fSFam Zheng } 24514a1d5e1fSFam Zheng if (bs->file) { 24524a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 24534a1d5e1fSFam Zheng } 24544a1d5e1fSFam Zheng return -ENOTSUP; 24554a1d5e1fSFam Zheng } 24564a1d5e1fSFam Zheng 24574a1d5e1fSFam Zheng /** 245883f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 245983f64091Sbellard */ 246083f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 246183f64091Sbellard { 246283f64091Sbellard BlockDriver *drv = bs->drv; 246383f64091Sbellard if (!drv) 246419cb3738Sbellard return -ENOMEDIUM; 246551762288SStefan Hajnoczi 24662c6942faSMarkus Armbruster if (bs->growable || bdrv_dev_has_removable_media(bs)) { 246746a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 246883f64091Sbellard return drv->bdrv_getlength(bs); 2469fc01f7e7Sbellard } 247046a4e4e6SStefan Hajnoczi } 247146a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 247246a4e4e6SStefan Hajnoczi } 2473fc01f7e7Sbellard 247419cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 247596b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2476fc01f7e7Sbellard { 247719cb3738Sbellard int64_t length; 247819cb3738Sbellard length = bdrv_getlength(bs); 247919cb3738Sbellard if (length < 0) 248019cb3738Sbellard length = 0; 248119cb3738Sbellard else 24826ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 248319cb3738Sbellard *nb_sectors_ptr = length; 2484fc01f7e7Sbellard } 2485cf98951bSbellard 24860563e191SZhi Yong Wu /* throttling disk io limits */ 24870563e191SZhi Yong Wu void bdrv_set_io_limits(BlockDriverState *bs, 24880563e191SZhi Yong Wu BlockIOLimit *io_limits) 24890563e191SZhi Yong Wu { 24900563e191SZhi Yong Wu bs->io_limits = *io_limits; 24910563e191SZhi Yong Wu bs->io_limits_enabled = bdrv_io_limits_enabled(bs); 24920563e191SZhi Yong Wu } 24930563e191SZhi Yong Wu 2494ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 2495ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 2496abd7f68dSMarkus Armbruster { 2497abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2498abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2499abd7f68dSMarkus Armbruster } 2500abd7f68dSMarkus Armbruster 25011ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 2502abd7f68dSMarkus Armbruster { 2503abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2504abd7f68dSMarkus Armbruster } 2505abd7f68dSMarkus Armbruster 25063e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 25073e1caa5fSPaolo Bonzini { 25083e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 25093e1caa5fSPaolo Bonzini 25103e1caa5fSPaolo Bonzini switch (on_err) { 25113e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 25123e1caa5fSPaolo Bonzini return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; 25133e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 25143e1caa5fSPaolo Bonzini return BDRV_ACTION_STOP; 25153e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 25163e1caa5fSPaolo Bonzini return BDRV_ACTION_REPORT; 25173e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 25183e1caa5fSPaolo Bonzini return BDRV_ACTION_IGNORE; 25193e1caa5fSPaolo Bonzini default: 25203e1caa5fSPaolo Bonzini abort(); 25213e1caa5fSPaolo Bonzini } 25223e1caa5fSPaolo Bonzini } 25233e1caa5fSPaolo Bonzini 25243e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 25253e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 25263e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 25273e1caa5fSPaolo Bonzini */ 25283e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 25293e1caa5fSPaolo Bonzini bool is_read, int error) 25303e1caa5fSPaolo Bonzini { 25313e1caa5fSPaolo Bonzini assert(error >= 0); 253232c81a4aSPaolo Bonzini bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); 25333e1caa5fSPaolo Bonzini if (action == BDRV_ACTION_STOP) { 25343e1caa5fSPaolo Bonzini vm_stop(RUN_STATE_IO_ERROR); 25353e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 25363e1caa5fSPaolo Bonzini } 25373e1caa5fSPaolo Bonzini } 25383e1caa5fSPaolo Bonzini 2539b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2540b338082bSbellard { 2541b338082bSbellard return bs->read_only; 2542b338082bSbellard } 2543b338082bSbellard 2544985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2545985a03b0Sths { 2546985a03b0Sths return bs->sg; 2547985a03b0Sths } 2548985a03b0Sths 2549e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2550e900a7b7SChristoph Hellwig { 2551e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2552e900a7b7SChristoph Hellwig } 2553e900a7b7SChristoph Hellwig 2554425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 2555425b0148SPaolo Bonzini { 2556425b0148SPaolo Bonzini bs->enable_write_cache = wce; 255755b110f2SJeff Cody 255855b110f2SJeff Cody /* so a reopen() will preserve wce */ 255955b110f2SJeff Cody if (wce) { 256055b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 256155b110f2SJeff Cody } else { 256255b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 256355b110f2SJeff Cody } 2564425b0148SPaolo Bonzini } 2565425b0148SPaolo Bonzini 2566ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2567ea2384d3Sbellard { 2568ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2569ea2384d3Sbellard return 1; 2570ea2384d3Sbellard return bs->encrypted; 2571ea2384d3Sbellard } 2572ea2384d3Sbellard 2573c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2574c0f4ce77Saliguori { 2575c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2576c0f4ce77Saliguori 2577c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2578c0f4ce77Saliguori return 1; 2579c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2580c0f4ce77Saliguori } 2581c0f4ce77Saliguori 2582ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2583ea2384d3Sbellard { 2584ea2384d3Sbellard int ret; 2585ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2586ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2587ea2384d3Sbellard if (ret < 0) 2588ea2384d3Sbellard return ret; 2589ea2384d3Sbellard if (!bs->encrypted) 2590ea2384d3Sbellard return 0; 2591ea2384d3Sbellard } 2592fd04a2aeSShahar Havivi if (!bs->encrypted) { 2593fd04a2aeSShahar Havivi return -EINVAL; 2594fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2595fd04a2aeSShahar Havivi return -ENOMEDIUM; 2596fd04a2aeSShahar Havivi } 2597c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2598bb5fc20fSaliguori if (ret < 0) { 2599bb5fc20fSaliguori bs->valid_key = 0; 2600bb5fc20fSaliguori } else if (!bs->valid_key) { 2601bb5fc20fSaliguori bs->valid_key = 1; 2602bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 26037d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 2604bb5fc20fSaliguori } 2605c0f4ce77Saliguori return ret; 2606ea2384d3Sbellard } 2607ea2384d3Sbellard 2608f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 2609ea2384d3Sbellard { 2610f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 2611ea2384d3Sbellard } 2612ea2384d3Sbellard 2613ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2614ea2384d3Sbellard void *opaque) 2615ea2384d3Sbellard { 2616ea2384d3Sbellard BlockDriver *drv; 2617ea2384d3Sbellard 26188a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2619ea2384d3Sbellard it(opaque, drv->format_name); 2620ea2384d3Sbellard } 2621ea2384d3Sbellard } 2622ea2384d3Sbellard 2623b338082bSbellard BlockDriverState *bdrv_find(const char *name) 2624b338082bSbellard { 2625b338082bSbellard BlockDriverState *bs; 2626b338082bSbellard 26271b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 26281b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 2629b338082bSbellard return bs; 2630b338082bSbellard } 26311b7bdbc1SStefan Hajnoczi } 2632b338082bSbellard return NULL; 2633b338082bSbellard } 2634b338082bSbellard 26352f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 26362f399b0aSMarkus Armbruster { 26372f399b0aSMarkus Armbruster if (!bs) { 26382f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 26392f399b0aSMarkus Armbruster } 26402f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 26412f399b0aSMarkus Armbruster } 26422f399b0aSMarkus Armbruster 264351de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 264481d0912dSbellard { 264581d0912dSbellard BlockDriverState *bs; 264681d0912dSbellard 26471b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 264851de9760Saliguori it(opaque, bs); 264981d0912dSbellard } 265081d0912dSbellard } 265181d0912dSbellard 2652ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 2653ea2384d3Sbellard { 2654ea2384d3Sbellard return bs->device_name; 2655ea2384d3Sbellard } 2656ea2384d3Sbellard 2657c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 2658c8433287SMarkus Armbruster { 2659c8433287SMarkus Armbruster return bs->open_flags; 2660c8433287SMarkus Armbruster } 2661c8433287SMarkus Armbruster 2662c6ca28d6Saliguori void bdrv_flush_all(void) 2663c6ca28d6Saliguori { 2664c6ca28d6Saliguori BlockDriverState *bs; 2665c6ca28d6Saliguori 26661b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2667c6ca28d6Saliguori bdrv_flush(bs); 2668c6ca28d6Saliguori } 26691b7bdbc1SStefan Hajnoczi } 2670c6ca28d6Saliguori 2671f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2672f2feebbdSKevin Wolf { 2673f2feebbdSKevin Wolf assert(bs->drv); 2674f2feebbdSKevin Wolf 2675336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2676336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2677f2feebbdSKevin Wolf } 2678f2feebbdSKevin Wolf 2679f2feebbdSKevin Wolf return 1; 2680f2feebbdSKevin Wolf } 2681f2feebbdSKevin Wolf 2682376ae3f1SStefan Hajnoczi typedef struct BdrvCoIsAllocatedData { 2683376ae3f1SStefan Hajnoczi BlockDriverState *bs; 2684376ae3f1SStefan Hajnoczi int64_t sector_num; 2685376ae3f1SStefan Hajnoczi int nb_sectors; 2686376ae3f1SStefan Hajnoczi int *pnum; 2687376ae3f1SStefan Hajnoczi int ret; 2688376ae3f1SStefan Hajnoczi bool done; 2689376ae3f1SStefan Hajnoczi } BdrvCoIsAllocatedData; 2690376ae3f1SStefan Hajnoczi 2691f58c7b35Sths /* 2692f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 2693f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 2694f58c7b35Sths * hence all their sectors are reported as allocated. 2695f58c7b35Sths * 2696bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 2697bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 2698bd9533e3SStefan Hajnoczi * 2699f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 2700f58c7b35Sths * the specified sector) that are known to be in the same 2701f58c7b35Sths * allocated/unallocated state. 2702f58c7b35Sths * 2703bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2704bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 2705f58c7b35Sths */ 2706060f51c9SStefan Hajnoczi int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, 2707060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 2708f58c7b35Sths { 2709f58c7b35Sths int64_t n; 2710bd9533e3SStefan Hajnoczi 27116aebab14SStefan Hajnoczi if (sector_num >= bs->total_sectors) { 27126aebab14SStefan Hajnoczi *pnum = 0; 27136aebab14SStefan Hajnoczi return 0; 27146aebab14SStefan Hajnoczi } 2715bd9533e3SStefan Hajnoczi 27166aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 2717bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 2718bd9533e3SStefan Hajnoczi nb_sectors = n; 2719bd9533e3SStefan Hajnoczi } 2720bd9533e3SStefan Hajnoczi 2721bd9533e3SStefan Hajnoczi if (!bs->drv->bdrv_co_is_allocated) { 2722bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 27236aebab14SStefan Hajnoczi return 1; 27246aebab14SStefan Hajnoczi } 27256aebab14SStefan Hajnoczi 2726060f51c9SStefan Hajnoczi return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum); 2727060f51c9SStefan Hajnoczi } 2728060f51c9SStefan Hajnoczi 2729060f51c9SStefan Hajnoczi /* Coroutine wrapper for bdrv_is_allocated() */ 2730060f51c9SStefan Hajnoczi static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) 2731060f51c9SStefan Hajnoczi { 2732060f51c9SStefan Hajnoczi BdrvCoIsAllocatedData *data = opaque; 2733060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 2734060f51c9SStefan Hajnoczi 2735060f51c9SStefan Hajnoczi data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, 2736060f51c9SStefan Hajnoczi data->pnum); 2737060f51c9SStefan Hajnoczi data->done = true; 2738060f51c9SStefan Hajnoczi } 2739060f51c9SStefan Hajnoczi 2740060f51c9SStefan Hajnoczi /* 2741060f51c9SStefan Hajnoczi * Synchronous wrapper around bdrv_co_is_allocated(). 2742060f51c9SStefan Hajnoczi * 2743060f51c9SStefan Hajnoczi * See bdrv_co_is_allocated() for details. 2744060f51c9SStefan Hajnoczi */ 2745060f51c9SStefan Hajnoczi int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 2746060f51c9SStefan Hajnoczi int *pnum) 2747060f51c9SStefan Hajnoczi { 2748376ae3f1SStefan Hajnoczi Coroutine *co; 2749376ae3f1SStefan Hajnoczi BdrvCoIsAllocatedData data = { 2750376ae3f1SStefan Hajnoczi .bs = bs, 2751376ae3f1SStefan Hajnoczi .sector_num = sector_num, 2752376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 2753376ae3f1SStefan Hajnoczi .pnum = pnum, 2754376ae3f1SStefan Hajnoczi .done = false, 2755376ae3f1SStefan Hajnoczi }; 2756376ae3f1SStefan Hajnoczi 2757376ae3f1SStefan Hajnoczi co = qemu_coroutine_create(bdrv_is_allocated_co_entry); 2758376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 2759376ae3f1SStefan Hajnoczi while (!data.done) { 2760376ae3f1SStefan Hajnoczi qemu_aio_wait(); 2761376ae3f1SStefan Hajnoczi } 2762376ae3f1SStefan Hajnoczi return data.ret; 2763376ae3f1SStefan Hajnoczi } 2764f58c7b35Sths 2765188a7bbfSPaolo Bonzini /* 2766188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 2767188a7bbfSPaolo Bonzini * 2768188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 2769188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 2770188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 2771188a7bbfSPaolo Bonzini * 2772188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 2773188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 2774188a7bbfSPaolo Bonzini * allocated/unallocated state. 2775188a7bbfSPaolo Bonzini * 2776188a7bbfSPaolo Bonzini */ 2777188a7bbfSPaolo Bonzini int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top, 2778188a7bbfSPaolo Bonzini BlockDriverState *base, 2779188a7bbfSPaolo Bonzini int64_t sector_num, 2780188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 2781188a7bbfSPaolo Bonzini { 2782188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 2783188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 2784188a7bbfSPaolo Bonzini 2785188a7bbfSPaolo Bonzini intermediate = top; 2786188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 2787188a7bbfSPaolo Bonzini int pnum_inter; 2788188a7bbfSPaolo Bonzini ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors, 2789188a7bbfSPaolo Bonzini &pnum_inter); 2790188a7bbfSPaolo Bonzini if (ret < 0) { 2791188a7bbfSPaolo Bonzini return ret; 2792188a7bbfSPaolo Bonzini } else if (ret) { 2793188a7bbfSPaolo Bonzini *pnum = pnum_inter; 2794188a7bbfSPaolo Bonzini return 1; 2795188a7bbfSPaolo Bonzini } 2796188a7bbfSPaolo Bonzini 2797188a7bbfSPaolo Bonzini /* 2798188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 2799188a7bbfSPaolo Bonzini * might have 2800188a7bbfSPaolo Bonzini * 2801188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 2802188a7bbfSPaolo Bonzini */ 2803188a7bbfSPaolo Bonzini if (n > pnum_inter) { 2804188a7bbfSPaolo Bonzini n = pnum_inter; 2805188a7bbfSPaolo Bonzini } 2806188a7bbfSPaolo Bonzini 2807188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 2808188a7bbfSPaolo Bonzini } 2809188a7bbfSPaolo Bonzini 2810188a7bbfSPaolo Bonzini *pnum = n; 2811188a7bbfSPaolo Bonzini return 0; 2812188a7bbfSPaolo Bonzini } 2813188a7bbfSPaolo Bonzini 2814ac84adacSPaolo Bonzini BlockInfo *bdrv_query_info(BlockDriverState *bs) 2815ac84adacSPaolo Bonzini { 2816ac84adacSPaolo Bonzini BlockInfo *info = g_malloc0(sizeof(*info)); 2817ac84adacSPaolo Bonzini info->device = g_strdup(bs->device_name); 2818ac84adacSPaolo Bonzini info->type = g_strdup("unknown"); 2819ac84adacSPaolo Bonzini info->locked = bdrv_dev_is_medium_locked(bs); 2820ac84adacSPaolo Bonzini info->removable = bdrv_dev_has_removable_media(bs); 2821ac84adacSPaolo Bonzini 2822ac84adacSPaolo Bonzini if (bdrv_dev_has_removable_media(bs)) { 2823ac84adacSPaolo Bonzini info->has_tray_open = true; 2824ac84adacSPaolo Bonzini info->tray_open = bdrv_dev_is_tray_open(bs); 2825ac84adacSPaolo Bonzini } 2826ac84adacSPaolo Bonzini 2827ac84adacSPaolo Bonzini if (bdrv_iostatus_is_enabled(bs)) { 2828ac84adacSPaolo Bonzini info->has_io_status = true; 2829ac84adacSPaolo Bonzini info->io_status = bs->iostatus; 2830ac84adacSPaolo Bonzini } 2831ac84adacSPaolo Bonzini 2832b9a9b3a4SPaolo Bonzini if (bs->dirty_bitmap) { 2833b9a9b3a4SPaolo Bonzini info->has_dirty = true; 2834b9a9b3a4SPaolo Bonzini info->dirty = g_malloc0(sizeof(*info->dirty)); 2835acc906c6SPaolo Bonzini info->dirty->count = bdrv_get_dirty_count(bs) * BDRV_SECTOR_SIZE; 2836*50717e94SPaolo Bonzini info->dirty->granularity = 2837*50717e94SPaolo Bonzini ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bs->dirty_bitmap)); 2838b9a9b3a4SPaolo Bonzini } 2839b9a9b3a4SPaolo Bonzini 2840ac84adacSPaolo Bonzini if (bs->drv) { 2841ac84adacSPaolo Bonzini info->has_inserted = true; 2842ac84adacSPaolo Bonzini info->inserted = g_malloc0(sizeof(*info->inserted)); 2843ac84adacSPaolo Bonzini info->inserted->file = g_strdup(bs->filename); 2844ac84adacSPaolo Bonzini info->inserted->ro = bs->read_only; 2845ac84adacSPaolo Bonzini info->inserted->drv = g_strdup(bs->drv->format_name); 2846ac84adacSPaolo Bonzini info->inserted->encrypted = bs->encrypted; 2847ac84adacSPaolo Bonzini info->inserted->encryption_key_missing = bdrv_key_required(bs); 2848ac84adacSPaolo Bonzini 2849ac84adacSPaolo Bonzini if (bs->backing_file[0]) { 2850ac84adacSPaolo Bonzini info->inserted->has_backing_file = true; 2851ac84adacSPaolo Bonzini info->inserted->backing_file = g_strdup(bs->backing_file); 2852ac84adacSPaolo Bonzini } 2853ac84adacSPaolo Bonzini 2854ac84adacSPaolo Bonzini info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs); 2855ac84adacSPaolo Bonzini 2856ac84adacSPaolo Bonzini if (bs->io_limits_enabled) { 2857ac84adacSPaolo Bonzini info->inserted->bps = 2858ac84adacSPaolo Bonzini bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 2859ac84adacSPaolo Bonzini info->inserted->bps_rd = 2860ac84adacSPaolo Bonzini bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; 2861ac84adacSPaolo Bonzini info->inserted->bps_wr = 2862ac84adacSPaolo Bonzini bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; 2863ac84adacSPaolo Bonzini info->inserted->iops = 2864ac84adacSPaolo Bonzini bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 2865ac84adacSPaolo Bonzini info->inserted->iops_rd = 2866ac84adacSPaolo Bonzini bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; 2867ac84adacSPaolo Bonzini info->inserted->iops_wr = 2868ac84adacSPaolo Bonzini bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; 2869ac84adacSPaolo Bonzini } 2870ac84adacSPaolo Bonzini } 2871ac84adacSPaolo Bonzini return info; 2872ac84adacSPaolo Bonzini } 2873ac84adacSPaolo Bonzini 2874b2023818SLuiz Capitulino BlockInfoList *qmp_query_block(Error **errp) 2875b338082bSbellard { 2876ac84adacSPaolo Bonzini BlockInfoList *head = NULL, **p_next = &head; 2877d15e5465SLuiz Capitulino BlockDriverState *bs; 2878d15e5465SLuiz Capitulino 28791b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2880b2023818SLuiz Capitulino BlockInfoList *info = g_malloc0(sizeof(*info)); 2881ac84adacSPaolo Bonzini info->value = bdrv_query_info(bs); 2882d15e5465SLuiz Capitulino 2883ac84adacSPaolo Bonzini *p_next = info; 2884ac84adacSPaolo Bonzini p_next = &info->next; 2885d15e5465SLuiz Capitulino } 2886d15e5465SLuiz Capitulino 2887b2023818SLuiz Capitulino return head; 2888b338082bSbellard } 2889a36e69ddSths 28909887b616SPaolo Bonzini BlockStats *bdrv_query_stats(const BlockDriverState *bs) 2891a36e69ddSths { 2892f11f57e4SLuiz Capitulino BlockStats *s; 2893218a536aSLuiz Capitulino 2894f11f57e4SLuiz Capitulino s = g_malloc0(sizeof(*s)); 2895218a536aSLuiz Capitulino 2896f11f57e4SLuiz Capitulino if (bs->device_name[0]) { 2897f11f57e4SLuiz Capitulino s->has_device = true; 2898f11f57e4SLuiz Capitulino s->device = g_strdup(bs->device_name); 2899218a536aSLuiz Capitulino } 2900218a536aSLuiz Capitulino 2901f11f57e4SLuiz Capitulino s->stats = g_malloc0(sizeof(*s->stats)); 2902f11f57e4SLuiz Capitulino s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; 2903f11f57e4SLuiz Capitulino s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; 2904f11f57e4SLuiz Capitulino s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; 2905f11f57e4SLuiz Capitulino s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; 2906f11f57e4SLuiz Capitulino s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; 2907f11f57e4SLuiz Capitulino s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; 2908f11f57e4SLuiz Capitulino s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; 2909f11f57e4SLuiz Capitulino s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; 2910f11f57e4SLuiz Capitulino s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; 2911294cc35fSKevin Wolf 2912294cc35fSKevin Wolf if (bs->file) { 2913f11f57e4SLuiz Capitulino s->has_parent = true; 29149887b616SPaolo Bonzini s->parent = bdrv_query_stats(bs->file); 2915294cc35fSKevin Wolf } 2916294cc35fSKevin Wolf 2917f11f57e4SLuiz Capitulino return s; 2918294cc35fSKevin Wolf } 2919294cc35fSKevin Wolf 2920f11f57e4SLuiz Capitulino BlockStatsList *qmp_query_blockstats(Error **errp) 2921218a536aSLuiz Capitulino { 29229887b616SPaolo Bonzini BlockStatsList *head = NULL, **p_next = &head; 2923a36e69ddSths BlockDriverState *bs; 2924a36e69ddSths 29251b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2926f11f57e4SLuiz Capitulino BlockStatsList *info = g_malloc0(sizeof(*info)); 29279887b616SPaolo Bonzini info->value = bdrv_query_stats(bs); 2928f11f57e4SLuiz Capitulino 29299887b616SPaolo Bonzini *p_next = info; 29309887b616SPaolo Bonzini p_next = &info->next; 2931a36e69ddSths } 2932218a536aSLuiz Capitulino 2933f11f57e4SLuiz Capitulino return head; 2934a36e69ddSths } 2935ea2384d3Sbellard 2936045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 2937045df330Saliguori { 2938045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 2939045df330Saliguori return bs->backing_file; 2940045df330Saliguori else if (bs->encrypted) 2941045df330Saliguori return bs->filename; 2942045df330Saliguori else 2943045df330Saliguori return NULL; 2944045df330Saliguori } 2945045df330Saliguori 294683f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 294783f64091Sbellard char *filename, int filename_size) 294883f64091Sbellard { 294983f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 295083f64091Sbellard } 295183f64091Sbellard 2952faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 2953faea38e7Sbellard const uint8_t *buf, int nb_sectors) 2954faea38e7Sbellard { 2955faea38e7Sbellard BlockDriver *drv = bs->drv; 2956faea38e7Sbellard if (!drv) 295719cb3738Sbellard return -ENOMEDIUM; 2958faea38e7Sbellard if (!drv->bdrv_write_compressed) 2959faea38e7Sbellard return -ENOTSUP; 2960fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 2961fbb7b4e0SKevin Wolf return -EIO; 29627cd1e32aSlirans@il.ibm.com 29631755da16SPaolo Bonzini assert(!bs->dirty_bitmap); 29647cd1e32aSlirans@il.ibm.com 2965faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 2966faea38e7Sbellard } 2967faea38e7Sbellard 2968faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2969faea38e7Sbellard { 2970faea38e7Sbellard BlockDriver *drv = bs->drv; 2971faea38e7Sbellard if (!drv) 297219cb3738Sbellard return -ENOMEDIUM; 2973faea38e7Sbellard if (!drv->bdrv_get_info) 2974faea38e7Sbellard return -ENOTSUP; 2975faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 2976faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 2977faea38e7Sbellard } 2978faea38e7Sbellard 297945566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 298045566e9cSChristoph Hellwig int64_t pos, int size) 2981178e08a5Saliguori { 2982178e08a5Saliguori BlockDriver *drv = bs->drv; 2983178e08a5Saliguori if (!drv) 2984178e08a5Saliguori return -ENOMEDIUM; 29857cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 298645566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 29877cdb1f6dSMORITA Kazutaka if (bs->file) 29887cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 29897cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2990178e08a5Saliguori } 2991178e08a5Saliguori 299245566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 299345566e9cSChristoph Hellwig int64_t pos, int size) 2994178e08a5Saliguori { 2995178e08a5Saliguori BlockDriver *drv = bs->drv; 2996178e08a5Saliguori if (!drv) 2997178e08a5Saliguori return -ENOMEDIUM; 29987cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 299945566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 30007cdb1f6dSMORITA Kazutaka if (bs->file) 30017cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 30027cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3003178e08a5Saliguori } 3004178e08a5Saliguori 30058b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 30068b9b0cc2SKevin Wolf { 30078b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 30088b9b0cc2SKevin Wolf 30098b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 30108b9b0cc2SKevin Wolf return; 30118b9b0cc2SKevin Wolf } 30128b9b0cc2SKevin Wolf 30130ed8b6f6SBlue Swirl drv->bdrv_debug_event(bs, event); 301441c695c7SKevin Wolf } 30158b9b0cc2SKevin Wolf 301641c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 301741c695c7SKevin Wolf const char *tag) 301841c695c7SKevin Wolf { 301941c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 302041c695c7SKevin Wolf bs = bs->file; 302141c695c7SKevin Wolf } 302241c695c7SKevin Wolf 302341c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 302441c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 302541c695c7SKevin Wolf } 302641c695c7SKevin Wolf 302741c695c7SKevin Wolf return -ENOTSUP; 302841c695c7SKevin Wolf } 302941c695c7SKevin Wolf 303041c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 303141c695c7SKevin Wolf { 303241c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_resume) { 303341c695c7SKevin Wolf bs = bs->file; 303441c695c7SKevin Wolf } 303541c695c7SKevin Wolf 303641c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 303741c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 303841c695c7SKevin Wolf } 303941c695c7SKevin Wolf 304041c695c7SKevin Wolf return -ENOTSUP; 304141c695c7SKevin Wolf } 304241c695c7SKevin Wolf 304341c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 304441c695c7SKevin Wolf { 304541c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 304641c695c7SKevin Wolf bs = bs->file; 304741c695c7SKevin Wolf } 304841c695c7SKevin Wolf 304941c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 305041c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 305141c695c7SKevin Wolf } 305241c695c7SKevin Wolf 305341c695c7SKevin Wolf return false; 30548b9b0cc2SKevin Wolf } 30558b9b0cc2SKevin Wolf 3056faea38e7Sbellard /**************************************************************/ 3057faea38e7Sbellard /* handling of snapshots */ 3058faea38e7Sbellard 3059feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 3060feeee5acSMiguel Di Ciurcio Filho { 3061feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 306207b70bfbSMarkus Armbruster if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 3063feeee5acSMiguel Di Ciurcio Filho return 0; 3064feeee5acSMiguel Di Ciurcio Filho } 3065feeee5acSMiguel Di Ciurcio Filho 3066feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 3067feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 3068feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 3069feeee5acSMiguel Di Ciurcio Filho } 3070feeee5acSMiguel Di Ciurcio Filho return 0; 3071feeee5acSMiguel Di Ciurcio Filho } 3072feeee5acSMiguel Di Ciurcio Filho 3073feeee5acSMiguel Di Ciurcio Filho return 1; 3074feeee5acSMiguel Di Ciurcio Filho } 3075feeee5acSMiguel Di Ciurcio Filho 3076199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 3077199630b6SBlue Swirl { 3078199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 3079199630b6SBlue Swirl } 3080199630b6SBlue Swirl 3081f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 3082f9092b10SMarkus Armbruster { 3083f9092b10SMarkus Armbruster BlockDriverState *bs; 3084f9092b10SMarkus Armbruster 30853ac906f7SMarkus Armbruster if (bs_snapshots) { 3086f9092b10SMarkus Armbruster return bs_snapshots; 30873ac906f7SMarkus Armbruster } 3088f9092b10SMarkus Armbruster 3089f9092b10SMarkus Armbruster bs = NULL; 3090f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 3091f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 30923ac906f7SMarkus Armbruster bs_snapshots = bs; 30933ac906f7SMarkus Armbruster return bs; 3094f9092b10SMarkus Armbruster } 3095f9092b10SMarkus Armbruster } 3096f9092b10SMarkus Armbruster return NULL; 3097f9092b10SMarkus Armbruster } 3098f9092b10SMarkus Armbruster 3099faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 3100faea38e7Sbellard QEMUSnapshotInfo *sn_info) 3101faea38e7Sbellard { 3102faea38e7Sbellard BlockDriver *drv = bs->drv; 3103faea38e7Sbellard if (!drv) 310419cb3738Sbellard return -ENOMEDIUM; 31057cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 3106faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 31077cdb1f6dSMORITA Kazutaka if (bs->file) 31087cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 31097cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3110faea38e7Sbellard } 3111faea38e7Sbellard 3112faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 3113faea38e7Sbellard const char *snapshot_id) 3114faea38e7Sbellard { 3115faea38e7Sbellard BlockDriver *drv = bs->drv; 31167cdb1f6dSMORITA Kazutaka int ret, open_ret; 31177cdb1f6dSMORITA Kazutaka 3118faea38e7Sbellard if (!drv) 311919cb3738Sbellard return -ENOMEDIUM; 31207cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 3121faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 31227cdb1f6dSMORITA Kazutaka 31237cdb1f6dSMORITA Kazutaka if (bs->file) { 31247cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 31257cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 31267cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 31277cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 31287cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 31297cdb1f6dSMORITA Kazutaka bs->drv = NULL; 31307cdb1f6dSMORITA Kazutaka return open_ret; 31317cdb1f6dSMORITA Kazutaka } 31327cdb1f6dSMORITA Kazutaka return ret; 31337cdb1f6dSMORITA Kazutaka } 31347cdb1f6dSMORITA Kazutaka 31357cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3136faea38e7Sbellard } 3137faea38e7Sbellard 3138faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 3139faea38e7Sbellard { 3140faea38e7Sbellard BlockDriver *drv = bs->drv; 3141faea38e7Sbellard if (!drv) 314219cb3738Sbellard return -ENOMEDIUM; 31437cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 3144faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 31457cdb1f6dSMORITA Kazutaka if (bs->file) 31467cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 31477cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3148faea38e7Sbellard } 3149faea38e7Sbellard 3150faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 3151faea38e7Sbellard QEMUSnapshotInfo **psn_info) 3152faea38e7Sbellard { 3153faea38e7Sbellard BlockDriver *drv = bs->drv; 3154faea38e7Sbellard if (!drv) 315519cb3738Sbellard return -ENOMEDIUM; 31567cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 3157faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 31587cdb1f6dSMORITA Kazutaka if (bs->file) 31597cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 31607cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3161faea38e7Sbellard } 3162faea38e7Sbellard 316351ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 316451ef6727Sedison const char *snapshot_name) 316551ef6727Sedison { 316651ef6727Sedison BlockDriver *drv = bs->drv; 316751ef6727Sedison if (!drv) { 316851ef6727Sedison return -ENOMEDIUM; 316951ef6727Sedison } 317051ef6727Sedison if (!bs->read_only) { 317151ef6727Sedison return -EINVAL; 317251ef6727Sedison } 317351ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 317451ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 317551ef6727Sedison } 317651ef6727Sedison return -ENOTSUP; 317751ef6727Sedison } 317851ef6727Sedison 3179b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 3180b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 3181b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 3182b1b1d783SJeff Cody * the CWD rather than the chain. */ 3183e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 3184e8a6bb9cSMarcelo Tosatti const char *backing_file) 3185e8a6bb9cSMarcelo Tosatti { 3186b1b1d783SJeff Cody char *filename_full = NULL; 3187b1b1d783SJeff Cody char *backing_file_full = NULL; 3188b1b1d783SJeff Cody char *filename_tmp = NULL; 3189b1b1d783SJeff Cody int is_protocol = 0; 3190b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 3191b1b1d783SJeff Cody BlockDriverState *retval = NULL; 3192b1b1d783SJeff Cody 3193b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 3194e8a6bb9cSMarcelo Tosatti return NULL; 3195e8a6bb9cSMarcelo Tosatti } 3196e8a6bb9cSMarcelo Tosatti 3197b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 3198b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 3199b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 3200b1b1d783SJeff Cody 3201b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 3202b1b1d783SJeff Cody 3203b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 3204b1b1d783SJeff Cody 3205b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 3206b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 3207b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 3208b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 3209b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3210b1b1d783SJeff Cody break; 3211b1b1d783SJeff Cody } 3212e8a6bb9cSMarcelo Tosatti } else { 3213b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 3214b1b1d783SJeff Cody * image's filename path */ 3215b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3216b1b1d783SJeff Cody backing_file); 3217b1b1d783SJeff Cody 3218b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 3219b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 3220b1b1d783SJeff Cody continue; 3221b1b1d783SJeff Cody } 3222b1b1d783SJeff Cody 3223b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 3224b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 3225b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3226b1b1d783SJeff Cody curr_bs->backing_file); 3227b1b1d783SJeff Cody 3228b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 3229b1b1d783SJeff Cody continue; 3230b1b1d783SJeff Cody } 3231b1b1d783SJeff Cody 3232b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 3233b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3234b1b1d783SJeff Cody break; 3235b1b1d783SJeff Cody } 3236e8a6bb9cSMarcelo Tosatti } 3237e8a6bb9cSMarcelo Tosatti } 3238e8a6bb9cSMarcelo Tosatti 3239b1b1d783SJeff Cody g_free(filename_full); 3240b1b1d783SJeff Cody g_free(backing_file_full); 3241b1b1d783SJeff Cody g_free(filename_tmp); 3242b1b1d783SJeff Cody return retval; 3243e8a6bb9cSMarcelo Tosatti } 3244e8a6bb9cSMarcelo Tosatti 3245f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 3246f198fd1cSBenoît Canet { 3247f198fd1cSBenoît Canet if (!bs->drv) { 3248f198fd1cSBenoît Canet return 0; 3249f198fd1cSBenoît Canet } 3250f198fd1cSBenoît Canet 3251f198fd1cSBenoît Canet if (!bs->backing_hd) { 3252f198fd1cSBenoît Canet return 0; 3253f198fd1cSBenoît Canet } 3254f198fd1cSBenoît Canet 3255f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 3256f198fd1cSBenoît Canet } 3257f198fd1cSBenoît Canet 325879fac568SJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 325979fac568SJeff Cody { 326079fac568SJeff Cody BlockDriverState *curr_bs = NULL; 326179fac568SJeff Cody 326279fac568SJeff Cody if (!bs) { 326379fac568SJeff Cody return NULL; 326479fac568SJeff Cody } 326579fac568SJeff Cody 326679fac568SJeff Cody curr_bs = bs; 326779fac568SJeff Cody 326879fac568SJeff Cody while (curr_bs->backing_hd) { 326979fac568SJeff Cody curr_bs = curr_bs->backing_hd; 327079fac568SJeff Cody } 327179fac568SJeff Cody return curr_bs; 327279fac568SJeff Cody } 327379fac568SJeff Cody 3274faea38e7Sbellard #define NB_SUFFIXES 4 3275faea38e7Sbellard 3276faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 3277faea38e7Sbellard { 3278faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 3279faea38e7Sbellard int64_t base; 3280faea38e7Sbellard int i; 3281faea38e7Sbellard 3282faea38e7Sbellard if (size <= 999) { 3283faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 3284faea38e7Sbellard } else { 3285faea38e7Sbellard base = 1024; 3286faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 3287faea38e7Sbellard if (size < (10 * base)) { 3288faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 3289faea38e7Sbellard (double)size / base, 3290faea38e7Sbellard suffixes[i]); 3291faea38e7Sbellard break; 3292faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 3293faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 3294faea38e7Sbellard ((size + (base >> 1)) / base), 3295faea38e7Sbellard suffixes[i]); 3296faea38e7Sbellard break; 3297faea38e7Sbellard } 3298faea38e7Sbellard base = base * 1024; 3299faea38e7Sbellard } 3300faea38e7Sbellard } 3301faea38e7Sbellard return buf; 3302faea38e7Sbellard } 3303faea38e7Sbellard 3304faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 3305faea38e7Sbellard { 3306faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 3307faea38e7Sbellard struct tm tm; 3308faea38e7Sbellard time_t ti; 3309faea38e7Sbellard int64_t secs; 3310faea38e7Sbellard 3311faea38e7Sbellard if (!sn) { 3312faea38e7Sbellard snprintf(buf, buf_size, 3313faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 3314faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 3315faea38e7Sbellard } else { 3316faea38e7Sbellard ti = sn->date_sec; 3317faea38e7Sbellard localtime_r(&ti, &tm); 3318faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 3319faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 3320faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 3321faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 3322faea38e7Sbellard "%02d:%02d:%02d.%03d", 3323faea38e7Sbellard (int)(secs / 3600), 3324faea38e7Sbellard (int)((secs / 60) % 60), 3325faea38e7Sbellard (int)(secs % 60), 3326faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 3327faea38e7Sbellard snprintf(buf, buf_size, 3328faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 3329faea38e7Sbellard sn->id_str, sn->name, 3330faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 3331faea38e7Sbellard date_buf, 3332faea38e7Sbellard clock_buf); 3333faea38e7Sbellard } 3334faea38e7Sbellard return buf; 3335faea38e7Sbellard } 3336faea38e7Sbellard 3337ea2384d3Sbellard /**************************************************************/ 333883f64091Sbellard /* async I/Os */ 3339ea2384d3Sbellard 33403b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 3341f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 334283f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 3343ea2384d3Sbellard { 3344bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 3345bbf0a440SStefan Hajnoczi 3346b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 33478c5873d6SStefan Hajnoczi cb, opaque, false); 334883f64091Sbellard } 334983f64091Sbellard 3350f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 3351f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 335283f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 33537674e7bfSbellard { 3354bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 3355bbf0a440SStefan Hajnoczi 33561a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 33578c5873d6SStefan Hajnoczi cb, opaque, true); 335883f64091Sbellard } 335983f64091Sbellard 336040b4f539SKevin Wolf 336140b4f539SKevin Wolf typedef struct MultiwriteCB { 336240b4f539SKevin Wolf int error; 336340b4f539SKevin Wolf int num_requests; 336440b4f539SKevin Wolf int num_callbacks; 336540b4f539SKevin Wolf struct { 336640b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 336740b4f539SKevin Wolf void *opaque; 336840b4f539SKevin Wolf QEMUIOVector *free_qiov; 336940b4f539SKevin Wolf } callbacks[]; 337040b4f539SKevin Wolf } MultiwriteCB; 337140b4f539SKevin Wolf 337240b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 337340b4f539SKevin Wolf { 337440b4f539SKevin Wolf int i; 337540b4f539SKevin Wolf 337640b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 337740b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 33781e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 33791e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 33801e1ea48dSStefan Hajnoczi } 33817267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 338240b4f539SKevin Wolf } 338340b4f539SKevin Wolf } 338440b4f539SKevin Wolf 338540b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 338640b4f539SKevin Wolf { 338740b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 338840b4f539SKevin Wolf 33896d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 33906d519a5fSStefan Hajnoczi 3391cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 339240b4f539SKevin Wolf mcb->error = ret; 339340b4f539SKevin Wolf } 339440b4f539SKevin Wolf 339540b4f539SKevin Wolf mcb->num_requests--; 339640b4f539SKevin Wolf if (mcb->num_requests == 0) { 339740b4f539SKevin Wolf multiwrite_user_cb(mcb); 33987267c094SAnthony Liguori g_free(mcb); 339940b4f539SKevin Wolf } 340040b4f539SKevin Wolf } 340140b4f539SKevin Wolf 340240b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 340340b4f539SKevin Wolf { 340477be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 340577be4366SChristoph Hellwig 340677be4366SChristoph Hellwig /* 340777be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 340877be4366SChristoph Hellwig * here as that could overflow the return value. 340977be4366SChristoph Hellwig */ 341077be4366SChristoph Hellwig if (req1->sector > req2->sector) { 341177be4366SChristoph Hellwig return 1; 341277be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 341377be4366SChristoph Hellwig return -1; 341477be4366SChristoph Hellwig } else { 341577be4366SChristoph Hellwig return 0; 341677be4366SChristoph Hellwig } 341740b4f539SKevin Wolf } 341840b4f539SKevin Wolf 341940b4f539SKevin Wolf /* 342040b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 342140b4f539SKevin Wolf * requests that remain after merging. 342240b4f539SKevin Wolf */ 342340b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 342440b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 342540b4f539SKevin Wolf { 342640b4f539SKevin Wolf int i, outidx; 342740b4f539SKevin Wolf 342840b4f539SKevin Wolf // Sort requests by start sector 342940b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 343040b4f539SKevin Wolf 343140b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 343240b4f539SKevin Wolf // filling up gaps with zero sectors. 343340b4f539SKevin Wolf outidx = 0; 343440b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 343540b4f539SKevin Wolf int merge = 0; 343640b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 343740b4f539SKevin Wolf 3438b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 343940b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 344040b4f539SKevin Wolf merge = 1; 344140b4f539SKevin Wolf } 344240b4f539SKevin Wolf 3443e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3444e2a305fbSChristoph Hellwig merge = 0; 3445e2a305fbSChristoph Hellwig } 3446e2a305fbSChristoph Hellwig 344740b4f539SKevin Wolf if (merge) { 344840b4f539SKevin Wolf size_t size; 34497267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 345040b4f539SKevin Wolf qemu_iovec_init(qiov, 345140b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 345240b4f539SKevin Wolf 345340b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 345440b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 345540b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 34561b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 345740b4f539SKevin Wolf 3458b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3459b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 346040b4f539SKevin Wolf 346140b4f539SKevin Wolf // Add the second request 34621b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 346340b4f539SKevin Wolf 3464cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 346540b4f539SKevin Wolf reqs[outidx].qiov = qiov; 346640b4f539SKevin Wolf 346740b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 346840b4f539SKevin Wolf } else { 346940b4f539SKevin Wolf outidx++; 347040b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 347140b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 347240b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 347340b4f539SKevin Wolf } 347440b4f539SKevin Wolf } 347540b4f539SKevin Wolf 347640b4f539SKevin Wolf return outidx + 1; 347740b4f539SKevin Wolf } 347840b4f539SKevin Wolf 347940b4f539SKevin Wolf /* 348040b4f539SKevin Wolf * Submit multiple AIO write requests at once. 348140b4f539SKevin Wolf * 348240b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 348340b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 348440b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 348540b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 348640b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 348740b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 348840b4f539SKevin Wolf * 348940b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 349040b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 349140b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 349240b4f539SKevin Wolf */ 349340b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 349440b4f539SKevin Wolf { 349540b4f539SKevin Wolf MultiwriteCB *mcb; 349640b4f539SKevin Wolf int i; 349740b4f539SKevin Wolf 3498301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3499301db7c2SRyan Harper if (bs->drv == NULL) { 3500301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3501301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3502301db7c2SRyan Harper } 3503301db7c2SRyan Harper return -1; 3504301db7c2SRyan Harper } 3505301db7c2SRyan Harper 350640b4f539SKevin Wolf if (num_reqs == 0) { 350740b4f539SKevin Wolf return 0; 350840b4f539SKevin Wolf } 350940b4f539SKevin Wolf 351040b4f539SKevin Wolf // Create MultiwriteCB structure 35117267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 351240b4f539SKevin Wolf mcb->num_requests = 0; 351340b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 351440b4f539SKevin Wolf 351540b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 351640b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 351740b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 351840b4f539SKevin Wolf } 351940b4f539SKevin Wolf 352040b4f539SKevin Wolf // Check for mergable requests 352140b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 352240b4f539SKevin Wolf 35236d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 35246d519a5fSStefan Hajnoczi 3525df9309fbSPaolo Bonzini /* Run the aio requests. */ 3526df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 352740b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3528ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 352940b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 353040b4f539SKevin Wolf } 353140b4f539SKevin Wolf 353240b4f539SKevin Wolf return 0; 353340b4f539SKevin Wolf } 353440b4f539SKevin Wolf 353583f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 353683f64091Sbellard { 3537d7331bedSStefan Hajnoczi acb->aiocb_info->cancel(acb); 353883f64091Sbellard } 353983f64091Sbellard 354098f90dbaSZhi Yong Wu /* block I/O throttling */ 354198f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 354298f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait) 354398f90dbaSZhi Yong Wu { 354498f90dbaSZhi Yong Wu uint64_t bps_limit = 0; 354598f90dbaSZhi Yong Wu double bytes_limit, bytes_base, bytes_res; 354698f90dbaSZhi Yong Wu double slice_time, wait_time; 354798f90dbaSZhi Yong Wu 354898f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 354998f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 355098f90dbaSZhi Yong Wu } else if (bs->io_limits.bps[is_write]) { 355198f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[is_write]; 355298f90dbaSZhi Yong Wu } else { 355398f90dbaSZhi Yong Wu if (wait) { 355498f90dbaSZhi Yong Wu *wait = 0; 355598f90dbaSZhi Yong Wu } 355698f90dbaSZhi Yong Wu 355798f90dbaSZhi Yong Wu return false; 355898f90dbaSZhi Yong Wu } 355998f90dbaSZhi Yong Wu 356098f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 356198f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 356298f90dbaSZhi Yong Wu bytes_limit = bps_limit * slice_time; 356398f90dbaSZhi Yong Wu bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; 356498f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 356598f90dbaSZhi Yong Wu bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; 356698f90dbaSZhi Yong Wu } 356798f90dbaSZhi Yong Wu 356898f90dbaSZhi Yong Wu /* bytes_base: the bytes of data which have been read/written; and 356998f90dbaSZhi Yong Wu * it is obtained from the history statistic info. 357098f90dbaSZhi Yong Wu * bytes_res: the remaining bytes of data which need to be read/written. 357198f90dbaSZhi Yong Wu * (bytes_base + bytes_res) / bps_limit: used to calcuate 357298f90dbaSZhi Yong Wu * the total time for completing reading/writting all data. 357398f90dbaSZhi Yong Wu */ 357498f90dbaSZhi Yong Wu bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 357598f90dbaSZhi Yong Wu 357698f90dbaSZhi Yong Wu if (bytes_base + bytes_res <= bytes_limit) { 357798f90dbaSZhi Yong Wu if (wait) { 357898f90dbaSZhi Yong Wu *wait = 0; 357998f90dbaSZhi Yong Wu } 358098f90dbaSZhi Yong Wu 358198f90dbaSZhi Yong Wu return false; 358298f90dbaSZhi Yong Wu } 358398f90dbaSZhi Yong Wu 358498f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 358598f90dbaSZhi Yong Wu wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; 358698f90dbaSZhi Yong Wu 358798f90dbaSZhi Yong Wu /* When the I/O rate at runtime exceeds the limits, 358898f90dbaSZhi Yong Wu * bs->slice_end need to be extended in order that the current statistic 358998f90dbaSZhi Yong Wu * info can be kept until the timer fire, so it is increased and tuned 359098f90dbaSZhi Yong Wu * based on the result of experiment. 359198f90dbaSZhi Yong Wu */ 359298f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 359398f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 359498f90dbaSZhi Yong Wu if (wait) { 359598f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 359698f90dbaSZhi Yong Wu } 359798f90dbaSZhi Yong Wu 359898f90dbaSZhi Yong Wu return true; 359998f90dbaSZhi Yong Wu } 360098f90dbaSZhi Yong Wu 360198f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 360298f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait) 360398f90dbaSZhi Yong Wu { 360498f90dbaSZhi Yong Wu uint64_t iops_limit = 0; 360598f90dbaSZhi Yong Wu double ios_limit, ios_base; 360698f90dbaSZhi Yong Wu double slice_time, wait_time; 360798f90dbaSZhi Yong Wu 360898f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 360998f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 361098f90dbaSZhi Yong Wu } else if (bs->io_limits.iops[is_write]) { 361198f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[is_write]; 361298f90dbaSZhi Yong Wu } else { 361398f90dbaSZhi Yong Wu if (wait) { 361498f90dbaSZhi Yong Wu *wait = 0; 361598f90dbaSZhi Yong Wu } 361698f90dbaSZhi Yong Wu 361798f90dbaSZhi Yong Wu return false; 361898f90dbaSZhi Yong Wu } 361998f90dbaSZhi Yong Wu 362098f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 362198f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 362298f90dbaSZhi Yong Wu ios_limit = iops_limit * slice_time; 362398f90dbaSZhi Yong Wu ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; 362498f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 362598f90dbaSZhi Yong Wu ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; 362698f90dbaSZhi Yong Wu } 362798f90dbaSZhi Yong Wu 362898f90dbaSZhi Yong Wu if (ios_base + 1 <= ios_limit) { 362998f90dbaSZhi Yong Wu if (wait) { 363098f90dbaSZhi Yong Wu *wait = 0; 363198f90dbaSZhi Yong Wu } 363298f90dbaSZhi Yong Wu 363398f90dbaSZhi Yong Wu return false; 363498f90dbaSZhi Yong Wu } 363598f90dbaSZhi Yong Wu 363698f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 363798f90dbaSZhi Yong Wu wait_time = (ios_base + 1) / iops_limit; 363898f90dbaSZhi Yong Wu if (wait_time > elapsed_time) { 363998f90dbaSZhi Yong Wu wait_time = wait_time - elapsed_time; 364098f90dbaSZhi Yong Wu } else { 364198f90dbaSZhi Yong Wu wait_time = 0; 364298f90dbaSZhi Yong Wu } 364398f90dbaSZhi Yong Wu 364498f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 364598f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 364698f90dbaSZhi Yong Wu if (wait) { 364798f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 364898f90dbaSZhi Yong Wu } 364998f90dbaSZhi Yong Wu 365098f90dbaSZhi Yong Wu return true; 365198f90dbaSZhi Yong Wu } 365298f90dbaSZhi Yong Wu 365398f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 365498f90dbaSZhi Yong Wu bool is_write, int64_t *wait) 365598f90dbaSZhi Yong Wu { 365698f90dbaSZhi Yong Wu int64_t now, max_wait; 365798f90dbaSZhi Yong Wu uint64_t bps_wait = 0, iops_wait = 0; 365898f90dbaSZhi Yong Wu double elapsed_time; 365998f90dbaSZhi Yong Wu int bps_ret, iops_ret; 366098f90dbaSZhi Yong Wu 366198f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 366298f90dbaSZhi Yong Wu if ((bs->slice_start < now) 366398f90dbaSZhi Yong Wu && (bs->slice_end > now)) { 366498f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 366598f90dbaSZhi Yong Wu } else { 366698f90dbaSZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 366798f90dbaSZhi Yong Wu bs->slice_start = now; 366898f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 366998f90dbaSZhi Yong Wu 367098f90dbaSZhi Yong Wu bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; 367198f90dbaSZhi Yong Wu bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; 367298f90dbaSZhi Yong Wu 367398f90dbaSZhi Yong Wu bs->io_base.ios[is_write] = bs->nr_ops[is_write]; 367498f90dbaSZhi Yong Wu bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; 367598f90dbaSZhi Yong Wu } 367698f90dbaSZhi Yong Wu 367798f90dbaSZhi Yong Wu elapsed_time = now - bs->slice_start; 367898f90dbaSZhi Yong Wu elapsed_time /= (NANOSECONDS_PER_SECOND); 367998f90dbaSZhi Yong Wu 368098f90dbaSZhi Yong Wu bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, 368198f90dbaSZhi Yong Wu is_write, elapsed_time, &bps_wait); 368298f90dbaSZhi Yong Wu iops_ret = bdrv_exceed_iops_limits(bs, is_write, 368398f90dbaSZhi Yong Wu elapsed_time, &iops_wait); 368498f90dbaSZhi Yong Wu if (bps_ret || iops_ret) { 368598f90dbaSZhi Yong Wu max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; 368698f90dbaSZhi Yong Wu if (wait) { 368798f90dbaSZhi Yong Wu *wait = max_wait; 368898f90dbaSZhi Yong Wu } 368998f90dbaSZhi Yong Wu 369098f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 369198f90dbaSZhi Yong Wu if (bs->slice_end < now + max_wait) { 369298f90dbaSZhi Yong Wu bs->slice_end = now + max_wait; 369398f90dbaSZhi Yong Wu } 369498f90dbaSZhi Yong Wu 369598f90dbaSZhi Yong Wu return true; 369698f90dbaSZhi Yong Wu } 369798f90dbaSZhi Yong Wu 369898f90dbaSZhi Yong Wu if (wait) { 369998f90dbaSZhi Yong Wu *wait = 0; 370098f90dbaSZhi Yong Wu } 370198f90dbaSZhi Yong Wu 370298f90dbaSZhi Yong Wu return false; 370398f90dbaSZhi Yong Wu } 370483f64091Sbellard 370583f64091Sbellard /**************************************************************/ 370683f64091Sbellard /* async block device emulation */ 370783f64091Sbellard 3708c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3709c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3710c16b5a2cSChristoph Hellwig QEMUBH *bh; 3711c16b5a2cSChristoph Hellwig int ret; 3712c16b5a2cSChristoph Hellwig /* vector translation state */ 3713c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3714c16b5a2cSChristoph Hellwig uint8_t *bounce; 3715c16b5a2cSChristoph Hellwig int is_write; 3716c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3717c16b5a2cSChristoph Hellwig 3718c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3719c16b5a2cSChristoph Hellwig { 3720b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3721b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 37226a7ad299SDor Laor qemu_bh_delete(acb->bh); 372336afc451SAvi Kivity acb->bh = NULL; 3724c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3725c16b5a2cSChristoph Hellwig } 3726c16b5a2cSChristoph Hellwig 3727d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_aiocb_info = { 3728c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3729c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3730c16b5a2cSChristoph Hellwig }; 3731c16b5a2cSChristoph Hellwig 373283f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3733beac80cdSbellard { 3734ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3735f141eafeSaliguori 3736f141eafeSaliguori if (!acb->is_write) 373703396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 3738ceb42de8Saliguori qemu_vfree(acb->bounce); 3739ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 37406a7ad299SDor Laor qemu_bh_delete(acb->bh); 374136afc451SAvi Kivity acb->bh = NULL; 3742ce1a14dcSpbrook qemu_aio_release(acb); 3743beac80cdSbellard } 3744beac80cdSbellard 3745f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3746f141eafeSaliguori int64_t sector_num, 3747f141eafeSaliguori QEMUIOVector *qiov, 3748f141eafeSaliguori int nb_sectors, 3749f141eafeSaliguori BlockDriverCompletionFunc *cb, 3750f141eafeSaliguori void *opaque, 3751f141eafeSaliguori int is_write) 3752f141eafeSaliguori 3753ea2384d3Sbellard { 3754ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 375583f64091Sbellard 3756d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); 3757f141eafeSaliguori acb->is_write = is_write; 3758f141eafeSaliguori acb->qiov = qiov; 3759e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3760ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3761f141eafeSaliguori 3762f141eafeSaliguori if (is_write) { 3763d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 37641ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3765f141eafeSaliguori } else { 37661ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3767f141eafeSaliguori } 3768f141eafeSaliguori 3769ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3770f141eafeSaliguori 3771ce1a14dcSpbrook return &acb->common; 37727a6cba61Spbrook } 37737a6cba61Spbrook 3774f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3775f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3776ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 377783f64091Sbellard { 3778f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 377983f64091Sbellard } 378083f64091Sbellard 3781f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3782f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3783f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3784f141eafeSaliguori { 3785f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3786f141eafeSaliguori } 3787f141eafeSaliguori 378868485420SKevin Wolf 378968485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 379068485420SKevin Wolf BlockDriverAIOCB common; 379168485420SKevin Wolf BlockRequest req; 379268485420SKevin Wolf bool is_write; 3793d318aea9SKevin Wolf bool *done; 379468485420SKevin Wolf QEMUBH* bh; 379568485420SKevin Wolf } BlockDriverAIOCBCoroutine; 379668485420SKevin Wolf 379768485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 379868485420SKevin Wolf { 3799d318aea9SKevin Wolf BlockDriverAIOCBCoroutine *acb = 3800d318aea9SKevin Wolf container_of(blockacb, BlockDriverAIOCBCoroutine, common); 3801d318aea9SKevin Wolf bool done = false; 3802d318aea9SKevin Wolf 3803d318aea9SKevin Wolf acb->done = &done; 3804d318aea9SKevin Wolf while (!done) { 3805d318aea9SKevin Wolf qemu_aio_wait(); 3806d318aea9SKevin Wolf } 380768485420SKevin Wolf } 380868485420SKevin Wolf 3809d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_co_aiocb_info = { 381068485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 381168485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 381268485420SKevin Wolf }; 381368485420SKevin Wolf 381435246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 381568485420SKevin Wolf { 381668485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 381768485420SKevin Wolf 381868485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 3819d318aea9SKevin Wolf 3820d318aea9SKevin Wolf if (acb->done) { 3821d318aea9SKevin Wolf *acb->done = true; 3822d318aea9SKevin Wolf } 3823d318aea9SKevin Wolf 382468485420SKevin Wolf qemu_bh_delete(acb->bh); 382568485420SKevin Wolf qemu_aio_release(acb); 382668485420SKevin Wolf } 382768485420SKevin Wolf 3828b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3829b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3830b2a61371SStefan Hajnoczi { 3831b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3832b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3833b2a61371SStefan Hajnoczi 3834b2a61371SStefan Hajnoczi if (!acb->is_write) { 3835b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 3836470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3837b2a61371SStefan Hajnoczi } else { 3838b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 3839f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3840b2a61371SStefan Hajnoczi } 3841b2a61371SStefan Hajnoczi 384235246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3843b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 3844b2a61371SStefan Hajnoczi } 3845b2a61371SStefan Hajnoczi 384668485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 384768485420SKevin Wolf int64_t sector_num, 384868485420SKevin Wolf QEMUIOVector *qiov, 384968485420SKevin Wolf int nb_sectors, 385068485420SKevin Wolf BlockDriverCompletionFunc *cb, 385168485420SKevin Wolf void *opaque, 38528c5873d6SStefan Hajnoczi bool is_write) 385368485420SKevin Wolf { 385468485420SKevin Wolf Coroutine *co; 385568485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 385668485420SKevin Wolf 3857d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 385868485420SKevin Wolf acb->req.sector = sector_num; 385968485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 386068485420SKevin Wolf acb->req.qiov = qiov; 386168485420SKevin Wolf acb->is_write = is_write; 3862d318aea9SKevin Wolf acb->done = NULL; 386368485420SKevin Wolf 38648c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 386568485420SKevin Wolf qemu_coroutine_enter(co, acb); 386668485420SKevin Wolf 386768485420SKevin Wolf return &acb->common; 386868485420SKevin Wolf } 386968485420SKevin Wolf 387007f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 3871b2e12bc6SChristoph Hellwig { 387207f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 387307f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 3874b2e12bc6SChristoph Hellwig 387507f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 387607f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3877b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 3878b2e12bc6SChristoph Hellwig } 3879b2e12bc6SChristoph Hellwig 388007f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 3881016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 3882016f5cf6SAlexander Graf { 388307f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 3884016f5cf6SAlexander Graf 388507f07615SPaolo Bonzini Coroutine *co; 388607f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 3887016f5cf6SAlexander Graf 3888d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 3889d318aea9SKevin Wolf acb->done = NULL; 3890d318aea9SKevin Wolf 389107f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 389207f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 3893016f5cf6SAlexander Graf 3894016f5cf6SAlexander Graf return &acb->common; 3895016f5cf6SAlexander Graf } 3896016f5cf6SAlexander Graf 38974265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 38984265d620SPaolo Bonzini { 38994265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 39004265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 39014265d620SPaolo Bonzini 39024265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 39034265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 39044265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 39054265d620SPaolo Bonzini } 39064265d620SPaolo Bonzini 39074265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 39084265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 39094265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 39104265d620SPaolo Bonzini { 39114265d620SPaolo Bonzini Coroutine *co; 39124265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 39134265d620SPaolo Bonzini 39144265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 39154265d620SPaolo Bonzini 3916d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 39174265d620SPaolo Bonzini acb->req.sector = sector_num; 39184265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 3919d318aea9SKevin Wolf acb->done = NULL; 39204265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 39214265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 39224265d620SPaolo Bonzini 39234265d620SPaolo Bonzini return &acb->common; 39244265d620SPaolo Bonzini } 39254265d620SPaolo Bonzini 3926ea2384d3Sbellard void bdrv_init(void) 3927ea2384d3Sbellard { 39285efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3929ea2384d3Sbellard } 3930ce1a14dcSpbrook 3931eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3932eb852011SMarkus Armbruster { 3933eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3934eb852011SMarkus Armbruster bdrv_init(); 3935eb852011SMarkus Armbruster } 3936eb852011SMarkus Armbruster 3937d7331bedSStefan Hajnoczi void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 39386bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 39396bbff9a0Saliguori { 3940ce1a14dcSpbrook BlockDriverAIOCB *acb; 3941ce1a14dcSpbrook 3942d7331bedSStefan Hajnoczi acb = g_slice_alloc(aiocb_info->aiocb_size); 3943d7331bedSStefan Hajnoczi acb->aiocb_info = aiocb_info; 3944ce1a14dcSpbrook acb->bs = bs; 3945ce1a14dcSpbrook acb->cb = cb; 3946ce1a14dcSpbrook acb->opaque = opaque; 3947ce1a14dcSpbrook return acb; 3948ce1a14dcSpbrook } 3949ce1a14dcSpbrook 3950ce1a14dcSpbrook void qemu_aio_release(void *p) 3951ce1a14dcSpbrook { 3952d37c975fSStefan Hajnoczi BlockDriverAIOCB *acb = p; 3953d7331bedSStefan Hajnoczi g_slice_free1(acb->aiocb_info->aiocb_size, acb); 3954ce1a14dcSpbrook } 395519cb3738Sbellard 395619cb3738Sbellard /**************************************************************/ 3957f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 3958f9f05dc5SKevin Wolf 3959f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 3960f9f05dc5SKevin Wolf Coroutine *coroutine; 3961f9f05dc5SKevin Wolf int ret; 3962f9f05dc5SKevin Wolf } CoroutineIOCompletion; 3963f9f05dc5SKevin Wolf 3964f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 3965f9f05dc5SKevin Wolf { 3966f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 3967f9f05dc5SKevin Wolf 3968f9f05dc5SKevin Wolf co->ret = ret; 3969f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 3970f9f05dc5SKevin Wolf } 3971f9f05dc5SKevin Wolf 3972f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 3973f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 3974f9f05dc5SKevin Wolf bool is_write) 3975f9f05dc5SKevin Wolf { 3976f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 3977f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 3978f9f05dc5SKevin Wolf }; 3979f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 3980f9f05dc5SKevin Wolf 3981f9f05dc5SKevin Wolf if (is_write) { 3982a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 3983f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3984f9f05dc5SKevin Wolf } else { 3985a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 3986f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3987f9f05dc5SKevin Wolf } 3988f9f05dc5SKevin Wolf 398959370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 3990f9f05dc5SKevin Wolf if (!acb) { 3991f9f05dc5SKevin Wolf return -EIO; 3992f9f05dc5SKevin Wolf } 3993f9f05dc5SKevin Wolf qemu_coroutine_yield(); 3994f9f05dc5SKevin Wolf 3995f9f05dc5SKevin Wolf return co.ret; 3996f9f05dc5SKevin Wolf } 3997f9f05dc5SKevin Wolf 3998f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 3999f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4000f9f05dc5SKevin Wolf QEMUIOVector *iov) 4001f9f05dc5SKevin Wolf { 4002f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 4003f9f05dc5SKevin Wolf } 4004f9f05dc5SKevin Wolf 4005f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 4006f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4007f9f05dc5SKevin Wolf QEMUIOVector *iov) 4008f9f05dc5SKevin Wolf { 4009f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 4010f9f05dc5SKevin Wolf } 4011f9f05dc5SKevin Wolf 401207f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 4013e7a8a783SKevin Wolf { 401407f07615SPaolo Bonzini RwCo *rwco = opaque; 401507f07615SPaolo Bonzini 401607f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 401707f07615SPaolo Bonzini } 401807f07615SPaolo Bonzini 401907f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 402007f07615SPaolo Bonzini { 4021eb489bb1SKevin Wolf int ret; 4022eb489bb1SKevin Wolf 402329cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 402407f07615SPaolo Bonzini return 0; 4025eb489bb1SKevin Wolf } 4026eb489bb1SKevin Wolf 4027ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 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 4040eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 404129cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 404207f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 404307f07615SPaolo Bonzini BlockDriverAIOCB *acb; 4044e7a8a783SKevin Wolf CoroutineIOCompletion co = { 4045e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 4046e7a8a783SKevin Wolf }; 4047e7a8a783SKevin Wolf 404807f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 404907f07615SPaolo Bonzini if (acb == NULL) { 405029cdb251SPaolo Bonzini ret = -EIO; 405107f07615SPaolo Bonzini } else { 4052e7a8a783SKevin Wolf qemu_coroutine_yield(); 405329cdb251SPaolo Bonzini ret = co.ret; 4054e7a8a783SKevin Wolf } 405507f07615SPaolo Bonzini } else { 405607f07615SPaolo Bonzini /* 405707f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 405807f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 405907f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 406007f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 406107f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 406207f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 406307f07615SPaolo Bonzini * mode. 406407f07615SPaolo Bonzini * 406507f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 406607f07615SPaolo Bonzini */ 406729cdb251SPaolo Bonzini ret = 0; 406807f07615SPaolo Bonzini } 406929cdb251SPaolo Bonzini if (ret < 0) { 407029cdb251SPaolo Bonzini return ret; 407129cdb251SPaolo Bonzini } 407229cdb251SPaolo Bonzini 407329cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 407429cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 407529cdb251SPaolo Bonzini */ 4076d4c82329SKevin Wolf flush_parent: 407729cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 407807f07615SPaolo Bonzini } 407907f07615SPaolo Bonzini 40800f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 40810f15423cSAnthony Liguori { 40820f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 40830f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 40840f15423cSAnthony Liguori } 40850f15423cSAnthony Liguori } 40860f15423cSAnthony Liguori 40870f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 40880f15423cSAnthony Liguori { 40890f15423cSAnthony Liguori BlockDriverState *bs; 40900f15423cSAnthony Liguori 40910f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 40920f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 40930f15423cSAnthony Liguori } 40940f15423cSAnthony Liguori } 40950f15423cSAnthony Liguori 409607789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 409707789269SBenoît Canet { 409807789269SBenoît Canet BlockDriverState *bs; 409907789269SBenoît Canet 410007789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 410107789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 410207789269SBenoît Canet } 410307789269SBenoît Canet } 410407789269SBenoît Canet 410507f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 410607f07615SPaolo Bonzini { 410707f07615SPaolo Bonzini Coroutine *co; 410807f07615SPaolo Bonzini RwCo rwco = { 410907f07615SPaolo Bonzini .bs = bs, 411007f07615SPaolo Bonzini .ret = NOT_DONE, 411107f07615SPaolo Bonzini }; 411207f07615SPaolo Bonzini 411307f07615SPaolo Bonzini if (qemu_in_coroutine()) { 411407f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 411507f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 411607f07615SPaolo Bonzini } else { 411707f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 411807f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 411907f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 412007f07615SPaolo Bonzini qemu_aio_wait(); 412107f07615SPaolo Bonzini } 412207f07615SPaolo Bonzini } 412307f07615SPaolo Bonzini 412407f07615SPaolo Bonzini return rwco.ret; 412507f07615SPaolo Bonzini } 4126e7a8a783SKevin Wolf 41274265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 41284265d620SPaolo Bonzini { 41294265d620SPaolo Bonzini RwCo *rwco = opaque; 41304265d620SPaolo Bonzini 41314265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 41324265d620SPaolo Bonzini } 41334265d620SPaolo Bonzini 41344265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 41354265d620SPaolo Bonzini int nb_sectors) 41364265d620SPaolo Bonzini { 41374265d620SPaolo Bonzini if (!bs->drv) { 41384265d620SPaolo Bonzini return -ENOMEDIUM; 41394265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 41404265d620SPaolo Bonzini return -EIO; 41414265d620SPaolo Bonzini } else if (bs->read_only) { 41424265d620SPaolo Bonzini return -EROFS; 4143df702c9bSPaolo Bonzini } 4144df702c9bSPaolo Bonzini 4145df702c9bSPaolo Bonzini if (bs->dirty_bitmap) { 41468f0720ecSPaolo Bonzini bdrv_reset_dirty(bs, sector_num, nb_sectors); 4147df702c9bSPaolo Bonzini } 4148df702c9bSPaolo Bonzini 4149df702c9bSPaolo Bonzini if (bs->drv->bdrv_co_discard) { 41504265d620SPaolo Bonzini return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors); 41514265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 41524265d620SPaolo Bonzini BlockDriverAIOCB *acb; 41534265d620SPaolo Bonzini CoroutineIOCompletion co = { 41544265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 41554265d620SPaolo Bonzini }; 41564265d620SPaolo Bonzini 41574265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 41584265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 41594265d620SPaolo Bonzini if (acb == NULL) { 41604265d620SPaolo Bonzini return -EIO; 41614265d620SPaolo Bonzini } else { 41624265d620SPaolo Bonzini qemu_coroutine_yield(); 41634265d620SPaolo Bonzini return co.ret; 41644265d620SPaolo Bonzini } 41654265d620SPaolo Bonzini } else { 41664265d620SPaolo Bonzini return 0; 41674265d620SPaolo Bonzini } 41684265d620SPaolo Bonzini } 41694265d620SPaolo Bonzini 41704265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 41714265d620SPaolo Bonzini { 41724265d620SPaolo Bonzini Coroutine *co; 41734265d620SPaolo Bonzini RwCo rwco = { 41744265d620SPaolo Bonzini .bs = bs, 41754265d620SPaolo Bonzini .sector_num = sector_num, 41764265d620SPaolo Bonzini .nb_sectors = nb_sectors, 41774265d620SPaolo Bonzini .ret = NOT_DONE, 41784265d620SPaolo Bonzini }; 41794265d620SPaolo Bonzini 41804265d620SPaolo Bonzini if (qemu_in_coroutine()) { 41814265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 41824265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 41834265d620SPaolo Bonzini } else { 41844265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 41854265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 41864265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 41874265d620SPaolo Bonzini qemu_aio_wait(); 41884265d620SPaolo Bonzini } 41894265d620SPaolo Bonzini } 41904265d620SPaolo Bonzini 41914265d620SPaolo Bonzini return rwco.ret; 41924265d620SPaolo Bonzini } 41934265d620SPaolo Bonzini 4194f9f05dc5SKevin Wolf /**************************************************************/ 419519cb3738Sbellard /* removable device support */ 419619cb3738Sbellard 419719cb3738Sbellard /** 419819cb3738Sbellard * Return TRUE if the media is present 419919cb3738Sbellard */ 420019cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 420119cb3738Sbellard { 420219cb3738Sbellard BlockDriver *drv = bs->drv; 4203a1aff5bfSMarkus Armbruster 420419cb3738Sbellard if (!drv) 420519cb3738Sbellard return 0; 420619cb3738Sbellard if (!drv->bdrv_is_inserted) 4207a1aff5bfSMarkus Armbruster return 1; 4208a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 420919cb3738Sbellard } 421019cb3738Sbellard 421119cb3738Sbellard /** 42128e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 42138e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 421419cb3738Sbellard */ 421519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 421619cb3738Sbellard { 421719cb3738Sbellard BlockDriver *drv = bs->drv; 421819cb3738Sbellard 42198e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 42208e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 42218e49ca46SMarkus Armbruster } 42228e49ca46SMarkus Armbruster return -ENOTSUP; 422319cb3738Sbellard } 422419cb3738Sbellard 422519cb3738Sbellard /** 422619cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 422719cb3738Sbellard */ 4228f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 422919cb3738Sbellard { 423019cb3738Sbellard BlockDriver *drv = bs->drv; 423119cb3738Sbellard 4232822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 4233822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 423419cb3738Sbellard } 42356f382ed2SLuiz Capitulino 42366f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 42376f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 42386f382ed2SLuiz Capitulino } 423919cb3738Sbellard } 424019cb3738Sbellard 424119cb3738Sbellard /** 424219cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 424319cb3738Sbellard * to eject it manually). 424419cb3738Sbellard */ 4245025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 424619cb3738Sbellard { 424719cb3738Sbellard BlockDriver *drv = bs->drv; 424819cb3738Sbellard 4249025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 4250b8c6d095SStefan Hajnoczi 4251025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 4252025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 425319cb3738Sbellard } 425419cb3738Sbellard } 4255985a03b0Sths 4256985a03b0Sths /* needed for generic scsi interface */ 4257985a03b0Sths 4258985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 4259985a03b0Sths { 4260985a03b0Sths BlockDriver *drv = bs->drv; 4261985a03b0Sths 4262985a03b0Sths if (drv && drv->bdrv_ioctl) 4263985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 4264985a03b0Sths return -ENOTSUP; 4265985a03b0Sths } 42667d780669Saliguori 4267221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 4268221f715dSaliguori unsigned long int req, void *buf, 42697d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 42707d780669Saliguori { 4271221f715dSaliguori BlockDriver *drv = bs->drv; 42727d780669Saliguori 4273221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 4274221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 4275221f715dSaliguori return NULL; 42767d780669Saliguori } 4277e268ca52Saliguori 42787b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 42797b6f9300SMarkus Armbruster { 42807b6f9300SMarkus Armbruster bs->buffer_alignment = align; 42817b6f9300SMarkus Armbruster } 42827cd1e32aSlirans@il.ibm.com 4283e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 4284e268ca52Saliguori { 4285e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 4286e268ca52Saliguori } 42877cd1e32aSlirans@il.ibm.com 4288c53b1c51SStefan Hajnoczi /* 4289c53b1c51SStefan Hajnoczi * Check if all memory in this vector is sector aligned. 4290c53b1c51SStefan Hajnoczi */ 4291c53b1c51SStefan Hajnoczi bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) 4292c53b1c51SStefan Hajnoczi { 4293c53b1c51SStefan Hajnoczi int i; 4294c53b1c51SStefan Hajnoczi 4295c53b1c51SStefan Hajnoczi for (i = 0; i < qiov->niov; i++) { 4296c53b1c51SStefan Hajnoczi if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { 4297c53b1c51SStefan Hajnoczi return false; 4298c53b1c51SStefan Hajnoczi } 4299c53b1c51SStefan Hajnoczi } 4300c53b1c51SStefan Hajnoczi 4301c53b1c51SStefan Hajnoczi return true; 4302c53b1c51SStefan Hajnoczi } 4303c53b1c51SStefan Hajnoczi 4304*50717e94SPaolo Bonzini void bdrv_set_dirty_tracking(BlockDriverState *bs, int granularity) 43057cd1e32aSlirans@il.ibm.com { 43067cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 4307a55eb92cSJan Kiszka 4308*50717e94SPaolo Bonzini assert((granularity & (granularity - 1)) == 0); 4309*50717e94SPaolo Bonzini 4310*50717e94SPaolo Bonzini if (granularity) { 4311*50717e94SPaolo Bonzini granularity >>= BDRV_SECTOR_BITS; 4312*50717e94SPaolo Bonzini assert(!bs->dirty_bitmap); 43138f0720ecSPaolo Bonzini bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); 4314*50717e94SPaolo Bonzini bs->dirty_bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); 43157cd1e32aSlirans@il.ibm.com } else { 4316c6d22830SJan Kiszka if (bs->dirty_bitmap) { 43178f0720ecSPaolo Bonzini hbitmap_free(bs->dirty_bitmap); 4318c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 43197cd1e32aSlirans@il.ibm.com } 43207cd1e32aSlirans@il.ibm.com } 43217cd1e32aSlirans@il.ibm.com } 43227cd1e32aSlirans@il.ibm.com 43237cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 43247cd1e32aSlirans@il.ibm.com { 43258f0720ecSPaolo Bonzini if (bs->dirty_bitmap) { 43268f0720ecSPaolo Bonzini return hbitmap_get(bs->dirty_bitmap, sector); 43277cd1e32aSlirans@il.ibm.com } else { 43287cd1e32aSlirans@il.ibm.com return 0; 43297cd1e32aSlirans@il.ibm.com } 43307cd1e32aSlirans@il.ibm.com } 43317cd1e32aSlirans@il.ibm.com 43328f0720ecSPaolo Bonzini void bdrv_dirty_iter_init(BlockDriverState *bs, HBitmapIter *hbi) 43331755da16SPaolo Bonzini { 43348f0720ecSPaolo Bonzini hbitmap_iter_init(hbi, bs->dirty_bitmap, 0); 43351755da16SPaolo Bonzini } 43361755da16SPaolo Bonzini 43371755da16SPaolo Bonzini void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 43381755da16SPaolo Bonzini int nr_sectors) 43391755da16SPaolo Bonzini { 43408f0720ecSPaolo Bonzini hbitmap_set(bs->dirty_bitmap, cur_sector, nr_sectors); 43411755da16SPaolo Bonzini } 43421755da16SPaolo Bonzini 43437cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 43447cd1e32aSlirans@il.ibm.com int nr_sectors) 43457cd1e32aSlirans@il.ibm.com { 43468f0720ecSPaolo Bonzini hbitmap_reset(bs->dirty_bitmap, cur_sector, nr_sectors); 43477cd1e32aSlirans@il.ibm.com } 4348aaa0eb75SLiran Schour 4349aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 4350aaa0eb75SLiran Schour { 43518f0720ecSPaolo Bonzini if (bs->dirty_bitmap) { 4352acc906c6SPaolo Bonzini return hbitmap_count(bs->dirty_bitmap); 43538f0720ecSPaolo Bonzini } else { 43548f0720ecSPaolo Bonzini return 0; 43558f0720ecSPaolo Bonzini } 4356aaa0eb75SLiran Schour } 4357f88e1a42SJes Sorensen 4358db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 4359db593f25SMarcelo Tosatti { 4360db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 4361db593f25SMarcelo Tosatti bs->in_use = in_use; 4362db593f25SMarcelo Tosatti } 4363db593f25SMarcelo Tosatti 4364db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 4365db593f25SMarcelo Tosatti { 4366db593f25SMarcelo Tosatti return bs->in_use; 4367db593f25SMarcelo Tosatti } 4368db593f25SMarcelo Tosatti 436928a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 437028a7282aSLuiz Capitulino { 4371d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 437258e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 437328a7282aSLuiz Capitulino } 437428a7282aSLuiz Capitulino 437528a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 437628a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 437728a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 437828a7282aSLuiz Capitulino { 4379d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 438092aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 438192aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 438292aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 438328a7282aSLuiz Capitulino } 438428a7282aSLuiz Capitulino 438528a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 438628a7282aSLuiz Capitulino { 4387d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 438828a7282aSLuiz Capitulino } 438928a7282aSLuiz Capitulino 439028a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 439128a7282aSLuiz Capitulino { 439228a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 439358e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 43943bd293c3SPaolo Bonzini if (bs->job) { 43953bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 43963bd293c3SPaolo Bonzini } 439728a7282aSLuiz Capitulino } 439828a7282aSLuiz Capitulino } 439928a7282aSLuiz Capitulino 440028a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 440128a7282aSLuiz Capitulino { 44023e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 44033e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 440458e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 440558e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 440628a7282aSLuiz Capitulino } 440728a7282aSLuiz Capitulino } 440828a7282aSLuiz Capitulino 4409a597e79cSChristoph Hellwig void 4410a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 4411a597e79cSChristoph Hellwig enum BlockAcctType type) 4412a597e79cSChristoph Hellwig { 4413a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 4414a597e79cSChristoph Hellwig 4415a597e79cSChristoph Hellwig cookie->bytes = bytes; 4416c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4417a597e79cSChristoph Hellwig cookie->type = type; 4418a597e79cSChristoph Hellwig } 4419a597e79cSChristoph Hellwig 4420a597e79cSChristoph Hellwig void 4421a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4422a597e79cSChristoph Hellwig { 4423a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4424a597e79cSChristoph Hellwig 4425a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4426a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4427c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4428a597e79cSChristoph Hellwig } 4429a597e79cSChristoph Hellwig 4430d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 4431f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 443271c79813SLuiz Capitulino char *options, uint64_t img_size, int flags, Error **errp) 4433f88e1a42SJes Sorensen { 4434f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4435d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4436f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 4437f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 443896df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4439f88e1a42SJes Sorensen int ret = 0; 4440f88e1a42SJes Sorensen 4441f88e1a42SJes Sorensen /* Find driver and parse its options */ 4442f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4443f88e1a42SJes Sorensen if (!drv) { 444471c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 4445d92ada22SLuiz Capitulino return; 4446f88e1a42SJes Sorensen } 4447f88e1a42SJes Sorensen 4448f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 4449f88e1a42SJes Sorensen if (!proto_drv) { 445071c79813SLuiz Capitulino error_setg(errp, "Unknown protocol '%s'", filename); 4451d92ada22SLuiz Capitulino return; 4452f88e1a42SJes Sorensen } 4453f88e1a42SJes Sorensen 4454f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4455f88e1a42SJes Sorensen drv->create_options); 4456f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4457f88e1a42SJes Sorensen proto_drv->create_options); 4458f88e1a42SJes Sorensen 4459f88e1a42SJes Sorensen /* Create parameter list with default values */ 4460f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4461f88e1a42SJes Sorensen 4462f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4463f88e1a42SJes Sorensen 4464f88e1a42SJes Sorensen /* Parse -o options */ 4465f88e1a42SJes Sorensen if (options) { 4466f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4467f88e1a42SJes Sorensen if (param == NULL) { 446871c79813SLuiz Capitulino error_setg(errp, "Invalid options for file format '%s'.", fmt); 4469f88e1a42SJes Sorensen goto out; 4470f88e1a42SJes Sorensen } 4471f88e1a42SJes Sorensen } 4472f88e1a42SJes Sorensen 4473f88e1a42SJes Sorensen if (base_filename) { 4474f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4475f88e1a42SJes Sorensen base_filename)) { 447671c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 447771c79813SLuiz Capitulino fmt); 4478f88e1a42SJes Sorensen goto out; 4479f88e1a42SJes Sorensen } 4480f88e1a42SJes Sorensen } 4481f88e1a42SJes Sorensen 4482f88e1a42SJes Sorensen if (base_fmt) { 4483f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 448471c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 448571c79813SLuiz Capitulino "format '%s'", fmt); 4486f88e1a42SJes Sorensen goto out; 4487f88e1a42SJes Sorensen } 4488f88e1a42SJes Sorensen } 4489f88e1a42SJes Sorensen 4490792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4491792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4492792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 449371c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 449471c79813SLuiz Capitulino "same filename as the backing file"); 4495792da93aSJes Sorensen goto out; 4496792da93aSJes Sorensen } 4497792da93aSJes Sorensen } 4498792da93aSJes Sorensen 4499f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4500f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 450196df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 450296df67d1SStefan Hajnoczi if (!backing_drv) { 450371c79813SLuiz Capitulino error_setg(errp, "Unknown backing file format '%s'", 450471c79813SLuiz Capitulino backing_fmt->value.s); 4505f88e1a42SJes Sorensen goto out; 4506f88e1a42SJes Sorensen } 4507f88e1a42SJes Sorensen } 4508f88e1a42SJes Sorensen 4509f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4510f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4511d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4512d220894eSKevin Wolf if (size && size->value.n == -1) { 4513f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4514f88e1a42SJes Sorensen uint64_t size; 4515f88e1a42SJes Sorensen char buf[32]; 451663090dacSPaolo Bonzini int back_flags; 451763090dacSPaolo Bonzini 451863090dacSPaolo Bonzini /* backing files always opened read-only */ 451963090dacSPaolo Bonzini back_flags = 452063090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 4521f88e1a42SJes Sorensen 4522f88e1a42SJes Sorensen bs = bdrv_new(""); 4523f88e1a42SJes Sorensen 452463090dacSPaolo Bonzini ret = bdrv_open(bs, backing_file->value.s, back_flags, backing_drv); 4525f88e1a42SJes Sorensen if (ret < 0) { 452671c79813SLuiz Capitulino error_setg_errno(errp, -ret, "Could not open '%s'", 452771c79813SLuiz Capitulino backing_file->value.s); 4528f88e1a42SJes Sorensen goto out; 4529f88e1a42SJes Sorensen } 4530f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4531f88e1a42SJes Sorensen size *= 512; 4532f88e1a42SJes Sorensen 4533f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4534f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4535f88e1a42SJes Sorensen } else { 453671c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 4537f88e1a42SJes Sorensen goto out; 4538f88e1a42SJes Sorensen } 4539f88e1a42SJes Sorensen } 4540f88e1a42SJes Sorensen 4541f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4542f88e1a42SJes Sorensen print_option_parameters(param); 4543f88e1a42SJes Sorensen puts(""); 4544f88e1a42SJes Sorensen 4545f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 4546f88e1a42SJes Sorensen if (ret < 0) { 4547f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 454871c79813SLuiz Capitulino error_setg(errp,"Formatting or formatting option not supported for " 454971c79813SLuiz Capitulino "file format '%s'", fmt); 4550f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 455171c79813SLuiz Capitulino error_setg(errp, "The image size is too large for file format '%s'", 455271c79813SLuiz Capitulino fmt); 4553f88e1a42SJes Sorensen } else { 455471c79813SLuiz Capitulino error_setg(errp, "%s: error while creating %s: %s", filename, fmt, 455571c79813SLuiz Capitulino strerror(-ret)); 4556f88e1a42SJes Sorensen } 4557f88e1a42SJes Sorensen } 4558f88e1a42SJes Sorensen 4559f88e1a42SJes Sorensen out: 4560f88e1a42SJes Sorensen free_option_parameters(create_options); 4561f88e1a42SJes Sorensen free_option_parameters(param); 4562f88e1a42SJes Sorensen 4563f88e1a42SJes Sorensen if (bs) { 4564f88e1a42SJes Sorensen bdrv_delete(bs); 4565f88e1a42SJes Sorensen } 4566f88e1a42SJes Sorensen } 4567