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 3445b7e1542SZhi Yong Wu typedef struct CreateCo { 3455b7e1542SZhi Yong Wu BlockDriver *drv; 3465b7e1542SZhi Yong Wu char *filename; 3475b7e1542SZhi Yong Wu QEMUOptionParameter *options; 3485b7e1542SZhi Yong Wu int ret; 3495b7e1542SZhi Yong Wu } CreateCo; 3505b7e1542SZhi Yong Wu 3515b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 3525b7e1542SZhi Yong Wu { 3535b7e1542SZhi Yong Wu CreateCo *cco = opaque; 3545b7e1542SZhi Yong Wu assert(cco->drv); 3555b7e1542SZhi Yong Wu 3565b7e1542SZhi Yong Wu cco->ret = cco->drv->bdrv_create(cco->filename, cco->options); 3575b7e1542SZhi Yong Wu } 3585b7e1542SZhi Yong Wu 3590e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 3600e7e1989SKevin Wolf QEMUOptionParameter *options) 361ea2384d3Sbellard { 3625b7e1542SZhi Yong Wu int ret; 3630e7e1989SKevin Wolf 3645b7e1542SZhi Yong Wu Coroutine *co; 3655b7e1542SZhi Yong Wu CreateCo cco = { 3665b7e1542SZhi Yong Wu .drv = drv, 3675b7e1542SZhi Yong Wu .filename = g_strdup(filename), 3685b7e1542SZhi Yong Wu .options = options, 3695b7e1542SZhi Yong Wu .ret = NOT_DONE, 3705b7e1542SZhi Yong Wu }; 3715b7e1542SZhi Yong Wu 3725b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 3735b7e1542SZhi Yong Wu return -ENOTSUP; 3745b7e1542SZhi Yong Wu } 3755b7e1542SZhi Yong Wu 3765b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 3775b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 3785b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 3795b7e1542SZhi Yong Wu } else { 3805b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 3815b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 3825b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 3835b7e1542SZhi Yong Wu qemu_aio_wait(); 3845b7e1542SZhi Yong Wu } 3855b7e1542SZhi Yong Wu } 3865b7e1542SZhi Yong Wu 3875b7e1542SZhi Yong Wu ret = cco.ret; 3885b7e1542SZhi Yong Wu g_free(cco.filename); 3895b7e1542SZhi Yong Wu 3905b7e1542SZhi 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. 9094c355d53SZhi Yong Wu * 9104c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 9114c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 9124c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 9134c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 9144c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 915922453bcSStefan Hajnoczi */ 916922453bcSStefan Hajnoczi void bdrv_drain_all(void) 917922453bcSStefan Hajnoczi { 918922453bcSStefan Hajnoczi BlockDriverState *bs; 9194c355d53SZhi Yong Wu bool busy; 920922453bcSStefan Hajnoczi 9214c355d53SZhi Yong Wu do { 9224c355d53SZhi Yong Wu busy = qemu_aio_wait(); 9234c355d53SZhi Yong Wu 9244c355d53SZhi Yong Wu /* FIXME: We do not have timer support here, so this is effectively 9254c355d53SZhi Yong Wu * a busy wait. 9264c355d53SZhi Yong Wu */ 9274c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 9284c355d53SZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 9294c355d53SZhi Yong Wu qemu_co_queue_restart_all(&bs->throttled_reqs); 9304c355d53SZhi Yong Wu busy = true; 9314c355d53SZhi Yong Wu } 9324c355d53SZhi Yong Wu } 9334c355d53SZhi Yong Wu } while (busy); 934922453bcSStefan Hajnoczi 935922453bcSStefan Hajnoczi /* If requests are still pending there is a bug somewhere */ 936922453bcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 937922453bcSStefan Hajnoczi assert(QLIST_EMPTY(&bs->tracked_requests)); 938922453bcSStefan Hajnoczi assert(qemu_co_queue_empty(&bs->throttled_reqs)); 939922453bcSStefan Hajnoczi } 940922453bcSStefan Hajnoczi } 941922453bcSStefan Hajnoczi 942d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 943d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 944d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 945d22b2f41SRyan Harper { 946d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 947d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 948d22b2f41SRyan Harper } 949d22b2f41SRyan Harper bs->device_name[0] = '\0'; 950d22b2f41SRyan Harper } 951d22b2f41SRyan Harper 9528802d1fdSJeff Cody /* 9538802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 9548802d1fdSJeff Cody * live, while keeping required fields on the top layer. 9558802d1fdSJeff Cody * 9568802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 9578802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 9588802d1fdSJeff Cody * 959f6801b83SJeff Cody * bs_new is required to be anonymous. 960f6801b83SJeff Cody * 9618802d1fdSJeff Cody * This function does not create any image files. 9628802d1fdSJeff Cody */ 9638802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 9648802d1fdSJeff Cody { 9658802d1fdSJeff Cody BlockDriverState tmp; 9668802d1fdSJeff Cody 967f6801b83SJeff Cody /* bs_new must be anonymous */ 968f6801b83SJeff Cody assert(bs_new->device_name[0] == '\0'); 9698802d1fdSJeff Cody 9708802d1fdSJeff Cody tmp = *bs_new; 9718802d1fdSJeff Cody 9728802d1fdSJeff Cody /* there are some fields that need to stay on the top layer: */ 9738802d1fdSJeff Cody 9748802d1fdSJeff Cody /* dev info */ 9758802d1fdSJeff Cody tmp.dev_ops = bs_top->dev_ops; 9768802d1fdSJeff Cody tmp.dev_opaque = bs_top->dev_opaque; 9778802d1fdSJeff Cody tmp.dev = bs_top->dev; 9788802d1fdSJeff Cody tmp.buffer_alignment = bs_top->buffer_alignment; 9798802d1fdSJeff Cody tmp.copy_on_read = bs_top->copy_on_read; 9808802d1fdSJeff Cody 9818802d1fdSJeff Cody /* i/o timing parameters */ 9828802d1fdSJeff Cody tmp.slice_time = bs_top->slice_time; 9838802d1fdSJeff Cody tmp.slice_start = bs_top->slice_start; 9848802d1fdSJeff Cody tmp.slice_end = bs_top->slice_end; 9858802d1fdSJeff Cody tmp.io_limits = bs_top->io_limits; 9868802d1fdSJeff Cody tmp.io_base = bs_top->io_base; 9878802d1fdSJeff Cody tmp.throttled_reqs = bs_top->throttled_reqs; 9888802d1fdSJeff Cody tmp.block_timer = bs_top->block_timer; 9898802d1fdSJeff Cody tmp.io_limits_enabled = bs_top->io_limits_enabled; 9908802d1fdSJeff Cody 9918802d1fdSJeff Cody /* geometry */ 9928802d1fdSJeff Cody tmp.cyls = bs_top->cyls; 9938802d1fdSJeff Cody tmp.heads = bs_top->heads; 9948802d1fdSJeff Cody tmp.secs = bs_top->secs; 9958802d1fdSJeff Cody tmp.translation = bs_top->translation; 9968802d1fdSJeff Cody 9978802d1fdSJeff Cody /* r/w error */ 9988802d1fdSJeff Cody tmp.on_read_error = bs_top->on_read_error; 9998802d1fdSJeff Cody tmp.on_write_error = bs_top->on_write_error; 10008802d1fdSJeff Cody 10018802d1fdSJeff Cody /* i/o status */ 10028802d1fdSJeff Cody tmp.iostatus_enabled = bs_top->iostatus_enabled; 10038802d1fdSJeff Cody tmp.iostatus = bs_top->iostatus; 10048802d1fdSJeff Cody 10058802d1fdSJeff Cody /* keep the same entry in bdrv_states */ 10068802d1fdSJeff Cody pstrcpy(tmp.device_name, sizeof(tmp.device_name), bs_top->device_name); 10078802d1fdSJeff Cody tmp.list = bs_top->list; 10088802d1fdSJeff Cody 10098802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 10108802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 10118802d1fdSJeff Cody tmp.backing_hd = bs_new; 10128802d1fdSJeff Cody pstrcpy(tmp.backing_file, sizeof(tmp.backing_file), bs_top->filename); 1013f6801b83SJeff Cody bdrv_get_format(bs_top, tmp.backing_format, sizeof(tmp.backing_format)); 10148802d1fdSJeff Cody 10158802d1fdSJeff Cody /* swap contents of the fixed new bs and the current top */ 10168802d1fdSJeff Cody *bs_new = *bs_top; 10178802d1fdSJeff Cody *bs_top = tmp; 10188802d1fdSJeff Cody 1019f6801b83SJeff Cody /* device_name[] was carried over from the old bs_top. bs_new 1020f6801b83SJeff Cody * shouldn't be in bdrv_states, so we need to make device_name[] 1021f6801b83SJeff Cody * reflect the anonymity of bs_new 1022f6801b83SJeff Cody */ 1023f6801b83SJeff Cody bs_new->device_name[0] = '\0'; 1024f6801b83SJeff Cody 10258802d1fdSJeff Cody /* clear the copied fields in the new backing file */ 10268802d1fdSJeff Cody bdrv_detach_dev(bs_new, bs_new->dev); 10278802d1fdSJeff Cody 10288802d1fdSJeff Cody qemu_co_queue_init(&bs_new->throttled_reqs); 10298802d1fdSJeff Cody memset(&bs_new->io_base, 0, sizeof(bs_new->io_base)); 10308802d1fdSJeff Cody memset(&bs_new->io_limits, 0, sizeof(bs_new->io_limits)); 10318802d1fdSJeff Cody bdrv_iostatus_disable(bs_new); 10328802d1fdSJeff Cody 10338802d1fdSJeff Cody /* we don't use bdrv_io_limits_disable() for this, because we don't want 10348802d1fdSJeff Cody * to affect or delete the block_timer, as it has been moved to bs_top */ 10358802d1fdSJeff Cody bs_new->io_limits_enabled = false; 10368802d1fdSJeff Cody bs_new->block_timer = NULL; 10378802d1fdSJeff Cody bs_new->slice_time = 0; 10388802d1fdSJeff Cody bs_new->slice_start = 0; 10398802d1fdSJeff Cody bs_new->slice_end = 0; 10408802d1fdSJeff Cody } 10418802d1fdSJeff Cody 1042b338082bSbellard void bdrv_delete(BlockDriverState *bs) 1043b338082bSbellard { 1044fa879d62SMarkus Armbruster assert(!bs->dev); 10453e914655SPaolo Bonzini assert(!bs->job); 10463e914655SPaolo Bonzini assert(!bs->in_use); 104718846deeSMarkus Armbruster 10481b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1049d22b2f41SRyan Harper bdrv_make_anon(bs); 105034c6f050Saurel32 1051b338082bSbellard bdrv_close(bs); 105266f82ceeSKevin Wolf if (bs->file != NULL) { 105366f82ceeSKevin Wolf bdrv_delete(bs->file); 105466f82ceeSKevin Wolf } 105566f82ceeSKevin Wolf 1056f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 10577267c094SAnthony Liguori g_free(bs); 1058fc01f7e7Sbellard } 1059fc01f7e7Sbellard 1060fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1061fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 106218846deeSMarkus Armbruster { 1063fa879d62SMarkus Armbruster if (bs->dev) { 106418846deeSMarkus Armbruster return -EBUSY; 106518846deeSMarkus Armbruster } 1066fa879d62SMarkus Armbruster bs->dev = dev; 106728a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 106818846deeSMarkus Armbruster return 0; 106918846deeSMarkus Armbruster } 107018846deeSMarkus Armbruster 1071fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1072fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 107318846deeSMarkus Armbruster { 1074fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1075fa879d62SMarkus Armbruster abort(); 1076fa879d62SMarkus Armbruster } 1077fa879d62SMarkus Armbruster } 1078fa879d62SMarkus Armbruster 1079fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1080fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1081fa879d62SMarkus Armbruster { 1082fa879d62SMarkus Armbruster assert(bs->dev == dev); 1083fa879d62SMarkus Armbruster bs->dev = NULL; 10840e49de52SMarkus Armbruster bs->dev_ops = NULL; 10850e49de52SMarkus Armbruster bs->dev_opaque = NULL; 108629e05f20SMarkus Armbruster bs->buffer_alignment = 512; 108718846deeSMarkus Armbruster } 108818846deeSMarkus Armbruster 1089fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1090fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 109118846deeSMarkus Armbruster { 1092fa879d62SMarkus Armbruster return bs->dev; 109318846deeSMarkus Armbruster } 109418846deeSMarkus Armbruster 10950e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 10960e49de52SMarkus Armbruster void *opaque) 10970e49de52SMarkus Armbruster { 10980e49de52SMarkus Armbruster bs->dev_ops = ops; 10990e49de52SMarkus Armbruster bs->dev_opaque = opaque; 11002c6942faSMarkus Armbruster if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) { 11012c6942faSMarkus Armbruster bs_snapshots = NULL; 11022c6942faSMarkus Armbruster } 11030e49de52SMarkus Armbruster } 11040e49de52SMarkus Armbruster 1105329c0a48SLuiz Capitulino void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 1106329c0a48SLuiz Capitulino BlockQMPEventAction action, int is_read) 1107329c0a48SLuiz Capitulino { 1108329c0a48SLuiz Capitulino QObject *data; 1109329c0a48SLuiz Capitulino const char *action_str; 1110329c0a48SLuiz Capitulino 1111329c0a48SLuiz Capitulino switch (action) { 1112329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1113329c0a48SLuiz Capitulino action_str = "report"; 1114329c0a48SLuiz Capitulino break; 1115329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1116329c0a48SLuiz Capitulino action_str = "ignore"; 1117329c0a48SLuiz Capitulino break; 1118329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1119329c0a48SLuiz Capitulino action_str = "stop"; 1120329c0a48SLuiz Capitulino break; 1121329c0a48SLuiz Capitulino default: 1122329c0a48SLuiz Capitulino abort(); 1123329c0a48SLuiz Capitulino } 1124329c0a48SLuiz Capitulino 1125329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1126329c0a48SLuiz Capitulino bdrv->device_name, 1127329c0a48SLuiz Capitulino action_str, 1128329c0a48SLuiz Capitulino is_read ? "read" : "write"); 1129329c0a48SLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 1130329c0a48SLuiz Capitulino 1131329c0a48SLuiz Capitulino qobject_decref(data); 1132329c0a48SLuiz Capitulino } 1133329c0a48SLuiz Capitulino 11346f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 11356f382ed2SLuiz Capitulino { 11366f382ed2SLuiz Capitulino QObject *data; 11376f382ed2SLuiz Capitulino 11386f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 11396f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 11406f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 11416f382ed2SLuiz Capitulino 11426f382ed2SLuiz Capitulino qobject_decref(data); 11436f382ed2SLuiz Capitulino } 11446f382ed2SLuiz Capitulino 11457d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 11460e49de52SMarkus Armbruster { 1147145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 11486f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 11497d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 11506f382ed2SLuiz Capitulino if (tray_was_closed) { 11516f382ed2SLuiz Capitulino /* tray open */ 11526f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 11536f382ed2SLuiz Capitulino } 11546f382ed2SLuiz Capitulino if (load) { 11556f382ed2SLuiz Capitulino /* tray close */ 11566f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 11576f382ed2SLuiz Capitulino } 1158145feb17SMarkus Armbruster } 1159145feb17SMarkus Armbruster } 1160145feb17SMarkus Armbruster 11612c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 11622c6942faSMarkus Armbruster { 11632c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 11642c6942faSMarkus Armbruster } 11652c6942faSMarkus Armbruster 1166025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1167025ccaa7SPaolo Bonzini { 1168025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1169025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1170025ccaa7SPaolo Bonzini } 1171025ccaa7SPaolo Bonzini } 1172025ccaa7SPaolo Bonzini 1173e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1174e4def80bSMarkus Armbruster { 1175e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1176e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1177e4def80bSMarkus Armbruster } 1178e4def80bSMarkus Armbruster return false; 1179e4def80bSMarkus Armbruster } 1180e4def80bSMarkus Armbruster 1181145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1182145feb17SMarkus Armbruster { 1183145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1184145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 11850e49de52SMarkus Armbruster } 11860e49de52SMarkus Armbruster } 11870e49de52SMarkus Armbruster 1188f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1189f107639aSMarkus Armbruster { 1190f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1191f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1192f107639aSMarkus Armbruster } 1193f107639aSMarkus Armbruster return false; 1194f107639aSMarkus Armbruster } 1195f107639aSMarkus Armbruster 1196e97fc193Saliguori /* 1197e97fc193Saliguori * Run consistency checks on an image 1198e97fc193Saliguori * 1199e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1200a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1201e076f338SKevin Wolf * check are stored in res. 1202e97fc193Saliguori */ 1203e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) 1204e97fc193Saliguori { 1205e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1206e97fc193Saliguori return -ENOTSUP; 1207e97fc193Saliguori } 1208e97fc193Saliguori 1209e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 12109ac228e0SKevin Wolf return bs->drv->bdrv_check(bs, res); 1211e97fc193Saliguori } 1212e97fc193Saliguori 12138a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 12148a426614SKevin Wolf 121533e3963eSbellard /* commit COW file into the raw image */ 121633e3963eSbellard int bdrv_commit(BlockDriverState *bs) 121733e3963eSbellard { 121819cb3738Sbellard BlockDriver *drv = bs->drv; 1219ee181196SKevin Wolf BlockDriver *backing_drv; 12208a426614SKevin Wolf int64_t sector, total_sectors; 12218a426614SKevin Wolf int n, ro, open_flags; 12224dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 12238a426614SKevin Wolf uint8_t *buf; 12244dca4b63SNaphtali Sprei char filename[1024]; 12254dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 122633e3963eSbellard 122719cb3738Sbellard if (!drv) 122819cb3738Sbellard return -ENOMEDIUM; 122933e3963eSbellard 12304dca4b63SNaphtali Sprei if (!bs->backing_hd) { 12314dca4b63SNaphtali Sprei return -ENOTSUP; 12324dca4b63SNaphtali Sprei } 12334dca4b63SNaphtali Sprei 12344dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 1235ea2384d3Sbellard return -EACCES; 123633e3963eSbellard } 123733e3963eSbellard 12382d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 12392d3735d3SStefan Hajnoczi return -EBUSY; 12402d3735d3SStefan Hajnoczi } 12412d3735d3SStefan Hajnoczi 1242ee181196SKevin Wolf backing_drv = bs->backing_hd->drv; 12434dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 12444dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 12454dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 12464dca4b63SNaphtali Sprei 12474dca4b63SNaphtali Sprei if (ro) { 12484dca4b63SNaphtali Sprei /* re-open as RW */ 12494dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 12504dca4b63SNaphtali Sprei bs->backing_hd = NULL; 12514dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 1252ee181196SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, 1253ee181196SKevin Wolf backing_drv); 12544dca4b63SNaphtali Sprei if (rw_ret < 0) { 12554dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 12564dca4b63SNaphtali Sprei /* try to re-open read-only */ 12574dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1258ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1259ee181196SKevin Wolf backing_drv); 12604dca4b63SNaphtali Sprei if (ret < 0) { 12614dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 12624dca4b63SNaphtali Sprei /* drive not functional anymore */ 12634dca4b63SNaphtali Sprei bs->drv = NULL; 12644dca4b63SNaphtali Sprei return ret; 12654dca4b63SNaphtali Sprei } 12664dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 12674dca4b63SNaphtali Sprei return rw_ret; 12684dca4b63SNaphtali Sprei } 12694dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 1270ea2384d3Sbellard } 1271ea2384d3Sbellard 12726ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 12737267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 12748a426614SKevin Wolf 12758a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 127605c4af54SStefan Hajnoczi if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 12778a426614SKevin Wolf 12788a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 12794dca4b63SNaphtali Sprei ret = -EIO; 12804dca4b63SNaphtali Sprei goto ro_cleanup; 128133e3963eSbellard } 128233e3963eSbellard 12838a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 12844dca4b63SNaphtali Sprei ret = -EIO; 12854dca4b63SNaphtali Sprei goto ro_cleanup; 128633e3963eSbellard } 128733e3963eSbellard } 128833e3963eSbellard } 128995389c86Sbellard 12901d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 12911d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 12921d44952fSChristoph Hellwig bdrv_flush(bs); 12931d44952fSChristoph Hellwig } 129495389c86Sbellard 12953f5075aeSChristoph Hellwig /* 12963f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 12973f5075aeSChristoph Hellwig * stable on disk. 12983f5075aeSChristoph Hellwig */ 12993f5075aeSChristoph Hellwig if (bs->backing_hd) 13003f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 13014dca4b63SNaphtali Sprei 13024dca4b63SNaphtali Sprei ro_cleanup: 13037267c094SAnthony Liguori g_free(buf); 13044dca4b63SNaphtali Sprei 13054dca4b63SNaphtali Sprei if (ro) { 13064dca4b63SNaphtali Sprei /* re-open as RO */ 13074dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 13084dca4b63SNaphtali Sprei bs->backing_hd = NULL; 13094dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1310ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1311ee181196SKevin Wolf backing_drv); 13124dca4b63SNaphtali Sprei if (ret < 0) { 13134dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 13144dca4b63SNaphtali Sprei /* drive not functional anymore */ 13154dca4b63SNaphtali Sprei bs->drv = NULL; 13164dca4b63SNaphtali Sprei return ret; 13174dca4b63SNaphtali Sprei } 13184dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 13194dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 13204dca4b63SNaphtali Sprei } 13214dca4b63SNaphtali Sprei 13221d44952fSChristoph Hellwig return ret; 132333e3963eSbellard } 132433e3963eSbellard 1325e8877497SStefan Hajnoczi int bdrv_commit_all(void) 13266ab4b5abSMarkus Armbruster { 13276ab4b5abSMarkus Armbruster BlockDriverState *bs; 13286ab4b5abSMarkus Armbruster 13296ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1330e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1331e8877497SStefan Hajnoczi if (ret < 0) { 1332e8877497SStefan Hajnoczi return ret; 13336ab4b5abSMarkus Armbruster } 13346ab4b5abSMarkus Armbruster } 1335e8877497SStefan Hajnoczi return 0; 1336e8877497SStefan Hajnoczi } 13376ab4b5abSMarkus Armbruster 1338dbffbdcfSStefan Hajnoczi struct BdrvTrackedRequest { 1339dbffbdcfSStefan Hajnoczi BlockDriverState *bs; 1340dbffbdcfSStefan Hajnoczi int64_t sector_num; 1341dbffbdcfSStefan Hajnoczi int nb_sectors; 1342dbffbdcfSStefan Hajnoczi bool is_write; 1343dbffbdcfSStefan Hajnoczi QLIST_ENTRY(BdrvTrackedRequest) list; 13445f8b6491SStefan Hajnoczi Coroutine *co; /* owner, used for deadlock detection */ 1345f4658285SStefan Hajnoczi CoQueue wait_queue; /* coroutines blocked on this request */ 1346dbffbdcfSStefan Hajnoczi }; 1347dbffbdcfSStefan Hajnoczi 1348dbffbdcfSStefan Hajnoczi /** 1349dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1350dbffbdcfSStefan Hajnoczi * 1351dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1352dbffbdcfSStefan Hajnoczi */ 1353dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1354dbffbdcfSStefan Hajnoczi { 1355dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1356f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1357dbffbdcfSStefan Hajnoczi } 1358dbffbdcfSStefan Hajnoczi 1359dbffbdcfSStefan Hajnoczi /** 1360dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1361dbffbdcfSStefan Hajnoczi */ 1362dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1363dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1364dbffbdcfSStefan Hajnoczi int64_t sector_num, 1365dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1366dbffbdcfSStefan Hajnoczi { 1367dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1368dbffbdcfSStefan Hajnoczi .bs = bs, 1369dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1370dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1371dbffbdcfSStefan Hajnoczi .is_write = is_write, 13725f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1373dbffbdcfSStefan Hajnoczi }; 1374dbffbdcfSStefan Hajnoczi 1375f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1376f4658285SStefan Hajnoczi 1377dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1378dbffbdcfSStefan Hajnoczi } 1379dbffbdcfSStefan Hajnoczi 1380d83947acSStefan Hajnoczi /** 1381d83947acSStefan Hajnoczi * Round a region to cluster boundaries 1382d83947acSStefan Hajnoczi */ 1383d83947acSStefan Hajnoczi static void round_to_clusters(BlockDriverState *bs, 1384d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 1385d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 1386d83947acSStefan Hajnoczi int *cluster_nb_sectors) 1387d83947acSStefan Hajnoczi { 1388d83947acSStefan Hajnoczi BlockDriverInfo bdi; 1389d83947acSStefan Hajnoczi 1390d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 1391d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 1392d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 1393d83947acSStefan Hajnoczi } else { 1394d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 1395d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 1396d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 1397d83947acSStefan Hajnoczi nb_sectors, c); 1398d83947acSStefan Hajnoczi } 1399d83947acSStefan Hajnoczi } 1400d83947acSStefan Hajnoczi 1401f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 1402f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 1403d83947acSStefan Hajnoczi /* aaaa bbbb */ 1404d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 1405d83947acSStefan Hajnoczi return false; 1406d83947acSStefan Hajnoczi } 1407d83947acSStefan Hajnoczi /* bbbb aaaa */ 1408d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 1409d83947acSStefan Hajnoczi return false; 1410d83947acSStefan Hajnoczi } 1411d83947acSStefan Hajnoczi return true; 1412f4658285SStefan Hajnoczi } 1413f4658285SStefan Hajnoczi 1414f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 1415f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 1416f4658285SStefan Hajnoczi { 1417f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 1418d83947acSStefan Hajnoczi int64_t cluster_sector_num; 1419d83947acSStefan Hajnoczi int cluster_nb_sectors; 1420f4658285SStefan Hajnoczi bool retry; 1421f4658285SStefan Hajnoczi 1422d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 1423d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 1424d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 1425d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 1426d83947acSStefan Hajnoczi * interleave between them. 1427d83947acSStefan Hajnoczi */ 1428d83947acSStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1429d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1430d83947acSStefan Hajnoczi 1431f4658285SStefan Hajnoczi do { 1432f4658285SStefan Hajnoczi retry = false; 1433f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 1434d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 1435d83947acSStefan Hajnoczi cluster_nb_sectors)) { 14365f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 14375f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 14385f8b6491SStefan Hajnoczi * never happen since it means deadlock. 14395f8b6491SStefan Hajnoczi */ 14405f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 14415f8b6491SStefan Hajnoczi 1442f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 1443f4658285SStefan Hajnoczi retry = true; 1444f4658285SStefan Hajnoczi break; 1445f4658285SStefan Hajnoczi } 1446f4658285SStefan Hajnoczi } 1447f4658285SStefan Hajnoczi } while (retry); 1448f4658285SStefan Hajnoczi } 1449f4658285SStefan Hajnoczi 1450756e6736SKevin Wolf /* 1451756e6736SKevin Wolf * Return values: 1452756e6736SKevin Wolf * 0 - success 1453756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 1454756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 1455756e6736SKevin Wolf * image file header 1456756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 1457756e6736SKevin Wolf */ 1458756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 1459756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1460756e6736SKevin Wolf { 1461756e6736SKevin Wolf BlockDriver *drv = bs->drv; 1462*469ef350SPaolo Bonzini int ret; 1463756e6736SKevin Wolf 14645f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 14655f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 14665f377794SPaolo Bonzini return -EINVAL; 14675f377794SPaolo Bonzini } 14685f377794SPaolo Bonzini 1469756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 1470*469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 1471756e6736SKevin Wolf } else { 1472*469ef350SPaolo Bonzini ret = -ENOTSUP; 1473756e6736SKevin Wolf } 1474*469ef350SPaolo Bonzini 1475*469ef350SPaolo Bonzini if (ret == 0) { 1476*469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1477*469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1478*469ef350SPaolo Bonzini } 1479*469ef350SPaolo Bonzini return ret; 1480756e6736SKevin Wolf } 1481756e6736SKevin Wolf 148271d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 148371d0770cSaliguori size_t size) 148471d0770cSaliguori { 148571d0770cSaliguori int64_t len; 148671d0770cSaliguori 148771d0770cSaliguori if (!bdrv_is_inserted(bs)) 148871d0770cSaliguori return -ENOMEDIUM; 148971d0770cSaliguori 149071d0770cSaliguori if (bs->growable) 149171d0770cSaliguori return 0; 149271d0770cSaliguori 149371d0770cSaliguori len = bdrv_getlength(bs); 149471d0770cSaliguori 1495fbb7b4e0SKevin Wolf if (offset < 0) 1496fbb7b4e0SKevin Wolf return -EIO; 1497fbb7b4e0SKevin Wolf 1498fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 149971d0770cSaliguori return -EIO; 150071d0770cSaliguori 150171d0770cSaliguori return 0; 150271d0770cSaliguori } 150371d0770cSaliguori 150471d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 150571d0770cSaliguori int nb_sectors) 150671d0770cSaliguori { 1507eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1508eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 150971d0770cSaliguori } 151071d0770cSaliguori 15111c9805a3SStefan Hajnoczi typedef struct RwCo { 15121c9805a3SStefan Hajnoczi BlockDriverState *bs; 15131c9805a3SStefan Hajnoczi int64_t sector_num; 15141c9805a3SStefan Hajnoczi int nb_sectors; 15151c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 15161c9805a3SStefan Hajnoczi bool is_write; 15171c9805a3SStefan Hajnoczi int ret; 15181c9805a3SStefan Hajnoczi } RwCo; 15191c9805a3SStefan Hajnoczi 15201c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 1521fc01f7e7Sbellard { 15221c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 1523fc01f7e7Sbellard 15241c9805a3SStefan Hajnoczi if (!rwco->is_write) { 15251c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 1526470c0504SStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 15271c9805a3SStefan Hajnoczi } else { 15281c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 1529f08f2ddaSStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 15301c9805a3SStefan Hajnoczi } 15311c9805a3SStefan Hajnoczi } 1532e7a8a783SKevin Wolf 15331c9805a3SStefan Hajnoczi /* 15341c9805a3SStefan Hajnoczi * Process a synchronous request using coroutines 15351c9805a3SStefan Hajnoczi */ 15361c9805a3SStefan Hajnoczi static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 15371c9805a3SStefan Hajnoczi int nb_sectors, bool is_write) 15381c9805a3SStefan Hajnoczi { 1539e7a8a783SKevin Wolf QEMUIOVector qiov; 1540e7a8a783SKevin Wolf struct iovec iov = { 1541e7a8a783SKevin Wolf .iov_base = (void *)buf, 1542e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 1543e7a8a783SKevin Wolf }; 15441c9805a3SStefan Hajnoczi Coroutine *co; 15451c9805a3SStefan Hajnoczi RwCo rwco = { 15461c9805a3SStefan Hajnoczi .bs = bs, 15471c9805a3SStefan Hajnoczi .sector_num = sector_num, 15481c9805a3SStefan Hajnoczi .nb_sectors = nb_sectors, 15491c9805a3SStefan Hajnoczi .qiov = &qiov, 15501c9805a3SStefan Hajnoczi .is_write = is_write, 15511c9805a3SStefan Hajnoczi .ret = NOT_DONE, 15521c9805a3SStefan Hajnoczi }; 1553e7a8a783SKevin Wolf 1554e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 15551c9805a3SStefan Hajnoczi 1556498e386cSZhi Yong Wu /** 1557498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 1558498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 1559498e386cSZhi Yong Wu * if it has been enabled. 1560498e386cSZhi Yong Wu */ 1561498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 1562498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 1563498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 1564498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 1565498e386cSZhi Yong Wu } 1566498e386cSZhi Yong Wu 15671c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 15681c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 15691c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 15701c9805a3SStefan Hajnoczi } else { 15711c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 15721c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 15731c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 15741c9805a3SStefan Hajnoczi qemu_aio_wait(); 15751c9805a3SStefan Hajnoczi } 15761c9805a3SStefan Hajnoczi } 15771c9805a3SStefan Hajnoczi return rwco.ret; 1578e7a8a783SKevin Wolf } 1579e7a8a783SKevin Wolf 15801c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 15811c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 15821c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 15831c9805a3SStefan Hajnoczi { 15841c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); 158583f64091Sbellard } 1586fc01f7e7Sbellard 15877cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 15887cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 15897cd1e32aSlirans@il.ibm.com { 15907cd1e32aSlirans@il.ibm.com int64_t start, end; 1591c6d22830SJan Kiszka unsigned long val, idx, bit; 1592a55eb92cSJan Kiszka 15936ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 1594c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 15957cd1e32aSlirans@il.ibm.com 15967cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 1597c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 1598c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 1599c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 1600c6d22830SJan Kiszka if (dirty) { 16016d59fec1SMarcelo Tosatti if (!(val & (1UL << bit))) { 1602aaa0eb75SLiran Schour bs->dirty_count++; 16036d59fec1SMarcelo Tosatti val |= 1UL << bit; 1604aaa0eb75SLiran Schour } 1605c6d22830SJan Kiszka } else { 16066d59fec1SMarcelo Tosatti if (val & (1UL << bit)) { 1607aaa0eb75SLiran Schour bs->dirty_count--; 16086d59fec1SMarcelo Tosatti val &= ~(1UL << bit); 1609c6d22830SJan Kiszka } 1610aaa0eb75SLiran Schour } 1611c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 16127cd1e32aSlirans@il.ibm.com } 16137cd1e32aSlirans@il.ibm.com } 16147cd1e32aSlirans@il.ibm.com 161519cb3738Sbellard /* Return < 0 if error. Important errors are: 161619cb3738Sbellard -EIO generic I/O error (may happen for all errors) 161719cb3738Sbellard -ENOMEDIUM No media inserted. 161819cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 161919cb3738Sbellard -EACCES Trying to write a read-only device 162019cb3738Sbellard */ 1621fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 1622fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 1623fc01f7e7Sbellard { 16241c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); 162583f64091Sbellard } 162683f64091Sbellard 1627eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 1628eda578e5Saliguori void *buf, int count1) 162983f64091Sbellard { 16306ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 163183f64091Sbellard int len, nb_sectors, count; 163283f64091Sbellard int64_t sector_num; 16339a8c4cceSKevin Wolf int ret; 163483f64091Sbellard 163583f64091Sbellard count = count1; 163683f64091Sbellard /* first read to align to sector start */ 16376ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 163883f64091Sbellard if (len > count) 163983f64091Sbellard len = count; 16406ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 164183f64091Sbellard if (len > 0) { 16429a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16439a8c4cceSKevin Wolf return ret; 16446ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 164583f64091Sbellard count -= len; 164683f64091Sbellard if (count == 0) 164783f64091Sbellard return count1; 164883f64091Sbellard sector_num++; 164983f64091Sbellard buf += len; 165083f64091Sbellard } 165183f64091Sbellard 165283f64091Sbellard /* read the sectors "in place" */ 16536ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 165483f64091Sbellard if (nb_sectors > 0) { 16559a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 16569a8c4cceSKevin Wolf return ret; 165783f64091Sbellard sector_num += nb_sectors; 16586ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 165983f64091Sbellard buf += len; 166083f64091Sbellard count -= len; 166183f64091Sbellard } 166283f64091Sbellard 166383f64091Sbellard /* add data from the last sector */ 166483f64091Sbellard if (count > 0) { 16659a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16669a8c4cceSKevin Wolf return ret; 166783f64091Sbellard memcpy(buf, tmp_buf, count); 166883f64091Sbellard } 166983f64091Sbellard return count1; 167083f64091Sbellard } 167183f64091Sbellard 1672eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 1673eda578e5Saliguori const void *buf, int count1) 167483f64091Sbellard { 16756ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 167683f64091Sbellard int len, nb_sectors, count; 167783f64091Sbellard int64_t sector_num; 16789a8c4cceSKevin Wolf int ret; 167983f64091Sbellard 168083f64091Sbellard count = count1; 168183f64091Sbellard /* first write to align to sector start */ 16826ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 168383f64091Sbellard if (len > count) 168483f64091Sbellard len = count; 16856ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 168683f64091Sbellard if (len > 0) { 16879a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16889a8c4cceSKevin Wolf return ret; 16896ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 16909a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 16919a8c4cceSKevin Wolf return ret; 169283f64091Sbellard count -= len; 169383f64091Sbellard if (count == 0) 169483f64091Sbellard return count1; 169583f64091Sbellard sector_num++; 169683f64091Sbellard buf += len; 169783f64091Sbellard } 169883f64091Sbellard 169983f64091Sbellard /* write the sectors "in place" */ 17006ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 170183f64091Sbellard if (nb_sectors > 0) { 17029a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 17039a8c4cceSKevin Wolf return ret; 170483f64091Sbellard sector_num += nb_sectors; 17056ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 170683f64091Sbellard buf += len; 170783f64091Sbellard count -= len; 170883f64091Sbellard } 170983f64091Sbellard 171083f64091Sbellard /* add data from the last sector */ 171183f64091Sbellard if (count > 0) { 17129a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 17139a8c4cceSKevin Wolf return ret; 171483f64091Sbellard memcpy(tmp_buf, buf, count); 17159a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 17169a8c4cceSKevin Wolf return ret; 171783f64091Sbellard } 171883f64091Sbellard return count1; 171983f64091Sbellard } 172083f64091Sbellard 1721f08145feSKevin Wolf /* 1722f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1723f08145feSKevin Wolf * request (acts as a barrier) 1724f08145feSKevin Wolf * 1725f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1726f08145feSKevin Wolf */ 1727f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1728f08145feSKevin Wolf const void *buf, int count) 1729f08145feSKevin Wolf { 1730f08145feSKevin Wolf int ret; 1731f08145feSKevin Wolf 1732f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1733f08145feSKevin Wolf if (ret < 0) { 1734f08145feSKevin Wolf return ret; 1735f08145feSKevin Wolf } 1736f08145feSKevin Wolf 173792196b2fSStefan Hajnoczi /* No flush needed for cache modes that use O_DSYNC */ 173892196b2fSStefan Hajnoczi if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { 1739f08145feSKevin Wolf bdrv_flush(bs); 1740f08145feSKevin Wolf } 1741f08145feSKevin Wolf 1742f08145feSKevin Wolf return 0; 1743f08145feSKevin Wolf } 1744f08145feSKevin Wolf 1745470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 1746ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1747ab185921SStefan Hajnoczi { 1748ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 1749ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 1750ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 1751ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 1752ab185921SStefan Hajnoczi */ 1753ab185921SStefan Hajnoczi void *bounce_buffer; 1754ab185921SStefan Hajnoczi 175579c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 1756ab185921SStefan Hajnoczi struct iovec iov; 1757ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 1758ab185921SStefan Hajnoczi int64_t cluster_sector_num; 1759ab185921SStefan Hajnoczi int cluster_nb_sectors; 1760ab185921SStefan Hajnoczi size_t skip_bytes; 1761ab185921SStefan Hajnoczi int ret; 1762ab185921SStefan Hajnoczi 1763ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 1764ab185921SStefan Hajnoczi * allocating cluster in the image file. 1765ab185921SStefan Hajnoczi */ 1766ab185921SStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1767ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1768ab185921SStefan Hajnoczi 1769470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 1770ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 1771ab185921SStefan Hajnoczi 1772ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 1773ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 1774ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 1775ab185921SStefan Hajnoczi 177679c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 1777ab185921SStefan Hajnoczi &bounce_qiov); 1778ab185921SStefan Hajnoczi if (ret < 0) { 1779ab185921SStefan Hajnoczi goto err; 1780ab185921SStefan Hajnoczi } 1781ab185921SStefan Hajnoczi 178279c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 178379c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 1784621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 178579c053bdSStefan Hajnoczi cluster_nb_sectors); 178679c053bdSStefan Hajnoczi } else { 178779c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 1788ab185921SStefan Hajnoczi &bounce_qiov); 178979c053bdSStefan Hajnoczi } 179079c053bdSStefan Hajnoczi 1791ab185921SStefan Hajnoczi if (ret < 0) { 1792ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 1793ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 1794ab185921SStefan Hajnoczi * Simply report it in all cases. 1795ab185921SStefan Hajnoczi */ 1796ab185921SStefan Hajnoczi goto err; 1797ab185921SStefan Hajnoczi } 1798ab185921SStefan Hajnoczi 1799ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 1800ab185921SStefan Hajnoczi qemu_iovec_from_buffer(qiov, bounce_buffer + skip_bytes, 1801ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 1802ab185921SStefan Hajnoczi 1803ab185921SStefan Hajnoczi err: 1804ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 1805ab185921SStefan Hajnoczi return ret; 1806ab185921SStefan Hajnoczi } 1807ab185921SStefan Hajnoczi 1808c5fbe571SStefan Hajnoczi /* 1809c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 1810c5fbe571SStefan Hajnoczi */ 1811c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 1812470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1813470c0504SStefan Hajnoczi BdrvRequestFlags flags) 1814da1fa91dSKevin Wolf { 1815da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 1816dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 1817dbffbdcfSStefan Hajnoczi int ret; 1818da1fa91dSKevin Wolf 1819da1fa91dSKevin Wolf if (!drv) { 1820da1fa91dSKevin Wolf return -ENOMEDIUM; 1821da1fa91dSKevin Wolf } 1822da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1823da1fa91dSKevin Wolf return -EIO; 1824da1fa91dSKevin Wolf } 1825da1fa91dSKevin Wolf 182698f90dbaSZhi Yong Wu /* throttling disk read I/O */ 182798f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 182898f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, false, nb_sectors); 182998f90dbaSZhi Yong Wu } 183098f90dbaSZhi Yong Wu 1831f4658285SStefan Hajnoczi if (bs->copy_on_read) { 1832470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 1833470c0504SStefan Hajnoczi } 1834470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1835470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 1836470c0504SStefan Hajnoczi } 1837470c0504SStefan Hajnoczi 1838470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1839f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1840f4658285SStefan Hajnoczi } 1841f4658285SStefan Hajnoczi 1842dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 1843ab185921SStefan Hajnoczi 1844470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1845ab185921SStefan Hajnoczi int pnum; 1846ab185921SStefan Hajnoczi 1847ab185921SStefan Hajnoczi ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); 1848ab185921SStefan Hajnoczi if (ret < 0) { 1849ab185921SStefan Hajnoczi goto out; 1850ab185921SStefan Hajnoczi } 1851ab185921SStefan Hajnoczi 1852ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 1853470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 1854ab185921SStefan Hajnoczi goto out; 1855ab185921SStefan Hajnoczi } 1856ab185921SStefan Hajnoczi } 1857ab185921SStefan Hajnoczi 1858dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 1859ab185921SStefan Hajnoczi 1860ab185921SStefan Hajnoczi out: 1861dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1862470c0504SStefan Hajnoczi 1863470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1864470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 1865470c0504SStefan Hajnoczi } 1866470c0504SStefan Hajnoczi 1867dbffbdcfSStefan Hajnoczi return ret; 1868da1fa91dSKevin Wolf } 1869da1fa91dSKevin Wolf 1870c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 1871da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 1872da1fa91dSKevin Wolf { 1873c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 1874da1fa91dSKevin Wolf 1875470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 1876470c0504SStefan Hajnoczi } 1877470c0504SStefan Hajnoczi 1878470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 1879470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1880470c0504SStefan Hajnoczi { 1881470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 1882470c0504SStefan Hajnoczi 1883470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 1884470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 1885c5fbe571SStefan Hajnoczi } 1886c5fbe571SStefan Hajnoczi 1887f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 1888f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1889f08f2ddaSStefan Hajnoczi { 1890f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 1891f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 1892f08f2ddaSStefan Hajnoczi struct iovec iov; 1893f08f2ddaSStefan Hajnoczi int ret; 1894f08f2ddaSStefan Hajnoczi 1895621f0589SKevin Wolf /* TODO Emulate only part of misaligned requests instead of letting block 1896621f0589SKevin Wolf * drivers return -ENOTSUP and emulate everything */ 1897621f0589SKevin Wolf 1898f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 1899f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 1900621f0589SKevin Wolf ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1901621f0589SKevin Wolf if (ret != -ENOTSUP) { 1902621f0589SKevin Wolf return ret; 1903621f0589SKevin Wolf } 1904f08f2ddaSStefan Hajnoczi } 1905f08f2ddaSStefan Hajnoczi 1906f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 1907f08f2ddaSStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 1908f08f2ddaSStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 1909f08f2ddaSStefan Hajnoczi memset(iov.iov_base, 0, iov.iov_len); 1910f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 1911f08f2ddaSStefan Hajnoczi 1912f08f2ddaSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 1913f08f2ddaSStefan Hajnoczi 1914f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 1915f08f2ddaSStefan Hajnoczi return ret; 1916f08f2ddaSStefan Hajnoczi } 1917f08f2ddaSStefan Hajnoczi 1918c5fbe571SStefan Hajnoczi /* 1919c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 1920c5fbe571SStefan Hajnoczi */ 1921c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 1922f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1923f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 1924c5fbe571SStefan Hajnoczi { 1925c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 1926dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 19276b7cb247SStefan Hajnoczi int ret; 1928da1fa91dSKevin Wolf 1929da1fa91dSKevin Wolf if (!bs->drv) { 1930da1fa91dSKevin Wolf return -ENOMEDIUM; 1931da1fa91dSKevin Wolf } 1932da1fa91dSKevin Wolf if (bs->read_only) { 1933da1fa91dSKevin Wolf return -EACCES; 1934da1fa91dSKevin Wolf } 1935da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1936da1fa91dSKevin Wolf return -EIO; 1937da1fa91dSKevin Wolf } 1938da1fa91dSKevin Wolf 193998f90dbaSZhi Yong Wu /* throttling disk write I/O */ 194098f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 194198f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, true, nb_sectors); 194298f90dbaSZhi Yong Wu } 194398f90dbaSZhi Yong Wu 1944470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1945f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1946f4658285SStefan Hajnoczi } 1947f4658285SStefan Hajnoczi 1948dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 1949dbffbdcfSStefan Hajnoczi 1950f08f2ddaSStefan Hajnoczi if (flags & BDRV_REQ_ZERO_WRITE) { 1951f08f2ddaSStefan Hajnoczi ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); 1952f08f2ddaSStefan Hajnoczi } else { 19536b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 1954f08f2ddaSStefan Hajnoczi } 19556b7cb247SStefan Hajnoczi 1956da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 1957da1fa91dSKevin Wolf set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 1958da1fa91dSKevin Wolf } 1959da1fa91dSKevin Wolf 1960da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 1961da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 1962da1fa91dSKevin Wolf } 1963da1fa91dSKevin Wolf 1964dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1965dbffbdcfSStefan Hajnoczi 19666b7cb247SStefan Hajnoczi return ret; 1967da1fa91dSKevin Wolf } 1968da1fa91dSKevin Wolf 1969c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 1970c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 1971c5fbe571SStefan Hajnoczi { 1972c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 1973c5fbe571SStefan Hajnoczi 1974f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 1975f08f2ddaSStefan Hajnoczi } 1976f08f2ddaSStefan Hajnoczi 1977f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 1978f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1979f08f2ddaSStefan Hajnoczi { 1980f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1981f08f2ddaSStefan Hajnoczi 1982f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 1983f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE); 1984c5fbe571SStefan Hajnoczi } 1985c5fbe571SStefan Hajnoczi 198683f64091Sbellard /** 198783f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 198883f64091Sbellard */ 198983f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 199083f64091Sbellard { 199183f64091Sbellard BlockDriver *drv = bs->drv; 199251762288SStefan Hajnoczi int ret; 199383f64091Sbellard if (!drv) 199419cb3738Sbellard return -ENOMEDIUM; 199583f64091Sbellard if (!drv->bdrv_truncate) 199683f64091Sbellard return -ENOTSUP; 199759f2689dSNaphtali Sprei if (bs->read_only) 199859f2689dSNaphtali Sprei return -EACCES; 19998591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 20008591675fSMarcelo Tosatti return -EBUSY; 200151762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 200251762288SStefan Hajnoczi if (ret == 0) { 200351762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2004145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 200551762288SStefan Hajnoczi } 200651762288SStefan Hajnoczi return ret; 200783f64091Sbellard } 200883f64091Sbellard 200983f64091Sbellard /** 20104a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 20114a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 20124a1d5e1fSFam Zheng */ 20134a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 20144a1d5e1fSFam Zheng { 20154a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 20164a1d5e1fSFam Zheng if (!drv) { 20174a1d5e1fSFam Zheng return -ENOMEDIUM; 20184a1d5e1fSFam Zheng } 20194a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 20204a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 20214a1d5e1fSFam Zheng } 20224a1d5e1fSFam Zheng if (bs->file) { 20234a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 20244a1d5e1fSFam Zheng } 20254a1d5e1fSFam Zheng return -ENOTSUP; 20264a1d5e1fSFam Zheng } 20274a1d5e1fSFam Zheng 20284a1d5e1fSFam Zheng /** 202983f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 203083f64091Sbellard */ 203183f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 203283f64091Sbellard { 203383f64091Sbellard BlockDriver *drv = bs->drv; 203483f64091Sbellard if (!drv) 203519cb3738Sbellard return -ENOMEDIUM; 203651762288SStefan Hajnoczi 20372c6942faSMarkus Armbruster if (bs->growable || bdrv_dev_has_removable_media(bs)) { 203846a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 203983f64091Sbellard return drv->bdrv_getlength(bs); 2040fc01f7e7Sbellard } 204146a4e4e6SStefan Hajnoczi } 204246a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 204346a4e4e6SStefan Hajnoczi } 2044fc01f7e7Sbellard 204519cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 204696b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2047fc01f7e7Sbellard { 204819cb3738Sbellard int64_t length; 204919cb3738Sbellard length = bdrv_getlength(bs); 205019cb3738Sbellard if (length < 0) 205119cb3738Sbellard length = 0; 205219cb3738Sbellard else 20536ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 205419cb3738Sbellard *nb_sectors_ptr = length; 2055fc01f7e7Sbellard } 2056cf98951bSbellard 2057f3d54fc4Saliguori struct partition { 2058f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 2059f3d54fc4Saliguori uint8_t head; /* starting head */ 2060f3d54fc4Saliguori uint8_t sector; /* starting sector */ 2061f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 2062f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 2063f3d54fc4Saliguori uint8_t end_head; /* end head */ 2064f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 2065f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 2066f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 2067f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 2068541dc0d4SStefan Weil } QEMU_PACKED; 2069f3d54fc4Saliguori 2070f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 2071f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 2072f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 2073f3d54fc4Saliguori { 2074eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 2075f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 2076f3d54fc4Saliguori struct partition *p; 2077f3d54fc4Saliguori uint32_t nr_sects; 2078a38131b6Sblueswir1 uint64_t nb_sectors; 2079498e386cSZhi Yong Wu bool enabled; 2080f3d54fc4Saliguori 2081f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 2082f3d54fc4Saliguori 2083498e386cSZhi Yong Wu /** 2084498e386cSZhi Yong Wu * The function will be invoked during startup not only in sync I/O mode, 2085498e386cSZhi Yong Wu * but also in async I/O mode. So the I/O throttling function has to 2086498e386cSZhi Yong Wu * be disabled temporarily here, not permanently. 2087498e386cSZhi Yong Wu */ 2088498e386cSZhi Yong Wu enabled = bs->io_limits_enabled; 2089498e386cSZhi Yong Wu bs->io_limits_enabled = false; 2090f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 2091498e386cSZhi Yong Wu bs->io_limits_enabled = enabled; 2092f3d54fc4Saliguori if (ret < 0) 2093f3d54fc4Saliguori return -1; 2094f3d54fc4Saliguori /* test msdos magic */ 2095f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 2096f3d54fc4Saliguori return -1; 2097f3d54fc4Saliguori for(i = 0; i < 4; i++) { 2098f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 2099f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 2100f3d54fc4Saliguori if (nr_sects && p->end_head) { 2101f3d54fc4Saliguori /* We make the assumption that the partition terminates on 2102f3d54fc4Saliguori a cylinder boundary */ 2103f3d54fc4Saliguori heads = p->end_head + 1; 2104f3d54fc4Saliguori sectors = p->end_sector & 63; 2105f3d54fc4Saliguori if (sectors == 0) 2106f3d54fc4Saliguori continue; 2107f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 2108f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 2109f3d54fc4Saliguori continue; 2110f3d54fc4Saliguori *pheads = heads; 2111f3d54fc4Saliguori *psectors = sectors; 2112f3d54fc4Saliguori *pcylinders = cylinders; 2113f3d54fc4Saliguori #if 0 2114f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 2115f3d54fc4Saliguori cylinders, heads, sectors); 2116f3d54fc4Saliguori #endif 2117f3d54fc4Saliguori return 0; 2118f3d54fc4Saliguori } 2119f3d54fc4Saliguori } 2120f3d54fc4Saliguori return -1; 2121f3d54fc4Saliguori } 2122f3d54fc4Saliguori 2123f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 2124f3d54fc4Saliguori { 2125f3d54fc4Saliguori int translation, lba_detected = 0; 2126f3d54fc4Saliguori int cylinders, heads, secs; 2127a38131b6Sblueswir1 uint64_t nb_sectors; 2128f3d54fc4Saliguori 2129f3d54fc4Saliguori /* if a geometry hint is available, use it */ 2130f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 2131f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 2132f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 2133f3d54fc4Saliguori if (cylinders != 0) { 2134f3d54fc4Saliguori *pcyls = cylinders; 2135f3d54fc4Saliguori *pheads = heads; 2136f3d54fc4Saliguori *psecs = secs; 2137f3d54fc4Saliguori } else { 2138f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 2139f3d54fc4Saliguori if (heads > 16) { 2140f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 2141f3d54fc4Saliguori translation was active, so the default 2142f3d54fc4Saliguori hardware geometry is OK */ 2143f3d54fc4Saliguori lba_detected = 1; 2144f3d54fc4Saliguori goto default_geometry; 2145f3d54fc4Saliguori } else { 2146f3d54fc4Saliguori *pcyls = cylinders; 2147f3d54fc4Saliguori *pheads = heads; 2148f3d54fc4Saliguori *psecs = secs; 2149f3d54fc4Saliguori /* disable any translation to be in sync with 2150f3d54fc4Saliguori the logical geometry */ 2151f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 2152f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2153f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 2154f3d54fc4Saliguori } 2155f3d54fc4Saliguori } 2156f3d54fc4Saliguori } else { 2157f3d54fc4Saliguori default_geometry: 2158f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 2159f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 2160f3d54fc4Saliguori 2161f3d54fc4Saliguori if (cylinders > 16383) 2162f3d54fc4Saliguori cylinders = 16383; 2163f3d54fc4Saliguori else if (cylinders < 2) 2164f3d54fc4Saliguori cylinders = 2; 2165f3d54fc4Saliguori *pcyls = cylinders; 2166f3d54fc4Saliguori *pheads = 16; 2167f3d54fc4Saliguori *psecs = 63; 2168f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 2169f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 2170f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2171f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 2172f3d54fc4Saliguori } else { 2173f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2174f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 2175f3d54fc4Saliguori } 2176f3d54fc4Saliguori } 2177f3d54fc4Saliguori } 2178f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 2179f3d54fc4Saliguori } 2180f3d54fc4Saliguori } 2181f3d54fc4Saliguori 2182b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 2183b338082bSbellard int cyls, int heads, int secs) 2184b338082bSbellard { 2185b338082bSbellard bs->cyls = cyls; 2186b338082bSbellard bs->heads = heads; 2187b338082bSbellard bs->secs = secs; 2188b338082bSbellard } 2189b338082bSbellard 219046d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 219146d4767dSbellard { 219246d4767dSbellard bs->translation = translation; 219346d4767dSbellard } 219446d4767dSbellard 2195b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 2196b338082bSbellard int *pcyls, int *pheads, int *psecs) 2197b338082bSbellard { 2198b338082bSbellard *pcyls = bs->cyls; 2199b338082bSbellard *pheads = bs->heads; 2200b338082bSbellard *psecs = bs->secs; 2201b338082bSbellard } 2202b338082bSbellard 22030563e191SZhi Yong Wu /* throttling disk io limits */ 22040563e191SZhi Yong Wu void bdrv_set_io_limits(BlockDriverState *bs, 22050563e191SZhi Yong Wu BlockIOLimit *io_limits) 22060563e191SZhi Yong Wu { 22070563e191SZhi Yong Wu bs->io_limits = *io_limits; 22080563e191SZhi Yong Wu bs->io_limits_enabled = bdrv_io_limits_enabled(bs); 22090563e191SZhi Yong Wu } 22100563e191SZhi Yong Wu 22115bbdbb46SBlue Swirl /* Recognize floppy formats */ 22125bbdbb46SBlue Swirl typedef struct FDFormat { 22135bbdbb46SBlue Swirl FDriveType drive; 22145bbdbb46SBlue Swirl uint8_t last_sect; 22155bbdbb46SBlue Swirl uint8_t max_track; 22165bbdbb46SBlue Swirl uint8_t max_head; 2217f8d3d128SHervé Poussineau FDriveRate rate; 22185bbdbb46SBlue Swirl } FDFormat; 22195bbdbb46SBlue Swirl 22205bbdbb46SBlue Swirl static const FDFormat fd_formats[] = { 22215bbdbb46SBlue Swirl /* First entry is default format */ 22225bbdbb46SBlue Swirl /* 1.44 MB 3"1/2 floppy disks */ 2223f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 18, 80, 1, FDRIVE_RATE_500K, }, 2224f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 20, 80, 1, FDRIVE_RATE_500K, }, 2225f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 80, 1, FDRIVE_RATE_500K, }, 2226f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 82, 1, FDRIVE_RATE_500K, }, 2227f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 83, 1, FDRIVE_RATE_500K, }, 2228f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 22, 80, 1, FDRIVE_RATE_500K, }, 2229f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 23, 80, 1, FDRIVE_RATE_500K, }, 2230f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 24, 80, 1, FDRIVE_RATE_500K, }, 22315bbdbb46SBlue Swirl /* 2.88 MB 3"1/2 floppy disks */ 2232f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 36, 80, 1, FDRIVE_RATE_1M, }, 2233f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 39, 80, 1, FDRIVE_RATE_1M, }, 2234f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 40, 80, 1, FDRIVE_RATE_1M, }, 2235f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 44, 80, 1, FDRIVE_RATE_1M, }, 2236f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 48, 80, 1, FDRIVE_RATE_1M, }, 22375bbdbb46SBlue Swirl /* 720 kB 3"1/2 floppy disks */ 2238f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 9, 80, 1, FDRIVE_RATE_250K, }, 2239f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 80, 1, FDRIVE_RATE_250K, }, 2240f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 82, 1, FDRIVE_RATE_250K, }, 2241f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 83, 1, FDRIVE_RATE_250K, }, 2242f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 13, 80, 1, FDRIVE_RATE_250K, }, 2243f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 14, 80, 1, FDRIVE_RATE_250K, }, 22445bbdbb46SBlue Swirl /* 1.2 MB 5"1/4 floppy disks */ 2245f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 15, 80, 1, FDRIVE_RATE_500K, }, 2246f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 80, 1, FDRIVE_RATE_500K, }, 2247f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 82, 1, FDRIVE_RATE_500K, }, 2248f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 83, 1, FDRIVE_RATE_500K, }, 2249f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 20, 80, 1, FDRIVE_RATE_500K, }, 22505bbdbb46SBlue Swirl /* 720 kB 5"1/4 floppy disks */ 2251f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 80, 1, FDRIVE_RATE_250K, }, 2252f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 11, 80, 1, FDRIVE_RATE_250K, }, 22535bbdbb46SBlue Swirl /* 360 kB 5"1/4 floppy disks */ 2254f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 40, 1, FDRIVE_RATE_300K, }, 2255f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 40, 0, FDRIVE_RATE_300K, }, 2256f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 10, 41, 1, FDRIVE_RATE_300K, }, 2257f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 10, 42, 1, FDRIVE_RATE_300K, }, 22585bbdbb46SBlue Swirl /* 320 kB 5"1/4 floppy disks */ 2259f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 8, 40, 1, FDRIVE_RATE_250K, }, 2260f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 8, 40, 0, FDRIVE_RATE_250K, }, 22615bbdbb46SBlue Swirl /* 360 kB must match 5"1/4 better than 3"1/2... */ 2262f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 9, 80, 0, FDRIVE_RATE_250K, }, 22635bbdbb46SBlue Swirl /* end */ 2264f8d3d128SHervé Poussineau { FDRIVE_DRV_NONE, -1, -1, 0, 0, }, 22655bbdbb46SBlue Swirl }; 22665bbdbb46SBlue Swirl 22675bbdbb46SBlue Swirl void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, 22685bbdbb46SBlue Swirl int *max_track, int *last_sect, 2269f8d3d128SHervé Poussineau FDriveType drive_in, FDriveType *drive, 2270f8d3d128SHervé Poussineau FDriveRate *rate) 22715bbdbb46SBlue Swirl { 22725bbdbb46SBlue Swirl const FDFormat *parse; 22735bbdbb46SBlue Swirl uint64_t nb_sectors, size; 22745bbdbb46SBlue Swirl int i, first_match, match; 22755bbdbb46SBlue Swirl 22765bbdbb46SBlue Swirl bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); 22775bbdbb46SBlue Swirl if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { 22785bbdbb46SBlue Swirl /* User defined disk */ 2279f8d3d128SHervé Poussineau *rate = FDRIVE_RATE_500K; 22805bbdbb46SBlue Swirl } else { 22815bbdbb46SBlue Swirl bdrv_get_geometry(bs, &nb_sectors); 22825bbdbb46SBlue Swirl match = -1; 22835bbdbb46SBlue Swirl first_match = -1; 22845bbdbb46SBlue Swirl for (i = 0; ; i++) { 22855bbdbb46SBlue Swirl parse = &fd_formats[i]; 22865bbdbb46SBlue Swirl if (parse->drive == FDRIVE_DRV_NONE) { 22875bbdbb46SBlue Swirl break; 22885bbdbb46SBlue Swirl } 22895bbdbb46SBlue Swirl if (drive_in == parse->drive || 22905bbdbb46SBlue Swirl drive_in == FDRIVE_DRV_NONE) { 22915bbdbb46SBlue Swirl size = (parse->max_head + 1) * parse->max_track * 22925bbdbb46SBlue Swirl parse->last_sect; 22935bbdbb46SBlue Swirl if (nb_sectors == size) { 22945bbdbb46SBlue Swirl match = i; 22955bbdbb46SBlue Swirl break; 22965bbdbb46SBlue Swirl } 22975bbdbb46SBlue Swirl if (first_match == -1) { 22985bbdbb46SBlue Swirl first_match = i; 22995bbdbb46SBlue Swirl } 23005bbdbb46SBlue Swirl } 23015bbdbb46SBlue Swirl } 23025bbdbb46SBlue Swirl if (match == -1) { 23035bbdbb46SBlue Swirl if (first_match == -1) { 23045bbdbb46SBlue Swirl match = 1; 23055bbdbb46SBlue Swirl } else { 23065bbdbb46SBlue Swirl match = first_match; 23075bbdbb46SBlue Swirl } 23085bbdbb46SBlue Swirl parse = &fd_formats[match]; 23095bbdbb46SBlue Swirl } 23105bbdbb46SBlue Swirl *nb_heads = parse->max_head + 1; 23115bbdbb46SBlue Swirl *max_track = parse->max_track; 23125bbdbb46SBlue Swirl *last_sect = parse->last_sect; 23135bbdbb46SBlue Swirl *drive = parse->drive; 2314f8d3d128SHervé Poussineau *rate = parse->rate; 23155bbdbb46SBlue Swirl } 23165bbdbb46SBlue Swirl } 23175bbdbb46SBlue Swirl 231846d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 231946d4767dSbellard { 232046d4767dSbellard return bs->translation; 232146d4767dSbellard } 232246d4767dSbellard 2323abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 2324abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 2325abd7f68dSMarkus Armbruster { 2326abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2327abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2328abd7f68dSMarkus Armbruster } 2329abd7f68dSMarkus Armbruster 2330abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 2331abd7f68dSMarkus Armbruster { 2332abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2333abd7f68dSMarkus Armbruster } 2334abd7f68dSMarkus Armbruster 2335b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2336b338082bSbellard { 2337b338082bSbellard return bs->read_only; 2338b338082bSbellard } 2339b338082bSbellard 2340985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2341985a03b0Sths { 2342985a03b0Sths return bs->sg; 2343985a03b0Sths } 2344985a03b0Sths 2345e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2346e900a7b7SChristoph Hellwig { 2347e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2348e900a7b7SChristoph Hellwig } 2349e900a7b7SChristoph Hellwig 2350ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2351ea2384d3Sbellard { 2352ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2353ea2384d3Sbellard return 1; 2354ea2384d3Sbellard return bs->encrypted; 2355ea2384d3Sbellard } 2356ea2384d3Sbellard 2357c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2358c0f4ce77Saliguori { 2359c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2360c0f4ce77Saliguori 2361c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2362c0f4ce77Saliguori return 1; 2363c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2364c0f4ce77Saliguori } 2365c0f4ce77Saliguori 2366ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2367ea2384d3Sbellard { 2368ea2384d3Sbellard int ret; 2369ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2370ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2371ea2384d3Sbellard if (ret < 0) 2372ea2384d3Sbellard return ret; 2373ea2384d3Sbellard if (!bs->encrypted) 2374ea2384d3Sbellard return 0; 2375ea2384d3Sbellard } 2376fd04a2aeSShahar Havivi if (!bs->encrypted) { 2377fd04a2aeSShahar Havivi return -EINVAL; 2378fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2379fd04a2aeSShahar Havivi return -ENOMEDIUM; 2380fd04a2aeSShahar Havivi } 2381c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2382bb5fc20fSaliguori if (ret < 0) { 2383bb5fc20fSaliguori bs->valid_key = 0; 2384bb5fc20fSaliguori } else if (!bs->valid_key) { 2385bb5fc20fSaliguori bs->valid_key = 1; 2386bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 23877d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 2388bb5fc20fSaliguori } 2389c0f4ce77Saliguori return ret; 2390ea2384d3Sbellard } 2391ea2384d3Sbellard 2392ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 2393ea2384d3Sbellard { 239419cb3738Sbellard if (!bs->drv) { 2395ea2384d3Sbellard buf[0] = '\0'; 2396ea2384d3Sbellard } else { 2397ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 2398ea2384d3Sbellard } 2399ea2384d3Sbellard } 2400ea2384d3Sbellard 2401ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2402ea2384d3Sbellard void *opaque) 2403ea2384d3Sbellard { 2404ea2384d3Sbellard BlockDriver *drv; 2405ea2384d3Sbellard 24068a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2407ea2384d3Sbellard it(opaque, drv->format_name); 2408ea2384d3Sbellard } 2409ea2384d3Sbellard } 2410ea2384d3Sbellard 2411b338082bSbellard BlockDriverState *bdrv_find(const char *name) 2412b338082bSbellard { 2413b338082bSbellard BlockDriverState *bs; 2414b338082bSbellard 24151b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 24161b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 2417b338082bSbellard return bs; 2418b338082bSbellard } 24191b7bdbc1SStefan Hajnoczi } 2420b338082bSbellard return NULL; 2421b338082bSbellard } 2422b338082bSbellard 24232f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 24242f399b0aSMarkus Armbruster { 24252f399b0aSMarkus Armbruster if (!bs) { 24262f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 24272f399b0aSMarkus Armbruster } 24282f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 24292f399b0aSMarkus Armbruster } 24302f399b0aSMarkus Armbruster 243151de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 243281d0912dSbellard { 243381d0912dSbellard BlockDriverState *bs; 243481d0912dSbellard 24351b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 243651de9760Saliguori it(opaque, bs); 243781d0912dSbellard } 243881d0912dSbellard } 243981d0912dSbellard 2440ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 2441ea2384d3Sbellard { 2442ea2384d3Sbellard return bs->device_name; 2443ea2384d3Sbellard } 2444ea2384d3Sbellard 2445c6ca28d6Saliguori void bdrv_flush_all(void) 2446c6ca28d6Saliguori { 2447c6ca28d6Saliguori BlockDriverState *bs; 2448c6ca28d6Saliguori 24491b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2450c6ca28d6Saliguori bdrv_flush(bs); 2451c6ca28d6Saliguori } 24521b7bdbc1SStefan Hajnoczi } 2453c6ca28d6Saliguori 2454f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2455f2feebbdSKevin Wolf { 2456f2feebbdSKevin Wolf assert(bs->drv); 2457f2feebbdSKevin Wolf 2458336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2459336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2460f2feebbdSKevin Wolf } 2461f2feebbdSKevin Wolf 2462f2feebbdSKevin Wolf return 1; 2463f2feebbdSKevin Wolf } 2464f2feebbdSKevin Wolf 2465376ae3f1SStefan Hajnoczi typedef struct BdrvCoIsAllocatedData { 2466376ae3f1SStefan Hajnoczi BlockDriverState *bs; 2467376ae3f1SStefan Hajnoczi int64_t sector_num; 2468376ae3f1SStefan Hajnoczi int nb_sectors; 2469376ae3f1SStefan Hajnoczi int *pnum; 2470376ae3f1SStefan Hajnoczi int ret; 2471376ae3f1SStefan Hajnoczi bool done; 2472376ae3f1SStefan Hajnoczi } BdrvCoIsAllocatedData; 2473376ae3f1SStefan Hajnoczi 2474f58c7b35Sths /* 2475f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 2476f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 2477f58c7b35Sths * hence all their sectors are reported as allocated. 2478f58c7b35Sths * 2479bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 2480bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 2481bd9533e3SStefan Hajnoczi * 2482f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 2483f58c7b35Sths * the specified sector) that are known to be in the same 2484f58c7b35Sths * allocated/unallocated state. 2485f58c7b35Sths * 2486bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2487bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 2488f58c7b35Sths */ 2489060f51c9SStefan Hajnoczi int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, 2490060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 2491f58c7b35Sths { 2492f58c7b35Sths int64_t n; 2493bd9533e3SStefan Hajnoczi 24946aebab14SStefan Hajnoczi if (sector_num >= bs->total_sectors) { 24956aebab14SStefan Hajnoczi *pnum = 0; 24966aebab14SStefan Hajnoczi return 0; 24976aebab14SStefan Hajnoczi } 2498bd9533e3SStefan Hajnoczi 24996aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 2500bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 2501bd9533e3SStefan Hajnoczi nb_sectors = n; 2502bd9533e3SStefan Hajnoczi } 2503bd9533e3SStefan Hajnoczi 2504bd9533e3SStefan Hajnoczi if (!bs->drv->bdrv_co_is_allocated) { 2505bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 25066aebab14SStefan Hajnoczi return 1; 25076aebab14SStefan Hajnoczi } 25086aebab14SStefan Hajnoczi 2509060f51c9SStefan Hajnoczi return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum); 2510060f51c9SStefan Hajnoczi } 2511060f51c9SStefan Hajnoczi 2512060f51c9SStefan Hajnoczi /* Coroutine wrapper for bdrv_is_allocated() */ 2513060f51c9SStefan Hajnoczi static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) 2514060f51c9SStefan Hajnoczi { 2515060f51c9SStefan Hajnoczi BdrvCoIsAllocatedData *data = opaque; 2516060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 2517060f51c9SStefan Hajnoczi 2518060f51c9SStefan Hajnoczi data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, 2519060f51c9SStefan Hajnoczi data->pnum); 2520060f51c9SStefan Hajnoczi data->done = true; 2521060f51c9SStefan Hajnoczi } 2522060f51c9SStefan Hajnoczi 2523060f51c9SStefan Hajnoczi /* 2524060f51c9SStefan Hajnoczi * Synchronous wrapper around bdrv_co_is_allocated(). 2525060f51c9SStefan Hajnoczi * 2526060f51c9SStefan Hajnoczi * See bdrv_co_is_allocated() for details. 2527060f51c9SStefan Hajnoczi */ 2528060f51c9SStefan Hajnoczi int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 2529060f51c9SStefan Hajnoczi int *pnum) 2530060f51c9SStefan Hajnoczi { 2531376ae3f1SStefan Hajnoczi Coroutine *co; 2532376ae3f1SStefan Hajnoczi BdrvCoIsAllocatedData data = { 2533376ae3f1SStefan Hajnoczi .bs = bs, 2534376ae3f1SStefan Hajnoczi .sector_num = sector_num, 2535376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 2536376ae3f1SStefan Hajnoczi .pnum = pnum, 2537376ae3f1SStefan Hajnoczi .done = false, 2538376ae3f1SStefan Hajnoczi }; 2539376ae3f1SStefan Hajnoczi 2540376ae3f1SStefan Hajnoczi co = qemu_coroutine_create(bdrv_is_allocated_co_entry); 2541376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 2542376ae3f1SStefan Hajnoczi while (!data.done) { 2543376ae3f1SStefan Hajnoczi qemu_aio_wait(); 2544376ae3f1SStefan Hajnoczi } 2545376ae3f1SStefan Hajnoczi return data.ret; 2546376ae3f1SStefan Hajnoczi } 2547f58c7b35Sths 2548b2023818SLuiz Capitulino BlockInfoList *qmp_query_block(Error **errp) 2549b338082bSbellard { 2550b2023818SLuiz Capitulino BlockInfoList *head = NULL, *cur_item = NULL; 2551d15e5465SLuiz Capitulino BlockDriverState *bs; 2552d15e5465SLuiz Capitulino 25531b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2554b2023818SLuiz Capitulino BlockInfoList *info = g_malloc0(sizeof(*info)); 2555d15e5465SLuiz Capitulino 2556b2023818SLuiz Capitulino info->value = g_malloc0(sizeof(*info->value)); 2557b2023818SLuiz Capitulino info->value->device = g_strdup(bs->device_name); 2558b2023818SLuiz Capitulino info->value->type = g_strdup("unknown"); 2559b2023818SLuiz Capitulino info->value->locked = bdrv_dev_is_medium_locked(bs); 2560b2023818SLuiz Capitulino info->value->removable = bdrv_dev_has_removable_media(bs); 2561d15e5465SLuiz Capitulino 2562e4def80bSMarkus Armbruster if (bdrv_dev_has_removable_media(bs)) { 2563b2023818SLuiz Capitulino info->value->has_tray_open = true; 2564b2023818SLuiz Capitulino info->value->tray_open = bdrv_dev_is_tray_open(bs); 2565e4def80bSMarkus Armbruster } 2566f04ef601SLuiz Capitulino 2567f04ef601SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 2568b2023818SLuiz Capitulino info->value->has_io_status = true; 2569b2023818SLuiz Capitulino info->value->io_status = bs->iostatus; 2570f04ef601SLuiz Capitulino } 2571f04ef601SLuiz Capitulino 2572d15e5465SLuiz Capitulino if (bs->drv) { 2573b2023818SLuiz Capitulino info->value->has_inserted = true; 2574b2023818SLuiz Capitulino info->value->inserted = g_malloc0(sizeof(*info->value->inserted)); 2575b2023818SLuiz Capitulino info->value->inserted->file = g_strdup(bs->filename); 2576b2023818SLuiz Capitulino info->value->inserted->ro = bs->read_only; 2577b2023818SLuiz Capitulino info->value->inserted->drv = g_strdup(bs->drv->format_name); 2578b2023818SLuiz Capitulino info->value->inserted->encrypted = bs->encrypted; 2579b2023818SLuiz Capitulino if (bs->backing_file[0]) { 2580b2023818SLuiz Capitulino info->value->inserted->has_backing_file = true; 2581b2023818SLuiz Capitulino info->value->inserted->backing_file = g_strdup(bs->backing_file); 2582b2023818SLuiz Capitulino } 2583727f005eSZhi Yong Wu 2584727f005eSZhi Yong Wu if (bs->io_limits_enabled) { 2585727f005eSZhi Yong Wu info->value->inserted->bps = 2586727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 2587727f005eSZhi Yong Wu info->value->inserted->bps_rd = 2588727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; 2589727f005eSZhi Yong Wu info->value->inserted->bps_wr = 2590727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; 2591727f005eSZhi Yong Wu info->value->inserted->iops = 2592727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 2593727f005eSZhi Yong Wu info->value->inserted->iops_rd = 2594727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; 2595727f005eSZhi Yong Wu info->value->inserted->iops_wr = 2596727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; 2597727f005eSZhi Yong Wu } 2598d15e5465SLuiz Capitulino } 2599d15e5465SLuiz Capitulino 2600b2023818SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2601b2023818SLuiz Capitulino if (!cur_item) { 2602b2023818SLuiz Capitulino head = cur_item = info; 2603b2023818SLuiz Capitulino } else { 2604b2023818SLuiz Capitulino cur_item->next = info; 2605b2023818SLuiz Capitulino cur_item = info; 2606d15e5465SLuiz Capitulino } 2607d15e5465SLuiz Capitulino } 2608d15e5465SLuiz Capitulino 2609b2023818SLuiz Capitulino return head; 2610b338082bSbellard } 2611a36e69ddSths 2612f11f57e4SLuiz Capitulino /* Consider exposing this as a full fledged QMP command */ 2613f11f57e4SLuiz Capitulino static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp) 2614a36e69ddSths { 2615f11f57e4SLuiz Capitulino BlockStats *s; 2616218a536aSLuiz Capitulino 2617f11f57e4SLuiz Capitulino s = g_malloc0(sizeof(*s)); 2618218a536aSLuiz Capitulino 2619f11f57e4SLuiz Capitulino if (bs->device_name[0]) { 2620f11f57e4SLuiz Capitulino s->has_device = true; 2621f11f57e4SLuiz Capitulino s->device = g_strdup(bs->device_name); 2622218a536aSLuiz Capitulino } 2623218a536aSLuiz Capitulino 2624f11f57e4SLuiz Capitulino s->stats = g_malloc0(sizeof(*s->stats)); 2625f11f57e4SLuiz Capitulino s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; 2626f11f57e4SLuiz Capitulino s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; 2627f11f57e4SLuiz Capitulino s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; 2628f11f57e4SLuiz Capitulino s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; 2629f11f57e4SLuiz Capitulino s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; 2630f11f57e4SLuiz Capitulino s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; 2631f11f57e4SLuiz Capitulino s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; 2632f11f57e4SLuiz Capitulino s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; 2633f11f57e4SLuiz Capitulino s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; 2634294cc35fSKevin Wolf 2635294cc35fSKevin Wolf if (bs->file) { 2636f11f57e4SLuiz Capitulino s->has_parent = true; 2637f11f57e4SLuiz Capitulino s->parent = qmp_query_blockstat(bs->file, NULL); 2638294cc35fSKevin Wolf } 2639294cc35fSKevin Wolf 2640f11f57e4SLuiz Capitulino return s; 2641294cc35fSKevin Wolf } 2642294cc35fSKevin Wolf 2643f11f57e4SLuiz Capitulino BlockStatsList *qmp_query_blockstats(Error **errp) 2644218a536aSLuiz Capitulino { 2645f11f57e4SLuiz Capitulino BlockStatsList *head = NULL, *cur_item = NULL; 2646a36e69ddSths BlockDriverState *bs; 2647a36e69ddSths 26481b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2649f11f57e4SLuiz Capitulino BlockStatsList *info = g_malloc0(sizeof(*info)); 2650f11f57e4SLuiz Capitulino info->value = qmp_query_blockstat(bs, NULL); 2651f11f57e4SLuiz Capitulino 2652f11f57e4SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2653f11f57e4SLuiz Capitulino if (!cur_item) { 2654f11f57e4SLuiz Capitulino head = cur_item = info; 2655f11f57e4SLuiz Capitulino } else { 2656f11f57e4SLuiz Capitulino cur_item->next = info; 2657f11f57e4SLuiz Capitulino cur_item = info; 2658f11f57e4SLuiz Capitulino } 2659a36e69ddSths } 2660218a536aSLuiz Capitulino 2661f11f57e4SLuiz Capitulino return head; 2662a36e69ddSths } 2663ea2384d3Sbellard 2664045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 2665045df330Saliguori { 2666045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 2667045df330Saliguori return bs->backing_file; 2668045df330Saliguori else if (bs->encrypted) 2669045df330Saliguori return bs->filename; 2670045df330Saliguori else 2671045df330Saliguori return NULL; 2672045df330Saliguori } 2673045df330Saliguori 267483f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 267583f64091Sbellard char *filename, int filename_size) 267683f64091Sbellard { 267783f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 267883f64091Sbellard } 267983f64091Sbellard 2680faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 2681faea38e7Sbellard const uint8_t *buf, int nb_sectors) 2682faea38e7Sbellard { 2683faea38e7Sbellard BlockDriver *drv = bs->drv; 2684faea38e7Sbellard if (!drv) 268519cb3738Sbellard return -ENOMEDIUM; 2686faea38e7Sbellard if (!drv->bdrv_write_compressed) 2687faea38e7Sbellard return -ENOTSUP; 2688fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 2689fbb7b4e0SKevin Wolf return -EIO; 26907cd1e32aSlirans@il.ibm.com 2691c6d22830SJan Kiszka if (bs->dirty_bitmap) { 26927cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 26937cd1e32aSlirans@il.ibm.com } 26947cd1e32aSlirans@il.ibm.com 2695faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 2696faea38e7Sbellard } 2697faea38e7Sbellard 2698faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2699faea38e7Sbellard { 2700faea38e7Sbellard BlockDriver *drv = bs->drv; 2701faea38e7Sbellard if (!drv) 270219cb3738Sbellard return -ENOMEDIUM; 2703faea38e7Sbellard if (!drv->bdrv_get_info) 2704faea38e7Sbellard return -ENOTSUP; 2705faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 2706faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 2707faea38e7Sbellard } 2708faea38e7Sbellard 270945566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 271045566e9cSChristoph Hellwig int64_t pos, int size) 2711178e08a5Saliguori { 2712178e08a5Saliguori BlockDriver *drv = bs->drv; 2713178e08a5Saliguori if (!drv) 2714178e08a5Saliguori return -ENOMEDIUM; 27157cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 271645566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 27177cdb1f6dSMORITA Kazutaka if (bs->file) 27187cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 27197cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2720178e08a5Saliguori } 2721178e08a5Saliguori 272245566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 272345566e9cSChristoph Hellwig int64_t pos, int size) 2724178e08a5Saliguori { 2725178e08a5Saliguori BlockDriver *drv = bs->drv; 2726178e08a5Saliguori if (!drv) 2727178e08a5Saliguori return -ENOMEDIUM; 27287cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 272945566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 27307cdb1f6dSMORITA Kazutaka if (bs->file) 27317cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 27327cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2733178e08a5Saliguori } 2734178e08a5Saliguori 27358b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 27368b9b0cc2SKevin Wolf { 27378b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 27388b9b0cc2SKevin Wolf 27398b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 27408b9b0cc2SKevin Wolf return; 27418b9b0cc2SKevin Wolf } 27428b9b0cc2SKevin Wolf 27438b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 27448b9b0cc2SKevin Wolf 27458b9b0cc2SKevin Wolf } 27468b9b0cc2SKevin Wolf 2747faea38e7Sbellard /**************************************************************/ 2748faea38e7Sbellard /* handling of snapshots */ 2749faea38e7Sbellard 2750feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 2751feeee5acSMiguel Di Ciurcio Filho { 2752feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 275307b70bfbSMarkus Armbruster if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 2754feeee5acSMiguel Di Ciurcio Filho return 0; 2755feeee5acSMiguel Di Ciurcio Filho } 2756feeee5acSMiguel Di Ciurcio Filho 2757feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 2758feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 2759feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 2760feeee5acSMiguel Di Ciurcio Filho } 2761feeee5acSMiguel Di Ciurcio Filho return 0; 2762feeee5acSMiguel Di Ciurcio Filho } 2763feeee5acSMiguel Di Ciurcio Filho 2764feeee5acSMiguel Di Ciurcio Filho return 1; 2765feeee5acSMiguel Di Ciurcio Filho } 2766feeee5acSMiguel Di Ciurcio Filho 2767199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 2768199630b6SBlue Swirl { 2769199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 2770199630b6SBlue Swirl } 2771199630b6SBlue Swirl 2772f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 2773f9092b10SMarkus Armbruster { 2774f9092b10SMarkus Armbruster BlockDriverState *bs; 2775f9092b10SMarkus Armbruster 27763ac906f7SMarkus Armbruster if (bs_snapshots) { 2777f9092b10SMarkus Armbruster return bs_snapshots; 27783ac906f7SMarkus Armbruster } 2779f9092b10SMarkus Armbruster 2780f9092b10SMarkus Armbruster bs = NULL; 2781f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 2782f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 27833ac906f7SMarkus Armbruster bs_snapshots = bs; 27843ac906f7SMarkus Armbruster return bs; 2785f9092b10SMarkus Armbruster } 2786f9092b10SMarkus Armbruster } 2787f9092b10SMarkus Armbruster return NULL; 2788f9092b10SMarkus Armbruster } 2789f9092b10SMarkus Armbruster 2790faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 2791faea38e7Sbellard QEMUSnapshotInfo *sn_info) 2792faea38e7Sbellard { 2793faea38e7Sbellard BlockDriver *drv = bs->drv; 2794faea38e7Sbellard if (!drv) 279519cb3738Sbellard return -ENOMEDIUM; 27967cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 2797faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 27987cdb1f6dSMORITA Kazutaka if (bs->file) 27997cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 28007cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2801faea38e7Sbellard } 2802faea38e7Sbellard 2803faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 2804faea38e7Sbellard const char *snapshot_id) 2805faea38e7Sbellard { 2806faea38e7Sbellard BlockDriver *drv = bs->drv; 28077cdb1f6dSMORITA Kazutaka int ret, open_ret; 28087cdb1f6dSMORITA Kazutaka 2809faea38e7Sbellard if (!drv) 281019cb3738Sbellard return -ENOMEDIUM; 28117cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 2812faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 28137cdb1f6dSMORITA Kazutaka 28147cdb1f6dSMORITA Kazutaka if (bs->file) { 28157cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 28167cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 28177cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 28187cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 28197cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 28207cdb1f6dSMORITA Kazutaka bs->drv = NULL; 28217cdb1f6dSMORITA Kazutaka return open_ret; 28227cdb1f6dSMORITA Kazutaka } 28237cdb1f6dSMORITA Kazutaka return ret; 28247cdb1f6dSMORITA Kazutaka } 28257cdb1f6dSMORITA Kazutaka 28267cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2827faea38e7Sbellard } 2828faea38e7Sbellard 2829faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 2830faea38e7Sbellard { 2831faea38e7Sbellard BlockDriver *drv = bs->drv; 2832faea38e7Sbellard if (!drv) 283319cb3738Sbellard return -ENOMEDIUM; 28347cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 2835faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 28367cdb1f6dSMORITA Kazutaka if (bs->file) 28377cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 28387cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2839faea38e7Sbellard } 2840faea38e7Sbellard 2841faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 2842faea38e7Sbellard QEMUSnapshotInfo **psn_info) 2843faea38e7Sbellard { 2844faea38e7Sbellard BlockDriver *drv = bs->drv; 2845faea38e7Sbellard if (!drv) 284619cb3738Sbellard return -ENOMEDIUM; 28477cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 2848faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 28497cdb1f6dSMORITA Kazutaka if (bs->file) 28507cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 28517cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2852faea38e7Sbellard } 2853faea38e7Sbellard 285451ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 285551ef6727Sedison const char *snapshot_name) 285651ef6727Sedison { 285751ef6727Sedison BlockDriver *drv = bs->drv; 285851ef6727Sedison if (!drv) { 285951ef6727Sedison return -ENOMEDIUM; 286051ef6727Sedison } 286151ef6727Sedison if (!bs->read_only) { 286251ef6727Sedison return -EINVAL; 286351ef6727Sedison } 286451ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 286551ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 286651ef6727Sedison } 286751ef6727Sedison return -ENOTSUP; 286851ef6727Sedison } 286951ef6727Sedison 2870e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 2871e8a6bb9cSMarcelo Tosatti const char *backing_file) 2872e8a6bb9cSMarcelo Tosatti { 2873e8a6bb9cSMarcelo Tosatti if (!bs->drv) { 2874e8a6bb9cSMarcelo Tosatti return NULL; 2875e8a6bb9cSMarcelo Tosatti } 2876e8a6bb9cSMarcelo Tosatti 2877e8a6bb9cSMarcelo Tosatti if (bs->backing_hd) { 2878e8a6bb9cSMarcelo Tosatti if (strcmp(bs->backing_file, backing_file) == 0) { 2879e8a6bb9cSMarcelo Tosatti return bs->backing_hd; 2880e8a6bb9cSMarcelo Tosatti } else { 2881e8a6bb9cSMarcelo Tosatti return bdrv_find_backing_image(bs->backing_hd, backing_file); 2882e8a6bb9cSMarcelo Tosatti } 2883e8a6bb9cSMarcelo Tosatti } 2884e8a6bb9cSMarcelo Tosatti 2885e8a6bb9cSMarcelo Tosatti return NULL; 2886e8a6bb9cSMarcelo Tosatti } 2887e8a6bb9cSMarcelo Tosatti 2888faea38e7Sbellard #define NB_SUFFIXES 4 2889faea38e7Sbellard 2890faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 2891faea38e7Sbellard { 2892faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 2893faea38e7Sbellard int64_t base; 2894faea38e7Sbellard int i; 2895faea38e7Sbellard 2896faea38e7Sbellard if (size <= 999) { 2897faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 2898faea38e7Sbellard } else { 2899faea38e7Sbellard base = 1024; 2900faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 2901faea38e7Sbellard if (size < (10 * base)) { 2902faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 2903faea38e7Sbellard (double)size / base, 2904faea38e7Sbellard suffixes[i]); 2905faea38e7Sbellard break; 2906faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 2907faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 2908faea38e7Sbellard ((size + (base >> 1)) / base), 2909faea38e7Sbellard suffixes[i]); 2910faea38e7Sbellard break; 2911faea38e7Sbellard } 2912faea38e7Sbellard base = base * 1024; 2913faea38e7Sbellard } 2914faea38e7Sbellard } 2915faea38e7Sbellard return buf; 2916faea38e7Sbellard } 2917faea38e7Sbellard 2918faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 2919faea38e7Sbellard { 2920faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 29213b9f94e1Sbellard #ifdef _WIN32 29223b9f94e1Sbellard struct tm *ptm; 29233b9f94e1Sbellard #else 2924faea38e7Sbellard struct tm tm; 29253b9f94e1Sbellard #endif 2926faea38e7Sbellard time_t ti; 2927faea38e7Sbellard int64_t secs; 2928faea38e7Sbellard 2929faea38e7Sbellard if (!sn) { 2930faea38e7Sbellard snprintf(buf, buf_size, 2931faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2932faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 2933faea38e7Sbellard } else { 2934faea38e7Sbellard ti = sn->date_sec; 29353b9f94e1Sbellard #ifdef _WIN32 29363b9f94e1Sbellard ptm = localtime(&ti); 29373b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 29383b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 29393b9f94e1Sbellard #else 2940faea38e7Sbellard localtime_r(&ti, &tm); 2941faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 2942faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 29433b9f94e1Sbellard #endif 2944faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 2945faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 2946faea38e7Sbellard "%02d:%02d:%02d.%03d", 2947faea38e7Sbellard (int)(secs / 3600), 2948faea38e7Sbellard (int)((secs / 60) % 60), 2949faea38e7Sbellard (int)(secs % 60), 2950faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 2951faea38e7Sbellard snprintf(buf, buf_size, 2952faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2953faea38e7Sbellard sn->id_str, sn->name, 2954faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 2955faea38e7Sbellard date_buf, 2956faea38e7Sbellard clock_buf); 2957faea38e7Sbellard } 2958faea38e7Sbellard return buf; 2959faea38e7Sbellard } 2960faea38e7Sbellard 2961ea2384d3Sbellard /**************************************************************/ 296283f64091Sbellard /* async I/Os */ 2963ea2384d3Sbellard 29643b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 2965f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 296683f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 2967ea2384d3Sbellard { 2968bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 2969bbf0a440SStefan Hajnoczi 2970b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 29718c5873d6SStefan Hajnoczi cb, opaque, false); 297283f64091Sbellard } 297383f64091Sbellard 2974f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 2975f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 297683f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 29777674e7bfSbellard { 2978bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 2979bbf0a440SStefan Hajnoczi 29801a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 29818c5873d6SStefan Hajnoczi cb, opaque, true); 298283f64091Sbellard } 298383f64091Sbellard 298440b4f539SKevin Wolf 298540b4f539SKevin Wolf typedef struct MultiwriteCB { 298640b4f539SKevin Wolf int error; 298740b4f539SKevin Wolf int num_requests; 298840b4f539SKevin Wolf int num_callbacks; 298940b4f539SKevin Wolf struct { 299040b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 299140b4f539SKevin Wolf void *opaque; 299240b4f539SKevin Wolf QEMUIOVector *free_qiov; 299340b4f539SKevin Wolf } callbacks[]; 299440b4f539SKevin Wolf } MultiwriteCB; 299540b4f539SKevin Wolf 299640b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 299740b4f539SKevin Wolf { 299840b4f539SKevin Wolf int i; 299940b4f539SKevin Wolf 300040b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 300140b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 30021e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 30031e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 30041e1ea48dSStefan Hajnoczi } 30057267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 300640b4f539SKevin Wolf } 300740b4f539SKevin Wolf } 300840b4f539SKevin Wolf 300940b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 301040b4f539SKevin Wolf { 301140b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 301240b4f539SKevin Wolf 30136d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 30146d519a5fSStefan Hajnoczi 3015cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 301640b4f539SKevin Wolf mcb->error = ret; 301740b4f539SKevin Wolf } 301840b4f539SKevin Wolf 301940b4f539SKevin Wolf mcb->num_requests--; 302040b4f539SKevin Wolf if (mcb->num_requests == 0) { 302140b4f539SKevin Wolf multiwrite_user_cb(mcb); 30227267c094SAnthony Liguori g_free(mcb); 302340b4f539SKevin Wolf } 302440b4f539SKevin Wolf } 302540b4f539SKevin Wolf 302640b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 302740b4f539SKevin Wolf { 302877be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 302977be4366SChristoph Hellwig 303077be4366SChristoph Hellwig /* 303177be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 303277be4366SChristoph Hellwig * here as that could overflow the return value. 303377be4366SChristoph Hellwig */ 303477be4366SChristoph Hellwig if (req1->sector > req2->sector) { 303577be4366SChristoph Hellwig return 1; 303677be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 303777be4366SChristoph Hellwig return -1; 303877be4366SChristoph Hellwig } else { 303977be4366SChristoph Hellwig return 0; 304077be4366SChristoph Hellwig } 304140b4f539SKevin Wolf } 304240b4f539SKevin Wolf 304340b4f539SKevin Wolf /* 304440b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 304540b4f539SKevin Wolf * requests that remain after merging. 304640b4f539SKevin Wolf */ 304740b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 304840b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 304940b4f539SKevin Wolf { 305040b4f539SKevin Wolf int i, outidx; 305140b4f539SKevin Wolf 305240b4f539SKevin Wolf // Sort requests by start sector 305340b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 305440b4f539SKevin Wolf 305540b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 305640b4f539SKevin Wolf // filling up gaps with zero sectors. 305740b4f539SKevin Wolf outidx = 0; 305840b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 305940b4f539SKevin Wolf int merge = 0; 306040b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 306140b4f539SKevin Wolf 3062b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 306340b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 306440b4f539SKevin Wolf merge = 1; 306540b4f539SKevin Wolf } 306640b4f539SKevin Wolf 3067e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3068e2a305fbSChristoph Hellwig merge = 0; 3069e2a305fbSChristoph Hellwig } 3070e2a305fbSChristoph Hellwig 307140b4f539SKevin Wolf if (merge) { 307240b4f539SKevin Wolf size_t size; 30737267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 307440b4f539SKevin Wolf qemu_iovec_init(qiov, 307540b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 307640b4f539SKevin Wolf 307740b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 307840b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 307940b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 308040b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 308140b4f539SKevin Wolf 3082b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3083b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 308440b4f539SKevin Wolf 308540b4f539SKevin Wolf // Add the second request 308640b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 308740b4f539SKevin Wolf 3088cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 308940b4f539SKevin Wolf reqs[outidx].qiov = qiov; 309040b4f539SKevin Wolf 309140b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 309240b4f539SKevin Wolf } else { 309340b4f539SKevin Wolf outidx++; 309440b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 309540b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 309640b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 309740b4f539SKevin Wolf } 309840b4f539SKevin Wolf } 309940b4f539SKevin Wolf 310040b4f539SKevin Wolf return outidx + 1; 310140b4f539SKevin Wolf } 310240b4f539SKevin Wolf 310340b4f539SKevin Wolf /* 310440b4f539SKevin Wolf * Submit multiple AIO write requests at once. 310540b4f539SKevin Wolf * 310640b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 310740b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 310840b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 310940b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 311040b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 311140b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 311240b4f539SKevin Wolf * 311340b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 311440b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 311540b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 311640b4f539SKevin Wolf */ 311740b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 311840b4f539SKevin Wolf { 311940b4f539SKevin Wolf MultiwriteCB *mcb; 312040b4f539SKevin Wolf int i; 312140b4f539SKevin Wolf 3122301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3123301db7c2SRyan Harper if (bs->drv == NULL) { 3124301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3125301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3126301db7c2SRyan Harper } 3127301db7c2SRyan Harper return -1; 3128301db7c2SRyan Harper } 3129301db7c2SRyan Harper 313040b4f539SKevin Wolf if (num_reqs == 0) { 313140b4f539SKevin Wolf return 0; 313240b4f539SKevin Wolf } 313340b4f539SKevin Wolf 313440b4f539SKevin Wolf // Create MultiwriteCB structure 31357267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 313640b4f539SKevin Wolf mcb->num_requests = 0; 313740b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 313840b4f539SKevin Wolf 313940b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 314040b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 314140b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 314240b4f539SKevin Wolf } 314340b4f539SKevin Wolf 314440b4f539SKevin Wolf // Check for mergable requests 314540b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 314640b4f539SKevin Wolf 31476d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 31486d519a5fSStefan Hajnoczi 3149df9309fbSPaolo Bonzini /* Run the aio requests. */ 3150df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 315140b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3152ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 315340b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 315440b4f539SKevin Wolf } 315540b4f539SKevin Wolf 315640b4f539SKevin Wolf return 0; 315740b4f539SKevin Wolf } 315840b4f539SKevin Wolf 315983f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 316083f64091Sbellard { 31616bbff9a0Saliguori acb->pool->cancel(acb); 316283f64091Sbellard } 316383f64091Sbellard 316498f90dbaSZhi Yong Wu /* block I/O throttling */ 316598f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 316698f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait) 316798f90dbaSZhi Yong Wu { 316898f90dbaSZhi Yong Wu uint64_t bps_limit = 0; 316998f90dbaSZhi Yong Wu double bytes_limit, bytes_base, bytes_res; 317098f90dbaSZhi Yong Wu double slice_time, wait_time; 317198f90dbaSZhi Yong Wu 317298f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 317398f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 317498f90dbaSZhi Yong Wu } else if (bs->io_limits.bps[is_write]) { 317598f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[is_write]; 317698f90dbaSZhi Yong Wu } else { 317798f90dbaSZhi Yong Wu if (wait) { 317898f90dbaSZhi Yong Wu *wait = 0; 317998f90dbaSZhi Yong Wu } 318098f90dbaSZhi Yong Wu 318198f90dbaSZhi Yong Wu return false; 318298f90dbaSZhi Yong Wu } 318398f90dbaSZhi Yong Wu 318498f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 318598f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 318698f90dbaSZhi Yong Wu bytes_limit = bps_limit * slice_time; 318798f90dbaSZhi Yong Wu bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; 318898f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 318998f90dbaSZhi Yong Wu bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; 319098f90dbaSZhi Yong Wu } 319198f90dbaSZhi Yong Wu 319298f90dbaSZhi Yong Wu /* bytes_base: the bytes of data which have been read/written; and 319398f90dbaSZhi Yong Wu * it is obtained from the history statistic info. 319498f90dbaSZhi Yong Wu * bytes_res: the remaining bytes of data which need to be read/written. 319598f90dbaSZhi Yong Wu * (bytes_base + bytes_res) / bps_limit: used to calcuate 319698f90dbaSZhi Yong Wu * the total time for completing reading/writting all data. 319798f90dbaSZhi Yong Wu */ 319898f90dbaSZhi Yong Wu bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 319998f90dbaSZhi Yong Wu 320098f90dbaSZhi Yong Wu if (bytes_base + bytes_res <= bytes_limit) { 320198f90dbaSZhi Yong Wu if (wait) { 320298f90dbaSZhi Yong Wu *wait = 0; 320398f90dbaSZhi Yong Wu } 320498f90dbaSZhi Yong Wu 320598f90dbaSZhi Yong Wu return false; 320698f90dbaSZhi Yong Wu } 320798f90dbaSZhi Yong Wu 320898f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 320998f90dbaSZhi Yong Wu wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; 321098f90dbaSZhi Yong Wu 321198f90dbaSZhi Yong Wu /* When the I/O rate at runtime exceeds the limits, 321298f90dbaSZhi Yong Wu * bs->slice_end need to be extended in order that the current statistic 321398f90dbaSZhi Yong Wu * info can be kept until the timer fire, so it is increased and tuned 321498f90dbaSZhi Yong Wu * based on the result of experiment. 321598f90dbaSZhi Yong Wu */ 321698f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 321798f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 321898f90dbaSZhi Yong Wu if (wait) { 321998f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 322098f90dbaSZhi Yong Wu } 322198f90dbaSZhi Yong Wu 322298f90dbaSZhi Yong Wu return true; 322398f90dbaSZhi Yong Wu } 322498f90dbaSZhi Yong Wu 322598f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 322698f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait) 322798f90dbaSZhi Yong Wu { 322898f90dbaSZhi Yong Wu uint64_t iops_limit = 0; 322998f90dbaSZhi Yong Wu double ios_limit, ios_base; 323098f90dbaSZhi Yong Wu double slice_time, wait_time; 323198f90dbaSZhi Yong Wu 323298f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 323398f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 323498f90dbaSZhi Yong Wu } else if (bs->io_limits.iops[is_write]) { 323598f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[is_write]; 323698f90dbaSZhi Yong Wu } else { 323798f90dbaSZhi Yong Wu if (wait) { 323898f90dbaSZhi Yong Wu *wait = 0; 323998f90dbaSZhi Yong Wu } 324098f90dbaSZhi Yong Wu 324198f90dbaSZhi Yong Wu return false; 324298f90dbaSZhi Yong Wu } 324398f90dbaSZhi Yong Wu 324498f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 324598f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 324698f90dbaSZhi Yong Wu ios_limit = iops_limit * slice_time; 324798f90dbaSZhi Yong Wu ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; 324898f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 324998f90dbaSZhi Yong Wu ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; 325098f90dbaSZhi Yong Wu } 325198f90dbaSZhi Yong Wu 325298f90dbaSZhi Yong Wu if (ios_base + 1 <= ios_limit) { 325398f90dbaSZhi Yong Wu if (wait) { 325498f90dbaSZhi Yong Wu *wait = 0; 325598f90dbaSZhi Yong Wu } 325698f90dbaSZhi Yong Wu 325798f90dbaSZhi Yong Wu return false; 325898f90dbaSZhi Yong Wu } 325998f90dbaSZhi Yong Wu 326098f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 326198f90dbaSZhi Yong Wu wait_time = (ios_base + 1) / iops_limit; 326298f90dbaSZhi Yong Wu if (wait_time > elapsed_time) { 326398f90dbaSZhi Yong Wu wait_time = wait_time - elapsed_time; 326498f90dbaSZhi Yong Wu } else { 326598f90dbaSZhi Yong Wu wait_time = 0; 326698f90dbaSZhi Yong Wu } 326798f90dbaSZhi Yong Wu 326898f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 326998f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 327098f90dbaSZhi Yong Wu if (wait) { 327198f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 327298f90dbaSZhi Yong Wu } 327398f90dbaSZhi Yong Wu 327498f90dbaSZhi Yong Wu return true; 327598f90dbaSZhi Yong Wu } 327698f90dbaSZhi Yong Wu 327798f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 327898f90dbaSZhi Yong Wu bool is_write, int64_t *wait) 327998f90dbaSZhi Yong Wu { 328098f90dbaSZhi Yong Wu int64_t now, max_wait; 328198f90dbaSZhi Yong Wu uint64_t bps_wait = 0, iops_wait = 0; 328298f90dbaSZhi Yong Wu double elapsed_time; 328398f90dbaSZhi Yong Wu int bps_ret, iops_ret; 328498f90dbaSZhi Yong Wu 328598f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 328698f90dbaSZhi Yong Wu if ((bs->slice_start < now) 328798f90dbaSZhi Yong Wu && (bs->slice_end > now)) { 328898f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 328998f90dbaSZhi Yong Wu } else { 329098f90dbaSZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 329198f90dbaSZhi Yong Wu bs->slice_start = now; 329298f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 329398f90dbaSZhi Yong Wu 329498f90dbaSZhi Yong Wu bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; 329598f90dbaSZhi Yong Wu bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; 329698f90dbaSZhi Yong Wu 329798f90dbaSZhi Yong Wu bs->io_base.ios[is_write] = bs->nr_ops[is_write]; 329898f90dbaSZhi Yong Wu bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; 329998f90dbaSZhi Yong Wu } 330098f90dbaSZhi Yong Wu 330198f90dbaSZhi Yong Wu elapsed_time = now - bs->slice_start; 330298f90dbaSZhi Yong Wu elapsed_time /= (NANOSECONDS_PER_SECOND); 330398f90dbaSZhi Yong Wu 330498f90dbaSZhi Yong Wu bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, 330598f90dbaSZhi Yong Wu is_write, elapsed_time, &bps_wait); 330698f90dbaSZhi Yong Wu iops_ret = bdrv_exceed_iops_limits(bs, is_write, 330798f90dbaSZhi Yong Wu elapsed_time, &iops_wait); 330898f90dbaSZhi Yong Wu if (bps_ret || iops_ret) { 330998f90dbaSZhi Yong Wu max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; 331098f90dbaSZhi Yong Wu if (wait) { 331198f90dbaSZhi Yong Wu *wait = max_wait; 331298f90dbaSZhi Yong Wu } 331398f90dbaSZhi Yong Wu 331498f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 331598f90dbaSZhi Yong Wu if (bs->slice_end < now + max_wait) { 331698f90dbaSZhi Yong Wu bs->slice_end = now + max_wait; 331798f90dbaSZhi Yong Wu } 331898f90dbaSZhi Yong Wu 331998f90dbaSZhi Yong Wu return true; 332098f90dbaSZhi Yong Wu } 332198f90dbaSZhi Yong Wu 332298f90dbaSZhi Yong Wu if (wait) { 332398f90dbaSZhi Yong Wu *wait = 0; 332498f90dbaSZhi Yong Wu } 332598f90dbaSZhi Yong Wu 332698f90dbaSZhi Yong Wu return false; 332798f90dbaSZhi Yong Wu } 332883f64091Sbellard 332983f64091Sbellard /**************************************************************/ 333083f64091Sbellard /* async block device emulation */ 333183f64091Sbellard 3332c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3333c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3334c16b5a2cSChristoph Hellwig QEMUBH *bh; 3335c16b5a2cSChristoph Hellwig int ret; 3336c16b5a2cSChristoph Hellwig /* vector translation state */ 3337c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3338c16b5a2cSChristoph Hellwig uint8_t *bounce; 3339c16b5a2cSChristoph Hellwig int is_write; 3340c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3341c16b5a2cSChristoph Hellwig 3342c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3343c16b5a2cSChristoph Hellwig { 3344b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3345b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 33466a7ad299SDor Laor qemu_bh_delete(acb->bh); 334736afc451SAvi Kivity acb->bh = NULL; 3348c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3349c16b5a2cSChristoph Hellwig } 3350c16b5a2cSChristoph Hellwig 3351c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 3352c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3353c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3354c16b5a2cSChristoph Hellwig }; 3355c16b5a2cSChristoph Hellwig 335683f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3357beac80cdSbellard { 3358ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3359f141eafeSaliguori 3360f141eafeSaliguori if (!acb->is_write) 3361f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 3362ceb42de8Saliguori qemu_vfree(acb->bounce); 3363ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 33646a7ad299SDor Laor qemu_bh_delete(acb->bh); 336536afc451SAvi Kivity acb->bh = NULL; 3366ce1a14dcSpbrook qemu_aio_release(acb); 3367beac80cdSbellard } 3368beac80cdSbellard 3369f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3370f141eafeSaliguori int64_t sector_num, 3371f141eafeSaliguori QEMUIOVector *qiov, 3372f141eafeSaliguori int nb_sectors, 3373f141eafeSaliguori BlockDriverCompletionFunc *cb, 3374f141eafeSaliguori void *opaque, 3375f141eafeSaliguori int is_write) 3376f141eafeSaliguori 3377ea2384d3Sbellard { 3378ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 337983f64091Sbellard 3380c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 3381f141eafeSaliguori acb->is_write = is_write; 3382f141eafeSaliguori acb->qiov = qiov; 3383e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3384ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3385f141eafeSaliguori 3386f141eafeSaliguori if (is_write) { 3387f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 33881ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3389f141eafeSaliguori } else { 33901ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3391f141eafeSaliguori } 3392f141eafeSaliguori 3393ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3394f141eafeSaliguori 3395ce1a14dcSpbrook return &acb->common; 33967a6cba61Spbrook } 33977a6cba61Spbrook 3398f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3399f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3400ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 340183f64091Sbellard { 3402f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 340383f64091Sbellard } 340483f64091Sbellard 3405f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3406f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3407f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3408f141eafeSaliguori { 3409f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3410f141eafeSaliguori } 3411f141eafeSaliguori 341268485420SKevin Wolf 341368485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 341468485420SKevin Wolf BlockDriverAIOCB common; 341568485420SKevin Wolf BlockRequest req; 341668485420SKevin Wolf bool is_write; 341768485420SKevin Wolf QEMUBH* bh; 341868485420SKevin Wolf } BlockDriverAIOCBCoroutine; 341968485420SKevin Wolf 342068485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 342168485420SKevin Wolf { 342268485420SKevin Wolf qemu_aio_flush(); 342368485420SKevin Wolf } 342468485420SKevin Wolf 342568485420SKevin Wolf static AIOPool bdrv_em_co_aio_pool = { 342668485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 342768485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 342868485420SKevin Wolf }; 342968485420SKevin Wolf 343035246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 343168485420SKevin Wolf { 343268485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 343368485420SKevin Wolf 343468485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 343568485420SKevin Wolf qemu_bh_delete(acb->bh); 343668485420SKevin Wolf qemu_aio_release(acb); 343768485420SKevin Wolf } 343868485420SKevin Wolf 3439b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3440b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3441b2a61371SStefan Hajnoczi { 3442b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3443b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3444b2a61371SStefan Hajnoczi 3445b2a61371SStefan Hajnoczi if (!acb->is_write) { 3446b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 3447470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3448b2a61371SStefan Hajnoczi } else { 3449b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 3450f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3451b2a61371SStefan Hajnoczi } 3452b2a61371SStefan Hajnoczi 345335246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3454b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 3455b2a61371SStefan Hajnoczi } 3456b2a61371SStefan Hajnoczi 345768485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 345868485420SKevin Wolf int64_t sector_num, 345968485420SKevin Wolf QEMUIOVector *qiov, 346068485420SKevin Wolf int nb_sectors, 346168485420SKevin Wolf BlockDriverCompletionFunc *cb, 346268485420SKevin Wolf void *opaque, 34638c5873d6SStefan Hajnoczi bool is_write) 346468485420SKevin Wolf { 346568485420SKevin Wolf Coroutine *co; 346668485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 346768485420SKevin Wolf 346868485420SKevin Wolf acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 346968485420SKevin Wolf acb->req.sector = sector_num; 347068485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 347168485420SKevin Wolf acb->req.qiov = qiov; 347268485420SKevin Wolf acb->is_write = is_write; 347368485420SKevin Wolf 34748c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 347568485420SKevin Wolf qemu_coroutine_enter(co, acb); 347668485420SKevin Wolf 347768485420SKevin Wolf return &acb->common; 347868485420SKevin Wolf } 347968485420SKevin Wolf 348007f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 3481b2e12bc6SChristoph Hellwig { 348207f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 348307f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 3484b2e12bc6SChristoph Hellwig 348507f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 348607f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3487b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 3488b2e12bc6SChristoph Hellwig } 3489b2e12bc6SChristoph Hellwig 349007f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 3491016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 3492016f5cf6SAlexander Graf { 349307f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 3494016f5cf6SAlexander Graf 349507f07615SPaolo Bonzini Coroutine *co; 349607f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 3497016f5cf6SAlexander Graf 349807f07615SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 349907f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 350007f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 3501016f5cf6SAlexander Graf 3502016f5cf6SAlexander Graf return &acb->common; 3503016f5cf6SAlexander Graf } 3504016f5cf6SAlexander Graf 35054265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 35064265d620SPaolo Bonzini { 35074265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 35084265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 35094265d620SPaolo Bonzini 35104265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 35114265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 35124265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 35134265d620SPaolo Bonzini } 35144265d620SPaolo Bonzini 35154265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 35164265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 35174265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 35184265d620SPaolo Bonzini { 35194265d620SPaolo Bonzini Coroutine *co; 35204265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 35214265d620SPaolo Bonzini 35224265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 35234265d620SPaolo Bonzini 35244265d620SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 35254265d620SPaolo Bonzini acb->req.sector = sector_num; 35264265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 35274265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 35284265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 35294265d620SPaolo Bonzini 35304265d620SPaolo Bonzini return &acb->common; 35314265d620SPaolo Bonzini } 35324265d620SPaolo Bonzini 3533ea2384d3Sbellard void bdrv_init(void) 3534ea2384d3Sbellard { 35355efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3536ea2384d3Sbellard } 3537ce1a14dcSpbrook 3538eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3539eb852011SMarkus Armbruster { 3540eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3541eb852011SMarkus Armbruster bdrv_init(); 3542eb852011SMarkus Armbruster } 3543eb852011SMarkus Armbruster 3544c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 35456bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 35466bbff9a0Saliguori { 3547ce1a14dcSpbrook BlockDriverAIOCB *acb; 3548ce1a14dcSpbrook 35496bbff9a0Saliguori if (pool->free_aiocb) { 35506bbff9a0Saliguori acb = pool->free_aiocb; 35516bbff9a0Saliguori pool->free_aiocb = acb->next; 3552ce1a14dcSpbrook } else { 35537267c094SAnthony Liguori acb = g_malloc0(pool->aiocb_size); 35546bbff9a0Saliguori acb->pool = pool; 3555ce1a14dcSpbrook } 3556ce1a14dcSpbrook acb->bs = bs; 3557ce1a14dcSpbrook acb->cb = cb; 3558ce1a14dcSpbrook acb->opaque = opaque; 3559ce1a14dcSpbrook return acb; 3560ce1a14dcSpbrook } 3561ce1a14dcSpbrook 3562ce1a14dcSpbrook void qemu_aio_release(void *p) 3563ce1a14dcSpbrook { 35646bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 35656bbff9a0Saliguori AIOPool *pool = acb->pool; 35666bbff9a0Saliguori acb->next = pool->free_aiocb; 35676bbff9a0Saliguori pool->free_aiocb = acb; 3568ce1a14dcSpbrook } 356919cb3738Sbellard 357019cb3738Sbellard /**************************************************************/ 3571f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 3572f9f05dc5SKevin Wolf 3573f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 3574f9f05dc5SKevin Wolf Coroutine *coroutine; 3575f9f05dc5SKevin Wolf int ret; 3576f9f05dc5SKevin Wolf } CoroutineIOCompletion; 3577f9f05dc5SKevin Wolf 3578f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 3579f9f05dc5SKevin Wolf { 3580f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 3581f9f05dc5SKevin Wolf 3582f9f05dc5SKevin Wolf co->ret = ret; 3583f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 3584f9f05dc5SKevin Wolf } 3585f9f05dc5SKevin Wolf 3586f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 3587f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 3588f9f05dc5SKevin Wolf bool is_write) 3589f9f05dc5SKevin Wolf { 3590f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 3591f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 3592f9f05dc5SKevin Wolf }; 3593f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 3594f9f05dc5SKevin Wolf 3595f9f05dc5SKevin Wolf if (is_write) { 3596a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 3597f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3598f9f05dc5SKevin Wolf } else { 3599a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 3600f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3601f9f05dc5SKevin Wolf } 3602f9f05dc5SKevin Wolf 360359370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 3604f9f05dc5SKevin Wolf if (!acb) { 3605f9f05dc5SKevin Wolf return -EIO; 3606f9f05dc5SKevin Wolf } 3607f9f05dc5SKevin Wolf qemu_coroutine_yield(); 3608f9f05dc5SKevin Wolf 3609f9f05dc5SKevin Wolf return co.ret; 3610f9f05dc5SKevin Wolf } 3611f9f05dc5SKevin Wolf 3612f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 3613f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3614f9f05dc5SKevin Wolf QEMUIOVector *iov) 3615f9f05dc5SKevin Wolf { 3616f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 3617f9f05dc5SKevin Wolf } 3618f9f05dc5SKevin Wolf 3619f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 3620f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3621f9f05dc5SKevin Wolf QEMUIOVector *iov) 3622f9f05dc5SKevin Wolf { 3623f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 3624f9f05dc5SKevin Wolf } 3625f9f05dc5SKevin Wolf 362607f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 3627e7a8a783SKevin Wolf { 362807f07615SPaolo Bonzini RwCo *rwco = opaque; 362907f07615SPaolo Bonzini 363007f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 363107f07615SPaolo Bonzini } 363207f07615SPaolo Bonzini 363307f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 363407f07615SPaolo Bonzini { 3635eb489bb1SKevin Wolf int ret; 3636eb489bb1SKevin Wolf 363729cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 363807f07615SPaolo Bonzini return 0; 3639eb489bb1SKevin Wolf } 3640eb489bb1SKevin Wolf 3641ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 3642eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 3643eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 3644eb489bb1SKevin Wolf if (ret < 0) { 3645eb489bb1SKevin Wolf return ret; 3646eb489bb1SKevin Wolf } 3647eb489bb1SKevin Wolf } 3648eb489bb1SKevin Wolf 3649ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 3650ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 3651ca716364SKevin Wolf return 0; 3652ca716364SKevin Wolf } 3653ca716364SKevin Wolf 3654eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 365529cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 365607f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 365707f07615SPaolo Bonzini BlockDriverAIOCB *acb; 3658e7a8a783SKevin Wolf CoroutineIOCompletion co = { 3659e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 3660e7a8a783SKevin Wolf }; 3661e7a8a783SKevin Wolf 366207f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 366307f07615SPaolo Bonzini if (acb == NULL) { 366429cdb251SPaolo Bonzini ret = -EIO; 366507f07615SPaolo Bonzini } else { 3666e7a8a783SKevin Wolf qemu_coroutine_yield(); 366729cdb251SPaolo Bonzini ret = co.ret; 3668e7a8a783SKevin Wolf } 366907f07615SPaolo Bonzini } else { 367007f07615SPaolo Bonzini /* 367107f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 367207f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 367307f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 367407f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 367507f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 367607f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 367707f07615SPaolo Bonzini * mode. 367807f07615SPaolo Bonzini * 367907f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 368007f07615SPaolo Bonzini */ 368129cdb251SPaolo Bonzini ret = 0; 368207f07615SPaolo Bonzini } 368329cdb251SPaolo Bonzini if (ret < 0) { 368429cdb251SPaolo Bonzini return ret; 368529cdb251SPaolo Bonzini } 368629cdb251SPaolo Bonzini 368729cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 368829cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 368929cdb251SPaolo Bonzini */ 369029cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 369107f07615SPaolo Bonzini } 369207f07615SPaolo Bonzini 36930f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 36940f15423cSAnthony Liguori { 36950f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 36960f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 36970f15423cSAnthony Liguori } 36980f15423cSAnthony Liguori } 36990f15423cSAnthony Liguori 37000f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 37010f15423cSAnthony Liguori { 37020f15423cSAnthony Liguori BlockDriverState *bs; 37030f15423cSAnthony Liguori 37040f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 37050f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 37060f15423cSAnthony Liguori } 37070f15423cSAnthony Liguori } 37080f15423cSAnthony Liguori 370907789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 371007789269SBenoît Canet { 371107789269SBenoît Canet BlockDriverState *bs; 371207789269SBenoît Canet 371307789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 371407789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 371507789269SBenoît Canet } 371607789269SBenoît Canet } 371707789269SBenoît Canet 371807f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 371907f07615SPaolo Bonzini { 372007f07615SPaolo Bonzini Coroutine *co; 372107f07615SPaolo Bonzini RwCo rwco = { 372207f07615SPaolo Bonzini .bs = bs, 372307f07615SPaolo Bonzini .ret = NOT_DONE, 372407f07615SPaolo Bonzini }; 372507f07615SPaolo Bonzini 372607f07615SPaolo Bonzini if (qemu_in_coroutine()) { 372707f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 372807f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 372907f07615SPaolo Bonzini } else { 373007f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 373107f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 373207f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 373307f07615SPaolo Bonzini qemu_aio_wait(); 373407f07615SPaolo Bonzini } 373507f07615SPaolo Bonzini } 373607f07615SPaolo Bonzini 373707f07615SPaolo Bonzini return rwco.ret; 373807f07615SPaolo Bonzini } 3739e7a8a783SKevin Wolf 37404265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 37414265d620SPaolo Bonzini { 37424265d620SPaolo Bonzini RwCo *rwco = opaque; 37434265d620SPaolo Bonzini 37444265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 37454265d620SPaolo Bonzini } 37464265d620SPaolo Bonzini 37474265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 37484265d620SPaolo Bonzini int nb_sectors) 37494265d620SPaolo Bonzini { 37504265d620SPaolo Bonzini if (!bs->drv) { 37514265d620SPaolo Bonzini return -ENOMEDIUM; 37524265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 37534265d620SPaolo Bonzini return -EIO; 37544265d620SPaolo Bonzini } else if (bs->read_only) { 37554265d620SPaolo Bonzini return -EROFS; 37564265d620SPaolo Bonzini } else if (bs->drv->bdrv_co_discard) { 37574265d620SPaolo Bonzini return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors); 37584265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 37594265d620SPaolo Bonzini BlockDriverAIOCB *acb; 37604265d620SPaolo Bonzini CoroutineIOCompletion co = { 37614265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 37624265d620SPaolo Bonzini }; 37634265d620SPaolo Bonzini 37644265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 37654265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 37664265d620SPaolo Bonzini if (acb == NULL) { 37674265d620SPaolo Bonzini return -EIO; 37684265d620SPaolo Bonzini } else { 37694265d620SPaolo Bonzini qemu_coroutine_yield(); 37704265d620SPaolo Bonzini return co.ret; 37714265d620SPaolo Bonzini } 37724265d620SPaolo Bonzini } else { 37734265d620SPaolo Bonzini return 0; 37744265d620SPaolo Bonzini } 37754265d620SPaolo Bonzini } 37764265d620SPaolo Bonzini 37774265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 37784265d620SPaolo Bonzini { 37794265d620SPaolo Bonzini Coroutine *co; 37804265d620SPaolo Bonzini RwCo rwco = { 37814265d620SPaolo Bonzini .bs = bs, 37824265d620SPaolo Bonzini .sector_num = sector_num, 37834265d620SPaolo Bonzini .nb_sectors = nb_sectors, 37844265d620SPaolo Bonzini .ret = NOT_DONE, 37854265d620SPaolo Bonzini }; 37864265d620SPaolo Bonzini 37874265d620SPaolo Bonzini if (qemu_in_coroutine()) { 37884265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 37894265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 37904265d620SPaolo Bonzini } else { 37914265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 37924265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 37934265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 37944265d620SPaolo Bonzini qemu_aio_wait(); 37954265d620SPaolo Bonzini } 37964265d620SPaolo Bonzini } 37974265d620SPaolo Bonzini 37984265d620SPaolo Bonzini return rwco.ret; 37994265d620SPaolo Bonzini } 38004265d620SPaolo Bonzini 3801f9f05dc5SKevin Wolf /**************************************************************/ 380219cb3738Sbellard /* removable device support */ 380319cb3738Sbellard 380419cb3738Sbellard /** 380519cb3738Sbellard * Return TRUE if the media is present 380619cb3738Sbellard */ 380719cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 380819cb3738Sbellard { 380919cb3738Sbellard BlockDriver *drv = bs->drv; 3810a1aff5bfSMarkus Armbruster 381119cb3738Sbellard if (!drv) 381219cb3738Sbellard return 0; 381319cb3738Sbellard if (!drv->bdrv_is_inserted) 3814a1aff5bfSMarkus Armbruster return 1; 3815a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 381619cb3738Sbellard } 381719cb3738Sbellard 381819cb3738Sbellard /** 38198e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 38208e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 382119cb3738Sbellard */ 382219cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 382319cb3738Sbellard { 382419cb3738Sbellard BlockDriver *drv = bs->drv; 382519cb3738Sbellard 38268e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 38278e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 38288e49ca46SMarkus Armbruster } 38298e49ca46SMarkus Armbruster return -ENOTSUP; 383019cb3738Sbellard } 383119cb3738Sbellard 383219cb3738Sbellard /** 383319cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 383419cb3738Sbellard */ 3835f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 383619cb3738Sbellard { 383719cb3738Sbellard BlockDriver *drv = bs->drv; 383819cb3738Sbellard 3839822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 3840822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 384119cb3738Sbellard } 38426f382ed2SLuiz Capitulino 38436f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 38446f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 38456f382ed2SLuiz Capitulino } 384619cb3738Sbellard } 384719cb3738Sbellard 384819cb3738Sbellard /** 384919cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 385019cb3738Sbellard * to eject it manually). 385119cb3738Sbellard */ 3852025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 385319cb3738Sbellard { 385419cb3738Sbellard BlockDriver *drv = bs->drv; 385519cb3738Sbellard 3856025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 3857b8c6d095SStefan Hajnoczi 3858025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 3859025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 386019cb3738Sbellard } 386119cb3738Sbellard } 3862985a03b0Sths 3863985a03b0Sths /* needed for generic scsi interface */ 3864985a03b0Sths 3865985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 3866985a03b0Sths { 3867985a03b0Sths BlockDriver *drv = bs->drv; 3868985a03b0Sths 3869985a03b0Sths if (drv && drv->bdrv_ioctl) 3870985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 3871985a03b0Sths return -ENOTSUP; 3872985a03b0Sths } 38737d780669Saliguori 3874221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 3875221f715dSaliguori unsigned long int req, void *buf, 38767d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 38777d780669Saliguori { 3878221f715dSaliguori BlockDriver *drv = bs->drv; 38797d780669Saliguori 3880221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 3881221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 3882221f715dSaliguori return NULL; 38837d780669Saliguori } 3884e268ca52Saliguori 38857b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 38867b6f9300SMarkus Armbruster { 38877b6f9300SMarkus Armbruster bs->buffer_alignment = align; 38887b6f9300SMarkus Armbruster } 38897cd1e32aSlirans@il.ibm.com 3890e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 3891e268ca52Saliguori { 3892e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 3893e268ca52Saliguori } 38947cd1e32aSlirans@il.ibm.com 38957cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 38967cd1e32aSlirans@il.ibm.com { 38977cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 3898a55eb92cSJan Kiszka 3899aaa0eb75SLiran Schour bs->dirty_count = 0; 39007cd1e32aSlirans@il.ibm.com if (enable) { 3901c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 3902c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 3903c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 3904c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 39057cd1e32aSlirans@il.ibm.com 39067267c094SAnthony Liguori bs->dirty_bitmap = g_malloc0(bitmap_size); 39077cd1e32aSlirans@il.ibm.com } 39087cd1e32aSlirans@il.ibm.com } else { 3909c6d22830SJan Kiszka if (bs->dirty_bitmap) { 39107267c094SAnthony Liguori g_free(bs->dirty_bitmap); 3911c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 39127cd1e32aSlirans@il.ibm.com } 39137cd1e32aSlirans@il.ibm.com } 39147cd1e32aSlirans@il.ibm.com } 39157cd1e32aSlirans@il.ibm.com 39167cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 39177cd1e32aSlirans@il.ibm.com { 39186ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 39197cd1e32aSlirans@il.ibm.com 3920c6d22830SJan Kiszka if (bs->dirty_bitmap && 3921c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 39226d59fec1SMarcelo Tosatti return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 39236d59fec1SMarcelo Tosatti (1UL << (chunk % (sizeof(unsigned long) * 8)))); 39247cd1e32aSlirans@il.ibm.com } else { 39257cd1e32aSlirans@il.ibm.com return 0; 39267cd1e32aSlirans@il.ibm.com } 39277cd1e32aSlirans@il.ibm.com } 39287cd1e32aSlirans@il.ibm.com 39297cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 39307cd1e32aSlirans@il.ibm.com int nr_sectors) 39317cd1e32aSlirans@il.ibm.com { 39327cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 39337cd1e32aSlirans@il.ibm.com } 3934aaa0eb75SLiran Schour 3935aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 3936aaa0eb75SLiran Schour { 3937aaa0eb75SLiran Schour return bs->dirty_count; 3938aaa0eb75SLiran Schour } 3939f88e1a42SJes Sorensen 3940db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 3941db593f25SMarcelo Tosatti { 3942db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 3943db593f25SMarcelo Tosatti bs->in_use = in_use; 3944db593f25SMarcelo Tosatti } 3945db593f25SMarcelo Tosatti 3946db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 3947db593f25SMarcelo Tosatti { 3948db593f25SMarcelo Tosatti return bs->in_use; 3949db593f25SMarcelo Tosatti } 3950db593f25SMarcelo Tosatti 395128a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 395228a7282aSLuiz Capitulino { 3953d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 395458e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 395528a7282aSLuiz Capitulino } 395628a7282aSLuiz Capitulino 395728a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 395828a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 395928a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 396028a7282aSLuiz Capitulino { 3961d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 396228a7282aSLuiz Capitulino (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC || 396328a7282aSLuiz Capitulino bs->on_write_error == BLOCK_ERR_STOP_ANY || 396428a7282aSLuiz Capitulino bs->on_read_error == BLOCK_ERR_STOP_ANY)); 396528a7282aSLuiz Capitulino } 396628a7282aSLuiz Capitulino 396728a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 396828a7282aSLuiz Capitulino { 3969d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 397028a7282aSLuiz Capitulino } 397128a7282aSLuiz Capitulino 397228a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 397328a7282aSLuiz Capitulino { 397428a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 397558e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 397628a7282aSLuiz Capitulino } 397728a7282aSLuiz Capitulino } 397828a7282aSLuiz Capitulino 397928a7282aSLuiz Capitulino /* XXX: Today this is set by device models because it makes the implementation 398028a7282aSLuiz Capitulino quite simple. However, the block layer knows about the error, so it's 398128a7282aSLuiz Capitulino possible to implement this without device models being involved */ 398228a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 398328a7282aSLuiz Capitulino { 398458e21ef5SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs) && 398558e21ef5SLuiz Capitulino bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 398628a7282aSLuiz Capitulino assert(error >= 0); 398758e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 398858e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 398928a7282aSLuiz Capitulino } 399028a7282aSLuiz Capitulino } 399128a7282aSLuiz Capitulino 3992a597e79cSChristoph Hellwig void 3993a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 3994a597e79cSChristoph Hellwig enum BlockAcctType type) 3995a597e79cSChristoph Hellwig { 3996a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 3997a597e79cSChristoph Hellwig 3998a597e79cSChristoph Hellwig cookie->bytes = bytes; 3999c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4000a597e79cSChristoph Hellwig cookie->type = type; 4001a597e79cSChristoph Hellwig } 4002a597e79cSChristoph Hellwig 4003a597e79cSChristoph Hellwig void 4004a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4005a597e79cSChristoph Hellwig { 4006a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4007a597e79cSChristoph Hellwig 4008a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4009a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4010c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4011a597e79cSChristoph Hellwig } 4012a597e79cSChristoph Hellwig 4013f88e1a42SJes Sorensen int bdrv_img_create(const char *filename, const char *fmt, 4014f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 4015f88e1a42SJes Sorensen char *options, uint64_t img_size, int flags) 4016f88e1a42SJes Sorensen { 4017f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4018d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4019f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 4020f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 402196df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4022f88e1a42SJes Sorensen int ret = 0; 4023f88e1a42SJes Sorensen 4024f88e1a42SJes Sorensen /* Find driver and parse its options */ 4025f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4026f88e1a42SJes Sorensen if (!drv) { 4027f88e1a42SJes Sorensen error_report("Unknown file format '%s'", fmt); 40284f70f249SJes Sorensen ret = -EINVAL; 4029f88e1a42SJes Sorensen goto out; 4030f88e1a42SJes Sorensen } 4031f88e1a42SJes Sorensen 4032f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 4033f88e1a42SJes Sorensen if (!proto_drv) { 4034f88e1a42SJes Sorensen error_report("Unknown protocol '%s'", filename); 40354f70f249SJes Sorensen ret = -EINVAL; 4036f88e1a42SJes Sorensen goto out; 4037f88e1a42SJes Sorensen } 4038f88e1a42SJes Sorensen 4039f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4040f88e1a42SJes Sorensen drv->create_options); 4041f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4042f88e1a42SJes Sorensen proto_drv->create_options); 4043f88e1a42SJes Sorensen 4044f88e1a42SJes Sorensen /* Create parameter list with default values */ 4045f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4046f88e1a42SJes Sorensen 4047f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4048f88e1a42SJes Sorensen 4049f88e1a42SJes Sorensen /* Parse -o options */ 4050f88e1a42SJes Sorensen if (options) { 4051f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4052f88e1a42SJes Sorensen if (param == NULL) { 4053f88e1a42SJes Sorensen error_report("Invalid options for file format '%s'.", fmt); 40544f70f249SJes Sorensen ret = -EINVAL; 4055f88e1a42SJes Sorensen goto out; 4056f88e1a42SJes Sorensen } 4057f88e1a42SJes Sorensen } 4058f88e1a42SJes Sorensen 4059f88e1a42SJes Sorensen if (base_filename) { 4060f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4061f88e1a42SJes Sorensen base_filename)) { 4062f88e1a42SJes Sorensen error_report("Backing file not supported for file format '%s'", 4063f88e1a42SJes Sorensen fmt); 40644f70f249SJes Sorensen ret = -EINVAL; 4065f88e1a42SJes Sorensen goto out; 4066f88e1a42SJes Sorensen } 4067f88e1a42SJes Sorensen } 4068f88e1a42SJes Sorensen 4069f88e1a42SJes Sorensen if (base_fmt) { 4070f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 4071f88e1a42SJes Sorensen error_report("Backing file format not supported for file " 4072f88e1a42SJes Sorensen "format '%s'", fmt); 40734f70f249SJes Sorensen ret = -EINVAL; 4074f88e1a42SJes Sorensen goto out; 4075f88e1a42SJes Sorensen } 4076f88e1a42SJes Sorensen } 4077f88e1a42SJes Sorensen 4078792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4079792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4080792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 4081792da93aSJes Sorensen error_report("Error: Trying to create an image with the " 4082792da93aSJes Sorensen "same filename as the backing file"); 40834f70f249SJes Sorensen ret = -EINVAL; 4084792da93aSJes Sorensen goto out; 4085792da93aSJes Sorensen } 4086792da93aSJes Sorensen } 4087792da93aSJes Sorensen 4088f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4089f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 409096df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 409196df67d1SStefan Hajnoczi if (!backing_drv) { 4092f88e1a42SJes Sorensen error_report("Unknown backing file format '%s'", 4093f88e1a42SJes Sorensen backing_fmt->value.s); 40944f70f249SJes Sorensen ret = -EINVAL; 4095f88e1a42SJes Sorensen goto out; 4096f88e1a42SJes Sorensen } 4097f88e1a42SJes Sorensen } 4098f88e1a42SJes Sorensen 4099f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4100f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4101d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4102d220894eSKevin Wolf if (size && size->value.n == -1) { 4103f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4104f88e1a42SJes Sorensen uint64_t size; 4105f88e1a42SJes Sorensen char buf[32]; 4106f88e1a42SJes Sorensen 4107f88e1a42SJes Sorensen bs = bdrv_new(""); 4108f88e1a42SJes Sorensen 410996df67d1SStefan Hajnoczi ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); 4110f88e1a42SJes Sorensen if (ret < 0) { 411196df67d1SStefan Hajnoczi error_report("Could not open '%s'", backing_file->value.s); 4112f88e1a42SJes Sorensen goto out; 4113f88e1a42SJes Sorensen } 4114f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4115f88e1a42SJes Sorensen size *= 512; 4116f88e1a42SJes Sorensen 4117f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4118f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4119f88e1a42SJes Sorensen } else { 4120f88e1a42SJes Sorensen error_report("Image creation needs a size parameter"); 41214f70f249SJes Sorensen ret = -EINVAL; 4122f88e1a42SJes Sorensen goto out; 4123f88e1a42SJes Sorensen } 4124f88e1a42SJes Sorensen } 4125f88e1a42SJes Sorensen 4126f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4127f88e1a42SJes Sorensen print_option_parameters(param); 4128f88e1a42SJes Sorensen puts(""); 4129f88e1a42SJes Sorensen 4130f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 4131f88e1a42SJes Sorensen 4132f88e1a42SJes Sorensen if (ret < 0) { 4133f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 4134f88e1a42SJes Sorensen error_report("Formatting or formatting option not supported for " 4135f88e1a42SJes Sorensen "file format '%s'", fmt); 4136f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 4137f88e1a42SJes Sorensen error_report("The image size is too large for file format '%s'", 4138f88e1a42SJes Sorensen fmt); 4139f88e1a42SJes Sorensen } else { 4140f88e1a42SJes Sorensen error_report("%s: error while creating %s: %s", filename, fmt, 4141f88e1a42SJes Sorensen strerror(-ret)); 4142f88e1a42SJes Sorensen } 4143f88e1a42SJes Sorensen } 4144f88e1a42SJes Sorensen 4145f88e1a42SJes Sorensen out: 4146f88e1a42SJes Sorensen free_option_parameters(create_options); 4147f88e1a42SJes Sorensen free_option_parameters(param); 4148f88e1a42SJes Sorensen 4149f88e1a42SJes Sorensen if (bs) { 4150f88e1a42SJes Sorensen bdrv_delete(bs); 4151f88e1a42SJes Sorensen } 41524f70f249SJes Sorensen 41534f70f249SJes Sorensen return ret; 4154f88e1a42SJes Sorensen } 4155eeec61f2SStefan Hajnoczi 4156eeec61f2SStefan Hajnoczi void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs, 4157c83c66c3SStefan Hajnoczi int64_t speed, BlockDriverCompletionFunc *cb, 4158c83c66c3SStefan Hajnoczi void *opaque, Error **errp) 4159eeec61f2SStefan Hajnoczi { 4160eeec61f2SStefan Hajnoczi BlockJob *job; 4161eeec61f2SStefan Hajnoczi 4162eeec61f2SStefan Hajnoczi if (bs->job || bdrv_in_use(bs)) { 4163fd7f8c65SStefan Hajnoczi error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); 4164eeec61f2SStefan Hajnoczi return NULL; 4165eeec61f2SStefan Hajnoczi } 4166eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 1); 4167eeec61f2SStefan Hajnoczi 4168eeec61f2SStefan Hajnoczi job = g_malloc0(job_type->instance_size); 4169eeec61f2SStefan Hajnoczi job->job_type = job_type; 4170eeec61f2SStefan Hajnoczi job->bs = bs; 4171eeec61f2SStefan Hajnoczi job->cb = cb; 4172eeec61f2SStefan Hajnoczi job->opaque = opaque; 4173eeec61f2SStefan Hajnoczi bs->job = job; 4174c83c66c3SStefan Hajnoczi 4175c83c66c3SStefan Hajnoczi /* Only set speed when necessary to avoid NotSupported error */ 4176c83c66c3SStefan Hajnoczi if (speed != 0) { 4177c83c66c3SStefan Hajnoczi Error *local_err = NULL; 4178c83c66c3SStefan Hajnoczi 4179c83c66c3SStefan Hajnoczi block_job_set_speed(job, speed, &local_err); 4180c83c66c3SStefan Hajnoczi if (error_is_set(&local_err)) { 4181c83c66c3SStefan Hajnoczi bs->job = NULL; 4182c83c66c3SStefan Hajnoczi g_free(job); 4183c83c66c3SStefan Hajnoczi bdrv_set_in_use(bs, 0); 4184c83c66c3SStefan Hajnoczi error_propagate(errp, local_err); 4185c83c66c3SStefan Hajnoczi return NULL; 4186c83c66c3SStefan Hajnoczi } 4187c83c66c3SStefan Hajnoczi } 4188eeec61f2SStefan Hajnoczi return job; 4189eeec61f2SStefan Hajnoczi } 4190eeec61f2SStefan Hajnoczi 4191eeec61f2SStefan Hajnoczi void block_job_complete(BlockJob *job, int ret) 4192eeec61f2SStefan Hajnoczi { 4193eeec61f2SStefan Hajnoczi BlockDriverState *bs = job->bs; 4194eeec61f2SStefan Hajnoczi 4195eeec61f2SStefan Hajnoczi assert(bs->job == job); 4196eeec61f2SStefan Hajnoczi job->cb(job->opaque, ret); 4197eeec61f2SStefan Hajnoczi bs->job = NULL; 4198eeec61f2SStefan Hajnoczi g_free(job); 4199eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 0); 4200eeec61f2SStefan Hajnoczi } 4201eeec61f2SStefan Hajnoczi 4202882ec7ceSStefan Hajnoczi void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) 4203eeec61f2SStefan Hajnoczi { 42049e6636c7SStefan Hajnoczi Error *local_err = NULL; 42059f25ecccSPaolo Bonzini 4206eeec61f2SStefan Hajnoczi if (!job->job_type->set_speed) { 42079e6636c7SStefan Hajnoczi error_set(errp, QERR_NOT_SUPPORTED); 42089e6636c7SStefan Hajnoczi return; 4209eeec61f2SStefan Hajnoczi } 4210882ec7ceSStefan Hajnoczi job->job_type->set_speed(job, speed, &local_err); 42119e6636c7SStefan Hajnoczi if (error_is_set(&local_err)) { 42129e6636c7SStefan Hajnoczi error_propagate(errp, local_err); 42139e6636c7SStefan Hajnoczi return; 42149e6636c7SStefan Hajnoczi } 42159e6636c7SStefan Hajnoczi 4216882ec7ceSStefan Hajnoczi job->speed = speed; 42179f25ecccSPaolo Bonzini } 4218eeec61f2SStefan Hajnoczi 4219eeec61f2SStefan Hajnoczi void block_job_cancel(BlockJob *job) 4220eeec61f2SStefan Hajnoczi { 4221eeec61f2SStefan Hajnoczi job->cancelled = true; 4222eeec61f2SStefan Hajnoczi } 4223eeec61f2SStefan Hajnoczi 4224eeec61f2SStefan Hajnoczi bool block_job_is_cancelled(BlockJob *job) 4225eeec61f2SStefan Hajnoczi { 4226eeec61f2SStefan Hajnoczi return job->cancelled; 4227eeec61f2SStefan Hajnoczi } 42283e914655SPaolo Bonzini 42293e914655SPaolo Bonzini void block_job_cancel_sync(BlockJob *job) 42303e914655SPaolo Bonzini { 42313e914655SPaolo Bonzini BlockDriverState *bs = job->bs; 42323e914655SPaolo Bonzini 42333e914655SPaolo Bonzini assert(bs->job == job); 42343e914655SPaolo Bonzini block_job_cancel(job); 42353e914655SPaolo Bonzini while (bs->job != NULL && bs->job->busy) { 42363e914655SPaolo Bonzini qemu_aio_wait(); 42373e914655SPaolo Bonzini } 42383e914655SPaolo Bonzini } 4239