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" 27376253ecSaliguori #include "monitor.h" 28ea2384d3Sbellard #include "block_int.h" 292f0c9fe6SPaolo Bonzini #include "blockjob.h" 305efa9d5aSAnthony Liguori #include "module.h" 31f795e743SLuiz Capitulino #include "qjson.h" 323e1caa5fSPaolo Bonzini #include "sysemu.h" 33d7d512f6SPaolo Bonzini #include "notify.h" 3468485420SKevin Wolf #include "qemu-coroutine.h" 35b2023818SLuiz Capitulino #include "qmp-commands.h" 360563e191SZhi Yong Wu #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->slice_time = 5 * BLOCK_IO_SLICE_TIME; 1590563e191SZhi Yong Wu bs->slice_start = qemu_get_clock_ns(vm_clock); 1600563e191SZhi Yong Wu bs->slice_end = bs->slice_start + bs->slice_time; 1610563e191SZhi Yong Wu memset(&bs->io_base, 0, sizeof(bs->io_base)); 1620563e191SZhi Yong Wu bs->io_limits_enabled = true; 1630563e191SZhi Yong Wu } 1640563e191SZhi Yong Wu 1650563e191SZhi Yong Wu bool bdrv_io_limits_enabled(BlockDriverState *bs) 1660563e191SZhi Yong Wu { 1670563e191SZhi Yong Wu BlockIOLimit *io_limits = &bs->io_limits; 1680563e191SZhi Yong Wu return io_limits->bps[BLOCK_IO_LIMIT_READ] 1690563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_WRITE] 1700563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_TOTAL] 1710563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_READ] 1720563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_WRITE] 1730563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_TOTAL]; 1740563e191SZhi Yong Wu } 1750563e191SZhi Yong Wu 17698f90dbaSZhi Yong Wu static void bdrv_io_limits_intercept(BlockDriverState *bs, 17798f90dbaSZhi Yong Wu bool is_write, int nb_sectors) 17898f90dbaSZhi Yong Wu { 17998f90dbaSZhi Yong Wu int64_t wait_time = -1; 18098f90dbaSZhi Yong Wu 18198f90dbaSZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 18298f90dbaSZhi Yong Wu qemu_co_queue_wait(&bs->throttled_reqs); 18398f90dbaSZhi Yong Wu } 18498f90dbaSZhi Yong Wu 18598f90dbaSZhi Yong Wu /* In fact, we hope to keep each request's timing, in FIFO mode. The next 18698f90dbaSZhi Yong Wu * throttled requests will not be dequeued until the current request is 18798f90dbaSZhi Yong Wu * allowed to be serviced. So if the current request still exceeds the 18898f90dbaSZhi Yong Wu * limits, it will be inserted to the head. All requests followed it will 18998f90dbaSZhi Yong Wu * be still in throttled_reqs queue. 19098f90dbaSZhi Yong Wu */ 19198f90dbaSZhi Yong Wu 19298f90dbaSZhi Yong Wu while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) { 19398f90dbaSZhi Yong Wu qemu_mod_timer(bs->block_timer, 19498f90dbaSZhi Yong Wu wait_time + qemu_get_clock_ns(vm_clock)); 19598f90dbaSZhi Yong Wu qemu_co_queue_wait_insert_head(&bs->throttled_reqs); 19698f90dbaSZhi Yong Wu } 19798f90dbaSZhi Yong Wu 19898f90dbaSZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 19998f90dbaSZhi Yong Wu } 20098f90dbaSZhi Yong Wu 2019e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 2029e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 2039e0b22f4SStefan Hajnoczi { 204947995c0SPaolo Bonzini const char *p; 205947995c0SPaolo Bonzini 2069e0b22f4SStefan Hajnoczi #ifdef _WIN32 2079e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2089e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2099e0b22f4SStefan Hajnoczi return 0; 2109e0b22f4SStefan Hajnoczi } 211947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 212947995c0SPaolo Bonzini #else 213947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2149e0b22f4SStefan Hajnoczi #endif 2159e0b22f4SStefan Hajnoczi 216947995c0SPaolo Bonzini return *p == ':'; 2179e0b22f4SStefan Hajnoczi } 2189e0b22f4SStefan Hajnoczi 21983f64091Sbellard int path_is_absolute(const char *path) 22083f64091Sbellard { 22121664424Sbellard #ifdef _WIN32 22221664424Sbellard /* specific case for names like: "\\.\d:" */ 223f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 22421664424Sbellard return 1; 225f53f4da9SPaolo Bonzini } 226f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 2273b9f94e1Sbellard #else 228f53f4da9SPaolo Bonzini return (*path == '/'); 2293b9f94e1Sbellard #endif 23083f64091Sbellard } 23183f64091Sbellard 23283f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 23383f64091Sbellard path to it by considering it is relative to base_path. URL are 23483f64091Sbellard supported. */ 23583f64091Sbellard void path_combine(char *dest, int dest_size, 23683f64091Sbellard const char *base_path, 23783f64091Sbellard const char *filename) 23883f64091Sbellard { 23983f64091Sbellard const char *p, *p1; 24083f64091Sbellard int len; 24183f64091Sbellard 24283f64091Sbellard if (dest_size <= 0) 24383f64091Sbellard return; 24483f64091Sbellard if (path_is_absolute(filename)) { 24583f64091Sbellard pstrcpy(dest, dest_size, filename); 24683f64091Sbellard } else { 24783f64091Sbellard p = strchr(base_path, ':'); 24883f64091Sbellard if (p) 24983f64091Sbellard p++; 25083f64091Sbellard else 25183f64091Sbellard p = base_path; 2523b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2533b9f94e1Sbellard #ifdef _WIN32 2543b9f94e1Sbellard { 2553b9f94e1Sbellard const char *p2; 2563b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2573b9f94e1Sbellard if (!p1 || p2 > p1) 2583b9f94e1Sbellard p1 = p2; 2593b9f94e1Sbellard } 2603b9f94e1Sbellard #endif 26183f64091Sbellard if (p1) 26283f64091Sbellard p1++; 26383f64091Sbellard else 26483f64091Sbellard p1 = base_path; 26583f64091Sbellard if (p1 > p) 26683f64091Sbellard p = p1; 26783f64091Sbellard len = p - base_path; 26883f64091Sbellard if (len > dest_size - 1) 26983f64091Sbellard len = dest_size - 1; 27083f64091Sbellard memcpy(dest, base_path, len); 27183f64091Sbellard dest[len] = '\0'; 27283f64091Sbellard pstrcat(dest, dest_size, filename); 27383f64091Sbellard } 27483f64091Sbellard } 27583f64091Sbellard 276dc5a1371SPaolo Bonzini void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) 277dc5a1371SPaolo Bonzini { 278dc5a1371SPaolo Bonzini if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { 279dc5a1371SPaolo Bonzini pstrcpy(dest, sz, bs->backing_file); 280dc5a1371SPaolo Bonzini } else { 281dc5a1371SPaolo Bonzini path_combine(dest, sz, bs->filename, bs->backing_file); 282dc5a1371SPaolo Bonzini } 283dc5a1371SPaolo Bonzini } 284dc5a1371SPaolo Bonzini 2855efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 286ea2384d3Sbellard { 2878c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 2888c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 289f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 290f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 291f9f05dc5SKevin Wolf 292f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 293f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 294f8c35c1dSStefan Hajnoczi */ 295f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 29683f64091Sbellard /* add AIO emulation layer */ 297f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 298f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 29983f64091Sbellard } 300f9f05dc5SKevin Wolf } 301b2e12bc6SChristoph Hellwig 3028a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 303ea2384d3Sbellard } 304b338082bSbellard 305b338082bSbellard /* create a new block device (by default it is empty) */ 306b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 307fc01f7e7Sbellard { 3081b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 309b338082bSbellard 3107267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 311b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 312ea2384d3Sbellard if (device_name[0] != '\0') { 3131b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 314ea2384d3Sbellard } 31528a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 316d7d512f6SPaolo Bonzini notifier_list_init(&bs->close_notifiers); 317d7d512f6SPaolo Bonzini 318b338082bSbellard return bs; 319b338082bSbellard } 320b338082bSbellard 321d7d512f6SPaolo Bonzini void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) 322d7d512f6SPaolo Bonzini { 323d7d512f6SPaolo Bonzini notifier_list_add(&bs->close_notifiers, notify); 324d7d512f6SPaolo Bonzini } 325d7d512f6SPaolo Bonzini 326ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 327ea2384d3Sbellard { 328ea2384d3Sbellard BlockDriver *drv1; 3298a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3308a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 331ea2384d3Sbellard return drv1; 332ea2384d3Sbellard } 3338a22f02aSStefan Hajnoczi } 334ea2384d3Sbellard return NULL; 335ea2384d3Sbellard } 336ea2384d3Sbellard 337eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 338eb852011SMarkus Armbruster { 339eb852011SMarkus Armbruster static const char *whitelist[] = { 340eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 341eb852011SMarkus Armbruster }; 342eb852011SMarkus Armbruster const char **p; 343eb852011SMarkus Armbruster 344eb852011SMarkus Armbruster if (!whitelist[0]) 345eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 346eb852011SMarkus Armbruster 347eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 348eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 349eb852011SMarkus Armbruster return 1; 350eb852011SMarkus Armbruster } 351eb852011SMarkus Armbruster } 352eb852011SMarkus Armbruster return 0; 353eb852011SMarkus Armbruster } 354eb852011SMarkus Armbruster 355eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 356eb852011SMarkus Armbruster { 357eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 358eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 359eb852011SMarkus Armbruster } 360eb852011SMarkus Armbruster 3615b7e1542SZhi Yong Wu typedef struct CreateCo { 3625b7e1542SZhi Yong Wu BlockDriver *drv; 3635b7e1542SZhi Yong Wu char *filename; 3645b7e1542SZhi Yong Wu QEMUOptionParameter *options; 3655b7e1542SZhi Yong Wu int ret; 3665b7e1542SZhi Yong Wu } CreateCo; 3675b7e1542SZhi Yong Wu 3685b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 3695b7e1542SZhi Yong Wu { 3705b7e1542SZhi Yong Wu CreateCo *cco = opaque; 3715b7e1542SZhi Yong Wu assert(cco->drv); 3725b7e1542SZhi Yong Wu 3735b7e1542SZhi Yong Wu cco->ret = cco->drv->bdrv_create(cco->filename, cco->options); 3745b7e1542SZhi Yong Wu } 3755b7e1542SZhi Yong Wu 3760e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 3770e7e1989SKevin Wolf QEMUOptionParameter *options) 378ea2384d3Sbellard { 3795b7e1542SZhi Yong Wu int ret; 3800e7e1989SKevin Wolf 3815b7e1542SZhi Yong Wu Coroutine *co; 3825b7e1542SZhi Yong Wu CreateCo cco = { 3835b7e1542SZhi Yong Wu .drv = drv, 3845b7e1542SZhi Yong Wu .filename = g_strdup(filename), 3855b7e1542SZhi Yong Wu .options = options, 3865b7e1542SZhi Yong Wu .ret = NOT_DONE, 3875b7e1542SZhi Yong Wu }; 3885b7e1542SZhi Yong Wu 3895b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 39080168bffSLuiz Capitulino ret = -ENOTSUP; 39180168bffSLuiz Capitulino goto out; 3925b7e1542SZhi Yong Wu } 3935b7e1542SZhi Yong Wu 3945b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 3955b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 3965b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 3975b7e1542SZhi Yong Wu } else { 3985b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 3995b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 4005b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 4015b7e1542SZhi Yong Wu qemu_aio_wait(); 4025b7e1542SZhi Yong Wu } 4035b7e1542SZhi Yong Wu } 4045b7e1542SZhi Yong Wu 4055b7e1542SZhi Yong Wu ret = cco.ret; 4065b7e1542SZhi Yong Wu 40780168bffSLuiz Capitulino out: 40880168bffSLuiz Capitulino g_free(cco.filename); 4095b7e1542SZhi Yong Wu return ret; 410ea2384d3Sbellard } 411ea2384d3Sbellard 41284a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 41384a12e66SChristoph Hellwig { 41484a12e66SChristoph Hellwig BlockDriver *drv; 41584a12e66SChristoph Hellwig 416b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 41784a12e66SChristoph Hellwig if (drv == NULL) { 41816905d71SStefan Hajnoczi return -ENOENT; 41984a12e66SChristoph Hellwig } 42084a12e66SChristoph Hellwig 42184a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 42284a12e66SChristoph Hellwig } 42384a12e66SChristoph Hellwig 424eba25057SJim Meyering /* 425eba25057SJim Meyering * Create a uniquely-named empty temporary file. 426eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 427eba25057SJim Meyering */ 428eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 429eba25057SJim Meyering { 430d5249393Sbellard #ifdef _WIN32 4313b9f94e1Sbellard char temp_dir[MAX_PATH]; 432eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 433eba25057SJim Meyering have length MAX_PATH or greater. */ 434eba25057SJim Meyering assert(size >= MAX_PATH); 435eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 436eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 437eba25057SJim Meyering ? 0 : -GetLastError()); 438d5249393Sbellard #else 439ea2384d3Sbellard int fd; 4407ccfb2ebSblueswir1 const char *tmpdir; 4410badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 4420badc1eeSaurel32 if (!tmpdir) 4430badc1eeSaurel32 tmpdir = "/tmp"; 444eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 445eba25057SJim Meyering return -EOVERFLOW; 446ea2384d3Sbellard } 447eba25057SJim Meyering fd = mkstemp(filename); 448fe235a06SDunrong Huang if (fd < 0) { 449fe235a06SDunrong Huang return -errno; 450fe235a06SDunrong Huang } 451fe235a06SDunrong Huang if (close(fd) != 0) { 452fe235a06SDunrong Huang unlink(filename); 453eba25057SJim Meyering return -errno; 454eba25057SJim Meyering } 455eba25057SJim Meyering return 0; 456d5249393Sbellard #endif 457eba25057SJim Meyering } 458ea2384d3Sbellard 459f3a5d3f8SChristoph Hellwig /* 460f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 461f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 462f3a5d3f8SChristoph Hellwig */ 463f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 464f3a5d3f8SChristoph Hellwig { 465508c7cb3SChristoph Hellwig int score_max = 0, score; 466508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 467f3a5d3f8SChristoph Hellwig 4688a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 469508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 470508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 471508c7cb3SChristoph Hellwig if (score > score_max) { 472508c7cb3SChristoph Hellwig score_max = score; 473508c7cb3SChristoph Hellwig drv = d; 474f3a5d3f8SChristoph Hellwig } 475508c7cb3SChristoph Hellwig } 476f3a5d3f8SChristoph Hellwig } 477f3a5d3f8SChristoph Hellwig 478508c7cb3SChristoph Hellwig return drv; 479f3a5d3f8SChristoph Hellwig } 480f3a5d3f8SChristoph Hellwig 481b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 48284a12e66SChristoph Hellwig { 48384a12e66SChristoph Hellwig BlockDriver *drv1; 48484a12e66SChristoph Hellwig char protocol[128]; 48584a12e66SChristoph Hellwig int len; 48684a12e66SChristoph Hellwig const char *p; 48784a12e66SChristoph Hellwig 48866f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 48966f82ceeSKevin Wolf 49039508e7aSChristoph Hellwig /* 49139508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 49239508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 49339508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 49439508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 49539508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 49639508e7aSChristoph Hellwig */ 49784a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 49839508e7aSChristoph Hellwig if (drv1) { 49984a12e66SChristoph Hellwig return drv1; 50084a12e66SChristoph Hellwig } 50139508e7aSChristoph Hellwig 5029e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 50339508e7aSChristoph Hellwig return bdrv_find_format("file"); 50439508e7aSChristoph Hellwig } 5059e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 5069e0b22f4SStefan Hajnoczi assert(p != NULL); 50784a12e66SChristoph Hellwig len = p - filename; 50884a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 50984a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 51084a12e66SChristoph Hellwig memcpy(protocol, filename, len); 51184a12e66SChristoph Hellwig protocol[len] = '\0'; 51284a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 51384a12e66SChristoph Hellwig if (drv1->protocol_name && 51484a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 51584a12e66SChristoph Hellwig return drv1; 51684a12e66SChristoph Hellwig } 51784a12e66SChristoph Hellwig } 51884a12e66SChristoph Hellwig return NULL; 51984a12e66SChristoph Hellwig } 52084a12e66SChristoph Hellwig 521c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv) 522ea2384d3Sbellard { 52383f64091Sbellard int ret, score, score_max; 524ea2384d3Sbellard BlockDriver *drv1, *drv; 52583f64091Sbellard uint8_t buf[2048]; 52683f64091Sbellard BlockDriverState *bs; 527ea2384d3Sbellard 528f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 529c98ac35dSStefan Weil if (ret < 0) { 530c98ac35dSStefan Weil *pdrv = NULL; 531c98ac35dSStefan Weil return ret; 532c98ac35dSStefan Weil } 533f8ea0b00SNicholas Bellinger 53408a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 53508a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 5361a396859SNicholas A. Bellinger bdrv_delete(bs); 537c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 538c98ac35dSStefan Weil if (!drv) { 539c98ac35dSStefan Weil ret = -ENOENT; 540c98ac35dSStefan Weil } 541c98ac35dSStefan Weil *pdrv = drv; 542c98ac35dSStefan Weil return ret; 5431a396859SNicholas A. Bellinger } 544f8ea0b00SNicholas Bellinger 54583f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 54683f64091Sbellard bdrv_delete(bs); 547ea2384d3Sbellard if (ret < 0) { 548c98ac35dSStefan Weil *pdrv = NULL; 549c98ac35dSStefan Weil return ret; 550ea2384d3Sbellard } 551ea2384d3Sbellard 552ea2384d3Sbellard score_max = 0; 55384a12e66SChristoph Hellwig drv = NULL; 5548a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 55583f64091Sbellard if (drv1->bdrv_probe) { 556ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 557ea2384d3Sbellard if (score > score_max) { 558ea2384d3Sbellard score_max = score; 559ea2384d3Sbellard drv = drv1; 560ea2384d3Sbellard } 561ea2384d3Sbellard } 56283f64091Sbellard } 563c98ac35dSStefan Weil if (!drv) { 564c98ac35dSStefan Weil ret = -ENOENT; 565c98ac35dSStefan Weil } 566c98ac35dSStefan Weil *pdrv = drv; 567c98ac35dSStefan Weil return ret; 568ea2384d3Sbellard } 569ea2384d3Sbellard 57051762288SStefan Hajnoczi /** 57151762288SStefan Hajnoczi * Set the current 'total_sectors' value 57251762288SStefan Hajnoczi */ 57351762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 57451762288SStefan Hajnoczi { 57551762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 57651762288SStefan Hajnoczi 577396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 578396759adSNicholas Bellinger if (bs->sg) 579396759adSNicholas Bellinger return 0; 580396759adSNicholas Bellinger 58151762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 58251762288SStefan Hajnoczi if (drv->bdrv_getlength) { 58351762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 58451762288SStefan Hajnoczi if (length < 0) { 58551762288SStefan Hajnoczi return length; 58651762288SStefan Hajnoczi } 58751762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 58851762288SStefan Hajnoczi } 58951762288SStefan Hajnoczi 59051762288SStefan Hajnoczi bs->total_sectors = hint; 59151762288SStefan Hajnoczi return 0; 59251762288SStefan Hajnoczi } 59351762288SStefan Hajnoczi 594c3993cdcSStefan Hajnoczi /** 595c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 596c3993cdcSStefan Hajnoczi * 597c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 598c3993cdcSStefan Hajnoczi */ 599c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 600c3993cdcSStefan Hajnoczi { 601c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 602c3993cdcSStefan Hajnoczi 603c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 604c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 60592196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 60692196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 607c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 608c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 609c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 610c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 611c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 612c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 613c3993cdcSStefan Hajnoczi /* this is the default */ 614c3993cdcSStefan Hajnoczi } else { 615c3993cdcSStefan Hajnoczi return -1; 616c3993cdcSStefan Hajnoczi } 617c3993cdcSStefan Hajnoczi 618c3993cdcSStefan Hajnoczi return 0; 619c3993cdcSStefan Hajnoczi } 620c3993cdcSStefan Hajnoczi 62153fec9d3SStefan Hajnoczi /** 62253fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 62353fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 62453fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 62553fec9d3SStefan Hajnoczi */ 62653fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 62753fec9d3SStefan Hajnoczi { 62853fec9d3SStefan Hajnoczi bs->copy_on_read++; 62953fec9d3SStefan Hajnoczi } 63053fec9d3SStefan Hajnoczi 63153fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 63253fec9d3SStefan Hajnoczi { 63353fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 63453fec9d3SStefan Hajnoczi bs->copy_on_read--; 63553fec9d3SStefan Hajnoczi } 63653fec9d3SStefan Hajnoczi 637b6ce07aaSKevin Wolf /* 63857915332SKevin Wolf * Common part for opening disk images and files 63957915332SKevin Wolf */ 64057915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 64157915332SKevin Wolf int flags, BlockDriver *drv) 64257915332SKevin Wolf { 64357915332SKevin Wolf int ret, open_flags; 64457915332SKevin Wolf 64557915332SKevin Wolf assert(drv != NULL); 6466405875cSPaolo Bonzini assert(bs->file == NULL); 64757915332SKevin Wolf 64828dcee10SStefan Hajnoczi trace_bdrv_open_common(bs, filename, flags, drv->format_name); 64928dcee10SStefan Hajnoczi 65057915332SKevin Wolf bs->open_flags = flags; 65157915332SKevin Wolf bs->buffer_alignment = 512; 65257915332SKevin Wolf 65353fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 65453fec9d3SStefan Hajnoczi if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) { 65553fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 65653fec9d3SStefan Hajnoczi } 65753fec9d3SStefan Hajnoczi 65857915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 65957915332SKevin Wolf 66057915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 66157915332SKevin Wolf return -ENOTSUP; 66257915332SKevin Wolf } 66357915332SKevin Wolf 66457915332SKevin Wolf bs->drv = drv; 6657267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 66657915332SKevin Wolf 66703f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 668e1e9b0acSPaolo Bonzini open_flags = flags | BDRV_O_CACHE_WB; 66957915332SKevin Wolf 67057915332SKevin Wolf /* 67157915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 67257915332SKevin Wolf * image. 67357915332SKevin Wolf */ 674e1e9b0acSPaolo Bonzini open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 67557915332SKevin Wolf 67657915332SKevin Wolf /* 677ebabb67aSStefan Weil * Snapshots should be writable. 67857915332SKevin Wolf */ 67957915332SKevin Wolf if (bs->is_temporary) { 68057915332SKevin Wolf open_flags |= BDRV_O_RDWR; 68157915332SKevin Wolf } 68257915332SKevin Wolf 683be028adcSJeff Cody bs->read_only = !(open_flags & BDRV_O_RDWR); 684e7c63796SStefan Hajnoczi 68566f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 68666f82ceeSKevin Wolf if (drv->bdrv_file_open) { 68766f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 68866f82ceeSKevin Wolf } else { 68966f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 69066f82ceeSKevin Wolf if (ret >= 0) { 69166f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 69266f82ceeSKevin Wolf } 69366f82ceeSKevin Wolf } 69466f82ceeSKevin Wolf 69557915332SKevin Wolf if (ret < 0) { 69657915332SKevin Wolf goto free_and_fail; 69757915332SKevin Wolf } 69857915332SKevin Wolf 69951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 70051762288SStefan Hajnoczi if (ret < 0) { 70151762288SStefan Hajnoczi goto free_and_fail; 70257915332SKevin Wolf } 70351762288SStefan Hajnoczi 70457915332SKevin Wolf #ifndef _WIN32 70557915332SKevin Wolf if (bs->is_temporary) { 70657915332SKevin Wolf unlink(filename); 70757915332SKevin Wolf } 70857915332SKevin Wolf #endif 70957915332SKevin Wolf return 0; 71057915332SKevin Wolf 71157915332SKevin Wolf free_and_fail: 71266f82ceeSKevin Wolf if (bs->file) { 71366f82ceeSKevin Wolf bdrv_delete(bs->file); 71466f82ceeSKevin Wolf bs->file = NULL; 71566f82ceeSKevin Wolf } 7167267c094SAnthony Liguori g_free(bs->opaque); 71757915332SKevin Wolf bs->opaque = NULL; 71857915332SKevin Wolf bs->drv = NULL; 71957915332SKevin Wolf return ret; 72057915332SKevin Wolf } 72157915332SKevin Wolf 72257915332SKevin Wolf /* 723b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 724b6ce07aaSKevin Wolf */ 72583f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 726b338082bSbellard { 72783f64091Sbellard BlockDriverState *bs; 7286db95603SChristoph Hellwig BlockDriver *drv; 72983f64091Sbellard int ret; 7303b0d4f61Sbellard 731b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 7326db95603SChristoph Hellwig if (!drv) { 7336db95603SChristoph Hellwig return -ENOENT; 7346db95603SChristoph Hellwig } 7356db95603SChristoph Hellwig 73683f64091Sbellard bs = bdrv_new(""); 737b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 73883f64091Sbellard if (ret < 0) { 73983f64091Sbellard bdrv_delete(bs); 74083f64091Sbellard return ret; 7413b0d4f61Sbellard } 74271d0770cSaliguori bs->growable = 1; 74383f64091Sbellard *pbs = bs; 74483f64091Sbellard return 0; 7453b0d4f61Sbellard } 7463b0d4f61Sbellard 7479156df12SPaolo Bonzini int bdrv_open_backing_file(BlockDriverState *bs) 7489156df12SPaolo Bonzini { 7499156df12SPaolo Bonzini char backing_filename[PATH_MAX]; 7509156df12SPaolo Bonzini int back_flags, ret; 7519156df12SPaolo Bonzini BlockDriver *back_drv = NULL; 7529156df12SPaolo Bonzini 7539156df12SPaolo Bonzini if (bs->backing_hd != NULL) { 7549156df12SPaolo Bonzini return 0; 7559156df12SPaolo Bonzini } 7569156df12SPaolo Bonzini 7579156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 7589156df12SPaolo Bonzini if (bs->backing_file[0] == '\0') { 7599156df12SPaolo Bonzini return 0; 7609156df12SPaolo Bonzini } 7619156df12SPaolo Bonzini 7629156df12SPaolo Bonzini bs->backing_hd = bdrv_new(""); 7639156df12SPaolo Bonzini bdrv_get_full_backing_filename(bs, backing_filename, 7649156df12SPaolo Bonzini sizeof(backing_filename)); 7659156df12SPaolo Bonzini 7669156df12SPaolo Bonzini if (bs->backing_format[0] != '\0') { 7679156df12SPaolo Bonzini back_drv = bdrv_find_format(bs->backing_format); 7689156df12SPaolo Bonzini } 7699156df12SPaolo Bonzini 7709156df12SPaolo Bonzini /* backing files always opened read-only */ 7719156df12SPaolo Bonzini back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT); 7729156df12SPaolo Bonzini 7739156df12SPaolo Bonzini ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 7749156df12SPaolo Bonzini if (ret < 0) { 7759156df12SPaolo Bonzini bdrv_delete(bs->backing_hd); 7769156df12SPaolo Bonzini bs->backing_hd = NULL; 7779156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 7789156df12SPaolo Bonzini return ret; 7799156df12SPaolo Bonzini } 7809156df12SPaolo Bonzini return 0; 7819156df12SPaolo Bonzini } 7829156df12SPaolo Bonzini 783b6ce07aaSKevin Wolf /* 784b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 785b6ce07aaSKevin Wolf */ 786d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 787ea2384d3Sbellard BlockDriver *drv) 788ea2384d3Sbellard { 789b6ce07aaSKevin Wolf int ret; 7902b572816SKevin Wolf char tmp_filename[PATH_MAX]; 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 850b6ce07aaSKevin Wolf /* Find the right image format driver */ 8516db95603SChristoph Hellwig if (!drv) { 852c98ac35dSStefan Weil ret = find_image_format(filename, &drv); 853ea2384d3Sbellard } 8546987307cSChristoph Hellwig 85551d7c00cSaliguori if (!drv) { 85651d7c00cSaliguori goto unlink_and_fail; 85783f64091Sbellard } 858b6ce07aaSKevin Wolf 859be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 860be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 861be028adcSJeff Cody } 862be028adcSJeff Cody 863b6ce07aaSKevin Wolf /* Open the image */ 864b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 865b6ce07aaSKevin Wolf if (ret < 0) { 8666987307cSChristoph Hellwig goto unlink_and_fail; 8676987307cSChristoph Hellwig } 8686987307cSChristoph Hellwig 869b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 8709156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 8719156df12SPaolo Bonzini ret = bdrv_open_backing_file(bs); 872b6ce07aaSKevin Wolf if (ret < 0) { 873b6ce07aaSKevin Wolf bdrv_close(bs); 874b6ce07aaSKevin Wolf return ret; 875b6ce07aaSKevin Wolf } 876b6ce07aaSKevin Wolf } 877b6ce07aaSKevin Wolf 878b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 8797d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 880b6ce07aaSKevin Wolf } 881b6ce07aaSKevin Wolf 88298f90dbaSZhi Yong Wu /* throttling disk I/O limits */ 88398f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 88498f90dbaSZhi Yong Wu bdrv_io_limits_enable(bs); 88598f90dbaSZhi Yong Wu } 88698f90dbaSZhi Yong Wu 887b6ce07aaSKevin Wolf return 0; 888b6ce07aaSKevin Wolf 889b6ce07aaSKevin Wolf unlink_and_fail: 890b6ce07aaSKevin Wolf if (bs->is_temporary) { 891b6ce07aaSKevin Wolf unlink(filename); 892b6ce07aaSKevin Wolf } 893b6ce07aaSKevin Wolf return ret; 894b6ce07aaSKevin Wolf } 895b6ce07aaSKevin Wolf 896e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 897e971aa12SJeff Cody bool prepared; 898e971aa12SJeff Cody BDRVReopenState state; 899e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 900e971aa12SJeff Cody } BlockReopenQueueEntry; 901e971aa12SJeff Cody 902e971aa12SJeff Cody /* 903e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 904e971aa12SJeff Cody * reopen of multiple devices. 905e971aa12SJeff Cody * 906e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 907e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 908e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 909e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 910e971aa12SJeff Cody * atomic 'set'. 911e971aa12SJeff Cody * 912e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 913e971aa12SJeff Cody * 914e971aa12SJeff Cody * flags contains the open flags for the associated bs 915e971aa12SJeff Cody * 916e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 917e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 918e971aa12SJeff Cody * 919e971aa12SJeff Cody */ 920e971aa12SJeff Cody BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 921e971aa12SJeff Cody BlockDriverState *bs, int flags) 922e971aa12SJeff Cody { 923e971aa12SJeff Cody assert(bs != NULL); 924e971aa12SJeff Cody 925e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 926e971aa12SJeff Cody if (bs_queue == NULL) { 927e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 928e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 929e971aa12SJeff Cody } 930e971aa12SJeff Cody 931e971aa12SJeff Cody if (bs->file) { 932e971aa12SJeff Cody bdrv_reopen_queue(bs_queue, bs->file, flags); 933e971aa12SJeff Cody } 934e971aa12SJeff Cody 935e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 936e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 937e971aa12SJeff Cody 938e971aa12SJeff Cody bs_entry->state.bs = bs; 939e971aa12SJeff Cody bs_entry->state.flags = flags; 940e971aa12SJeff Cody 941e971aa12SJeff Cody return bs_queue; 942e971aa12SJeff Cody } 943e971aa12SJeff Cody 944e971aa12SJeff Cody /* 945e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 946e971aa12SJeff Cody * 947e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 948e971aa12SJeff Cody * via bdrv_reopen_queue(). 949e971aa12SJeff Cody * 950e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 951e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 952e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 953e971aa12SJeff Cody * data cleaned up. 954e971aa12SJeff Cody * 955e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 956e971aa12SJeff Cody * to all devices. 957e971aa12SJeff Cody * 958e971aa12SJeff Cody */ 959e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 960e971aa12SJeff Cody { 961e971aa12SJeff Cody int ret = -1; 962e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 963e971aa12SJeff Cody Error *local_err = NULL; 964e971aa12SJeff Cody 965e971aa12SJeff Cody assert(bs_queue != NULL); 966e971aa12SJeff Cody 967e971aa12SJeff Cody bdrv_drain_all(); 968e971aa12SJeff Cody 969e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 970e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 971e971aa12SJeff Cody error_propagate(errp, local_err); 972e971aa12SJeff Cody goto cleanup; 973e971aa12SJeff Cody } 974e971aa12SJeff Cody bs_entry->prepared = true; 975e971aa12SJeff Cody } 976e971aa12SJeff Cody 977e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 978e971aa12SJeff Cody * changes 979e971aa12SJeff Cody */ 980e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 981e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 982e971aa12SJeff Cody } 983e971aa12SJeff Cody 984e971aa12SJeff Cody ret = 0; 985e971aa12SJeff Cody 986e971aa12SJeff Cody cleanup: 987e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 988e971aa12SJeff Cody if (ret && bs_entry->prepared) { 989e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 990e971aa12SJeff Cody } 991e971aa12SJeff Cody g_free(bs_entry); 992e971aa12SJeff Cody } 993e971aa12SJeff Cody g_free(bs_queue); 994e971aa12SJeff Cody return ret; 995e971aa12SJeff Cody } 996e971aa12SJeff Cody 997e971aa12SJeff Cody 998e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 999e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1000e971aa12SJeff Cody { 1001e971aa12SJeff Cody int ret = -1; 1002e971aa12SJeff Cody Error *local_err = NULL; 1003e971aa12SJeff Cody BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); 1004e971aa12SJeff Cody 1005e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1006e971aa12SJeff Cody if (local_err != NULL) { 1007e971aa12SJeff Cody error_propagate(errp, local_err); 1008e971aa12SJeff Cody } 1009e971aa12SJeff Cody return ret; 1010e971aa12SJeff Cody } 1011e971aa12SJeff Cody 1012e971aa12SJeff Cody 1013e971aa12SJeff Cody /* 1014e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1015e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1016e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1017e971aa12SJeff Cody * 1018e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1019e971aa12SJeff Cody * flags are the new open flags 1020e971aa12SJeff Cody * queue is the reopen queue 1021e971aa12SJeff Cody * 1022e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1023e971aa12SJeff Cody * as well. 1024e971aa12SJeff Cody * 1025e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1026e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1027e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1028e971aa12SJeff Cody * 1029e971aa12SJeff Cody */ 1030e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1031e971aa12SJeff Cody Error **errp) 1032e971aa12SJeff Cody { 1033e971aa12SJeff Cody int ret = -1; 1034e971aa12SJeff Cody Error *local_err = NULL; 1035e971aa12SJeff Cody BlockDriver *drv; 1036e971aa12SJeff Cody 1037e971aa12SJeff Cody assert(reopen_state != NULL); 1038e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1039e971aa12SJeff Cody drv = reopen_state->bs->drv; 1040e971aa12SJeff Cody 1041e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 1042e971aa12SJeff Cody * to r/w */ 1043e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 1044e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 1045e971aa12SJeff Cody error_set(errp, QERR_DEVICE_IS_READ_ONLY, 1046e971aa12SJeff Cody reopen_state->bs->device_name); 1047e971aa12SJeff Cody goto error; 1048e971aa12SJeff Cody } 1049e971aa12SJeff Cody 1050e971aa12SJeff Cody 1051e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 1052e971aa12SJeff Cody if (ret) { 1053e971aa12SJeff Cody error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", 1054e971aa12SJeff Cody strerror(-ret)); 1055e971aa12SJeff Cody goto error; 1056e971aa12SJeff Cody } 1057e971aa12SJeff Cody 1058e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 1059e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 1060e971aa12SJeff Cody if (ret) { 1061e971aa12SJeff Cody if (local_err != NULL) { 1062e971aa12SJeff Cody error_propagate(errp, local_err); 1063e971aa12SJeff Cody } else { 1064e971aa12SJeff Cody error_set(errp, QERR_OPEN_FILE_FAILED, 1065e971aa12SJeff Cody reopen_state->bs->filename); 1066e971aa12SJeff Cody } 1067e971aa12SJeff Cody goto error; 1068e971aa12SJeff Cody } 1069e971aa12SJeff Cody } else { 1070e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 1071e971aa12SJeff Cody * handler for each supported drv. */ 1072e971aa12SJeff Cody error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 1073e971aa12SJeff Cody drv->format_name, reopen_state->bs->device_name, 1074e971aa12SJeff Cody "reopening of file"); 1075e971aa12SJeff Cody ret = -1; 1076e971aa12SJeff Cody goto error; 1077e971aa12SJeff Cody } 1078e971aa12SJeff Cody 1079e971aa12SJeff Cody ret = 0; 1080e971aa12SJeff Cody 1081e971aa12SJeff Cody error: 1082e971aa12SJeff Cody return ret; 1083e971aa12SJeff Cody } 1084e971aa12SJeff Cody 1085e971aa12SJeff Cody /* 1086e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 1087e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 1088e971aa12SJeff Cody * the active BlockDriverState contents. 1089e971aa12SJeff Cody */ 1090e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 1091e971aa12SJeff Cody { 1092e971aa12SJeff Cody BlockDriver *drv; 1093e971aa12SJeff Cody 1094e971aa12SJeff Cody assert(reopen_state != NULL); 1095e971aa12SJeff Cody drv = reopen_state->bs->drv; 1096e971aa12SJeff Cody assert(drv != NULL); 1097e971aa12SJeff Cody 1098e971aa12SJeff Cody /* If there are any driver level actions to take */ 1099e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 1100e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 1101e971aa12SJeff Cody } 1102e971aa12SJeff Cody 1103e971aa12SJeff Cody /* set BDS specific flags now */ 1104e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 1105e971aa12SJeff Cody reopen_state->bs->enable_write_cache = !!(reopen_state->flags & 1106e971aa12SJeff Cody BDRV_O_CACHE_WB); 1107e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 1108e971aa12SJeff Cody } 1109e971aa12SJeff Cody 1110e971aa12SJeff Cody /* 1111e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 1112e971aa12SJeff Cody * reopen_state 1113e971aa12SJeff Cody */ 1114e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 1115e971aa12SJeff Cody { 1116e971aa12SJeff Cody BlockDriver *drv; 1117e971aa12SJeff Cody 1118e971aa12SJeff Cody assert(reopen_state != NULL); 1119e971aa12SJeff Cody drv = reopen_state->bs->drv; 1120e971aa12SJeff Cody assert(drv != NULL); 1121e971aa12SJeff Cody 1122e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 1123e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 1124e971aa12SJeff Cody } 1125e971aa12SJeff Cody } 1126e971aa12SJeff Cody 1127e971aa12SJeff Cody 1128fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 1129fc01f7e7Sbellard { 113080ccf93bSLiu Yuan bdrv_flush(bs); 11313e914655SPaolo Bonzini if (bs->job) { 11323e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 11333e914655SPaolo Bonzini } 11347094f12fSKevin Wolf bdrv_drain_all(); 1135d7d512f6SPaolo Bonzini notifier_list_notify(&bs->close_notifiers, bs); 11367094f12fSKevin Wolf 11373cbc002cSPaolo Bonzini if (bs->drv) { 1138f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 1139f9092b10SMarkus Armbruster bs_snapshots = NULL; 1140f9092b10SMarkus Armbruster } 1141557df6acSStefan Hajnoczi if (bs->backing_hd) { 1142ea2384d3Sbellard bdrv_delete(bs->backing_hd); 1143557df6acSStefan Hajnoczi bs->backing_hd = NULL; 1144557df6acSStefan Hajnoczi } 1145ea2384d3Sbellard bs->drv->bdrv_close(bs); 11467267c094SAnthony Liguori g_free(bs->opaque); 1147ea2384d3Sbellard #ifdef _WIN32 1148ea2384d3Sbellard if (bs->is_temporary) { 1149ea2384d3Sbellard unlink(bs->filename); 1150ea2384d3Sbellard } 115167b915a5Sbellard #endif 1152ea2384d3Sbellard bs->opaque = NULL; 1153ea2384d3Sbellard bs->drv = NULL; 115453fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 1155a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 1156a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 11576405875cSPaolo Bonzini bs->total_sectors = 0; 11586405875cSPaolo Bonzini bs->encrypted = 0; 11596405875cSPaolo Bonzini bs->valid_key = 0; 11606405875cSPaolo Bonzini bs->sg = 0; 11616405875cSPaolo Bonzini bs->growable = 0; 1162b338082bSbellard 116366f82ceeSKevin Wolf if (bs->file != NULL) { 11640ac9377dSPaolo Bonzini bdrv_delete(bs->file); 11650ac9377dSPaolo Bonzini bs->file = NULL; 116666f82ceeSKevin Wolf } 11679ca11154SPavel Hrdina } 116866f82ceeSKevin Wolf 11697d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 117098f90dbaSZhi Yong Wu 117198f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 117298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 117398f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 117498f90dbaSZhi Yong Wu } 1175b338082bSbellard } 1176b338082bSbellard 11772bc93fedSMORITA Kazutaka void bdrv_close_all(void) 11782bc93fedSMORITA Kazutaka { 11792bc93fedSMORITA Kazutaka BlockDriverState *bs; 11802bc93fedSMORITA Kazutaka 11812bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 11822bc93fedSMORITA Kazutaka bdrv_close(bs); 11832bc93fedSMORITA Kazutaka } 11842bc93fedSMORITA Kazutaka } 11852bc93fedSMORITA Kazutaka 1186922453bcSStefan Hajnoczi /* 1187922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 1188922453bcSStefan Hajnoczi * 1189922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 1190922453bcSStefan Hajnoczi * after calling this function. 11914c355d53SZhi Yong Wu * 11924c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 11934c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 11944c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 11954c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 11964c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 1197922453bcSStefan Hajnoczi */ 1198922453bcSStefan Hajnoczi void bdrv_drain_all(void) 1199922453bcSStefan Hajnoczi { 1200922453bcSStefan Hajnoczi BlockDriverState *bs; 12014c355d53SZhi Yong Wu bool busy; 1202922453bcSStefan Hajnoczi 12034c355d53SZhi Yong Wu do { 12044c355d53SZhi Yong Wu busy = qemu_aio_wait(); 12054c355d53SZhi Yong Wu 12064c355d53SZhi Yong Wu /* FIXME: We do not have timer support here, so this is effectively 12074c355d53SZhi Yong Wu * a busy wait. 12084c355d53SZhi Yong Wu */ 12094c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 12104c355d53SZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 12114c355d53SZhi Yong Wu qemu_co_queue_restart_all(&bs->throttled_reqs); 12124c355d53SZhi Yong Wu busy = true; 12134c355d53SZhi Yong Wu } 12144c355d53SZhi Yong Wu } 12154c355d53SZhi Yong Wu } while (busy); 1216922453bcSStefan Hajnoczi 1217922453bcSStefan Hajnoczi /* If requests are still pending there is a bug somewhere */ 1218922453bcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1219922453bcSStefan Hajnoczi assert(QLIST_EMPTY(&bs->tracked_requests)); 1220922453bcSStefan Hajnoczi assert(qemu_co_queue_empty(&bs->throttled_reqs)); 1221922453bcSStefan Hajnoczi } 1222922453bcSStefan Hajnoczi } 1223922453bcSStefan Hajnoczi 1224d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 1225d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1226d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1227d22b2f41SRyan Harper { 1228d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 1229d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 1230d22b2f41SRyan Harper } 1231d22b2f41SRyan Harper bs->device_name[0] = '\0'; 1232d22b2f41SRyan Harper } 1233d22b2f41SRyan Harper 1234e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1235e023b2e2SPaolo Bonzini { 1236e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1237e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1238e023b2e2SPaolo Bonzini } 1239e023b2e2SPaolo Bonzini } 1240e023b2e2SPaolo Bonzini 12414ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 12424ddc07caSPaolo Bonzini BlockDriverState *bs_src) 12434ddc07caSPaolo Bonzini { 12444ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 12454ddc07caSPaolo Bonzini bs_dest->open_flags = bs_src->open_flags; 12464ddc07caSPaolo Bonzini 12474ddc07caSPaolo Bonzini /* dev info */ 12484ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 12494ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 12504ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 12514ddc07caSPaolo Bonzini bs_dest->buffer_alignment = bs_src->buffer_alignment; 12524ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 12534ddc07caSPaolo Bonzini 12544ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 12554ddc07caSPaolo Bonzini 12564ddc07caSPaolo Bonzini /* i/o timing parameters */ 12574ddc07caSPaolo Bonzini bs_dest->slice_time = bs_src->slice_time; 12584ddc07caSPaolo Bonzini bs_dest->slice_start = bs_src->slice_start; 12594ddc07caSPaolo Bonzini bs_dest->slice_end = bs_src->slice_end; 12604ddc07caSPaolo Bonzini bs_dest->io_limits = bs_src->io_limits; 12614ddc07caSPaolo Bonzini bs_dest->io_base = bs_src->io_base; 12624ddc07caSPaolo Bonzini bs_dest->throttled_reqs = bs_src->throttled_reqs; 12634ddc07caSPaolo Bonzini bs_dest->block_timer = bs_src->block_timer; 12644ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 12654ddc07caSPaolo Bonzini 12664ddc07caSPaolo Bonzini /* r/w error */ 12674ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 12684ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 12694ddc07caSPaolo Bonzini 12704ddc07caSPaolo Bonzini /* i/o status */ 12714ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 12724ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 12734ddc07caSPaolo Bonzini 12744ddc07caSPaolo Bonzini /* dirty bitmap */ 12754ddc07caSPaolo Bonzini bs_dest->dirty_count = bs_src->dirty_count; 12764ddc07caSPaolo Bonzini bs_dest->dirty_bitmap = bs_src->dirty_bitmap; 12774ddc07caSPaolo Bonzini 12784ddc07caSPaolo Bonzini /* job */ 12794ddc07caSPaolo Bonzini bs_dest->in_use = bs_src->in_use; 12804ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 12814ddc07caSPaolo Bonzini 12824ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 12834ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 12844ddc07caSPaolo Bonzini bs_src->device_name); 12854ddc07caSPaolo Bonzini bs_dest->list = bs_src->list; 12864ddc07caSPaolo Bonzini } 12874ddc07caSPaolo Bonzini 12884ddc07caSPaolo Bonzini /* 12894ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 12904ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 12914ddc07caSPaolo Bonzini * actually attached to a device. 12924ddc07caSPaolo Bonzini * 12934ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 12944ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 12954ddc07caSPaolo Bonzini * 12964ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 12974ddc07caSPaolo Bonzini * 12984ddc07caSPaolo Bonzini * This function does not create any image files. 12994ddc07caSPaolo Bonzini */ 13004ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 13014ddc07caSPaolo Bonzini { 13024ddc07caSPaolo Bonzini BlockDriverState tmp; 13034ddc07caSPaolo Bonzini 13044ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 13054ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 13064ddc07caSPaolo Bonzini assert(bs_new->dirty_bitmap == NULL); 13074ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 13084ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 13094ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 13104ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 13114ddc07caSPaolo Bonzini assert(bs_new->block_timer == NULL); 13124ddc07caSPaolo Bonzini 13134ddc07caSPaolo Bonzini tmp = *bs_new; 13144ddc07caSPaolo Bonzini *bs_new = *bs_old; 13154ddc07caSPaolo Bonzini *bs_old = tmp; 13164ddc07caSPaolo Bonzini 13174ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 13184ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 13194ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 13204ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 13214ddc07caSPaolo Bonzini 13224ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 13234ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 13244ddc07caSPaolo Bonzini 13254ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 13264ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 13274ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 13284ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 13294ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 13304ddc07caSPaolo Bonzini assert(bs_new->block_timer == NULL); 13314ddc07caSPaolo Bonzini 13324ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 13334ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 13344ddc07caSPaolo Bonzini } 13354ddc07caSPaolo Bonzini 13368802d1fdSJeff Cody /* 13378802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 13388802d1fdSJeff Cody * live, while keeping required fields on the top layer. 13398802d1fdSJeff Cody * 13408802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 13418802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 13428802d1fdSJeff Cody * 1343f6801b83SJeff Cody * bs_new is required to be anonymous. 1344f6801b83SJeff Cody * 13458802d1fdSJeff Cody * This function does not create any image files. 13468802d1fdSJeff Cody */ 13478802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 13488802d1fdSJeff Cody { 13494ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 13508802d1fdSJeff Cody 13518802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 13528802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 13534ddc07caSPaolo Bonzini bs_top->backing_hd = bs_new; 13544ddc07caSPaolo Bonzini bs_top->open_flags &= ~BDRV_O_NO_BACKING; 13554ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 13564ddc07caSPaolo Bonzini bs_new->filename); 13574ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 13584ddc07caSPaolo Bonzini bs_new->drv ? bs_new->drv->format_name : ""); 13598802d1fdSJeff Cody } 13608802d1fdSJeff Cody 1361b338082bSbellard void bdrv_delete(BlockDriverState *bs) 1362b338082bSbellard { 1363fa879d62SMarkus Armbruster assert(!bs->dev); 13643e914655SPaolo Bonzini assert(!bs->job); 13653e914655SPaolo Bonzini assert(!bs->in_use); 136618846deeSMarkus Armbruster 13671b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1368d22b2f41SRyan Harper bdrv_make_anon(bs); 136934c6f050Saurel32 1370b338082bSbellard bdrv_close(bs); 137166f82ceeSKevin Wolf 1372f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 13737267c094SAnthony Liguori g_free(bs); 1374fc01f7e7Sbellard } 1375fc01f7e7Sbellard 1376fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1377fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 137818846deeSMarkus Armbruster { 1379fa879d62SMarkus Armbruster if (bs->dev) { 138018846deeSMarkus Armbruster return -EBUSY; 138118846deeSMarkus Armbruster } 1382fa879d62SMarkus Armbruster bs->dev = dev; 138328a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 138418846deeSMarkus Armbruster return 0; 138518846deeSMarkus Armbruster } 138618846deeSMarkus Armbruster 1387fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1388fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 138918846deeSMarkus Armbruster { 1390fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1391fa879d62SMarkus Armbruster abort(); 1392fa879d62SMarkus Armbruster } 1393fa879d62SMarkus Armbruster } 1394fa879d62SMarkus Armbruster 1395fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1396fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1397fa879d62SMarkus Armbruster { 1398fa879d62SMarkus Armbruster assert(bs->dev == dev); 1399fa879d62SMarkus Armbruster bs->dev = NULL; 14000e49de52SMarkus Armbruster bs->dev_ops = NULL; 14010e49de52SMarkus Armbruster bs->dev_opaque = NULL; 140229e05f20SMarkus Armbruster bs->buffer_alignment = 512; 140318846deeSMarkus Armbruster } 140418846deeSMarkus Armbruster 1405fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1406fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 140718846deeSMarkus Armbruster { 1408fa879d62SMarkus Armbruster return bs->dev; 140918846deeSMarkus Armbruster } 141018846deeSMarkus Armbruster 14110e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 14120e49de52SMarkus Armbruster void *opaque) 14130e49de52SMarkus Armbruster { 14140e49de52SMarkus Armbruster bs->dev_ops = ops; 14150e49de52SMarkus Armbruster bs->dev_opaque = opaque; 14162c6942faSMarkus Armbruster if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) { 14172c6942faSMarkus Armbruster bs_snapshots = NULL; 14182c6942faSMarkus Armbruster } 14190e49de52SMarkus Armbruster } 14200e49de52SMarkus Armbruster 142132c81a4aSPaolo Bonzini void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 142232c81a4aSPaolo Bonzini enum MonitorEvent ev, 14231ceee0d5SPaolo Bonzini BlockErrorAction action, bool is_read) 1424329c0a48SLuiz Capitulino { 1425329c0a48SLuiz Capitulino QObject *data; 1426329c0a48SLuiz Capitulino const char *action_str; 1427329c0a48SLuiz Capitulino 1428329c0a48SLuiz Capitulino switch (action) { 1429329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1430329c0a48SLuiz Capitulino action_str = "report"; 1431329c0a48SLuiz Capitulino break; 1432329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1433329c0a48SLuiz Capitulino action_str = "ignore"; 1434329c0a48SLuiz Capitulino break; 1435329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1436329c0a48SLuiz Capitulino action_str = "stop"; 1437329c0a48SLuiz Capitulino break; 1438329c0a48SLuiz Capitulino default: 1439329c0a48SLuiz Capitulino abort(); 1440329c0a48SLuiz Capitulino } 1441329c0a48SLuiz Capitulino 1442329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1443329c0a48SLuiz Capitulino bdrv->device_name, 1444329c0a48SLuiz Capitulino action_str, 1445329c0a48SLuiz Capitulino is_read ? "read" : "write"); 144632c81a4aSPaolo Bonzini monitor_protocol_event(ev, data); 1447329c0a48SLuiz Capitulino 1448329c0a48SLuiz Capitulino qobject_decref(data); 1449329c0a48SLuiz Capitulino } 1450329c0a48SLuiz Capitulino 14516f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 14526f382ed2SLuiz Capitulino { 14536f382ed2SLuiz Capitulino QObject *data; 14546f382ed2SLuiz Capitulino 14556f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 14566f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 14576f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 14586f382ed2SLuiz Capitulino 14596f382ed2SLuiz Capitulino qobject_decref(data); 14606f382ed2SLuiz Capitulino } 14616f382ed2SLuiz Capitulino 14627d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 14630e49de52SMarkus Armbruster { 1464145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 14656f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 14667d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 14676f382ed2SLuiz Capitulino if (tray_was_closed) { 14686f382ed2SLuiz Capitulino /* tray open */ 14696f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 14706f382ed2SLuiz Capitulino } 14716f382ed2SLuiz Capitulino if (load) { 14726f382ed2SLuiz Capitulino /* tray close */ 14736f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 14746f382ed2SLuiz Capitulino } 1475145feb17SMarkus Armbruster } 1476145feb17SMarkus Armbruster } 1477145feb17SMarkus Armbruster 14782c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 14792c6942faSMarkus Armbruster { 14802c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 14812c6942faSMarkus Armbruster } 14822c6942faSMarkus Armbruster 1483025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1484025ccaa7SPaolo Bonzini { 1485025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1486025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1487025ccaa7SPaolo Bonzini } 1488025ccaa7SPaolo Bonzini } 1489025ccaa7SPaolo Bonzini 1490e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1491e4def80bSMarkus Armbruster { 1492e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1493e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1494e4def80bSMarkus Armbruster } 1495e4def80bSMarkus Armbruster return false; 1496e4def80bSMarkus Armbruster } 1497e4def80bSMarkus Armbruster 1498145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1499145feb17SMarkus Armbruster { 1500145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1501145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 15020e49de52SMarkus Armbruster } 15030e49de52SMarkus Armbruster } 15040e49de52SMarkus Armbruster 1505f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1506f107639aSMarkus Armbruster { 1507f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1508f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1509f107639aSMarkus Armbruster } 1510f107639aSMarkus Armbruster return false; 1511f107639aSMarkus Armbruster } 1512f107639aSMarkus Armbruster 1513e97fc193Saliguori /* 1514e97fc193Saliguori * Run consistency checks on an image 1515e97fc193Saliguori * 1516e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1517a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1518e076f338SKevin Wolf * check are stored in res. 1519e97fc193Saliguori */ 15204534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 1521e97fc193Saliguori { 1522e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1523e97fc193Saliguori return -ENOTSUP; 1524e97fc193Saliguori } 1525e97fc193Saliguori 1526e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 15274534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 1528e97fc193Saliguori } 1529e97fc193Saliguori 15308a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 15318a426614SKevin Wolf 153233e3963eSbellard /* commit COW file into the raw image */ 153333e3963eSbellard int bdrv_commit(BlockDriverState *bs) 153433e3963eSbellard { 153519cb3738Sbellard BlockDriver *drv = bs->drv; 15368a426614SKevin Wolf int64_t sector, total_sectors; 15378a426614SKevin Wolf int n, ro, open_flags; 15380bce597dSJeff Cody int ret = 0; 15398a426614SKevin Wolf uint8_t *buf; 1540c2cba3d9SJim Meyering char filename[PATH_MAX]; 154133e3963eSbellard 154219cb3738Sbellard if (!drv) 154319cb3738Sbellard return -ENOMEDIUM; 154433e3963eSbellard 15454dca4b63SNaphtali Sprei if (!bs->backing_hd) { 15464dca4b63SNaphtali Sprei return -ENOTSUP; 15474dca4b63SNaphtali Sprei } 15484dca4b63SNaphtali Sprei 15492d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 15502d3735d3SStefan Hajnoczi return -EBUSY; 15512d3735d3SStefan Hajnoczi } 15522d3735d3SStefan Hajnoczi 15534dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 1554c2cba3d9SJim Meyering /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ 1555c2cba3d9SJim Meyering pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); 15564dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 15574dca4b63SNaphtali Sprei 15584dca4b63SNaphtali Sprei if (ro) { 15590bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 15600bce597dSJeff Cody return -EACCES; 15614dca4b63SNaphtali Sprei } 1562ea2384d3Sbellard } 1563ea2384d3Sbellard 15646ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 15657267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 15668a426614SKevin Wolf 15678a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 156805c4af54SStefan Hajnoczi if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 15698a426614SKevin Wolf 15708a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 15714dca4b63SNaphtali Sprei ret = -EIO; 15724dca4b63SNaphtali Sprei goto ro_cleanup; 157333e3963eSbellard } 157433e3963eSbellard 15758a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 15764dca4b63SNaphtali Sprei ret = -EIO; 15774dca4b63SNaphtali Sprei goto ro_cleanup; 157833e3963eSbellard } 157933e3963eSbellard } 158033e3963eSbellard } 158195389c86Sbellard 15821d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 15831d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 15841d44952fSChristoph Hellwig bdrv_flush(bs); 15851d44952fSChristoph Hellwig } 158695389c86Sbellard 15873f5075aeSChristoph Hellwig /* 15883f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 15893f5075aeSChristoph Hellwig * stable on disk. 15903f5075aeSChristoph Hellwig */ 15913f5075aeSChristoph Hellwig if (bs->backing_hd) 15923f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 15934dca4b63SNaphtali Sprei 15944dca4b63SNaphtali Sprei ro_cleanup: 15957267c094SAnthony Liguori g_free(buf); 15964dca4b63SNaphtali Sprei 15974dca4b63SNaphtali Sprei if (ro) { 15980bce597dSJeff Cody /* ignoring error return here */ 15990bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 16004dca4b63SNaphtali Sprei } 16014dca4b63SNaphtali Sprei 16021d44952fSChristoph Hellwig return ret; 160333e3963eSbellard } 160433e3963eSbellard 1605e8877497SStefan Hajnoczi int bdrv_commit_all(void) 16066ab4b5abSMarkus Armbruster { 16076ab4b5abSMarkus Armbruster BlockDriverState *bs; 16086ab4b5abSMarkus Armbruster 16096ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1610e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1611e8877497SStefan Hajnoczi if (ret < 0) { 1612e8877497SStefan Hajnoczi return ret; 16136ab4b5abSMarkus Armbruster } 16146ab4b5abSMarkus Armbruster } 1615e8877497SStefan Hajnoczi return 0; 1616e8877497SStefan Hajnoczi } 16176ab4b5abSMarkus Armbruster 1618dbffbdcfSStefan Hajnoczi struct BdrvTrackedRequest { 1619dbffbdcfSStefan Hajnoczi BlockDriverState *bs; 1620dbffbdcfSStefan Hajnoczi int64_t sector_num; 1621dbffbdcfSStefan Hajnoczi int nb_sectors; 1622dbffbdcfSStefan Hajnoczi bool is_write; 1623dbffbdcfSStefan Hajnoczi QLIST_ENTRY(BdrvTrackedRequest) list; 16245f8b6491SStefan Hajnoczi Coroutine *co; /* owner, used for deadlock detection */ 1625f4658285SStefan Hajnoczi CoQueue wait_queue; /* coroutines blocked on this request */ 1626dbffbdcfSStefan Hajnoczi }; 1627dbffbdcfSStefan Hajnoczi 1628dbffbdcfSStefan Hajnoczi /** 1629dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1630dbffbdcfSStefan Hajnoczi * 1631dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1632dbffbdcfSStefan Hajnoczi */ 1633dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1634dbffbdcfSStefan Hajnoczi { 1635dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1636f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1637dbffbdcfSStefan Hajnoczi } 1638dbffbdcfSStefan Hajnoczi 1639dbffbdcfSStefan Hajnoczi /** 1640dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1641dbffbdcfSStefan Hajnoczi */ 1642dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1643dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1644dbffbdcfSStefan Hajnoczi int64_t sector_num, 1645dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1646dbffbdcfSStefan Hajnoczi { 1647dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1648dbffbdcfSStefan Hajnoczi .bs = bs, 1649dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1650dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1651dbffbdcfSStefan Hajnoczi .is_write = is_write, 16525f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1653dbffbdcfSStefan Hajnoczi }; 1654dbffbdcfSStefan Hajnoczi 1655f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1656f4658285SStefan Hajnoczi 1657dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1658dbffbdcfSStefan Hajnoczi } 1659dbffbdcfSStefan Hajnoczi 1660d83947acSStefan Hajnoczi /** 1661d83947acSStefan Hajnoczi * Round a region to cluster boundaries 1662d83947acSStefan Hajnoczi */ 1663d83947acSStefan Hajnoczi static void round_to_clusters(BlockDriverState *bs, 1664d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 1665d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 1666d83947acSStefan Hajnoczi int *cluster_nb_sectors) 1667d83947acSStefan Hajnoczi { 1668d83947acSStefan Hajnoczi BlockDriverInfo bdi; 1669d83947acSStefan Hajnoczi 1670d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 1671d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 1672d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 1673d83947acSStefan Hajnoczi } else { 1674d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 1675d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 1676d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 1677d83947acSStefan Hajnoczi nb_sectors, c); 1678d83947acSStefan Hajnoczi } 1679d83947acSStefan Hajnoczi } 1680d83947acSStefan Hajnoczi 1681f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 1682f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 1683d83947acSStefan Hajnoczi /* aaaa bbbb */ 1684d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 1685d83947acSStefan Hajnoczi return false; 1686d83947acSStefan Hajnoczi } 1687d83947acSStefan Hajnoczi /* bbbb aaaa */ 1688d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 1689d83947acSStefan Hajnoczi return false; 1690d83947acSStefan Hajnoczi } 1691d83947acSStefan Hajnoczi return true; 1692f4658285SStefan Hajnoczi } 1693f4658285SStefan Hajnoczi 1694f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 1695f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 1696f4658285SStefan Hajnoczi { 1697f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 1698d83947acSStefan Hajnoczi int64_t cluster_sector_num; 1699d83947acSStefan Hajnoczi int cluster_nb_sectors; 1700f4658285SStefan Hajnoczi bool retry; 1701f4658285SStefan Hajnoczi 1702d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 1703d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 1704d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 1705d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 1706d83947acSStefan Hajnoczi * interleave between them. 1707d83947acSStefan Hajnoczi */ 1708d83947acSStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1709d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1710d83947acSStefan Hajnoczi 1711f4658285SStefan Hajnoczi do { 1712f4658285SStefan Hajnoczi retry = false; 1713f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 1714d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 1715d83947acSStefan Hajnoczi cluster_nb_sectors)) { 17165f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 17175f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 17185f8b6491SStefan Hajnoczi * never happen since it means deadlock. 17195f8b6491SStefan Hajnoczi */ 17205f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 17215f8b6491SStefan Hajnoczi 1722f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 1723f4658285SStefan Hajnoczi retry = true; 1724f4658285SStefan Hajnoczi break; 1725f4658285SStefan Hajnoczi } 1726f4658285SStefan Hajnoczi } 1727f4658285SStefan Hajnoczi } while (retry); 1728f4658285SStefan Hajnoczi } 1729f4658285SStefan Hajnoczi 1730756e6736SKevin Wolf /* 1731756e6736SKevin Wolf * Return values: 1732756e6736SKevin Wolf * 0 - success 1733756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 1734756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 1735756e6736SKevin Wolf * image file header 1736756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 1737756e6736SKevin Wolf */ 1738756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 1739756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1740756e6736SKevin Wolf { 1741756e6736SKevin Wolf BlockDriver *drv = bs->drv; 1742469ef350SPaolo Bonzini int ret; 1743756e6736SKevin Wolf 17445f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 17455f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 17465f377794SPaolo Bonzini return -EINVAL; 17475f377794SPaolo Bonzini } 17485f377794SPaolo Bonzini 1749756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 1750469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 1751756e6736SKevin Wolf } else { 1752469ef350SPaolo Bonzini ret = -ENOTSUP; 1753756e6736SKevin Wolf } 1754469ef350SPaolo Bonzini 1755469ef350SPaolo Bonzini if (ret == 0) { 1756469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1757469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1758469ef350SPaolo Bonzini } 1759469ef350SPaolo Bonzini return ret; 1760756e6736SKevin Wolf } 1761756e6736SKevin Wolf 17626ebdcee2SJeff Cody /* 17636ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 17646ebdcee2SJeff Cody * 17656ebdcee2SJeff Cody * active is the current topmost image. 17666ebdcee2SJeff Cody * 17676ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 17686ebdcee2SJeff Cody * or if active == bs. 17696ebdcee2SJeff Cody */ 17706ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 17716ebdcee2SJeff Cody BlockDriverState *bs) 17726ebdcee2SJeff Cody { 17736ebdcee2SJeff Cody BlockDriverState *overlay = NULL; 17746ebdcee2SJeff Cody BlockDriverState *intermediate; 17756ebdcee2SJeff Cody 17766ebdcee2SJeff Cody assert(active != NULL); 17776ebdcee2SJeff Cody assert(bs != NULL); 17786ebdcee2SJeff Cody 17796ebdcee2SJeff Cody /* if bs is the same as active, then by definition it has no overlay 17806ebdcee2SJeff Cody */ 17816ebdcee2SJeff Cody if (active == bs) { 17826ebdcee2SJeff Cody return NULL; 17836ebdcee2SJeff Cody } 17846ebdcee2SJeff Cody 17856ebdcee2SJeff Cody intermediate = active; 17866ebdcee2SJeff Cody while (intermediate->backing_hd) { 17876ebdcee2SJeff Cody if (intermediate->backing_hd == bs) { 17886ebdcee2SJeff Cody overlay = intermediate; 17896ebdcee2SJeff Cody break; 17906ebdcee2SJeff Cody } 17916ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 17926ebdcee2SJeff Cody } 17936ebdcee2SJeff Cody 17946ebdcee2SJeff Cody return overlay; 17956ebdcee2SJeff Cody } 17966ebdcee2SJeff Cody 17976ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 17986ebdcee2SJeff Cody BlockDriverState *bs; 17996ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 18006ebdcee2SJeff Cody } BlkIntermediateStates; 18016ebdcee2SJeff Cody 18026ebdcee2SJeff Cody 18036ebdcee2SJeff Cody /* 18046ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 18056ebdcee2SJeff Cody * above 'top' to have base as its backing file. 18066ebdcee2SJeff Cody * 18076ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 18086ebdcee2SJeff Cody * information in 'bs' can be properly updated. 18096ebdcee2SJeff Cody * 18106ebdcee2SJeff Cody * E.g., this will convert the following chain: 18116ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 18126ebdcee2SJeff Cody * 18136ebdcee2SJeff Cody * to 18146ebdcee2SJeff Cody * 18156ebdcee2SJeff Cody * bottom <- base <- active 18166ebdcee2SJeff Cody * 18176ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 18186ebdcee2SJeff Cody * 18196ebdcee2SJeff Cody * base <- intermediate <- top <- active 18206ebdcee2SJeff Cody * 18216ebdcee2SJeff Cody * to 18226ebdcee2SJeff Cody * 18236ebdcee2SJeff Cody * base <- active 18246ebdcee2SJeff Cody * 18256ebdcee2SJeff Cody * Error conditions: 18266ebdcee2SJeff Cody * if active == top, that is considered an error 18276ebdcee2SJeff Cody * 18286ebdcee2SJeff Cody */ 18296ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 18306ebdcee2SJeff Cody BlockDriverState *base) 18316ebdcee2SJeff Cody { 18326ebdcee2SJeff Cody BlockDriverState *intermediate; 18336ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 18346ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 18356ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 18366ebdcee2SJeff Cody int ret = -EIO; 18376ebdcee2SJeff Cody 18386ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 18396ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 18406ebdcee2SJeff Cody 18416ebdcee2SJeff Cody if (!top->drv || !base->drv) { 18426ebdcee2SJeff Cody goto exit; 18436ebdcee2SJeff Cody } 18446ebdcee2SJeff Cody 18456ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 18466ebdcee2SJeff Cody 18476ebdcee2SJeff Cody if (new_top_bs == NULL) { 18486ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 18496ebdcee2SJeff Cody goto exit; 18506ebdcee2SJeff Cody } 18516ebdcee2SJeff Cody 18526ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 18536ebdcee2SJeff Cody * to do, no intermediate images */ 18546ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 18556ebdcee2SJeff Cody ret = 0; 18566ebdcee2SJeff Cody goto exit; 18576ebdcee2SJeff Cody } 18586ebdcee2SJeff Cody 18596ebdcee2SJeff Cody intermediate = top; 18606ebdcee2SJeff Cody 18616ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 18626ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 18636ebdcee2SJeff Cody */ 18646ebdcee2SJeff Cody while (intermediate) { 18656ebdcee2SJeff Cody intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); 18666ebdcee2SJeff Cody intermediate_state->bs = intermediate; 18676ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 18686ebdcee2SJeff Cody 18696ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 18706ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 18716ebdcee2SJeff Cody break; 18726ebdcee2SJeff Cody } 18736ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 18746ebdcee2SJeff Cody } 18756ebdcee2SJeff Cody if (base_bs == NULL) { 18766ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 18776ebdcee2SJeff Cody * unravel everything, and exit with error */ 18786ebdcee2SJeff Cody goto exit; 18796ebdcee2SJeff Cody } 18806ebdcee2SJeff Cody 18816ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 18826ebdcee2SJeff Cody ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, 18836ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 18846ebdcee2SJeff Cody if (ret) { 18856ebdcee2SJeff Cody goto exit; 18866ebdcee2SJeff Cody } 18876ebdcee2SJeff Cody new_top_bs->backing_hd = base_bs; 18886ebdcee2SJeff Cody 18896ebdcee2SJeff Cody 18906ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 18916ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 18926ebdcee2SJeff Cody intermediate_state->bs->backing_hd = NULL; 18936ebdcee2SJeff Cody bdrv_delete(intermediate_state->bs); 18946ebdcee2SJeff Cody } 18956ebdcee2SJeff Cody ret = 0; 18966ebdcee2SJeff Cody 18976ebdcee2SJeff Cody exit: 18986ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 18996ebdcee2SJeff Cody g_free(intermediate_state); 19006ebdcee2SJeff Cody } 19016ebdcee2SJeff Cody return ret; 19026ebdcee2SJeff Cody } 19036ebdcee2SJeff Cody 19046ebdcee2SJeff Cody 190571d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 190671d0770cSaliguori size_t size) 190771d0770cSaliguori { 190871d0770cSaliguori int64_t len; 190971d0770cSaliguori 191071d0770cSaliguori if (!bdrv_is_inserted(bs)) 191171d0770cSaliguori return -ENOMEDIUM; 191271d0770cSaliguori 191371d0770cSaliguori if (bs->growable) 191471d0770cSaliguori return 0; 191571d0770cSaliguori 191671d0770cSaliguori len = bdrv_getlength(bs); 191771d0770cSaliguori 1918fbb7b4e0SKevin Wolf if (offset < 0) 1919fbb7b4e0SKevin Wolf return -EIO; 1920fbb7b4e0SKevin Wolf 1921fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 192271d0770cSaliguori return -EIO; 192371d0770cSaliguori 192471d0770cSaliguori return 0; 192571d0770cSaliguori } 192671d0770cSaliguori 192771d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 192871d0770cSaliguori int nb_sectors) 192971d0770cSaliguori { 1930eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1931eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 193271d0770cSaliguori } 193371d0770cSaliguori 19341c9805a3SStefan Hajnoczi typedef struct RwCo { 19351c9805a3SStefan Hajnoczi BlockDriverState *bs; 19361c9805a3SStefan Hajnoczi int64_t sector_num; 19371c9805a3SStefan Hajnoczi int nb_sectors; 19381c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 19391c9805a3SStefan Hajnoczi bool is_write; 19401c9805a3SStefan Hajnoczi int ret; 19411c9805a3SStefan Hajnoczi } RwCo; 19421c9805a3SStefan Hajnoczi 19431c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 1944fc01f7e7Sbellard { 19451c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 1946fc01f7e7Sbellard 19471c9805a3SStefan Hajnoczi if (!rwco->is_write) { 19481c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 1949470c0504SStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 19501c9805a3SStefan Hajnoczi } else { 19511c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 1952f08f2ddaSStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 19531c9805a3SStefan Hajnoczi } 19541c9805a3SStefan Hajnoczi } 1955e7a8a783SKevin Wolf 19561c9805a3SStefan Hajnoczi /* 19571c9805a3SStefan Hajnoczi * Process a synchronous request using coroutines 19581c9805a3SStefan Hajnoczi */ 19591c9805a3SStefan Hajnoczi static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 19601c9805a3SStefan Hajnoczi int nb_sectors, bool is_write) 19611c9805a3SStefan Hajnoczi { 1962e7a8a783SKevin Wolf QEMUIOVector qiov; 1963e7a8a783SKevin Wolf struct iovec iov = { 1964e7a8a783SKevin Wolf .iov_base = (void *)buf, 1965e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 1966e7a8a783SKevin Wolf }; 19671c9805a3SStefan Hajnoczi Coroutine *co; 19681c9805a3SStefan Hajnoczi RwCo rwco = { 19691c9805a3SStefan Hajnoczi .bs = bs, 19701c9805a3SStefan Hajnoczi .sector_num = sector_num, 19711c9805a3SStefan Hajnoczi .nb_sectors = nb_sectors, 19721c9805a3SStefan Hajnoczi .qiov = &qiov, 19731c9805a3SStefan Hajnoczi .is_write = is_write, 19741c9805a3SStefan Hajnoczi .ret = NOT_DONE, 19751c9805a3SStefan Hajnoczi }; 1976e7a8a783SKevin Wolf 1977e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 19781c9805a3SStefan Hajnoczi 1979498e386cSZhi Yong Wu /** 1980498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 1981498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 1982498e386cSZhi Yong Wu * if it has been enabled. 1983498e386cSZhi Yong Wu */ 1984498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 1985498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 1986498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 1987498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 1988498e386cSZhi Yong Wu } 1989498e386cSZhi Yong Wu 19901c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 19911c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 19921c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 19931c9805a3SStefan Hajnoczi } else { 19941c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 19951c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 19961c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 19971c9805a3SStefan Hajnoczi qemu_aio_wait(); 19981c9805a3SStefan Hajnoczi } 19991c9805a3SStefan Hajnoczi } 20001c9805a3SStefan Hajnoczi return rwco.ret; 2001e7a8a783SKevin Wolf } 2002e7a8a783SKevin Wolf 20031c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 20041c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 20051c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 20061c9805a3SStefan Hajnoczi { 20071c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); 200883f64091Sbellard } 2009fc01f7e7Sbellard 201007d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 201107d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 201207d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 201307d27a44SMarkus Armbruster { 201407d27a44SMarkus Armbruster bool enabled; 201507d27a44SMarkus Armbruster int ret; 201607d27a44SMarkus Armbruster 201707d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 201807d27a44SMarkus Armbruster bs->io_limits_enabled = false; 201907d27a44SMarkus Armbruster ret = bdrv_read(bs, 0, buf, 1); 202007d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 202107d27a44SMarkus Armbruster return ret; 202207d27a44SMarkus Armbruster } 202307d27a44SMarkus Armbruster 202471df14fcSPaolo Bonzini #define BITS_PER_LONG (sizeof(unsigned long) * 8) 202571df14fcSPaolo Bonzini 20267cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 20277cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 20287cd1e32aSlirans@il.ibm.com { 20297cd1e32aSlirans@il.ibm.com int64_t start, end; 2030c6d22830SJan Kiszka unsigned long val, idx, bit; 2031a55eb92cSJan Kiszka 20326ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 2033c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 20347cd1e32aSlirans@il.ibm.com 20357cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 203671df14fcSPaolo Bonzini idx = start / BITS_PER_LONG; 203771df14fcSPaolo Bonzini bit = start % BITS_PER_LONG; 2038c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 2039c6d22830SJan Kiszka if (dirty) { 20406d59fec1SMarcelo Tosatti if (!(val & (1UL << bit))) { 2041aaa0eb75SLiran Schour bs->dirty_count++; 20426d59fec1SMarcelo Tosatti val |= 1UL << bit; 2043aaa0eb75SLiran Schour } 2044c6d22830SJan Kiszka } else { 20456d59fec1SMarcelo Tosatti if (val & (1UL << bit)) { 2046aaa0eb75SLiran Schour bs->dirty_count--; 20476d59fec1SMarcelo Tosatti val &= ~(1UL << bit); 2048c6d22830SJan Kiszka } 2049aaa0eb75SLiran Schour } 2050c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 20517cd1e32aSlirans@il.ibm.com } 20527cd1e32aSlirans@il.ibm.com } 20537cd1e32aSlirans@il.ibm.com 205419cb3738Sbellard /* Return < 0 if error. Important errors are: 205519cb3738Sbellard -EIO generic I/O error (may happen for all errors) 205619cb3738Sbellard -ENOMEDIUM No media inserted. 205719cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 205819cb3738Sbellard -EACCES Trying to write a read-only device 205919cb3738Sbellard */ 2060fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 2061fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 2062fc01f7e7Sbellard { 20631c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); 206483f64091Sbellard } 206583f64091Sbellard 2066eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 2067eda578e5Saliguori void *buf, int count1) 206883f64091Sbellard { 20696ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 207083f64091Sbellard int len, nb_sectors, count; 207183f64091Sbellard int64_t sector_num; 20729a8c4cceSKevin Wolf int ret; 207383f64091Sbellard 207483f64091Sbellard count = count1; 207583f64091Sbellard /* first read to align to sector start */ 20766ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 207783f64091Sbellard if (len > count) 207883f64091Sbellard len = count; 20796ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 208083f64091Sbellard if (len > 0) { 20819a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 20829a8c4cceSKevin Wolf return ret; 20836ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 208483f64091Sbellard count -= len; 208583f64091Sbellard if (count == 0) 208683f64091Sbellard return count1; 208783f64091Sbellard sector_num++; 208883f64091Sbellard buf += len; 208983f64091Sbellard } 209083f64091Sbellard 209183f64091Sbellard /* read the sectors "in place" */ 20926ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 209383f64091Sbellard if (nb_sectors > 0) { 20949a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 20959a8c4cceSKevin Wolf return ret; 209683f64091Sbellard sector_num += nb_sectors; 20976ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 209883f64091Sbellard buf += len; 209983f64091Sbellard count -= len; 210083f64091Sbellard } 210183f64091Sbellard 210283f64091Sbellard /* add data from the last sector */ 210383f64091Sbellard if (count > 0) { 21049a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 21059a8c4cceSKevin Wolf return ret; 210683f64091Sbellard memcpy(buf, tmp_buf, count); 210783f64091Sbellard } 210883f64091Sbellard return count1; 210983f64091Sbellard } 211083f64091Sbellard 2111eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 2112eda578e5Saliguori const void *buf, int count1) 211383f64091Sbellard { 21146ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 211583f64091Sbellard int len, nb_sectors, count; 211683f64091Sbellard int64_t sector_num; 21179a8c4cceSKevin Wolf int ret; 211883f64091Sbellard 211983f64091Sbellard count = count1; 212083f64091Sbellard /* first write to align to sector start */ 21216ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 212283f64091Sbellard if (len > count) 212383f64091Sbellard len = count; 21246ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 212583f64091Sbellard if (len > 0) { 21269a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 21279a8c4cceSKevin Wolf return ret; 21286ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 21299a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 21309a8c4cceSKevin Wolf return ret; 213183f64091Sbellard count -= len; 213283f64091Sbellard if (count == 0) 213383f64091Sbellard return count1; 213483f64091Sbellard sector_num++; 213583f64091Sbellard buf += len; 213683f64091Sbellard } 213783f64091Sbellard 213883f64091Sbellard /* write the sectors "in place" */ 21396ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 214083f64091Sbellard if (nb_sectors > 0) { 21419a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 21429a8c4cceSKevin Wolf return ret; 214383f64091Sbellard sector_num += nb_sectors; 21446ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 214583f64091Sbellard buf += len; 214683f64091Sbellard count -= len; 214783f64091Sbellard } 214883f64091Sbellard 214983f64091Sbellard /* add data from the last sector */ 215083f64091Sbellard if (count > 0) { 21519a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 21529a8c4cceSKevin Wolf return ret; 215383f64091Sbellard memcpy(tmp_buf, buf, count); 21549a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 21559a8c4cceSKevin Wolf return ret; 215683f64091Sbellard } 215783f64091Sbellard return count1; 215883f64091Sbellard } 215983f64091Sbellard 2160f08145feSKevin Wolf /* 2161f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 2162f08145feSKevin Wolf * request (acts as a barrier) 2163f08145feSKevin Wolf * 2164f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 2165f08145feSKevin Wolf */ 2166f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 2167f08145feSKevin Wolf const void *buf, int count) 2168f08145feSKevin Wolf { 2169f08145feSKevin Wolf int ret; 2170f08145feSKevin Wolf 2171f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 2172f08145feSKevin Wolf if (ret < 0) { 2173f08145feSKevin Wolf return ret; 2174f08145feSKevin Wolf } 2175f08145feSKevin Wolf 2176f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 2177f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 2178f08145feSKevin Wolf bdrv_flush(bs); 2179f08145feSKevin Wolf } 2180f08145feSKevin Wolf 2181f08145feSKevin Wolf return 0; 2182f08145feSKevin Wolf } 2183f08145feSKevin Wolf 2184470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 2185ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2186ab185921SStefan Hajnoczi { 2187ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 2188ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 2189ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 2190ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 2191ab185921SStefan Hajnoczi */ 2192ab185921SStefan Hajnoczi void *bounce_buffer; 2193ab185921SStefan Hajnoczi 219479c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 2195ab185921SStefan Hajnoczi struct iovec iov; 2196ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 2197ab185921SStefan Hajnoczi int64_t cluster_sector_num; 2198ab185921SStefan Hajnoczi int cluster_nb_sectors; 2199ab185921SStefan Hajnoczi size_t skip_bytes; 2200ab185921SStefan Hajnoczi int ret; 2201ab185921SStefan Hajnoczi 2202ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 2203ab185921SStefan Hajnoczi * allocating cluster in the image file. 2204ab185921SStefan Hajnoczi */ 2205ab185921SStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 2206ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2207ab185921SStefan Hajnoczi 2208470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 2209ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 2210ab185921SStefan Hajnoczi 2211ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 2212ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 2213ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 2214ab185921SStefan Hajnoczi 221579c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 2216ab185921SStefan Hajnoczi &bounce_qiov); 2217ab185921SStefan Hajnoczi if (ret < 0) { 2218ab185921SStefan Hajnoczi goto err; 2219ab185921SStefan Hajnoczi } 2220ab185921SStefan Hajnoczi 222179c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 222279c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 2223621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 222479c053bdSStefan Hajnoczi cluster_nb_sectors); 222579c053bdSStefan Hajnoczi } else { 2226f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 2227f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 2228f05fa4adSPaolo Bonzini */ 222979c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 2230ab185921SStefan Hajnoczi &bounce_qiov); 223179c053bdSStefan Hajnoczi } 223279c053bdSStefan Hajnoczi 2233ab185921SStefan Hajnoczi if (ret < 0) { 2234ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 2235ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 2236ab185921SStefan Hajnoczi * Simply report it in all cases. 2237ab185921SStefan Hajnoczi */ 2238ab185921SStefan Hajnoczi goto err; 2239ab185921SStefan Hajnoczi } 2240ab185921SStefan Hajnoczi 2241ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 224203396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 2243ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 2244ab185921SStefan Hajnoczi 2245ab185921SStefan Hajnoczi err: 2246ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 2247ab185921SStefan Hajnoczi return ret; 2248ab185921SStefan Hajnoczi } 2249ab185921SStefan Hajnoczi 2250c5fbe571SStefan Hajnoczi /* 2251c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 2252c5fbe571SStefan Hajnoczi */ 2253c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 2254470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2255470c0504SStefan Hajnoczi BdrvRequestFlags flags) 2256da1fa91dSKevin Wolf { 2257da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 2258dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 2259dbffbdcfSStefan Hajnoczi int ret; 2260da1fa91dSKevin Wolf 2261da1fa91dSKevin Wolf if (!drv) { 2262da1fa91dSKevin Wolf return -ENOMEDIUM; 2263da1fa91dSKevin Wolf } 2264da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2265da1fa91dSKevin Wolf return -EIO; 2266da1fa91dSKevin Wolf } 2267da1fa91dSKevin Wolf 226898f90dbaSZhi Yong Wu /* throttling disk read I/O */ 226998f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 227098f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, false, nb_sectors); 227198f90dbaSZhi Yong Wu } 227298f90dbaSZhi Yong Wu 2273f4658285SStefan Hajnoczi if (bs->copy_on_read) { 2274470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 2275470c0504SStefan Hajnoczi } 2276470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2277470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 2278470c0504SStefan Hajnoczi } 2279470c0504SStefan Hajnoczi 2280470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2281f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2282f4658285SStefan Hajnoczi } 2283f4658285SStefan Hajnoczi 2284dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 2285ab185921SStefan Hajnoczi 2286470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2287ab185921SStefan Hajnoczi int pnum; 2288ab185921SStefan Hajnoczi 2289ab185921SStefan Hajnoczi ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); 2290ab185921SStefan Hajnoczi if (ret < 0) { 2291ab185921SStefan Hajnoczi goto out; 2292ab185921SStefan Hajnoczi } 2293ab185921SStefan Hajnoczi 2294ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 2295470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 2296ab185921SStefan Hajnoczi goto out; 2297ab185921SStefan Hajnoczi } 2298ab185921SStefan Hajnoczi } 2299ab185921SStefan Hajnoczi 2300dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 2301ab185921SStefan Hajnoczi 2302ab185921SStefan Hajnoczi out: 2303dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2304470c0504SStefan Hajnoczi 2305470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2306470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 2307470c0504SStefan Hajnoczi } 2308470c0504SStefan Hajnoczi 2309dbffbdcfSStefan Hajnoczi return ret; 2310da1fa91dSKevin Wolf } 2311da1fa91dSKevin Wolf 2312c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 2313da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 2314da1fa91dSKevin Wolf { 2315c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 2316da1fa91dSKevin Wolf 2317470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 2318470c0504SStefan Hajnoczi } 2319470c0504SStefan Hajnoczi 2320470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 2321470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2322470c0504SStefan Hajnoczi { 2323470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 2324470c0504SStefan Hajnoczi 2325470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 2326470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 2327c5fbe571SStefan Hajnoczi } 2328c5fbe571SStefan Hajnoczi 2329f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 2330f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 2331f08f2ddaSStefan Hajnoczi { 2332f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 2333f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 2334f08f2ddaSStefan Hajnoczi struct iovec iov; 2335f08f2ddaSStefan Hajnoczi int ret; 2336f08f2ddaSStefan Hajnoczi 2337621f0589SKevin Wolf /* TODO Emulate only part of misaligned requests instead of letting block 2338621f0589SKevin Wolf * drivers return -ENOTSUP and emulate everything */ 2339621f0589SKevin Wolf 2340f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 2341f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 2342621f0589SKevin Wolf ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 2343621f0589SKevin Wolf if (ret != -ENOTSUP) { 2344621f0589SKevin Wolf return ret; 2345621f0589SKevin Wolf } 2346f08f2ddaSStefan Hajnoczi } 2347f08f2ddaSStefan Hajnoczi 2348f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 2349f08f2ddaSStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2350f08f2ddaSStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 2351f08f2ddaSStefan Hajnoczi memset(iov.iov_base, 0, iov.iov_len); 2352f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 2353f08f2ddaSStefan Hajnoczi 2354f08f2ddaSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 2355f08f2ddaSStefan Hajnoczi 2356f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 2357f08f2ddaSStefan Hajnoczi return ret; 2358f08f2ddaSStefan Hajnoczi } 2359f08f2ddaSStefan Hajnoczi 2360c5fbe571SStefan Hajnoczi /* 2361c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 2362c5fbe571SStefan Hajnoczi */ 2363c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 2364f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2365f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 2366c5fbe571SStefan Hajnoczi { 2367c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 2368dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 23696b7cb247SStefan Hajnoczi int ret; 2370da1fa91dSKevin Wolf 2371da1fa91dSKevin Wolf if (!bs->drv) { 2372da1fa91dSKevin Wolf return -ENOMEDIUM; 2373da1fa91dSKevin Wolf } 2374da1fa91dSKevin Wolf if (bs->read_only) { 2375da1fa91dSKevin Wolf return -EACCES; 2376da1fa91dSKevin Wolf } 2377da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2378da1fa91dSKevin Wolf return -EIO; 2379da1fa91dSKevin Wolf } 2380da1fa91dSKevin Wolf 238198f90dbaSZhi Yong Wu /* throttling disk write I/O */ 238298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 238398f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, true, nb_sectors); 238498f90dbaSZhi Yong Wu } 238598f90dbaSZhi Yong Wu 2386470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2387f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2388f4658285SStefan Hajnoczi } 2389f4658285SStefan Hajnoczi 2390dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 2391dbffbdcfSStefan Hajnoczi 2392f08f2ddaSStefan Hajnoczi if (flags & BDRV_REQ_ZERO_WRITE) { 2393f08f2ddaSStefan Hajnoczi ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); 2394f08f2ddaSStefan Hajnoczi } else { 23956b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 2396f08f2ddaSStefan Hajnoczi } 23976b7cb247SStefan Hajnoczi 2398f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 2399f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 2400f05fa4adSPaolo Bonzini } 2401f05fa4adSPaolo Bonzini 2402da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 24031755da16SPaolo Bonzini bdrv_set_dirty(bs, sector_num, nb_sectors); 2404da1fa91dSKevin Wolf } 2405da1fa91dSKevin Wolf 2406da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2407da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2408da1fa91dSKevin Wolf } 2409da1fa91dSKevin Wolf 2410dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2411dbffbdcfSStefan Hajnoczi 24126b7cb247SStefan Hajnoczi return ret; 2413da1fa91dSKevin Wolf } 2414da1fa91dSKevin Wolf 2415c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 2416c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 2417c5fbe571SStefan Hajnoczi { 2418c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 2419c5fbe571SStefan Hajnoczi 2420f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 2421f08f2ddaSStefan Hajnoczi } 2422f08f2ddaSStefan Hajnoczi 2423f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 2424f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 2425f08f2ddaSStefan Hajnoczi { 2426f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 2427f08f2ddaSStefan Hajnoczi 2428f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 2429f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE); 2430c5fbe571SStefan Hajnoczi } 2431c5fbe571SStefan Hajnoczi 243283f64091Sbellard /** 243383f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 243483f64091Sbellard */ 243583f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 243683f64091Sbellard { 243783f64091Sbellard BlockDriver *drv = bs->drv; 243851762288SStefan Hajnoczi int ret; 243983f64091Sbellard if (!drv) 244019cb3738Sbellard return -ENOMEDIUM; 244183f64091Sbellard if (!drv->bdrv_truncate) 244283f64091Sbellard return -ENOTSUP; 244359f2689dSNaphtali Sprei if (bs->read_only) 244459f2689dSNaphtali Sprei return -EACCES; 24458591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 24468591675fSMarcelo Tosatti return -EBUSY; 244751762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 244851762288SStefan Hajnoczi if (ret == 0) { 244951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2450145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 245151762288SStefan Hajnoczi } 245251762288SStefan Hajnoczi return ret; 245383f64091Sbellard } 245483f64091Sbellard 245583f64091Sbellard /** 24564a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 24574a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 24584a1d5e1fSFam Zheng */ 24594a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 24604a1d5e1fSFam Zheng { 24614a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 24624a1d5e1fSFam Zheng if (!drv) { 24634a1d5e1fSFam Zheng return -ENOMEDIUM; 24644a1d5e1fSFam Zheng } 24654a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 24664a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 24674a1d5e1fSFam Zheng } 24684a1d5e1fSFam Zheng if (bs->file) { 24694a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 24704a1d5e1fSFam Zheng } 24714a1d5e1fSFam Zheng return -ENOTSUP; 24724a1d5e1fSFam Zheng } 24734a1d5e1fSFam Zheng 24744a1d5e1fSFam Zheng /** 247583f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 247683f64091Sbellard */ 247783f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 247883f64091Sbellard { 247983f64091Sbellard BlockDriver *drv = bs->drv; 248083f64091Sbellard if (!drv) 248119cb3738Sbellard return -ENOMEDIUM; 248251762288SStefan Hajnoczi 24832c6942faSMarkus Armbruster if (bs->growable || bdrv_dev_has_removable_media(bs)) { 248446a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 248583f64091Sbellard return drv->bdrv_getlength(bs); 2486fc01f7e7Sbellard } 248746a4e4e6SStefan Hajnoczi } 248846a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 248946a4e4e6SStefan Hajnoczi } 2490fc01f7e7Sbellard 249119cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 249296b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2493fc01f7e7Sbellard { 249419cb3738Sbellard int64_t length; 249519cb3738Sbellard length = bdrv_getlength(bs); 249619cb3738Sbellard if (length < 0) 249719cb3738Sbellard length = 0; 249819cb3738Sbellard else 24996ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 250019cb3738Sbellard *nb_sectors_ptr = length; 2501fc01f7e7Sbellard } 2502cf98951bSbellard 25030563e191SZhi Yong Wu /* throttling disk io limits */ 25040563e191SZhi Yong Wu void bdrv_set_io_limits(BlockDriverState *bs, 25050563e191SZhi Yong Wu BlockIOLimit *io_limits) 25060563e191SZhi Yong Wu { 25070563e191SZhi Yong Wu bs->io_limits = *io_limits; 25080563e191SZhi Yong Wu bs->io_limits_enabled = bdrv_io_limits_enabled(bs); 25090563e191SZhi Yong Wu } 25100563e191SZhi Yong Wu 2511ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 2512ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 2513abd7f68dSMarkus Armbruster { 2514abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2515abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2516abd7f68dSMarkus Armbruster } 2517abd7f68dSMarkus Armbruster 25181ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 2519abd7f68dSMarkus Armbruster { 2520abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2521abd7f68dSMarkus Armbruster } 2522abd7f68dSMarkus Armbruster 25233e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 25243e1caa5fSPaolo Bonzini { 25253e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 25263e1caa5fSPaolo Bonzini 25273e1caa5fSPaolo Bonzini switch (on_err) { 25283e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 25293e1caa5fSPaolo Bonzini return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; 25303e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 25313e1caa5fSPaolo Bonzini return BDRV_ACTION_STOP; 25323e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 25333e1caa5fSPaolo Bonzini return BDRV_ACTION_REPORT; 25343e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 25353e1caa5fSPaolo Bonzini return BDRV_ACTION_IGNORE; 25363e1caa5fSPaolo Bonzini default: 25373e1caa5fSPaolo Bonzini abort(); 25383e1caa5fSPaolo Bonzini } 25393e1caa5fSPaolo Bonzini } 25403e1caa5fSPaolo Bonzini 25413e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 25423e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 25433e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 25443e1caa5fSPaolo Bonzini */ 25453e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 25463e1caa5fSPaolo Bonzini bool is_read, int error) 25473e1caa5fSPaolo Bonzini { 25483e1caa5fSPaolo Bonzini assert(error >= 0); 254932c81a4aSPaolo Bonzini bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); 25503e1caa5fSPaolo Bonzini if (action == BDRV_ACTION_STOP) { 25513e1caa5fSPaolo Bonzini vm_stop(RUN_STATE_IO_ERROR); 25523e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 25533e1caa5fSPaolo Bonzini } 25543e1caa5fSPaolo Bonzini } 25553e1caa5fSPaolo Bonzini 2556b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2557b338082bSbellard { 2558b338082bSbellard return bs->read_only; 2559b338082bSbellard } 2560b338082bSbellard 2561985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2562985a03b0Sths { 2563985a03b0Sths return bs->sg; 2564985a03b0Sths } 2565985a03b0Sths 2566e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2567e900a7b7SChristoph Hellwig { 2568e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2569e900a7b7SChristoph Hellwig } 2570e900a7b7SChristoph Hellwig 2571425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 2572425b0148SPaolo Bonzini { 2573425b0148SPaolo Bonzini bs->enable_write_cache = wce; 257455b110f2SJeff Cody 257555b110f2SJeff Cody /* so a reopen() will preserve wce */ 257655b110f2SJeff Cody if (wce) { 257755b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 257855b110f2SJeff Cody } else { 257955b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 258055b110f2SJeff Cody } 2581425b0148SPaolo Bonzini } 2582425b0148SPaolo Bonzini 2583ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2584ea2384d3Sbellard { 2585ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2586ea2384d3Sbellard return 1; 2587ea2384d3Sbellard return bs->encrypted; 2588ea2384d3Sbellard } 2589ea2384d3Sbellard 2590c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2591c0f4ce77Saliguori { 2592c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2593c0f4ce77Saliguori 2594c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2595c0f4ce77Saliguori return 1; 2596c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2597c0f4ce77Saliguori } 2598c0f4ce77Saliguori 2599ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2600ea2384d3Sbellard { 2601ea2384d3Sbellard int ret; 2602ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2603ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2604ea2384d3Sbellard if (ret < 0) 2605ea2384d3Sbellard return ret; 2606ea2384d3Sbellard if (!bs->encrypted) 2607ea2384d3Sbellard return 0; 2608ea2384d3Sbellard } 2609fd04a2aeSShahar Havivi if (!bs->encrypted) { 2610fd04a2aeSShahar Havivi return -EINVAL; 2611fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2612fd04a2aeSShahar Havivi return -ENOMEDIUM; 2613fd04a2aeSShahar Havivi } 2614c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2615bb5fc20fSaliguori if (ret < 0) { 2616bb5fc20fSaliguori bs->valid_key = 0; 2617bb5fc20fSaliguori } else if (!bs->valid_key) { 2618bb5fc20fSaliguori bs->valid_key = 1; 2619bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 26207d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 2621bb5fc20fSaliguori } 2622c0f4ce77Saliguori return ret; 2623ea2384d3Sbellard } 2624ea2384d3Sbellard 2625f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 2626ea2384d3Sbellard { 2627f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 2628ea2384d3Sbellard } 2629ea2384d3Sbellard 2630ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2631ea2384d3Sbellard void *opaque) 2632ea2384d3Sbellard { 2633ea2384d3Sbellard BlockDriver *drv; 2634ea2384d3Sbellard 26358a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2636ea2384d3Sbellard it(opaque, drv->format_name); 2637ea2384d3Sbellard } 2638ea2384d3Sbellard } 2639ea2384d3Sbellard 2640b338082bSbellard BlockDriverState *bdrv_find(const char *name) 2641b338082bSbellard { 2642b338082bSbellard BlockDriverState *bs; 2643b338082bSbellard 26441b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 26451b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 2646b338082bSbellard return bs; 2647b338082bSbellard } 26481b7bdbc1SStefan Hajnoczi } 2649b338082bSbellard return NULL; 2650b338082bSbellard } 2651b338082bSbellard 26522f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 26532f399b0aSMarkus Armbruster { 26542f399b0aSMarkus Armbruster if (!bs) { 26552f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 26562f399b0aSMarkus Armbruster } 26572f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 26582f399b0aSMarkus Armbruster } 26592f399b0aSMarkus Armbruster 266051de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 266181d0912dSbellard { 266281d0912dSbellard BlockDriverState *bs; 266381d0912dSbellard 26641b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 266551de9760Saliguori it(opaque, bs); 266681d0912dSbellard } 266781d0912dSbellard } 266881d0912dSbellard 2669ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 2670ea2384d3Sbellard { 2671ea2384d3Sbellard return bs->device_name; 2672ea2384d3Sbellard } 2673ea2384d3Sbellard 2674c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 2675c8433287SMarkus Armbruster { 2676c8433287SMarkus Armbruster return bs->open_flags; 2677c8433287SMarkus Armbruster } 2678c8433287SMarkus Armbruster 2679c6ca28d6Saliguori void bdrv_flush_all(void) 2680c6ca28d6Saliguori { 2681c6ca28d6Saliguori BlockDriverState *bs; 2682c6ca28d6Saliguori 26831b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2684c6ca28d6Saliguori bdrv_flush(bs); 2685c6ca28d6Saliguori } 26861b7bdbc1SStefan Hajnoczi } 2687c6ca28d6Saliguori 2688f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2689f2feebbdSKevin Wolf { 2690f2feebbdSKevin Wolf assert(bs->drv); 2691f2feebbdSKevin Wolf 2692336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2693336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2694f2feebbdSKevin Wolf } 2695f2feebbdSKevin Wolf 2696f2feebbdSKevin Wolf return 1; 2697f2feebbdSKevin Wolf } 2698f2feebbdSKevin Wolf 2699376ae3f1SStefan Hajnoczi typedef struct BdrvCoIsAllocatedData { 2700376ae3f1SStefan Hajnoczi BlockDriverState *bs; 2701376ae3f1SStefan Hajnoczi int64_t sector_num; 2702376ae3f1SStefan Hajnoczi int nb_sectors; 2703376ae3f1SStefan Hajnoczi int *pnum; 2704376ae3f1SStefan Hajnoczi int ret; 2705376ae3f1SStefan Hajnoczi bool done; 2706376ae3f1SStefan Hajnoczi } BdrvCoIsAllocatedData; 2707376ae3f1SStefan Hajnoczi 2708f58c7b35Sths /* 2709f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 2710f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 2711f58c7b35Sths * hence all their sectors are reported as allocated. 2712f58c7b35Sths * 2713bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 2714bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 2715bd9533e3SStefan Hajnoczi * 2716f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 2717f58c7b35Sths * the specified sector) that are known to be in the same 2718f58c7b35Sths * allocated/unallocated state. 2719f58c7b35Sths * 2720bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2721bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 2722f58c7b35Sths */ 2723060f51c9SStefan Hajnoczi int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, 2724060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 2725f58c7b35Sths { 2726f58c7b35Sths int64_t n; 2727bd9533e3SStefan Hajnoczi 27286aebab14SStefan Hajnoczi if (sector_num >= bs->total_sectors) { 27296aebab14SStefan Hajnoczi *pnum = 0; 27306aebab14SStefan Hajnoczi return 0; 27316aebab14SStefan Hajnoczi } 2732bd9533e3SStefan Hajnoczi 27336aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 2734bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 2735bd9533e3SStefan Hajnoczi nb_sectors = n; 2736bd9533e3SStefan Hajnoczi } 2737bd9533e3SStefan Hajnoczi 2738bd9533e3SStefan Hajnoczi if (!bs->drv->bdrv_co_is_allocated) { 2739bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 27406aebab14SStefan Hajnoczi return 1; 27416aebab14SStefan Hajnoczi } 27426aebab14SStefan Hajnoczi 2743060f51c9SStefan Hajnoczi return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum); 2744060f51c9SStefan Hajnoczi } 2745060f51c9SStefan Hajnoczi 2746060f51c9SStefan Hajnoczi /* Coroutine wrapper for bdrv_is_allocated() */ 2747060f51c9SStefan Hajnoczi static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) 2748060f51c9SStefan Hajnoczi { 2749060f51c9SStefan Hajnoczi BdrvCoIsAllocatedData *data = opaque; 2750060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 2751060f51c9SStefan Hajnoczi 2752060f51c9SStefan Hajnoczi data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, 2753060f51c9SStefan Hajnoczi data->pnum); 2754060f51c9SStefan Hajnoczi data->done = true; 2755060f51c9SStefan Hajnoczi } 2756060f51c9SStefan Hajnoczi 2757060f51c9SStefan Hajnoczi /* 2758060f51c9SStefan Hajnoczi * Synchronous wrapper around bdrv_co_is_allocated(). 2759060f51c9SStefan Hajnoczi * 2760060f51c9SStefan Hajnoczi * See bdrv_co_is_allocated() for details. 2761060f51c9SStefan Hajnoczi */ 2762060f51c9SStefan Hajnoczi int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 2763060f51c9SStefan Hajnoczi int *pnum) 2764060f51c9SStefan Hajnoczi { 2765376ae3f1SStefan Hajnoczi Coroutine *co; 2766376ae3f1SStefan Hajnoczi BdrvCoIsAllocatedData data = { 2767376ae3f1SStefan Hajnoczi .bs = bs, 2768376ae3f1SStefan Hajnoczi .sector_num = sector_num, 2769376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 2770376ae3f1SStefan Hajnoczi .pnum = pnum, 2771376ae3f1SStefan Hajnoczi .done = false, 2772376ae3f1SStefan Hajnoczi }; 2773376ae3f1SStefan Hajnoczi 2774376ae3f1SStefan Hajnoczi co = qemu_coroutine_create(bdrv_is_allocated_co_entry); 2775376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 2776376ae3f1SStefan Hajnoczi while (!data.done) { 2777376ae3f1SStefan Hajnoczi qemu_aio_wait(); 2778376ae3f1SStefan Hajnoczi } 2779376ae3f1SStefan Hajnoczi return data.ret; 2780376ae3f1SStefan Hajnoczi } 2781f58c7b35Sths 2782188a7bbfSPaolo Bonzini /* 2783188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 2784188a7bbfSPaolo Bonzini * 2785188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 2786188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 2787188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 2788188a7bbfSPaolo Bonzini * 2789188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 2790188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 2791188a7bbfSPaolo Bonzini * allocated/unallocated state. 2792188a7bbfSPaolo Bonzini * 2793188a7bbfSPaolo Bonzini */ 2794188a7bbfSPaolo Bonzini int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top, 2795188a7bbfSPaolo Bonzini BlockDriverState *base, 2796188a7bbfSPaolo Bonzini int64_t sector_num, 2797188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 2798188a7bbfSPaolo Bonzini { 2799188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 2800188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 2801188a7bbfSPaolo Bonzini 2802188a7bbfSPaolo Bonzini intermediate = top; 2803188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 2804188a7bbfSPaolo Bonzini int pnum_inter; 2805188a7bbfSPaolo Bonzini ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors, 2806188a7bbfSPaolo Bonzini &pnum_inter); 2807188a7bbfSPaolo Bonzini if (ret < 0) { 2808188a7bbfSPaolo Bonzini return ret; 2809188a7bbfSPaolo Bonzini } else if (ret) { 2810188a7bbfSPaolo Bonzini *pnum = pnum_inter; 2811188a7bbfSPaolo Bonzini return 1; 2812188a7bbfSPaolo Bonzini } 2813188a7bbfSPaolo Bonzini 2814188a7bbfSPaolo Bonzini /* 2815188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 2816188a7bbfSPaolo Bonzini * might have 2817188a7bbfSPaolo Bonzini * 2818188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 2819188a7bbfSPaolo Bonzini */ 2820188a7bbfSPaolo Bonzini if (n > pnum_inter) { 2821188a7bbfSPaolo Bonzini n = pnum_inter; 2822188a7bbfSPaolo Bonzini } 2823188a7bbfSPaolo Bonzini 2824188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 2825188a7bbfSPaolo Bonzini } 2826188a7bbfSPaolo Bonzini 2827188a7bbfSPaolo Bonzini *pnum = n; 2828188a7bbfSPaolo Bonzini return 0; 2829188a7bbfSPaolo Bonzini } 2830188a7bbfSPaolo Bonzini 2831ac84adacSPaolo Bonzini BlockInfo *bdrv_query_info(BlockDriverState *bs) 2832ac84adacSPaolo Bonzini { 2833ac84adacSPaolo Bonzini BlockInfo *info = g_malloc0(sizeof(*info)); 2834ac84adacSPaolo Bonzini info->device = g_strdup(bs->device_name); 2835ac84adacSPaolo Bonzini info->type = g_strdup("unknown"); 2836ac84adacSPaolo Bonzini info->locked = bdrv_dev_is_medium_locked(bs); 2837ac84adacSPaolo Bonzini info->removable = bdrv_dev_has_removable_media(bs); 2838ac84adacSPaolo Bonzini 2839ac84adacSPaolo Bonzini if (bdrv_dev_has_removable_media(bs)) { 2840ac84adacSPaolo Bonzini info->has_tray_open = true; 2841ac84adacSPaolo Bonzini info->tray_open = bdrv_dev_is_tray_open(bs); 2842ac84adacSPaolo Bonzini } 2843ac84adacSPaolo Bonzini 2844ac84adacSPaolo Bonzini if (bdrv_iostatus_is_enabled(bs)) { 2845ac84adacSPaolo Bonzini info->has_io_status = true; 2846ac84adacSPaolo Bonzini info->io_status = bs->iostatus; 2847ac84adacSPaolo Bonzini } 2848ac84adacSPaolo Bonzini 2849b9a9b3a4SPaolo Bonzini if (bs->dirty_bitmap) { 2850b9a9b3a4SPaolo Bonzini info->has_dirty = true; 2851b9a9b3a4SPaolo Bonzini info->dirty = g_malloc0(sizeof(*info->dirty)); 2852b9a9b3a4SPaolo Bonzini info->dirty->count = bdrv_get_dirty_count(bs) * 2853b9a9b3a4SPaolo Bonzini BDRV_SECTORS_PER_DIRTY_CHUNK * BDRV_SECTOR_SIZE; 2854b9a9b3a4SPaolo Bonzini } 2855b9a9b3a4SPaolo Bonzini 2856ac84adacSPaolo Bonzini if (bs->drv) { 2857ac84adacSPaolo Bonzini info->has_inserted = true; 2858ac84adacSPaolo Bonzini info->inserted = g_malloc0(sizeof(*info->inserted)); 2859ac84adacSPaolo Bonzini info->inserted->file = g_strdup(bs->filename); 2860ac84adacSPaolo Bonzini info->inserted->ro = bs->read_only; 2861ac84adacSPaolo Bonzini info->inserted->drv = g_strdup(bs->drv->format_name); 2862ac84adacSPaolo Bonzini info->inserted->encrypted = bs->encrypted; 2863ac84adacSPaolo Bonzini info->inserted->encryption_key_missing = bdrv_key_required(bs); 2864ac84adacSPaolo Bonzini 2865ac84adacSPaolo Bonzini if (bs->backing_file[0]) { 2866ac84adacSPaolo Bonzini info->inserted->has_backing_file = true; 2867ac84adacSPaolo Bonzini info->inserted->backing_file = g_strdup(bs->backing_file); 2868ac84adacSPaolo Bonzini } 2869ac84adacSPaolo Bonzini 2870ac84adacSPaolo Bonzini info->inserted->backing_file_depth = bdrv_get_backing_file_depth(bs); 2871ac84adacSPaolo Bonzini 2872ac84adacSPaolo Bonzini if (bs->io_limits_enabled) { 2873ac84adacSPaolo Bonzini info->inserted->bps = 2874ac84adacSPaolo Bonzini bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 2875ac84adacSPaolo Bonzini info->inserted->bps_rd = 2876ac84adacSPaolo Bonzini bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; 2877ac84adacSPaolo Bonzini info->inserted->bps_wr = 2878ac84adacSPaolo Bonzini bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; 2879ac84adacSPaolo Bonzini info->inserted->iops = 2880ac84adacSPaolo Bonzini bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 2881ac84adacSPaolo Bonzini info->inserted->iops_rd = 2882ac84adacSPaolo Bonzini bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; 2883ac84adacSPaolo Bonzini info->inserted->iops_wr = 2884ac84adacSPaolo Bonzini bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; 2885ac84adacSPaolo Bonzini } 2886ac84adacSPaolo Bonzini } 2887ac84adacSPaolo Bonzini return info; 2888ac84adacSPaolo Bonzini } 2889ac84adacSPaolo Bonzini 2890b2023818SLuiz Capitulino BlockInfoList *qmp_query_block(Error **errp) 2891b338082bSbellard { 2892ac84adacSPaolo Bonzini BlockInfoList *head = NULL, **p_next = &head; 2893d15e5465SLuiz Capitulino BlockDriverState *bs; 2894d15e5465SLuiz Capitulino 28951b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2896b2023818SLuiz Capitulino BlockInfoList *info = g_malloc0(sizeof(*info)); 2897ac84adacSPaolo Bonzini info->value = bdrv_query_info(bs); 2898d15e5465SLuiz Capitulino 2899ac84adacSPaolo Bonzini *p_next = info; 2900ac84adacSPaolo Bonzini p_next = &info->next; 2901d15e5465SLuiz Capitulino } 2902d15e5465SLuiz Capitulino 2903b2023818SLuiz Capitulino return head; 2904b338082bSbellard } 2905a36e69ddSths 29069887b616SPaolo Bonzini BlockStats *bdrv_query_stats(const BlockDriverState *bs) 2907a36e69ddSths { 2908f11f57e4SLuiz Capitulino BlockStats *s; 2909218a536aSLuiz Capitulino 2910f11f57e4SLuiz Capitulino s = g_malloc0(sizeof(*s)); 2911218a536aSLuiz Capitulino 2912f11f57e4SLuiz Capitulino if (bs->device_name[0]) { 2913f11f57e4SLuiz Capitulino s->has_device = true; 2914f11f57e4SLuiz Capitulino s->device = g_strdup(bs->device_name); 2915218a536aSLuiz Capitulino } 2916218a536aSLuiz Capitulino 2917f11f57e4SLuiz Capitulino s->stats = g_malloc0(sizeof(*s->stats)); 2918f11f57e4SLuiz Capitulino s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; 2919f11f57e4SLuiz Capitulino s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; 2920f11f57e4SLuiz Capitulino s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; 2921f11f57e4SLuiz Capitulino s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; 2922f11f57e4SLuiz Capitulino s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; 2923f11f57e4SLuiz Capitulino s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; 2924f11f57e4SLuiz Capitulino s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; 2925f11f57e4SLuiz Capitulino s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; 2926f11f57e4SLuiz Capitulino s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; 2927294cc35fSKevin Wolf 2928294cc35fSKevin Wolf if (bs->file) { 2929f11f57e4SLuiz Capitulino s->has_parent = true; 29309887b616SPaolo Bonzini s->parent = bdrv_query_stats(bs->file); 2931294cc35fSKevin Wolf } 2932294cc35fSKevin Wolf 2933f11f57e4SLuiz Capitulino return s; 2934294cc35fSKevin Wolf } 2935294cc35fSKevin Wolf 2936f11f57e4SLuiz Capitulino BlockStatsList *qmp_query_blockstats(Error **errp) 2937218a536aSLuiz Capitulino { 29389887b616SPaolo Bonzini BlockStatsList *head = NULL, **p_next = &head; 2939a36e69ddSths BlockDriverState *bs; 2940a36e69ddSths 29411b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2942f11f57e4SLuiz Capitulino BlockStatsList *info = g_malloc0(sizeof(*info)); 29439887b616SPaolo Bonzini info->value = bdrv_query_stats(bs); 2944f11f57e4SLuiz Capitulino 29459887b616SPaolo Bonzini *p_next = info; 29469887b616SPaolo Bonzini p_next = &info->next; 2947a36e69ddSths } 2948218a536aSLuiz Capitulino 2949f11f57e4SLuiz Capitulino return head; 2950a36e69ddSths } 2951ea2384d3Sbellard 2952045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 2953045df330Saliguori { 2954045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 2955045df330Saliguori return bs->backing_file; 2956045df330Saliguori else if (bs->encrypted) 2957045df330Saliguori return bs->filename; 2958045df330Saliguori else 2959045df330Saliguori return NULL; 2960045df330Saliguori } 2961045df330Saliguori 296283f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 296383f64091Sbellard char *filename, int filename_size) 296483f64091Sbellard { 296583f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 296683f64091Sbellard } 296783f64091Sbellard 2968faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 2969faea38e7Sbellard const uint8_t *buf, int nb_sectors) 2970faea38e7Sbellard { 2971faea38e7Sbellard BlockDriver *drv = bs->drv; 2972faea38e7Sbellard if (!drv) 297319cb3738Sbellard return -ENOMEDIUM; 2974faea38e7Sbellard if (!drv->bdrv_write_compressed) 2975faea38e7Sbellard return -ENOTSUP; 2976fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 2977fbb7b4e0SKevin Wolf return -EIO; 29787cd1e32aSlirans@il.ibm.com 29791755da16SPaolo Bonzini assert(!bs->dirty_bitmap); 29807cd1e32aSlirans@il.ibm.com 2981faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 2982faea38e7Sbellard } 2983faea38e7Sbellard 2984faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2985faea38e7Sbellard { 2986faea38e7Sbellard BlockDriver *drv = bs->drv; 2987faea38e7Sbellard if (!drv) 298819cb3738Sbellard return -ENOMEDIUM; 2989faea38e7Sbellard if (!drv->bdrv_get_info) 2990faea38e7Sbellard return -ENOTSUP; 2991faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 2992faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 2993faea38e7Sbellard } 2994faea38e7Sbellard 299545566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 299645566e9cSChristoph Hellwig int64_t pos, int size) 2997178e08a5Saliguori { 2998178e08a5Saliguori BlockDriver *drv = bs->drv; 2999178e08a5Saliguori if (!drv) 3000178e08a5Saliguori return -ENOMEDIUM; 30017cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 300245566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 30037cdb1f6dSMORITA Kazutaka if (bs->file) 30047cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 30057cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3006178e08a5Saliguori } 3007178e08a5Saliguori 300845566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 300945566e9cSChristoph Hellwig int64_t pos, int size) 3010178e08a5Saliguori { 3011178e08a5Saliguori BlockDriver *drv = bs->drv; 3012178e08a5Saliguori if (!drv) 3013178e08a5Saliguori return -ENOMEDIUM; 30147cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 301545566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 30167cdb1f6dSMORITA Kazutaka if (bs->file) 30177cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 30187cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3019178e08a5Saliguori } 3020178e08a5Saliguori 30218b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 30228b9b0cc2SKevin Wolf { 30238b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 30248b9b0cc2SKevin Wolf 30258b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 30268b9b0cc2SKevin Wolf return; 30278b9b0cc2SKevin Wolf } 30288b9b0cc2SKevin Wolf 30290ed8b6f6SBlue Swirl drv->bdrv_debug_event(bs, event); 30308b9b0cc2SKevin Wolf 30318b9b0cc2SKevin Wolf } 30328b9b0cc2SKevin Wolf 3033faea38e7Sbellard /**************************************************************/ 3034faea38e7Sbellard /* handling of snapshots */ 3035faea38e7Sbellard 3036feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 3037feeee5acSMiguel Di Ciurcio Filho { 3038feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 303907b70bfbSMarkus Armbruster if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 3040feeee5acSMiguel Di Ciurcio Filho return 0; 3041feeee5acSMiguel Di Ciurcio Filho } 3042feeee5acSMiguel Di Ciurcio Filho 3043feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 3044feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 3045feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 3046feeee5acSMiguel Di Ciurcio Filho } 3047feeee5acSMiguel Di Ciurcio Filho return 0; 3048feeee5acSMiguel Di Ciurcio Filho } 3049feeee5acSMiguel Di Ciurcio Filho 3050feeee5acSMiguel Di Ciurcio Filho return 1; 3051feeee5acSMiguel Di Ciurcio Filho } 3052feeee5acSMiguel Di Ciurcio Filho 3053199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 3054199630b6SBlue Swirl { 3055199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 3056199630b6SBlue Swirl } 3057199630b6SBlue Swirl 3058f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 3059f9092b10SMarkus Armbruster { 3060f9092b10SMarkus Armbruster BlockDriverState *bs; 3061f9092b10SMarkus Armbruster 30623ac906f7SMarkus Armbruster if (bs_snapshots) { 3063f9092b10SMarkus Armbruster return bs_snapshots; 30643ac906f7SMarkus Armbruster } 3065f9092b10SMarkus Armbruster 3066f9092b10SMarkus Armbruster bs = NULL; 3067f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 3068f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 30693ac906f7SMarkus Armbruster bs_snapshots = bs; 30703ac906f7SMarkus Armbruster return bs; 3071f9092b10SMarkus Armbruster } 3072f9092b10SMarkus Armbruster } 3073f9092b10SMarkus Armbruster return NULL; 3074f9092b10SMarkus Armbruster } 3075f9092b10SMarkus Armbruster 3076faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 3077faea38e7Sbellard QEMUSnapshotInfo *sn_info) 3078faea38e7Sbellard { 3079faea38e7Sbellard BlockDriver *drv = bs->drv; 3080faea38e7Sbellard if (!drv) 308119cb3738Sbellard return -ENOMEDIUM; 30827cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 3083faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 30847cdb1f6dSMORITA Kazutaka if (bs->file) 30857cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 30867cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3087faea38e7Sbellard } 3088faea38e7Sbellard 3089faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 3090faea38e7Sbellard const char *snapshot_id) 3091faea38e7Sbellard { 3092faea38e7Sbellard BlockDriver *drv = bs->drv; 30937cdb1f6dSMORITA Kazutaka int ret, open_ret; 30947cdb1f6dSMORITA Kazutaka 3095faea38e7Sbellard if (!drv) 309619cb3738Sbellard return -ENOMEDIUM; 30977cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 3098faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 30997cdb1f6dSMORITA Kazutaka 31007cdb1f6dSMORITA Kazutaka if (bs->file) { 31017cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 31027cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 31037cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 31047cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 31057cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 31067cdb1f6dSMORITA Kazutaka bs->drv = NULL; 31077cdb1f6dSMORITA Kazutaka return open_ret; 31087cdb1f6dSMORITA Kazutaka } 31097cdb1f6dSMORITA Kazutaka return ret; 31107cdb1f6dSMORITA Kazutaka } 31117cdb1f6dSMORITA Kazutaka 31127cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3113faea38e7Sbellard } 3114faea38e7Sbellard 3115faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 3116faea38e7Sbellard { 3117faea38e7Sbellard BlockDriver *drv = bs->drv; 3118faea38e7Sbellard if (!drv) 311919cb3738Sbellard return -ENOMEDIUM; 31207cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 3121faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 31227cdb1f6dSMORITA Kazutaka if (bs->file) 31237cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 31247cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3125faea38e7Sbellard } 3126faea38e7Sbellard 3127faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 3128faea38e7Sbellard QEMUSnapshotInfo **psn_info) 3129faea38e7Sbellard { 3130faea38e7Sbellard BlockDriver *drv = bs->drv; 3131faea38e7Sbellard if (!drv) 313219cb3738Sbellard return -ENOMEDIUM; 31337cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 3134faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 31357cdb1f6dSMORITA Kazutaka if (bs->file) 31367cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 31377cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3138faea38e7Sbellard } 3139faea38e7Sbellard 314051ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 314151ef6727Sedison const char *snapshot_name) 314251ef6727Sedison { 314351ef6727Sedison BlockDriver *drv = bs->drv; 314451ef6727Sedison if (!drv) { 314551ef6727Sedison return -ENOMEDIUM; 314651ef6727Sedison } 314751ef6727Sedison if (!bs->read_only) { 314851ef6727Sedison return -EINVAL; 314951ef6727Sedison } 315051ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 315151ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 315251ef6727Sedison } 315351ef6727Sedison return -ENOTSUP; 315451ef6727Sedison } 315551ef6727Sedison 3156b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 3157b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 3158b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 3159b1b1d783SJeff Cody * the CWD rather than the chain. */ 3160e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 3161e8a6bb9cSMarcelo Tosatti const char *backing_file) 3162e8a6bb9cSMarcelo Tosatti { 3163b1b1d783SJeff Cody char *filename_full = NULL; 3164b1b1d783SJeff Cody char *backing_file_full = NULL; 3165b1b1d783SJeff Cody char *filename_tmp = NULL; 3166b1b1d783SJeff Cody int is_protocol = 0; 3167b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 3168b1b1d783SJeff Cody BlockDriverState *retval = NULL; 3169b1b1d783SJeff Cody 3170b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 3171e8a6bb9cSMarcelo Tosatti return NULL; 3172e8a6bb9cSMarcelo Tosatti } 3173e8a6bb9cSMarcelo Tosatti 3174b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 3175b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 3176b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 3177b1b1d783SJeff Cody 3178b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 3179b1b1d783SJeff Cody 3180b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 3181b1b1d783SJeff Cody 3182b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 3183b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 3184b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 3185b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 3186b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3187b1b1d783SJeff Cody break; 3188b1b1d783SJeff Cody } 3189e8a6bb9cSMarcelo Tosatti } else { 3190b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 3191b1b1d783SJeff Cody * image's filename path */ 3192b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3193b1b1d783SJeff Cody backing_file); 3194b1b1d783SJeff Cody 3195b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 3196b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 3197b1b1d783SJeff Cody continue; 3198b1b1d783SJeff Cody } 3199b1b1d783SJeff Cody 3200b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 3201b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 3202b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3203b1b1d783SJeff Cody curr_bs->backing_file); 3204b1b1d783SJeff Cody 3205b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 3206b1b1d783SJeff Cody continue; 3207b1b1d783SJeff Cody } 3208b1b1d783SJeff Cody 3209b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 3210b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3211b1b1d783SJeff Cody break; 3212b1b1d783SJeff Cody } 3213e8a6bb9cSMarcelo Tosatti } 3214e8a6bb9cSMarcelo Tosatti } 3215e8a6bb9cSMarcelo Tosatti 3216b1b1d783SJeff Cody g_free(filename_full); 3217b1b1d783SJeff Cody g_free(backing_file_full); 3218b1b1d783SJeff Cody g_free(filename_tmp); 3219b1b1d783SJeff Cody return retval; 3220e8a6bb9cSMarcelo Tosatti } 3221e8a6bb9cSMarcelo Tosatti 3222f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 3223f198fd1cSBenoît Canet { 3224f198fd1cSBenoît Canet if (!bs->drv) { 3225f198fd1cSBenoît Canet return 0; 3226f198fd1cSBenoît Canet } 3227f198fd1cSBenoît Canet 3228f198fd1cSBenoît Canet if (!bs->backing_hd) { 3229f198fd1cSBenoît Canet return 0; 3230f198fd1cSBenoît Canet } 3231f198fd1cSBenoît Canet 3232f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 3233f198fd1cSBenoît Canet } 3234f198fd1cSBenoît Canet 323579fac568SJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 323679fac568SJeff Cody { 323779fac568SJeff Cody BlockDriverState *curr_bs = NULL; 323879fac568SJeff Cody 323979fac568SJeff Cody if (!bs) { 324079fac568SJeff Cody return NULL; 324179fac568SJeff Cody } 324279fac568SJeff Cody 324379fac568SJeff Cody curr_bs = bs; 324479fac568SJeff Cody 324579fac568SJeff Cody while (curr_bs->backing_hd) { 324679fac568SJeff Cody curr_bs = curr_bs->backing_hd; 324779fac568SJeff Cody } 324879fac568SJeff Cody return curr_bs; 324979fac568SJeff Cody } 325079fac568SJeff Cody 3251faea38e7Sbellard #define NB_SUFFIXES 4 3252faea38e7Sbellard 3253faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 3254faea38e7Sbellard { 3255faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 3256faea38e7Sbellard int64_t base; 3257faea38e7Sbellard int i; 3258faea38e7Sbellard 3259faea38e7Sbellard if (size <= 999) { 3260faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 3261faea38e7Sbellard } else { 3262faea38e7Sbellard base = 1024; 3263faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 3264faea38e7Sbellard if (size < (10 * base)) { 3265faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 3266faea38e7Sbellard (double)size / base, 3267faea38e7Sbellard suffixes[i]); 3268faea38e7Sbellard break; 3269faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 3270faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 3271faea38e7Sbellard ((size + (base >> 1)) / base), 3272faea38e7Sbellard suffixes[i]); 3273faea38e7Sbellard break; 3274faea38e7Sbellard } 3275faea38e7Sbellard base = base * 1024; 3276faea38e7Sbellard } 3277faea38e7Sbellard } 3278faea38e7Sbellard return buf; 3279faea38e7Sbellard } 3280faea38e7Sbellard 3281faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 3282faea38e7Sbellard { 3283faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 32843b9f94e1Sbellard #ifdef _WIN32 32853b9f94e1Sbellard struct tm *ptm; 32863b9f94e1Sbellard #else 3287faea38e7Sbellard struct tm tm; 32883b9f94e1Sbellard #endif 3289faea38e7Sbellard time_t ti; 3290faea38e7Sbellard int64_t secs; 3291faea38e7Sbellard 3292faea38e7Sbellard if (!sn) { 3293faea38e7Sbellard snprintf(buf, buf_size, 3294faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 3295faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 3296faea38e7Sbellard } else { 3297faea38e7Sbellard ti = sn->date_sec; 32983b9f94e1Sbellard #ifdef _WIN32 32993b9f94e1Sbellard ptm = localtime(&ti); 33003b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 33013b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 33023b9f94e1Sbellard #else 3303faea38e7Sbellard localtime_r(&ti, &tm); 3304faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 3305faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 33063b9f94e1Sbellard #endif 3307faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 3308faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 3309faea38e7Sbellard "%02d:%02d:%02d.%03d", 3310faea38e7Sbellard (int)(secs / 3600), 3311faea38e7Sbellard (int)((secs / 60) % 60), 3312faea38e7Sbellard (int)(secs % 60), 3313faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 3314faea38e7Sbellard snprintf(buf, buf_size, 3315faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 3316faea38e7Sbellard sn->id_str, sn->name, 3317faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 3318faea38e7Sbellard date_buf, 3319faea38e7Sbellard clock_buf); 3320faea38e7Sbellard } 3321faea38e7Sbellard return buf; 3322faea38e7Sbellard } 3323faea38e7Sbellard 3324ea2384d3Sbellard /**************************************************************/ 332583f64091Sbellard /* async I/Os */ 3326ea2384d3Sbellard 33273b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 3328f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 332983f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 3330ea2384d3Sbellard { 3331bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 3332bbf0a440SStefan Hajnoczi 3333b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 33348c5873d6SStefan Hajnoczi cb, opaque, false); 333583f64091Sbellard } 333683f64091Sbellard 3337f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 3338f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 333983f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 33407674e7bfSbellard { 3341bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 3342bbf0a440SStefan Hajnoczi 33431a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 33448c5873d6SStefan Hajnoczi cb, opaque, true); 334583f64091Sbellard } 334683f64091Sbellard 334740b4f539SKevin Wolf 334840b4f539SKevin Wolf typedef struct MultiwriteCB { 334940b4f539SKevin Wolf int error; 335040b4f539SKevin Wolf int num_requests; 335140b4f539SKevin Wolf int num_callbacks; 335240b4f539SKevin Wolf struct { 335340b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 335440b4f539SKevin Wolf void *opaque; 335540b4f539SKevin Wolf QEMUIOVector *free_qiov; 335640b4f539SKevin Wolf } callbacks[]; 335740b4f539SKevin Wolf } MultiwriteCB; 335840b4f539SKevin Wolf 335940b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 336040b4f539SKevin Wolf { 336140b4f539SKevin Wolf int i; 336240b4f539SKevin Wolf 336340b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 336440b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 33651e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 33661e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 33671e1ea48dSStefan Hajnoczi } 33687267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 336940b4f539SKevin Wolf } 337040b4f539SKevin Wolf } 337140b4f539SKevin Wolf 337240b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 337340b4f539SKevin Wolf { 337440b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 337540b4f539SKevin Wolf 33766d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 33776d519a5fSStefan Hajnoczi 3378cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 337940b4f539SKevin Wolf mcb->error = ret; 338040b4f539SKevin Wolf } 338140b4f539SKevin Wolf 338240b4f539SKevin Wolf mcb->num_requests--; 338340b4f539SKevin Wolf if (mcb->num_requests == 0) { 338440b4f539SKevin Wolf multiwrite_user_cb(mcb); 33857267c094SAnthony Liguori g_free(mcb); 338640b4f539SKevin Wolf } 338740b4f539SKevin Wolf } 338840b4f539SKevin Wolf 338940b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 339040b4f539SKevin Wolf { 339177be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 339277be4366SChristoph Hellwig 339377be4366SChristoph Hellwig /* 339477be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 339577be4366SChristoph Hellwig * here as that could overflow the return value. 339677be4366SChristoph Hellwig */ 339777be4366SChristoph Hellwig if (req1->sector > req2->sector) { 339877be4366SChristoph Hellwig return 1; 339977be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 340077be4366SChristoph Hellwig return -1; 340177be4366SChristoph Hellwig } else { 340277be4366SChristoph Hellwig return 0; 340377be4366SChristoph Hellwig } 340440b4f539SKevin Wolf } 340540b4f539SKevin Wolf 340640b4f539SKevin Wolf /* 340740b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 340840b4f539SKevin Wolf * requests that remain after merging. 340940b4f539SKevin Wolf */ 341040b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 341140b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 341240b4f539SKevin Wolf { 341340b4f539SKevin Wolf int i, outidx; 341440b4f539SKevin Wolf 341540b4f539SKevin Wolf // Sort requests by start sector 341640b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 341740b4f539SKevin Wolf 341840b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 341940b4f539SKevin Wolf // filling up gaps with zero sectors. 342040b4f539SKevin Wolf outidx = 0; 342140b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 342240b4f539SKevin Wolf int merge = 0; 342340b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 342440b4f539SKevin Wolf 3425b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 342640b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 342740b4f539SKevin Wolf merge = 1; 342840b4f539SKevin Wolf } 342940b4f539SKevin Wolf 3430e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3431e2a305fbSChristoph Hellwig merge = 0; 3432e2a305fbSChristoph Hellwig } 3433e2a305fbSChristoph Hellwig 343440b4f539SKevin Wolf if (merge) { 343540b4f539SKevin Wolf size_t size; 34367267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 343740b4f539SKevin Wolf qemu_iovec_init(qiov, 343840b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 343940b4f539SKevin Wolf 344040b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 344140b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 344240b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 34431b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 344440b4f539SKevin Wolf 3445b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3446b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 344740b4f539SKevin Wolf 344840b4f539SKevin Wolf // Add the second request 34491b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 345040b4f539SKevin Wolf 3451cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 345240b4f539SKevin Wolf reqs[outidx].qiov = qiov; 345340b4f539SKevin Wolf 345440b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 345540b4f539SKevin Wolf } else { 345640b4f539SKevin Wolf outidx++; 345740b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 345840b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 345940b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 346040b4f539SKevin Wolf } 346140b4f539SKevin Wolf } 346240b4f539SKevin Wolf 346340b4f539SKevin Wolf return outidx + 1; 346440b4f539SKevin Wolf } 346540b4f539SKevin Wolf 346640b4f539SKevin Wolf /* 346740b4f539SKevin Wolf * Submit multiple AIO write requests at once. 346840b4f539SKevin Wolf * 346940b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 347040b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 347140b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 347240b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 347340b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 347440b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 347540b4f539SKevin Wolf * 347640b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 347740b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 347840b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 347940b4f539SKevin Wolf */ 348040b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 348140b4f539SKevin Wolf { 348240b4f539SKevin Wolf MultiwriteCB *mcb; 348340b4f539SKevin Wolf int i; 348440b4f539SKevin Wolf 3485301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3486301db7c2SRyan Harper if (bs->drv == NULL) { 3487301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3488301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3489301db7c2SRyan Harper } 3490301db7c2SRyan Harper return -1; 3491301db7c2SRyan Harper } 3492301db7c2SRyan Harper 349340b4f539SKevin Wolf if (num_reqs == 0) { 349440b4f539SKevin Wolf return 0; 349540b4f539SKevin Wolf } 349640b4f539SKevin Wolf 349740b4f539SKevin Wolf // Create MultiwriteCB structure 34987267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 349940b4f539SKevin Wolf mcb->num_requests = 0; 350040b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 350140b4f539SKevin Wolf 350240b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 350340b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 350440b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 350540b4f539SKevin Wolf } 350640b4f539SKevin Wolf 350740b4f539SKevin Wolf // Check for mergable requests 350840b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 350940b4f539SKevin Wolf 35106d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 35116d519a5fSStefan Hajnoczi 3512df9309fbSPaolo Bonzini /* Run the aio requests. */ 3513df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 351440b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3515ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 351640b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 351740b4f539SKevin Wolf } 351840b4f539SKevin Wolf 351940b4f539SKevin Wolf return 0; 352040b4f539SKevin Wolf } 352140b4f539SKevin Wolf 352283f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 352383f64091Sbellard { 35246bbff9a0Saliguori acb->pool->cancel(acb); 352583f64091Sbellard } 352683f64091Sbellard 352798f90dbaSZhi Yong Wu /* block I/O throttling */ 352898f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 352998f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait) 353098f90dbaSZhi Yong Wu { 353198f90dbaSZhi Yong Wu uint64_t bps_limit = 0; 353298f90dbaSZhi Yong Wu double bytes_limit, bytes_base, bytes_res; 353398f90dbaSZhi Yong Wu double slice_time, wait_time; 353498f90dbaSZhi Yong Wu 353598f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 353698f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 353798f90dbaSZhi Yong Wu } else if (bs->io_limits.bps[is_write]) { 353898f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[is_write]; 353998f90dbaSZhi Yong Wu } else { 354098f90dbaSZhi Yong Wu if (wait) { 354198f90dbaSZhi Yong Wu *wait = 0; 354298f90dbaSZhi Yong Wu } 354398f90dbaSZhi Yong Wu 354498f90dbaSZhi Yong Wu return false; 354598f90dbaSZhi Yong Wu } 354698f90dbaSZhi Yong Wu 354798f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 354898f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 354998f90dbaSZhi Yong Wu bytes_limit = bps_limit * slice_time; 355098f90dbaSZhi Yong Wu bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; 355198f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 355298f90dbaSZhi Yong Wu bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; 355398f90dbaSZhi Yong Wu } 355498f90dbaSZhi Yong Wu 355598f90dbaSZhi Yong Wu /* bytes_base: the bytes of data which have been read/written; and 355698f90dbaSZhi Yong Wu * it is obtained from the history statistic info. 355798f90dbaSZhi Yong Wu * bytes_res: the remaining bytes of data which need to be read/written. 355898f90dbaSZhi Yong Wu * (bytes_base + bytes_res) / bps_limit: used to calcuate 355998f90dbaSZhi Yong Wu * the total time for completing reading/writting all data. 356098f90dbaSZhi Yong Wu */ 356198f90dbaSZhi Yong Wu bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 356298f90dbaSZhi Yong Wu 356398f90dbaSZhi Yong Wu if (bytes_base + bytes_res <= bytes_limit) { 356498f90dbaSZhi Yong Wu if (wait) { 356598f90dbaSZhi Yong Wu *wait = 0; 356698f90dbaSZhi Yong Wu } 356798f90dbaSZhi Yong Wu 356898f90dbaSZhi Yong Wu return false; 356998f90dbaSZhi Yong Wu } 357098f90dbaSZhi Yong Wu 357198f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 357298f90dbaSZhi Yong Wu wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; 357398f90dbaSZhi Yong Wu 357498f90dbaSZhi Yong Wu /* When the I/O rate at runtime exceeds the limits, 357598f90dbaSZhi Yong Wu * bs->slice_end need to be extended in order that the current statistic 357698f90dbaSZhi Yong Wu * info can be kept until the timer fire, so it is increased and tuned 357798f90dbaSZhi Yong Wu * based on the result of experiment. 357898f90dbaSZhi Yong Wu */ 357998f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 358098f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 358198f90dbaSZhi Yong Wu if (wait) { 358298f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 358398f90dbaSZhi Yong Wu } 358498f90dbaSZhi Yong Wu 358598f90dbaSZhi Yong Wu return true; 358698f90dbaSZhi Yong Wu } 358798f90dbaSZhi Yong Wu 358898f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 358998f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait) 359098f90dbaSZhi Yong Wu { 359198f90dbaSZhi Yong Wu uint64_t iops_limit = 0; 359298f90dbaSZhi Yong Wu double ios_limit, ios_base; 359398f90dbaSZhi Yong Wu double slice_time, wait_time; 359498f90dbaSZhi Yong Wu 359598f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 359698f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 359798f90dbaSZhi Yong Wu } else if (bs->io_limits.iops[is_write]) { 359898f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[is_write]; 359998f90dbaSZhi Yong Wu } else { 360098f90dbaSZhi Yong Wu if (wait) { 360198f90dbaSZhi Yong Wu *wait = 0; 360298f90dbaSZhi Yong Wu } 360398f90dbaSZhi Yong Wu 360498f90dbaSZhi Yong Wu return false; 360598f90dbaSZhi Yong Wu } 360698f90dbaSZhi Yong Wu 360798f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 360898f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 360998f90dbaSZhi Yong Wu ios_limit = iops_limit * slice_time; 361098f90dbaSZhi Yong Wu ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; 361198f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 361298f90dbaSZhi Yong Wu ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; 361398f90dbaSZhi Yong Wu } 361498f90dbaSZhi Yong Wu 361598f90dbaSZhi Yong Wu if (ios_base + 1 <= ios_limit) { 361698f90dbaSZhi Yong Wu if (wait) { 361798f90dbaSZhi Yong Wu *wait = 0; 361898f90dbaSZhi Yong Wu } 361998f90dbaSZhi Yong Wu 362098f90dbaSZhi Yong Wu return false; 362198f90dbaSZhi Yong Wu } 362298f90dbaSZhi Yong Wu 362398f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 362498f90dbaSZhi Yong Wu wait_time = (ios_base + 1) / iops_limit; 362598f90dbaSZhi Yong Wu if (wait_time > elapsed_time) { 362698f90dbaSZhi Yong Wu wait_time = wait_time - elapsed_time; 362798f90dbaSZhi Yong Wu } else { 362898f90dbaSZhi Yong Wu wait_time = 0; 362998f90dbaSZhi Yong Wu } 363098f90dbaSZhi Yong Wu 363198f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 363298f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 363398f90dbaSZhi Yong Wu if (wait) { 363498f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 363598f90dbaSZhi Yong Wu } 363698f90dbaSZhi Yong Wu 363798f90dbaSZhi Yong Wu return true; 363898f90dbaSZhi Yong Wu } 363998f90dbaSZhi Yong Wu 364098f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 364198f90dbaSZhi Yong Wu bool is_write, int64_t *wait) 364298f90dbaSZhi Yong Wu { 364398f90dbaSZhi Yong Wu int64_t now, max_wait; 364498f90dbaSZhi Yong Wu uint64_t bps_wait = 0, iops_wait = 0; 364598f90dbaSZhi Yong Wu double elapsed_time; 364698f90dbaSZhi Yong Wu int bps_ret, iops_ret; 364798f90dbaSZhi Yong Wu 364898f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 364998f90dbaSZhi Yong Wu if ((bs->slice_start < now) 365098f90dbaSZhi Yong Wu && (bs->slice_end > now)) { 365198f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 365298f90dbaSZhi Yong Wu } else { 365398f90dbaSZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 365498f90dbaSZhi Yong Wu bs->slice_start = now; 365598f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 365698f90dbaSZhi Yong Wu 365798f90dbaSZhi Yong Wu bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; 365898f90dbaSZhi Yong Wu bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; 365998f90dbaSZhi Yong Wu 366098f90dbaSZhi Yong Wu bs->io_base.ios[is_write] = bs->nr_ops[is_write]; 366198f90dbaSZhi Yong Wu bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; 366298f90dbaSZhi Yong Wu } 366398f90dbaSZhi Yong Wu 366498f90dbaSZhi Yong Wu elapsed_time = now - bs->slice_start; 366598f90dbaSZhi Yong Wu elapsed_time /= (NANOSECONDS_PER_SECOND); 366698f90dbaSZhi Yong Wu 366798f90dbaSZhi Yong Wu bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, 366898f90dbaSZhi Yong Wu is_write, elapsed_time, &bps_wait); 366998f90dbaSZhi Yong Wu iops_ret = bdrv_exceed_iops_limits(bs, is_write, 367098f90dbaSZhi Yong Wu elapsed_time, &iops_wait); 367198f90dbaSZhi Yong Wu if (bps_ret || iops_ret) { 367298f90dbaSZhi Yong Wu max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; 367398f90dbaSZhi Yong Wu if (wait) { 367498f90dbaSZhi Yong Wu *wait = max_wait; 367598f90dbaSZhi Yong Wu } 367698f90dbaSZhi Yong Wu 367798f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 367898f90dbaSZhi Yong Wu if (bs->slice_end < now + max_wait) { 367998f90dbaSZhi Yong Wu bs->slice_end = now + max_wait; 368098f90dbaSZhi Yong Wu } 368198f90dbaSZhi Yong Wu 368298f90dbaSZhi Yong Wu return true; 368398f90dbaSZhi Yong Wu } 368498f90dbaSZhi Yong Wu 368598f90dbaSZhi Yong Wu if (wait) { 368698f90dbaSZhi Yong Wu *wait = 0; 368798f90dbaSZhi Yong Wu } 368898f90dbaSZhi Yong Wu 368998f90dbaSZhi Yong Wu return false; 369098f90dbaSZhi Yong Wu } 369183f64091Sbellard 369283f64091Sbellard /**************************************************************/ 369383f64091Sbellard /* async block device emulation */ 369483f64091Sbellard 3695c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3696c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3697c16b5a2cSChristoph Hellwig QEMUBH *bh; 3698c16b5a2cSChristoph Hellwig int ret; 3699c16b5a2cSChristoph Hellwig /* vector translation state */ 3700c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3701c16b5a2cSChristoph Hellwig uint8_t *bounce; 3702c16b5a2cSChristoph Hellwig int is_write; 3703c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3704c16b5a2cSChristoph Hellwig 3705c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3706c16b5a2cSChristoph Hellwig { 3707b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3708b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 37096a7ad299SDor Laor qemu_bh_delete(acb->bh); 371036afc451SAvi Kivity acb->bh = NULL; 3711c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3712c16b5a2cSChristoph Hellwig } 3713c16b5a2cSChristoph Hellwig 3714c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 3715c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3716c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3717c16b5a2cSChristoph Hellwig }; 3718c16b5a2cSChristoph Hellwig 371983f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3720beac80cdSbellard { 3721ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3722f141eafeSaliguori 3723f141eafeSaliguori if (!acb->is_write) 372403396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 3725ceb42de8Saliguori qemu_vfree(acb->bounce); 3726ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 37276a7ad299SDor Laor qemu_bh_delete(acb->bh); 372836afc451SAvi Kivity acb->bh = NULL; 3729ce1a14dcSpbrook qemu_aio_release(acb); 3730beac80cdSbellard } 3731beac80cdSbellard 3732f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3733f141eafeSaliguori int64_t sector_num, 3734f141eafeSaliguori QEMUIOVector *qiov, 3735f141eafeSaliguori int nb_sectors, 3736f141eafeSaliguori BlockDriverCompletionFunc *cb, 3737f141eafeSaliguori void *opaque, 3738f141eafeSaliguori int is_write) 3739f141eafeSaliguori 3740ea2384d3Sbellard { 3741ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 374283f64091Sbellard 3743c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 3744f141eafeSaliguori acb->is_write = is_write; 3745f141eafeSaliguori acb->qiov = qiov; 3746e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3747ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3748f141eafeSaliguori 3749f141eafeSaliguori if (is_write) { 3750d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 37511ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3752f141eafeSaliguori } else { 37531ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3754f141eafeSaliguori } 3755f141eafeSaliguori 3756ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3757f141eafeSaliguori 3758ce1a14dcSpbrook return &acb->common; 37597a6cba61Spbrook } 37607a6cba61Spbrook 3761f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3762f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3763ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 376483f64091Sbellard { 3765f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 376683f64091Sbellard } 376783f64091Sbellard 3768f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3769f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3770f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3771f141eafeSaliguori { 3772f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3773f141eafeSaliguori } 3774f141eafeSaliguori 377568485420SKevin Wolf 377668485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 377768485420SKevin Wolf BlockDriverAIOCB common; 377868485420SKevin Wolf BlockRequest req; 377968485420SKevin Wolf bool is_write; 378068485420SKevin Wolf QEMUBH* bh; 378168485420SKevin Wolf } BlockDriverAIOCBCoroutine; 378268485420SKevin Wolf 378368485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 378468485420SKevin Wolf { 378568485420SKevin Wolf qemu_aio_flush(); 378668485420SKevin Wolf } 378768485420SKevin Wolf 378868485420SKevin Wolf static AIOPool bdrv_em_co_aio_pool = { 378968485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 379068485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 379168485420SKevin Wolf }; 379268485420SKevin Wolf 379335246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 379468485420SKevin Wolf { 379568485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 379668485420SKevin Wolf 379768485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 379868485420SKevin Wolf qemu_bh_delete(acb->bh); 379968485420SKevin Wolf qemu_aio_release(acb); 380068485420SKevin Wolf } 380168485420SKevin Wolf 3802b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3803b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3804b2a61371SStefan Hajnoczi { 3805b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3806b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3807b2a61371SStefan Hajnoczi 3808b2a61371SStefan Hajnoczi if (!acb->is_write) { 3809b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 3810470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3811b2a61371SStefan Hajnoczi } else { 3812b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 3813f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3814b2a61371SStefan Hajnoczi } 3815b2a61371SStefan Hajnoczi 381635246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3817b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 3818b2a61371SStefan Hajnoczi } 3819b2a61371SStefan Hajnoczi 382068485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 382168485420SKevin Wolf int64_t sector_num, 382268485420SKevin Wolf QEMUIOVector *qiov, 382368485420SKevin Wolf int nb_sectors, 382468485420SKevin Wolf BlockDriverCompletionFunc *cb, 382568485420SKevin Wolf void *opaque, 38268c5873d6SStefan Hajnoczi bool is_write) 382768485420SKevin Wolf { 382868485420SKevin Wolf Coroutine *co; 382968485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 383068485420SKevin Wolf 383168485420SKevin Wolf acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 383268485420SKevin Wolf acb->req.sector = sector_num; 383368485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 383468485420SKevin Wolf acb->req.qiov = qiov; 383568485420SKevin Wolf acb->is_write = is_write; 383668485420SKevin Wolf 38378c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 383868485420SKevin Wolf qemu_coroutine_enter(co, acb); 383968485420SKevin Wolf 384068485420SKevin Wolf return &acb->common; 384168485420SKevin Wolf } 384268485420SKevin Wolf 384307f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 3844b2e12bc6SChristoph Hellwig { 384507f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 384607f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 3847b2e12bc6SChristoph Hellwig 384807f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 384907f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3850b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 3851b2e12bc6SChristoph Hellwig } 3852b2e12bc6SChristoph Hellwig 385307f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 3854016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 3855016f5cf6SAlexander Graf { 385607f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 3857016f5cf6SAlexander Graf 385807f07615SPaolo Bonzini Coroutine *co; 385907f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 3860016f5cf6SAlexander Graf 386107f07615SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 386207f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 386307f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 3864016f5cf6SAlexander Graf 3865016f5cf6SAlexander Graf return &acb->common; 3866016f5cf6SAlexander Graf } 3867016f5cf6SAlexander Graf 38684265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 38694265d620SPaolo Bonzini { 38704265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 38714265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 38724265d620SPaolo Bonzini 38734265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 38744265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 38754265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 38764265d620SPaolo Bonzini } 38774265d620SPaolo Bonzini 38784265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 38794265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 38804265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 38814265d620SPaolo Bonzini { 38824265d620SPaolo Bonzini Coroutine *co; 38834265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 38844265d620SPaolo Bonzini 38854265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 38864265d620SPaolo Bonzini 38874265d620SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 38884265d620SPaolo Bonzini acb->req.sector = sector_num; 38894265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 38904265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 38914265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 38924265d620SPaolo Bonzini 38934265d620SPaolo Bonzini return &acb->common; 38944265d620SPaolo Bonzini } 38954265d620SPaolo Bonzini 3896ea2384d3Sbellard void bdrv_init(void) 3897ea2384d3Sbellard { 38985efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3899ea2384d3Sbellard } 3900ce1a14dcSpbrook 3901eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3902eb852011SMarkus Armbruster { 3903eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3904eb852011SMarkus Armbruster bdrv_init(); 3905eb852011SMarkus Armbruster } 3906eb852011SMarkus Armbruster 3907c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 39086bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 39096bbff9a0Saliguori { 3910ce1a14dcSpbrook BlockDriverAIOCB *acb; 3911ce1a14dcSpbrook 3912*d37c975fSStefan Hajnoczi acb = g_slice_alloc(pool->aiocb_size); 39136bbff9a0Saliguori acb->pool = pool; 3914ce1a14dcSpbrook acb->bs = bs; 3915ce1a14dcSpbrook acb->cb = cb; 3916ce1a14dcSpbrook acb->opaque = opaque; 3917ce1a14dcSpbrook return acb; 3918ce1a14dcSpbrook } 3919ce1a14dcSpbrook 3920ce1a14dcSpbrook void qemu_aio_release(void *p) 3921ce1a14dcSpbrook { 3922*d37c975fSStefan Hajnoczi BlockDriverAIOCB *acb = p; 3923*d37c975fSStefan Hajnoczi g_slice_free1(acb->pool->aiocb_size, acb); 3924ce1a14dcSpbrook } 392519cb3738Sbellard 392619cb3738Sbellard /**************************************************************/ 3927f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 3928f9f05dc5SKevin Wolf 3929f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 3930f9f05dc5SKevin Wolf Coroutine *coroutine; 3931f9f05dc5SKevin Wolf int ret; 3932f9f05dc5SKevin Wolf } CoroutineIOCompletion; 3933f9f05dc5SKevin Wolf 3934f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 3935f9f05dc5SKevin Wolf { 3936f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 3937f9f05dc5SKevin Wolf 3938f9f05dc5SKevin Wolf co->ret = ret; 3939f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 3940f9f05dc5SKevin Wolf } 3941f9f05dc5SKevin Wolf 3942f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 3943f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 3944f9f05dc5SKevin Wolf bool is_write) 3945f9f05dc5SKevin Wolf { 3946f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 3947f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 3948f9f05dc5SKevin Wolf }; 3949f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 3950f9f05dc5SKevin Wolf 3951f9f05dc5SKevin Wolf if (is_write) { 3952a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 3953f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3954f9f05dc5SKevin Wolf } else { 3955a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 3956f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3957f9f05dc5SKevin Wolf } 3958f9f05dc5SKevin Wolf 395959370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 3960f9f05dc5SKevin Wolf if (!acb) { 3961f9f05dc5SKevin Wolf return -EIO; 3962f9f05dc5SKevin Wolf } 3963f9f05dc5SKevin Wolf qemu_coroutine_yield(); 3964f9f05dc5SKevin Wolf 3965f9f05dc5SKevin Wolf return co.ret; 3966f9f05dc5SKevin Wolf } 3967f9f05dc5SKevin Wolf 3968f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 3969f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3970f9f05dc5SKevin Wolf QEMUIOVector *iov) 3971f9f05dc5SKevin Wolf { 3972f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 3973f9f05dc5SKevin Wolf } 3974f9f05dc5SKevin Wolf 3975f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 3976f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3977f9f05dc5SKevin Wolf QEMUIOVector *iov) 3978f9f05dc5SKevin Wolf { 3979f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 3980f9f05dc5SKevin Wolf } 3981f9f05dc5SKevin Wolf 398207f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 3983e7a8a783SKevin Wolf { 398407f07615SPaolo Bonzini RwCo *rwco = opaque; 398507f07615SPaolo Bonzini 398607f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 398707f07615SPaolo Bonzini } 398807f07615SPaolo Bonzini 398907f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 399007f07615SPaolo Bonzini { 3991eb489bb1SKevin Wolf int ret; 3992eb489bb1SKevin Wolf 399329cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 399407f07615SPaolo Bonzini return 0; 3995eb489bb1SKevin Wolf } 3996eb489bb1SKevin Wolf 3997ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 3998eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 3999eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 4000eb489bb1SKevin Wolf if (ret < 0) { 4001eb489bb1SKevin Wolf return ret; 4002eb489bb1SKevin Wolf } 4003eb489bb1SKevin Wolf } 4004eb489bb1SKevin Wolf 4005ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 4006ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 4007d4c82329SKevin Wolf goto flush_parent; 4008ca716364SKevin Wolf } 4009ca716364SKevin Wolf 4010eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 401129cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 401207f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 401307f07615SPaolo Bonzini BlockDriverAIOCB *acb; 4014e7a8a783SKevin Wolf CoroutineIOCompletion co = { 4015e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 4016e7a8a783SKevin Wolf }; 4017e7a8a783SKevin Wolf 401807f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 401907f07615SPaolo Bonzini if (acb == NULL) { 402029cdb251SPaolo Bonzini ret = -EIO; 402107f07615SPaolo Bonzini } else { 4022e7a8a783SKevin Wolf qemu_coroutine_yield(); 402329cdb251SPaolo Bonzini ret = co.ret; 4024e7a8a783SKevin Wolf } 402507f07615SPaolo Bonzini } else { 402607f07615SPaolo Bonzini /* 402707f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 402807f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 402907f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 403007f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 403107f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 403207f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 403307f07615SPaolo Bonzini * mode. 403407f07615SPaolo Bonzini * 403507f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 403607f07615SPaolo Bonzini */ 403729cdb251SPaolo Bonzini ret = 0; 403807f07615SPaolo Bonzini } 403929cdb251SPaolo Bonzini if (ret < 0) { 404029cdb251SPaolo Bonzini return ret; 404129cdb251SPaolo Bonzini } 404229cdb251SPaolo Bonzini 404329cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 404429cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 404529cdb251SPaolo Bonzini */ 4046d4c82329SKevin Wolf flush_parent: 404729cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 404807f07615SPaolo Bonzini } 404907f07615SPaolo Bonzini 40500f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 40510f15423cSAnthony Liguori { 40520f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 40530f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 40540f15423cSAnthony Liguori } 40550f15423cSAnthony Liguori } 40560f15423cSAnthony Liguori 40570f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 40580f15423cSAnthony Liguori { 40590f15423cSAnthony Liguori BlockDriverState *bs; 40600f15423cSAnthony Liguori 40610f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 40620f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 40630f15423cSAnthony Liguori } 40640f15423cSAnthony Liguori } 40650f15423cSAnthony Liguori 406607789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 406707789269SBenoît Canet { 406807789269SBenoît Canet BlockDriverState *bs; 406907789269SBenoît Canet 407007789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 407107789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 407207789269SBenoît Canet } 407307789269SBenoît Canet } 407407789269SBenoît Canet 407507f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 407607f07615SPaolo Bonzini { 407707f07615SPaolo Bonzini Coroutine *co; 407807f07615SPaolo Bonzini RwCo rwco = { 407907f07615SPaolo Bonzini .bs = bs, 408007f07615SPaolo Bonzini .ret = NOT_DONE, 408107f07615SPaolo Bonzini }; 408207f07615SPaolo Bonzini 408307f07615SPaolo Bonzini if (qemu_in_coroutine()) { 408407f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 408507f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 408607f07615SPaolo Bonzini } else { 408707f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 408807f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 408907f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 409007f07615SPaolo Bonzini qemu_aio_wait(); 409107f07615SPaolo Bonzini } 409207f07615SPaolo Bonzini } 409307f07615SPaolo Bonzini 409407f07615SPaolo Bonzini return rwco.ret; 409507f07615SPaolo Bonzini } 4096e7a8a783SKevin Wolf 40974265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 40984265d620SPaolo Bonzini { 40994265d620SPaolo Bonzini RwCo *rwco = opaque; 41004265d620SPaolo Bonzini 41014265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 41024265d620SPaolo Bonzini } 41034265d620SPaolo Bonzini 41044265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 41054265d620SPaolo Bonzini int nb_sectors) 41064265d620SPaolo Bonzini { 41074265d620SPaolo Bonzini if (!bs->drv) { 41084265d620SPaolo Bonzini return -ENOMEDIUM; 41094265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 41104265d620SPaolo Bonzini return -EIO; 41114265d620SPaolo Bonzini } else if (bs->read_only) { 41124265d620SPaolo Bonzini return -EROFS; 41134265d620SPaolo Bonzini } else if (bs->drv->bdrv_co_discard) { 41144265d620SPaolo Bonzini return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors); 41154265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 41164265d620SPaolo Bonzini BlockDriverAIOCB *acb; 41174265d620SPaolo Bonzini CoroutineIOCompletion co = { 41184265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 41194265d620SPaolo Bonzini }; 41204265d620SPaolo Bonzini 41214265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 41224265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 41234265d620SPaolo Bonzini if (acb == NULL) { 41244265d620SPaolo Bonzini return -EIO; 41254265d620SPaolo Bonzini } else { 41264265d620SPaolo Bonzini qemu_coroutine_yield(); 41274265d620SPaolo Bonzini return co.ret; 41284265d620SPaolo Bonzini } 41294265d620SPaolo Bonzini } else { 41304265d620SPaolo Bonzini return 0; 41314265d620SPaolo Bonzini } 41324265d620SPaolo Bonzini } 41334265d620SPaolo Bonzini 41344265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 41354265d620SPaolo Bonzini { 41364265d620SPaolo Bonzini Coroutine *co; 41374265d620SPaolo Bonzini RwCo rwco = { 41384265d620SPaolo Bonzini .bs = bs, 41394265d620SPaolo Bonzini .sector_num = sector_num, 41404265d620SPaolo Bonzini .nb_sectors = nb_sectors, 41414265d620SPaolo Bonzini .ret = NOT_DONE, 41424265d620SPaolo Bonzini }; 41434265d620SPaolo Bonzini 41444265d620SPaolo Bonzini if (qemu_in_coroutine()) { 41454265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 41464265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 41474265d620SPaolo Bonzini } else { 41484265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 41494265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 41504265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 41514265d620SPaolo Bonzini qemu_aio_wait(); 41524265d620SPaolo Bonzini } 41534265d620SPaolo Bonzini } 41544265d620SPaolo Bonzini 41554265d620SPaolo Bonzini return rwco.ret; 41564265d620SPaolo Bonzini } 41574265d620SPaolo Bonzini 4158f9f05dc5SKevin Wolf /**************************************************************/ 415919cb3738Sbellard /* removable device support */ 416019cb3738Sbellard 416119cb3738Sbellard /** 416219cb3738Sbellard * Return TRUE if the media is present 416319cb3738Sbellard */ 416419cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 416519cb3738Sbellard { 416619cb3738Sbellard BlockDriver *drv = bs->drv; 4167a1aff5bfSMarkus Armbruster 416819cb3738Sbellard if (!drv) 416919cb3738Sbellard return 0; 417019cb3738Sbellard if (!drv->bdrv_is_inserted) 4171a1aff5bfSMarkus Armbruster return 1; 4172a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 417319cb3738Sbellard } 417419cb3738Sbellard 417519cb3738Sbellard /** 41768e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 41778e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 417819cb3738Sbellard */ 417919cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 418019cb3738Sbellard { 418119cb3738Sbellard BlockDriver *drv = bs->drv; 418219cb3738Sbellard 41838e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 41848e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 41858e49ca46SMarkus Armbruster } 41868e49ca46SMarkus Armbruster return -ENOTSUP; 418719cb3738Sbellard } 418819cb3738Sbellard 418919cb3738Sbellard /** 419019cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 419119cb3738Sbellard */ 4192f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 419319cb3738Sbellard { 419419cb3738Sbellard BlockDriver *drv = bs->drv; 419519cb3738Sbellard 4196822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 4197822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 419819cb3738Sbellard } 41996f382ed2SLuiz Capitulino 42006f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 42016f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 42026f382ed2SLuiz Capitulino } 420319cb3738Sbellard } 420419cb3738Sbellard 420519cb3738Sbellard /** 420619cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 420719cb3738Sbellard * to eject it manually). 420819cb3738Sbellard */ 4209025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 421019cb3738Sbellard { 421119cb3738Sbellard BlockDriver *drv = bs->drv; 421219cb3738Sbellard 4213025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 4214b8c6d095SStefan Hajnoczi 4215025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 4216025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 421719cb3738Sbellard } 421819cb3738Sbellard } 4219985a03b0Sths 4220985a03b0Sths /* needed for generic scsi interface */ 4221985a03b0Sths 4222985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 4223985a03b0Sths { 4224985a03b0Sths BlockDriver *drv = bs->drv; 4225985a03b0Sths 4226985a03b0Sths if (drv && drv->bdrv_ioctl) 4227985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 4228985a03b0Sths return -ENOTSUP; 4229985a03b0Sths } 42307d780669Saliguori 4231221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 4232221f715dSaliguori unsigned long int req, void *buf, 42337d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 42347d780669Saliguori { 4235221f715dSaliguori BlockDriver *drv = bs->drv; 42367d780669Saliguori 4237221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 4238221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 4239221f715dSaliguori return NULL; 42407d780669Saliguori } 4241e268ca52Saliguori 42427b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 42437b6f9300SMarkus Armbruster { 42447b6f9300SMarkus Armbruster bs->buffer_alignment = align; 42457b6f9300SMarkus Armbruster } 42467cd1e32aSlirans@il.ibm.com 4247e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 4248e268ca52Saliguori { 4249e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 4250e268ca52Saliguori } 42517cd1e32aSlirans@il.ibm.com 42527cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 42537cd1e32aSlirans@il.ibm.com { 42547cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 4255a55eb92cSJan Kiszka 4256aaa0eb75SLiran Schour bs->dirty_count = 0; 42577cd1e32aSlirans@il.ibm.com if (enable) { 4258c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 4259c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 426071df14fcSPaolo Bonzini BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG - 1; 426171df14fcSPaolo Bonzini bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG; 42627cd1e32aSlirans@il.ibm.com 426371df14fcSPaolo Bonzini bs->dirty_bitmap = g_new0(unsigned long, bitmap_size); 42647cd1e32aSlirans@il.ibm.com } 42657cd1e32aSlirans@il.ibm.com } else { 4266c6d22830SJan Kiszka if (bs->dirty_bitmap) { 42677267c094SAnthony Liguori g_free(bs->dirty_bitmap); 4268c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 42697cd1e32aSlirans@il.ibm.com } 42707cd1e32aSlirans@il.ibm.com } 42717cd1e32aSlirans@il.ibm.com } 42727cd1e32aSlirans@il.ibm.com 42737cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 42747cd1e32aSlirans@il.ibm.com { 42756ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 42767cd1e32aSlirans@il.ibm.com 4277c6d22830SJan Kiszka if (bs->dirty_bitmap && 4278c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 42791755da16SPaolo Bonzini return !!(bs->dirty_bitmap[chunk / BITS_PER_LONG] & 42801755da16SPaolo Bonzini (1UL << (chunk % BITS_PER_LONG))); 42817cd1e32aSlirans@il.ibm.com } else { 42827cd1e32aSlirans@il.ibm.com return 0; 42837cd1e32aSlirans@il.ibm.com } 42847cd1e32aSlirans@il.ibm.com } 42857cd1e32aSlirans@il.ibm.com 42861755da16SPaolo Bonzini int64_t bdrv_get_next_dirty(BlockDriverState *bs, int64_t sector) 42871755da16SPaolo Bonzini { 42881755da16SPaolo Bonzini int64_t chunk; 42891755da16SPaolo Bonzini int bit, elem; 42901755da16SPaolo Bonzini 42911755da16SPaolo Bonzini /* Avoid an infinite loop. */ 42921755da16SPaolo Bonzini assert(bs->dirty_count > 0); 42931755da16SPaolo Bonzini 42941755da16SPaolo Bonzini sector = (sector | (BDRV_SECTORS_PER_DIRTY_CHUNK - 1)) + 1; 42951755da16SPaolo Bonzini chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 42961755da16SPaolo Bonzini 42971755da16SPaolo Bonzini QEMU_BUILD_BUG_ON(sizeof(bs->dirty_bitmap[0]) * 8 != BITS_PER_LONG); 42981755da16SPaolo Bonzini elem = chunk / BITS_PER_LONG; 42991755da16SPaolo Bonzini bit = chunk % BITS_PER_LONG; 43001755da16SPaolo Bonzini for (;;) { 43011755da16SPaolo Bonzini if (sector >= bs->total_sectors) { 43021755da16SPaolo Bonzini sector = 0; 43031755da16SPaolo Bonzini bit = elem = 0; 43041755da16SPaolo Bonzini } 43051755da16SPaolo Bonzini if (bit == 0 && bs->dirty_bitmap[elem] == 0) { 43061755da16SPaolo Bonzini sector += BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG; 43071755da16SPaolo Bonzini elem++; 43081755da16SPaolo Bonzini } else { 43091755da16SPaolo Bonzini if (bs->dirty_bitmap[elem] & (1UL << bit)) { 43101755da16SPaolo Bonzini return sector; 43111755da16SPaolo Bonzini } 43121755da16SPaolo Bonzini sector += BDRV_SECTORS_PER_DIRTY_CHUNK; 43131755da16SPaolo Bonzini if (++bit == BITS_PER_LONG) { 43141755da16SPaolo Bonzini bit = 0; 43151755da16SPaolo Bonzini elem++; 43161755da16SPaolo Bonzini } 43171755da16SPaolo Bonzini } 43181755da16SPaolo Bonzini } 43191755da16SPaolo Bonzini } 43201755da16SPaolo Bonzini 43211755da16SPaolo Bonzini void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 43221755da16SPaolo Bonzini int nr_sectors) 43231755da16SPaolo Bonzini { 43241755da16SPaolo Bonzini set_dirty_bitmap(bs, cur_sector, nr_sectors, 1); 43251755da16SPaolo Bonzini } 43261755da16SPaolo Bonzini 43277cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 43287cd1e32aSlirans@il.ibm.com int nr_sectors) 43297cd1e32aSlirans@il.ibm.com { 43307cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 43317cd1e32aSlirans@il.ibm.com } 4332aaa0eb75SLiran Schour 4333aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 4334aaa0eb75SLiran Schour { 4335aaa0eb75SLiran Schour return bs->dirty_count; 4336aaa0eb75SLiran Schour } 4337f88e1a42SJes Sorensen 4338db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 4339db593f25SMarcelo Tosatti { 4340db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 4341db593f25SMarcelo Tosatti bs->in_use = in_use; 4342db593f25SMarcelo Tosatti } 4343db593f25SMarcelo Tosatti 4344db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 4345db593f25SMarcelo Tosatti { 4346db593f25SMarcelo Tosatti return bs->in_use; 4347db593f25SMarcelo Tosatti } 4348db593f25SMarcelo Tosatti 434928a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 435028a7282aSLuiz Capitulino { 4351d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 435258e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 435328a7282aSLuiz Capitulino } 435428a7282aSLuiz Capitulino 435528a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 435628a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 435728a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 435828a7282aSLuiz Capitulino { 4359d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 436092aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 436192aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 436292aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 436328a7282aSLuiz Capitulino } 436428a7282aSLuiz Capitulino 436528a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 436628a7282aSLuiz Capitulino { 4367d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 436828a7282aSLuiz Capitulino } 436928a7282aSLuiz Capitulino 437028a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 437128a7282aSLuiz Capitulino { 437228a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 437358e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 43743bd293c3SPaolo Bonzini if (bs->job) { 43753bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 43763bd293c3SPaolo Bonzini } 437728a7282aSLuiz Capitulino } 437828a7282aSLuiz Capitulino } 437928a7282aSLuiz Capitulino 438028a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 438128a7282aSLuiz Capitulino { 43823e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 43833e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 438458e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 438558e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 438628a7282aSLuiz Capitulino } 438728a7282aSLuiz Capitulino } 438828a7282aSLuiz Capitulino 4389a597e79cSChristoph Hellwig void 4390a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 4391a597e79cSChristoph Hellwig enum BlockAcctType type) 4392a597e79cSChristoph Hellwig { 4393a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 4394a597e79cSChristoph Hellwig 4395a597e79cSChristoph Hellwig cookie->bytes = bytes; 4396c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4397a597e79cSChristoph Hellwig cookie->type = type; 4398a597e79cSChristoph Hellwig } 4399a597e79cSChristoph Hellwig 4400a597e79cSChristoph Hellwig void 4401a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4402a597e79cSChristoph Hellwig { 4403a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4404a597e79cSChristoph Hellwig 4405a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4406a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4407c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4408a597e79cSChristoph Hellwig } 4409a597e79cSChristoph Hellwig 4410f88e1a42SJes Sorensen int bdrv_img_create(const char *filename, const char *fmt, 4411f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 4412f88e1a42SJes Sorensen char *options, uint64_t img_size, int flags) 4413f88e1a42SJes Sorensen { 4414f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4415d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4416f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 4417f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 441896df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4419f88e1a42SJes Sorensen int ret = 0; 4420f88e1a42SJes Sorensen 4421f88e1a42SJes Sorensen /* Find driver and parse its options */ 4422f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4423f88e1a42SJes Sorensen if (!drv) { 4424f88e1a42SJes Sorensen error_report("Unknown file format '%s'", fmt); 44254f70f249SJes Sorensen ret = -EINVAL; 4426f88e1a42SJes Sorensen goto out; 4427f88e1a42SJes Sorensen } 4428f88e1a42SJes Sorensen 4429f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 4430f88e1a42SJes Sorensen if (!proto_drv) { 4431f88e1a42SJes Sorensen error_report("Unknown protocol '%s'", filename); 44324f70f249SJes Sorensen ret = -EINVAL; 4433f88e1a42SJes Sorensen goto out; 4434f88e1a42SJes Sorensen } 4435f88e1a42SJes Sorensen 4436f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4437f88e1a42SJes Sorensen drv->create_options); 4438f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4439f88e1a42SJes Sorensen proto_drv->create_options); 4440f88e1a42SJes Sorensen 4441f88e1a42SJes Sorensen /* Create parameter list with default values */ 4442f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4443f88e1a42SJes Sorensen 4444f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4445f88e1a42SJes Sorensen 4446f88e1a42SJes Sorensen /* Parse -o options */ 4447f88e1a42SJes Sorensen if (options) { 4448f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4449f88e1a42SJes Sorensen if (param == NULL) { 4450f88e1a42SJes Sorensen error_report("Invalid options for file format '%s'.", fmt); 44514f70f249SJes Sorensen ret = -EINVAL; 4452f88e1a42SJes Sorensen goto out; 4453f88e1a42SJes Sorensen } 4454f88e1a42SJes Sorensen } 4455f88e1a42SJes Sorensen 4456f88e1a42SJes Sorensen if (base_filename) { 4457f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4458f88e1a42SJes Sorensen base_filename)) { 4459f88e1a42SJes Sorensen error_report("Backing file not supported for file format '%s'", 4460f88e1a42SJes Sorensen fmt); 44614f70f249SJes Sorensen ret = -EINVAL; 4462f88e1a42SJes Sorensen goto out; 4463f88e1a42SJes Sorensen } 4464f88e1a42SJes Sorensen } 4465f88e1a42SJes Sorensen 4466f88e1a42SJes Sorensen if (base_fmt) { 4467f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 4468f88e1a42SJes Sorensen error_report("Backing file format not supported for file " 4469f88e1a42SJes Sorensen "format '%s'", fmt); 44704f70f249SJes Sorensen ret = -EINVAL; 4471f88e1a42SJes Sorensen goto out; 4472f88e1a42SJes Sorensen } 4473f88e1a42SJes Sorensen } 4474f88e1a42SJes Sorensen 4475792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4476792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4477792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 4478792da93aSJes Sorensen error_report("Error: Trying to create an image with the " 4479792da93aSJes Sorensen "same filename as the backing file"); 44804f70f249SJes Sorensen ret = -EINVAL; 4481792da93aSJes Sorensen goto out; 4482792da93aSJes Sorensen } 4483792da93aSJes Sorensen } 4484792da93aSJes Sorensen 4485f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4486f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 448796df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 448896df67d1SStefan Hajnoczi if (!backing_drv) { 4489f88e1a42SJes Sorensen error_report("Unknown backing file format '%s'", 4490f88e1a42SJes Sorensen backing_fmt->value.s); 44914f70f249SJes Sorensen ret = -EINVAL; 4492f88e1a42SJes Sorensen goto out; 4493f88e1a42SJes Sorensen } 4494f88e1a42SJes Sorensen } 4495f88e1a42SJes Sorensen 4496f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4497f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4498d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4499d220894eSKevin Wolf if (size && size->value.n == -1) { 4500f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4501f88e1a42SJes Sorensen uint64_t size; 4502f88e1a42SJes Sorensen char buf[32]; 450363090dacSPaolo Bonzini int back_flags; 450463090dacSPaolo Bonzini 450563090dacSPaolo Bonzini /* backing files always opened read-only */ 450663090dacSPaolo Bonzini back_flags = 450763090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 4508f88e1a42SJes Sorensen 4509f88e1a42SJes Sorensen bs = bdrv_new(""); 4510f88e1a42SJes Sorensen 451163090dacSPaolo Bonzini ret = bdrv_open(bs, backing_file->value.s, back_flags, backing_drv); 4512f88e1a42SJes Sorensen if (ret < 0) { 451396df67d1SStefan Hajnoczi error_report("Could not open '%s'", backing_file->value.s); 4514f88e1a42SJes Sorensen goto out; 4515f88e1a42SJes Sorensen } 4516f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4517f88e1a42SJes Sorensen size *= 512; 4518f88e1a42SJes Sorensen 4519f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4520f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4521f88e1a42SJes Sorensen } else { 4522f88e1a42SJes Sorensen error_report("Image creation needs a size parameter"); 45234f70f249SJes Sorensen ret = -EINVAL; 4524f88e1a42SJes Sorensen goto out; 4525f88e1a42SJes Sorensen } 4526f88e1a42SJes Sorensen } 4527f88e1a42SJes Sorensen 4528f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4529f88e1a42SJes Sorensen print_option_parameters(param); 4530f88e1a42SJes Sorensen puts(""); 4531f88e1a42SJes Sorensen 4532f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 4533f88e1a42SJes Sorensen 4534f88e1a42SJes Sorensen if (ret < 0) { 4535f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 4536f88e1a42SJes Sorensen error_report("Formatting or formatting option not supported for " 4537f88e1a42SJes Sorensen "file format '%s'", fmt); 4538f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 4539f88e1a42SJes Sorensen error_report("The image size is too large for file format '%s'", 4540f88e1a42SJes Sorensen fmt); 4541f88e1a42SJes Sorensen } else { 4542f88e1a42SJes Sorensen error_report("%s: error while creating %s: %s", filename, fmt, 4543f88e1a42SJes Sorensen strerror(-ret)); 4544f88e1a42SJes Sorensen } 4545f88e1a42SJes Sorensen } 4546f88e1a42SJes Sorensen 4547f88e1a42SJes Sorensen out: 4548f88e1a42SJes Sorensen free_option_parameters(create_options); 4549f88e1a42SJes Sorensen free_option_parameters(param); 4550f88e1a42SJes Sorensen 4551f88e1a42SJes Sorensen if (bs) { 4552f88e1a42SJes Sorensen bdrv_delete(bs); 4553f88e1a42SJes Sorensen } 45544f70f249SJes Sorensen 45554f70f249SJes Sorensen return ret; 4556f88e1a42SJes Sorensen } 4557