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 { 201*947995c0SPaolo Bonzini const char *p; 202*947995c0SPaolo Bonzini 2039e0b22f4SStefan Hajnoczi #ifdef _WIN32 2049e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2059e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2069e0b22f4SStefan Hajnoczi return 0; 2079e0b22f4SStefan Hajnoczi } 208*947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 209*947995c0SPaolo Bonzini #else 210*947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2119e0b22f4SStefan Hajnoczi #endif 2129e0b22f4SStefan Hajnoczi 213*947995c0SPaolo Bonzini return *p == ':'; 2149e0b22f4SStefan Hajnoczi } 2159e0b22f4SStefan Hajnoczi 21683f64091Sbellard int path_is_absolute(const char *path) 21783f64091Sbellard { 21821664424Sbellard #ifdef _WIN32 21921664424Sbellard /* specific case for names like: "\\.\d:" */ 220f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 22121664424Sbellard return 1; 222f53f4da9SPaolo Bonzini } 223f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 2243b9f94e1Sbellard #else 225f53f4da9SPaolo Bonzini return (*path == '/'); 2263b9f94e1Sbellard #endif 22783f64091Sbellard } 22883f64091Sbellard 22983f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 23083f64091Sbellard path to it by considering it is relative to base_path. URL are 23183f64091Sbellard supported. */ 23283f64091Sbellard void path_combine(char *dest, int dest_size, 23383f64091Sbellard const char *base_path, 23483f64091Sbellard const char *filename) 23583f64091Sbellard { 23683f64091Sbellard const char *p, *p1; 23783f64091Sbellard int len; 23883f64091Sbellard 23983f64091Sbellard if (dest_size <= 0) 24083f64091Sbellard return; 24183f64091Sbellard if (path_is_absolute(filename)) { 24283f64091Sbellard pstrcpy(dest, dest_size, filename); 24383f64091Sbellard } else { 24483f64091Sbellard p = strchr(base_path, ':'); 24583f64091Sbellard if (p) 24683f64091Sbellard p++; 24783f64091Sbellard else 24883f64091Sbellard p = base_path; 2493b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2503b9f94e1Sbellard #ifdef _WIN32 2513b9f94e1Sbellard { 2523b9f94e1Sbellard const char *p2; 2533b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2543b9f94e1Sbellard if (!p1 || p2 > p1) 2553b9f94e1Sbellard p1 = p2; 2563b9f94e1Sbellard } 2573b9f94e1Sbellard #endif 25883f64091Sbellard if (p1) 25983f64091Sbellard p1++; 26083f64091Sbellard else 26183f64091Sbellard p1 = base_path; 26283f64091Sbellard if (p1 > p) 26383f64091Sbellard p = p1; 26483f64091Sbellard len = p - base_path; 26583f64091Sbellard if (len > dest_size - 1) 26683f64091Sbellard len = dest_size - 1; 26783f64091Sbellard memcpy(dest, base_path, len); 26883f64091Sbellard dest[len] = '\0'; 26983f64091Sbellard pstrcat(dest, dest_size, filename); 27083f64091Sbellard } 27183f64091Sbellard } 27283f64091Sbellard 2735efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 274ea2384d3Sbellard { 2758c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 2768c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 277f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 278f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 279f9f05dc5SKevin Wolf 280f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 281f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 282f8c35c1dSStefan Hajnoczi */ 283f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 28483f64091Sbellard /* add AIO emulation layer */ 285f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 286f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 28783f64091Sbellard } 288f9f05dc5SKevin Wolf } 289b2e12bc6SChristoph Hellwig 2908a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 291ea2384d3Sbellard } 292b338082bSbellard 293b338082bSbellard /* create a new block device (by default it is empty) */ 294b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 295fc01f7e7Sbellard { 2961b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 297b338082bSbellard 2987267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 299b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 300ea2384d3Sbellard if (device_name[0] != '\0') { 3011b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 302ea2384d3Sbellard } 30328a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 304b338082bSbellard return bs; 305b338082bSbellard } 306b338082bSbellard 307ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 308ea2384d3Sbellard { 309ea2384d3Sbellard BlockDriver *drv1; 3108a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3118a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 312ea2384d3Sbellard return drv1; 313ea2384d3Sbellard } 3148a22f02aSStefan Hajnoczi } 315ea2384d3Sbellard return NULL; 316ea2384d3Sbellard } 317ea2384d3Sbellard 318eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 319eb852011SMarkus Armbruster { 320eb852011SMarkus Armbruster static const char *whitelist[] = { 321eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 322eb852011SMarkus Armbruster }; 323eb852011SMarkus Armbruster const char **p; 324eb852011SMarkus Armbruster 325eb852011SMarkus Armbruster if (!whitelist[0]) 326eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 327eb852011SMarkus Armbruster 328eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 329eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 330eb852011SMarkus Armbruster return 1; 331eb852011SMarkus Armbruster } 332eb852011SMarkus Armbruster } 333eb852011SMarkus Armbruster return 0; 334eb852011SMarkus Armbruster } 335eb852011SMarkus Armbruster 336eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 337eb852011SMarkus Armbruster { 338eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 339eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 340eb852011SMarkus Armbruster } 341eb852011SMarkus Armbruster 3425b7e1542SZhi Yong Wu typedef struct CreateCo { 3435b7e1542SZhi Yong Wu BlockDriver *drv; 3445b7e1542SZhi Yong Wu char *filename; 3455b7e1542SZhi Yong Wu QEMUOptionParameter *options; 3465b7e1542SZhi Yong Wu int ret; 3475b7e1542SZhi Yong Wu } CreateCo; 3485b7e1542SZhi Yong Wu 3495b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 3505b7e1542SZhi Yong Wu { 3515b7e1542SZhi Yong Wu CreateCo *cco = opaque; 3525b7e1542SZhi Yong Wu assert(cco->drv); 3535b7e1542SZhi Yong Wu 3545b7e1542SZhi Yong Wu cco->ret = cco->drv->bdrv_create(cco->filename, cco->options); 3555b7e1542SZhi Yong Wu } 3565b7e1542SZhi Yong Wu 3570e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 3580e7e1989SKevin Wolf QEMUOptionParameter *options) 359ea2384d3Sbellard { 3605b7e1542SZhi Yong Wu int ret; 3610e7e1989SKevin Wolf 3625b7e1542SZhi Yong Wu Coroutine *co; 3635b7e1542SZhi Yong Wu CreateCo cco = { 3645b7e1542SZhi Yong Wu .drv = drv, 3655b7e1542SZhi Yong Wu .filename = g_strdup(filename), 3665b7e1542SZhi Yong Wu .options = options, 3675b7e1542SZhi Yong Wu .ret = NOT_DONE, 3685b7e1542SZhi Yong Wu }; 3695b7e1542SZhi Yong Wu 3705b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 3715b7e1542SZhi Yong Wu return -ENOTSUP; 3725b7e1542SZhi Yong Wu } 3735b7e1542SZhi Yong Wu 3745b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 3755b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 3765b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 3775b7e1542SZhi Yong Wu } else { 3785b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 3795b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 3805b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 3815b7e1542SZhi Yong Wu qemu_aio_wait(); 3825b7e1542SZhi Yong Wu } 3835b7e1542SZhi Yong Wu } 3845b7e1542SZhi Yong Wu 3855b7e1542SZhi Yong Wu ret = cco.ret; 3865b7e1542SZhi Yong Wu g_free(cco.filename); 3875b7e1542SZhi Yong Wu 3885b7e1542SZhi Yong Wu return ret; 389ea2384d3Sbellard } 390ea2384d3Sbellard 39184a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 39284a12e66SChristoph Hellwig { 39384a12e66SChristoph Hellwig BlockDriver *drv; 39484a12e66SChristoph Hellwig 395b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 39684a12e66SChristoph Hellwig if (drv == NULL) { 39716905d71SStefan Hajnoczi return -ENOENT; 39884a12e66SChristoph Hellwig } 39984a12e66SChristoph Hellwig 40084a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 40184a12e66SChristoph Hellwig } 40284a12e66SChristoph Hellwig 403d5249393Sbellard #ifdef _WIN32 40495389c86Sbellard void get_tmp_filename(char *filename, int size) 405d5249393Sbellard { 4063b9f94e1Sbellard char temp_dir[MAX_PATH]; 4073b9f94e1Sbellard 4083b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 4093b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 410d5249393Sbellard } 411d5249393Sbellard #else 41295389c86Sbellard void get_tmp_filename(char *filename, int size) 413ea2384d3Sbellard { 414ea2384d3Sbellard int fd; 4157ccfb2ebSblueswir1 const char *tmpdir; 416d5249393Sbellard /* XXX: race condition possible */ 4170badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 4180badc1eeSaurel32 if (!tmpdir) 4190badc1eeSaurel32 tmpdir = "/tmp"; 4200badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 421ea2384d3Sbellard fd = mkstemp(filename); 422ea2384d3Sbellard close(fd); 423ea2384d3Sbellard } 424d5249393Sbellard #endif 425ea2384d3Sbellard 426f3a5d3f8SChristoph Hellwig /* 427f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 428f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 429f3a5d3f8SChristoph Hellwig */ 430f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 431f3a5d3f8SChristoph Hellwig { 432508c7cb3SChristoph Hellwig int score_max = 0, score; 433508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 434f3a5d3f8SChristoph Hellwig 4358a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 436508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 437508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 438508c7cb3SChristoph Hellwig if (score > score_max) { 439508c7cb3SChristoph Hellwig score_max = score; 440508c7cb3SChristoph Hellwig drv = d; 441f3a5d3f8SChristoph Hellwig } 442508c7cb3SChristoph Hellwig } 443f3a5d3f8SChristoph Hellwig } 444f3a5d3f8SChristoph Hellwig 445508c7cb3SChristoph Hellwig return drv; 446f3a5d3f8SChristoph Hellwig } 447f3a5d3f8SChristoph Hellwig 448b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 44984a12e66SChristoph Hellwig { 45084a12e66SChristoph Hellwig BlockDriver *drv1; 45184a12e66SChristoph Hellwig char protocol[128]; 45284a12e66SChristoph Hellwig int len; 45384a12e66SChristoph Hellwig const char *p; 45484a12e66SChristoph Hellwig 45566f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 45666f82ceeSKevin Wolf 45739508e7aSChristoph Hellwig /* 45839508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 45939508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 46039508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 46139508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 46239508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 46339508e7aSChristoph Hellwig */ 46484a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 46539508e7aSChristoph Hellwig if (drv1) { 46684a12e66SChristoph Hellwig return drv1; 46784a12e66SChristoph Hellwig } 46839508e7aSChristoph Hellwig 4699e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 47039508e7aSChristoph Hellwig return bdrv_find_format("file"); 47139508e7aSChristoph Hellwig } 4729e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 4739e0b22f4SStefan Hajnoczi assert(p != NULL); 47484a12e66SChristoph Hellwig len = p - filename; 47584a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 47684a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 47784a12e66SChristoph Hellwig memcpy(protocol, filename, len); 47884a12e66SChristoph Hellwig protocol[len] = '\0'; 47984a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 48084a12e66SChristoph Hellwig if (drv1->protocol_name && 48184a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 48284a12e66SChristoph Hellwig return drv1; 48384a12e66SChristoph Hellwig } 48484a12e66SChristoph Hellwig } 48584a12e66SChristoph Hellwig return NULL; 48684a12e66SChristoph Hellwig } 48784a12e66SChristoph Hellwig 488c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv) 489ea2384d3Sbellard { 49083f64091Sbellard int ret, score, score_max; 491ea2384d3Sbellard BlockDriver *drv1, *drv; 49283f64091Sbellard uint8_t buf[2048]; 49383f64091Sbellard BlockDriverState *bs; 494ea2384d3Sbellard 495f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 496c98ac35dSStefan Weil if (ret < 0) { 497c98ac35dSStefan Weil *pdrv = NULL; 498c98ac35dSStefan Weil return ret; 499c98ac35dSStefan Weil } 500f8ea0b00SNicholas Bellinger 50108a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 50208a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 5031a396859SNicholas A. Bellinger bdrv_delete(bs); 504c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 505c98ac35dSStefan Weil if (!drv) { 506c98ac35dSStefan Weil ret = -ENOENT; 507c98ac35dSStefan Weil } 508c98ac35dSStefan Weil *pdrv = drv; 509c98ac35dSStefan Weil return ret; 5101a396859SNicholas A. Bellinger } 511f8ea0b00SNicholas Bellinger 51283f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 51383f64091Sbellard bdrv_delete(bs); 514ea2384d3Sbellard if (ret < 0) { 515c98ac35dSStefan Weil *pdrv = NULL; 516c98ac35dSStefan Weil return ret; 517ea2384d3Sbellard } 518ea2384d3Sbellard 519ea2384d3Sbellard score_max = 0; 52084a12e66SChristoph Hellwig drv = NULL; 5218a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 52283f64091Sbellard if (drv1->bdrv_probe) { 523ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 524ea2384d3Sbellard if (score > score_max) { 525ea2384d3Sbellard score_max = score; 526ea2384d3Sbellard drv = drv1; 527ea2384d3Sbellard } 528ea2384d3Sbellard } 52983f64091Sbellard } 530c98ac35dSStefan Weil if (!drv) { 531c98ac35dSStefan Weil ret = -ENOENT; 532c98ac35dSStefan Weil } 533c98ac35dSStefan Weil *pdrv = drv; 534c98ac35dSStefan Weil return ret; 535ea2384d3Sbellard } 536ea2384d3Sbellard 53751762288SStefan Hajnoczi /** 53851762288SStefan Hajnoczi * Set the current 'total_sectors' value 53951762288SStefan Hajnoczi */ 54051762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 54151762288SStefan Hajnoczi { 54251762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 54351762288SStefan Hajnoczi 544396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 545396759adSNicholas Bellinger if (bs->sg) 546396759adSNicholas Bellinger return 0; 547396759adSNicholas Bellinger 54851762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 54951762288SStefan Hajnoczi if (drv->bdrv_getlength) { 55051762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 55151762288SStefan Hajnoczi if (length < 0) { 55251762288SStefan Hajnoczi return length; 55351762288SStefan Hajnoczi } 55451762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 55551762288SStefan Hajnoczi } 55651762288SStefan Hajnoczi 55751762288SStefan Hajnoczi bs->total_sectors = hint; 55851762288SStefan Hajnoczi return 0; 55951762288SStefan Hajnoczi } 56051762288SStefan Hajnoczi 561c3993cdcSStefan Hajnoczi /** 562c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 563c3993cdcSStefan Hajnoczi * 564c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 565c3993cdcSStefan Hajnoczi */ 566c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 567c3993cdcSStefan Hajnoczi { 568c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 569c3993cdcSStefan Hajnoczi 570c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 571c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 57292196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 57392196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 574c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 575c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 576c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 577c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 578c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 579c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 580c3993cdcSStefan Hajnoczi /* this is the default */ 581c3993cdcSStefan Hajnoczi } else { 582c3993cdcSStefan Hajnoczi return -1; 583c3993cdcSStefan Hajnoczi } 584c3993cdcSStefan Hajnoczi 585c3993cdcSStefan Hajnoczi return 0; 586c3993cdcSStefan Hajnoczi } 587c3993cdcSStefan Hajnoczi 58853fec9d3SStefan Hajnoczi /** 58953fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 59053fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 59153fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 59253fec9d3SStefan Hajnoczi */ 59353fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 59453fec9d3SStefan Hajnoczi { 59553fec9d3SStefan Hajnoczi bs->copy_on_read++; 59653fec9d3SStefan Hajnoczi } 59753fec9d3SStefan Hajnoczi 59853fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 59953fec9d3SStefan Hajnoczi { 60053fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 60153fec9d3SStefan Hajnoczi bs->copy_on_read--; 60253fec9d3SStefan Hajnoczi } 60353fec9d3SStefan Hajnoczi 604b6ce07aaSKevin Wolf /* 60557915332SKevin Wolf * Common part for opening disk images and files 60657915332SKevin Wolf */ 60757915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 60857915332SKevin Wolf int flags, BlockDriver *drv) 60957915332SKevin Wolf { 61057915332SKevin Wolf int ret, open_flags; 61157915332SKevin Wolf 61257915332SKevin Wolf assert(drv != NULL); 61357915332SKevin Wolf 61428dcee10SStefan Hajnoczi trace_bdrv_open_common(bs, filename, flags, drv->format_name); 61528dcee10SStefan Hajnoczi 61666f82ceeSKevin Wolf bs->file = NULL; 61751762288SStefan Hajnoczi bs->total_sectors = 0; 61857915332SKevin Wolf bs->encrypted = 0; 61957915332SKevin Wolf bs->valid_key = 0; 62003f541bdSStefan Hajnoczi bs->sg = 0; 62157915332SKevin Wolf bs->open_flags = flags; 62203f541bdSStefan Hajnoczi bs->growable = 0; 62357915332SKevin Wolf bs->buffer_alignment = 512; 62457915332SKevin Wolf 62553fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 62653fec9d3SStefan Hajnoczi if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) { 62753fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 62853fec9d3SStefan Hajnoczi } 62953fec9d3SStefan Hajnoczi 63057915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 63103f541bdSStefan Hajnoczi bs->backing_file[0] = '\0'; 63257915332SKevin Wolf 63357915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 63457915332SKevin Wolf return -ENOTSUP; 63557915332SKevin Wolf } 63657915332SKevin Wolf 63757915332SKevin Wolf bs->drv = drv; 6387267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 63957915332SKevin Wolf 64003f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 64157915332SKevin Wolf 64257915332SKevin Wolf /* 64357915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 64457915332SKevin Wolf * image. 64557915332SKevin Wolf */ 64657915332SKevin Wolf open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 64757915332SKevin Wolf 64857915332SKevin Wolf /* 649ebabb67aSStefan Weil * Snapshots should be writable. 65057915332SKevin Wolf */ 65157915332SKevin Wolf if (bs->is_temporary) { 65257915332SKevin Wolf open_flags |= BDRV_O_RDWR; 65357915332SKevin Wolf } 65457915332SKevin Wolf 655e7c63796SStefan Hajnoczi bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 656e7c63796SStefan Hajnoczi 65766f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 65866f82ceeSKevin Wolf if (drv->bdrv_file_open) { 65966f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 66066f82ceeSKevin Wolf } else { 66166f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 66266f82ceeSKevin Wolf if (ret >= 0) { 66366f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 66466f82ceeSKevin Wolf } 66566f82ceeSKevin Wolf } 66666f82ceeSKevin Wolf 66757915332SKevin Wolf if (ret < 0) { 66857915332SKevin Wolf goto free_and_fail; 66957915332SKevin Wolf } 67057915332SKevin Wolf 67151762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 67251762288SStefan Hajnoczi if (ret < 0) { 67351762288SStefan Hajnoczi goto free_and_fail; 67457915332SKevin Wolf } 67551762288SStefan Hajnoczi 67657915332SKevin Wolf #ifndef _WIN32 67757915332SKevin Wolf if (bs->is_temporary) { 67857915332SKevin Wolf unlink(filename); 67957915332SKevin Wolf } 68057915332SKevin Wolf #endif 68157915332SKevin Wolf return 0; 68257915332SKevin Wolf 68357915332SKevin Wolf free_and_fail: 68466f82ceeSKevin Wolf if (bs->file) { 68566f82ceeSKevin Wolf bdrv_delete(bs->file); 68666f82ceeSKevin Wolf bs->file = NULL; 68766f82ceeSKevin Wolf } 6887267c094SAnthony Liguori g_free(bs->opaque); 68957915332SKevin Wolf bs->opaque = NULL; 69057915332SKevin Wolf bs->drv = NULL; 69157915332SKevin Wolf return ret; 69257915332SKevin Wolf } 69357915332SKevin Wolf 69457915332SKevin Wolf /* 695b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 696b6ce07aaSKevin Wolf */ 69783f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 698b338082bSbellard { 69983f64091Sbellard BlockDriverState *bs; 7006db95603SChristoph Hellwig BlockDriver *drv; 70183f64091Sbellard int ret; 7023b0d4f61Sbellard 703b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 7046db95603SChristoph Hellwig if (!drv) { 7056db95603SChristoph Hellwig return -ENOENT; 7066db95603SChristoph Hellwig } 7076db95603SChristoph Hellwig 70883f64091Sbellard bs = bdrv_new(""); 709b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 71083f64091Sbellard if (ret < 0) { 71183f64091Sbellard bdrv_delete(bs); 71283f64091Sbellard return ret; 7133b0d4f61Sbellard } 71471d0770cSaliguori bs->growable = 1; 71583f64091Sbellard *pbs = bs; 71683f64091Sbellard return 0; 7173b0d4f61Sbellard } 7183b0d4f61Sbellard 719b6ce07aaSKevin Wolf /* 720b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 721b6ce07aaSKevin Wolf */ 722d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 723ea2384d3Sbellard BlockDriver *drv) 724ea2384d3Sbellard { 725b6ce07aaSKevin Wolf int ret; 7262b572816SKevin Wolf char tmp_filename[PATH_MAX]; 72733e3963eSbellard 72883f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 729ea2384d3Sbellard BlockDriverState *bs1; 730ea2384d3Sbellard int64_t total_size; 7317c96d46eSaliguori int is_protocol = 0; 73291a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 73391a073a9SKevin Wolf QEMUOptionParameter *options; 734b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 73533e3963eSbellard 736ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 737ea2384d3Sbellard instead of opening 'filename' directly */ 738ea2384d3Sbellard 739ea2384d3Sbellard /* if there is a backing file, use it */ 740ea2384d3Sbellard bs1 = bdrv_new(""); 741d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 74251d7c00cSaliguori if (ret < 0) { 743ea2384d3Sbellard bdrv_delete(bs1); 74451d7c00cSaliguori return ret; 745ea2384d3Sbellard } 7463e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 7477c96d46eSaliguori 7487c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 7497c96d46eSaliguori is_protocol = 1; 7507c96d46eSaliguori 751ea2384d3Sbellard bdrv_delete(bs1); 752ea2384d3Sbellard 753ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 7547c96d46eSaliguori 7557c96d46eSaliguori /* Real path is meaningless for protocols */ 7567c96d46eSaliguori if (is_protocol) 7577c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 7587c96d46eSaliguori "%s", filename); 759114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 760114cdfa9SKirill A. Shutemov return -errno; 7617c96d46eSaliguori 76291a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 76391a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 76491a073a9SKevin Wolf 7653e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 76691a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 76791a073a9SKevin Wolf if (drv) { 76891a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 76991a073a9SKevin Wolf drv->format_name); 77091a073a9SKevin Wolf } 77191a073a9SKevin Wolf 77291a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 773d748768cSJan Kiszka free_option_parameters(options); 77451d7c00cSaliguori if (ret < 0) { 77551d7c00cSaliguori return ret; 776ea2384d3Sbellard } 77791a073a9SKevin Wolf 778ea2384d3Sbellard filename = tmp_filename; 77991a073a9SKevin Wolf drv = bdrv_qcow2; 780ea2384d3Sbellard bs->is_temporary = 1; 781ea2384d3Sbellard } 782ea2384d3Sbellard 783b6ce07aaSKevin Wolf /* Find the right image format driver */ 7846db95603SChristoph Hellwig if (!drv) { 785c98ac35dSStefan Weil ret = find_image_format(filename, &drv); 786ea2384d3Sbellard } 7876987307cSChristoph Hellwig 78851d7c00cSaliguori if (!drv) { 78951d7c00cSaliguori goto unlink_and_fail; 79083f64091Sbellard } 791b6ce07aaSKevin Wolf 792b6ce07aaSKevin Wolf /* Open the image */ 793b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 794b6ce07aaSKevin Wolf if (ret < 0) { 7956987307cSChristoph Hellwig goto unlink_and_fail; 7966987307cSChristoph Hellwig } 7976987307cSChristoph Hellwig 798b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 799b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 800b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 801b6ce07aaSKevin Wolf int back_flags; 802b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 803b6ce07aaSKevin Wolf 804b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 805df2dbb4aSStefan Hajnoczi 806df2dbb4aSStefan Hajnoczi if (path_has_protocol(bs->backing_file)) { 807df2dbb4aSStefan Hajnoczi pstrcpy(backing_filename, sizeof(backing_filename), 808df2dbb4aSStefan Hajnoczi bs->backing_file); 809df2dbb4aSStefan Hajnoczi } else { 810b6ce07aaSKevin Wolf path_combine(backing_filename, sizeof(backing_filename), 811b6ce07aaSKevin Wolf filename, bs->backing_file); 812df2dbb4aSStefan Hajnoczi } 813df2dbb4aSStefan Hajnoczi 814df2dbb4aSStefan Hajnoczi if (bs->backing_format[0] != '\0') { 815b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 816df2dbb4aSStefan Hajnoczi } 817b6ce07aaSKevin Wolf 818b6ce07aaSKevin Wolf /* backing files always opened read-only */ 819b6ce07aaSKevin Wolf back_flags = 820b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 821b6ce07aaSKevin Wolf 822b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 823b6ce07aaSKevin Wolf if (ret < 0) { 824b6ce07aaSKevin Wolf bdrv_close(bs); 825b6ce07aaSKevin Wolf return ret; 826b6ce07aaSKevin Wolf } 827b6ce07aaSKevin Wolf if (bs->is_temporary) { 828b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 829b6ce07aaSKevin Wolf } else { 830b6ce07aaSKevin Wolf /* base image inherits from "parent" */ 831b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = bs->keep_read_only; 832b6ce07aaSKevin Wolf } 833b6ce07aaSKevin Wolf } 834b6ce07aaSKevin Wolf 835b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 8367d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 837b6ce07aaSKevin Wolf } 838b6ce07aaSKevin Wolf 83998f90dbaSZhi Yong Wu /* throttling disk I/O limits */ 84098f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 84198f90dbaSZhi Yong Wu bdrv_io_limits_enable(bs); 84298f90dbaSZhi Yong Wu } 84398f90dbaSZhi Yong Wu 844b6ce07aaSKevin Wolf return 0; 845b6ce07aaSKevin Wolf 846b6ce07aaSKevin Wolf unlink_and_fail: 847b6ce07aaSKevin Wolf if (bs->is_temporary) { 848b6ce07aaSKevin Wolf unlink(filename); 849b6ce07aaSKevin Wolf } 850b6ce07aaSKevin Wolf return ret; 851b6ce07aaSKevin Wolf } 852b6ce07aaSKevin Wolf 853fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 854fc01f7e7Sbellard { 85580ccf93bSLiu Yuan bdrv_flush(bs); 85619cb3738Sbellard if (bs->drv) { 8573e914655SPaolo Bonzini if (bs->job) { 8583e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 8593e914655SPaolo Bonzini } 8607094f12fSKevin Wolf bdrv_drain_all(); 8617094f12fSKevin Wolf 862f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 863f9092b10SMarkus Armbruster bs_snapshots = NULL; 864f9092b10SMarkus Armbruster } 865557df6acSStefan Hajnoczi if (bs->backing_hd) { 866ea2384d3Sbellard bdrv_delete(bs->backing_hd); 867557df6acSStefan Hajnoczi bs->backing_hd = NULL; 868557df6acSStefan Hajnoczi } 869ea2384d3Sbellard bs->drv->bdrv_close(bs); 8707267c094SAnthony Liguori g_free(bs->opaque); 871ea2384d3Sbellard #ifdef _WIN32 872ea2384d3Sbellard if (bs->is_temporary) { 873ea2384d3Sbellard unlink(bs->filename); 874ea2384d3Sbellard } 87567b915a5Sbellard #endif 876ea2384d3Sbellard bs->opaque = NULL; 877ea2384d3Sbellard bs->drv = NULL; 87853fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 879a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 880a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 881b338082bSbellard 88266f82ceeSKevin Wolf if (bs->file != NULL) { 8830ac9377dSPaolo Bonzini bdrv_delete(bs->file); 8840ac9377dSPaolo Bonzini bs->file = NULL; 88566f82ceeSKevin Wolf } 88666f82ceeSKevin Wolf 8877d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 888b338082bSbellard } 88998f90dbaSZhi Yong Wu 89098f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 89198f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 89298f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 89398f90dbaSZhi Yong Wu } 894b338082bSbellard } 895b338082bSbellard 8962bc93fedSMORITA Kazutaka void bdrv_close_all(void) 8972bc93fedSMORITA Kazutaka { 8982bc93fedSMORITA Kazutaka BlockDriverState *bs; 8992bc93fedSMORITA Kazutaka 9002bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 9012bc93fedSMORITA Kazutaka bdrv_close(bs); 9022bc93fedSMORITA Kazutaka } 9032bc93fedSMORITA Kazutaka } 9042bc93fedSMORITA Kazutaka 905922453bcSStefan Hajnoczi /* 906922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 907922453bcSStefan Hajnoczi * 908922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 909922453bcSStefan Hajnoczi * after calling this function. 9104c355d53SZhi Yong Wu * 9114c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 9124c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 9134c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 9144c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 9154c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 916922453bcSStefan Hajnoczi */ 917922453bcSStefan Hajnoczi void bdrv_drain_all(void) 918922453bcSStefan Hajnoczi { 919922453bcSStefan Hajnoczi BlockDriverState *bs; 9204c355d53SZhi Yong Wu bool busy; 921922453bcSStefan Hajnoczi 9224c355d53SZhi Yong Wu do { 9234c355d53SZhi Yong Wu busy = qemu_aio_wait(); 9244c355d53SZhi Yong Wu 9254c355d53SZhi Yong Wu /* FIXME: We do not have timer support here, so this is effectively 9264c355d53SZhi Yong Wu * a busy wait. 9274c355d53SZhi Yong Wu */ 9284c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 9294c355d53SZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 9304c355d53SZhi Yong Wu qemu_co_queue_restart_all(&bs->throttled_reqs); 9314c355d53SZhi Yong Wu busy = true; 9324c355d53SZhi Yong Wu } 9334c355d53SZhi Yong Wu } 9344c355d53SZhi Yong Wu } while (busy); 935922453bcSStefan Hajnoczi 936922453bcSStefan Hajnoczi /* If requests are still pending there is a bug somewhere */ 937922453bcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 938922453bcSStefan Hajnoczi assert(QLIST_EMPTY(&bs->tracked_requests)); 939922453bcSStefan Hajnoczi assert(qemu_co_queue_empty(&bs->throttled_reqs)); 940922453bcSStefan Hajnoczi } 941922453bcSStefan Hajnoczi } 942922453bcSStefan Hajnoczi 943d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 944d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 945d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 946d22b2f41SRyan Harper { 947d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 948d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 949d22b2f41SRyan Harper } 950d22b2f41SRyan Harper bs->device_name[0] = '\0'; 951d22b2f41SRyan Harper } 952d22b2f41SRyan Harper 953e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 954e023b2e2SPaolo Bonzini { 955e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 956e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 957e023b2e2SPaolo Bonzini } 958e023b2e2SPaolo Bonzini } 959e023b2e2SPaolo Bonzini 9608802d1fdSJeff Cody /* 9618802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 9628802d1fdSJeff Cody * live, while keeping required fields on the top layer. 9638802d1fdSJeff Cody * 9648802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 9658802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 9668802d1fdSJeff Cody * 967f6801b83SJeff Cody * bs_new is required to be anonymous. 968f6801b83SJeff Cody * 9698802d1fdSJeff Cody * This function does not create any image files. 9708802d1fdSJeff Cody */ 9718802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 9728802d1fdSJeff Cody { 9738802d1fdSJeff Cody BlockDriverState tmp; 9748802d1fdSJeff Cody 975f6801b83SJeff Cody /* bs_new must be anonymous */ 976f6801b83SJeff Cody assert(bs_new->device_name[0] == '\0'); 9778802d1fdSJeff Cody 9788802d1fdSJeff Cody tmp = *bs_new; 9798802d1fdSJeff Cody 9808802d1fdSJeff Cody /* there are some fields that need to stay on the top layer: */ 9813a389e79SPaolo Bonzini tmp.open_flags = bs_top->open_flags; 9828802d1fdSJeff Cody 9838802d1fdSJeff Cody /* dev info */ 9848802d1fdSJeff Cody tmp.dev_ops = bs_top->dev_ops; 9858802d1fdSJeff Cody tmp.dev_opaque = bs_top->dev_opaque; 9868802d1fdSJeff Cody tmp.dev = bs_top->dev; 9878802d1fdSJeff Cody tmp.buffer_alignment = bs_top->buffer_alignment; 9888802d1fdSJeff Cody tmp.copy_on_read = bs_top->copy_on_read; 9898802d1fdSJeff Cody 9908802d1fdSJeff Cody /* i/o timing parameters */ 9918802d1fdSJeff Cody tmp.slice_time = bs_top->slice_time; 9928802d1fdSJeff Cody tmp.slice_start = bs_top->slice_start; 9938802d1fdSJeff Cody tmp.slice_end = bs_top->slice_end; 9948802d1fdSJeff Cody tmp.io_limits = bs_top->io_limits; 9958802d1fdSJeff Cody tmp.io_base = bs_top->io_base; 9968802d1fdSJeff Cody tmp.throttled_reqs = bs_top->throttled_reqs; 9978802d1fdSJeff Cody tmp.block_timer = bs_top->block_timer; 9988802d1fdSJeff Cody tmp.io_limits_enabled = bs_top->io_limits_enabled; 9998802d1fdSJeff Cody 10008802d1fdSJeff Cody /* geometry */ 10018802d1fdSJeff Cody tmp.cyls = bs_top->cyls; 10028802d1fdSJeff Cody tmp.heads = bs_top->heads; 10038802d1fdSJeff Cody tmp.secs = bs_top->secs; 10048802d1fdSJeff Cody tmp.translation = bs_top->translation; 10058802d1fdSJeff Cody 10068802d1fdSJeff Cody /* r/w error */ 10078802d1fdSJeff Cody tmp.on_read_error = bs_top->on_read_error; 10088802d1fdSJeff Cody tmp.on_write_error = bs_top->on_write_error; 10098802d1fdSJeff Cody 10108802d1fdSJeff Cody /* i/o status */ 10118802d1fdSJeff Cody tmp.iostatus_enabled = bs_top->iostatus_enabled; 10128802d1fdSJeff Cody tmp.iostatus = bs_top->iostatus; 10138802d1fdSJeff Cody 10148802d1fdSJeff Cody /* keep the same entry in bdrv_states */ 10158802d1fdSJeff Cody pstrcpy(tmp.device_name, sizeof(tmp.device_name), bs_top->device_name); 10168802d1fdSJeff Cody tmp.list = bs_top->list; 10178802d1fdSJeff Cody 10188802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 10198802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 10208802d1fdSJeff Cody tmp.backing_hd = bs_new; 10218802d1fdSJeff Cody pstrcpy(tmp.backing_file, sizeof(tmp.backing_file), bs_top->filename); 1022f6801b83SJeff Cody bdrv_get_format(bs_top, tmp.backing_format, sizeof(tmp.backing_format)); 10238802d1fdSJeff Cody 10248802d1fdSJeff Cody /* swap contents of the fixed new bs and the current top */ 10258802d1fdSJeff Cody *bs_new = *bs_top; 10268802d1fdSJeff Cody *bs_top = tmp; 10278802d1fdSJeff Cody 1028f6801b83SJeff Cody /* device_name[] was carried over from the old bs_top. bs_new 1029f6801b83SJeff Cody * shouldn't be in bdrv_states, so we need to make device_name[] 1030f6801b83SJeff Cody * reflect the anonymity of bs_new 1031f6801b83SJeff Cody */ 1032f6801b83SJeff Cody bs_new->device_name[0] = '\0'; 1033f6801b83SJeff Cody 10348802d1fdSJeff Cody /* clear the copied fields in the new backing file */ 10358802d1fdSJeff Cody bdrv_detach_dev(bs_new, bs_new->dev); 10368802d1fdSJeff Cody 10378802d1fdSJeff Cody qemu_co_queue_init(&bs_new->throttled_reqs); 10388802d1fdSJeff Cody memset(&bs_new->io_base, 0, sizeof(bs_new->io_base)); 10398802d1fdSJeff Cody memset(&bs_new->io_limits, 0, sizeof(bs_new->io_limits)); 10408802d1fdSJeff Cody bdrv_iostatus_disable(bs_new); 10418802d1fdSJeff Cody 10428802d1fdSJeff Cody /* we don't use bdrv_io_limits_disable() for this, because we don't want 10438802d1fdSJeff Cody * to affect or delete the block_timer, as it has been moved to bs_top */ 10448802d1fdSJeff Cody bs_new->io_limits_enabled = false; 10458802d1fdSJeff Cody bs_new->block_timer = NULL; 10468802d1fdSJeff Cody bs_new->slice_time = 0; 10478802d1fdSJeff Cody bs_new->slice_start = 0; 10488802d1fdSJeff Cody bs_new->slice_end = 0; 1049e023b2e2SPaolo Bonzini 1050e023b2e2SPaolo Bonzini bdrv_rebind(bs_new); 1051e023b2e2SPaolo Bonzini bdrv_rebind(bs_top); 10528802d1fdSJeff Cody } 10538802d1fdSJeff Cody 1054b338082bSbellard void bdrv_delete(BlockDriverState *bs) 1055b338082bSbellard { 1056fa879d62SMarkus Armbruster assert(!bs->dev); 10573e914655SPaolo Bonzini assert(!bs->job); 10583e914655SPaolo Bonzini assert(!bs->in_use); 105918846deeSMarkus Armbruster 10601b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1061d22b2f41SRyan Harper bdrv_make_anon(bs); 106234c6f050Saurel32 1063b338082bSbellard bdrv_close(bs); 106466f82ceeSKevin Wolf 1065f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 10667267c094SAnthony Liguori g_free(bs); 1067fc01f7e7Sbellard } 1068fc01f7e7Sbellard 1069fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1070fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 107118846deeSMarkus Armbruster { 1072fa879d62SMarkus Armbruster if (bs->dev) { 107318846deeSMarkus Armbruster return -EBUSY; 107418846deeSMarkus Armbruster } 1075fa879d62SMarkus Armbruster bs->dev = dev; 107628a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 107718846deeSMarkus Armbruster return 0; 107818846deeSMarkus Armbruster } 107918846deeSMarkus Armbruster 1080fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1081fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 108218846deeSMarkus Armbruster { 1083fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1084fa879d62SMarkus Armbruster abort(); 1085fa879d62SMarkus Armbruster } 1086fa879d62SMarkus Armbruster } 1087fa879d62SMarkus Armbruster 1088fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1089fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1090fa879d62SMarkus Armbruster { 1091fa879d62SMarkus Armbruster assert(bs->dev == dev); 1092fa879d62SMarkus Armbruster bs->dev = NULL; 10930e49de52SMarkus Armbruster bs->dev_ops = NULL; 10940e49de52SMarkus Armbruster bs->dev_opaque = NULL; 109529e05f20SMarkus Armbruster bs->buffer_alignment = 512; 109618846deeSMarkus Armbruster } 109718846deeSMarkus Armbruster 1098fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1099fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 110018846deeSMarkus Armbruster { 1101fa879d62SMarkus Armbruster return bs->dev; 110218846deeSMarkus Armbruster } 110318846deeSMarkus Armbruster 11040e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 11050e49de52SMarkus Armbruster void *opaque) 11060e49de52SMarkus Armbruster { 11070e49de52SMarkus Armbruster bs->dev_ops = ops; 11080e49de52SMarkus Armbruster bs->dev_opaque = opaque; 11092c6942faSMarkus Armbruster if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) { 11102c6942faSMarkus Armbruster bs_snapshots = NULL; 11112c6942faSMarkus Armbruster } 11120e49de52SMarkus Armbruster } 11130e49de52SMarkus Armbruster 1114329c0a48SLuiz Capitulino void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 1115329c0a48SLuiz Capitulino BlockQMPEventAction action, int is_read) 1116329c0a48SLuiz Capitulino { 1117329c0a48SLuiz Capitulino QObject *data; 1118329c0a48SLuiz Capitulino const char *action_str; 1119329c0a48SLuiz Capitulino 1120329c0a48SLuiz Capitulino switch (action) { 1121329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1122329c0a48SLuiz Capitulino action_str = "report"; 1123329c0a48SLuiz Capitulino break; 1124329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1125329c0a48SLuiz Capitulino action_str = "ignore"; 1126329c0a48SLuiz Capitulino break; 1127329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1128329c0a48SLuiz Capitulino action_str = "stop"; 1129329c0a48SLuiz Capitulino break; 1130329c0a48SLuiz Capitulino default: 1131329c0a48SLuiz Capitulino abort(); 1132329c0a48SLuiz Capitulino } 1133329c0a48SLuiz Capitulino 1134329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1135329c0a48SLuiz Capitulino bdrv->device_name, 1136329c0a48SLuiz Capitulino action_str, 1137329c0a48SLuiz Capitulino is_read ? "read" : "write"); 1138329c0a48SLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 1139329c0a48SLuiz Capitulino 1140329c0a48SLuiz Capitulino qobject_decref(data); 1141329c0a48SLuiz Capitulino } 1142329c0a48SLuiz Capitulino 11436f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 11446f382ed2SLuiz Capitulino { 11456f382ed2SLuiz Capitulino QObject *data; 11466f382ed2SLuiz Capitulino 11476f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 11486f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 11496f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 11506f382ed2SLuiz Capitulino 11516f382ed2SLuiz Capitulino qobject_decref(data); 11526f382ed2SLuiz Capitulino } 11536f382ed2SLuiz Capitulino 11547d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 11550e49de52SMarkus Armbruster { 1156145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 11576f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 11587d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 11596f382ed2SLuiz Capitulino if (tray_was_closed) { 11606f382ed2SLuiz Capitulino /* tray open */ 11616f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 11626f382ed2SLuiz Capitulino } 11636f382ed2SLuiz Capitulino if (load) { 11646f382ed2SLuiz Capitulino /* tray close */ 11656f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 11666f382ed2SLuiz Capitulino } 1167145feb17SMarkus Armbruster } 1168145feb17SMarkus Armbruster } 1169145feb17SMarkus Armbruster 11702c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 11712c6942faSMarkus Armbruster { 11722c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 11732c6942faSMarkus Armbruster } 11742c6942faSMarkus Armbruster 1175025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1176025ccaa7SPaolo Bonzini { 1177025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1178025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1179025ccaa7SPaolo Bonzini } 1180025ccaa7SPaolo Bonzini } 1181025ccaa7SPaolo Bonzini 1182e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1183e4def80bSMarkus Armbruster { 1184e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1185e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1186e4def80bSMarkus Armbruster } 1187e4def80bSMarkus Armbruster return false; 1188e4def80bSMarkus Armbruster } 1189e4def80bSMarkus Armbruster 1190145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1191145feb17SMarkus Armbruster { 1192145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1193145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 11940e49de52SMarkus Armbruster } 11950e49de52SMarkus Armbruster } 11960e49de52SMarkus Armbruster 1197f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1198f107639aSMarkus Armbruster { 1199f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1200f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1201f107639aSMarkus Armbruster } 1202f107639aSMarkus Armbruster return false; 1203f107639aSMarkus Armbruster } 1204f107639aSMarkus Armbruster 1205e97fc193Saliguori /* 1206e97fc193Saliguori * Run consistency checks on an image 1207e97fc193Saliguori * 1208e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1209a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1210e076f338SKevin Wolf * check are stored in res. 1211e97fc193Saliguori */ 1212e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) 1213e97fc193Saliguori { 1214e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1215e97fc193Saliguori return -ENOTSUP; 1216e97fc193Saliguori } 1217e97fc193Saliguori 1218e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 12199ac228e0SKevin Wolf return bs->drv->bdrv_check(bs, res); 1220e97fc193Saliguori } 1221e97fc193Saliguori 12228a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 12238a426614SKevin Wolf 122433e3963eSbellard /* commit COW file into the raw image */ 122533e3963eSbellard int bdrv_commit(BlockDriverState *bs) 122633e3963eSbellard { 122719cb3738Sbellard BlockDriver *drv = bs->drv; 1228ee181196SKevin Wolf BlockDriver *backing_drv; 12298a426614SKevin Wolf int64_t sector, total_sectors; 12308a426614SKevin Wolf int n, ro, open_flags; 12314dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 12328a426614SKevin Wolf uint8_t *buf; 12334dca4b63SNaphtali Sprei char filename[1024]; 12344dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 123533e3963eSbellard 123619cb3738Sbellard if (!drv) 123719cb3738Sbellard return -ENOMEDIUM; 123833e3963eSbellard 12394dca4b63SNaphtali Sprei if (!bs->backing_hd) { 12404dca4b63SNaphtali Sprei return -ENOTSUP; 12414dca4b63SNaphtali Sprei } 12424dca4b63SNaphtali Sprei 12434dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 1244ea2384d3Sbellard return -EACCES; 124533e3963eSbellard } 124633e3963eSbellard 12472d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 12482d3735d3SStefan Hajnoczi return -EBUSY; 12492d3735d3SStefan Hajnoczi } 12502d3735d3SStefan Hajnoczi 1251ee181196SKevin Wolf backing_drv = bs->backing_hd->drv; 12524dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 12534dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 12544dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 12554dca4b63SNaphtali Sprei 12564dca4b63SNaphtali Sprei if (ro) { 12574dca4b63SNaphtali Sprei /* re-open as RW */ 12584dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 12594dca4b63SNaphtali Sprei bs->backing_hd = NULL; 12604dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 1261ee181196SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, 1262ee181196SKevin Wolf backing_drv); 12634dca4b63SNaphtali Sprei if (rw_ret < 0) { 12644dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 12654dca4b63SNaphtali Sprei /* try to re-open read-only */ 12664dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1267ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1268ee181196SKevin Wolf backing_drv); 12694dca4b63SNaphtali Sprei if (ret < 0) { 12704dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 12714dca4b63SNaphtali Sprei /* drive not functional anymore */ 12724dca4b63SNaphtali Sprei bs->drv = NULL; 12734dca4b63SNaphtali Sprei return ret; 12744dca4b63SNaphtali Sprei } 12754dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 12764dca4b63SNaphtali Sprei return rw_ret; 12774dca4b63SNaphtali Sprei } 12784dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 1279ea2384d3Sbellard } 1280ea2384d3Sbellard 12816ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 12827267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 12838a426614SKevin Wolf 12848a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 128505c4af54SStefan Hajnoczi if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 12868a426614SKevin Wolf 12878a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 12884dca4b63SNaphtali Sprei ret = -EIO; 12894dca4b63SNaphtali Sprei goto ro_cleanup; 129033e3963eSbellard } 129133e3963eSbellard 12928a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 12934dca4b63SNaphtali Sprei ret = -EIO; 12944dca4b63SNaphtali Sprei goto ro_cleanup; 129533e3963eSbellard } 129633e3963eSbellard } 129733e3963eSbellard } 129895389c86Sbellard 12991d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 13001d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 13011d44952fSChristoph Hellwig bdrv_flush(bs); 13021d44952fSChristoph Hellwig } 130395389c86Sbellard 13043f5075aeSChristoph Hellwig /* 13053f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 13063f5075aeSChristoph Hellwig * stable on disk. 13073f5075aeSChristoph Hellwig */ 13083f5075aeSChristoph Hellwig if (bs->backing_hd) 13093f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 13104dca4b63SNaphtali Sprei 13114dca4b63SNaphtali Sprei ro_cleanup: 13127267c094SAnthony Liguori g_free(buf); 13134dca4b63SNaphtali Sprei 13144dca4b63SNaphtali Sprei if (ro) { 13154dca4b63SNaphtali Sprei /* re-open as RO */ 13164dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 13174dca4b63SNaphtali Sprei bs->backing_hd = NULL; 13184dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1319ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1320ee181196SKevin Wolf backing_drv); 13214dca4b63SNaphtali Sprei if (ret < 0) { 13224dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 13234dca4b63SNaphtali Sprei /* drive not functional anymore */ 13244dca4b63SNaphtali Sprei bs->drv = NULL; 13254dca4b63SNaphtali Sprei return ret; 13264dca4b63SNaphtali Sprei } 13274dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 13284dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 13294dca4b63SNaphtali Sprei } 13304dca4b63SNaphtali Sprei 13311d44952fSChristoph Hellwig return ret; 133233e3963eSbellard } 133333e3963eSbellard 1334e8877497SStefan Hajnoczi int bdrv_commit_all(void) 13356ab4b5abSMarkus Armbruster { 13366ab4b5abSMarkus Armbruster BlockDriverState *bs; 13376ab4b5abSMarkus Armbruster 13386ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1339e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1340e8877497SStefan Hajnoczi if (ret < 0) { 1341e8877497SStefan Hajnoczi return ret; 13426ab4b5abSMarkus Armbruster } 13436ab4b5abSMarkus Armbruster } 1344e8877497SStefan Hajnoczi return 0; 1345e8877497SStefan Hajnoczi } 13466ab4b5abSMarkus Armbruster 1347dbffbdcfSStefan Hajnoczi struct BdrvTrackedRequest { 1348dbffbdcfSStefan Hajnoczi BlockDriverState *bs; 1349dbffbdcfSStefan Hajnoczi int64_t sector_num; 1350dbffbdcfSStefan Hajnoczi int nb_sectors; 1351dbffbdcfSStefan Hajnoczi bool is_write; 1352dbffbdcfSStefan Hajnoczi QLIST_ENTRY(BdrvTrackedRequest) list; 13535f8b6491SStefan Hajnoczi Coroutine *co; /* owner, used for deadlock detection */ 1354f4658285SStefan Hajnoczi CoQueue wait_queue; /* coroutines blocked on this request */ 1355dbffbdcfSStefan Hajnoczi }; 1356dbffbdcfSStefan Hajnoczi 1357dbffbdcfSStefan Hajnoczi /** 1358dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1359dbffbdcfSStefan Hajnoczi * 1360dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1361dbffbdcfSStefan Hajnoczi */ 1362dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1363dbffbdcfSStefan Hajnoczi { 1364dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1365f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1366dbffbdcfSStefan Hajnoczi } 1367dbffbdcfSStefan Hajnoczi 1368dbffbdcfSStefan Hajnoczi /** 1369dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1370dbffbdcfSStefan Hajnoczi */ 1371dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1372dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1373dbffbdcfSStefan Hajnoczi int64_t sector_num, 1374dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1375dbffbdcfSStefan Hajnoczi { 1376dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1377dbffbdcfSStefan Hajnoczi .bs = bs, 1378dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1379dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1380dbffbdcfSStefan Hajnoczi .is_write = is_write, 13815f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1382dbffbdcfSStefan Hajnoczi }; 1383dbffbdcfSStefan Hajnoczi 1384f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1385f4658285SStefan Hajnoczi 1386dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1387dbffbdcfSStefan Hajnoczi } 1388dbffbdcfSStefan Hajnoczi 1389d83947acSStefan Hajnoczi /** 1390d83947acSStefan Hajnoczi * Round a region to cluster boundaries 1391d83947acSStefan Hajnoczi */ 1392d83947acSStefan Hajnoczi static void round_to_clusters(BlockDriverState *bs, 1393d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 1394d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 1395d83947acSStefan Hajnoczi int *cluster_nb_sectors) 1396d83947acSStefan Hajnoczi { 1397d83947acSStefan Hajnoczi BlockDriverInfo bdi; 1398d83947acSStefan Hajnoczi 1399d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 1400d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 1401d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 1402d83947acSStefan Hajnoczi } else { 1403d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 1404d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 1405d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 1406d83947acSStefan Hajnoczi nb_sectors, c); 1407d83947acSStefan Hajnoczi } 1408d83947acSStefan Hajnoczi } 1409d83947acSStefan Hajnoczi 1410f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 1411f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 1412d83947acSStefan Hajnoczi /* aaaa bbbb */ 1413d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 1414d83947acSStefan Hajnoczi return false; 1415d83947acSStefan Hajnoczi } 1416d83947acSStefan Hajnoczi /* bbbb aaaa */ 1417d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 1418d83947acSStefan Hajnoczi return false; 1419d83947acSStefan Hajnoczi } 1420d83947acSStefan Hajnoczi return true; 1421f4658285SStefan Hajnoczi } 1422f4658285SStefan Hajnoczi 1423f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 1424f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 1425f4658285SStefan Hajnoczi { 1426f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 1427d83947acSStefan Hajnoczi int64_t cluster_sector_num; 1428d83947acSStefan Hajnoczi int cluster_nb_sectors; 1429f4658285SStefan Hajnoczi bool retry; 1430f4658285SStefan Hajnoczi 1431d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 1432d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 1433d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 1434d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 1435d83947acSStefan Hajnoczi * interleave between them. 1436d83947acSStefan Hajnoczi */ 1437d83947acSStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1438d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1439d83947acSStefan Hajnoczi 1440f4658285SStefan Hajnoczi do { 1441f4658285SStefan Hajnoczi retry = false; 1442f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 1443d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 1444d83947acSStefan Hajnoczi cluster_nb_sectors)) { 14455f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 14465f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 14475f8b6491SStefan Hajnoczi * never happen since it means deadlock. 14485f8b6491SStefan Hajnoczi */ 14495f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 14505f8b6491SStefan Hajnoczi 1451f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 1452f4658285SStefan Hajnoczi retry = true; 1453f4658285SStefan Hajnoczi break; 1454f4658285SStefan Hajnoczi } 1455f4658285SStefan Hajnoczi } 1456f4658285SStefan Hajnoczi } while (retry); 1457f4658285SStefan Hajnoczi } 1458f4658285SStefan Hajnoczi 1459756e6736SKevin Wolf /* 1460756e6736SKevin Wolf * Return values: 1461756e6736SKevin Wolf * 0 - success 1462756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 1463756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 1464756e6736SKevin Wolf * image file header 1465756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 1466756e6736SKevin Wolf */ 1467756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 1468756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1469756e6736SKevin Wolf { 1470756e6736SKevin Wolf BlockDriver *drv = bs->drv; 1471469ef350SPaolo Bonzini int ret; 1472756e6736SKevin Wolf 14735f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 14745f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 14755f377794SPaolo Bonzini return -EINVAL; 14765f377794SPaolo Bonzini } 14775f377794SPaolo Bonzini 1478756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 1479469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 1480756e6736SKevin Wolf } else { 1481469ef350SPaolo Bonzini ret = -ENOTSUP; 1482756e6736SKevin Wolf } 1483469ef350SPaolo Bonzini 1484469ef350SPaolo Bonzini if (ret == 0) { 1485469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1486469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1487469ef350SPaolo Bonzini } 1488469ef350SPaolo Bonzini return ret; 1489756e6736SKevin Wolf } 1490756e6736SKevin Wolf 149171d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 149271d0770cSaliguori size_t size) 149371d0770cSaliguori { 149471d0770cSaliguori int64_t len; 149571d0770cSaliguori 149671d0770cSaliguori if (!bdrv_is_inserted(bs)) 149771d0770cSaliguori return -ENOMEDIUM; 149871d0770cSaliguori 149971d0770cSaliguori if (bs->growable) 150071d0770cSaliguori return 0; 150171d0770cSaliguori 150271d0770cSaliguori len = bdrv_getlength(bs); 150371d0770cSaliguori 1504fbb7b4e0SKevin Wolf if (offset < 0) 1505fbb7b4e0SKevin Wolf return -EIO; 1506fbb7b4e0SKevin Wolf 1507fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 150871d0770cSaliguori return -EIO; 150971d0770cSaliguori 151071d0770cSaliguori return 0; 151171d0770cSaliguori } 151271d0770cSaliguori 151371d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 151471d0770cSaliguori int nb_sectors) 151571d0770cSaliguori { 1516eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1517eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 151871d0770cSaliguori } 151971d0770cSaliguori 15201c9805a3SStefan Hajnoczi typedef struct RwCo { 15211c9805a3SStefan Hajnoczi BlockDriverState *bs; 15221c9805a3SStefan Hajnoczi int64_t sector_num; 15231c9805a3SStefan Hajnoczi int nb_sectors; 15241c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 15251c9805a3SStefan Hajnoczi bool is_write; 15261c9805a3SStefan Hajnoczi int ret; 15271c9805a3SStefan Hajnoczi } RwCo; 15281c9805a3SStefan Hajnoczi 15291c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 1530fc01f7e7Sbellard { 15311c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 1532fc01f7e7Sbellard 15331c9805a3SStefan Hajnoczi if (!rwco->is_write) { 15341c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 1535470c0504SStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 15361c9805a3SStefan Hajnoczi } else { 15371c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 1538f08f2ddaSStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 15391c9805a3SStefan Hajnoczi } 15401c9805a3SStefan Hajnoczi } 1541e7a8a783SKevin Wolf 15421c9805a3SStefan Hajnoczi /* 15431c9805a3SStefan Hajnoczi * Process a synchronous request using coroutines 15441c9805a3SStefan Hajnoczi */ 15451c9805a3SStefan Hajnoczi static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 15461c9805a3SStefan Hajnoczi int nb_sectors, bool is_write) 15471c9805a3SStefan Hajnoczi { 1548e7a8a783SKevin Wolf QEMUIOVector qiov; 1549e7a8a783SKevin Wolf struct iovec iov = { 1550e7a8a783SKevin Wolf .iov_base = (void *)buf, 1551e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 1552e7a8a783SKevin Wolf }; 15531c9805a3SStefan Hajnoczi Coroutine *co; 15541c9805a3SStefan Hajnoczi RwCo rwco = { 15551c9805a3SStefan Hajnoczi .bs = bs, 15561c9805a3SStefan Hajnoczi .sector_num = sector_num, 15571c9805a3SStefan Hajnoczi .nb_sectors = nb_sectors, 15581c9805a3SStefan Hajnoczi .qiov = &qiov, 15591c9805a3SStefan Hajnoczi .is_write = is_write, 15601c9805a3SStefan Hajnoczi .ret = NOT_DONE, 15611c9805a3SStefan Hajnoczi }; 1562e7a8a783SKevin Wolf 1563e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 15641c9805a3SStefan Hajnoczi 1565498e386cSZhi Yong Wu /** 1566498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 1567498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 1568498e386cSZhi Yong Wu * if it has been enabled. 1569498e386cSZhi Yong Wu */ 1570498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 1571498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 1572498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 1573498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 1574498e386cSZhi Yong Wu } 1575498e386cSZhi Yong Wu 15761c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 15771c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 15781c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 15791c9805a3SStefan Hajnoczi } else { 15801c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 15811c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 15821c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 15831c9805a3SStefan Hajnoczi qemu_aio_wait(); 15841c9805a3SStefan Hajnoczi } 15851c9805a3SStefan Hajnoczi } 15861c9805a3SStefan Hajnoczi return rwco.ret; 1587e7a8a783SKevin Wolf } 1588e7a8a783SKevin Wolf 15891c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 15901c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 15911c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 15921c9805a3SStefan Hajnoczi { 15931c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); 159483f64091Sbellard } 1595fc01f7e7Sbellard 159671df14fcSPaolo Bonzini #define BITS_PER_LONG (sizeof(unsigned long) * 8) 159771df14fcSPaolo Bonzini 15987cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 15997cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 16007cd1e32aSlirans@il.ibm.com { 16017cd1e32aSlirans@il.ibm.com int64_t start, end; 1602c6d22830SJan Kiszka unsigned long val, idx, bit; 1603a55eb92cSJan Kiszka 16046ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 1605c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 16067cd1e32aSlirans@il.ibm.com 16077cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 160871df14fcSPaolo Bonzini idx = start / BITS_PER_LONG; 160971df14fcSPaolo Bonzini bit = start % BITS_PER_LONG; 1610c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 1611c6d22830SJan Kiszka if (dirty) { 16126d59fec1SMarcelo Tosatti if (!(val & (1UL << bit))) { 1613aaa0eb75SLiran Schour bs->dirty_count++; 16146d59fec1SMarcelo Tosatti val |= 1UL << bit; 1615aaa0eb75SLiran Schour } 1616c6d22830SJan Kiszka } else { 16176d59fec1SMarcelo Tosatti if (val & (1UL << bit)) { 1618aaa0eb75SLiran Schour bs->dirty_count--; 16196d59fec1SMarcelo Tosatti val &= ~(1UL << bit); 1620c6d22830SJan Kiszka } 1621aaa0eb75SLiran Schour } 1622c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 16237cd1e32aSlirans@il.ibm.com } 16247cd1e32aSlirans@il.ibm.com } 16257cd1e32aSlirans@il.ibm.com 162619cb3738Sbellard /* Return < 0 if error. Important errors are: 162719cb3738Sbellard -EIO generic I/O error (may happen for all errors) 162819cb3738Sbellard -ENOMEDIUM No media inserted. 162919cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 163019cb3738Sbellard -EACCES Trying to write a read-only device 163119cb3738Sbellard */ 1632fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 1633fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 1634fc01f7e7Sbellard { 16351c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); 163683f64091Sbellard } 163783f64091Sbellard 1638eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 1639eda578e5Saliguori void *buf, int count1) 164083f64091Sbellard { 16416ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 164283f64091Sbellard int len, nb_sectors, count; 164383f64091Sbellard int64_t sector_num; 16449a8c4cceSKevin Wolf int ret; 164583f64091Sbellard 164683f64091Sbellard count = count1; 164783f64091Sbellard /* first read to align to sector start */ 16486ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 164983f64091Sbellard if (len > count) 165083f64091Sbellard len = count; 16516ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 165283f64091Sbellard if (len > 0) { 16539a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16549a8c4cceSKevin Wolf return ret; 16556ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 165683f64091Sbellard count -= len; 165783f64091Sbellard if (count == 0) 165883f64091Sbellard return count1; 165983f64091Sbellard sector_num++; 166083f64091Sbellard buf += len; 166183f64091Sbellard } 166283f64091Sbellard 166383f64091Sbellard /* read the sectors "in place" */ 16646ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 166583f64091Sbellard if (nb_sectors > 0) { 16669a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 16679a8c4cceSKevin Wolf return ret; 166883f64091Sbellard sector_num += nb_sectors; 16696ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 167083f64091Sbellard buf += len; 167183f64091Sbellard count -= len; 167283f64091Sbellard } 167383f64091Sbellard 167483f64091Sbellard /* add data from the last sector */ 167583f64091Sbellard if (count > 0) { 16769a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16779a8c4cceSKevin Wolf return ret; 167883f64091Sbellard memcpy(buf, tmp_buf, count); 167983f64091Sbellard } 168083f64091Sbellard return count1; 168183f64091Sbellard } 168283f64091Sbellard 1683eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 1684eda578e5Saliguori const void *buf, int count1) 168583f64091Sbellard { 16866ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 168783f64091Sbellard int len, nb_sectors, count; 168883f64091Sbellard int64_t sector_num; 16899a8c4cceSKevin Wolf int ret; 169083f64091Sbellard 169183f64091Sbellard count = count1; 169283f64091Sbellard /* first write to align to sector start */ 16936ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 169483f64091Sbellard if (len > count) 169583f64091Sbellard len = count; 16966ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 169783f64091Sbellard if (len > 0) { 16989a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 16999a8c4cceSKevin Wolf return ret; 17006ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 17019a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 17029a8c4cceSKevin Wolf return ret; 170383f64091Sbellard count -= len; 170483f64091Sbellard if (count == 0) 170583f64091Sbellard return count1; 170683f64091Sbellard sector_num++; 170783f64091Sbellard buf += len; 170883f64091Sbellard } 170983f64091Sbellard 171083f64091Sbellard /* write the sectors "in place" */ 17116ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 171283f64091Sbellard if (nb_sectors > 0) { 17139a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 17149a8c4cceSKevin Wolf return ret; 171583f64091Sbellard sector_num += nb_sectors; 17166ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 171783f64091Sbellard buf += len; 171883f64091Sbellard count -= len; 171983f64091Sbellard } 172083f64091Sbellard 172183f64091Sbellard /* add data from the last sector */ 172283f64091Sbellard if (count > 0) { 17239a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 17249a8c4cceSKevin Wolf return ret; 172583f64091Sbellard memcpy(tmp_buf, buf, count); 17269a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 17279a8c4cceSKevin Wolf return ret; 172883f64091Sbellard } 172983f64091Sbellard return count1; 173083f64091Sbellard } 173183f64091Sbellard 1732f08145feSKevin Wolf /* 1733f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1734f08145feSKevin Wolf * request (acts as a barrier) 1735f08145feSKevin Wolf * 1736f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1737f08145feSKevin Wolf */ 1738f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1739f08145feSKevin Wolf const void *buf, int count) 1740f08145feSKevin Wolf { 1741f08145feSKevin Wolf int ret; 1742f08145feSKevin Wolf 1743f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1744f08145feSKevin Wolf if (ret < 0) { 1745f08145feSKevin Wolf return ret; 1746f08145feSKevin Wolf } 1747f08145feSKevin Wolf 174892196b2fSStefan Hajnoczi /* No flush needed for cache modes that use O_DSYNC */ 174992196b2fSStefan Hajnoczi if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { 1750f08145feSKevin Wolf bdrv_flush(bs); 1751f08145feSKevin Wolf } 1752f08145feSKevin Wolf 1753f08145feSKevin Wolf return 0; 1754f08145feSKevin Wolf } 1755f08145feSKevin Wolf 1756470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 1757ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1758ab185921SStefan Hajnoczi { 1759ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 1760ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 1761ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 1762ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 1763ab185921SStefan Hajnoczi */ 1764ab185921SStefan Hajnoczi void *bounce_buffer; 1765ab185921SStefan Hajnoczi 176679c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 1767ab185921SStefan Hajnoczi struct iovec iov; 1768ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 1769ab185921SStefan Hajnoczi int64_t cluster_sector_num; 1770ab185921SStefan Hajnoczi int cluster_nb_sectors; 1771ab185921SStefan Hajnoczi size_t skip_bytes; 1772ab185921SStefan Hajnoczi int ret; 1773ab185921SStefan Hajnoczi 1774ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 1775ab185921SStefan Hajnoczi * allocating cluster in the image file. 1776ab185921SStefan Hajnoczi */ 1777ab185921SStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1778ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1779ab185921SStefan Hajnoczi 1780470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 1781ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 1782ab185921SStefan Hajnoczi 1783ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 1784ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 1785ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 1786ab185921SStefan Hajnoczi 178779c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 1788ab185921SStefan Hajnoczi &bounce_qiov); 1789ab185921SStefan Hajnoczi if (ret < 0) { 1790ab185921SStefan Hajnoczi goto err; 1791ab185921SStefan Hajnoczi } 1792ab185921SStefan Hajnoczi 179379c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 179479c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 1795621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 179679c053bdSStefan Hajnoczi cluster_nb_sectors); 179779c053bdSStefan Hajnoczi } else { 179879c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 1799ab185921SStefan Hajnoczi &bounce_qiov); 180079c053bdSStefan Hajnoczi } 180179c053bdSStefan Hajnoczi 1802ab185921SStefan Hajnoczi if (ret < 0) { 1803ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 1804ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 1805ab185921SStefan Hajnoczi * Simply report it in all cases. 1806ab185921SStefan Hajnoczi */ 1807ab185921SStefan Hajnoczi goto err; 1808ab185921SStefan Hajnoczi } 1809ab185921SStefan Hajnoczi 1810ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 1811ab185921SStefan Hajnoczi qemu_iovec_from_buffer(qiov, bounce_buffer + skip_bytes, 1812ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 1813ab185921SStefan Hajnoczi 1814ab185921SStefan Hajnoczi err: 1815ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 1816ab185921SStefan Hajnoczi return ret; 1817ab185921SStefan Hajnoczi } 1818ab185921SStefan Hajnoczi 1819c5fbe571SStefan Hajnoczi /* 1820c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 1821c5fbe571SStefan Hajnoczi */ 1822c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 1823470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1824470c0504SStefan Hajnoczi BdrvRequestFlags flags) 1825da1fa91dSKevin Wolf { 1826da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 1827dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 1828dbffbdcfSStefan Hajnoczi int ret; 1829da1fa91dSKevin Wolf 1830da1fa91dSKevin Wolf if (!drv) { 1831da1fa91dSKevin Wolf return -ENOMEDIUM; 1832da1fa91dSKevin Wolf } 1833da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1834da1fa91dSKevin Wolf return -EIO; 1835da1fa91dSKevin Wolf } 1836da1fa91dSKevin Wolf 183798f90dbaSZhi Yong Wu /* throttling disk read I/O */ 183898f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 183998f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, false, nb_sectors); 184098f90dbaSZhi Yong Wu } 184198f90dbaSZhi Yong Wu 1842f4658285SStefan Hajnoczi if (bs->copy_on_read) { 1843470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 1844470c0504SStefan Hajnoczi } 1845470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1846470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 1847470c0504SStefan Hajnoczi } 1848470c0504SStefan Hajnoczi 1849470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1850f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1851f4658285SStefan Hajnoczi } 1852f4658285SStefan Hajnoczi 1853dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 1854ab185921SStefan Hajnoczi 1855470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1856ab185921SStefan Hajnoczi int pnum; 1857ab185921SStefan Hajnoczi 1858ab185921SStefan Hajnoczi ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); 1859ab185921SStefan Hajnoczi if (ret < 0) { 1860ab185921SStefan Hajnoczi goto out; 1861ab185921SStefan Hajnoczi } 1862ab185921SStefan Hajnoczi 1863ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 1864470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 1865ab185921SStefan Hajnoczi goto out; 1866ab185921SStefan Hajnoczi } 1867ab185921SStefan Hajnoczi } 1868ab185921SStefan Hajnoczi 1869dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 1870ab185921SStefan Hajnoczi 1871ab185921SStefan Hajnoczi out: 1872dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1873470c0504SStefan Hajnoczi 1874470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1875470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 1876470c0504SStefan Hajnoczi } 1877470c0504SStefan Hajnoczi 1878dbffbdcfSStefan Hajnoczi return ret; 1879da1fa91dSKevin Wolf } 1880da1fa91dSKevin Wolf 1881c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 1882da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 1883da1fa91dSKevin Wolf { 1884c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 1885da1fa91dSKevin Wolf 1886470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 1887470c0504SStefan Hajnoczi } 1888470c0504SStefan Hajnoczi 1889470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 1890470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1891470c0504SStefan Hajnoczi { 1892470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 1893470c0504SStefan Hajnoczi 1894470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 1895470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 1896c5fbe571SStefan Hajnoczi } 1897c5fbe571SStefan Hajnoczi 1898f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 1899f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1900f08f2ddaSStefan Hajnoczi { 1901f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 1902f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 1903f08f2ddaSStefan Hajnoczi struct iovec iov; 1904f08f2ddaSStefan Hajnoczi int ret; 1905f08f2ddaSStefan Hajnoczi 1906621f0589SKevin Wolf /* TODO Emulate only part of misaligned requests instead of letting block 1907621f0589SKevin Wolf * drivers return -ENOTSUP and emulate everything */ 1908621f0589SKevin Wolf 1909f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 1910f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 1911621f0589SKevin Wolf ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1912621f0589SKevin Wolf if (ret != -ENOTSUP) { 1913621f0589SKevin Wolf return ret; 1914621f0589SKevin Wolf } 1915f08f2ddaSStefan Hajnoczi } 1916f08f2ddaSStefan Hajnoczi 1917f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 1918f08f2ddaSStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 1919f08f2ddaSStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 1920f08f2ddaSStefan Hajnoczi memset(iov.iov_base, 0, iov.iov_len); 1921f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 1922f08f2ddaSStefan Hajnoczi 1923f08f2ddaSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 1924f08f2ddaSStefan Hajnoczi 1925f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 1926f08f2ddaSStefan Hajnoczi return ret; 1927f08f2ddaSStefan Hajnoczi } 1928f08f2ddaSStefan Hajnoczi 1929c5fbe571SStefan Hajnoczi /* 1930c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 1931c5fbe571SStefan Hajnoczi */ 1932c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 1933f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1934f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 1935c5fbe571SStefan Hajnoczi { 1936c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 1937dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 19386b7cb247SStefan Hajnoczi int ret; 1939da1fa91dSKevin Wolf 1940da1fa91dSKevin Wolf if (!bs->drv) { 1941da1fa91dSKevin Wolf return -ENOMEDIUM; 1942da1fa91dSKevin Wolf } 1943da1fa91dSKevin Wolf if (bs->read_only) { 1944da1fa91dSKevin Wolf return -EACCES; 1945da1fa91dSKevin Wolf } 1946da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1947da1fa91dSKevin Wolf return -EIO; 1948da1fa91dSKevin Wolf } 1949da1fa91dSKevin Wolf 195098f90dbaSZhi Yong Wu /* throttling disk write I/O */ 195198f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 195298f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, true, nb_sectors); 195398f90dbaSZhi Yong Wu } 195498f90dbaSZhi Yong Wu 1955470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1956f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1957f4658285SStefan Hajnoczi } 1958f4658285SStefan Hajnoczi 1959dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 1960dbffbdcfSStefan Hajnoczi 1961f08f2ddaSStefan Hajnoczi if (flags & BDRV_REQ_ZERO_WRITE) { 1962f08f2ddaSStefan Hajnoczi ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); 1963f08f2ddaSStefan Hajnoczi } else { 19646b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 1965f08f2ddaSStefan Hajnoczi } 19666b7cb247SStefan Hajnoczi 1967da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 1968da1fa91dSKevin Wolf set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 1969da1fa91dSKevin Wolf } 1970da1fa91dSKevin Wolf 1971da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 1972da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 1973da1fa91dSKevin Wolf } 1974da1fa91dSKevin Wolf 1975dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1976dbffbdcfSStefan Hajnoczi 19776b7cb247SStefan Hajnoczi return ret; 1978da1fa91dSKevin Wolf } 1979da1fa91dSKevin Wolf 1980c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 1981c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 1982c5fbe571SStefan Hajnoczi { 1983c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 1984c5fbe571SStefan Hajnoczi 1985f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 1986f08f2ddaSStefan Hajnoczi } 1987f08f2ddaSStefan Hajnoczi 1988f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 1989f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1990f08f2ddaSStefan Hajnoczi { 1991f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1992f08f2ddaSStefan Hajnoczi 1993f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 1994f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE); 1995c5fbe571SStefan Hajnoczi } 1996c5fbe571SStefan Hajnoczi 199783f64091Sbellard /** 199883f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 199983f64091Sbellard */ 200083f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 200183f64091Sbellard { 200283f64091Sbellard BlockDriver *drv = bs->drv; 200351762288SStefan Hajnoczi int ret; 200483f64091Sbellard if (!drv) 200519cb3738Sbellard return -ENOMEDIUM; 200683f64091Sbellard if (!drv->bdrv_truncate) 200783f64091Sbellard return -ENOTSUP; 200859f2689dSNaphtali Sprei if (bs->read_only) 200959f2689dSNaphtali Sprei return -EACCES; 20108591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 20118591675fSMarcelo Tosatti return -EBUSY; 201251762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 201351762288SStefan Hajnoczi if (ret == 0) { 201451762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2015145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 201651762288SStefan Hajnoczi } 201751762288SStefan Hajnoczi return ret; 201883f64091Sbellard } 201983f64091Sbellard 202083f64091Sbellard /** 20214a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 20224a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 20234a1d5e1fSFam Zheng */ 20244a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 20254a1d5e1fSFam Zheng { 20264a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 20274a1d5e1fSFam Zheng if (!drv) { 20284a1d5e1fSFam Zheng return -ENOMEDIUM; 20294a1d5e1fSFam Zheng } 20304a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 20314a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 20324a1d5e1fSFam Zheng } 20334a1d5e1fSFam Zheng if (bs->file) { 20344a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 20354a1d5e1fSFam Zheng } 20364a1d5e1fSFam Zheng return -ENOTSUP; 20374a1d5e1fSFam Zheng } 20384a1d5e1fSFam Zheng 20394a1d5e1fSFam Zheng /** 204083f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 204183f64091Sbellard */ 204283f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 204383f64091Sbellard { 204483f64091Sbellard BlockDriver *drv = bs->drv; 204583f64091Sbellard if (!drv) 204619cb3738Sbellard return -ENOMEDIUM; 204751762288SStefan Hajnoczi 20482c6942faSMarkus Armbruster if (bs->growable || bdrv_dev_has_removable_media(bs)) { 204946a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 205083f64091Sbellard return drv->bdrv_getlength(bs); 2051fc01f7e7Sbellard } 205246a4e4e6SStefan Hajnoczi } 205346a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 205446a4e4e6SStefan Hajnoczi } 2055fc01f7e7Sbellard 205619cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 205796b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2058fc01f7e7Sbellard { 205919cb3738Sbellard int64_t length; 206019cb3738Sbellard length = bdrv_getlength(bs); 206119cb3738Sbellard if (length < 0) 206219cb3738Sbellard length = 0; 206319cb3738Sbellard else 20646ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 206519cb3738Sbellard *nb_sectors_ptr = length; 2066fc01f7e7Sbellard } 2067cf98951bSbellard 2068f3d54fc4Saliguori struct partition { 2069f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 2070f3d54fc4Saliguori uint8_t head; /* starting head */ 2071f3d54fc4Saliguori uint8_t sector; /* starting sector */ 2072f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 2073f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 2074f3d54fc4Saliguori uint8_t end_head; /* end head */ 2075f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 2076f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 2077f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 2078f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 2079541dc0d4SStefan Weil } QEMU_PACKED; 2080f3d54fc4Saliguori 2081f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 2082f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 2083f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 2084f3d54fc4Saliguori { 2085eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 2086f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 2087f3d54fc4Saliguori struct partition *p; 2088f3d54fc4Saliguori uint32_t nr_sects; 2089a38131b6Sblueswir1 uint64_t nb_sectors; 2090498e386cSZhi Yong Wu bool enabled; 2091f3d54fc4Saliguori 2092f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 2093f3d54fc4Saliguori 2094498e386cSZhi Yong Wu /** 2095498e386cSZhi Yong Wu * The function will be invoked during startup not only in sync I/O mode, 2096498e386cSZhi Yong Wu * but also in async I/O mode. So the I/O throttling function has to 2097498e386cSZhi Yong Wu * be disabled temporarily here, not permanently. 2098498e386cSZhi Yong Wu */ 2099498e386cSZhi Yong Wu enabled = bs->io_limits_enabled; 2100498e386cSZhi Yong Wu bs->io_limits_enabled = false; 2101f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 2102498e386cSZhi Yong Wu bs->io_limits_enabled = enabled; 2103f3d54fc4Saliguori if (ret < 0) 2104f3d54fc4Saliguori return -1; 2105f3d54fc4Saliguori /* test msdos magic */ 2106f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 2107f3d54fc4Saliguori return -1; 2108f3d54fc4Saliguori for(i = 0; i < 4; i++) { 2109f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 2110f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 2111f3d54fc4Saliguori if (nr_sects && p->end_head) { 2112f3d54fc4Saliguori /* We make the assumption that the partition terminates on 2113f3d54fc4Saliguori a cylinder boundary */ 2114f3d54fc4Saliguori heads = p->end_head + 1; 2115f3d54fc4Saliguori sectors = p->end_sector & 63; 2116f3d54fc4Saliguori if (sectors == 0) 2117f3d54fc4Saliguori continue; 2118f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 2119f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 2120f3d54fc4Saliguori continue; 2121f3d54fc4Saliguori *pheads = heads; 2122f3d54fc4Saliguori *psectors = sectors; 2123f3d54fc4Saliguori *pcylinders = cylinders; 2124f3d54fc4Saliguori #if 0 2125f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 2126f3d54fc4Saliguori cylinders, heads, sectors); 2127f3d54fc4Saliguori #endif 2128f3d54fc4Saliguori return 0; 2129f3d54fc4Saliguori } 2130f3d54fc4Saliguori } 2131f3d54fc4Saliguori return -1; 2132f3d54fc4Saliguori } 2133f3d54fc4Saliguori 2134f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 2135f3d54fc4Saliguori { 2136f3d54fc4Saliguori int translation, lba_detected = 0; 2137f3d54fc4Saliguori int cylinders, heads, secs; 2138a38131b6Sblueswir1 uint64_t nb_sectors; 2139f3d54fc4Saliguori 2140f3d54fc4Saliguori /* if a geometry hint is available, use it */ 2141f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 2142f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 2143f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 2144f3d54fc4Saliguori if (cylinders != 0) { 2145f3d54fc4Saliguori *pcyls = cylinders; 2146f3d54fc4Saliguori *pheads = heads; 2147f3d54fc4Saliguori *psecs = secs; 2148f3d54fc4Saliguori } else { 2149f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 2150f3d54fc4Saliguori if (heads > 16) { 2151f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 2152f3d54fc4Saliguori translation was active, so the default 2153f3d54fc4Saliguori hardware geometry is OK */ 2154f3d54fc4Saliguori lba_detected = 1; 2155f3d54fc4Saliguori goto default_geometry; 2156f3d54fc4Saliguori } else { 2157f3d54fc4Saliguori *pcyls = cylinders; 2158f3d54fc4Saliguori *pheads = heads; 2159f3d54fc4Saliguori *psecs = secs; 2160f3d54fc4Saliguori /* disable any translation to be in sync with 2161f3d54fc4Saliguori the logical geometry */ 2162f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 2163f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2164f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 2165f3d54fc4Saliguori } 2166f3d54fc4Saliguori } 2167f3d54fc4Saliguori } else { 2168f3d54fc4Saliguori default_geometry: 2169f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 2170f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 2171f3d54fc4Saliguori 2172f3d54fc4Saliguori if (cylinders > 16383) 2173f3d54fc4Saliguori cylinders = 16383; 2174f3d54fc4Saliguori else if (cylinders < 2) 2175f3d54fc4Saliguori cylinders = 2; 2176f3d54fc4Saliguori *pcyls = cylinders; 2177f3d54fc4Saliguori *pheads = 16; 2178f3d54fc4Saliguori *psecs = 63; 2179f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 2180f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 2181f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2182f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 2183f3d54fc4Saliguori } else { 2184f3d54fc4Saliguori bdrv_set_translation_hint(bs, 2185f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 2186f3d54fc4Saliguori } 2187f3d54fc4Saliguori } 2188f3d54fc4Saliguori } 2189f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 2190f3d54fc4Saliguori } 2191f3d54fc4Saliguori } 2192f3d54fc4Saliguori 2193b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 2194b338082bSbellard int cyls, int heads, int secs) 2195b338082bSbellard { 2196b338082bSbellard bs->cyls = cyls; 2197b338082bSbellard bs->heads = heads; 2198b338082bSbellard bs->secs = secs; 2199b338082bSbellard } 2200b338082bSbellard 220146d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 220246d4767dSbellard { 220346d4767dSbellard bs->translation = translation; 220446d4767dSbellard } 220546d4767dSbellard 2206b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 2207b338082bSbellard int *pcyls, int *pheads, int *psecs) 2208b338082bSbellard { 2209b338082bSbellard *pcyls = bs->cyls; 2210b338082bSbellard *pheads = bs->heads; 2211b338082bSbellard *psecs = bs->secs; 2212b338082bSbellard } 2213b338082bSbellard 22140563e191SZhi Yong Wu /* throttling disk io limits */ 22150563e191SZhi Yong Wu void bdrv_set_io_limits(BlockDriverState *bs, 22160563e191SZhi Yong Wu BlockIOLimit *io_limits) 22170563e191SZhi Yong Wu { 22180563e191SZhi Yong Wu bs->io_limits = *io_limits; 22190563e191SZhi Yong Wu bs->io_limits_enabled = bdrv_io_limits_enabled(bs); 22200563e191SZhi Yong Wu } 22210563e191SZhi Yong Wu 22225bbdbb46SBlue Swirl /* Recognize floppy formats */ 22235bbdbb46SBlue Swirl typedef struct FDFormat { 22245bbdbb46SBlue Swirl FDriveType drive; 22255bbdbb46SBlue Swirl uint8_t last_sect; 22265bbdbb46SBlue Swirl uint8_t max_track; 22275bbdbb46SBlue Swirl uint8_t max_head; 2228f8d3d128SHervé Poussineau FDriveRate rate; 22295bbdbb46SBlue Swirl } FDFormat; 22305bbdbb46SBlue Swirl 22315bbdbb46SBlue Swirl static const FDFormat fd_formats[] = { 22325bbdbb46SBlue Swirl /* First entry is default format */ 22335bbdbb46SBlue Swirl /* 1.44 MB 3"1/2 floppy disks */ 2234f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 18, 80, 1, FDRIVE_RATE_500K, }, 2235f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 20, 80, 1, FDRIVE_RATE_500K, }, 2236f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 80, 1, FDRIVE_RATE_500K, }, 2237f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 82, 1, FDRIVE_RATE_500K, }, 2238f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 83, 1, FDRIVE_RATE_500K, }, 2239f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 22, 80, 1, FDRIVE_RATE_500K, }, 2240f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 23, 80, 1, FDRIVE_RATE_500K, }, 2241f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 24, 80, 1, FDRIVE_RATE_500K, }, 22425bbdbb46SBlue Swirl /* 2.88 MB 3"1/2 floppy disks */ 2243f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 36, 80, 1, FDRIVE_RATE_1M, }, 2244f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 39, 80, 1, FDRIVE_RATE_1M, }, 2245f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 40, 80, 1, FDRIVE_RATE_1M, }, 2246f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 44, 80, 1, FDRIVE_RATE_1M, }, 2247f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 48, 80, 1, FDRIVE_RATE_1M, }, 22485bbdbb46SBlue Swirl /* 720 kB 3"1/2 floppy disks */ 2249f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 9, 80, 1, FDRIVE_RATE_250K, }, 2250f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 80, 1, FDRIVE_RATE_250K, }, 2251f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 82, 1, FDRIVE_RATE_250K, }, 2252f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 83, 1, FDRIVE_RATE_250K, }, 2253f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 13, 80, 1, FDRIVE_RATE_250K, }, 2254f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 14, 80, 1, FDRIVE_RATE_250K, }, 22555bbdbb46SBlue Swirl /* 1.2 MB 5"1/4 floppy disks */ 2256f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 15, 80, 1, FDRIVE_RATE_500K, }, 2257f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 80, 1, FDRIVE_RATE_500K, }, 2258f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 82, 1, FDRIVE_RATE_500K, }, 2259f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 83, 1, FDRIVE_RATE_500K, }, 2260f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 20, 80, 1, FDRIVE_RATE_500K, }, 22615bbdbb46SBlue Swirl /* 720 kB 5"1/4 floppy disks */ 2262f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 80, 1, FDRIVE_RATE_250K, }, 2263f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 11, 80, 1, FDRIVE_RATE_250K, }, 22645bbdbb46SBlue Swirl /* 360 kB 5"1/4 floppy disks */ 2265f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 40, 1, FDRIVE_RATE_300K, }, 2266f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 40, 0, FDRIVE_RATE_300K, }, 2267f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 10, 41, 1, FDRIVE_RATE_300K, }, 2268f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 10, 42, 1, FDRIVE_RATE_300K, }, 22695bbdbb46SBlue Swirl /* 320 kB 5"1/4 floppy disks */ 2270f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 8, 40, 1, FDRIVE_RATE_250K, }, 2271f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 8, 40, 0, FDRIVE_RATE_250K, }, 22725bbdbb46SBlue Swirl /* 360 kB must match 5"1/4 better than 3"1/2... */ 2273f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 9, 80, 0, FDRIVE_RATE_250K, }, 22745bbdbb46SBlue Swirl /* end */ 2275f8d3d128SHervé Poussineau { FDRIVE_DRV_NONE, -1, -1, 0, 0, }, 22765bbdbb46SBlue Swirl }; 22775bbdbb46SBlue Swirl 22785bbdbb46SBlue Swirl void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, 22795bbdbb46SBlue Swirl int *max_track, int *last_sect, 2280f8d3d128SHervé Poussineau FDriveType drive_in, FDriveType *drive, 2281f8d3d128SHervé Poussineau FDriveRate *rate) 22825bbdbb46SBlue Swirl { 22835bbdbb46SBlue Swirl const FDFormat *parse; 22845bbdbb46SBlue Swirl uint64_t nb_sectors, size; 22855bbdbb46SBlue Swirl int i, first_match, match; 22865bbdbb46SBlue Swirl 22875bbdbb46SBlue Swirl bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); 22885bbdbb46SBlue Swirl if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { 22895bbdbb46SBlue Swirl /* User defined disk */ 2290f8d3d128SHervé Poussineau *rate = FDRIVE_RATE_500K; 22915bbdbb46SBlue Swirl } else { 22925bbdbb46SBlue Swirl bdrv_get_geometry(bs, &nb_sectors); 22935bbdbb46SBlue Swirl match = -1; 22945bbdbb46SBlue Swirl first_match = -1; 22955bbdbb46SBlue Swirl for (i = 0; ; i++) { 22965bbdbb46SBlue Swirl parse = &fd_formats[i]; 22975bbdbb46SBlue Swirl if (parse->drive == FDRIVE_DRV_NONE) { 22985bbdbb46SBlue Swirl break; 22995bbdbb46SBlue Swirl } 23005bbdbb46SBlue Swirl if (drive_in == parse->drive || 23015bbdbb46SBlue Swirl drive_in == FDRIVE_DRV_NONE) { 23025bbdbb46SBlue Swirl size = (parse->max_head + 1) * parse->max_track * 23035bbdbb46SBlue Swirl parse->last_sect; 23045bbdbb46SBlue Swirl if (nb_sectors == size) { 23055bbdbb46SBlue Swirl match = i; 23065bbdbb46SBlue Swirl break; 23075bbdbb46SBlue Swirl } 23085bbdbb46SBlue Swirl if (first_match == -1) { 23095bbdbb46SBlue Swirl first_match = i; 23105bbdbb46SBlue Swirl } 23115bbdbb46SBlue Swirl } 23125bbdbb46SBlue Swirl } 23135bbdbb46SBlue Swirl if (match == -1) { 23145bbdbb46SBlue Swirl if (first_match == -1) { 23155bbdbb46SBlue Swirl match = 1; 23165bbdbb46SBlue Swirl } else { 23175bbdbb46SBlue Swirl match = first_match; 23185bbdbb46SBlue Swirl } 23195bbdbb46SBlue Swirl parse = &fd_formats[match]; 23205bbdbb46SBlue Swirl } 23215bbdbb46SBlue Swirl *nb_heads = parse->max_head + 1; 23225bbdbb46SBlue Swirl *max_track = parse->max_track; 23235bbdbb46SBlue Swirl *last_sect = parse->last_sect; 23245bbdbb46SBlue Swirl *drive = parse->drive; 2325f8d3d128SHervé Poussineau *rate = parse->rate; 23265bbdbb46SBlue Swirl } 23275bbdbb46SBlue Swirl } 23285bbdbb46SBlue Swirl 232946d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 233046d4767dSbellard { 233146d4767dSbellard return bs->translation; 233246d4767dSbellard } 233346d4767dSbellard 2334abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 2335abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 2336abd7f68dSMarkus Armbruster { 2337abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2338abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2339abd7f68dSMarkus Armbruster } 2340abd7f68dSMarkus Armbruster 2341abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 2342abd7f68dSMarkus Armbruster { 2343abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2344abd7f68dSMarkus Armbruster } 2345abd7f68dSMarkus Armbruster 2346b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2347b338082bSbellard { 2348b338082bSbellard return bs->read_only; 2349b338082bSbellard } 2350b338082bSbellard 2351985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2352985a03b0Sths { 2353985a03b0Sths return bs->sg; 2354985a03b0Sths } 2355985a03b0Sths 2356e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2357e900a7b7SChristoph Hellwig { 2358e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2359e900a7b7SChristoph Hellwig } 2360e900a7b7SChristoph Hellwig 2361ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2362ea2384d3Sbellard { 2363ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2364ea2384d3Sbellard return 1; 2365ea2384d3Sbellard return bs->encrypted; 2366ea2384d3Sbellard } 2367ea2384d3Sbellard 2368c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2369c0f4ce77Saliguori { 2370c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2371c0f4ce77Saliguori 2372c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2373c0f4ce77Saliguori return 1; 2374c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2375c0f4ce77Saliguori } 2376c0f4ce77Saliguori 2377ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2378ea2384d3Sbellard { 2379ea2384d3Sbellard int ret; 2380ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2381ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2382ea2384d3Sbellard if (ret < 0) 2383ea2384d3Sbellard return ret; 2384ea2384d3Sbellard if (!bs->encrypted) 2385ea2384d3Sbellard return 0; 2386ea2384d3Sbellard } 2387fd04a2aeSShahar Havivi if (!bs->encrypted) { 2388fd04a2aeSShahar Havivi return -EINVAL; 2389fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2390fd04a2aeSShahar Havivi return -ENOMEDIUM; 2391fd04a2aeSShahar Havivi } 2392c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2393bb5fc20fSaliguori if (ret < 0) { 2394bb5fc20fSaliguori bs->valid_key = 0; 2395bb5fc20fSaliguori } else if (!bs->valid_key) { 2396bb5fc20fSaliguori bs->valid_key = 1; 2397bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 23987d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 2399bb5fc20fSaliguori } 2400c0f4ce77Saliguori return ret; 2401ea2384d3Sbellard } 2402ea2384d3Sbellard 2403ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 2404ea2384d3Sbellard { 240519cb3738Sbellard if (!bs->drv) { 2406ea2384d3Sbellard buf[0] = '\0'; 2407ea2384d3Sbellard } else { 2408ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 2409ea2384d3Sbellard } 2410ea2384d3Sbellard } 2411ea2384d3Sbellard 2412ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2413ea2384d3Sbellard void *opaque) 2414ea2384d3Sbellard { 2415ea2384d3Sbellard BlockDriver *drv; 2416ea2384d3Sbellard 24178a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2418ea2384d3Sbellard it(opaque, drv->format_name); 2419ea2384d3Sbellard } 2420ea2384d3Sbellard } 2421ea2384d3Sbellard 2422b338082bSbellard BlockDriverState *bdrv_find(const char *name) 2423b338082bSbellard { 2424b338082bSbellard BlockDriverState *bs; 2425b338082bSbellard 24261b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 24271b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 2428b338082bSbellard return bs; 2429b338082bSbellard } 24301b7bdbc1SStefan Hajnoczi } 2431b338082bSbellard return NULL; 2432b338082bSbellard } 2433b338082bSbellard 24342f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 24352f399b0aSMarkus Armbruster { 24362f399b0aSMarkus Armbruster if (!bs) { 24372f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 24382f399b0aSMarkus Armbruster } 24392f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 24402f399b0aSMarkus Armbruster } 24412f399b0aSMarkus Armbruster 244251de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 244381d0912dSbellard { 244481d0912dSbellard BlockDriverState *bs; 244581d0912dSbellard 24461b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 244751de9760Saliguori it(opaque, bs); 244881d0912dSbellard } 244981d0912dSbellard } 245081d0912dSbellard 2451ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 2452ea2384d3Sbellard { 2453ea2384d3Sbellard return bs->device_name; 2454ea2384d3Sbellard } 2455ea2384d3Sbellard 2456c6ca28d6Saliguori void bdrv_flush_all(void) 2457c6ca28d6Saliguori { 2458c6ca28d6Saliguori BlockDriverState *bs; 2459c6ca28d6Saliguori 24601b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2461c6ca28d6Saliguori bdrv_flush(bs); 2462c6ca28d6Saliguori } 24631b7bdbc1SStefan Hajnoczi } 2464c6ca28d6Saliguori 2465f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2466f2feebbdSKevin Wolf { 2467f2feebbdSKevin Wolf assert(bs->drv); 2468f2feebbdSKevin Wolf 2469336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2470336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2471f2feebbdSKevin Wolf } 2472f2feebbdSKevin Wolf 2473f2feebbdSKevin Wolf return 1; 2474f2feebbdSKevin Wolf } 2475f2feebbdSKevin Wolf 2476376ae3f1SStefan Hajnoczi typedef struct BdrvCoIsAllocatedData { 2477376ae3f1SStefan Hajnoczi BlockDriverState *bs; 2478376ae3f1SStefan Hajnoczi int64_t sector_num; 2479376ae3f1SStefan Hajnoczi int nb_sectors; 2480376ae3f1SStefan Hajnoczi int *pnum; 2481376ae3f1SStefan Hajnoczi int ret; 2482376ae3f1SStefan Hajnoczi bool done; 2483376ae3f1SStefan Hajnoczi } BdrvCoIsAllocatedData; 2484376ae3f1SStefan Hajnoczi 2485f58c7b35Sths /* 2486f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 2487f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 2488f58c7b35Sths * hence all their sectors are reported as allocated. 2489f58c7b35Sths * 2490bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 2491bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 2492bd9533e3SStefan Hajnoczi * 2493f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 2494f58c7b35Sths * the specified sector) that are known to be in the same 2495f58c7b35Sths * allocated/unallocated state. 2496f58c7b35Sths * 2497bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2498bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 2499f58c7b35Sths */ 2500060f51c9SStefan Hajnoczi int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, 2501060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 2502f58c7b35Sths { 2503f58c7b35Sths int64_t n; 2504bd9533e3SStefan Hajnoczi 25056aebab14SStefan Hajnoczi if (sector_num >= bs->total_sectors) { 25066aebab14SStefan Hajnoczi *pnum = 0; 25076aebab14SStefan Hajnoczi return 0; 25086aebab14SStefan Hajnoczi } 2509bd9533e3SStefan Hajnoczi 25106aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 2511bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 2512bd9533e3SStefan Hajnoczi nb_sectors = n; 2513bd9533e3SStefan Hajnoczi } 2514bd9533e3SStefan Hajnoczi 2515bd9533e3SStefan Hajnoczi if (!bs->drv->bdrv_co_is_allocated) { 2516bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 25176aebab14SStefan Hajnoczi return 1; 25186aebab14SStefan Hajnoczi } 25196aebab14SStefan Hajnoczi 2520060f51c9SStefan Hajnoczi return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum); 2521060f51c9SStefan Hajnoczi } 2522060f51c9SStefan Hajnoczi 2523060f51c9SStefan Hajnoczi /* Coroutine wrapper for bdrv_is_allocated() */ 2524060f51c9SStefan Hajnoczi static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) 2525060f51c9SStefan Hajnoczi { 2526060f51c9SStefan Hajnoczi BdrvCoIsAllocatedData *data = opaque; 2527060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 2528060f51c9SStefan Hajnoczi 2529060f51c9SStefan Hajnoczi data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, 2530060f51c9SStefan Hajnoczi data->pnum); 2531060f51c9SStefan Hajnoczi data->done = true; 2532060f51c9SStefan Hajnoczi } 2533060f51c9SStefan Hajnoczi 2534060f51c9SStefan Hajnoczi /* 2535060f51c9SStefan Hajnoczi * Synchronous wrapper around bdrv_co_is_allocated(). 2536060f51c9SStefan Hajnoczi * 2537060f51c9SStefan Hajnoczi * See bdrv_co_is_allocated() for details. 2538060f51c9SStefan Hajnoczi */ 2539060f51c9SStefan Hajnoczi int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 2540060f51c9SStefan Hajnoczi int *pnum) 2541060f51c9SStefan Hajnoczi { 2542376ae3f1SStefan Hajnoczi Coroutine *co; 2543376ae3f1SStefan Hajnoczi BdrvCoIsAllocatedData data = { 2544376ae3f1SStefan Hajnoczi .bs = bs, 2545376ae3f1SStefan Hajnoczi .sector_num = sector_num, 2546376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 2547376ae3f1SStefan Hajnoczi .pnum = pnum, 2548376ae3f1SStefan Hajnoczi .done = false, 2549376ae3f1SStefan Hajnoczi }; 2550376ae3f1SStefan Hajnoczi 2551376ae3f1SStefan Hajnoczi co = qemu_coroutine_create(bdrv_is_allocated_co_entry); 2552376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 2553376ae3f1SStefan Hajnoczi while (!data.done) { 2554376ae3f1SStefan Hajnoczi qemu_aio_wait(); 2555376ae3f1SStefan Hajnoczi } 2556376ae3f1SStefan Hajnoczi return data.ret; 2557376ae3f1SStefan Hajnoczi } 2558f58c7b35Sths 2559b2023818SLuiz Capitulino BlockInfoList *qmp_query_block(Error **errp) 2560b338082bSbellard { 2561b2023818SLuiz Capitulino BlockInfoList *head = NULL, *cur_item = NULL; 2562d15e5465SLuiz Capitulino BlockDriverState *bs; 2563d15e5465SLuiz Capitulino 25641b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2565b2023818SLuiz Capitulino BlockInfoList *info = g_malloc0(sizeof(*info)); 2566d15e5465SLuiz Capitulino 2567b2023818SLuiz Capitulino info->value = g_malloc0(sizeof(*info->value)); 2568b2023818SLuiz Capitulino info->value->device = g_strdup(bs->device_name); 2569b2023818SLuiz Capitulino info->value->type = g_strdup("unknown"); 2570b2023818SLuiz Capitulino info->value->locked = bdrv_dev_is_medium_locked(bs); 2571b2023818SLuiz Capitulino info->value->removable = bdrv_dev_has_removable_media(bs); 2572d15e5465SLuiz Capitulino 2573e4def80bSMarkus Armbruster if (bdrv_dev_has_removable_media(bs)) { 2574b2023818SLuiz Capitulino info->value->has_tray_open = true; 2575b2023818SLuiz Capitulino info->value->tray_open = bdrv_dev_is_tray_open(bs); 2576e4def80bSMarkus Armbruster } 2577f04ef601SLuiz Capitulino 2578f04ef601SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 2579b2023818SLuiz Capitulino info->value->has_io_status = true; 2580b2023818SLuiz Capitulino info->value->io_status = bs->iostatus; 2581f04ef601SLuiz Capitulino } 2582f04ef601SLuiz Capitulino 2583d15e5465SLuiz Capitulino if (bs->drv) { 2584b2023818SLuiz Capitulino info->value->has_inserted = true; 2585b2023818SLuiz Capitulino info->value->inserted = g_malloc0(sizeof(*info->value->inserted)); 2586b2023818SLuiz Capitulino info->value->inserted->file = g_strdup(bs->filename); 2587b2023818SLuiz Capitulino info->value->inserted->ro = bs->read_only; 2588b2023818SLuiz Capitulino info->value->inserted->drv = g_strdup(bs->drv->format_name); 2589b2023818SLuiz Capitulino info->value->inserted->encrypted = bs->encrypted; 2590b2023818SLuiz Capitulino if (bs->backing_file[0]) { 2591b2023818SLuiz Capitulino info->value->inserted->has_backing_file = true; 2592b2023818SLuiz Capitulino info->value->inserted->backing_file = g_strdup(bs->backing_file); 2593b2023818SLuiz Capitulino } 2594727f005eSZhi Yong Wu 2595727f005eSZhi Yong Wu if (bs->io_limits_enabled) { 2596727f005eSZhi Yong Wu info->value->inserted->bps = 2597727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 2598727f005eSZhi Yong Wu info->value->inserted->bps_rd = 2599727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; 2600727f005eSZhi Yong Wu info->value->inserted->bps_wr = 2601727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; 2602727f005eSZhi Yong Wu info->value->inserted->iops = 2603727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 2604727f005eSZhi Yong Wu info->value->inserted->iops_rd = 2605727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; 2606727f005eSZhi Yong Wu info->value->inserted->iops_wr = 2607727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; 2608727f005eSZhi Yong Wu } 2609d15e5465SLuiz Capitulino } 2610d15e5465SLuiz Capitulino 2611b2023818SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2612b2023818SLuiz Capitulino if (!cur_item) { 2613b2023818SLuiz Capitulino head = cur_item = info; 2614b2023818SLuiz Capitulino } else { 2615b2023818SLuiz Capitulino cur_item->next = info; 2616b2023818SLuiz Capitulino cur_item = info; 2617d15e5465SLuiz Capitulino } 2618d15e5465SLuiz Capitulino } 2619d15e5465SLuiz Capitulino 2620b2023818SLuiz Capitulino return head; 2621b338082bSbellard } 2622a36e69ddSths 2623f11f57e4SLuiz Capitulino /* Consider exposing this as a full fledged QMP command */ 2624f11f57e4SLuiz Capitulino static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp) 2625a36e69ddSths { 2626f11f57e4SLuiz Capitulino BlockStats *s; 2627218a536aSLuiz Capitulino 2628f11f57e4SLuiz Capitulino s = g_malloc0(sizeof(*s)); 2629218a536aSLuiz Capitulino 2630f11f57e4SLuiz Capitulino if (bs->device_name[0]) { 2631f11f57e4SLuiz Capitulino s->has_device = true; 2632f11f57e4SLuiz Capitulino s->device = g_strdup(bs->device_name); 2633218a536aSLuiz Capitulino } 2634218a536aSLuiz Capitulino 2635f11f57e4SLuiz Capitulino s->stats = g_malloc0(sizeof(*s->stats)); 2636f11f57e4SLuiz Capitulino s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; 2637f11f57e4SLuiz Capitulino s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; 2638f11f57e4SLuiz Capitulino s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; 2639f11f57e4SLuiz Capitulino s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; 2640f11f57e4SLuiz Capitulino s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; 2641f11f57e4SLuiz Capitulino s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; 2642f11f57e4SLuiz Capitulino s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; 2643f11f57e4SLuiz Capitulino s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; 2644f11f57e4SLuiz Capitulino s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; 2645294cc35fSKevin Wolf 2646294cc35fSKevin Wolf if (bs->file) { 2647f11f57e4SLuiz Capitulino s->has_parent = true; 2648f11f57e4SLuiz Capitulino s->parent = qmp_query_blockstat(bs->file, NULL); 2649294cc35fSKevin Wolf } 2650294cc35fSKevin Wolf 2651f11f57e4SLuiz Capitulino return s; 2652294cc35fSKevin Wolf } 2653294cc35fSKevin Wolf 2654f11f57e4SLuiz Capitulino BlockStatsList *qmp_query_blockstats(Error **errp) 2655218a536aSLuiz Capitulino { 2656f11f57e4SLuiz Capitulino BlockStatsList *head = NULL, *cur_item = NULL; 2657a36e69ddSths BlockDriverState *bs; 2658a36e69ddSths 26591b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2660f11f57e4SLuiz Capitulino BlockStatsList *info = g_malloc0(sizeof(*info)); 2661f11f57e4SLuiz Capitulino info->value = qmp_query_blockstat(bs, NULL); 2662f11f57e4SLuiz Capitulino 2663f11f57e4SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2664f11f57e4SLuiz Capitulino if (!cur_item) { 2665f11f57e4SLuiz Capitulino head = cur_item = info; 2666f11f57e4SLuiz Capitulino } else { 2667f11f57e4SLuiz Capitulino cur_item->next = info; 2668f11f57e4SLuiz Capitulino cur_item = info; 2669f11f57e4SLuiz Capitulino } 2670a36e69ddSths } 2671218a536aSLuiz Capitulino 2672f11f57e4SLuiz Capitulino return head; 2673a36e69ddSths } 2674ea2384d3Sbellard 2675045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 2676045df330Saliguori { 2677045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 2678045df330Saliguori return bs->backing_file; 2679045df330Saliguori else if (bs->encrypted) 2680045df330Saliguori return bs->filename; 2681045df330Saliguori else 2682045df330Saliguori return NULL; 2683045df330Saliguori } 2684045df330Saliguori 268583f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 268683f64091Sbellard char *filename, int filename_size) 268783f64091Sbellard { 268883f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 268983f64091Sbellard } 269083f64091Sbellard 2691faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 2692faea38e7Sbellard const uint8_t *buf, int nb_sectors) 2693faea38e7Sbellard { 2694faea38e7Sbellard BlockDriver *drv = bs->drv; 2695faea38e7Sbellard if (!drv) 269619cb3738Sbellard return -ENOMEDIUM; 2697faea38e7Sbellard if (!drv->bdrv_write_compressed) 2698faea38e7Sbellard return -ENOTSUP; 2699fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 2700fbb7b4e0SKevin Wolf return -EIO; 27017cd1e32aSlirans@il.ibm.com 2702c6d22830SJan Kiszka if (bs->dirty_bitmap) { 27037cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 27047cd1e32aSlirans@il.ibm.com } 27057cd1e32aSlirans@il.ibm.com 2706faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 2707faea38e7Sbellard } 2708faea38e7Sbellard 2709faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2710faea38e7Sbellard { 2711faea38e7Sbellard BlockDriver *drv = bs->drv; 2712faea38e7Sbellard if (!drv) 271319cb3738Sbellard return -ENOMEDIUM; 2714faea38e7Sbellard if (!drv->bdrv_get_info) 2715faea38e7Sbellard return -ENOTSUP; 2716faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 2717faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 2718faea38e7Sbellard } 2719faea38e7Sbellard 272045566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 272145566e9cSChristoph Hellwig int64_t pos, int size) 2722178e08a5Saliguori { 2723178e08a5Saliguori BlockDriver *drv = bs->drv; 2724178e08a5Saliguori if (!drv) 2725178e08a5Saliguori return -ENOMEDIUM; 27267cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 272745566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 27287cdb1f6dSMORITA Kazutaka if (bs->file) 27297cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 27307cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2731178e08a5Saliguori } 2732178e08a5Saliguori 273345566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 273445566e9cSChristoph Hellwig int64_t pos, int size) 2735178e08a5Saliguori { 2736178e08a5Saliguori BlockDriver *drv = bs->drv; 2737178e08a5Saliguori if (!drv) 2738178e08a5Saliguori return -ENOMEDIUM; 27397cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 274045566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 27417cdb1f6dSMORITA Kazutaka if (bs->file) 27427cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 27437cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2744178e08a5Saliguori } 2745178e08a5Saliguori 27468b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 27478b9b0cc2SKevin Wolf { 27488b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 27498b9b0cc2SKevin Wolf 27508b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 27518b9b0cc2SKevin Wolf return; 27528b9b0cc2SKevin Wolf } 27538b9b0cc2SKevin Wolf 27548b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 27558b9b0cc2SKevin Wolf 27568b9b0cc2SKevin Wolf } 27578b9b0cc2SKevin Wolf 2758faea38e7Sbellard /**************************************************************/ 2759faea38e7Sbellard /* handling of snapshots */ 2760faea38e7Sbellard 2761feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 2762feeee5acSMiguel Di Ciurcio Filho { 2763feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 276407b70bfbSMarkus Armbruster if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 2765feeee5acSMiguel Di Ciurcio Filho return 0; 2766feeee5acSMiguel Di Ciurcio Filho } 2767feeee5acSMiguel Di Ciurcio Filho 2768feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 2769feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 2770feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 2771feeee5acSMiguel Di Ciurcio Filho } 2772feeee5acSMiguel Di Ciurcio Filho return 0; 2773feeee5acSMiguel Di Ciurcio Filho } 2774feeee5acSMiguel Di Ciurcio Filho 2775feeee5acSMiguel Di Ciurcio Filho return 1; 2776feeee5acSMiguel Di Ciurcio Filho } 2777feeee5acSMiguel Di Ciurcio Filho 2778199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 2779199630b6SBlue Swirl { 2780199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 2781199630b6SBlue Swirl } 2782199630b6SBlue Swirl 2783f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 2784f9092b10SMarkus Armbruster { 2785f9092b10SMarkus Armbruster BlockDriverState *bs; 2786f9092b10SMarkus Armbruster 27873ac906f7SMarkus Armbruster if (bs_snapshots) { 2788f9092b10SMarkus Armbruster return bs_snapshots; 27893ac906f7SMarkus Armbruster } 2790f9092b10SMarkus Armbruster 2791f9092b10SMarkus Armbruster bs = NULL; 2792f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 2793f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 27943ac906f7SMarkus Armbruster bs_snapshots = bs; 27953ac906f7SMarkus Armbruster return bs; 2796f9092b10SMarkus Armbruster } 2797f9092b10SMarkus Armbruster } 2798f9092b10SMarkus Armbruster return NULL; 2799f9092b10SMarkus Armbruster } 2800f9092b10SMarkus Armbruster 2801faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 2802faea38e7Sbellard QEMUSnapshotInfo *sn_info) 2803faea38e7Sbellard { 2804faea38e7Sbellard BlockDriver *drv = bs->drv; 2805faea38e7Sbellard if (!drv) 280619cb3738Sbellard return -ENOMEDIUM; 28077cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 2808faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 28097cdb1f6dSMORITA Kazutaka if (bs->file) 28107cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 28117cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2812faea38e7Sbellard } 2813faea38e7Sbellard 2814faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 2815faea38e7Sbellard const char *snapshot_id) 2816faea38e7Sbellard { 2817faea38e7Sbellard BlockDriver *drv = bs->drv; 28187cdb1f6dSMORITA Kazutaka int ret, open_ret; 28197cdb1f6dSMORITA Kazutaka 2820faea38e7Sbellard if (!drv) 282119cb3738Sbellard return -ENOMEDIUM; 28227cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 2823faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 28247cdb1f6dSMORITA Kazutaka 28257cdb1f6dSMORITA Kazutaka if (bs->file) { 28267cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 28277cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 28287cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 28297cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 28307cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 28317cdb1f6dSMORITA Kazutaka bs->drv = NULL; 28327cdb1f6dSMORITA Kazutaka return open_ret; 28337cdb1f6dSMORITA Kazutaka } 28347cdb1f6dSMORITA Kazutaka return ret; 28357cdb1f6dSMORITA Kazutaka } 28367cdb1f6dSMORITA Kazutaka 28377cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2838faea38e7Sbellard } 2839faea38e7Sbellard 2840faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 2841faea38e7Sbellard { 2842faea38e7Sbellard BlockDriver *drv = bs->drv; 2843faea38e7Sbellard if (!drv) 284419cb3738Sbellard return -ENOMEDIUM; 28457cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 2846faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 28477cdb1f6dSMORITA Kazutaka if (bs->file) 28487cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 28497cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2850faea38e7Sbellard } 2851faea38e7Sbellard 2852faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 2853faea38e7Sbellard QEMUSnapshotInfo **psn_info) 2854faea38e7Sbellard { 2855faea38e7Sbellard BlockDriver *drv = bs->drv; 2856faea38e7Sbellard if (!drv) 285719cb3738Sbellard return -ENOMEDIUM; 28587cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 2859faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 28607cdb1f6dSMORITA Kazutaka if (bs->file) 28617cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 28627cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2863faea38e7Sbellard } 2864faea38e7Sbellard 286551ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 286651ef6727Sedison const char *snapshot_name) 286751ef6727Sedison { 286851ef6727Sedison BlockDriver *drv = bs->drv; 286951ef6727Sedison if (!drv) { 287051ef6727Sedison return -ENOMEDIUM; 287151ef6727Sedison } 287251ef6727Sedison if (!bs->read_only) { 287351ef6727Sedison return -EINVAL; 287451ef6727Sedison } 287551ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 287651ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 287751ef6727Sedison } 287851ef6727Sedison return -ENOTSUP; 287951ef6727Sedison } 288051ef6727Sedison 2881e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 2882e8a6bb9cSMarcelo Tosatti const char *backing_file) 2883e8a6bb9cSMarcelo Tosatti { 2884e8a6bb9cSMarcelo Tosatti if (!bs->drv) { 2885e8a6bb9cSMarcelo Tosatti return NULL; 2886e8a6bb9cSMarcelo Tosatti } 2887e8a6bb9cSMarcelo Tosatti 2888e8a6bb9cSMarcelo Tosatti if (bs->backing_hd) { 2889e8a6bb9cSMarcelo Tosatti if (strcmp(bs->backing_file, backing_file) == 0) { 2890e8a6bb9cSMarcelo Tosatti return bs->backing_hd; 2891e8a6bb9cSMarcelo Tosatti } else { 2892e8a6bb9cSMarcelo Tosatti return bdrv_find_backing_image(bs->backing_hd, backing_file); 2893e8a6bb9cSMarcelo Tosatti } 2894e8a6bb9cSMarcelo Tosatti } 2895e8a6bb9cSMarcelo Tosatti 2896e8a6bb9cSMarcelo Tosatti return NULL; 2897e8a6bb9cSMarcelo Tosatti } 2898e8a6bb9cSMarcelo Tosatti 2899faea38e7Sbellard #define NB_SUFFIXES 4 2900faea38e7Sbellard 2901faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 2902faea38e7Sbellard { 2903faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 2904faea38e7Sbellard int64_t base; 2905faea38e7Sbellard int i; 2906faea38e7Sbellard 2907faea38e7Sbellard if (size <= 999) { 2908faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 2909faea38e7Sbellard } else { 2910faea38e7Sbellard base = 1024; 2911faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 2912faea38e7Sbellard if (size < (10 * base)) { 2913faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 2914faea38e7Sbellard (double)size / base, 2915faea38e7Sbellard suffixes[i]); 2916faea38e7Sbellard break; 2917faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 2918faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 2919faea38e7Sbellard ((size + (base >> 1)) / base), 2920faea38e7Sbellard suffixes[i]); 2921faea38e7Sbellard break; 2922faea38e7Sbellard } 2923faea38e7Sbellard base = base * 1024; 2924faea38e7Sbellard } 2925faea38e7Sbellard } 2926faea38e7Sbellard return buf; 2927faea38e7Sbellard } 2928faea38e7Sbellard 2929faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 2930faea38e7Sbellard { 2931faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 29323b9f94e1Sbellard #ifdef _WIN32 29333b9f94e1Sbellard struct tm *ptm; 29343b9f94e1Sbellard #else 2935faea38e7Sbellard struct tm tm; 29363b9f94e1Sbellard #endif 2937faea38e7Sbellard time_t ti; 2938faea38e7Sbellard int64_t secs; 2939faea38e7Sbellard 2940faea38e7Sbellard if (!sn) { 2941faea38e7Sbellard snprintf(buf, buf_size, 2942faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2943faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 2944faea38e7Sbellard } else { 2945faea38e7Sbellard ti = sn->date_sec; 29463b9f94e1Sbellard #ifdef _WIN32 29473b9f94e1Sbellard ptm = localtime(&ti); 29483b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 29493b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 29503b9f94e1Sbellard #else 2951faea38e7Sbellard localtime_r(&ti, &tm); 2952faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 2953faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 29543b9f94e1Sbellard #endif 2955faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 2956faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 2957faea38e7Sbellard "%02d:%02d:%02d.%03d", 2958faea38e7Sbellard (int)(secs / 3600), 2959faea38e7Sbellard (int)((secs / 60) % 60), 2960faea38e7Sbellard (int)(secs % 60), 2961faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 2962faea38e7Sbellard snprintf(buf, buf_size, 2963faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2964faea38e7Sbellard sn->id_str, sn->name, 2965faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 2966faea38e7Sbellard date_buf, 2967faea38e7Sbellard clock_buf); 2968faea38e7Sbellard } 2969faea38e7Sbellard return buf; 2970faea38e7Sbellard } 2971faea38e7Sbellard 2972ea2384d3Sbellard /**************************************************************/ 297383f64091Sbellard /* async I/Os */ 2974ea2384d3Sbellard 29753b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 2976f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 297783f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 2978ea2384d3Sbellard { 2979bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 2980bbf0a440SStefan Hajnoczi 2981b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 29828c5873d6SStefan Hajnoczi cb, opaque, false); 298383f64091Sbellard } 298483f64091Sbellard 2985f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 2986f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 298783f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 29887674e7bfSbellard { 2989bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 2990bbf0a440SStefan Hajnoczi 29911a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 29928c5873d6SStefan Hajnoczi cb, opaque, true); 299383f64091Sbellard } 299483f64091Sbellard 299540b4f539SKevin Wolf 299640b4f539SKevin Wolf typedef struct MultiwriteCB { 299740b4f539SKevin Wolf int error; 299840b4f539SKevin Wolf int num_requests; 299940b4f539SKevin Wolf int num_callbacks; 300040b4f539SKevin Wolf struct { 300140b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 300240b4f539SKevin Wolf void *opaque; 300340b4f539SKevin Wolf QEMUIOVector *free_qiov; 300440b4f539SKevin Wolf } callbacks[]; 300540b4f539SKevin Wolf } MultiwriteCB; 300640b4f539SKevin Wolf 300740b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 300840b4f539SKevin Wolf { 300940b4f539SKevin Wolf int i; 301040b4f539SKevin Wolf 301140b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 301240b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 30131e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 30141e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 30151e1ea48dSStefan Hajnoczi } 30167267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 301740b4f539SKevin Wolf } 301840b4f539SKevin Wolf } 301940b4f539SKevin Wolf 302040b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 302140b4f539SKevin Wolf { 302240b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 302340b4f539SKevin Wolf 30246d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 30256d519a5fSStefan Hajnoczi 3026cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 302740b4f539SKevin Wolf mcb->error = ret; 302840b4f539SKevin Wolf } 302940b4f539SKevin Wolf 303040b4f539SKevin Wolf mcb->num_requests--; 303140b4f539SKevin Wolf if (mcb->num_requests == 0) { 303240b4f539SKevin Wolf multiwrite_user_cb(mcb); 30337267c094SAnthony Liguori g_free(mcb); 303440b4f539SKevin Wolf } 303540b4f539SKevin Wolf } 303640b4f539SKevin Wolf 303740b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 303840b4f539SKevin Wolf { 303977be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 304077be4366SChristoph Hellwig 304177be4366SChristoph Hellwig /* 304277be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 304377be4366SChristoph Hellwig * here as that could overflow the return value. 304477be4366SChristoph Hellwig */ 304577be4366SChristoph Hellwig if (req1->sector > req2->sector) { 304677be4366SChristoph Hellwig return 1; 304777be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 304877be4366SChristoph Hellwig return -1; 304977be4366SChristoph Hellwig } else { 305077be4366SChristoph Hellwig return 0; 305177be4366SChristoph Hellwig } 305240b4f539SKevin Wolf } 305340b4f539SKevin Wolf 305440b4f539SKevin Wolf /* 305540b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 305640b4f539SKevin Wolf * requests that remain after merging. 305740b4f539SKevin Wolf */ 305840b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 305940b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 306040b4f539SKevin Wolf { 306140b4f539SKevin Wolf int i, outidx; 306240b4f539SKevin Wolf 306340b4f539SKevin Wolf // Sort requests by start sector 306440b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 306540b4f539SKevin Wolf 306640b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 306740b4f539SKevin Wolf // filling up gaps with zero sectors. 306840b4f539SKevin Wolf outidx = 0; 306940b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 307040b4f539SKevin Wolf int merge = 0; 307140b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 307240b4f539SKevin Wolf 3073b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 307440b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 307540b4f539SKevin Wolf merge = 1; 307640b4f539SKevin Wolf } 307740b4f539SKevin Wolf 3078e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3079e2a305fbSChristoph Hellwig merge = 0; 3080e2a305fbSChristoph Hellwig } 3081e2a305fbSChristoph Hellwig 308240b4f539SKevin Wolf if (merge) { 308340b4f539SKevin Wolf size_t size; 30847267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 308540b4f539SKevin Wolf qemu_iovec_init(qiov, 308640b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 308740b4f539SKevin Wolf 308840b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 308940b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 309040b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 309140b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 309240b4f539SKevin Wolf 3093b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3094b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 309540b4f539SKevin Wolf 309640b4f539SKevin Wolf // Add the second request 309740b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 309840b4f539SKevin Wolf 3099cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 310040b4f539SKevin Wolf reqs[outidx].qiov = qiov; 310140b4f539SKevin Wolf 310240b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 310340b4f539SKevin Wolf } else { 310440b4f539SKevin Wolf outidx++; 310540b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 310640b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 310740b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 310840b4f539SKevin Wolf } 310940b4f539SKevin Wolf } 311040b4f539SKevin Wolf 311140b4f539SKevin Wolf return outidx + 1; 311240b4f539SKevin Wolf } 311340b4f539SKevin Wolf 311440b4f539SKevin Wolf /* 311540b4f539SKevin Wolf * Submit multiple AIO write requests at once. 311640b4f539SKevin Wolf * 311740b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 311840b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 311940b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 312040b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 312140b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 312240b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 312340b4f539SKevin Wolf * 312440b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 312540b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 312640b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 312740b4f539SKevin Wolf */ 312840b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 312940b4f539SKevin Wolf { 313040b4f539SKevin Wolf MultiwriteCB *mcb; 313140b4f539SKevin Wolf int i; 313240b4f539SKevin Wolf 3133301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3134301db7c2SRyan Harper if (bs->drv == NULL) { 3135301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3136301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3137301db7c2SRyan Harper } 3138301db7c2SRyan Harper return -1; 3139301db7c2SRyan Harper } 3140301db7c2SRyan Harper 314140b4f539SKevin Wolf if (num_reqs == 0) { 314240b4f539SKevin Wolf return 0; 314340b4f539SKevin Wolf } 314440b4f539SKevin Wolf 314540b4f539SKevin Wolf // Create MultiwriteCB structure 31467267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 314740b4f539SKevin Wolf mcb->num_requests = 0; 314840b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 314940b4f539SKevin Wolf 315040b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 315140b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 315240b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 315340b4f539SKevin Wolf } 315440b4f539SKevin Wolf 315540b4f539SKevin Wolf // Check for mergable requests 315640b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 315740b4f539SKevin Wolf 31586d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 31596d519a5fSStefan Hajnoczi 3160df9309fbSPaolo Bonzini /* Run the aio requests. */ 3161df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 316240b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3163ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 316440b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 316540b4f539SKevin Wolf } 316640b4f539SKevin Wolf 316740b4f539SKevin Wolf return 0; 316840b4f539SKevin Wolf } 316940b4f539SKevin Wolf 317083f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 317183f64091Sbellard { 31726bbff9a0Saliguori acb->pool->cancel(acb); 317383f64091Sbellard } 317483f64091Sbellard 317598f90dbaSZhi Yong Wu /* block I/O throttling */ 317698f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 317798f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait) 317898f90dbaSZhi Yong Wu { 317998f90dbaSZhi Yong Wu uint64_t bps_limit = 0; 318098f90dbaSZhi Yong Wu double bytes_limit, bytes_base, bytes_res; 318198f90dbaSZhi Yong Wu double slice_time, wait_time; 318298f90dbaSZhi Yong Wu 318398f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 318498f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 318598f90dbaSZhi Yong Wu } else if (bs->io_limits.bps[is_write]) { 318698f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[is_write]; 318798f90dbaSZhi Yong Wu } else { 318898f90dbaSZhi Yong Wu if (wait) { 318998f90dbaSZhi Yong Wu *wait = 0; 319098f90dbaSZhi Yong Wu } 319198f90dbaSZhi Yong Wu 319298f90dbaSZhi Yong Wu return false; 319398f90dbaSZhi Yong Wu } 319498f90dbaSZhi Yong Wu 319598f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 319698f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 319798f90dbaSZhi Yong Wu bytes_limit = bps_limit * slice_time; 319898f90dbaSZhi Yong Wu bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; 319998f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 320098f90dbaSZhi Yong Wu bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; 320198f90dbaSZhi Yong Wu } 320298f90dbaSZhi Yong Wu 320398f90dbaSZhi Yong Wu /* bytes_base: the bytes of data which have been read/written; and 320498f90dbaSZhi Yong Wu * it is obtained from the history statistic info. 320598f90dbaSZhi Yong Wu * bytes_res: the remaining bytes of data which need to be read/written. 320698f90dbaSZhi Yong Wu * (bytes_base + bytes_res) / bps_limit: used to calcuate 320798f90dbaSZhi Yong Wu * the total time for completing reading/writting all data. 320898f90dbaSZhi Yong Wu */ 320998f90dbaSZhi Yong Wu bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 321098f90dbaSZhi Yong Wu 321198f90dbaSZhi Yong Wu if (bytes_base + bytes_res <= bytes_limit) { 321298f90dbaSZhi Yong Wu if (wait) { 321398f90dbaSZhi Yong Wu *wait = 0; 321498f90dbaSZhi Yong Wu } 321598f90dbaSZhi Yong Wu 321698f90dbaSZhi Yong Wu return false; 321798f90dbaSZhi Yong Wu } 321898f90dbaSZhi Yong Wu 321998f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 322098f90dbaSZhi Yong Wu wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; 322198f90dbaSZhi Yong Wu 322298f90dbaSZhi Yong Wu /* When the I/O rate at runtime exceeds the limits, 322398f90dbaSZhi Yong Wu * bs->slice_end need to be extended in order that the current statistic 322498f90dbaSZhi Yong Wu * info can be kept until the timer fire, so it is increased and tuned 322598f90dbaSZhi Yong Wu * based on the result of experiment. 322698f90dbaSZhi Yong Wu */ 322798f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 322898f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 322998f90dbaSZhi Yong Wu if (wait) { 323098f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 323198f90dbaSZhi Yong Wu } 323298f90dbaSZhi Yong Wu 323398f90dbaSZhi Yong Wu return true; 323498f90dbaSZhi Yong Wu } 323598f90dbaSZhi Yong Wu 323698f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 323798f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait) 323898f90dbaSZhi Yong Wu { 323998f90dbaSZhi Yong Wu uint64_t iops_limit = 0; 324098f90dbaSZhi Yong Wu double ios_limit, ios_base; 324198f90dbaSZhi Yong Wu double slice_time, wait_time; 324298f90dbaSZhi Yong Wu 324398f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 324498f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 324598f90dbaSZhi Yong Wu } else if (bs->io_limits.iops[is_write]) { 324698f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[is_write]; 324798f90dbaSZhi Yong Wu } else { 324898f90dbaSZhi Yong Wu if (wait) { 324998f90dbaSZhi Yong Wu *wait = 0; 325098f90dbaSZhi Yong Wu } 325198f90dbaSZhi Yong Wu 325298f90dbaSZhi Yong Wu return false; 325398f90dbaSZhi Yong Wu } 325498f90dbaSZhi Yong Wu 325598f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 325698f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 325798f90dbaSZhi Yong Wu ios_limit = iops_limit * slice_time; 325898f90dbaSZhi Yong Wu ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; 325998f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 326098f90dbaSZhi Yong Wu ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; 326198f90dbaSZhi Yong Wu } 326298f90dbaSZhi Yong Wu 326398f90dbaSZhi Yong Wu if (ios_base + 1 <= ios_limit) { 326498f90dbaSZhi Yong Wu if (wait) { 326598f90dbaSZhi Yong Wu *wait = 0; 326698f90dbaSZhi Yong Wu } 326798f90dbaSZhi Yong Wu 326898f90dbaSZhi Yong Wu return false; 326998f90dbaSZhi Yong Wu } 327098f90dbaSZhi Yong Wu 327198f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 327298f90dbaSZhi Yong Wu wait_time = (ios_base + 1) / iops_limit; 327398f90dbaSZhi Yong Wu if (wait_time > elapsed_time) { 327498f90dbaSZhi Yong Wu wait_time = wait_time - elapsed_time; 327598f90dbaSZhi Yong Wu } else { 327698f90dbaSZhi Yong Wu wait_time = 0; 327798f90dbaSZhi Yong Wu } 327898f90dbaSZhi Yong Wu 327998f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 328098f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 328198f90dbaSZhi Yong Wu if (wait) { 328298f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 328398f90dbaSZhi Yong Wu } 328498f90dbaSZhi Yong Wu 328598f90dbaSZhi Yong Wu return true; 328698f90dbaSZhi Yong Wu } 328798f90dbaSZhi Yong Wu 328898f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 328998f90dbaSZhi Yong Wu bool is_write, int64_t *wait) 329098f90dbaSZhi Yong Wu { 329198f90dbaSZhi Yong Wu int64_t now, max_wait; 329298f90dbaSZhi Yong Wu uint64_t bps_wait = 0, iops_wait = 0; 329398f90dbaSZhi Yong Wu double elapsed_time; 329498f90dbaSZhi Yong Wu int bps_ret, iops_ret; 329598f90dbaSZhi Yong Wu 329698f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 329798f90dbaSZhi Yong Wu if ((bs->slice_start < now) 329898f90dbaSZhi Yong Wu && (bs->slice_end > now)) { 329998f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 330098f90dbaSZhi Yong Wu } else { 330198f90dbaSZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 330298f90dbaSZhi Yong Wu bs->slice_start = now; 330398f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 330498f90dbaSZhi Yong Wu 330598f90dbaSZhi Yong Wu bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; 330698f90dbaSZhi Yong Wu bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; 330798f90dbaSZhi Yong Wu 330898f90dbaSZhi Yong Wu bs->io_base.ios[is_write] = bs->nr_ops[is_write]; 330998f90dbaSZhi Yong Wu bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; 331098f90dbaSZhi Yong Wu } 331198f90dbaSZhi Yong Wu 331298f90dbaSZhi Yong Wu elapsed_time = now - bs->slice_start; 331398f90dbaSZhi Yong Wu elapsed_time /= (NANOSECONDS_PER_SECOND); 331498f90dbaSZhi Yong Wu 331598f90dbaSZhi Yong Wu bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, 331698f90dbaSZhi Yong Wu is_write, elapsed_time, &bps_wait); 331798f90dbaSZhi Yong Wu iops_ret = bdrv_exceed_iops_limits(bs, is_write, 331898f90dbaSZhi Yong Wu elapsed_time, &iops_wait); 331998f90dbaSZhi Yong Wu if (bps_ret || iops_ret) { 332098f90dbaSZhi Yong Wu max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; 332198f90dbaSZhi Yong Wu if (wait) { 332298f90dbaSZhi Yong Wu *wait = max_wait; 332398f90dbaSZhi Yong Wu } 332498f90dbaSZhi Yong Wu 332598f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 332698f90dbaSZhi Yong Wu if (bs->slice_end < now + max_wait) { 332798f90dbaSZhi Yong Wu bs->slice_end = now + max_wait; 332898f90dbaSZhi Yong Wu } 332998f90dbaSZhi Yong Wu 333098f90dbaSZhi Yong Wu return true; 333198f90dbaSZhi Yong Wu } 333298f90dbaSZhi Yong Wu 333398f90dbaSZhi Yong Wu if (wait) { 333498f90dbaSZhi Yong Wu *wait = 0; 333598f90dbaSZhi Yong Wu } 333698f90dbaSZhi Yong Wu 333798f90dbaSZhi Yong Wu return false; 333898f90dbaSZhi Yong Wu } 333983f64091Sbellard 334083f64091Sbellard /**************************************************************/ 334183f64091Sbellard /* async block device emulation */ 334283f64091Sbellard 3343c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3344c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3345c16b5a2cSChristoph Hellwig QEMUBH *bh; 3346c16b5a2cSChristoph Hellwig int ret; 3347c16b5a2cSChristoph Hellwig /* vector translation state */ 3348c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3349c16b5a2cSChristoph Hellwig uint8_t *bounce; 3350c16b5a2cSChristoph Hellwig int is_write; 3351c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3352c16b5a2cSChristoph Hellwig 3353c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3354c16b5a2cSChristoph Hellwig { 3355b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3356b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 33576a7ad299SDor Laor qemu_bh_delete(acb->bh); 335836afc451SAvi Kivity acb->bh = NULL; 3359c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3360c16b5a2cSChristoph Hellwig } 3361c16b5a2cSChristoph Hellwig 3362c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 3363c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3364c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3365c16b5a2cSChristoph Hellwig }; 3366c16b5a2cSChristoph Hellwig 336783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3368beac80cdSbellard { 3369ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3370f141eafeSaliguori 3371f141eafeSaliguori if (!acb->is_write) 3372f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 3373ceb42de8Saliguori qemu_vfree(acb->bounce); 3374ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 33756a7ad299SDor Laor qemu_bh_delete(acb->bh); 337636afc451SAvi Kivity acb->bh = NULL; 3377ce1a14dcSpbrook qemu_aio_release(acb); 3378beac80cdSbellard } 3379beac80cdSbellard 3380f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3381f141eafeSaliguori int64_t sector_num, 3382f141eafeSaliguori QEMUIOVector *qiov, 3383f141eafeSaliguori int nb_sectors, 3384f141eafeSaliguori BlockDriverCompletionFunc *cb, 3385f141eafeSaliguori void *opaque, 3386f141eafeSaliguori int is_write) 3387f141eafeSaliguori 3388ea2384d3Sbellard { 3389ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 339083f64091Sbellard 3391c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 3392f141eafeSaliguori acb->is_write = is_write; 3393f141eafeSaliguori acb->qiov = qiov; 3394e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3395ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3396f141eafeSaliguori 3397f141eafeSaliguori if (is_write) { 3398f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 33991ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3400f141eafeSaliguori } else { 34011ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3402f141eafeSaliguori } 3403f141eafeSaliguori 3404ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3405f141eafeSaliguori 3406ce1a14dcSpbrook return &acb->common; 34077a6cba61Spbrook } 34087a6cba61Spbrook 3409f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3410f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3411ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 341283f64091Sbellard { 3413f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 341483f64091Sbellard } 341583f64091Sbellard 3416f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3417f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3418f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3419f141eafeSaliguori { 3420f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3421f141eafeSaliguori } 3422f141eafeSaliguori 342368485420SKevin Wolf 342468485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 342568485420SKevin Wolf BlockDriverAIOCB common; 342668485420SKevin Wolf BlockRequest req; 342768485420SKevin Wolf bool is_write; 342868485420SKevin Wolf QEMUBH* bh; 342968485420SKevin Wolf } BlockDriverAIOCBCoroutine; 343068485420SKevin Wolf 343168485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 343268485420SKevin Wolf { 343368485420SKevin Wolf qemu_aio_flush(); 343468485420SKevin Wolf } 343568485420SKevin Wolf 343668485420SKevin Wolf static AIOPool bdrv_em_co_aio_pool = { 343768485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 343868485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 343968485420SKevin Wolf }; 344068485420SKevin Wolf 344135246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 344268485420SKevin Wolf { 344368485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 344468485420SKevin Wolf 344568485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 344668485420SKevin Wolf qemu_bh_delete(acb->bh); 344768485420SKevin Wolf qemu_aio_release(acb); 344868485420SKevin Wolf } 344968485420SKevin Wolf 3450b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3451b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3452b2a61371SStefan Hajnoczi { 3453b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3454b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3455b2a61371SStefan Hajnoczi 3456b2a61371SStefan Hajnoczi if (!acb->is_write) { 3457b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 3458470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3459b2a61371SStefan Hajnoczi } else { 3460b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 3461f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3462b2a61371SStefan Hajnoczi } 3463b2a61371SStefan Hajnoczi 346435246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3465b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 3466b2a61371SStefan Hajnoczi } 3467b2a61371SStefan Hajnoczi 346868485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 346968485420SKevin Wolf int64_t sector_num, 347068485420SKevin Wolf QEMUIOVector *qiov, 347168485420SKevin Wolf int nb_sectors, 347268485420SKevin Wolf BlockDriverCompletionFunc *cb, 347368485420SKevin Wolf void *opaque, 34748c5873d6SStefan Hajnoczi bool is_write) 347568485420SKevin Wolf { 347668485420SKevin Wolf Coroutine *co; 347768485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 347868485420SKevin Wolf 347968485420SKevin Wolf acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 348068485420SKevin Wolf acb->req.sector = sector_num; 348168485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 348268485420SKevin Wolf acb->req.qiov = qiov; 348368485420SKevin Wolf acb->is_write = is_write; 348468485420SKevin Wolf 34858c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 348668485420SKevin Wolf qemu_coroutine_enter(co, acb); 348768485420SKevin Wolf 348868485420SKevin Wolf return &acb->common; 348968485420SKevin Wolf } 349068485420SKevin Wolf 349107f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 3492b2e12bc6SChristoph Hellwig { 349307f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 349407f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 3495b2e12bc6SChristoph Hellwig 349607f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 349707f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3498b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 3499b2e12bc6SChristoph Hellwig } 3500b2e12bc6SChristoph Hellwig 350107f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 3502016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 3503016f5cf6SAlexander Graf { 350407f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 3505016f5cf6SAlexander Graf 350607f07615SPaolo Bonzini Coroutine *co; 350707f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 3508016f5cf6SAlexander Graf 350907f07615SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 351007f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 351107f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 3512016f5cf6SAlexander Graf 3513016f5cf6SAlexander Graf return &acb->common; 3514016f5cf6SAlexander Graf } 3515016f5cf6SAlexander Graf 35164265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 35174265d620SPaolo Bonzini { 35184265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 35194265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 35204265d620SPaolo Bonzini 35214265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 35224265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 35234265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 35244265d620SPaolo Bonzini } 35254265d620SPaolo Bonzini 35264265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 35274265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 35284265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 35294265d620SPaolo Bonzini { 35304265d620SPaolo Bonzini Coroutine *co; 35314265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 35324265d620SPaolo Bonzini 35334265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 35344265d620SPaolo Bonzini 35354265d620SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 35364265d620SPaolo Bonzini acb->req.sector = sector_num; 35374265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 35384265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 35394265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 35404265d620SPaolo Bonzini 35414265d620SPaolo Bonzini return &acb->common; 35424265d620SPaolo Bonzini } 35434265d620SPaolo Bonzini 3544ea2384d3Sbellard void bdrv_init(void) 3545ea2384d3Sbellard { 35465efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3547ea2384d3Sbellard } 3548ce1a14dcSpbrook 3549eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3550eb852011SMarkus Armbruster { 3551eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3552eb852011SMarkus Armbruster bdrv_init(); 3553eb852011SMarkus Armbruster } 3554eb852011SMarkus Armbruster 3555c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 35566bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 35576bbff9a0Saliguori { 3558ce1a14dcSpbrook BlockDriverAIOCB *acb; 3559ce1a14dcSpbrook 35606bbff9a0Saliguori if (pool->free_aiocb) { 35616bbff9a0Saliguori acb = pool->free_aiocb; 35626bbff9a0Saliguori pool->free_aiocb = acb->next; 3563ce1a14dcSpbrook } else { 35647267c094SAnthony Liguori acb = g_malloc0(pool->aiocb_size); 35656bbff9a0Saliguori acb->pool = pool; 3566ce1a14dcSpbrook } 3567ce1a14dcSpbrook acb->bs = bs; 3568ce1a14dcSpbrook acb->cb = cb; 3569ce1a14dcSpbrook acb->opaque = opaque; 3570ce1a14dcSpbrook return acb; 3571ce1a14dcSpbrook } 3572ce1a14dcSpbrook 3573ce1a14dcSpbrook void qemu_aio_release(void *p) 3574ce1a14dcSpbrook { 35756bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 35766bbff9a0Saliguori AIOPool *pool = acb->pool; 35776bbff9a0Saliguori acb->next = pool->free_aiocb; 35786bbff9a0Saliguori pool->free_aiocb = acb; 3579ce1a14dcSpbrook } 358019cb3738Sbellard 358119cb3738Sbellard /**************************************************************/ 3582f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 3583f9f05dc5SKevin Wolf 3584f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 3585f9f05dc5SKevin Wolf Coroutine *coroutine; 3586f9f05dc5SKevin Wolf int ret; 3587f9f05dc5SKevin Wolf } CoroutineIOCompletion; 3588f9f05dc5SKevin Wolf 3589f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 3590f9f05dc5SKevin Wolf { 3591f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 3592f9f05dc5SKevin Wolf 3593f9f05dc5SKevin Wolf co->ret = ret; 3594f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 3595f9f05dc5SKevin Wolf } 3596f9f05dc5SKevin Wolf 3597f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 3598f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 3599f9f05dc5SKevin Wolf bool is_write) 3600f9f05dc5SKevin Wolf { 3601f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 3602f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 3603f9f05dc5SKevin Wolf }; 3604f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 3605f9f05dc5SKevin Wolf 3606f9f05dc5SKevin Wolf if (is_write) { 3607a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 3608f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3609f9f05dc5SKevin Wolf } else { 3610a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 3611f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3612f9f05dc5SKevin Wolf } 3613f9f05dc5SKevin Wolf 361459370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 3615f9f05dc5SKevin Wolf if (!acb) { 3616f9f05dc5SKevin Wolf return -EIO; 3617f9f05dc5SKevin Wolf } 3618f9f05dc5SKevin Wolf qemu_coroutine_yield(); 3619f9f05dc5SKevin Wolf 3620f9f05dc5SKevin Wolf return co.ret; 3621f9f05dc5SKevin Wolf } 3622f9f05dc5SKevin Wolf 3623f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 3624f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3625f9f05dc5SKevin Wolf QEMUIOVector *iov) 3626f9f05dc5SKevin Wolf { 3627f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 3628f9f05dc5SKevin Wolf } 3629f9f05dc5SKevin Wolf 3630f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 3631f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3632f9f05dc5SKevin Wolf QEMUIOVector *iov) 3633f9f05dc5SKevin Wolf { 3634f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 3635f9f05dc5SKevin Wolf } 3636f9f05dc5SKevin Wolf 363707f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 3638e7a8a783SKevin Wolf { 363907f07615SPaolo Bonzini RwCo *rwco = opaque; 364007f07615SPaolo Bonzini 364107f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 364207f07615SPaolo Bonzini } 364307f07615SPaolo Bonzini 364407f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 364507f07615SPaolo Bonzini { 3646eb489bb1SKevin Wolf int ret; 3647eb489bb1SKevin Wolf 364829cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 364907f07615SPaolo Bonzini return 0; 3650eb489bb1SKevin Wolf } 3651eb489bb1SKevin Wolf 3652ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 3653eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 3654eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 3655eb489bb1SKevin Wolf if (ret < 0) { 3656eb489bb1SKevin Wolf return ret; 3657eb489bb1SKevin Wolf } 3658eb489bb1SKevin Wolf } 3659eb489bb1SKevin Wolf 3660ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 3661ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 3662ca716364SKevin Wolf return 0; 3663ca716364SKevin Wolf } 3664ca716364SKevin Wolf 3665eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 366629cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 366707f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 366807f07615SPaolo Bonzini BlockDriverAIOCB *acb; 3669e7a8a783SKevin Wolf CoroutineIOCompletion co = { 3670e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 3671e7a8a783SKevin Wolf }; 3672e7a8a783SKevin Wolf 367307f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 367407f07615SPaolo Bonzini if (acb == NULL) { 367529cdb251SPaolo Bonzini ret = -EIO; 367607f07615SPaolo Bonzini } else { 3677e7a8a783SKevin Wolf qemu_coroutine_yield(); 367829cdb251SPaolo Bonzini ret = co.ret; 3679e7a8a783SKevin Wolf } 368007f07615SPaolo Bonzini } else { 368107f07615SPaolo Bonzini /* 368207f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 368307f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 368407f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 368507f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 368607f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 368707f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 368807f07615SPaolo Bonzini * mode. 368907f07615SPaolo Bonzini * 369007f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 369107f07615SPaolo Bonzini */ 369229cdb251SPaolo Bonzini ret = 0; 369307f07615SPaolo Bonzini } 369429cdb251SPaolo Bonzini if (ret < 0) { 369529cdb251SPaolo Bonzini return ret; 369629cdb251SPaolo Bonzini } 369729cdb251SPaolo Bonzini 369829cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 369929cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 370029cdb251SPaolo Bonzini */ 370129cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 370207f07615SPaolo Bonzini } 370307f07615SPaolo Bonzini 37040f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 37050f15423cSAnthony Liguori { 37060f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 37070f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 37080f15423cSAnthony Liguori } 37090f15423cSAnthony Liguori } 37100f15423cSAnthony Liguori 37110f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 37120f15423cSAnthony Liguori { 37130f15423cSAnthony Liguori BlockDriverState *bs; 37140f15423cSAnthony Liguori 37150f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 37160f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 37170f15423cSAnthony Liguori } 37180f15423cSAnthony Liguori } 37190f15423cSAnthony Liguori 372007789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 372107789269SBenoît Canet { 372207789269SBenoît Canet BlockDriverState *bs; 372307789269SBenoît Canet 372407789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 372507789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 372607789269SBenoît Canet } 372707789269SBenoît Canet } 372807789269SBenoît Canet 372907f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 373007f07615SPaolo Bonzini { 373107f07615SPaolo Bonzini Coroutine *co; 373207f07615SPaolo Bonzini RwCo rwco = { 373307f07615SPaolo Bonzini .bs = bs, 373407f07615SPaolo Bonzini .ret = NOT_DONE, 373507f07615SPaolo Bonzini }; 373607f07615SPaolo Bonzini 373707f07615SPaolo Bonzini if (qemu_in_coroutine()) { 373807f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 373907f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 374007f07615SPaolo Bonzini } else { 374107f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 374207f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 374307f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 374407f07615SPaolo Bonzini qemu_aio_wait(); 374507f07615SPaolo Bonzini } 374607f07615SPaolo Bonzini } 374707f07615SPaolo Bonzini 374807f07615SPaolo Bonzini return rwco.ret; 374907f07615SPaolo Bonzini } 3750e7a8a783SKevin Wolf 37514265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 37524265d620SPaolo Bonzini { 37534265d620SPaolo Bonzini RwCo *rwco = opaque; 37544265d620SPaolo Bonzini 37554265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 37564265d620SPaolo Bonzini } 37574265d620SPaolo Bonzini 37584265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 37594265d620SPaolo Bonzini int nb_sectors) 37604265d620SPaolo Bonzini { 37614265d620SPaolo Bonzini if (!bs->drv) { 37624265d620SPaolo Bonzini return -ENOMEDIUM; 37634265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 37644265d620SPaolo Bonzini return -EIO; 37654265d620SPaolo Bonzini } else if (bs->read_only) { 37664265d620SPaolo Bonzini return -EROFS; 37674265d620SPaolo Bonzini } else if (bs->drv->bdrv_co_discard) { 37684265d620SPaolo Bonzini return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors); 37694265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 37704265d620SPaolo Bonzini BlockDriverAIOCB *acb; 37714265d620SPaolo Bonzini CoroutineIOCompletion co = { 37724265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 37734265d620SPaolo Bonzini }; 37744265d620SPaolo Bonzini 37754265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 37764265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 37774265d620SPaolo Bonzini if (acb == NULL) { 37784265d620SPaolo Bonzini return -EIO; 37794265d620SPaolo Bonzini } else { 37804265d620SPaolo Bonzini qemu_coroutine_yield(); 37814265d620SPaolo Bonzini return co.ret; 37824265d620SPaolo Bonzini } 37834265d620SPaolo Bonzini } else { 37844265d620SPaolo Bonzini return 0; 37854265d620SPaolo Bonzini } 37864265d620SPaolo Bonzini } 37874265d620SPaolo Bonzini 37884265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 37894265d620SPaolo Bonzini { 37904265d620SPaolo Bonzini Coroutine *co; 37914265d620SPaolo Bonzini RwCo rwco = { 37924265d620SPaolo Bonzini .bs = bs, 37934265d620SPaolo Bonzini .sector_num = sector_num, 37944265d620SPaolo Bonzini .nb_sectors = nb_sectors, 37954265d620SPaolo Bonzini .ret = NOT_DONE, 37964265d620SPaolo Bonzini }; 37974265d620SPaolo Bonzini 37984265d620SPaolo Bonzini if (qemu_in_coroutine()) { 37994265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 38004265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 38014265d620SPaolo Bonzini } else { 38024265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 38034265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 38044265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 38054265d620SPaolo Bonzini qemu_aio_wait(); 38064265d620SPaolo Bonzini } 38074265d620SPaolo Bonzini } 38084265d620SPaolo Bonzini 38094265d620SPaolo Bonzini return rwco.ret; 38104265d620SPaolo Bonzini } 38114265d620SPaolo Bonzini 3812f9f05dc5SKevin Wolf /**************************************************************/ 381319cb3738Sbellard /* removable device support */ 381419cb3738Sbellard 381519cb3738Sbellard /** 381619cb3738Sbellard * Return TRUE if the media is present 381719cb3738Sbellard */ 381819cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 381919cb3738Sbellard { 382019cb3738Sbellard BlockDriver *drv = bs->drv; 3821a1aff5bfSMarkus Armbruster 382219cb3738Sbellard if (!drv) 382319cb3738Sbellard return 0; 382419cb3738Sbellard if (!drv->bdrv_is_inserted) 3825a1aff5bfSMarkus Armbruster return 1; 3826a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 382719cb3738Sbellard } 382819cb3738Sbellard 382919cb3738Sbellard /** 38308e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 38318e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 383219cb3738Sbellard */ 383319cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 383419cb3738Sbellard { 383519cb3738Sbellard BlockDriver *drv = bs->drv; 383619cb3738Sbellard 38378e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 38388e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 38398e49ca46SMarkus Armbruster } 38408e49ca46SMarkus Armbruster return -ENOTSUP; 384119cb3738Sbellard } 384219cb3738Sbellard 384319cb3738Sbellard /** 384419cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 384519cb3738Sbellard */ 3846f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 384719cb3738Sbellard { 384819cb3738Sbellard BlockDriver *drv = bs->drv; 384919cb3738Sbellard 3850822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 3851822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 385219cb3738Sbellard } 38536f382ed2SLuiz Capitulino 38546f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 38556f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 38566f382ed2SLuiz Capitulino } 385719cb3738Sbellard } 385819cb3738Sbellard 385919cb3738Sbellard /** 386019cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 386119cb3738Sbellard * to eject it manually). 386219cb3738Sbellard */ 3863025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 386419cb3738Sbellard { 386519cb3738Sbellard BlockDriver *drv = bs->drv; 386619cb3738Sbellard 3867025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 3868b8c6d095SStefan Hajnoczi 3869025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 3870025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 387119cb3738Sbellard } 387219cb3738Sbellard } 3873985a03b0Sths 3874985a03b0Sths /* needed for generic scsi interface */ 3875985a03b0Sths 3876985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 3877985a03b0Sths { 3878985a03b0Sths BlockDriver *drv = bs->drv; 3879985a03b0Sths 3880985a03b0Sths if (drv && drv->bdrv_ioctl) 3881985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 3882985a03b0Sths return -ENOTSUP; 3883985a03b0Sths } 38847d780669Saliguori 3885221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 3886221f715dSaliguori unsigned long int req, void *buf, 38877d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 38887d780669Saliguori { 3889221f715dSaliguori BlockDriver *drv = bs->drv; 38907d780669Saliguori 3891221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 3892221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 3893221f715dSaliguori return NULL; 38947d780669Saliguori } 3895e268ca52Saliguori 38967b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 38977b6f9300SMarkus Armbruster { 38987b6f9300SMarkus Armbruster bs->buffer_alignment = align; 38997b6f9300SMarkus Armbruster } 39007cd1e32aSlirans@il.ibm.com 3901e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 3902e268ca52Saliguori { 3903e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 3904e268ca52Saliguori } 39057cd1e32aSlirans@il.ibm.com 39067cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 39077cd1e32aSlirans@il.ibm.com { 39087cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 3909a55eb92cSJan Kiszka 3910aaa0eb75SLiran Schour bs->dirty_count = 0; 39117cd1e32aSlirans@il.ibm.com if (enable) { 3912c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 3913c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 391471df14fcSPaolo Bonzini BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG - 1; 391571df14fcSPaolo Bonzini bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG; 39167cd1e32aSlirans@il.ibm.com 391771df14fcSPaolo Bonzini bs->dirty_bitmap = g_new0(unsigned long, bitmap_size); 39187cd1e32aSlirans@il.ibm.com } 39197cd1e32aSlirans@il.ibm.com } else { 3920c6d22830SJan Kiszka if (bs->dirty_bitmap) { 39217267c094SAnthony Liguori g_free(bs->dirty_bitmap); 3922c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 39237cd1e32aSlirans@il.ibm.com } 39247cd1e32aSlirans@il.ibm.com } 39257cd1e32aSlirans@il.ibm.com } 39267cd1e32aSlirans@il.ibm.com 39277cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 39287cd1e32aSlirans@il.ibm.com { 39296ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 39307cd1e32aSlirans@il.ibm.com 3931c6d22830SJan Kiszka if (bs->dirty_bitmap && 3932c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 39336d59fec1SMarcelo Tosatti return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 39346d59fec1SMarcelo Tosatti (1UL << (chunk % (sizeof(unsigned long) * 8)))); 39357cd1e32aSlirans@il.ibm.com } else { 39367cd1e32aSlirans@il.ibm.com return 0; 39377cd1e32aSlirans@il.ibm.com } 39387cd1e32aSlirans@il.ibm.com } 39397cd1e32aSlirans@il.ibm.com 39407cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 39417cd1e32aSlirans@il.ibm.com int nr_sectors) 39427cd1e32aSlirans@il.ibm.com { 39437cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 39447cd1e32aSlirans@il.ibm.com } 3945aaa0eb75SLiran Schour 3946aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 3947aaa0eb75SLiran Schour { 3948aaa0eb75SLiran Schour return bs->dirty_count; 3949aaa0eb75SLiran Schour } 3950f88e1a42SJes Sorensen 3951db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 3952db593f25SMarcelo Tosatti { 3953db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 3954db593f25SMarcelo Tosatti bs->in_use = in_use; 3955db593f25SMarcelo Tosatti } 3956db593f25SMarcelo Tosatti 3957db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 3958db593f25SMarcelo Tosatti { 3959db593f25SMarcelo Tosatti return bs->in_use; 3960db593f25SMarcelo Tosatti } 3961db593f25SMarcelo Tosatti 396228a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 396328a7282aSLuiz Capitulino { 3964d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 396558e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 396628a7282aSLuiz Capitulino } 396728a7282aSLuiz Capitulino 396828a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 396928a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 397028a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 397128a7282aSLuiz Capitulino { 3972d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 397328a7282aSLuiz Capitulino (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC || 397428a7282aSLuiz Capitulino bs->on_write_error == BLOCK_ERR_STOP_ANY || 397528a7282aSLuiz Capitulino bs->on_read_error == BLOCK_ERR_STOP_ANY)); 397628a7282aSLuiz Capitulino } 397728a7282aSLuiz Capitulino 397828a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 397928a7282aSLuiz Capitulino { 3980d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 398128a7282aSLuiz Capitulino } 398228a7282aSLuiz Capitulino 398328a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 398428a7282aSLuiz Capitulino { 398528a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 398658e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 398728a7282aSLuiz Capitulino } 398828a7282aSLuiz Capitulino } 398928a7282aSLuiz Capitulino 399028a7282aSLuiz Capitulino /* XXX: Today this is set by device models because it makes the implementation 399128a7282aSLuiz Capitulino quite simple. However, the block layer knows about the error, so it's 399228a7282aSLuiz Capitulino possible to implement this without device models being involved */ 399328a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 399428a7282aSLuiz Capitulino { 399558e21ef5SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs) && 399658e21ef5SLuiz Capitulino bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 399728a7282aSLuiz Capitulino assert(error >= 0); 399858e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 399958e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 400028a7282aSLuiz Capitulino } 400128a7282aSLuiz Capitulino } 400228a7282aSLuiz Capitulino 4003a597e79cSChristoph Hellwig void 4004a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 4005a597e79cSChristoph Hellwig enum BlockAcctType type) 4006a597e79cSChristoph Hellwig { 4007a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 4008a597e79cSChristoph Hellwig 4009a597e79cSChristoph Hellwig cookie->bytes = bytes; 4010c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4011a597e79cSChristoph Hellwig cookie->type = type; 4012a597e79cSChristoph Hellwig } 4013a597e79cSChristoph Hellwig 4014a597e79cSChristoph Hellwig void 4015a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4016a597e79cSChristoph Hellwig { 4017a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4018a597e79cSChristoph Hellwig 4019a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4020a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4021c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4022a597e79cSChristoph Hellwig } 4023a597e79cSChristoph Hellwig 4024f88e1a42SJes Sorensen int bdrv_img_create(const char *filename, const char *fmt, 4025f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 4026f88e1a42SJes Sorensen char *options, uint64_t img_size, int flags) 4027f88e1a42SJes Sorensen { 4028f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4029d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4030f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 4031f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 403296df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4033f88e1a42SJes Sorensen int ret = 0; 4034f88e1a42SJes Sorensen 4035f88e1a42SJes Sorensen /* Find driver and parse its options */ 4036f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4037f88e1a42SJes Sorensen if (!drv) { 4038f88e1a42SJes Sorensen error_report("Unknown file format '%s'", fmt); 40394f70f249SJes Sorensen ret = -EINVAL; 4040f88e1a42SJes Sorensen goto out; 4041f88e1a42SJes Sorensen } 4042f88e1a42SJes Sorensen 4043f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 4044f88e1a42SJes Sorensen if (!proto_drv) { 4045f88e1a42SJes Sorensen error_report("Unknown protocol '%s'", filename); 40464f70f249SJes Sorensen ret = -EINVAL; 4047f88e1a42SJes Sorensen goto out; 4048f88e1a42SJes Sorensen } 4049f88e1a42SJes Sorensen 4050f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4051f88e1a42SJes Sorensen drv->create_options); 4052f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4053f88e1a42SJes Sorensen proto_drv->create_options); 4054f88e1a42SJes Sorensen 4055f88e1a42SJes Sorensen /* Create parameter list with default values */ 4056f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4057f88e1a42SJes Sorensen 4058f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4059f88e1a42SJes Sorensen 4060f88e1a42SJes Sorensen /* Parse -o options */ 4061f88e1a42SJes Sorensen if (options) { 4062f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4063f88e1a42SJes Sorensen if (param == NULL) { 4064f88e1a42SJes Sorensen error_report("Invalid options for file format '%s'.", fmt); 40654f70f249SJes Sorensen ret = -EINVAL; 4066f88e1a42SJes Sorensen goto out; 4067f88e1a42SJes Sorensen } 4068f88e1a42SJes Sorensen } 4069f88e1a42SJes Sorensen 4070f88e1a42SJes Sorensen if (base_filename) { 4071f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4072f88e1a42SJes Sorensen base_filename)) { 4073f88e1a42SJes Sorensen error_report("Backing file not supported for file format '%s'", 4074f88e1a42SJes Sorensen fmt); 40754f70f249SJes Sorensen ret = -EINVAL; 4076f88e1a42SJes Sorensen goto out; 4077f88e1a42SJes Sorensen } 4078f88e1a42SJes Sorensen } 4079f88e1a42SJes Sorensen 4080f88e1a42SJes Sorensen if (base_fmt) { 4081f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 4082f88e1a42SJes Sorensen error_report("Backing file format not supported for file " 4083f88e1a42SJes Sorensen "format '%s'", fmt); 40844f70f249SJes Sorensen ret = -EINVAL; 4085f88e1a42SJes Sorensen goto out; 4086f88e1a42SJes Sorensen } 4087f88e1a42SJes Sorensen } 4088f88e1a42SJes Sorensen 4089792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4090792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4091792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 4092792da93aSJes Sorensen error_report("Error: Trying to create an image with the " 4093792da93aSJes Sorensen "same filename as the backing file"); 40944f70f249SJes Sorensen ret = -EINVAL; 4095792da93aSJes Sorensen goto out; 4096792da93aSJes Sorensen } 4097792da93aSJes Sorensen } 4098792da93aSJes Sorensen 4099f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4100f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 410196df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 410296df67d1SStefan Hajnoczi if (!backing_drv) { 4103f88e1a42SJes Sorensen error_report("Unknown backing file format '%s'", 4104f88e1a42SJes Sorensen backing_fmt->value.s); 41054f70f249SJes Sorensen ret = -EINVAL; 4106f88e1a42SJes Sorensen goto out; 4107f88e1a42SJes Sorensen } 4108f88e1a42SJes Sorensen } 4109f88e1a42SJes Sorensen 4110f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4111f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4112d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4113d220894eSKevin Wolf if (size && size->value.n == -1) { 4114f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4115f88e1a42SJes Sorensen uint64_t size; 4116f88e1a42SJes Sorensen char buf[32]; 411763090dacSPaolo Bonzini int back_flags; 411863090dacSPaolo Bonzini 411963090dacSPaolo Bonzini /* backing files always opened read-only */ 412063090dacSPaolo Bonzini back_flags = 412163090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 4122f88e1a42SJes Sorensen 4123f88e1a42SJes Sorensen bs = bdrv_new(""); 4124f88e1a42SJes Sorensen 412563090dacSPaolo Bonzini ret = bdrv_open(bs, backing_file->value.s, back_flags, backing_drv); 4126f88e1a42SJes Sorensen if (ret < 0) { 412796df67d1SStefan Hajnoczi error_report("Could not open '%s'", backing_file->value.s); 4128f88e1a42SJes Sorensen goto out; 4129f88e1a42SJes Sorensen } 4130f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4131f88e1a42SJes Sorensen size *= 512; 4132f88e1a42SJes Sorensen 4133f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4134f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4135f88e1a42SJes Sorensen } else { 4136f88e1a42SJes Sorensen error_report("Image creation needs a size parameter"); 41374f70f249SJes Sorensen ret = -EINVAL; 4138f88e1a42SJes Sorensen goto out; 4139f88e1a42SJes Sorensen } 4140f88e1a42SJes Sorensen } 4141f88e1a42SJes Sorensen 4142f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4143f88e1a42SJes Sorensen print_option_parameters(param); 4144f88e1a42SJes Sorensen puts(""); 4145f88e1a42SJes Sorensen 4146f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 4147f88e1a42SJes Sorensen 4148f88e1a42SJes Sorensen if (ret < 0) { 4149f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 4150f88e1a42SJes Sorensen error_report("Formatting or formatting option not supported for " 4151f88e1a42SJes Sorensen "file format '%s'", fmt); 4152f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 4153f88e1a42SJes Sorensen error_report("The image size is too large for file format '%s'", 4154f88e1a42SJes Sorensen fmt); 4155f88e1a42SJes Sorensen } else { 4156f88e1a42SJes Sorensen error_report("%s: error while creating %s: %s", filename, fmt, 4157f88e1a42SJes Sorensen strerror(-ret)); 4158f88e1a42SJes Sorensen } 4159f88e1a42SJes Sorensen } 4160f88e1a42SJes Sorensen 4161f88e1a42SJes Sorensen out: 4162f88e1a42SJes Sorensen free_option_parameters(create_options); 4163f88e1a42SJes Sorensen free_option_parameters(param); 4164f88e1a42SJes Sorensen 4165f88e1a42SJes Sorensen if (bs) { 4166f88e1a42SJes Sorensen bdrv_delete(bs); 4167f88e1a42SJes Sorensen } 41684f70f249SJes Sorensen 41694f70f249SJes Sorensen return ret; 4170f88e1a42SJes Sorensen } 4171eeec61f2SStefan Hajnoczi 4172eeec61f2SStefan Hajnoczi void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs, 4173c83c66c3SStefan Hajnoczi int64_t speed, BlockDriverCompletionFunc *cb, 4174c83c66c3SStefan Hajnoczi void *opaque, Error **errp) 4175eeec61f2SStefan Hajnoczi { 4176eeec61f2SStefan Hajnoczi BlockJob *job; 4177eeec61f2SStefan Hajnoczi 4178eeec61f2SStefan Hajnoczi if (bs->job || bdrv_in_use(bs)) { 4179fd7f8c65SStefan Hajnoczi error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); 4180eeec61f2SStefan Hajnoczi return NULL; 4181eeec61f2SStefan Hajnoczi } 4182eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 1); 4183eeec61f2SStefan Hajnoczi 4184eeec61f2SStefan Hajnoczi job = g_malloc0(job_type->instance_size); 4185eeec61f2SStefan Hajnoczi job->job_type = job_type; 4186eeec61f2SStefan Hajnoczi job->bs = bs; 4187eeec61f2SStefan Hajnoczi job->cb = cb; 4188eeec61f2SStefan Hajnoczi job->opaque = opaque; 41894513eafeSPaolo Bonzini job->busy = true; 4190eeec61f2SStefan Hajnoczi bs->job = job; 4191c83c66c3SStefan Hajnoczi 4192c83c66c3SStefan Hajnoczi /* Only set speed when necessary to avoid NotSupported error */ 4193c83c66c3SStefan Hajnoczi if (speed != 0) { 4194c83c66c3SStefan Hajnoczi Error *local_err = NULL; 4195c83c66c3SStefan Hajnoczi 4196c83c66c3SStefan Hajnoczi block_job_set_speed(job, speed, &local_err); 4197c83c66c3SStefan Hajnoczi if (error_is_set(&local_err)) { 4198c83c66c3SStefan Hajnoczi bs->job = NULL; 4199c83c66c3SStefan Hajnoczi g_free(job); 4200c83c66c3SStefan Hajnoczi bdrv_set_in_use(bs, 0); 4201c83c66c3SStefan Hajnoczi error_propagate(errp, local_err); 4202c83c66c3SStefan Hajnoczi return NULL; 4203c83c66c3SStefan Hajnoczi } 4204c83c66c3SStefan Hajnoczi } 4205eeec61f2SStefan Hajnoczi return job; 4206eeec61f2SStefan Hajnoczi } 4207eeec61f2SStefan Hajnoczi 4208eeec61f2SStefan Hajnoczi void block_job_complete(BlockJob *job, int ret) 4209eeec61f2SStefan Hajnoczi { 4210eeec61f2SStefan Hajnoczi BlockDriverState *bs = job->bs; 4211eeec61f2SStefan Hajnoczi 4212eeec61f2SStefan Hajnoczi assert(bs->job == job); 4213eeec61f2SStefan Hajnoczi job->cb(job->opaque, ret); 4214eeec61f2SStefan Hajnoczi bs->job = NULL; 4215eeec61f2SStefan Hajnoczi g_free(job); 4216eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 0); 4217eeec61f2SStefan Hajnoczi } 4218eeec61f2SStefan Hajnoczi 4219882ec7ceSStefan Hajnoczi void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) 4220eeec61f2SStefan Hajnoczi { 42219e6636c7SStefan Hajnoczi Error *local_err = NULL; 42229f25ecccSPaolo Bonzini 4223eeec61f2SStefan Hajnoczi if (!job->job_type->set_speed) { 42249e6636c7SStefan Hajnoczi error_set(errp, QERR_NOT_SUPPORTED); 42259e6636c7SStefan Hajnoczi return; 4226eeec61f2SStefan Hajnoczi } 4227882ec7ceSStefan Hajnoczi job->job_type->set_speed(job, speed, &local_err); 42289e6636c7SStefan Hajnoczi if (error_is_set(&local_err)) { 42299e6636c7SStefan Hajnoczi error_propagate(errp, local_err); 42309e6636c7SStefan Hajnoczi return; 42319e6636c7SStefan Hajnoczi } 42329e6636c7SStefan Hajnoczi 4233882ec7ceSStefan Hajnoczi job->speed = speed; 42349f25ecccSPaolo Bonzini } 4235eeec61f2SStefan Hajnoczi 4236eeec61f2SStefan Hajnoczi void block_job_cancel(BlockJob *job) 4237eeec61f2SStefan Hajnoczi { 4238eeec61f2SStefan Hajnoczi job->cancelled = true; 4239fa4478d5SPaolo Bonzini if (job->co && !job->busy) { 4240fa4478d5SPaolo Bonzini qemu_coroutine_enter(job->co, NULL); 4241fa4478d5SPaolo Bonzini } 4242eeec61f2SStefan Hajnoczi } 4243eeec61f2SStefan Hajnoczi 4244eeec61f2SStefan Hajnoczi bool block_job_is_cancelled(BlockJob *job) 4245eeec61f2SStefan Hajnoczi { 4246eeec61f2SStefan Hajnoczi return job->cancelled; 4247eeec61f2SStefan Hajnoczi } 42483e914655SPaolo Bonzini 4249fa4478d5SPaolo Bonzini struct BlockCancelData { 4250fa4478d5SPaolo Bonzini BlockJob *job; 4251fa4478d5SPaolo Bonzini BlockDriverCompletionFunc *cb; 4252fa4478d5SPaolo Bonzini void *opaque; 4253fa4478d5SPaolo Bonzini bool cancelled; 4254fa4478d5SPaolo Bonzini int ret; 4255fa4478d5SPaolo Bonzini }; 4256fa4478d5SPaolo Bonzini 4257fa4478d5SPaolo Bonzini static void block_job_cancel_cb(void *opaque, int ret) 42583e914655SPaolo Bonzini { 4259fa4478d5SPaolo Bonzini struct BlockCancelData *data = opaque; 4260fa4478d5SPaolo Bonzini 4261fa4478d5SPaolo Bonzini data->cancelled = block_job_is_cancelled(data->job); 4262fa4478d5SPaolo Bonzini data->ret = ret; 4263fa4478d5SPaolo Bonzini data->cb(data->opaque, ret); 4264fa4478d5SPaolo Bonzini } 4265fa4478d5SPaolo Bonzini 4266fa4478d5SPaolo Bonzini int block_job_cancel_sync(BlockJob *job) 4267fa4478d5SPaolo Bonzini { 4268fa4478d5SPaolo Bonzini struct BlockCancelData data; 42693e914655SPaolo Bonzini BlockDriverState *bs = job->bs; 42703e914655SPaolo Bonzini 42713e914655SPaolo Bonzini assert(bs->job == job); 4272fa4478d5SPaolo Bonzini 4273fa4478d5SPaolo Bonzini /* Set up our own callback to store the result and chain to 4274fa4478d5SPaolo Bonzini * the original callback. 4275fa4478d5SPaolo Bonzini */ 4276fa4478d5SPaolo Bonzini data.job = job; 4277fa4478d5SPaolo Bonzini data.cb = job->cb; 4278fa4478d5SPaolo Bonzini data.opaque = job->opaque; 4279fa4478d5SPaolo Bonzini data.ret = -EINPROGRESS; 4280fa4478d5SPaolo Bonzini job->cb = block_job_cancel_cb; 4281fa4478d5SPaolo Bonzini job->opaque = &data; 42823e914655SPaolo Bonzini block_job_cancel(job); 4283fa4478d5SPaolo Bonzini while (data.ret == -EINPROGRESS) { 42843e914655SPaolo Bonzini qemu_aio_wait(); 42853e914655SPaolo Bonzini } 4286fa4478d5SPaolo Bonzini return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret; 42873e914655SPaolo Bonzini } 42884513eafeSPaolo Bonzini 42894513eafeSPaolo Bonzini void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns) 42904513eafeSPaolo Bonzini { 42914513eafeSPaolo Bonzini /* Check cancellation *before* setting busy = false, too! */ 42924513eafeSPaolo Bonzini if (!block_job_is_cancelled(job)) { 42934513eafeSPaolo Bonzini job->busy = false; 42944513eafeSPaolo Bonzini co_sleep_ns(clock, ns); 42954513eafeSPaolo Bonzini job->busy = true; 42964513eafeSPaolo Bonzini } 42974513eafeSPaolo Bonzini } 4298