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" 295efa9d5aSAnthony Liguori #include "module.h" 30f795e743SLuiz Capitulino #include "qjson.h" 3168485420SKevin Wolf #include "qemu-coroutine.h" 32b2023818SLuiz Capitulino #include "qmp-commands.h" 330563e191SZhi Yong Wu #include "qemu-timer.h" 34fc01f7e7Sbellard 3571e72a19SJuan Quintela #ifdef CONFIG_BSD 367674e7bfSbellard #include <sys/types.h> 377674e7bfSbellard #include <sys/stat.h> 387674e7bfSbellard #include <sys/ioctl.h> 3972cf2d4fSBlue Swirl #include <sys/queue.h> 40c5e97233Sblueswir1 #ifndef __DragonFly__ 417674e7bfSbellard #include <sys/disk.h> 427674e7bfSbellard #endif 43c5e97233Sblueswir1 #endif 447674e7bfSbellard 4549dc768dSaliguori #ifdef _WIN32 4649dc768dSaliguori #include <windows.h> 4749dc768dSaliguori #endif 4849dc768dSaliguori 491c9805a3SStefan Hajnoczi #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 501c9805a3SStefan Hajnoczi 51470c0504SStefan Hajnoczi typedef enum { 52470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ = 0x1, 53f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE = 0x2, 54470c0504SStefan Hajnoczi } BdrvRequestFlags; 55470c0504SStefan Hajnoczi 567d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); 57f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 58f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 59c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 60f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 61f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 62ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 63f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 64f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 65f9f05dc5SKevin Wolf QEMUIOVector *iov); 66f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 67f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 68f9f05dc5SKevin Wolf QEMUIOVector *iov); 69c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 70470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 71470c0504SStefan Hajnoczi BdrvRequestFlags flags); 721c9805a3SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 73f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 74f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags); 75b2a61371SStefan Hajnoczi static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 76b2a61371SStefan Hajnoczi int64_t sector_num, 77b2a61371SStefan Hajnoczi QEMUIOVector *qiov, 78b2a61371SStefan Hajnoczi int nb_sectors, 79b2a61371SStefan Hajnoczi BlockDriverCompletionFunc *cb, 80b2a61371SStefan Hajnoczi void *opaque, 818c5873d6SStefan Hajnoczi bool is_write); 82b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque); 83621f0589SKevin Wolf static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 84621f0589SKevin Wolf int64_t sector_num, int nb_sectors); 85ec530c81Sbellard 8698f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 8798f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait); 8898f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 8998f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait); 9098f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 9198f90dbaSZhi Yong Wu bool is_write, int64_t *wait); 9298f90dbaSZhi Yong Wu 931b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 941b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 957ee930d0Sblueswir1 968a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 978a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 98ea2384d3Sbellard 99f9092b10SMarkus Armbruster /* The device to use for VM snapshots */ 100f9092b10SMarkus Armbruster static BlockDriverState *bs_snapshots; 101f9092b10SMarkus Armbruster 102eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 103eb852011SMarkus Armbruster static int use_bdrv_whitelist; 104eb852011SMarkus Armbruster 1059e0b22f4SStefan Hajnoczi #ifdef _WIN32 1069e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 1079e0b22f4SStefan Hajnoczi { 1089e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 1099e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 1109e0b22f4SStefan Hajnoczi filename[1] == ':'); 1119e0b22f4SStefan Hajnoczi } 1129e0b22f4SStefan Hajnoczi 1139e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 1149e0b22f4SStefan Hajnoczi { 1159e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 1169e0b22f4SStefan Hajnoczi filename[2] == '\0') 1179e0b22f4SStefan Hajnoczi return 1; 1189e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1199e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1209e0b22f4SStefan Hajnoczi return 1; 1219e0b22f4SStefan Hajnoczi return 0; 1229e0b22f4SStefan Hajnoczi } 1239e0b22f4SStefan Hajnoczi #endif 1249e0b22f4SStefan Hajnoczi 1250563e191SZhi Yong Wu /* throttling disk I/O limits */ 12698f90dbaSZhi Yong Wu void bdrv_io_limits_disable(BlockDriverState *bs) 12798f90dbaSZhi Yong Wu { 12898f90dbaSZhi Yong Wu bs->io_limits_enabled = false; 12998f90dbaSZhi Yong Wu 13098f90dbaSZhi Yong Wu while (qemu_co_queue_next(&bs->throttled_reqs)); 13198f90dbaSZhi Yong Wu 13298f90dbaSZhi Yong Wu if (bs->block_timer) { 13398f90dbaSZhi Yong Wu qemu_del_timer(bs->block_timer); 13498f90dbaSZhi Yong Wu qemu_free_timer(bs->block_timer); 13598f90dbaSZhi Yong Wu bs->block_timer = NULL; 13698f90dbaSZhi Yong Wu } 13798f90dbaSZhi Yong Wu 13898f90dbaSZhi Yong Wu bs->slice_start = 0; 13998f90dbaSZhi Yong Wu bs->slice_end = 0; 14098f90dbaSZhi Yong Wu bs->slice_time = 0; 14198f90dbaSZhi Yong Wu memset(&bs->io_base, 0, sizeof(bs->io_base)); 14298f90dbaSZhi Yong Wu } 14398f90dbaSZhi Yong Wu 1440563e191SZhi Yong Wu static void bdrv_block_timer(void *opaque) 1450563e191SZhi Yong Wu { 1460563e191SZhi Yong Wu BlockDriverState *bs = opaque; 1470563e191SZhi Yong Wu 1480563e191SZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 1490563e191SZhi Yong Wu } 1500563e191SZhi Yong Wu 1510563e191SZhi Yong Wu void bdrv_io_limits_enable(BlockDriverState *bs) 1520563e191SZhi Yong Wu { 1530563e191SZhi Yong Wu qemu_co_queue_init(&bs->throttled_reqs); 1540563e191SZhi Yong Wu bs->block_timer = qemu_new_timer_ns(vm_clock, bdrv_block_timer, bs); 1550563e191SZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 1560563e191SZhi Yong Wu bs->slice_start = qemu_get_clock_ns(vm_clock); 1570563e191SZhi Yong Wu bs->slice_end = bs->slice_start + bs->slice_time; 1580563e191SZhi Yong Wu memset(&bs->io_base, 0, sizeof(bs->io_base)); 1590563e191SZhi Yong Wu bs->io_limits_enabled = true; 1600563e191SZhi Yong Wu } 1610563e191SZhi Yong Wu 1620563e191SZhi Yong Wu bool bdrv_io_limits_enabled(BlockDriverState *bs) 1630563e191SZhi Yong Wu { 1640563e191SZhi Yong Wu BlockIOLimit *io_limits = &bs->io_limits; 1650563e191SZhi Yong Wu return io_limits->bps[BLOCK_IO_LIMIT_READ] 1660563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_WRITE] 1670563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_TOTAL] 1680563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_READ] 1690563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_WRITE] 1700563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_TOTAL]; 1710563e191SZhi Yong Wu } 1720563e191SZhi Yong Wu 17398f90dbaSZhi Yong Wu static void bdrv_io_limits_intercept(BlockDriverState *bs, 17498f90dbaSZhi Yong Wu bool is_write, int nb_sectors) 17598f90dbaSZhi Yong Wu { 17698f90dbaSZhi Yong Wu int64_t wait_time = -1; 17798f90dbaSZhi Yong Wu 17898f90dbaSZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 17998f90dbaSZhi Yong Wu qemu_co_queue_wait(&bs->throttled_reqs); 18098f90dbaSZhi Yong Wu } 18198f90dbaSZhi Yong Wu 18298f90dbaSZhi Yong Wu /* In fact, we hope to keep each request's timing, in FIFO mode. The next 18398f90dbaSZhi Yong Wu * throttled requests will not be dequeued until the current request is 18498f90dbaSZhi Yong Wu * allowed to be serviced. So if the current request still exceeds the 18598f90dbaSZhi Yong Wu * limits, it will be inserted to the head. All requests followed it will 18698f90dbaSZhi Yong Wu * be still in throttled_reqs queue. 18798f90dbaSZhi Yong Wu */ 18898f90dbaSZhi Yong Wu 18998f90dbaSZhi Yong Wu while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) { 19098f90dbaSZhi Yong Wu qemu_mod_timer(bs->block_timer, 19198f90dbaSZhi Yong Wu wait_time + qemu_get_clock_ns(vm_clock)); 19298f90dbaSZhi Yong Wu qemu_co_queue_wait_insert_head(&bs->throttled_reqs); 19398f90dbaSZhi Yong Wu } 19498f90dbaSZhi Yong Wu 19598f90dbaSZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 19698f90dbaSZhi Yong Wu } 19798f90dbaSZhi Yong Wu 1989e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 1999e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 2009e0b22f4SStefan Hajnoczi { 2019e0b22f4SStefan Hajnoczi #ifdef _WIN32 2029e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2039e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2049e0b22f4SStefan Hajnoczi return 0; 2059e0b22f4SStefan Hajnoczi } 2069e0b22f4SStefan Hajnoczi #endif 2079e0b22f4SStefan Hajnoczi 2089e0b22f4SStefan Hajnoczi return strchr(path, ':') != NULL; 2099e0b22f4SStefan Hajnoczi } 2109e0b22f4SStefan Hajnoczi 21183f64091Sbellard int path_is_absolute(const char *path) 21283f64091Sbellard { 21383f64091Sbellard const char *p; 21421664424Sbellard #ifdef _WIN32 21521664424Sbellard /* specific case for names like: "\\.\d:" */ 21621664424Sbellard if (*path == '/' || *path == '\\') 21721664424Sbellard return 1; 21821664424Sbellard #endif 21983f64091Sbellard p = strchr(path, ':'); 22083f64091Sbellard if (p) 22183f64091Sbellard p++; 22283f64091Sbellard else 22383f64091Sbellard p = path; 2243b9f94e1Sbellard #ifdef _WIN32 2253b9f94e1Sbellard return (*p == '/' || *p == '\\'); 2263b9f94e1Sbellard #else 2273b9f94e1Sbellard return (*p == '/'); 2283b9f94e1Sbellard #endif 22983f64091Sbellard } 23083f64091Sbellard 23183f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 23283f64091Sbellard path to it by considering it is relative to base_path. URL are 23383f64091Sbellard supported. */ 23483f64091Sbellard void path_combine(char *dest, int dest_size, 23583f64091Sbellard const char *base_path, 23683f64091Sbellard const char *filename) 23783f64091Sbellard { 23883f64091Sbellard const char *p, *p1; 23983f64091Sbellard int len; 24083f64091Sbellard 24183f64091Sbellard if (dest_size <= 0) 24283f64091Sbellard return; 24383f64091Sbellard if (path_is_absolute(filename)) { 24483f64091Sbellard pstrcpy(dest, dest_size, filename); 24583f64091Sbellard } else { 24683f64091Sbellard p = strchr(base_path, ':'); 24783f64091Sbellard if (p) 24883f64091Sbellard p++; 24983f64091Sbellard else 25083f64091Sbellard p = base_path; 2513b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2523b9f94e1Sbellard #ifdef _WIN32 2533b9f94e1Sbellard { 2543b9f94e1Sbellard const char *p2; 2553b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2563b9f94e1Sbellard if (!p1 || p2 > p1) 2573b9f94e1Sbellard p1 = p2; 2583b9f94e1Sbellard } 2593b9f94e1Sbellard #endif 26083f64091Sbellard if (p1) 26183f64091Sbellard p1++; 26283f64091Sbellard else 26383f64091Sbellard p1 = base_path; 26483f64091Sbellard if (p1 > p) 26583f64091Sbellard p = p1; 26683f64091Sbellard len = p - base_path; 26783f64091Sbellard if (len > dest_size - 1) 26883f64091Sbellard len = dest_size - 1; 26983f64091Sbellard memcpy(dest, base_path, len); 27083f64091Sbellard dest[len] = '\0'; 27183f64091Sbellard pstrcat(dest, dest_size, filename); 27283f64091Sbellard } 27383f64091Sbellard } 27483f64091Sbellard 2755efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 276ea2384d3Sbellard { 2778c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 2788c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 279f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 280f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 281f9f05dc5SKevin Wolf 282f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 283f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 284f8c35c1dSStefan Hajnoczi */ 285f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 28683f64091Sbellard /* add AIO emulation layer */ 287f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 288f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 28983f64091Sbellard } 290f9f05dc5SKevin Wolf } 291b2e12bc6SChristoph Hellwig 2928a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 293ea2384d3Sbellard } 294b338082bSbellard 295b338082bSbellard /* create a new block device (by default it is empty) */ 296b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 297fc01f7e7Sbellard { 2981b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 299b338082bSbellard 3007267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 301b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 302ea2384d3Sbellard if (device_name[0] != '\0') { 3031b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 304ea2384d3Sbellard } 30528a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 306b338082bSbellard return bs; 307b338082bSbellard } 308b338082bSbellard 309ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 310ea2384d3Sbellard { 311ea2384d3Sbellard BlockDriver *drv1; 3128a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3138a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 314ea2384d3Sbellard return drv1; 315ea2384d3Sbellard } 3168a22f02aSStefan Hajnoczi } 317ea2384d3Sbellard return NULL; 318ea2384d3Sbellard } 319ea2384d3Sbellard 320eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 321eb852011SMarkus Armbruster { 322eb852011SMarkus Armbruster static const char *whitelist[] = { 323eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 324eb852011SMarkus Armbruster }; 325eb852011SMarkus Armbruster const char **p; 326eb852011SMarkus Armbruster 327eb852011SMarkus Armbruster if (!whitelist[0]) 328eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 329eb852011SMarkus Armbruster 330eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 331eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 332eb852011SMarkus Armbruster return 1; 333eb852011SMarkus Armbruster } 334eb852011SMarkus Armbruster } 335eb852011SMarkus Armbruster return 0; 336eb852011SMarkus Armbruster } 337eb852011SMarkus Armbruster 338eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 339eb852011SMarkus Armbruster { 340eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 341eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 342eb852011SMarkus Armbruster } 343eb852011SMarkus Armbruster 344*5b7e1542SZhi Yong Wu typedef struct CreateCo { 345*5b7e1542SZhi Yong Wu BlockDriver *drv; 346*5b7e1542SZhi Yong Wu char *filename; 347*5b7e1542SZhi Yong Wu QEMUOptionParameter *options; 348*5b7e1542SZhi Yong Wu int ret; 349*5b7e1542SZhi Yong Wu } CreateCo; 350*5b7e1542SZhi Yong Wu 351*5b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 352*5b7e1542SZhi Yong Wu { 353*5b7e1542SZhi Yong Wu CreateCo *cco = opaque; 354*5b7e1542SZhi Yong Wu assert(cco->drv); 355*5b7e1542SZhi Yong Wu 356*5b7e1542SZhi Yong Wu cco->ret = cco->drv->bdrv_create(cco->filename, cco->options); 357*5b7e1542SZhi Yong Wu } 358*5b7e1542SZhi Yong Wu 3590e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 3600e7e1989SKevin Wolf QEMUOptionParameter *options) 361ea2384d3Sbellard { 362*5b7e1542SZhi Yong Wu int ret; 3630e7e1989SKevin Wolf 364*5b7e1542SZhi Yong Wu Coroutine *co; 365*5b7e1542SZhi Yong Wu CreateCo cco = { 366*5b7e1542SZhi Yong Wu .drv = drv, 367*5b7e1542SZhi Yong Wu .filename = g_strdup(filename), 368*5b7e1542SZhi Yong Wu .options = options, 369*5b7e1542SZhi Yong Wu .ret = NOT_DONE, 370*5b7e1542SZhi Yong Wu }; 371*5b7e1542SZhi Yong Wu 372*5b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 373*5b7e1542SZhi Yong Wu return -ENOTSUP; 374*5b7e1542SZhi Yong Wu } 375*5b7e1542SZhi Yong Wu 376*5b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 377*5b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 378*5b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 379*5b7e1542SZhi Yong Wu } else { 380*5b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 381*5b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 382*5b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 383*5b7e1542SZhi Yong Wu qemu_aio_wait(); 384*5b7e1542SZhi Yong Wu } 385*5b7e1542SZhi Yong Wu } 386*5b7e1542SZhi Yong Wu 387*5b7e1542SZhi Yong Wu ret = cco.ret; 388*5b7e1542SZhi Yong Wu g_free(cco.filename); 389*5b7e1542SZhi Yong Wu 390*5b7e1542SZhi Yong Wu return ret; 391ea2384d3Sbellard } 392ea2384d3Sbellard 39384a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 39484a12e66SChristoph Hellwig { 39584a12e66SChristoph Hellwig BlockDriver *drv; 39684a12e66SChristoph Hellwig 397b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 39884a12e66SChristoph Hellwig if (drv == NULL) { 39916905d71SStefan Hajnoczi return -ENOENT; 40084a12e66SChristoph Hellwig } 40184a12e66SChristoph Hellwig 40284a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 40384a12e66SChristoph Hellwig } 40484a12e66SChristoph Hellwig 405d5249393Sbellard #ifdef _WIN32 40695389c86Sbellard void get_tmp_filename(char *filename, int size) 407d5249393Sbellard { 4083b9f94e1Sbellard char temp_dir[MAX_PATH]; 4093b9f94e1Sbellard 4103b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 4113b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 412d5249393Sbellard } 413d5249393Sbellard #else 41495389c86Sbellard void get_tmp_filename(char *filename, int size) 415ea2384d3Sbellard { 416ea2384d3Sbellard int fd; 4177ccfb2ebSblueswir1 const char *tmpdir; 418d5249393Sbellard /* XXX: race condition possible */ 4190badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 4200badc1eeSaurel32 if (!tmpdir) 4210badc1eeSaurel32 tmpdir = "/tmp"; 4220badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 423ea2384d3Sbellard fd = mkstemp(filename); 424ea2384d3Sbellard close(fd); 425ea2384d3Sbellard } 426d5249393Sbellard #endif 427ea2384d3Sbellard 428f3a5d3f8SChristoph Hellwig /* 429f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 430f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 431f3a5d3f8SChristoph Hellwig */ 432f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 433f3a5d3f8SChristoph Hellwig { 434508c7cb3SChristoph Hellwig int score_max = 0, score; 435508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 436f3a5d3f8SChristoph Hellwig 4378a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 438508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 439508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 440508c7cb3SChristoph Hellwig if (score > score_max) { 441508c7cb3SChristoph Hellwig score_max = score; 442508c7cb3SChristoph Hellwig drv = d; 443f3a5d3f8SChristoph Hellwig } 444508c7cb3SChristoph Hellwig } 445f3a5d3f8SChristoph Hellwig } 446f3a5d3f8SChristoph Hellwig 447508c7cb3SChristoph Hellwig return drv; 448f3a5d3f8SChristoph Hellwig } 449f3a5d3f8SChristoph Hellwig 450b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 45184a12e66SChristoph Hellwig { 45284a12e66SChristoph Hellwig BlockDriver *drv1; 45384a12e66SChristoph Hellwig char protocol[128]; 45484a12e66SChristoph Hellwig int len; 45584a12e66SChristoph Hellwig const char *p; 45684a12e66SChristoph Hellwig 45766f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 45866f82ceeSKevin Wolf 45939508e7aSChristoph Hellwig /* 46039508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 46139508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 46239508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 46339508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 46439508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 46539508e7aSChristoph Hellwig */ 46684a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 46739508e7aSChristoph Hellwig if (drv1) { 46884a12e66SChristoph Hellwig return drv1; 46984a12e66SChristoph Hellwig } 47039508e7aSChristoph Hellwig 4719e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 47239508e7aSChristoph Hellwig return bdrv_find_format("file"); 47339508e7aSChristoph Hellwig } 4749e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 4759e0b22f4SStefan Hajnoczi assert(p != NULL); 47684a12e66SChristoph Hellwig len = p - filename; 47784a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 47884a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 47984a12e66SChristoph Hellwig memcpy(protocol, filename, len); 48084a12e66SChristoph Hellwig protocol[len] = '\0'; 48184a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 48284a12e66SChristoph Hellwig if (drv1->protocol_name && 48384a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 48484a12e66SChristoph Hellwig return drv1; 48584a12e66SChristoph Hellwig } 48684a12e66SChristoph Hellwig } 48784a12e66SChristoph Hellwig return NULL; 48884a12e66SChristoph Hellwig } 48984a12e66SChristoph Hellwig 490c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv) 491ea2384d3Sbellard { 49283f64091Sbellard int ret, score, score_max; 493ea2384d3Sbellard BlockDriver *drv1, *drv; 49483f64091Sbellard uint8_t buf[2048]; 49583f64091Sbellard BlockDriverState *bs; 496ea2384d3Sbellard 497f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 498c98ac35dSStefan Weil if (ret < 0) { 499c98ac35dSStefan Weil *pdrv = NULL; 500c98ac35dSStefan Weil return ret; 501c98ac35dSStefan Weil } 502f8ea0b00SNicholas Bellinger 50308a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 50408a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 5051a396859SNicholas A. Bellinger bdrv_delete(bs); 506c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 507c98ac35dSStefan Weil if (!drv) { 508c98ac35dSStefan Weil ret = -ENOENT; 509c98ac35dSStefan Weil } 510c98ac35dSStefan Weil *pdrv = drv; 511c98ac35dSStefan Weil return ret; 5121a396859SNicholas A. Bellinger } 513f8ea0b00SNicholas Bellinger 51483f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 51583f64091Sbellard bdrv_delete(bs); 516ea2384d3Sbellard if (ret < 0) { 517c98ac35dSStefan Weil *pdrv = NULL; 518c98ac35dSStefan Weil return ret; 519ea2384d3Sbellard } 520ea2384d3Sbellard 521ea2384d3Sbellard score_max = 0; 52284a12e66SChristoph Hellwig drv = NULL; 5238a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 52483f64091Sbellard if (drv1->bdrv_probe) { 525ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 526ea2384d3Sbellard if (score > score_max) { 527ea2384d3Sbellard score_max = score; 528ea2384d3Sbellard drv = drv1; 529ea2384d3Sbellard } 530ea2384d3Sbellard } 53183f64091Sbellard } 532c98ac35dSStefan Weil if (!drv) { 533c98ac35dSStefan Weil ret = -ENOENT; 534c98ac35dSStefan Weil } 535c98ac35dSStefan Weil *pdrv = drv; 536c98ac35dSStefan Weil return ret; 537ea2384d3Sbellard } 538ea2384d3Sbellard 53951762288SStefan Hajnoczi /** 54051762288SStefan Hajnoczi * Set the current 'total_sectors' value 54151762288SStefan Hajnoczi */ 54251762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 54351762288SStefan Hajnoczi { 54451762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 54551762288SStefan Hajnoczi 546396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 547396759adSNicholas Bellinger if (bs->sg) 548396759adSNicholas Bellinger return 0; 549396759adSNicholas Bellinger 55051762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 55151762288SStefan Hajnoczi if (drv->bdrv_getlength) { 55251762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 55351762288SStefan Hajnoczi if (length < 0) { 55451762288SStefan Hajnoczi return length; 55551762288SStefan Hajnoczi } 55651762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 55751762288SStefan Hajnoczi } 55851762288SStefan Hajnoczi 55951762288SStefan Hajnoczi bs->total_sectors = hint; 56051762288SStefan Hajnoczi return 0; 56151762288SStefan Hajnoczi } 56251762288SStefan Hajnoczi 563c3993cdcSStefan Hajnoczi /** 564c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 565c3993cdcSStefan Hajnoczi * 566c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 567c3993cdcSStefan Hajnoczi */ 568c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 569c3993cdcSStefan Hajnoczi { 570c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 571c3993cdcSStefan Hajnoczi 572c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 573c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 57492196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 57592196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 576c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 577c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 578c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 579c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 580c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 581c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 582c3993cdcSStefan Hajnoczi /* this is the default */ 583c3993cdcSStefan Hajnoczi } else { 584c3993cdcSStefan Hajnoczi return -1; 585c3993cdcSStefan Hajnoczi } 586c3993cdcSStefan Hajnoczi 587c3993cdcSStefan Hajnoczi return 0; 588c3993cdcSStefan Hajnoczi } 589c3993cdcSStefan Hajnoczi 59053fec9d3SStefan Hajnoczi /** 59153fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 59253fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 59353fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 59453fec9d3SStefan Hajnoczi */ 59553fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 59653fec9d3SStefan Hajnoczi { 59753fec9d3SStefan Hajnoczi bs->copy_on_read++; 59853fec9d3SStefan Hajnoczi } 59953fec9d3SStefan Hajnoczi 60053fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 60153fec9d3SStefan Hajnoczi { 60253fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 60353fec9d3SStefan Hajnoczi bs->copy_on_read--; 60453fec9d3SStefan Hajnoczi } 60553fec9d3SStefan Hajnoczi 606b6ce07aaSKevin Wolf /* 60757915332SKevin Wolf * Common part for opening disk images and files 60857915332SKevin Wolf */ 60957915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 61057915332SKevin Wolf int flags, BlockDriver *drv) 61157915332SKevin Wolf { 61257915332SKevin Wolf int ret, open_flags; 61357915332SKevin Wolf 61457915332SKevin Wolf assert(drv != NULL); 61557915332SKevin Wolf 61628dcee10SStefan Hajnoczi trace_bdrv_open_common(bs, filename, flags, drv->format_name); 61728dcee10SStefan Hajnoczi 61866f82ceeSKevin Wolf bs->file = NULL; 61951762288SStefan Hajnoczi bs->total_sectors = 0; 62057915332SKevin Wolf bs->encrypted = 0; 62157915332SKevin Wolf bs->valid_key = 0; 62203f541bdSStefan Hajnoczi bs->sg = 0; 62357915332SKevin Wolf bs->open_flags = flags; 62403f541bdSStefan Hajnoczi bs->growable = 0; 62557915332SKevin Wolf bs->buffer_alignment = 512; 62657915332SKevin Wolf 62753fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 62853fec9d3SStefan Hajnoczi if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) { 62953fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 63053fec9d3SStefan Hajnoczi } 63153fec9d3SStefan Hajnoczi 63257915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 63303f541bdSStefan Hajnoczi bs->backing_file[0] = '\0'; 63457915332SKevin Wolf 63557915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 63657915332SKevin Wolf return -ENOTSUP; 63757915332SKevin Wolf } 63857915332SKevin Wolf 63957915332SKevin Wolf bs->drv = drv; 6407267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 64157915332SKevin Wolf 64203f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 64357915332SKevin Wolf 64457915332SKevin Wolf /* 64557915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 64657915332SKevin Wolf * image. 64757915332SKevin Wolf */ 64857915332SKevin Wolf open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 64957915332SKevin Wolf 65057915332SKevin Wolf /* 651ebabb67aSStefan Weil * Snapshots should be writable. 65257915332SKevin Wolf */ 65357915332SKevin Wolf if (bs->is_temporary) { 65457915332SKevin Wolf open_flags |= BDRV_O_RDWR; 65557915332SKevin Wolf } 65657915332SKevin Wolf 657e7c63796SStefan Hajnoczi bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 658e7c63796SStefan Hajnoczi 65966f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 66066f82ceeSKevin Wolf if (drv->bdrv_file_open) { 66166f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 66266f82ceeSKevin Wolf } else { 66366f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 66466f82ceeSKevin Wolf if (ret >= 0) { 66566f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 66666f82ceeSKevin Wolf } 66766f82ceeSKevin Wolf } 66866f82ceeSKevin Wolf 66957915332SKevin Wolf if (ret < 0) { 67057915332SKevin Wolf goto free_and_fail; 67157915332SKevin Wolf } 67257915332SKevin Wolf 67351762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 67451762288SStefan Hajnoczi if (ret < 0) { 67551762288SStefan Hajnoczi goto free_and_fail; 67657915332SKevin Wolf } 67751762288SStefan Hajnoczi 67857915332SKevin Wolf #ifndef _WIN32 67957915332SKevin Wolf if (bs->is_temporary) { 68057915332SKevin Wolf unlink(filename); 68157915332SKevin Wolf } 68257915332SKevin Wolf #endif 68357915332SKevin Wolf return 0; 68457915332SKevin Wolf 68557915332SKevin Wolf free_and_fail: 68666f82ceeSKevin Wolf if (bs->file) { 68766f82ceeSKevin Wolf bdrv_delete(bs->file); 68866f82ceeSKevin Wolf bs->file = NULL; 68966f82ceeSKevin Wolf } 6907267c094SAnthony Liguori g_free(bs->opaque); 69157915332SKevin Wolf bs->opaque = NULL; 69257915332SKevin Wolf bs->drv = NULL; 69357915332SKevin Wolf return ret; 69457915332SKevin Wolf } 69557915332SKevin Wolf 69657915332SKevin Wolf /* 697b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 698b6ce07aaSKevin Wolf */ 69983f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 700b338082bSbellard { 70183f64091Sbellard BlockDriverState *bs; 7026db95603SChristoph Hellwig BlockDriver *drv; 70383f64091Sbellard int ret; 7043b0d4f61Sbellard 705b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 7066db95603SChristoph Hellwig if (!drv) { 7076db95603SChristoph Hellwig return -ENOENT; 7086db95603SChristoph Hellwig } 7096db95603SChristoph Hellwig 71083f64091Sbellard bs = bdrv_new(""); 711b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 71283f64091Sbellard if (ret < 0) { 71383f64091Sbellard bdrv_delete(bs); 71483f64091Sbellard return ret; 7153b0d4f61Sbellard } 71671d0770cSaliguori bs->growable = 1; 71783f64091Sbellard *pbs = bs; 71883f64091Sbellard return 0; 7193b0d4f61Sbellard } 7203b0d4f61Sbellard 721b6ce07aaSKevin Wolf /* 722b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 723b6ce07aaSKevin Wolf */ 724d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 725ea2384d3Sbellard BlockDriver *drv) 726ea2384d3Sbellard { 727b6ce07aaSKevin Wolf int ret; 7282b572816SKevin Wolf char tmp_filename[PATH_MAX]; 72933e3963eSbellard 73083f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 731ea2384d3Sbellard BlockDriverState *bs1; 732ea2384d3Sbellard int64_t total_size; 7337c96d46eSaliguori int is_protocol = 0; 73491a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 73591a073a9SKevin Wolf QEMUOptionParameter *options; 736b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 73733e3963eSbellard 738ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 739ea2384d3Sbellard instead of opening 'filename' directly */ 740ea2384d3Sbellard 741ea2384d3Sbellard /* if there is a backing file, use it */ 742ea2384d3Sbellard bs1 = bdrv_new(""); 743d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 74451d7c00cSaliguori if (ret < 0) { 745ea2384d3Sbellard bdrv_delete(bs1); 74651d7c00cSaliguori return ret; 747ea2384d3Sbellard } 7483e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 7497c96d46eSaliguori 7507c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 7517c96d46eSaliguori is_protocol = 1; 7527c96d46eSaliguori 753ea2384d3Sbellard bdrv_delete(bs1); 754ea2384d3Sbellard 755ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 7567c96d46eSaliguori 7577c96d46eSaliguori /* Real path is meaningless for protocols */ 7587c96d46eSaliguori if (is_protocol) 7597c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 7607c96d46eSaliguori "%s", filename); 761114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 762114cdfa9SKirill A. Shutemov return -errno; 7637c96d46eSaliguori 76491a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 76591a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 76691a073a9SKevin Wolf 7673e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 76891a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 76991a073a9SKevin Wolf if (drv) { 77091a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 77191a073a9SKevin Wolf drv->format_name); 77291a073a9SKevin Wolf } 77391a073a9SKevin Wolf 77491a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 775d748768cSJan Kiszka free_option_parameters(options); 77651d7c00cSaliguori if (ret < 0) { 77751d7c00cSaliguori return ret; 778ea2384d3Sbellard } 77991a073a9SKevin Wolf 780ea2384d3Sbellard filename = tmp_filename; 78191a073a9SKevin Wolf drv = bdrv_qcow2; 782ea2384d3Sbellard bs->is_temporary = 1; 783ea2384d3Sbellard } 784ea2384d3Sbellard 785b6ce07aaSKevin Wolf /* Find the right image format driver */ 7866db95603SChristoph Hellwig if (!drv) { 787c98ac35dSStefan Weil ret = find_image_format(filename, &drv); 788ea2384d3Sbellard } 7896987307cSChristoph Hellwig 79051d7c00cSaliguori if (!drv) { 79151d7c00cSaliguori goto unlink_and_fail; 79283f64091Sbellard } 793b6ce07aaSKevin Wolf 794b6ce07aaSKevin Wolf /* Open the image */ 795b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 796b6ce07aaSKevin Wolf if (ret < 0) { 7976987307cSChristoph Hellwig goto unlink_and_fail; 7986987307cSChristoph Hellwig } 7996987307cSChristoph Hellwig 800b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 801b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 802b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 803b6ce07aaSKevin Wolf int back_flags; 804b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 805b6ce07aaSKevin Wolf 806b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 807df2dbb4aSStefan Hajnoczi 808df2dbb4aSStefan Hajnoczi if (path_has_protocol(bs->backing_file)) { 809df2dbb4aSStefan Hajnoczi pstrcpy(backing_filename, sizeof(backing_filename), 810df2dbb4aSStefan Hajnoczi bs->backing_file); 811df2dbb4aSStefan Hajnoczi } else { 812b6ce07aaSKevin Wolf path_combine(backing_filename, sizeof(backing_filename), 813b6ce07aaSKevin Wolf filename, bs->backing_file); 814df2dbb4aSStefan Hajnoczi } 815df2dbb4aSStefan Hajnoczi 816df2dbb4aSStefan Hajnoczi if (bs->backing_format[0] != '\0') { 817b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 818df2dbb4aSStefan Hajnoczi } 819b6ce07aaSKevin Wolf 820b6ce07aaSKevin Wolf /* backing files always opened read-only */ 821b6ce07aaSKevin Wolf back_flags = 822b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 823b6ce07aaSKevin Wolf 824b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 825b6ce07aaSKevin Wolf if (ret < 0) { 826b6ce07aaSKevin Wolf bdrv_close(bs); 827b6ce07aaSKevin Wolf return ret; 828b6ce07aaSKevin Wolf } 829b6ce07aaSKevin Wolf if (bs->is_temporary) { 830b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 831b6ce07aaSKevin Wolf } else { 832b6ce07aaSKevin Wolf /* base image inherits from "parent" */ 833b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = bs->keep_read_only; 834b6ce07aaSKevin Wolf } 835b6ce07aaSKevin Wolf } 836b6ce07aaSKevin Wolf 837b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 8387d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 839b6ce07aaSKevin Wolf } 840b6ce07aaSKevin Wolf 84198f90dbaSZhi Yong Wu /* throttling disk I/O limits */ 84298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 84398f90dbaSZhi Yong Wu bdrv_io_limits_enable(bs); 84498f90dbaSZhi Yong Wu } 84598f90dbaSZhi Yong Wu 846b6ce07aaSKevin Wolf return 0; 847b6ce07aaSKevin Wolf 848b6ce07aaSKevin Wolf unlink_and_fail: 849b6ce07aaSKevin Wolf if (bs->is_temporary) { 850b6ce07aaSKevin Wolf unlink(filename); 851b6ce07aaSKevin Wolf } 852b6ce07aaSKevin Wolf return ret; 853b6ce07aaSKevin Wolf } 854b6ce07aaSKevin Wolf 855fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 856fc01f7e7Sbellard { 85780ccf93bSLiu Yuan bdrv_flush(bs); 85819cb3738Sbellard if (bs->drv) { 8593e914655SPaolo Bonzini if (bs->job) { 8603e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 8613e914655SPaolo Bonzini } 8627094f12fSKevin Wolf bdrv_drain_all(); 8637094f12fSKevin Wolf 864f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 865f9092b10SMarkus Armbruster bs_snapshots = NULL; 866f9092b10SMarkus Armbruster } 867557df6acSStefan Hajnoczi if (bs->backing_hd) { 868ea2384d3Sbellard bdrv_delete(bs->backing_hd); 869557df6acSStefan Hajnoczi bs->backing_hd = NULL; 870557df6acSStefan Hajnoczi } 871ea2384d3Sbellard bs->drv->bdrv_close(bs); 8727267c094SAnthony Liguori g_free(bs->opaque); 873ea2384d3Sbellard #ifdef _WIN32 874ea2384d3Sbellard if (bs->is_temporary) { 875ea2384d3Sbellard unlink(bs->filename); 876ea2384d3Sbellard } 87767b915a5Sbellard #endif 878ea2384d3Sbellard bs->opaque = NULL; 879ea2384d3Sbellard bs->drv = NULL; 88053fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 881b338082bSbellard 88266f82ceeSKevin Wolf if (bs->file != NULL) { 88366f82ceeSKevin Wolf bdrv_close(bs->file); 88466f82ceeSKevin Wolf } 88566f82ceeSKevin Wolf 8867d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 887b338082bSbellard } 88898f90dbaSZhi Yong Wu 88998f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 89098f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 89198f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 89298f90dbaSZhi Yong Wu } 893b338082bSbellard } 894b338082bSbellard 8952bc93fedSMORITA Kazutaka void bdrv_close_all(void) 8962bc93fedSMORITA Kazutaka { 8972bc93fedSMORITA Kazutaka BlockDriverState *bs; 8982bc93fedSMORITA Kazutaka 8992bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 9002bc93fedSMORITA Kazutaka bdrv_close(bs); 9012bc93fedSMORITA Kazutaka } 9022bc93fedSMORITA Kazutaka } 9032bc93fedSMORITA Kazutaka 904922453bcSStefan Hajnoczi /* 905922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 906922453bcSStefan Hajnoczi * 907922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 908922453bcSStefan Hajnoczi * after calling this function. 909922453bcSStefan Hajnoczi */ 910922453bcSStefan Hajnoczi void bdrv_drain_all(void) 911922453bcSStefan Hajnoczi { 912922453bcSStefan Hajnoczi BlockDriverState *bs; 913922453bcSStefan Hajnoczi 914922453bcSStefan Hajnoczi qemu_aio_flush(); 915922453bcSStefan Hajnoczi 916922453bcSStefan Hajnoczi /* If requests are still pending there is a bug somewhere */ 917922453bcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 918922453bcSStefan Hajnoczi assert(QLIST_EMPTY(&bs->tracked_requests)); 919922453bcSStefan Hajnoczi assert(qemu_co_queue_empty(&bs->throttled_reqs)); 920922453bcSStefan Hajnoczi } 921922453bcSStefan Hajnoczi } 922922453bcSStefan Hajnoczi 923d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 924d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 925d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 926d22b2f41SRyan Harper { 927d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 928d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 929d22b2f41SRyan Harper } 930d22b2f41SRyan Harper bs->device_name[0] = '\0'; 931d22b2f41SRyan Harper } 932d22b2f41SRyan Harper 9338802d1fdSJeff Cody /* 9348802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 9358802d1fdSJeff Cody * live, while keeping required fields on the top layer. 9368802d1fdSJeff Cody * 9378802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 9388802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 9398802d1fdSJeff Cody * 940f6801b83SJeff Cody * bs_new is required to be anonymous. 941f6801b83SJeff Cody * 9428802d1fdSJeff Cody * This function does not create any image files. 9438802d1fdSJeff Cody */ 9448802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 9458802d1fdSJeff Cody { 9468802d1fdSJeff Cody BlockDriverState tmp; 9478802d1fdSJeff Cody 948f6801b83SJeff Cody /* bs_new must be anonymous */ 949f6801b83SJeff Cody assert(bs_new->device_name[0] == '\0'); 9508802d1fdSJeff Cody 9518802d1fdSJeff Cody tmp = *bs_new; 9528802d1fdSJeff Cody 9538802d1fdSJeff Cody /* there are some fields that need to stay on the top layer: */ 9548802d1fdSJeff Cody 9558802d1fdSJeff Cody /* dev info */ 9568802d1fdSJeff Cody tmp.dev_ops = bs_top->dev_ops; 9578802d1fdSJeff Cody tmp.dev_opaque = bs_top->dev_opaque; 9588802d1fdSJeff Cody tmp.dev = bs_top->dev; 9598802d1fdSJeff Cody tmp.buffer_alignment = bs_top->buffer_alignment; 9608802d1fdSJeff Cody tmp.copy_on_read = bs_top->copy_on_read; 9618802d1fdSJeff Cody 9628802d1fdSJeff Cody /* i/o timing parameters */ 9638802d1fdSJeff Cody tmp.slice_time = bs_top->slice_time; 9648802d1fdSJeff Cody tmp.slice_start = bs_top->slice_start; 9658802d1fdSJeff Cody tmp.slice_end = bs_top->slice_end; 9668802d1fdSJeff Cody tmp.io_limits = bs_top->io_limits; 9678802d1fdSJeff Cody tmp.io_base = bs_top->io_base; 9688802d1fdSJeff Cody tmp.throttled_reqs = bs_top->throttled_reqs; 9698802d1fdSJeff Cody tmp.block_timer = bs_top->block_timer; 9708802d1fdSJeff Cody tmp.io_limits_enabled = bs_top->io_limits_enabled; 9718802d1fdSJeff Cody 9728802d1fdSJeff Cody /* geometry */ 9738802d1fdSJeff Cody tmp.cyls = bs_top->cyls; 9748802d1fdSJeff Cody tmp.heads = bs_top->heads; 9758802d1fdSJeff Cody tmp.secs = bs_top->secs; 9768802d1fdSJeff Cody tmp.translation = bs_top->translation; 9778802d1fdSJeff Cody 9788802d1fdSJeff Cody /* r/w error */ 9798802d1fdSJeff Cody tmp.on_read_error = bs_top->on_read_error; 9808802d1fdSJeff Cody tmp.on_write_error = bs_top->on_write_error; 9818802d1fdSJeff Cody 9828802d1fdSJeff Cody /* i/o status */ 9838802d1fdSJeff Cody tmp.iostatus_enabled = bs_top->iostatus_enabled; 9848802d1fdSJeff Cody tmp.iostatus = bs_top->iostatus; 9858802d1fdSJeff Cody 9868802d1fdSJeff Cody /* keep the same entry in bdrv_states */ 9878802d1fdSJeff Cody pstrcpy(tmp.device_name, sizeof(tmp.device_name), bs_top->device_name); 9888802d1fdSJeff Cody tmp.list = bs_top->list; 9898802d1fdSJeff Cody 9908802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 9918802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 9928802d1fdSJeff Cody tmp.backing_hd = bs_new; 9938802d1fdSJeff Cody pstrcpy(tmp.backing_file, sizeof(tmp.backing_file), bs_top->filename); 994f6801b83SJeff Cody bdrv_get_format(bs_top, tmp.backing_format, sizeof(tmp.backing_format)); 9958802d1fdSJeff Cody 9968802d1fdSJeff Cody /* swap contents of the fixed new bs and the current top */ 9978802d1fdSJeff Cody *bs_new = *bs_top; 9988802d1fdSJeff Cody *bs_top = tmp; 9998802d1fdSJeff Cody 1000f6801b83SJeff Cody /* device_name[] was carried over from the old bs_top. bs_new 1001f6801b83SJeff Cody * shouldn't be in bdrv_states, so we need to make device_name[] 1002f6801b83SJeff Cody * reflect the anonymity of bs_new 1003f6801b83SJeff Cody */ 1004f6801b83SJeff Cody bs_new->device_name[0] = '\0'; 1005f6801b83SJeff Cody 10068802d1fdSJeff Cody /* clear the copied fields in the new backing file */ 10078802d1fdSJeff Cody bdrv_detach_dev(bs_new, bs_new->dev); 10088802d1fdSJeff Cody 10098802d1fdSJeff Cody qemu_co_queue_init(&bs_new->throttled_reqs); 10108802d1fdSJeff Cody memset(&bs_new->io_base, 0, sizeof(bs_new->io_base)); 10118802d1fdSJeff Cody memset(&bs_new->io_limits, 0, sizeof(bs_new->io_limits)); 10128802d1fdSJeff Cody bdrv_iostatus_disable(bs_new); 10138802d1fdSJeff Cody 10148802d1fdSJeff Cody /* we don't use bdrv_io_limits_disable() for this, because we don't want 10158802d1fdSJeff Cody * to affect or delete the block_timer, as it has been moved to bs_top */ 10168802d1fdSJeff Cody bs_new->io_limits_enabled = false; 10178802d1fdSJeff Cody bs_new->block_timer = NULL; 10188802d1fdSJeff Cody bs_new->slice_time = 0; 10198802d1fdSJeff Cody bs_new->slice_start = 0; 10208802d1fdSJeff Cody bs_new->slice_end = 0; 10218802d1fdSJeff Cody } 10228802d1fdSJeff Cody 1023b338082bSbellard void bdrv_delete(BlockDriverState *bs) 1024b338082bSbellard { 1025fa879d62SMarkus Armbruster assert(!bs->dev); 10263e914655SPaolo Bonzini assert(!bs->job); 10273e914655SPaolo Bonzini assert(!bs->in_use); 102818846deeSMarkus Armbruster 10291b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1030d22b2f41SRyan Harper bdrv_make_anon(bs); 103134c6f050Saurel32 1032b338082bSbellard bdrv_close(bs); 103366f82ceeSKevin Wolf if (bs->file != NULL) { 103466f82ceeSKevin Wolf bdrv_delete(bs->file); 103566f82ceeSKevin Wolf } 103666f82ceeSKevin Wolf 1037f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 10387267c094SAnthony Liguori g_free(bs); 1039fc01f7e7Sbellard } 1040fc01f7e7Sbellard 1041fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1042fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 104318846deeSMarkus Armbruster { 1044fa879d62SMarkus Armbruster if (bs->dev) { 104518846deeSMarkus Armbruster return -EBUSY; 104618846deeSMarkus Armbruster } 1047fa879d62SMarkus Armbruster bs->dev = dev; 104828a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 104918846deeSMarkus Armbruster return 0; 105018846deeSMarkus Armbruster } 105118846deeSMarkus Armbruster 1052fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1053fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 105418846deeSMarkus Armbruster { 1055fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1056fa879d62SMarkus Armbruster abort(); 1057fa879d62SMarkus Armbruster } 1058fa879d62SMarkus Armbruster } 1059fa879d62SMarkus Armbruster 1060fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1061fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1062fa879d62SMarkus Armbruster { 1063fa879d62SMarkus Armbruster assert(bs->dev == dev); 1064fa879d62SMarkus Armbruster bs->dev = NULL; 10650e49de52SMarkus Armbruster bs->dev_ops = NULL; 10660e49de52SMarkus Armbruster bs->dev_opaque = NULL; 106729e05f20SMarkus Armbruster bs->buffer_alignment = 512; 106818846deeSMarkus Armbruster } 106918846deeSMarkus Armbruster 1070fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1071fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 107218846deeSMarkus Armbruster { 1073fa879d62SMarkus Armbruster return bs->dev; 107418846deeSMarkus Armbruster } 107518846deeSMarkus Armbruster 10760e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 10770e49de52SMarkus Armbruster void *opaque) 10780e49de52SMarkus Armbruster { 10790e49de52SMarkus Armbruster bs->dev_ops = ops; 10800e49de52SMarkus Armbruster bs->dev_opaque = opaque; 10812c6942faSMarkus Armbruster if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) { 10822c6942faSMarkus Armbruster bs_snapshots = NULL; 10832c6942faSMarkus Armbruster } 10840e49de52SMarkus Armbruster } 10850e49de52SMarkus Armbruster 1086329c0a48SLuiz Capitulino void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 1087329c0a48SLuiz Capitulino BlockQMPEventAction action, int is_read) 1088329c0a48SLuiz Capitulino { 1089329c0a48SLuiz Capitulino QObject *data; 1090329c0a48SLuiz Capitulino const char *action_str; 1091329c0a48SLuiz Capitulino 1092329c0a48SLuiz Capitulino switch (action) { 1093329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1094329c0a48SLuiz Capitulino action_str = "report"; 1095329c0a48SLuiz Capitulino break; 1096329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1097329c0a48SLuiz Capitulino action_str = "ignore"; 1098329c0a48SLuiz Capitulino break; 1099329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1100329c0a48SLuiz Capitulino action_str = "stop"; 1101329c0a48SLuiz Capitulino break; 1102329c0a48SLuiz Capitulino default: 1103329c0a48SLuiz Capitulino abort(); 1104329c0a48SLuiz Capitulino } 1105329c0a48SLuiz Capitulino 1106329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1107329c0a48SLuiz Capitulino bdrv->device_name, 1108329c0a48SLuiz Capitulino action_str, 1109329c0a48SLuiz Capitulino is_read ? "read" : "write"); 1110329c0a48SLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 1111329c0a48SLuiz Capitulino 1112329c0a48SLuiz Capitulino qobject_decref(data); 1113329c0a48SLuiz Capitulino } 1114329c0a48SLuiz Capitulino 11156f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 11166f382ed2SLuiz Capitulino { 11176f382ed2SLuiz Capitulino QObject *data; 11186f382ed2SLuiz Capitulino 11196f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 11206f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 11216f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 11226f382ed2SLuiz Capitulino 11236f382ed2SLuiz Capitulino qobject_decref(data); 11246f382ed2SLuiz Capitulino } 11256f382ed2SLuiz Capitulino 11267d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 11270e49de52SMarkus Armbruster { 1128145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 11296f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 11307d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 11316f382ed2SLuiz Capitulino if (tray_was_closed) { 11326f382ed2SLuiz Capitulino /* tray open */ 11336f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 11346f382ed2SLuiz Capitulino } 11356f382ed2SLuiz Capitulino if (load) { 11366f382ed2SLuiz Capitulino /* tray close */ 11376f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 11386f382ed2SLuiz Capitulino } 1139145feb17SMarkus Armbruster } 1140145feb17SMarkus Armbruster } 1141145feb17SMarkus Armbruster 11422c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 11432c6942faSMarkus Armbruster { 11442c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 11452c6942faSMarkus Armbruster } 11462c6942faSMarkus Armbruster 1147025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1148025ccaa7SPaolo Bonzini { 1149025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1150025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1151025ccaa7SPaolo Bonzini } 1152025ccaa7SPaolo Bonzini } 1153025ccaa7SPaolo Bonzini 1154e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1155e4def80bSMarkus Armbruster { 1156e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1157e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1158e4def80bSMarkus Armbruster } 1159e4def80bSMarkus Armbruster return false; 1160e4def80bSMarkus Armbruster } 1161e4def80bSMarkus Armbruster 1162145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1163145feb17SMarkus Armbruster { 1164145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1165145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 11660e49de52SMarkus Armbruster } 11670e49de52SMarkus Armbruster } 11680e49de52SMarkus Armbruster 1169f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1170f107639aSMarkus Armbruster { 1171f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1172f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1173f107639aSMarkus Armbruster } 1174f107639aSMarkus Armbruster return false; 1175f107639aSMarkus Armbruster } 1176f107639aSMarkus Armbruster 1177e97fc193Saliguori /* 1178e97fc193Saliguori * Run consistency checks on an image 1179e97fc193Saliguori * 1180e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1181a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1182e076f338SKevin Wolf * check are stored in res. 1183e97fc193Saliguori */ 1184e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) 1185e97fc193Saliguori { 1186e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1187e97fc193Saliguori return -ENOTSUP; 1188e97fc193Saliguori } 1189e97fc193Saliguori 1190e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 11919ac228e0SKevin Wolf return bs->drv->bdrv_check(bs, res); 1192e97fc193Saliguori } 1193e97fc193Saliguori 11948a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 11958a426614SKevin Wolf 119633e3963eSbellard /* commit COW file into the raw image */ 119733e3963eSbellard int bdrv_commit(BlockDriverState *bs) 119833e3963eSbellard { 119919cb3738Sbellard BlockDriver *drv = bs->drv; 1200ee181196SKevin Wolf BlockDriver *backing_drv; 12018a426614SKevin Wolf int64_t sector, total_sectors; 12028a426614SKevin Wolf int n, ro, open_flags; 12034dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 12048a426614SKevin Wolf uint8_t *buf; 12054dca4b63SNaphtali Sprei char filename[1024]; 12064dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 120733e3963eSbellard 120819cb3738Sbellard if (!drv) 120919cb3738Sbellard return -ENOMEDIUM; 121033e3963eSbellard 12114dca4b63SNaphtali Sprei if (!bs->backing_hd) { 12124dca4b63SNaphtali Sprei return -ENOTSUP; 12134dca4b63SNaphtali Sprei } 12144dca4b63SNaphtali Sprei 12154dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 1216ea2384d3Sbellard return -EACCES; 121733e3963eSbellard } 121833e3963eSbellard 12192d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 12202d3735d3SStefan Hajnoczi return -EBUSY; 12212d3735d3SStefan Hajnoczi } 12222d3735d3SStefan Hajnoczi 1223ee181196SKevin Wolf backing_drv = bs->backing_hd->drv; 12244dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 12254dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 12264dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 12274dca4b63SNaphtali Sprei 12284dca4b63SNaphtali Sprei if (ro) { 12294dca4b63SNaphtali Sprei /* re-open as RW */ 12304dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 12314dca4b63SNaphtali Sprei bs->backing_hd = NULL; 12324dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 1233ee181196SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, 1234ee181196SKevin Wolf backing_drv); 12354dca4b63SNaphtali Sprei if (rw_ret < 0) { 12364dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 12374dca4b63SNaphtali Sprei /* try to re-open read-only */ 12384dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1239ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1240ee181196SKevin Wolf backing_drv); 12414dca4b63SNaphtali Sprei if (ret < 0) { 12424dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 12434dca4b63SNaphtali Sprei /* drive not functional anymore */ 12444dca4b63SNaphtali Sprei bs->drv = NULL; 12454dca4b63SNaphtali Sprei return ret; 12464dca4b63SNaphtali Sprei } 12474dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 12484dca4b63SNaphtali Sprei return rw_ret; 12494dca4b63SNaphtali Sprei } 12504dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 1251ea2384d3Sbellard } 1252ea2384d3Sbellard 12536ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 12547267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 12558a426614SKevin Wolf 12568a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 125705c4af54SStefan Hajnoczi if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 12588a426614SKevin Wolf 12598a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 12604dca4b63SNaphtali Sprei ret = -EIO; 12614dca4b63SNaphtali Sprei goto ro_cleanup; 126233e3963eSbellard } 126333e3963eSbellard 12648a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 12654dca4b63SNaphtali Sprei ret = -EIO; 12664dca4b63SNaphtali Sprei goto ro_cleanup; 126733e3963eSbellard } 126833e3963eSbellard } 126933e3963eSbellard } 127095389c86Sbellard 12711d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 12721d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 12731d44952fSChristoph Hellwig bdrv_flush(bs); 12741d44952fSChristoph Hellwig } 127595389c86Sbellard 12763f5075aeSChristoph Hellwig /* 12773f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 12783f5075aeSChristoph Hellwig * stable on disk. 12793f5075aeSChristoph Hellwig */ 12803f5075aeSChristoph Hellwig if (bs->backing_hd) 12813f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 12824dca4b63SNaphtali Sprei 12834dca4b63SNaphtali Sprei ro_cleanup: 12847267c094SAnthony Liguori g_free(buf); 12854dca4b63SNaphtali Sprei 12864dca4b63SNaphtali Sprei if (ro) { 12874dca4b63SNaphtali Sprei /* re-open as RO */ 12884dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 12894dca4b63SNaphtali Sprei bs->backing_hd = NULL; 12904dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1291ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1292ee181196SKevin Wolf backing_drv); 12934dca4b63SNaphtali Sprei if (ret < 0) { 12944dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 12954dca4b63SNaphtali Sprei /* drive not functional anymore */ 12964dca4b63SNaphtali Sprei bs->drv = NULL; 12974dca4b63SNaphtali Sprei return ret; 12984dca4b63SNaphtali Sprei } 12994dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 13004dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 13014dca4b63SNaphtali Sprei } 13024dca4b63SNaphtali Sprei 13031d44952fSChristoph Hellwig return ret; 130433e3963eSbellard } 130533e3963eSbellard 1306e8877497SStefan Hajnoczi int bdrv_commit_all(void) 13076ab4b5abSMarkus Armbruster { 13086ab4b5abSMarkus Armbruster BlockDriverState *bs; 13096ab4b5abSMarkus Armbruster 13106ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1311e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1312e8877497SStefan Hajnoczi if (ret < 0) { 1313e8877497SStefan Hajnoczi return ret; 13146ab4b5abSMarkus Armbruster } 13156ab4b5abSMarkus Armbruster } 1316e8877497SStefan Hajnoczi return 0; 1317e8877497SStefan Hajnoczi } 13186ab4b5abSMarkus Armbruster 1319dbffbdcfSStefan Hajnoczi struct BdrvTrackedRequest { 1320dbffbdcfSStefan Hajnoczi BlockDriverState *bs; 1321dbffbdcfSStefan Hajnoczi int64_t sector_num; 1322dbffbdcfSStefan Hajnoczi int nb_sectors; 1323dbffbdcfSStefan Hajnoczi bool is_write; 1324dbffbdcfSStefan Hajnoczi QLIST_ENTRY(BdrvTrackedRequest) list; 13255f8b6491SStefan Hajnoczi Coroutine *co; /* owner, used for deadlock detection */ 1326f4658285SStefan Hajnoczi CoQueue wait_queue; /* coroutines blocked on this request */ 1327dbffbdcfSStefan Hajnoczi }; 1328dbffbdcfSStefan Hajnoczi 1329dbffbdcfSStefan Hajnoczi /** 1330dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1331dbffbdcfSStefan Hajnoczi * 1332dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1333dbffbdcfSStefan Hajnoczi */ 1334dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1335dbffbdcfSStefan Hajnoczi { 1336dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1337f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1338dbffbdcfSStefan Hajnoczi } 1339dbffbdcfSStefan Hajnoczi 1340dbffbdcfSStefan Hajnoczi /** 1341dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1342dbffbdcfSStefan Hajnoczi */ 1343dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1344dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1345dbffbdcfSStefan Hajnoczi int64_t sector_num, 1346dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1347dbffbdcfSStefan Hajnoczi { 1348dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1349dbffbdcfSStefan Hajnoczi .bs = bs, 1350dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1351dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1352dbffbdcfSStefan Hajnoczi .is_write = is_write, 13535f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1354dbffbdcfSStefan Hajnoczi }; 1355dbffbdcfSStefan Hajnoczi 1356f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1357f4658285SStefan Hajnoczi 1358dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1359dbffbdcfSStefan Hajnoczi } 1360dbffbdcfSStefan Hajnoczi 1361d83947acSStefan Hajnoczi /** 1362d83947acSStefan Hajnoczi * Round a region to cluster boundaries 1363d83947acSStefan Hajnoczi */ 1364d83947acSStefan Hajnoczi static void round_to_clusters(BlockDriverState *bs, 1365d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 1366d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 1367d83947acSStefan Hajnoczi int *cluster_nb_sectors) 1368d83947acSStefan Hajnoczi { 1369d83947acSStefan Hajnoczi BlockDriverInfo bdi; 1370d83947acSStefan Hajnoczi 1371d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 1372d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 1373d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 1374d83947acSStefan Hajnoczi } else { 1375d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 1376d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 1377d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 1378d83947acSStefan Hajnoczi nb_sectors, c); 1379d83947acSStefan Hajnoczi } 1380d83947acSStefan Hajnoczi } 1381d83947acSStefan Hajnoczi 1382f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 1383f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 1384d83947acSStefan Hajnoczi /* aaaa bbbb */ 1385d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 1386d83947acSStefan Hajnoczi return false; 1387d83947acSStefan Hajnoczi } 1388d83947acSStefan Hajnoczi /* bbbb aaaa */ 1389d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 1390d83947acSStefan Hajnoczi return false; 1391d83947acSStefan Hajnoczi } 1392d83947acSStefan Hajnoczi return true; 1393f4658285SStefan Hajnoczi } 1394f4658285SStefan Hajnoczi 1395f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 1396f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 1397f4658285SStefan Hajnoczi { 1398f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 1399d83947acSStefan Hajnoczi int64_t cluster_sector_num; 1400d83947acSStefan Hajnoczi int cluster_nb_sectors; 1401f4658285SStefan Hajnoczi bool retry; 1402f4658285SStefan Hajnoczi 1403d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 1404d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 1405d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 1406d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 1407d83947acSStefan Hajnoczi * interleave between them. 1408d83947acSStefan Hajnoczi */ 1409d83947acSStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1410d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1411d83947acSStefan Hajnoczi 1412f4658285SStefan Hajnoczi do { 1413f4658285SStefan Hajnoczi retry = false; 1414f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 1415d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 1416d83947acSStefan Hajnoczi cluster_nb_sectors)) { 14175f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 14185f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 14195f8b6491SStefan Hajnoczi * never happen since it means deadlock. 14205f8b6491SStefan Hajnoczi */ 14215f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 14225f8b6491SStefan Hajnoczi 1423f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 1424f4658285SStefan Hajnoczi retry = true; 1425f4658285SStefan Hajnoczi break; 1426f4658285SStefan Hajnoczi } 1427f4658285SStefan Hajnoczi } 1428f4658285SStefan Hajnoczi } while (retry); 1429f4658285SStefan Hajnoczi } 1430f4658285SStefan Hajnoczi 1431756e6736SKevin Wolf /* 1432756e6736SKevin Wolf * Return values: 1433756e6736SKevin Wolf * 0 - success 1434756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 1435756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 1436756e6736SKevin Wolf * image file header 1437756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 1438756e6736SKevin Wolf */ 1439756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 1440756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1441756e6736SKevin Wolf { 1442756e6736SKevin Wolf BlockDriver *drv = bs->drv; 1443756e6736SKevin Wolf 1444756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 1445756e6736SKevin Wolf return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 1446756e6736SKevin Wolf } else { 1447756e6736SKevin Wolf return -ENOTSUP; 1448756e6736SKevin Wolf } 1449756e6736SKevin Wolf } 1450756e6736SKevin Wolf 145171d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 145271d0770cSaliguori size_t size) 145371d0770cSaliguori { 145471d0770cSaliguori int64_t len; 145571d0770cSaliguori 145671d0770cSaliguori if (!bdrv_is_inserted(bs)) 145771d0770cSaliguori return -ENOMEDIUM; 145871d0770cSaliguori 145971d0770cSaliguori if (bs->growable) 146071d0770cSaliguori return 0; 146171d0770cSaliguori 146271d0770cSaliguori len = bdrv_getlength(bs); 146371d0770cSaliguori 1464fbb7b4e0SKevin Wolf if (offset < 0) 1465fbb7b4e0SKevin Wolf return -EIO; 1466fbb7b4e0SKevin Wolf 1467fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 146871d0770cSaliguori return -EIO; 146971d0770cSaliguori 147071d0770cSaliguori return 0; 147171d0770cSaliguori } 147271d0770cSaliguori 147371d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 147471d0770cSaliguori int nb_sectors) 147571d0770cSaliguori { 1476eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1477eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 147871d0770cSaliguori } 147971d0770cSaliguori 14801c9805a3SStefan Hajnoczi typedef struct RwCo { 14811c9805a3SStefan Hajnoczi BlockDriverState *bs; 14821c9805a3SStefan Hajnoczi int64_t sector_num; 14831c9805a3SStefan Hajnoczi int nb_sectors; 14841c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 14851c9805a3SStefan Hajnoczi bool is_write; 14861c9805a3SStefan Hajnoczi int ret; 14871c9805a3SStefan Hajnoczi } RwCo; 14881c9805a3SStefan Hajnoczi 14891c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 1490fc01f7e7Sbellard { 14911c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 1492fc01f7e7Sbellard 14931c9805a3SStefan Hajnoczi if (!rwco->is_write) { 14941c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 1495470c0504SStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 14961c9805a3SStefan Hajnoczi } else { 14971c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 1498f08f2ddaSStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 14991c9805a3SStefan Hajnoczi } 15001c9805a3SStefan Hajnoczi } 1501e7a8a783SKevin Wolf 15021c9805a3SStefan Hajnoczi /* 15031c9805a3SStefan Hajnoczi * Process a synchronous request using coroutines 15041c9805a3SStefan Hajnoczi */ 15051c9805a3SStefan Hajnoczi static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 15061c9805a3SStefan Hajnoczi int nb_sectors, bool is_write) 15071c9805a3SStefan Hajnoczi { 1508e7a8a783SKevin Wolf QEMUIOVector qiov; 1509e7a8a783SKevin Wolf struct iovec iov = { 1510e7a8a783SKevin Wolf .iov_base = (void *)buf, 1511e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 1512e7a8a783SKevin Wolf }; 15131c9805a3SStefan Hajnoczi Coroutine *co; 15141c9805a3SStefan Hajnoczi RwCo rwco = { 15151c9805a3SStefan Hajnoczi .bs = bs, 15161c9805a3SStefan Hajnoczi .sector_num = sector_num, 15171c9805a3SStefan Hajnoczi .nb_sectors = nb_sectors, 15181c9805a3SStefan Hajnoczi .qiov = &qiov, 15191c9805a3SStefan Hajnoczi .is_write = is_write, 15201c9805a3SStefan Hajnoczi .ret = NOT_DONE, 15211c9805a3SStefan Hajnoczi }; 1522e7a8a783SKevin Wolf 1523e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 15241c9805a3SStefan Hajnoczi 1525498e386cSZhi Yong Wu /** 1526498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 1527498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 1528498e386cSZhi Yong Wu * if it has been enabled. 1529498e386cSZhi Yong Wu */ 1530498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 1531498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 1532498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 1533498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 1534498e386cSZhi Yong Wu } 1535498e386cSZhi Yong Wu 15361c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 15371c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 15381c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 15391c9805a3SStefan Hajnoczi } else { 15401c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 15411c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 15421c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 15431c9805a3SStefan Hajnoczi qemu_aio_wait(); 15441c9805a3SStefan Hajnoczi } 15451c9805a3SStefan Hajnoczi } 15461c9805a3SStefan Hajnoczi return rwco.ret; 1547e7a8a783SKevin Wolf } 1548e7a8a783SKevin Wolf 15491c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 15501c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 15511c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 15521c9805a3SStefan Hajnoczi { 15531c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); 155483f64091Sbellard } 1555fc01f7e7Sbellard 15567cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 15577cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 15587cd1e32aSlirans@il.ibm.com { 15597cd1e32aSlirans@il.ibm.com int64_t start, end; 1560c6d22830SJan Kiszka unsigned long val, idx, bit; 1561a55eb92cSJan Kiszka 15626ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 1563c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 15647cd1e32aSlirans@il.ibm.com 15657cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 1566c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 1567c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 1568c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 1569c6d22830SJan Kiszka if (dirty) { 15706d59fec1SMarcelo Tosatti if (!(val & (1UL << bit))) { 1571aaa0eb75SLiran Schour bs->dirty_count++; 15726d59fec1SMarcelo Tosatti val |= 1UL << bit; 1573aaa0eb75SLiran Schour } 1574c6d22830SJan Kiszka } else { 15756d59fec1SMarcelo Tosatti if (val & (1UL << bit)) { 1576aaa0eb75SLiran Schour bs->dirty_count--; 15776d59fec1SMarcelo Tosatti val &= ~(1UL << bit); 1578c6d22830SJan Kiszka } 1579aaa0eb75SLiran Schour } 1580c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 15817cd1e32aSlirans@il.ibm.com } 15827cd1e32aSlirans@il.ibm.com } 15837cd1e32aSlirans@il.ibm.com 158419cb3738Sbellard /* Return < 0 if error. Important errors are: 158519cb3738Sbellard -EIO generic I/O error (may happen for all errors) 158619cb3738Sbellard -ENOMEDIUM No media inserted. 158719cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 158819cb3738Sbellard -EACCES Trying to write a read-only device 158919cb3738Sbellard */ 1590fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 1591fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 1592fc01f7e7Sbellard { 15931c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); 159483f64091Sbellard } 159583f64091Sbellard 1596eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 1597eda578e5Saliguori void *buf, int count1) 159883f64091Sbellard { 15996ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 160083f64091Sbellard int len, nb_sectors, count; 160183f64091Sbellard int64_t sector_num; 16029a8c4cceSKevin Wolf int ret; 160383f64091Sbellard 160483f64091Sbellard count = count1; 160583f64091Sbellard /* first read to align to sector start */ 16066ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 160783f64091Sbellard if (len > count) 160883f64091Sbellard len = count; 16096ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 161083f64091Sbellard if (len > 0) { 16119a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16129a8c4cceSKevin Wolf return ret; 16136ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 161483f64091Sbellard count -= len; 161583f64091Sbellard if (count == 0) 161683f64091Sbellard return count1; 161783f64091Sbellard sector_num++; 161883f64091Sbellard buf += len; 161983f64091Sbellard } 162083f64091Sbellard 162183f64091Sbellard /* read the sectors "in place" */ 16226ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 162383f64091Sbellard if (nb_sectors > 0) { 16249a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 16259a8c4cceSKevin Wolf return ret; 162683f64091Sbellard sector_num += nb_sectors; 16276ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 162883f64091Sbellard buf += len; 162983f64091Sbellard count -= len; 163083f64091Sbellard } 163183f64091Sbellard 163283f64091Sbellard /* add data from the last sector */ 163383f64091Sbellard if (count > 0) { 16349a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16359a8c4cceSKevin Wolf return ret; 163683f64091Sbellard memcpy(buf, tmp_buf, count); 163783f64091Sbellard } 163883f64091Sbellard return count1; 163983f64091Sbellard } 164083f64091Sbellard 1641eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 1642eda578e5Saliguori const void *buf, int count1) 164383f64091Sbellard { 16446ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 164583f64091Sbellard int len, nb_sectors, count; 164683f64091Sbellard int64_t sector_num; 16479a8c4cceSKevin Wolf int ret; 164883f64091Sbellard 164983f64091Sbellard count = count1; 165083f64091Sbellard /* first write to align to sector start */ 16516ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 165283f64091Sbellard if (len > count) 165383f64091Sbellard len = count; 16546ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 165583f64091Sbellard if (len > 0) { 16569a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16579a8c4cceSKevin Wolf return ret; 16586ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 16599a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 16609a8c4cceSKevin Wolf return ret; 166183f64091Sbellard count -= len; 166283f64091Sbellard if (count == 0) 166383f64091Sbellard return count1; 166483f64091Sbellard sector_num++; 166583f64091Sbellard buf += len; 166683f64091Sbellard } 166783f64091Sbellard 166883f64091Sbellard /* write the sectors "in place" */ 16696ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 167083f64091Sbellard if (nb_sectors > 0) { 16719a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 16729a8c4cceSKevin Wolf return ret; 167383f64091Sbellard sector_num += nb_sectors; 16746ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 167583f64091Sbellard buf += len; 167683f64091Sbellard count -= len; 167783f64091Sbellard } 167883f64091Sbellard 167983f64091Sbellard /* add data from the last sector */ 168083f64091Sbellard if (count > 0) { 16819a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16829a8c4cceSKevin Wolf return ret; 168383f64091Sbellard memcpy(tmp_buf, buf, count); 16849a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 16859a8c4cceSKevin Wolf return ret; 168683f64091Sbellard } 168783f64091Sbellard return count1; 168883f64091Sbellard } 168983f64091Sbellard 1690f08145feSKevin Wolf /* 1691f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1692f08145feSKevin Wolf * request (acts as a barrier) 1693f08145feSKevin Wolf * 1694f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1695f08145feSKevin Wolf */ 1696f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1697f08145feSKevin Wolf const void *buf, int count) 1698f08145feSKevin Wolf { 1699f08145feSKevin Wolf int ret; 1700f08145feSKevin Wolf 1701f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1702f08145feSKevin Wolf if (ret < 0) { 1703f08145feSKevin Wolf return ret; 1704f08145feSKevin Wolf } 1705f08145feSKevin Wolf 170692196b2fSStefan Hajnoczi /* No flush needed for cache modes that use O_DSYNC */ 170792196b2fSStefan Hajnoczi if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { 1708f08145feSKevin Wolf bdrv_flush(bs); 1709f08145feSKevin Wolf } 1710f08145feSKevin Wolf 1711f08145feSKevin Wolf return 0; 1712f08145feSKevin Wolf } 1713f08145feSKevin Wolf 1714470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 1715ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1716ab185921SStefan Hajnoczi { 1717ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 1718ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 1719ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 1720ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 1721ab185921SStefan Hajnoczi */ 1722ab185921SStefan Hajnoczi void *bounce_buffer; 1723ab185921SStefan Hajnoczi 172479c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 1725ab185921SStefan Hajnoczi struct iovec iov; 1726ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 1727ab185921SStefan Hajnoczi int64_t cluster_sector_num; 1728ab185921SStefan Hajnoczi int cluster_nb_sectors; 1729ab185921SStefan Hajnoczi size_t skip_bytes; 1730ab185921SStefan Hajnoczi int ret; 1731ab185921SStefan Hajnoczi 1732ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 1733ab185921SStefan Hajnoczi * allocating cluster in the image file. 1734ab185921SStefan Hajnoczi */ 1735ab185921SStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1736ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1737ab185921SStefan Hajnoczi 1738470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 1739ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 1740ab185921SStefan Hajnoczi 1741ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 1742ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 1743ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 1744ab185921SStefan Hajnoczi 174579c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 1746ab185921SStefan Hajnoczi &bounce_qiov); 1747ab185921SStefan Hajnoczi if (ret < 0) { 1748ab185921SStefan Hajnoczi goto err; 1749ab185921SStefan Hajnoczi } 1750ab185921SStefan Hajnoczi 175179c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 175279c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 1753621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 175479c053bdSStefan Hajnoczi cluster_nb_sectors); 175579c053bdSStefan Hajnoczi } else { 175679c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 1757ab185921SStefan Hajnoczi &bounce_qiov); 175879c053bdSStefan Hajnoczi } 175979c053bdSStefan Hajnoczi 1760ab185921SStefan Hajnoczi if (ret < 0) { 1761ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 1762ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 1763ab185921SStefan Hajnoczi * Simply report it in all cases. 1764ab185921SStefan Hajnoczi */ 1765ab185921SStefan Hajnoczi goto err; 1766ab185921SStefan Hajnoczi } 1767ab185921SStefan Hajnoczi 1768ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 1769ab185921SStefan Hajnoczi qemu_iovec_from_buffer(qiov, bounce_buffer + skip_bytes, 1770ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 1771ab185921SStefan Hajnoczi 1772ab185921SStefan Hajnoczi err: 1773ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 1774ab185921SStefan Hajnoczi return ret; 1775ab185921SStefan Hajnoczi } 1776ab185921SStefan Hajnoczi 1777c5fbe571SStefan Hajnoczi /* 1778c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 1779c5fbe571SStefan Hajnoczi */ 1780c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 1781470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1782470c0504SStefan Hajnoczi BdrvRequestFlags flags) 1783da1fa91dSKevin Wolf { 1784da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 1785dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 1786dbffbdcfSStefan Hajnoczi int ret; 1787da1fa91dSKevin Wolf 1788da1fa91dSKevin Wolf if (!drv) { 1789da1fa91dSKevin Wolf return -ENOMEDIUM; 1790da1fa91dSKevin Wolf } 1791da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1792da1fa91dSKevin Wolf return -EIO; 1793da1fa91dSKevin Wolf } 1794da1fa91dSKevin Wolf 179598f90dbaSZhi Yong Wu /* throttling disk read I/O */ 179698f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 179798f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, false, nb_sectors); 179898f90dbaSZhi Yong Wu } 179998f90dbaSZhi Yong Wu 1800f4658285SStefan Hajnoczi if (bs->copy_on_read) { 1801470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 1802470c0504SStefan Hajnoczi } 1803470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1804470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 1805470c0504SStefan Hajnoczi } 1806470c0504SStefan Hajnoczi 1807470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1808f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1809f4658285SStefan Hajnoczi } 1810f4658285SStefan Hajnoczi 1811dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 1812ab185921SStefan Hajnoczi 1813470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1814ab185921SStefan Hajnoczi int pnum; 1815ab185921SStefan Hajnoczi 1816ab185921SStefan Hajnoczi ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); 1817ab185921SStefan Hajnoczi if (ret < 0) { 1818ab185921SStefan Hajnoczi goto out; 1819ab185921SStefan Hajnoczi } 1820ab185921SStefan Hajnoczi 1821ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 1822470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 1823ab185921SStefan Hajnoczi goto out; 1824ab185921SStefan Hajnoczi } 1825ab185921SStefan Hajnoczi } 1826ab185921SStefan Hajnoczi 1827dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 1828ab185921SStefan Hajnoczi 1829ab185921SStefan Hajnoczi out: 1830dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1831470c0504SStefan Hajnoczi 1832470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1833470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 1834470c0504SStefan Hajnoczi } 1835470c0504SStefan Hajnoczi 1836dbffbdcfSStefan Hajnoczi return ret; 1837da1fa91dSKevin Wolf } 1838da1fa91dSKevin Wolf 1839c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 1840da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 1841da1fa91dSKevin Wolf { 1842c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 1843da1fa91dSKevin Wolf 1844470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 1845470c0504SStefan Hajnoczi } 1846470c0504SStefan Hajnoczi 1847470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 1848470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1849470c0504SStefan Hajnoczi { 1850470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 1851470c0504SStefan Hajnoczi 1852470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 1853470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 1854c5fbe571SStefan Hajnoczi } 1855c5fbe571SStefan Hajnoczi 1856f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 1857f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1858f08f2ddaSStefan Hajnoczi { 1859f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 1860f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 1861f08f2ddaSStefan Hajnoczi struct iovec iov; 1862f08f2ddaSStefan Hajnoczi int ret; 1863f08f2ddaSStefan Hajnoczi 1864621f0589SKevin Wolf /* TODO Emulate only part of misaligned requests instead of letting block 1865621f0589SKevin Wolf * drivers return -ENOTSUP and emulate everything */ 1866621f0589SKevin Wolf 1867f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 1868f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 1869621f0589SKevin Wolf ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1870621f0589SKevin Wolf if (ret != -ENOTSUP) { 1871621f0589SKevin Wolf return ret; 1872621f0589SKevin Wolf } 1873f08f2ddaSStefan Hajnoczi } 1874f08f2ddaSStefan Hajnoczi 1875f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 1876f08f2ddaSStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 1877f08f2ddaSStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 1878f08f2ddaSStefan Hajnoczi memset(iov.iov_base, 0, iov.iov_len); 1879f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 1880f08f2ddaSStefan Hajnoczi 1881f08f2ddaSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 1882f08f2ddaSStefan Hajnoczi 1883f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 1884f08f2ddaSStefan Hajnoczi return ret; 1885f08f2ddaSStefan Hajnoczi } 1886f08f2ddaSStefan Hajnoczi 1887c5fbe571SStefan Hajnoczi /* 1888c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 1889c5fbe571SStefan Hajnoczi */ 1890c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 1891f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1892f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 1893c5fbe571SStefan Hajnoczi { 1894c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 1895dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 18966b7cb247SStefan Hajnoczi int ret; 1897da1fa91dSKevin Wolf 1898da1fa91dSKevin Wolf if (!bs->drv) { 1899da1fa91dSKevin Wolf return -ENOMEDIUM; 1900da1fa91dSKevin Wolf } 1901da1fa91dSKevin Wolf if (bs->read_only) { 1902da1fa91dSKevin Wolf return -EACCES; 1903da1fa91dSKevin Wolf } 1904da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1905da1fa91dSKevin Wolf return -EIO; 1906da1fa91dSKevin Wolf } 1907da1fa91dSKevin Wolf 190898f90dbaSZhi Yong Wu /* throttling disk write I/O */ 190998f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 191098f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, true, nb_sectors); 191198f90dbaSZhi Yong Wu } 191298f90dbaSZhi Yong Wu 1913470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1914f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1915f4658285SStefan Hajnoczi } 1916f4658285SStefan Hajnoczi 1917dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 1918dbffbdcfSStefan Hajnoczi 1919f08f2ddaSStefan Hajnoczi if (flags & BDRV_REQ_ZERO_WRITE) { 1920f08f2ddaSStefan Hajnoczi ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); 1921f08f2ddaSStefan Hajnoczi } else { 19226b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 1923f08f2ddaSStefan Hajnoczi } 19246b7cb247SStefan Hajnoczi 1925da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 1926da1fa91dSKevin Wolf set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 1927da1fa91dSKevin Wolf } 1928da1fa91dSKevin Wolf 1929da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 1930da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 1931da1fa91dSKevin Wolf } 1932da1fa91dSKevin Wolf 1933dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1934dbffbdcfSStefan Hajnoczi 19356b7cb247SStefan Hajnoczi return ret; 1936da1fa91dSKevin Wolf } 1937da1fa91dSKevin Wolf 1938c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 1939c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 1940c5fbe571SStefan Hajnoczi { 1941c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 1942c5fbe571SStefan Hajnoczi 1943f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 1944f08f2ddaSStefan Hajnoczi } 1945f08f2ddaSStefan Hajnoczi 1946f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 1947f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1948f08f2ddaSStefan Hajnoczi { 1949f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1950f08f2ddaSStefan Hajnoczi 1951f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 1952f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE); 1953c5fbe571SStefan Hajnoczi } 1954c5fbe571SStefan Hajnoczi 195583f64091Sbellard /** 195683f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 195783f64091Sbellard */ 195883f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 195983f64091Sbellard { 196083f64091Sbellard BlockDriver *drv = bs->drv; 196151762288SStefan Hajnoczi int ret; 196283f64091Sbellard if (!drv) 196319cb3738Sbellard return -ENOMEDIUM; 196483f64091Sbellard if (!drv->bdrv_truncate) 196583f64091Sbellard return -ENOTSUP; 196659f2689dSNaphtali Sprei if (bs->read_only) 196759f2689dSNaphtali Sprei return -EACCES; 19688591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 19698591675fSMarcelo Tosatti return -EBUSY; 197051762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 197151762288SStefan Hajnoczi if (ret == 0) { 197251762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 1973145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 197451762288SStefan Hajnoczi } 197551762288SStefan Hajnoczi return ret; 197683f64091Sbellard } 197783f64091Sbellard 197883f64091Sbellard /** 19794a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 19804a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 19814a1d5e1fSFam Zheng */ 19824a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 19834a1d5e1fSFam Zheng { 19844a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 19854a1d5e1fSFam Zheng if (!drv) { 19864a1d5e1fSFam Zheng return -ENOMEDIUM; 19874a1d5e1fSFam Zheng } 19884a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 19894a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 19904a1d5e1fSFam Zheng } 19914a1d5e1fSFam Zheng if (bs->file) { 19924a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 19934a1d5e1fSFam Zheng } 19944a1d5e1fSFam Zheng return -ENOTSUP; 19954a1d5e1fSFam Zheng } 19964a1d5e1fSFam Zheng 19974a1d5e1fSFam Zheng /** 199883f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 199983f64091Sbellard */ 200083f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 200183f64091Sbellard { 200283f64091Sbellard BlockDriver *drv = bs->drv; 200383f64091Sbellard if (!drv) 200419cb3738Sbellard return -ENOMEDIUM; 200551762288SStefan Hajnoczi 20062c6942faSMarkus Armbruster if (bs->growable || bdrv_dev_has_removable_media(bs)) { 200746a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 200883f64091Sbellard return drv->bdrv_getlength(bs); 2009fc01f7e7Sbellard } 201046a4e4e6SStefan Hajnoczi } 201146a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 201246a4e4e6SStefan Hajnoczi } 2013fc01f7e7Sbellard 201419cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 201596b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2016fc01f7e7Sbellard { 201719cb3738Sbellard int64_t length; 201819cb3738Sbellard length = bdrv_getlength(bs); 201919cb3738Sbellard if (length < 0) 202019cb3738Sbellard length = 0; 202119cb3738Sbellard else 20226ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 202319cb3738Sbellard *nb_sectors_ptr = length; 2024fc01f7e7Sbellard } 2025cf98951bSbellard 2026f3d54fc4Saliguori struct partition { 2027f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 2028f3d54fc4Saliguori uint8_t head; /* starting head */ 2029f3d54fc4Saliguori uint8_t sector; /* starting sector */ 2030f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 2031f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 2032f3d54fc4Saliguori uint8_t end_head; /* end head */ 2033f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 2034f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 2035f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 2036f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 2037541dc0d4SStefan Weil } QEMU_PACKED; 2038f3d54fc4Saliguori 2039f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 2040f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 2041f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 2042f3d54fc4Saliguori { 2043eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 2044f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 2045f3d54fc4Saliguori struct partition *p; 2046f3d54fc4Saliguori uint32_t nr_sects; 2047a38131b6Sblueswir1 uint64_t nb_sectors; 2048498e386cSZhi Yong Wu bool enabled; 2049f3d54fc4Saliguori 2050f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 2051f3d54fc4Saliguori 2052498e386cSZhi Yong Wu /** 2053498e386cSZhi Yong Wu * The function will be invoked during startup not only in sync I/O mode, 2054498e386cSZhi Yong Wu * but also in async I/O mode. So the I/O throttling function has to 2055498e386cSZhi Yong Wu * be disabled temporarily here, not permanently. 2056498e386cSZhi Yong Wu */ 2057498e386cSZhi Yong Wu enabled = bs->io_limits_enabled; 2058498e386cSZhi Yong Wu bs->io_limits_enabled = false; 2059f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 2060498e386cSZhi Yong Wu bs->io_limits_enabled = enabled; 2061f3d54fc4Saliguori if (ret < 0) 2062f3d54fc4Saliguori return -1; 2063f3d54fc4Saliguori /* test msdos magic */ 2064f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 2065f3d54fc4Saliguori return -1; 2066f3d54fc4Saliguori for(i = 0; i < 4; i++) { 2067f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 2068f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 2069f3d54fc4Saliguori if (nr_sects && p->end_head) { 2070f3d54fc4Saliguori /* We make the assumption that the partition terminates on 2071f3d54fc4Saliguori a cylinder boundary */ 2072f3d54fc4Saliguori heads = p->end_head + 1; 2073f3d54fc4Saliguori sectors = p->end_sector & 63; 2074f3d54fc4Saliguori if (sectors == 0) 2075f3d54fc4Saliguori continue; 2076f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 2077f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 2078f3d54fc4Saliguori continue; 2079f3d54fc4Saliguori *pheads = heads; 2080f3d54fc4Saliguori *psectors = sectors; 2081f3d54fc4Saliguori *pcylinders = cylinders; 2082f3d54fc4Saliguori #if 0 2083f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 2084f3d54fc4Saliguori cylinders, heads, sectors); 2085f3d54fc4Saliguori #endif 2086f3d54fc4Saliguori return 0; 2087f3d54fc4Saliguori } 2088f3d54fc4Saliguori } 2089f3d54fc4Saliguori return -1; 2090f3d54fc4Saliguori } 2091f3d54fc4Saliguori 2092f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 2093f3d54fc4Saliguori { 2094f3d54fc4Saliguori int translation, lba_detected = 0; 2095f3d54fc4Saliguori int cylinders, heads, secs; 2096a38131b6Sblueswir1 uint64_t nb_sectors; 2097f3d54fc4Saliguori 2098f3d54fc4Saliguori /* if a geometry hint is available, use it */ 2099f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 2100f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 2101f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 2102f3d54fc4Saliguori if (cylinders != 0) { 2103f3d54fc4Saliguori *pcyls = cylinders; 2104f3d54fc4Saliguori *pheads = heads; 2105f3d54fc4Saliguori *psecs = secs; 2106f3d54fc4Saliguori } else { 2107f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 2108f3d54fc4Saliguori if (heads > 16) { 2109f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 2110f3d54fc4Saliguori translation was active, so the default 2111f3d54fc4Saliguori hardware geometry is OK */ 2112f3d54fc4Saliguori lba_detected = 1; 2113f3d54fc4Saliguori goto default_geometry; 2114f3d54fc4Saliguori } else { 2115f3d54fc4Saliguori *pcyls = cylinders; 2116f3d54fc4Saliguori *pheads = heads; 2117f3d54fc4Saliguori *psecs = secs; 2118f3d54fc4Saliguori /* disable any translation to be in sync with 2119f3d54fc4Saliguori the logical geometry */ 2120f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 2121f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2122f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 2123f3d54fc4Saliguori } 2124f3d54fc4Saliguori } 2125f3d54fc4Saliguori } else { 2126f3d54fc4Saliguori default_geometry: 2127f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 2128f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 2129f3d54fc4Saliguori 2130f3d54fc4Saliguori if (cylinders > 16383) 2131f3d54fc4Saliguori cylinders = 16383; 2132f3d54fc4Saliguori else if (cylinders < 2) 2133f3d54fc4Saliguori cylinders = 2; 2134f3d54fc4Saliguori *pcyls = cylinders; 2135f3d54fc4Saliguori *pheads = 16; 2136f3d54fc4Saliguori *psecs = 63; 2137f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 2138f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 2139f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2140f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 2141f3d54fc4Saliguori } else { 2142f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2143f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 2144f3d54fc4Saliguori } 2145f3d54fc4Saliguori } 2146f3d54fc4Saliguori } 2147f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 2148f3d54fc4Saliguori } 2149f3d54fc4Saliguori } 2150f3d54fc4Saliguori 2151b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 2152b338082bSbellard int cyls, int heads, int secs) 2153b338082bSbellard { 2154b338082bSbellard bs->cyls = cyls; 2155b338082bSbellard bs->heads = heads; 2156b338082bSbellard bs->secs = secs; 2157b338082bSbellard } 2158b338082bSbellard 215946d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 216046d4767dSbellard { 216146d4767dSbellard bs->translation = translation; 216246d4767dSbellard } 216346d4767dSbellard 2164b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 2165b338082bSbellard int *pcyls, int *pheads, int *psecs) 2166b338082bSbellard { 2167b338082bSbellard *pcyls = bs->cyls; 2168b338082bSbellard *pheads = bs->heads; 2169b338082bSbellard *psecs = bs->secs; 2170b338082bSbellard } 2171b338082bSbellard 21720563e191SZhi Yong Wu /* throttling disk io limits */ 21730563e191SZhi Yong Wu void bdrv_set_io_limits(BlockDriverState *bs, 21740563e191SZhi Yong Wu BlockIOLimit *io_limits) 21750563e191SZhi Yong Wu { 21760563e191SZhi Yong Wu bs->io_limits = *io_limits; 21770563e191SZhi Yong Wu bs->io_limits_enabled = bdrv_io_limits_enabled(bs); 21780563e191SZhi Yong Wu } 21790563e191SZhi Yong Wu 21805bbdbb46SBlue Swirl /* Recognize floppy formats */ 21815bbdbb46SBlue Swirl typedef struct FDFormat { 21825bbdbb46SBlue Swirl FDriveType drive; 21835bbdbb46SBlue Swirl uint8_t last_sect; 21845bbdbb46SBlue Swirl uint8_t max_track; 21855bbdbb46SBlue Swirl uint8_t max_head; 2186f8d3d128SHervé Poussineau FDriveRate rate; 21875bbdbb46SBlue Swirl } FDFormat; 21885bbdbb46SBlue Swirl 21895bbdbb46SBlue Swirl static const FDFormat fd_formats[] = { 21905bbdbb46SBlue Swirl /* First entry is default format */ 21915bbdbb46SBlue Swirl /* 1.44 MB 3"1/2 floppy disks */ 2192f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 18, 80, 1, FDRIVE_RATE_500K, }, 2193f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 20, 80, 1, FDRIVE_RATE_500K, }, 2194f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 80, 1, FDRIVE_RATE_500K, }, 2195f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 82, 1, FDRIVE_RATE_500K, }, 2196f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 83, 1, FDRIVE_RATE_500K, }, 2197f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 22, 80, 1, FDRIVE_RATE_500K, }, 2198f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 23, 80, 1, FDRIVE_RATE_500K, }, 2199f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 24, 80, 1, FDRIVE_RATE_500K, }, 22005bbdbb46SBlue Swirl /* 2.88 MB 3"1/2 floppy disks */ 2201f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 36, 80, 1, FDRIVE_RATE_1M, }, 2202f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 39, 80, 1, FDRIVE_RATE_1M, }, 2203f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 40, 80, 1, FDRIVE_RATE_1M, }, 2204f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 44, 80, 1, FDRIVE_RATE_1M, }, 2205f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 48, 80, 1, FDRIVE_RATE_1M, }, 22065bbdbb46SBlue Swirl /* 720 kB 3"1/2 floppy disks */ 2207f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 9, 80, 1, FDRIVE_RATE_250K, }, 2208f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 80, 1, FDRIVE_RATE_250K, }, 2209f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 82, 1, FDRIVE_RATE_250K, }, 2210f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 83, 1, FDRIVE_RATE_250K, }, 2211f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 13, 80, 1, FDRIVE_RATE_250K, }, 2212f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 14, 80, 1, FDRIVE_RATE_250K, }, 22135bbdbb46SBlue Swirl /* 1.2 MB 5"1/4 floppy disks */ 2214f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 15, 80, 1, FDRIVE_RATE_500K, }, 2215f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 80, 1, FDRIVE_RATE_500K, }, 2216f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 82, 1, FDRIVE_RATE_500K, }, 2217f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 83, 1, FDRIVE_RATE_500K, }, 2218f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 20, 80, 1, FDRIVE_RATE_500K, }, 22195bbdbb46SBlue Swirl /* 720 kB 5"1/4 floppy disks */ 2220f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 80, 1, FDRIVE_RATE_250K, }, 2221f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 11, 80, 1, FDRIVE_RATE_250K, }, 22225bbdbb46SBlue Swirl /* 360 kB 5"1/4 floppy disks */ 2223f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 40, 1, FDRIVE_RATE_300K, }, 2224f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 40, 0, FDRIVE_RATE_300K, }, 2225f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 10, 41, 1, FDRIVE_RATE_300K, }, 2226f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 10, 42, 1, FDRIVE_RATE_300K, }, 22275bbdbb46SBlue Swirl /* 320 kB 5"1/4 floppy disks */ 2228f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 8, 40, 1, FDRIVE_RATE_250K, }, 2229f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 8, 40, 0, FDRIVE_RATE_250K, }, 22305bbdbb46SBlue Swirl /* 360 kB must match 5"1/4 better than 3"1/2... */ 2231f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 9, 80, 0, FDRIVE_RATE_250K, }, 22325bbdbb46SBlue Swirl /* end */ 2233f8d3d128SHervé Poussineau { FDRIVE_DRV_NONE, -1, -1, 0, 0, }, 22345bbdbb46SBlue Swirl }; 22355bbdbb46SBlue Swirl 22365bbdbb46SBlue Swirl void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, 22375bbdbb46SBlue Swirl int *max_track, int *last_sect, 2238f8d3d128SHervé Poussineau FDriveType drive_in, FDriveType *drive, 2239f8d3d128SHervé Poussineau FDriveRate *rate) 22405bbdbb46SBlue Swirl { 22415bbdbb46SBlue Swirl const FDFormat *parse; 22425bbdbb46SBlue Swirl uint64_t nb_sectors, size; 22435bbdbb46SBlue Swirl int i, first_match, match; 22445bbdbb46SBlue Swirl 22455bbdbb46SBlue Swirl bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); 22465bbdbb46SBlue Swirl if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { 22475bbdbb46SBlue Swirl /* User defined disk */ 2248f8d3d128SHervé Poussineau *rate = FDRIVE_RATE_500K; 22495bbdbb46SBlue Swirl } else { 22505bbdbb46SBlue Swirl bdrv_get_geometry(bs, &nb_sectors); 22515bbdbb46SBlue Swirl match = -1; 22525bbdbb46SBlue Swirl first_match = -1; 22535bbdbb46SBlue Swirl for (i = 0; ; i++) { 22545bbdbb46SBlue Swirl parse = &fd_formats[i]; 22555bbdbb46SBlue Swirl if (parse->drive == FDRIVE_DRV_NONE) { 22565bbdbb46SBlue Swirl break; 22575bbdbb46SBlue Swirl } 22585bbdbb46SBlue Swirl if (drive_in == parse->drive || 22595bbdbb46SBlue Swirl drive_in == FDRIVE_DRV_NONE) { 22605bbdbb46SBlue Swirl size = (parse->max_head + 1) * parse->max_track * 22615bbdbb46SBlue Swirl parse->last_sect; 22625bbdbb46SBlue Swirl if (nb_sectors == size) { 22635bbdbb46SBlue Swirl match = i; 22645bbdbb46SBlue Swirl break; 22655bbdbb46SBlue Swirl } 22665bbdbb46SBlue Swirl if (first_match == -1) { 22675bbdbb46SBlue Swirl first_match = i; 22685bbdbb46SBlue Swirl } 22695bbdbb46SBlue Swirl } 22705bbdbb46SBlue Swirl } 22715bbdbb46SBlue Swirl if (match == -1) { 22725bbdbb46SBlue Swirl if (first_match == -1) { 22735bbdbb46SBlue Swirl match = 1; 22745bbdbb46SBlue Swirl } else { 22755bbdbb46SBlue Swirl match = first_match; 22765bbdbb46SBlue Swirl } 22775bbdbb46SBlue Swirl parse = &fd_formats[match]; 22785bbdbb46SBlue Swirl } 22795bbdbb46SBlue Swirl *nb_heads = parse->max_head + 1; 22805bbdbb46SBlue Swirl *max_track = parse->max_track; 22815bbdbb46SBlue Swirl *last_sect = parse->last_sect; 22825bbdbb46SBlue Swirl *drive = parse->drive; 2283f8d3d128SHervé Poussineau *rate = parse->rate; 22845bbdbb46SBlue Swirl } 22855bbdbb46SBlue Swirl } 22865bbdbb46SBlue Swirl 228746d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 228846d4767dSbellard { 228946d4767dSbellard return bs->translation; 229046d4767dSbellard } 229146d4767dSbellard 2292abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 2293abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 2294abd7f68dSMarkus Armbruster { 2295abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2296abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2297abd7f68dSMarkus Armbruster } 2298abd7f68dSMarkus Armbruster 2299abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 2300abd7f68dSMarkus Armbruster { 2301abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2302abd7f68dSMarkus Armbruster } 2303abd7f68dSMarkus Armbruster 2304b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2305b338082bSbellard { 2306b338082bSbellard return bs->read_only; 2307b338082bSbellard } 2308b338082bSbellard 2309985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2310985a03b0Sths { 2311985a03b0Sths return bs->sg; 2312985a03b0Sths } 2313985a03b0Sths 2314e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2315e900a7b7SChristoph Hellwig { 2316e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2317e900a7b7SChristoph Hellwig } 2318e900a7b7SChristoph Hellwig 2319ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2320ea2384d3Sbellard { 2321ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2322ea2384d3Sbellard return 1; 2323ea2384d3Sbellard return bs->encrypted; 2324ea2384d3Sbellard } 2325ea2384d3Sbellard 2326c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2327c0f4ce77Saliguori { 2328c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2329c0f4ce77Saliguori 2330c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2331c0f4ce77Saliguori return 1; 2332c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2333c0f4ce77Saliguori } 2334c0f4ce77Saliguori 2335ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2336ea2384d3Sbellard { 2337ea2384d3Sbellard int ret; 2338ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2339ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2340ea2384d3Sbellard if (ret < 0) 2341ea2384d3Sbellard return ret; 2342ea2384d3Sbellard if (!bs->encrypted) 2343ea2384d3Sbellard return 0; 2344ea2384d3Sbellard } 2345fd04a2aeSShahar Havivi if (!bs->encrypted) { 2346fd04a2aeSShahar Havivi return -EINVAL; 2347fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2348fd04a2aeSShahar Havivi return -ENOMEDIUM; 2349fd04a2aeSShahar Havivi } 2350c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2351bb5fc20fSaliguori if (ret < 0) { 2352bb5fc20fSaliguori bs->valid_key = 0; 2353bb5fc20fSaliguori } else if (!bs->valid_key) { 2354bb5fc20fSaliguori bs->valid_key = 1; 2355bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 23567d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 2357bb5fc20fSaliguori } 2358c0f4ce77Saliguori return ret; 2359ea2384d3Sbellard } 2360ea2384d3Sbellard 2361ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 2362ea2384d3Sbellard { 236319cb3738Sbellard if (!bs->drv) { 2364ea2384d3Sbellard buf[0] = '\0'; 2365ea2384d3Sbellard } else { 2366ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 2367ea2384d3Sbellard } 2368ea2384d3Sbellard } 2369ea2384d3Sbellard 2370ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2371ea2384d3Sbellard void *opaque) 2372ea2384d3Sbellard { 2373ea2384d3Sbellard BlockDriver *drv; 2374ea2384d3Sbellard 23758a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2376ea2384d3Sbellard it(opaque, drv->format_name); 2377ea2384d3Sbellard } 2378ea2384d3Sbellard } 2379ea2384d3Sbellard 2380b338082bSbellard BlockDriverState *bdrv_find(const char *name) 2381b338082bSbellard { 2382b338082bSbellard BlockDriverState *bs; 2383b338082bSbellard 23841b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 23851b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 2386b338082bSbellard return bs; 2387b338082bSbellard } 23881b7bdbc1SStefan Hajnoczi } 2389b338082bSbellard return NULL; 2390b338082bSbellard } 2391b338082bSbellard 23922f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 23932f399b0aSMarkus Armbruster { 23942f399b0aSMarkus Armbruster if (!bs) { 23952f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 23962f399b0aSMarkus Armbruster } 23972f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 23982f399b0aSMarkus Armbruster } 23992f399b0aSMarkus Armbruster 240051de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 240181d0912dSbellard { 240281d0912dSbellard BlockDriverState *bs; 240381d0912dSbellard 24041b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 240551de9760Saliguori it(opaque, bs); 240681d0912dSbellard } 240781d0912dSbellard } 240881d0912dSbellard 2409ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 2410ea2384d3Sbellard { 2411ea2384d3Sbellard return bs->device_name; 2412ea2384d3Sbellard } 2413ea2384d3Sbellard 2414c6ca28d6Saliguori void bdrv_flush_all(void) 2415c6ca28d6Saliguori { 2416c6ca28d6Saliguori BlockDriverState *bs; 2417c6ca28d6Saliguori 24181b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2419c6ca28d6Saliguori bdrv_flush(bs); 2420c6ca28d6Saliguori } 24211b7bdbc1SStefan Hajnoczi } 2422c6ca28d6Saliguori 2423f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2424f2feebbdSKevin Wolf { 2425f2feebbdSKevin Wolf assert(bs->drv); 2426f2feebbdSKevin Wolf 2427336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2428336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2429f2feebbdSKevin Wolf } 2430f2feebbdSKevin Wolf 2431f2feebbdSKevin Wolf return 1; 2432f2feebbdSKevin Wolf } 2433f2feebbdSKevin Wolf 2434376ae3f1SStefan Hajnoczi typedef struct BdrvCoIsAllocatedData { 2435376ae3f1SStefan Hajnoczi BlockDriverState *bs; 2436376ae3f1SStefan Hajnoczi int64_t sector_num; 2437376ae3f1SStefan Hajnoczi int nb_sectors; 2438376ae3f1SStefan Hajnoczi int *pnum; 2439376ae3f1SStefan Hajnoczi int ret; 2440376ae3f1SStefan Hajnoczi bool done; 2441376ae3f1SStefan Hajnoczi } BdrvCoIsAllocatedData; 2442376ae3f1SStefan Hajnoczi 2443f58c7b35Sths /* 2444f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 2445f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 2446f58c7b35Sths * hence all their sectors are reported as allocated. 2447f58c7b35Sths * 2448bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 2449bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 2450bd9533e3SStefan Hajnoczi * 2451f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 2452f58c7b35Sths * the specified sector) that are known to be in the same 2453f58c7b35Sths * allocated/unallocated state. 2454f58c7b35Sths * 2455bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2456bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 2457f58c7b35Sths */ 2458060f51c9SStefan Hajnoczi int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, 2459060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 2460f58c7b35Sths { 2461f58c7b35Sths int64_t n; 2462bd9533e3SStefan Hajnoczi 24636aebab14SStefan Hajnoczi if (sector_num >= bs->total_sectors) { 24646aebab14SStefan Hajnoczi *pnum = 0; 24656aebab14SStefan Hajnoczi return 0; 24666aebab14SStefan Hajnoczi } 2467bd9533e3SStefan Hajnoczi 24686aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 2469bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 2470bd9533e3SStefan Hajnoczi nb_sectors = n; 2471bd9533e3SStefan Hajnoczi } 2472bd9533e3SStefan Hajnoczi 2473bd9533e3SStefan Hajnoczi if (!bs->drv->bdrv_co_is_allocated) { 2474bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 24756aebab14SStefan Hajnoczi return 1; 24766aebab14SStefan Hajnoczi } 24776aebab14SStefan Hajnoczi 2478060f51c9SStefan Hajnoczi return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum); 2479060f51c9SStefan Hajnoczi } 2480060f51c9SStefan Hajnoczi 2481060f51c9SStefan Hajnoczi /* Coroutine wrapper for bdrv_is_allocated() */ 2482060f51c9SStefan Hajnoczi static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) 2483060f51c9SStefan Hajnoczi { 2484060f51c9SStefan Hajnoczi BdrvCoIsAllocatedData *data = opaque; 2485060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 2486060f51c9SStefan Hajnoczi 2487060f51c9SStefan Hajnoczi data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, 2488060f51c9SStefan Hajnoczi data->pnum); 2489060f51c9SStefan Hajnoczi data->done = true; 2490060f51c9SStefan Hajnoczi } 2491060f51c9SStefan Hajnoczi 2492060f51c9SStefan Hajnoczi /* 2493060f51c9SStefan Hajnoczi * Synchronous wrapper around bdrv_co_is_allocated(). 2494060f51c9SStefan Hajnoczi * 2495060f51c9SStefan Hajnoczi * See bdrv_co_is_allocated() for details. 2496060f51c9SStefan Hajnoczi */ 2497060f51c9SStefan Hajnoczi int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 2498060f51c9SStefan Hajnoczi int *pnum) 2499060f51c9SStefan Hajnoczi { 2500376ae3f1SStefan Hajnoczi Coroutine *co; 2501376ae3f1SStefan Hajnoczi BdrvCoIsAllocatedData data = { 2502376ae3f1SStefan Hajnoczi .bs = bs, 2503376ae3f1SStefan Hajnoczi .sector_num = sector_num, 2504376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 2505376ae3f1SStefan Hajnoczi .pnum = pnum, 2506376ae3f1SStefan Hajnoczi .done = false, 2507376ae3f1SStefan Hajnoczi }; 2508376ae3f1SStefan Hajnoczi 2509376ae3f1SStefan Hajnoczi co = qemu_coroutine_create(bdrv_is_allocated_co_entry); 2510376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 2511376ae3f1SStefan Hajnoczi while (!data.done) { 2512376ae3f1SStefan Hajnoczi qemu_aio_wait(); 2513376ae3f1SStefan Hajnoczi } 2514376ae3f1SStefan Hajnoczi return data.ret; 2515376ae3f1SStefan Hajnoczi } 2516f58c7b35Sths 2517b2023818SLuiz Capitulino BlockInfoList *qmp_query_block(Error **errp) 2518b338082bSbellard { 2519b2023818SLuiz Capitulino BlockInfoList *head = NULL, *cur_item = NULL; 2520d15e5465SLuiz Capitulino BlockDriverState *bs; 2521d15e5465SLuiz Capitulino 25221b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2523b2023818SLuiz Capitulino BlockInfoList *info = g_malloc0(sizeof(*info)); 2524d15e5465SLuiz Capitulino 2525b2023818SLuiz Capitulino info->value = g_malloc0(sizeof(*info->value)); 2526b2023818SLuiz Capitulino info->value->device = g_strdup(bs->device_name); 2527b2023818SLuiz Capitulino info->value->type = g_strdup("unknown"); 2528b2023818SLuiz Capitulino info->value->locked = bdrv_dev_is_medium_locked(bs); 2529b2023818SLuiz Capitulino info->value->removable = bdrv_dev_has_removable_media(bs); 2530d15e5465SLuiz Capitulino 2531e4def80bSMarkus Armbruster if (bdrv_dev_has_removable_media(bs)) { 2532b2023818SLuiz Capitulino info->value->has_tray_open = true; 2533b2023818SLuiz Capitulino info->value->tray_open = bdrv_dev_is_tray_open(bs); 2534e4def80bSMarkus Armbruster } 2535f04ef601SLuiz Capitulino 2536f04ef601SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 2537b2023818SLuiz Capitulino info->value->has_io_status = true; 2538b2023818SLuiz Capitulino info->value->io_status = bs->iostatus; 2539f04ef601SLuiz Capitulino } 2540f04ef601SLuiz Capitulino 2541d15e5465SLuiz Capitulino if (bs->drv) { 2542b2023818SLuiz Capitulino info->value->has_inserted = true; 2543b2023818SLuiz Capitulino info->value->inserted = g_malloc0(sizeof(*info->value->inserted)); 2544b2023818SLuiz Capitulino info->value->inserted->file = g_strdup(bs->filename); 2545b2023818SLuiz Capitulino info->value->inserted->ro = bs->read_only; 2546b2023818SLuiz Capitulino info->value->inserted->drv = g_strdup(bs->drv->format_name); 2547b2023818SLuiz Capitulino info->value->inserted->encrypted = bs->encrypted; 2548b2023818SLuiz Capitulino if (bs->backing_file[0]) { 2549b2023818SLuiz Capitulino info->value->inserted->has_backing_file = true; 2550b2023818SLuiz Capitulino info->value->inserted->backing_file = g_strdup(bs->backing_file); 2551b2023818SLuiz Capitulino } 2552727f005eSZhi Yong Wu 2553727f005eSZhi Yong Wu if (bs->io_limits_enabled) { 2554727f005eSZhi Yong Wu info->value->inserted->bps = 2555727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 2556727f005eSZhi Yong Wu info->value->inserted->bps_rd = 2557727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; 2558727f005eSZhi Yong Wu info->value->inserted->bps_wr = 2559727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; 2560727f005eSZhi Yong Wu info->value->inserted->iops = 2561727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 2562727f005eSZhi Yong Wu info->value->inserted->iops_rd = 2563727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; 2564727f005eSZhi Yong Wu info->value->inserted->iops_wr = 2565727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; 2566727f005eSZhi Yong Wu } 2567d15e5465SLuiz Capitulino } 2568d15e5465SLuiz Capitulino 2569b2023818SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2570b2023818SLuiz Capitulino if (!cur_item) { 2571b2023818SLuiz Capitulino head = cur_item = info; 2572b2023818SLuiz Capitulino } else { 2573b2023818SLuiz Capitulino cur_item->next = info; 2574b2023818SLuiz Capitulino cur_item = info; 2575d15e5465SLuiz Capitulino } 2576d15e5465SLuiz Capitulino } 2577d15e5465SLuiz Capitulino 2578b2023818SLuiz Capitulino return head; 2579b338082bSbellard } 2580a36e69ddSths 2581f11f57e4SLuiz Capitulino /* Consider exposing this as a full fledged QMP command */ 2582f11f57e4SLuiz Capitulino static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp) 2583a36e69ddSths { 2584f11f57e4SLuiz Capitulino BlockStats *s; 2585218a536aSLuiz Capitulino 2586f11f57e4SLuiz Capitulino s = g_malloc0(sizeof(*s)); 2587218a536aSLuiz Capitulino 2588f11f57e4SLuiz Capitulino if (bs->device_name[0]) { 2589f11f57e4SLuiz Capitulino s->has_device = true; 2590f11f57e4SLuiz Capitulino s->device = g_strdup(bs->device_name); 2591218a536aSLuiz Capitulino } 2592218a536aSLuiz Capitulino 2593f11f57e4SLuiz Capitulino s->stats = g_malloc0(sizeof(*s->stats)); 2594f11f57e4SLuiz Capitulino s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; 2595f11f57e4SLuiz Capitulino s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; 2596f11f57e4SLuiz Capitulino s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; 2597f11f57e4SLuiz Capitulino s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; 2598f11f57e4SLuiz Capitulino s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; 2599f11f57e4SLuiz Capitulino s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; 2600f11f57e4SLuiz Capitulino s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; 2601f11f57e4SLuiz Capitulino s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; 2602f11f57e4SLuiz Capitulino s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; 2603294cc35fSKevin Wolf 2604294cc35fSKevin Wolf if (bs->file) { 2605f11f57e4SLuiz Capitulino s->has_parent = true; 2606f11f57e4SLuiz Capitulino s->parent = qmp_query_blockstat(bs->file, NULL); 2607294cc35fSKevin Wolf } 2608294cc35fSKevin Wolf 2609f11f57e4SLuiz Capitulino return s; 2610294cc35fSKevin Wolf } 2611294cc35fSKevin Wolf 2612f11f57e4SLuiz Capitulino BlockStatsList *qmp_query_blockstats(Error **errp) 2613218a536aSLuiz Capitulino { 2614f11f57e4SLuiz Capitulino BlockStatsList *head = NULL, *cur_item = NULL; 2615a36e69ddSths BlockDriverState *bs; 2616a36e69ddSths 26171b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2618f11f57e4SLuiz Capitulino BlockStatsList *info = g_malloc0(sizeof(*info)); 2619f11f57e4SLuiz Capitulino info->value = qmp_query_blockstat(bs, NULL); 2620f11f57e4SLuiz Capitulino 2621f11f57e4SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2622f11f57e4SLuiz Capitulino if (!cur_item) { 2623f11f57e4SLuiz Capitulino head = cur_item = info; 2624f11f57e4SLuiz Capitulino } else { 2625f11f57e4SLuiz Capitulino cur_item->next = info; 2626f11f57e4SLuiz Capitulino cur_item = info; 2627f11f57e4SLuiz Capitulino } 2628a36e69ddSths } 2629218a536aSLuiz Capitulino 2630f11f57e4SLuiz Capitulino return head; 2631a36e69ddSths } 2632ea2384d3Sbellard 2633045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 2634045df330Saliguori { 2635045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 2636045df330Saliguori return bs->backing_file; 2637045df330Saliguori else if (bs->encrypted) 2638045df330Saliguori return bs->filename; 2639045df330Saliguori else 2640045df330Saliguori return NULL; 2641045df330Saliguori } 2642045df330Saliguori 264383f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 264483f64091Sbellard char *filename, int filename_size) 264583f64091Sbellard { 264683f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 264783f64091Sbellard } 264883f64091Sbellard 2649faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 2650faea38e7Sbellard const uint8_t *buf, int nb_sectors) 2651faea38e7Sbellard { 2652faea38e7Sbellard BlockDriver *drv = bs->drv; 2653faea38e7Sbellard if (!drv) 265419cb3738Sbellard return -ENOMEDIUM; 2655faea38e7Sbellard if (!drv->bdrv_write_compressed) 2656faea38e7Sbellard return -ENOTSUP; 2657fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 2658fbb7b4e0SKevin Wolf return -EIO; 26597cd1e32aSlirans@il.ibm.com 2660c6d22830SJan Kiszka if (bs->dirty_bitmap) { 26617cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 26627cd1e32aSlirans@il.ibm.com } 26637cd1e32aSlirans@il.ibm.com 2664faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 2665faea38e7Sbellard } 2666faea38e7Sbellard 2667faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2668faea38e7Sbellard { 2669faea38e7Sbellard BlockDriver *drv = bs->drv; 2670faea38e7Sbellard if (!drv) 267119cb3738Sbellard return -ENOMEDIUM; 2672faea38e7Sbellard if (!drv->bdrv_get_info) 2673faea38e7Sbellard return -ENOTSUP; 2674faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 2675faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 2676faea38e7Sbellard } 2677faea38e7Sbellard 267845566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 267945566e9cSChristoph Hellwig int64_t pos, int size) 2680178e08a5Saliguori { 2681178e08a5Saliguori BlockDriver *drv = bs->drv; 2682178e08a5Saliguori if (!drv) 2683178e08a5Saliguori return -ENOMEDIUM; 26847cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 268545566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 26867cdb1f6dSMORITA Kazutaka if (bs->file) 26877cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 26887cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2689178e08a5Saliguori } 2690178e08a5Saliguori 269145566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 269245566e9cSChristoph Hellwig int64_t pos, int size) 2693178e08a5Saliguori { 2694178e08a5Saliguori BlockDriver *drv = bs->drv; 2695178e08a5Saliguori if (!drv) 2696178e08a5Saliguori return -ENOMEDIUM; 26977cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 269845566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 26997cdb1f6dSMORITA Kazutaka if (bs->file) 27007cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 27017cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2702178e08a5Saliguori } 2703178e08a5Saliguori 27048b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 27058b9b0cc2SKevin Wolf { 27068b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 27078b9b0cc2SKevin Wolf 27088b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 27098b9b0cc2SKevin Wolf return; 27108b9b0cc2SKevin Wolf } 27118b9b0cc2SKevin Wolf 27128b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 27138b9b0cc2SKevin Wolf 27148b9b0cc2SKevin Wolf } 27158b9b0cc2SKevin Wolf 2716faea38e7Sbellard /**************************************************************/ 2717faea38e7Sbellard /* handling of snapshots */ 2718faea38e7Sbellard 2719feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 2720feeee5acSMiguel Di Ciurcio Filho { 2721feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 272207b70bfbSMarkus Armbruster if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 2723feeee5acSMiguel Di Ciurcio Filho return 0; 2724feeee5acSMiguel Di Ciurcio Filho } 2725feeee5acSMiguel Di Ciurcio Filho 2726feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 2727feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 2728feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 2729feeee5acSMiguel Di Ciurcio Filho } 2730feeee5acSMiguel Di Ciurcio Filho return 0; 2731feeee5acSMiguel Di Ciurcio Filho } 2732feeee5acSMiguel Di Ciurcio Filho 2733feeee5acSMiguel Di Ciurcio Filho return 1; 2734feeee5acSMiguel Di Ciurcio Filho } 2735feeee5acSMiguel Di Ciurcio Filho 2736199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 2737199630b6SBlue Swirl { 2738199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 2739199630b6SBlue Swirl } 2740199630b6SBlue Swirl 2741f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 2742f9092b10SMarkus Armbruster { 2743f9092b10SMarkus Armbruster BlockDriverState *bs; 2744f9092b10SMarkus Armbruster 27453ac906f7SMarkus Armbruster if (bs_snapshots) { 2746f9092b10SMarkus Armbruster return bs_snapshots; 27473ac906f7SMarkus Armbruster } 2748f9092b10SMarkus Armbruster 2749f9092b10SMarkus Armbruster bs = NULL; 2750f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 2751f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 27523ac906f7SMarkus Armbruster bs_snapshots = bs; 27533ac906f7SMarkus Armbruster return bs; 2754f9092b10SMarkus Armbruster } 2755f9092b10SMarkus Armbruster } 2756f9092b10SMarkus Armbruster return NULL; 2757f9092b10SMarkus Armbruster } 2758f9092b10SMarkus Armbruster 2759faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 2760faea38e7Sbellard QEMUSnapshotInfo *sn_info) 2761faea38e7Sbellard { 2762faea38e7Sbellard BlockDriver *drv = bs->drv; 2763faea38e7Sbellard if (!drv) 276419cb3738Sbellard return -ENOMEDIUM; 27657cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 2766faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 27677cdb1f6dSMORITA Kazutaka if (bs->file) 27687cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 27697cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2770faea38e7Sbellard } 2771faea38e7Sbellard 2772faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 2773faea38e7Sbellard const char *snapshot_id) 2774faea38e7Sbellard { 2775faea38e7Sbellard BlockDriver *drv = bs->drv; 27767cdb1f6dSMORITA Kazutaka int ret, open_ret; 27777cdb1f6dSMORITA Kazutaka 2778faea38e7Sbellard if (!drv) 277919cb3738Sbellard return -ENOMEDIUM; 27807cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 2781faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 27827cdb1f6dSMORITA Kazutaka 27837cdb1f6dSMORITA Kazutaka if (bs->file) { 27847cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 27857cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 27867cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 27877cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 27887cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 27897cdb1f6dSMORITA Kazutaka bs->drv = NULL; 27907cdb1f6dSMORITA Kazutaka return open_ret; 27917cdb1f6dSMORITA Kazutaka } 27927cdb1f6dSMORITA Kazutaka return ret; 27937cdb1f6dSMORITA Kazutaka } 27947cdb1f6dSMORITA Kazutaka 27957cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2796faea38e7Sbellard } 2797faea38e7Sbellard 2798faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 2799faea38e7Sbellard { 2800faea38e7Sbellard BlockDriver *drv = bs->drv; 2801faea38e7Sbellard if (!drv) 280219cb3738Sbellard return -ENOMEDIUM; 28037cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 2804faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 28057cdb1f6dSMORITA Kazutaka if (bs->file) 28067cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 28077cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2808faea38e7Sbellard } 2809faea38e7Sbellard 2810faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 2811faea38e7Sbellard QEMUSnapshotInfo **psn_info) 2812faea38e7Sbellard { 2813faea38e7Sbellard BlockDriver *drv = bs->drv; 2814faea38e7Sbellard if (!drv) 281519cb3738Sbellard return -ENOMEDIUM; 28167cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 2817faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 28187cdb1f6dSMORITA Kazutaka if (bs->file) 28197cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 28207cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2821faea38e7Sbellard } 2822faea38e7Sbellard 282351ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 282451ef6727Sedison const char *snapshot_name) 282551ef6727Sedison { 282651ef6727Sedison BlockDriver *drv = bs->drv; 282751ef6727Sedison if (!drv) { 282851ef6727Sedison return -ENOMEDIUM; 282951ef6727Sedison } 283051ef6727Sedison if (!bs->read_only) { 283151ef6727Sedison return -EINVAL; 283251ef6727Sedison } 283351ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 283451ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 283551ef6727Sedison } 283651ef6727Sedison return -ENOTSUP; 283751ef6727Sedison } 283851ef6727Sedison 2839e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 2840e8a6bb9cSMarcelo Tosatti const char *backing_file) 2841e8a6bb9cSMarcelo Tosatti { 2842e8a6bb9cSMarcelo Tosatti if (!bs->drv) { 2843e8a6bb9cSMarcelo Tosatti return NULL; 2844e8a6bb9cSMarcelo Tosatti } 2845e8a6bb9cSMarcelo Tosatti 2846e8a6bb9cSMarcelo Tosatti if (bs->backing_hd) { 2847e8a6bb9cSMarcelo Tosatti if (strcmp(bs->backing_file, backing_file) == 0) { 2848e8a6bb9cSMarcelo Tosatti return bs->backing_hd; 2849e8a6bb9cSMarcelo Tosatti } else { 2850e8a6bb9cSMarcelo Tosatti return bdrv_find_backing_image(bs->backing_hd, backing_file); 2851e8a6bb9cSMarcelo Tosatti } 2852e8a6bb9cSMarcelo Tosatti } 2853e8a6bb9cSMarcelo Tosatti 2854e8a6bb9cSMarcelo Tosatti return NULL; 2855e8a6bb9cSMarcelo Tosatti } 2856e8a6bb9cSMarcelo Tosatti 2857faea38e7Sbellard #define NB_SUFFIXES 4 2858faea38e7Sbellard 2859faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 2860faea38e7Sbellard { 2861faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 2862faea38e7Sbellard int64_t base; 2863faea38e7Sbellard int i; 2864faea38e7Sbellard 2865faea38e7Sbellard if (size <= 999) { 2866faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 2867faea38e7Sbellard } else { 2868faea38e7Sbellard base = 1024; 2869faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 2870faea38e7Sbellard if (size < (10 * base)) { 2871faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 2872faea38e7Sbellard (double)size / base, 2873faea38e7Sbellard suffixes[i]); 2874faea38e7Sbellard break; 2875faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 2876faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 2877faea38e7Sbellard ((size + (base >> 1)) / base), 2878faea38e7Sbellard suffixes[i]); 2879faea38e7Sbellard break; 2880faea38e7Sbellard } 2881faea38e7Sbellard base = base * 1024; 2882faea38e7Sbellard } 2883faea38e7Sbellard } 2884faea38e7Sbellard return buf; 2885faea38e7Sbellard } 2886faea38e7Sbellard 2887faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 2888faea38e7Sbellard { 2889faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 28903b9f94e1Sbellard #ifdef _WIN32 28913b9f94e1Sbellard struct tm *ptm; 28923b9f94e1Sbellard #else 2893faea38e7Sbellard struct tm tm; 28943b9f94e1Sbellard #endif 2895faea38e7Sbellard time_t ti; 2896faea38e7Sbellard int64_t secs; 2897faea38e7Sbellard 2898faea38e7Sbellard if (!sn) { 2899faea38e7Sbellard snprintf(buf, buf_size, 2900faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2901faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 2902faea38e7Sbellard } else { 2903faea38e7Sbellard ti = sn->date_sec; 29043b9f94e1Sbellard #ifdef _WIN32 29053b9f94e1Sbellard ptm = localtime(&ti); 29063b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 29073b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 29083b9f94e1Sbellard #else 2909faea38e7Sbellard localtime_r(&ti, &tm); 2910faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 2911faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 29123b9f94e1Sbellard #endif 2913faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 2914faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 2915faea38e7Sbellard "%02d:%02d:%02d.%03d", 2916faea38e7Sbellard (int)(secs / 3600), 2917faea38e7Sbellard (int)((secs / 60) % 60), 2918faea38e7Sbellard (int)(secs % 60), 2919faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 2920faea38e7Sbellard snprintf(buf, buf_size, 2921faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2922faea38e7Sbellard sn->id_str, sn->name, 2923faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 2924faea38e7Sbellard date_buf, 2925faea38e7Sbellard clock_buf); 2926faea38e7Sbellard } 2927faea38e7Sbellard return buf; 2928faea38e7Sbellard } 2929faea38e7Sbellard 2930ea2384d3Sbellard /**************************************************************/ 293183f64091Sbellard /* async I/Os */ 2932ea2384d3Sbellard 29333b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 2934f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 293583f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 2936ea2384d3Sbellard { 2937bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 2938bbf0a440SStefan Hajnoczi 2939b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 29408c5873d6SStefan Hajnoczi cb, opaque, false); 294183f64091Sbellard } 294283f64091Sbellard 2943f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 2944f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 294583f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 29467674e7bfSbellard { 2947bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 2948bbf0a440SStefan Hajnoczi 29491a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 29508c5873d6SStefan Hajnoczi cb, opaque, true); 295183f64091Sbellard } 295283f64091Sbellard 295340b4f539SKevin Wolf 295440b4f539SKevin Wolf typedef struct MultiwriteCB { 295540b4f539SKevin Wolf int error; 295640b4f539SKevin Wolf int num_requests; 295740b4f539SKevin Wolf int num_callbacks; 295840b4f539SKevin Wolf struct { 295940b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 296040b4f539SKevin Wolf void *opaque; 296140b4f539SKevin Wolf QEMUIOVector *free_qiov; 296240b4f539SKevin Wolf } callbacks[]; 296340b4f539SKevin Wolf } MultiwriteCB; 296440b4f539SKevin Wolf 296540b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 296640b4f539SKevin Wolf { 296740b4f539SKevin Wolf int i; 296840b4f539SKevin Wolf 296940b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 297040b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 29711e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 29721e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 29731e1ea48dSStefan Hajnoczi } 29747267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 297540b4f539SKevin Wolf } 297640b4f539SKevin Wolf } 297740b4f539SKevin Wolf 297840b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 297940b4f539SKevin Wolf { 298040b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 298140b4f539SKevin Wolf 29826d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 29836d519a5fSStefan Hajnoczi 2984cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 298540b4f539SKevin Wolf mcb->error = ret; 298640b4f539SKevin Wolf } 298740b4f539SKevin Wolf 298840b4f539SKevin Wolf mcb->num_requests--; 298940b4f539SKevin Wolf if (mcb->num_requests == 0) { 299040b4f539SKevin Wolf multiwrite_user_cb(mcb); 29917267c094SAnthony Liguori g_free(mcb); 299240b4f539SKevin Wolf } 299340b4f539SKevin Wolf } 299440b4f539SKevin Wolf 299540b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 299640b4f539SKevin Wolf { 299777be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 299877be4366SChristoph Hellwig 299977be4366SChristoph Hellwig /* 300077be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 300177be4366SChristoph Hellwig * here as that could overflow the return value. 300277be4366SChristoph Hellwig */ 300377be4366SChristoph Hellwig if (req1->sector > req2->sector) { 300477be4366SChristoph Hellwig return 1; 300577be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 300677be4366SChristoph Hellwig return -1; 300777be4366SChristoph Hellwig } else { 300877be4366SChristoph Hellwig return 0; 300977be4366SChristoph Hellwig } 301040b4f539SKevin Wolf } 301140b4f539SKevin Wolf 301240b4f539SKevin Wolf /* 301340b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 301440b4f539SKevin Wolf * requests that remain after merging. 301540b4f539SKevin Wolf */ 301640b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 301740b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 301840b4f539SKevin Wolf { 301940b4f539SKevin Wolf int i, outidx; 302040b4f539SKevin Wolf 302140b4f539SKevin Wolf // Sort requests by start sector 302240b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 302340b4f539SKevin Wolf 302440b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 302540b4f539SKevin Wolf // filling up gaps with zero sectors. 302640b4f539SKevin Wolf outidx = 0; 302740b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 302840b4f539SKevin Wolf int merge = 0; 302940b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 303040b4f539SKevin Wolf 3031b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 303240b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 303340b4f539SKevin Wolf merge = 1; 303440b4f539SKevin Wolf } 303540b4f539SKevin Wolf 3036e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3037e2a305fbSChristoph Hellwig merge = 0; 3038e2a305fbSChristoph Hellwig } 3039e2a305fbSChristoph Hellwig 304040b4f539SKevin Wolf if (merge) { 304140b4f539SKevin Wolf size_t size; 30427267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 304340b4f539SKevin Wolf qemu_iovec_init(qiov, 304440b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 304540b4f539SKevin Wolf 304640b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 304740b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 304840b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 304940b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 305040b4f539SKevin Wolf 3051b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3052b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 305340b4f539SKevin Wolf 305440b4f539SKevin Wolf // Add the second request 305540b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 305640b4f539SKevin Wolf 3057cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 305840b4f539SKevin Wolf reqs[outidx].qiov = qiov; 305940b4f539SKevin Wolf 306040b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 306140b4f539SKevin Wolf } else { 306240b4f539SKevin Wolf outidx++; 306340b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 306440b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 306540b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 306640b4f539SKevin Wolf } 306740b4f539SKevin Wolf } 306840b4f539SKevin Wolf 306940b4f539SKevin Wolf return outidx + 1; 307040b4f539SKevin Wolf } 307140b4f539SKevin Wolf 307240b4f539SKevin Wolf /* 307340b4f539SKevin Wolf * Submit multiple AIO write requests at once. 307440b4f539SKevin Wolf * 307540b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 307640b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 307740b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 307840b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 307940b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 308040b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 308140b4f539SKevin Wolf * 308240b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 308340b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 308440b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 308540b4f539SKevin Wolf */ 308640b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 308740b4f539SKevin Wolf { 308840b4f539SKevin Wolf MultiwriteCB *mcb; 308940b4f539SKevin Wolf int i; 309040b4f539SKevin Wolf 3091301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3092301db7c2SRyan Harper if (bs->drv == NULL) { 3093301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3094301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3095301db7c2SRyan Harper } 3096301db7c2SRyan Harper return -1; 3097301db7c2SRyan Harper } 3098301db7c2SRyan Harper 309940b4f539SKevin Wolf if (num_reqs == 0) { 310040b4f539SKevin Wolf return 0; 310140b4f539SKevin Wolf } 310240b4f539SKevin Wolf 310340b4f539SKevin Wolf // Create MultiwriteCB structure 31047267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 310540b4f539SKevin Wolf mcb->num_requests = 0; 310640b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 310740b4f539SKevin Wolf 310840b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 310940b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 311040b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 311140b4f539SKevin Wolf } 311240b4f539SKevin Wolf 311340b4f539SKevin Wolf // Check for mergable requests 311440b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 311540b4f539SKevin Wolf 31166d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 31176d519a5fSStefan Hajnoczi 3118df9309fbSPaolo Bonzini /* Run the aio requests. */ 3119df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 312040b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3121ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 312240b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 312340b4f539SKevin Wolf } 312440b4f539SKevin Wolf 312540b4f539SKevin Wolf return 0; 312640b4f539SKevin Wolf } 312740b4f539SKevin Wolf 312883f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 312983f64091Sbellard { 31306bbff9a0Saliguori acb->pool->cancel(acb); 313183f64091Sbellard } 313283f64091Sbellard 313398f90dbaSZhi Yong Wu /* block I/O throttling */ 313498f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 313598f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait) 313698f90dbaSZhi Yong Wu { 313798f90dbaSZhi Yong Wu uint64_t bps_limit = 0; 313898f90dbaSZhi Yong Wu double bytes_limit, bytes_base, bytes_res; 313998f90dbaSZhi Yong Wu double slice_time, wait_time; 314098f90dbaSZhi Yong Wu 314198f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 314298f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 314398f90dbaSZhi Yong Wu } else if (bs->io_limits.bps[is_write]) { 314498f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[is_write]; 314598f90dbaSZhi Yong Wu } else { 314698f90dbaSZhi Yong Wu if (wait) { 314798f90dbaSZhi Yong Wu *wait = 0; 314898f90dbaSZhi Yong Wu } 314998f90dbaSZhi Yong Wu 315098f90dbaSZhi Yong Wu return false; 315198f90dbaSZhi Yong Wu } 315298f90dbaSZhi Yong Wu 315398f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 315498f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 315598f90dbaSZhi Yong Wu bytes_limit = bps_limit * slice_time; 315698f90dbaSZhi Yong Wu bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; 315798f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 315898f90dbaSZhi Yong Wu bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; 315998f90dbaSZhi Yong Wu } 316098f90dbaSZhi Yong Wu 316198f90dbaSZhi Yong Wu /* bytes_base: the bytes of data which have been read/written; and 316298f90dbaSZhi Yong Wu * it is obtained from the history statistic info. 316398f90dbaSZhi Yong Wu * bytes_res: the remaining bytes of data which need to be read/written. 316498f90dbaSZhi Yong Wu * (bytes_base + bytes_res) / bps_limit: used to calcuate 316598f90dbaSZhi Yong Wu * the total time for completing reading/writting all data. 316698f90dbaSZhi Yong Wu */ 316798f90dbaSZhi Yong Wu bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 316898f90dbaSZhi Yong Wu 316998f90dbaSZhi Yong Wu if (bytes_base + bytes_res <= bytes_limit) { 317098f90dbaSZhi Yong Wu if (wait) { 317198f90dbaSZhi Yong Wu *wait = 0; 317298f90dbaSZhi Yong Wu } 317398f90dbaSZhi Yong Wu 317498f90dbaSZhi Yong Wu return false; 317598f90dbaSZhi Yong Wu } 317698f90dbaSZhi Yong Wu 317798f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 317898f90dbaSZhi Yong Wu wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; 317998f90dbaSZhi Yong Wu 318098f90dbaSZhi Yong Wu /* When the I/O rate at runtime exceeds the limits, 318198f90dbaSZhi Yong Wu * bs->slice_end need to be extended in order that the current statistic 318298f90dbaSZhi Yong Wu * info can be kept until the timer fire, so it is increased and tuned 318398f90dbaSZhi Yong Wu * based on the result of experiment. 318498f90dbaSZhi Yong Wu */ 318598f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 318698f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 318798f90dbaSZhi Yong Wu if (wait) { 318898f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 318998f90dbaSZhi Yong Wu } 319098f90dbaSZhi Yong Wu 319198f90dbaSZhi Yong Wu return true; 319298f90dbaSZhi Yong Wu } 319398f90dbaSZhi Yong Wu 319498f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 319598f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait) 319698f90dbaSZhi Yong Wu { 319798f90dbaSZhi Yong Wu uint64_t iops_limit = 0; 319898f90dbaSZhi Yong Wu double ios_limit, ios_base; 319998f90dbaSZhi Yong Wu double slice_time, wait_time; 320098f90dbaSZhi Yong Wu 320198f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 320298f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 320398f90dbaSZhi Yong Wu } else if (bs->io_limits.iops[is_write]) { 320498f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[is_write]; 320598f90dbaSZhi Yong Wu } else { 320698f90dbaSZhi Yong Wu if (wait) { 320798f90dbaSZhi Yong Wu *wait = 0; 320898f90dbaSZhi Yong Wu } 320998f90dbaSZhi Yong Wu 321098f90dbaSZhi Yong Wu return false; 321198f90dbaSZhi Yong Wu } 321298f90dbaSZhi Yong Wu 321398f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 321498f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 321598f90dbaSZhi Yong Wu ios_limit = iops_limit * slice_time; 321698f90dbaSZhi Yong Wu ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; 321798f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 321898f90dbaSZhi Yong Wu ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; 321998f90dbaSZhi Yong Wu } 322098f90dbaSZhi Yong Wu 322198f90dbaSZhi Yong Wu if (ios_base + 1 <= ios_limit) { 322298f90dbaSZhi Yong Wu if (wait) { 322398f90dbaSZhi Yong Wu *wait = 0; 322498f90dbaSZhi Yong Wu } 322598f90dbaSZhi Yong Wu 322698f90dbaSZhi Yong Wu return false; 322798f90dbaSZhi Yong Wu } 322898f90dbaSZhi Yong Wu 322998f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 323098f90dbaSZhi Yong Wu wait_time = (ios_base + 1) / iops_limit; 323198f90dbaSZhi Yong Wu if (wait_time > elapsed_time) { 323298f90dbaSZhi Yong Wu wait_time = wait_time - elapsed_time; 323398f90dbaSZhi Yong Wu } else { 323498f90dbaSZhi Yong Wu wait_time = 0; 323598f90dbaSZhi Yong Wu } 323698f90dbaSZhi Yong Wu 323798f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 323898f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 323998f90dbaSZhi Yong Wu if (wait) { 324098f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 324198f90dbaSZhi Yong Wu } 324298f90dbaSZhi Yong Wu 324398f90dbaSZhi Yong Wu return true; 324498f90dbaSZhi Yong Wu } 324598f90dbaSZhi Yong Wu 324698f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 324798f90dbaSZhi Yong Wu bool is_write, int64_t *wait) 324898f90dbaSZhi Yong Wu { 324998f90dbaSZhi Yong Wu int64_t now, max_wait; 325098f90dbaSZhi Yong Wu uint64_t bps_wait = 0, iops_wait = 0; 325198f90dbaSZhi Yong Wu double elapsed_time; 325298f90dbaSZhi Yong Wu int bps_ret, iops_ret; 325398f90dbaSZhi Yong Wu 325498f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 325598f90dbaSZhi Yong Wu if ((bs->slice_start < now) 325698f90dbaSZhi Yong Wu && (bs->slice_end > now)) { 325798f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 325898f90dbaSZhi Yong Wu } else { 325998f90dbaSZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 326098f90dbaSZhi Yong Wu bs->slice_start = now; 326198f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 326298f90dbaSZhi Yong Wu 326398f90dbaSZhi Yong Wu bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; 326498f90dbaSZhi Yong Wu bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; 326598f90dbaSZhi Yong Wu 326698f90dbaSZhi Yong Wu bs->io_base.ios[is_write] = bs->nr_ops[is_write]; 326798f90dbaSZhi Yong Wu bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; 326898f90dbaSZhi Yong Wu } 326998f90dbaSZhi Yong Wu 327098f90dbaSZhi Yong Wu elapsed_time = now - bs->slice_start; 327198f90dbaSZhi Yong Wu elapsed_time /= (NANOSECONDS_PER_SECOND); 327298f90dbaSZhi Yong Wu 327398f90dbaSZhi Yong Wu bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, 327498f90dbaSZhi Yong Wu is_write, elapsed_time, &bps_wait); 327598f90dbaSZhi Yong Wu iops_ret = bdrv_exceed_iops_limits(bs, is_write, 327698f90dbaSZhi Yong Wu elapsed_time, &iops_wait); 327798f90dbaSZhi Yong Wu if (bps_ret || iops_ret) { 327898f90dbaSZhi Yong Wu max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; 327998f90dbaSZhi Yong Wu if (wait) { 328098f90dbaSZhi Yong Wu *wait = max_wait; 328198f90dbaSZhi Yong Wu } 328298f90dbaSZhi Yong Wu 328398f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 328498f90dbaSZhi Yong Wu if (bs->slice_end < now + max_wait) { 328598f90dbaSZhi Yong Wu bs->slice_end = now + max_wait; 328698f90dbaSZhi Yong Wu } 328798f90dbaSZhi Yong Wu 328898f90dbaSZhi Yong Wu return true; 328998f90dbaSZhi Yong Wu } 329098f90dbaSZhi Yong Wu 329198f90dbaSZhi Yong Wu if (wait) { 329298f90dbaSZhi Yong Wu *wait = 0; 329398f90dbaSZhi Yong Wu } 329498f90dbaSZhi Yong Wu 329598f90dbaSZhi Yong Wu return false; 329698f90dbaSZhi Yong Wu } 329783f64091Sbellard 329883f64091Sbellard /**************************************************************/ 329983f64091Sbellard /* async block device emulation */ 330083f64091Sbellard 3301c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3302c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3303c16b5a2cSChristoph Hellwig QEMUBH *bh; 3304c16b5a2cSChristoph Hellwig int ret; 3305c16b5a2cSChristoph Hellwig /* vector translation state */ 3306c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3307c16b5a2cSChristoph Hellwig uint8_t *bounce; 3308c16b5a2cSChristoph Hellwig int is_write; 3309c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3310c16b5a2cSChristoph Hellwig 3311c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3312c16b5a2cSChristoph Hellwig { 3313b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3314b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 33156a7ad299SDor Laor qemu_bh_delete(acb->bh); 331636afc451SAvi Kivity acb->bh = NULL; 3317c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3318c16b5a2cSChristoph Hellwig } 3319c16b5a2cSChristoph Hellwig 3320c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 3321c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3322c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3323c16b5a2cSChristoph Hellwig }; 3324c16b5a2cSChristoph Hellwig 332583f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3326beac80cdSbellard { 3327ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3328f141eafeSaliguori 3329f141eafeSaliguori if (!acb->is_write) 3330f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 3331ceb42de8Saliguori qemu_vfree(acb->bounce); 3332ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 33336a7ad299SDor Laor qemu_bh_delete(acb->bh); 333436afc451SAvi Kivity acb->bh = NULL; 3335ce1a14dcSpbrook qemu_aio_release(acb); 3336beac80cdSbellard } 3337beac80cdSbellard 3338f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3339f141eafeSaliguori int64_t sector_num, 3340f141eafeSaliguori QEMUIOVector *qiov, 3341f141eafeSaliguori int nb_sectors, 3342f141eafeSaliguori BlockDriverCompletionFunc *cb, 3343f141eafeSaliguori void *opaque, 3344f141eafeSaliguori int is_write) 3345f141eafeSaliguori 3346ea2384d3Sbellard { 3347ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 334883f64091Sbellard 3349c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 3350f141eafeSaliguori acb->is_write = is_write; 3351f141eafeSaliguori acb->qiov = qiov; 3352e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3353ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3354f141eafeSaliguori 3355f141eafeSaliguori if (is_write) { 3356f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 33571ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3358f141eafeSaliguori } else { 33591ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3360f141eafeSaliguori } 3361f141eafeSaliguori 3362ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3363f141eafeSaliguori 3364ce1a14dcSpbrook return &acb->common; 33657a6cba61Spbrook } 33667a6cba61Spbrook 3367f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3368f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3369ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 337083f64091Sbellard { 3371f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 337283f64091Sbellard } 337383f64091Sbellard 3374f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3375f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3376f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3377f141eafeSaliguori { 3378f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3379f141eafeSaliguori } 3380f141eafeSaliguori 338168485420SKevin Wolf 338268485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 338368485420SKevin Wolf BlockDriverAIOCB common; 338468485420SKevin Wolf BlockRequest req; 338568485420SKevin Wolf bool is_write; 338668485420SKevin Wolf QEMUBH* bh; 338768485420SKevin Wolf } BlockDriverAIOCBCoroutine; 338868485420SKevin Wolf 338968485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 339068485420SKevin Wolf { 339168485420SKevin Wolf qemu_aio_flush(); 339268485420SKevin Wolf } 339368485420SKevin Wolf 339468485420SKevin Wolf static AIOPool bdrv_em_co_aio_pool = { 339568485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 339668485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 339768485420SKevin Wolf }; 339868485420SKevin Wolf 339935246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 340068485420SKevin Wolf { 340168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 340268485420SKevin Wolf 340368485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 340468485420SKevin Wolf qemu_bh_delete(acb->bh); 340568485420SKevin Wolf qemu_aio_release(acb); 340668485420SKevin Wolf } 340768485420SKevin Wolf 3408b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3409b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3410b2a61371SStefan Hajnoczi { 3411b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3412b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3413b2a61371SStefan Hajnoczi 3414b2a61371SStefan Hajnoczi if (!acb->is_write) { 3415b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 3416470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3417b2a61371SStefan Hajnoczi } else { 3418b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 3419f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3420b2a61371SStefan Hajnoczi } 3421b2a61371SStefan Hajnoczi 342235246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3423b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 3424b2a61371SStefan Hajnoczi } 3425b2a61371SStefan Hajnoczi 342668485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 342768485420SKevin Wolf int64_t sector_num, 342868485420SKevin Wolf QEMUIOVector *qiov, 342968485420SKevin Wolf int nb_sectors, 343068485420SKevin Wolf BlockDriverCompletionFunc *cb, 343168485420SKevin Wolf void *opaque, 34328c5873d6SStefan Hajnoczi bool is_write) 343368485420SKevin Wolf { 343468485420SKevin Wolf Coroutine *co; 343568485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 343668485420SKevin Wolf 343768485420SKevin Wolf acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 343868485420SKevin Wolf acb->req.sector = sector_num; 343968485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 344068485420SKevin Wolf acb->req.qiov = qiov; 344168485420SKevin Wolf acb->is_write = is_write; 344268485420SKevin Wolf 34438c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 344468485420SKevin Wolf qemu_coroutine_enter(co, acb); 344568485420SKevin Wolf 344668485420SKevin Wolf return &acb->common; 344768485420SKevin Wolf } 344868485420SKevin Wolf 344907f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 3450b2e12bc6SChristoph Hellwig { 345107f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 345207f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 3453b2e12bc6SChristoph Hellwig 345407f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 345507f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3456b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 3457b2e12bc6SChristoph Hellwig } 3458b2e12bc6SChristoph Hellwig 345907f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 3460016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 3461016f5cf6SAlexander Graf { 346207f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 3463016f5cf6SAlexander Graf 346407f07615SPaolo Bonzini Coroutine *co; 346507f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 3466016f5cf6SAlexander Graf 346707f07615SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 346807f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 346907f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 3470016f5cf6SAlexander Graf 3471016f5cf6SAlexander Graf return &acb->common; 3472016f5cf6SAlexander Graf } 3473016f5cf6SAlexander Graf 34744265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 34754265d620SPaolo Bonzini { 34764265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 34774265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 34784265d620SPaolo Bonzini 34794265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 34804265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 34814265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 34824265d620SPaolo Bonzini } 34834265d620SPaolo Bonzini 34844265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 34854265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 34864265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 34874265d620SPaolo Bonzini { 34884265d620SPaolo Bonzini Coroutine *co; 34894265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 34904265d620SPaolo Bonzini 34914265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 34924265d620SPaolo Bonzini 34934265d620SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 34944265d620SPaolo Bonzini acb->req.sector = sector_num; 34954265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 34964265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 34974265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 34984265d620SPaolo Bonzini 34994265d620SPaolo Bonzini return &acb->common; 35004265d620SPaolo Bonzini } 35014265d620SPaolo Bonzini 3502ea2384d3Sbellard void bdrv_init(void) 3503ea2384d3Sbellard { 35045efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3505ea2384d3Sbellard } 3506ce1a14dcSpbrook 3507eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3508eb852011SMarkus Armbruster { 3509eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3510eb852011SMarkus Armbruster bdrv_init(); 3511eb852011SMarkus Armbruster } 3512eb852011SMarkus Armbruster 3513c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 35146bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 35156bbff9a0Saliguori { 3516ce1a14dcSpbrook BlockDriverAIOCB *acb; 3517ce1a14dcSpbrook 35186bbff9a0Saliguori if (pool->free_aiocb) { 35196bbff9a0Saliguori acb = pool->free_aiocb; 35206bbff9a0Saliguori pool->free_aiocb = acb->next; 3521ce1a14dcSpbrook } else { 35227267c094SAnthony Liguori acb = g_malloc0(pool->aiocb_size); 35236bbff9a0Saliguori acb->pool = pool; 3524ce1a14dcSpbrook } 3525ce1a14dcSpbrook acb->bs = bs; 3526ce1a14dcSpbrook acb->cb = cb; 3527ce1a14dcSpbrook acb->opaque = opaque; 3528ce1a14dcSpbrook return acb; 3529ce1a14dcSpbrook } 3530ce1a14dcSpbrook 3531ce1a14dcSpbrook void qemu_aio_release(void *p) 3532ce1a14dcSpbrook { 35336bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 35346bbff9a0Saliguori AIOPool *pool = acb->pool; 35356bbff9a0Saliguori acb->next = pool->free_aiocb; 35366bbff9a0Saliguori pool->free_aiocb = acb; 3537ce1a14dcSpbrook } 353819cb3738Sbellard 353919cb3738Sbellard /**************************************************************/ 3540f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 3541f9f05dc5SKevin Wolf 3542f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 3543f9f05dc5SKevin Wolf Coroutine *coroutine; 3544f9f05dc5SKevin Wolf int ret; 3545f9f05dc5SKevin Wolf } CoroutineIOCompletion; 3546f9f05dc5SKevin Wolf 3547f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 3548f9f05dc5SKevin Wolf { 3549f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 3550f9f05dc5SKevin Wolf 3551f9f05dc5SKevin Wolf co->ret = ret; 3552f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 3553f9f05dc5SKevin Wolf } 3554f9f05dc5SKevin Wolf 3555f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 3556f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 3557f9f05dc5SKevin Wolf bool is_write) 3558f9f05dc5SKevin Wolf { 3559f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 3560f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 3561f9f05dc5SKevin Wolf }; 3562f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 3563f9f05dc5SKevin Wolf 3564f9f05dc5SKevin Wolf if (is_write) { 3565a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 3566f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3567f9f05dc5SKevin Wolf } else { 3568a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 3569f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3570f9f05dc5SKevin Wolf } 3571f9f05dc5SKevin Wolf 357259370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 3573f9f05dc5SKevin Wolf if (!acb) { 3574f9f05dc5SKevin Wolf return -EIO; 3575f9f05dc5SKevin Wolf } 3576f9f05dc5SKevin Wolf qemu_coroutine_yield(); 3577f9f05dc5SKevin Wolf 3578f9f05dc5SKevin Wolf return co.ret; 3579f9f05dc5SKevin Wolf } 3580f9f05dc5SKevin Wolf 3581f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 3582f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3583f9f05dc5SKevin Wolf QEMUIOVector *iov) 3584f9f05dc5SKevin Wolf { 3585f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 3586f9f05dc5SKevin Wolf } 3587f9f05dc5SKevin Wolf 3588f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 3589f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3590f9f05dc5SKevin Wolf QEMUIOVector *iov) 3591f9f05dc5SKevin Wolf { 3592f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 3593f9f05dc5SKevin Wolf } 3594f9f05dc5SKevin Wolf 359507f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 3596e7a8a783SKevin Wolf { 359707f07615SPaolo Bonzini RwCo *rwco = opaque; 359807f07615SPaolo Bonzini 359907f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 360007f07615SPaolo Bonzini } 360107f07615SPaolo Bonzini 360207f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 360307f07615SPaolo Bonzini { 3604eb489bb1SKevin Wolf int ret; 3605eb489bb1SKevin Wolf 360629cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 360707f07615SPaolo Bonzini return 0; 3608eb489bb1SKevin Wolf } 3609eb489bb1SKevin Wolf 3610ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 3611eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 3612eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 3613eb489bb1SKevin Wolf if (ret < 0) { 3614eb489bb1SKevin Wolf return ret; 3615eb489bb1SKevin Wolf } 3616eb489bb1SKevin Wolf } 3617eb489bb1SKevin Wolf 3618ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 3619ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 3620ca716364SKevin Wolf return 0; 3621ca716364SKevin Wolf } 3622ca716364SKevin Wolf 3623eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 362429cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 362507f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 362607f07615SPaolo Bonzini BlockDriverAIOCB *acb; 3627e7a8a783SKevin Wolf CoroutineIOCompletion co = { 3628e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 3629e7a8a783SKevin Wolf }; 3630e7a8a783SKevin Wolf 363107f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 363207f07615SPaolo Bonzini if (acb == NULL) { 363329cdb251SPaolo Bonzini ret = -EIO; 363407f07615SPaolo Bonzini } else { 3635e7a8a783SKevin Wolf qemu_coroutine_yield(); 363629cdb251SPaolo Bonzini ret = co.ret; 3637e7a8a783SKevin Wolf } 363807f07615SPaolo Bonzini } else { 363907f07615SPaolo Bonzini /* 364007f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 364107f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 364207f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 364307f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 364407f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 364507f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 364607f07615SPaolo Bonzini * mode. 364707f07615SPaolo Bonzini * 364807f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 364907f07615SPaolo Bonzini */ 365029cdb251SPaolo Bonzini ret = 0; 365107f07615SPaolo Bonzini } 365229cdb251SPaolo Bonzini if (ret < 0) { 365329cdb251SPaolo Bonzini return ret; 365429cdb251SPaolo Bonzini } 365529cdb251SPaolo Bonzini 365629cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 365729cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 365829cdb251SPaolo Bonzini */ 365929cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 366007f07615SPaolo Bonzini } 366107f07615SPaolo Bonzini 36620f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 36630f15423cSAnthony Liguori { 36640f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 36650f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 36660f15423cSAnthony Liguori } 36670f15423cSAnthony Liguori } 36680f15423cSAnthony Liguori 36690f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 36700f15423cSAnthony Liguori { 36710f15423cSAnthony Liguori BlockDriverState *bs; 36720f15423cSAnthony Liguori 36730f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 36740f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 36750f15423cSAnthony Liguori } 36760f15423cSAnthony Liguori } 36770f15423cSAnthony Liguori 367807789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 367907789269SBenoît Canet { 368007789269SBenoît Canet BlockDriverState *bs; 368107789269SBenoît Canet 368207789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 368307789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 368407789269SBenoît Canet } 368507789269SBenoît Canet } 368607789269SBenoît Canet 368707f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 368807f07615SPaolo Bonzini { 368907f07615SPaolo Bonzini Coroutine *co; 369007f07615SPaolo Bonzini RwCo rwco = { 369107f07615SPaolo Bonzini .bs = bs, 369207f07615SPaolo Bonzini .ret = NOT_DONE, 369307f07615SPaolo Bonzini }; 369407f07615SPaolo Bonzini 369507f07615SPaolo Bonzini if (qemu_in_coroutine()) { 369607f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 369707f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 369807f07615SPaolo Bonzini } else { 369907f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 370007f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 370107f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 370207f07615SPaolo Bonzini qemu_aio_wait(); 370307f07615SPaolo Bonzini } 370407f07615SPaolo Bonzini } 370507f07615SPaolo Bonzini 370607f07615SPaolo Bonzini return rwco.ret; 370707f07615SPaolo Bonzini } 3708e7a8a783SKevin Wolf 37094265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 37104265d620SPaolo Bonzini { 37114265d620SPaolo Bonzini RwCo *rwco = opaque; 37124265d620SPaolo Bonzini 37134265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 37144265d620SPaolo Bonzini } 37154265d620SPaolo Bonzini 37164265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 37174265d620SPaolo Bonzini int nb_sectors) 37184265d620SPaolo Bonzini { 37194265d620SPaolo Bonzini if (!bs->drv) { 37204265d620SPaolo Bonzini return -ENOMEDIUM; 37214265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 37224265d620SPaolo Bonzini return -EIO; 37234265d620SPaolo Bonzini } else if (bs->read_only) { 37244265d620SPaolo Bonzini return -EROFS; 37254265d620SPaolo Bonzini } else if (bs->drv->bdrv_co_discard) { 37264265d620SPaolo Bonzini return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors); 37274265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 37284265d620SPaolo Bonzini BlockDriverAIOCB *acb; 37294265d620SPaolo Bonzini CoroutineIOCompletion co = { 37304265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 37314265d620SPaolo Bonzini }; 37324265d620SPaolo Bonzini 37334265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 37344265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 37354265d620SPaolo Bonzini if (acb == NULL) { 37364265d620SPaolo Bonzini return -EIO; 37374265d620SPaolo Bonzini } else { 37384265d620SPaolo Bonzini qemu_coroutine_yield(); 37394265d620SPaolo Bonzini return co.ret; 37404265d620SPaolo Bonzini } 37414265d620SPaolo Bonzini } else { 37424265d620SPaolo Bonzini return 0; 37434265d620SPaolo Bonzini } 37444265d620SPaolo Bonzini } 37454265d620SPaolo Bonzini 37464265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 37474265d620SPaolo Bonzini { 37484265d620SPaolo Bonzini Coroutine *co; 37494265d620SPaolo Bonzini RwCo rwco = { 37504265d620SPaolo Bonzini .bs = bs, 37514265d620SPaolo Bonzini .sector_num = sector_num, 37524265d620SPaolo Bonzini .nb_sectors = nb_sectors, 37534265d620SPaolo Bonzini .ret = NOT_DONE, 37544265d620SPaolo Bonzini }; 37554265d620SPaolo Bonzini 37564265d620SPaolo Bonzini if (qemu_in_coroutine()) { 37574265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 37584265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 37594265d620SPaolo Bonzini } else { 37604265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 37614265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 37624265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 37634265d620SPaolo Bonzini qemu_aio_wait(); 37644265d620SPaolo Bonzini } 37654265d620SPaolo Bonzini } 37664265d620SPaolo Bonzini 37674265d620SPaolo Bonzini return rwco.ret; 37684265d620SPaolo Bonzini } 37694265d620SPaolo Bonzini 3770f9f05dc5SKevin Wolf /**************************************************************/ 377119cb3738Sbellard /* removable device support */ 377219cb3738Sbellard 377319cb3738Sbellard /** 377419cb3738Sbellard * Return TRUE if the media is present 377519cb3738Sbellard */ 377619cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 377719cb3738Sbellard { 377819cb3738Sbellard BlockDriver *drv = bs->drv; 3779a1aff5bfSMarkus Armbruster 378019cb3738Sbellard if (!drv) 378119cb3738Sbellard return 0; 378219cb3738Sbellard if (!drv->bdrv_is_inserted) 3783a1aff5bfSMarkus Armbruster return 1; 3784a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 378519cb3738Sbellard } 378619cb3738Sbellard 378719cb3738Sbellard /** 37888e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 37898e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 379019cb3738Sbellard */ 379119cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 379219cb3738Sbellard { 379319cb3738Sbellard BlockDriver *drv = bs->drv; 379419cb3738Sbellard 37958e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 37968e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 37978e49ca46SMarkus Armbruster } 37988e49ca46SMarkus Armbruster return -ENOTSUP; 379919cb3738Sbellard } 380019cb3738Sbellard 380119cb3738Sbellard /** 380219cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 380319cb3738Sbellard */ 3804f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 380519cb3738Sbellard { 380619cb3738Sbellard BlockDriver *drv = bs->drv; 380719cb3738Sbellard 3808822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 3809822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 381019cb3738Sbellard } 38116f382ed2SLuiz Capitulino 38126f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 38136f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 38146f382ed2SLuiz Capitulino } 381519cb3738Sbellard } 381619cb3738Sbellard 381719cb3738Sbellard /** 381819cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 381919cb3738Sbellard * to eject it manually). 382019cb3738Sbellard */ 3821025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 382219cb3738Sbellard { 382319cb3738Sbellard BlockDriver *drv = bs->drv; 382419cb3738Sbellard 3825025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 3826b8c6d095SStefan Hajnoczi 3827025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 3828025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 382919cb3738Sbellard } 383019cb3738Sbellard } 3831985a03b0Sths 3832985a03b0Sths /* needed for generic scsi interface */ 3833985a03b0Sths 3834985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 3835985a03b0Sths { 3836985a03b0Sths BlockDriver *drv = bs->drv; 3837985a03b0Sths 3838985a03b0Sths if (drv && drv->bdrv_ioctl) 3839985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 3840985a03b0Sths return -ENOTSUP; 3841985a03b0Sths } 38427d780669Saliguori 3843221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 3844221f715dSaliguori unsigned long int req, void *buf, 38457d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 38467d780669Saliguori { 3847221f715dSaliguori BlockDriver *drv = bs->drv; 38487d780669Saliguori 3849221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 3850221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 3851221f715dSaliguori return NULL; 38527d780669Saliguori } 3853e268ca52Saliguori 38547b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 38557b6f9300SMarkus Armbruster { 38567b6f9300SMarkus Armbruster bs->buffer_alignment = align; 38577b6f9300SMarkus Armbruster } 38587cd1e32aSlirans@il.ibm.com 3859e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 3860e268ca52Saliguori { 3861e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 3862e268ca52Saliguori } 38637cd1e32aSlirans@il.ibm.com 38647cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 38657cd1e32aSlirans@il.ibm.com { 38667cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 3867a55eb92cSJan Kiszka 3868aaa0eb75SLiran Schour bs->dirty_count = 0; 38697cd1e32aSlirans@il.ibm.com if (enable) { 3870c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 3871c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 3872c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 3873c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 38747cd1e32aSlirans@il.ibm.com 38757267c094SAnthony Liguori bs->dirty_bitmap = g_malloc0(bitmap_size); 38767cd1e32aSlirans@il.ibm.com } 38777cd1e32aSlirans@il.ibm.com } else { 3878c6d22830SJan Kiszka if (bs->dirty_bitmap) { 38797267c094SAnthony Liguori g_free(bs->dirty_bitmap); 3880c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 38817cd1e32aSlirans@il.ibm.com } 38827cd1e32aSlirans@il.ibm.com } 38837cd1e32aSlirans@il.ibm.com } 38847cd1e32aSlirans@il.ibm.com 38857cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 38867cd1e32aSlirans@il.ibm.com { 38876ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 38887cd1e32aSlirans@il.ibm.com 3889c6d22830SJan Kiszka if (bs->dirty_bitmap && 3890c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 38916d59fec1SMarcelo Tosatti return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 38926d59fec1SMarcelo Tosatti (1UL << (chunk % (sizeof(unsigned long) * 8)))); 38937cd1e32aSlirans@il.ibm.com } else { 38947cd1e32aSlirans@il.ibm.com return 0; 38957cd1e32aSlirans@il.ibm.com } 38967cd1e32aSlirans@il.ibm.com } 38977cd1e32aSlirans@il.ibm.com 38987cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 38997cd1e32aSlirans@il.ibm.com int nr_sectors) 39007cd1e32aSlirans@il.ibm.com { 39017cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 39027cd1e32aSlirans@il.ibm.com } 3903aaa0eb75SLiran Schour 3904aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 3905aaa0eb75SLiran Schour { 3906aaa0eb75SLiran Schour return bs->dirty_count; 3907aaa0eb75SLiran Schour } 3908f88e1a42SJes Sorensen 3909db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 3910db593f25SMarcelo Tosatti { 3911db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 3912db593f25SMarcelo Tosatti bs->in_use = in_use; 3913db593f25SMarcelo Tosatti } 3914db593f25SMarcelo Tosatti 3915db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 3916db593f25SMarcelo Tosatti { 3917db593f25SMarcelo Tosatti return bs->in_use; 3918db593f25SMarcelo Tosatti } 3919db593f25SMarcelo Tosatti 392028a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 392128a7282aSLuiz Capitulino { 3922d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 392358e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 392428a7282aSLuiz Capitulino } 392528a7282aSLuiz Capitulino 392628a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 392728a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 392828a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 392928a7282aSLuiz Capitulino { 3930d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 393128a7282aSLuiz Capitulino (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC || 393228a7282aSLuiz Capitulino bs->on_write_error == BLOCK_ERR_STOP_ANY || 393328a7282aSLuiz Capitulino bs->on_read_error == BLOCK_ERR_STOP_ANY)); 393428a7282aSLuiz Capitulino } 393528a7282aSLuiz Capitulino 393628a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 393728a7282aSLuiz Capitulino { 3938d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 393928a7282aSLuiz Capitulino } 394028a7282aSLuiz Capitulino 394128a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 394228a7282aSLuiz Capitulino { 394328a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 394458e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 394528a7282aSLuiz Capitulino } 394628a7282aSLuiz Capitulino } 394728a7282aSLuiz Capitulino 394828a7282aSLuiz Capitulino /* XXX: Today this is set by device models because it makes the implementation 394928a7282aSLuiz Capitulino quite simple. However, the block layer knows about the error, so it's 395028a7282aSLuiz Capitulino possible to implement this without device models being involved */ 395128a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 395228a7282aSLuiz Capitulino { 395358e21ef5SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs) && 395458e21ef5SLuiz Capitulino bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 395528a7282aSLuiz Capitulino assert(error >= 0); 395658e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 395758e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 395828a7282aSLuiz Capitulino } 395928a7282aSLuiz Capitulino } 396028a7282aSLuiz Capitulino 3961a597e79cSChristoph Hellwig void 3962a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 3963a597e79cSChristoph Hellwig enum BlockAcctType type) 3964a597e79cSChristoph Hellwig { 3965a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 3966a597e79cSChristoph Hellwig 3967a597e79cSChristoph Hellwig cookie->bytes = bytes; 3968c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 3969a597e79cSChristoph Hellwig cookie->type = type; 3970a597e79cSChristoph Hellwig } 3971a597e79cSChristoph Hellwig 3972a597e79cSChristoph Hellwig void 3973a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 3974a597e79cSChristoph Hellwig { 3975a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 3976a597e79cSChristoph Hellwig 3977a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 3978a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 3979c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 3980a597e79cSChristoph Hellwig } 3981a597e79cSChristoph Hellwig 3982f88e1a42SJes Sorensen int bdrv_img_create(const char *filename, const char *fmt, 3983f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 3984f88e1a42SJes Sorensen char *options, uint64_t img_size, int flags) 3985f88e1a42SJes Sorensen { 3986f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 3987d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 3988f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 3989f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 399096df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 3991f88e1a42SJes Sorensen int ret = 0; 3992f88e1a42SJes Sorensen 3993f88e1a42SJes Sorensen /* Find driver and parse its options */ 3994f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 3995f88e1a42SJes Sorensen if (!drv) { 3996f88e1a42SJes Sorensen error_report("Unknown file format '%s'", fmt); 39974f70f249SJes Sorensen ret = -EINVAL; 3998f88e1a42SJes Sorensen goto out; 3999f88e1a42SJes Sorensen } 4000f88e1a42SJes Sorensen 4001f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 4002f88e1a42SJes Sorensen if (!proto_drv) { 4003f88e1a42SJes Sorensen error_report("Unknown protocol '%s'", filename); 40044f70f249SJes Sorensen ret = -EINVAL; 4005f88e1a42SJes Sorensen goto out; 4006f88e1a42SJes Sorensen } 4007f88e1a42SJes Sorensen 4008f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4009f88e1a42SJes Sorensen drv->create_options); 4010f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4011f88e1a42SJes Sorensen proto_drv->create_options); 4012f88e1a42SJes Sorensen 4013f88e1a42SJes Sorensen /* Create parameter list with default values */ 4014f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4015f88e1a42SJes Sorensen 4016f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4017f88e1a42SJes Sorensen 4018f88e1a42SJes Sorensen /* Parse -o options */ 4019f88e1a42SJes Sorensen if (options) { 4020f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4021f88e1a42SJes Sorensen if (param == NULL) { 4022f88e1a42SJes Sorensen error_report("Invalid options for file format '%s'.", fmt); 40234f70f249SJes Sorensen ret = -EINVAL; 4024f88e1a42SJes Sorensen goto out; 4025f88e1a42SJes Sorensen } 4026f88e1a42SJes Sorensen } 4027f88e1a42SJes Sorensen 4028f88e1a42SJes Sorensen if (base_filename) { 4029f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4030f88e1a42SJes Sorensen base_filename)) { 4031f88e1a42SJes Sorensen error_report("Backing file not supported for file format '%s'", 4032f88e1a42SJes Sorensen fmt); 40334f70f249SJes Sorensen ret = -EINVAL; 4034f88e1a42SJes Sorensen goto out; 4035f88e1a42SJes Sorensen } 4036f88e1a42SJes Sorensen } 4037f88e1a42SJes Sorensen 4038f88e1a42SJes Sorensen if (base_fmt) { 4039f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 4040f88e1a42SJes Sorensen error_report("Backing file format not supported for file " 4041f88e1a42SJes Sorensen "format '%s'", fmt); 40424f70f249SJes Sorensen ret = -EINVAL; 4043f88e1a42SJes Sorensen goto out; 4044f88e1a42SJes Sorensen } 4045f88e1a42SJes Sorensen } 4046f88e1a42SJes Sorensen 4047792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4048792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4049792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 4050792da93aSJes Sorensen error_report("Error: Trying to create an image with the " 4051792da93aSJes Sorensen "same filename as the backing file"); 40524f70f249SJes Sorensen ret = -EINVAL; 4053792da93aSJes Sorensen goto out; 4054792da93aSJes Sorensen } 4055792da93aSJes Sorensen } 4056792da93aSJes Sorensen 4057f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4058f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 405996df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 406096df67d1SStefan Hajnoczi if (!backing_drv) { 4061f88e1a42SJes Sorensen error_report("Unknown backing file format '%s'", 4062f88e1a42SJes Sorensen backing_fmt->value.s); 40634f70f249SJes Sorensen ret = -EINVAL; 4064f88e1a42SJes Sorensen goto out; 4065f88e1a42SJes Sorensen } 4066f88e1a42SJes Sorensen } 4067f88e1a42SJes Sorensen 4068f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4069f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4070d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4071d220894eSKevin Wolf if (size && size->value.n == -1) { 4072f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4073f88e1a42SJes Sorensen uint64_t size; 4074f88e1a42SJes Sorensen char buf[32]; 4075f88e1a42SJes Sorensen 4076f88e1a42SJes Sorensen bs = bdrv_new(""); 4077f88e1a42SJes Sorensen 407896df67d1SStefan Hajnoczi ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); 4079f88e1a42SJes Sorensen if (ret < 0) { 408096df67d1SStefan Hajnoczi error_report("Could not open '%s'", backing_file->value.s); 4081f88e1a42SJes Sorensen goto out; 4082f88e1a42SJes Sorensen } 4083f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4084f88e1a42SJes Sorensen size *= 512; 4085f88e1a42SJes Sorensen 4086f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4087f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4088f88e1a42SJes Sorensen } else { 4089f88e1a42SJes Sorensen error_report("Image creation needs a size parameter"); 40904f70f249SJes Sorensen ret = -EINVAL; 4091f88e1a42SJes Sorensen goto out; 4092f88e1a42SJes Sorensen } 4093f88e1a42SJes Sorensen } 4094f88e1a42SJes Sorensen 4095f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4096f88e1a42SJes Sorensen print_option_parameters(param); 4097f88e1a42SJes Sorensen puts(""); 4098f88e1a42SJes Sorensen 4099f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 4100f88e1a42SJes Sorensen 4101f88e1a42SJes Sorensen if (ret < 0) { 4102f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 4103f88e1a42SJes Sorensen error_report("Formatting or formatting option not supported for " 4104f88e1a42SJes Sorensen "file format '%s'", fmt); 4105f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 4106f88e1a42SJes Sorensen error_report("The image size is too large for file format '%s'", 4107f88e1a42SJes Sorensen fmt); 4108f88e1a42SJes Sorensen } else { 4109f88e1a42SJes Sorensen error_report("%s: error while creating %s: %s", filename, fmt, 4110f88e1a42SJes Sorensen strerror(-ret)); 4111f88e1a42SJes Sorensen } 4112f88e1a42SJes Sorensen } 4113f88e1a42SJes Sorensen 4114f88e1a42SJes Sorensen out: 4115f88e1a42SJes Sorensen free_option_parameters(create_options); 4116f88e1a42SJes Sorensen free_option_parameters(param); 4117f88e1a42SJes Sorensen 4118f88e1a42SJes Sorensen if (bs) { 4119f88e1a42SJes Sorensen bdrv_delete(bs); 4120f88e1a42SJes Sorensen } 41214f70f249SJes Sorensen 41224f70f249SJes Sorensen return ret; 4123f88e1a42SJes Sorensen } 4124eeec61f2SStefan Hajnoczi 4125eeec61f2SStefan Hajnoczi void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs, 4126c83c66c3SStefan Hajnoczi int64_t speed, BlockDriverCompletionFunc *cb, 4127c83c66c3SStefan Hajnoczi void *opaque, Error **errp) 4128eeec61f2SStefan Hajnoczi { 4129eeec61f2SStefan Hajnoczi BlockJob *job; 4130eeec61f2SStefan Hajnoczi 4131eeec61f2SStefan Hajnoczi if (bs->job || bdrv_in_use(bs)) { 4132fd7f8c65SStefan Hajnoczi error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); 4133eeec61f2SStefan Hajnoczi return NULL; 4134eeec61f2SStefan Hajnoczi } 4135eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 1); 4136eeec61f2SStefan Hajnoczi 4137eeec61f2SStefan Hajnoczi job = g_malloc0(job_type->instance_size); 4138eeec61f2SStefan Hajnoczi job->job_type = job_type; 4139eeec61f2SStefan Hajnoczi job->bs = bs; 4140eeec61f2SStefan Hajnoczi job->cb = cb; 4141eeec61f2SStefan Hajnoczi job->opaque = opaque; 4142eeec61f2SStefan Hajnoczi bs->job = job; 4143c83c66c3SStefan Hajnoczi 4144c83c66c3SStefan Hajnoczi /* Only set speed when necessary to avoid NotSupported error */ 4145c83c66c3SStefan Hajnoczi if (speed != 0) { 4146c83c66c3SStefan Hajnoczi Error *local_err = NULL; 4147c83c66c3SStefan Hajnoczi 4148c83c66c3SStefan Hajnoczi block_job_set_speed(job, speed, &local_err); 4149c83c66c3SStefan Hajnoczi if (error_is_set(&local_err)) { 4150c83c66c3SStefan Hajnoczi bs->job = NULL; 4151c83c66c3SStefan Hajnoczi g_free(job); 4152c83c66c3SStefan Hajnoczi bdrv_set_in_use(bs, 0); 4153c83c66c3SStefan Hajnoczi error_propagate(errp, local_err); 4154c83c66c3SStefan Hajnoczi return NULL; 4155c83c66c3SStefan Hajnoczi } 4156c83c66c3SStefan Hajnoczi } 4157eeec61f2SStefan Hajnoczi return job; 4158eeec61f2SStefan Hajnoczi } 4159eeec61f2SStefan Hajnoczi 4160eeec61f2SStefan Hajnoczi void block_job_complete(BlockJob *job, int ret) 4161eeec61f2SStefan Hajnoczi { 4162eeec61f2SStefan Hajnoczi BlockDriverState *bs = job->bs; 4163eeec61f2SStefan Hajnoczi 4164eeec61f2SStefan Hajnoczi assert(bs->job == job); 4165eeec61f2SStefan Hajnoczi job->cb(job->opaque, ret); 4166eeec61f2SStefan Hajnoczi bs->job = NULL; 4167eeec61f2SStefan Hajnoczi g_free(job); 4168eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 0); 4169eeec61f2SStefan Hajnoczi } 4170eeec61f2SStefan Hajnoczi 4171882ec7ceSStefan Hajnoczi void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) 4172eeec61f2SStefan Hajnoczi { 41739e6636c7SStefan Hajnoczi Error *local_err = NULL; 41749f25ecccSPaolo Bonzini 4175eeec61f2SStefan Hajnoczi if (!job->job_type->set_speed) { 41769e6636c7SStefan Hajnoczi error_set(errp, QERR_NOT_SUPPORTED); 41779e6636c7SStefan Hajnoczi return; 4178eeec61f2SStefan Hajnoczi } 4179882ec7ceSStefan Hajnoczi job->job_type->set_speed(job, speed, &local_err); 41809e6636c7SStefan Hajnoczi if (error_is_set(&local_err)) { 41819e6636c7SStefan Hajnoczi error_propagate(errp, local_err); 41829e6636c7SStefan Hajnoczi return; 41839e6636c7SStefan Hajnoczi } 41849e6636c7SStefan Hajnoczi 4185882ec7ceSStefan Hajnoczi job->speed = speed; 41869f25ecccSPaolo Bonzini } 4187eeec61f2SStefan Hajnoczi 4188eeec61f2SStefan Hajnoczi void block_job_cancel(BlockJob *job) 4189eeec61f2SStefan Hajnoczi { 4190eeec61f2SStefan Hajnoczi job->cancelled = true; 4191eeec61f2SStefan Hajnoczi } 4192eeec61f2SStefan Hajnoczi 4193eeec61f2SStefan Hajnoczi bool block_job_is_cancelled(BlockJob *job) 4194eeec61f2SStefan Hajnoczi { 4195eeec61f2SStefan Hajnoczi return job->cancelled; 4196eeec61f2SStefan Hajnoczi } 41973e914655SPaolo Bonzini 41983e914655SPaolo Bonzini void block_job_cancel_sync(BlockJob *job) 41993e914655SPaolo Bonzini { 42003e914655SPaolo Bonzini BlockDriverState *bs = job->bs; 42013e914655SPaolo Bonzini 42023e914655SPaolo Bonzini assert(bs->job == job); 42033e914655SPaolo Bonzini block_job_cancel(job); 42043e914655SPaolo Bonzini while (bs->job != NULL && bs->job->busy) { 42053e914655SPaolo Bonzini qemu_aio_wait(); 42063e914655SPaolo Bonzini } 42073e914655SPaolo Bonzini } 4208