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 { 201947995c0SPaolo Bonzini const char *p; 202947995c0SPaolo 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 } 208947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 209947995c0SPaolo Bonzini #else 210947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2119e0b22f4SStefan Hajnoczi #endif 2129e0b22f4SStefan Hajnoczi 213947995c0SPaolo 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 273dc5a1371SPaolo Bonzini void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) 274dc5a1371SPaolo Bonzini { 275dc5a1371SPaolo Bonzini if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { 276dc5a1371SPaolo Bonzini pstrcpy(dest, sz, bs->backing_file); 277dc5a1371SPaolo Bonzini } else { 278dc5a1371SPaolo Bonzini path_combine(dest, sz, bs->filename, bs->backing_file); 279dc5a1371SPaolo Bonzini } 280dc5a1371SPaolo Bonzini } 281dc5a1371SPaolo Bonzini 2825efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 283ea2384d3Sbellard { 2848c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 2858c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 286f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 287f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 288f9f05dc5SKevin Wolf 289f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 290f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 291f8c35c1dSStefan Hajnoczi */ 292f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 29383f64091Sbellard /* add AIO emulation layer */ 294f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 295f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 29683f64091Sbellard } 297f9f05dc5SKevin Wolf } 298b2e12bc6SChristoph Hellwig 2998a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 300ea2384d3Sbellard } 301b338082bSbellard 302b338082bSbellard /* create a new block device (by default it is empty) */ 303b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 304fc01f7e7Sbellard { 3051b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 306b338082bSbellard 3077267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 308b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 309ea2384d3Sbellard if (device_name[0] != '\0') { 3101b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 311ea2384d3Sbellard } 31228a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 313b338082bSbellard return bs; 314b338082bSbellard } 315b338082bSbellard 316ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 317ea2384d3Sbellard { 318ea2384d3Sbellard BlockDriver *drv1; 3198a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3208a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 321ea2384d3Sbellard return drv1; 322ea2384d3Sbellard } 3238a22f02aSStefan Hajnoczi } 324ea2384d3Sbellard return NULL; 325ea2384d3Sbellard } 326ea2384d3Sbellard 327eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 328eb852011SMarkus Armbruster { 329eb852011SMarkus Armbruster static const char *whitelist[] = { 330eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 331eb852011SMarkus Armbruster }; 332eb852011SMarkus Armbruster const char **p; 333eb852011SMarkus Armbruster 334eb852011SMarkus Armbruster if (!whitelist[0]) 335eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 336eb852011SMarkus Armbruster 337eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 338eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 339eb852011SMarkus Armbruster return 1; 340eb852011SMarkus Armbruster } 341eb852011SMarkus Armbruster } 342eb852011SMarkus Armbruster return 0; 343eb852011SMarkus Armbruster } 344eb852011SMarkus Armbruster 345eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 346eb852011SMarkus Armbruster { 347eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 348eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 349eb852011SMarkus Armbruster } 350eb852011SMarkus Armbruster 3515b7e1542SZhi Yong Wu typedef struct CreateCo { 3525b7e1542SZhi Yong Wu BlockDriver *drv; 3535b7e1542SZhi Yong Wu char *filename; 3545b7e1542SZhi Yong Wu QEMUOptionParameter *options; 3555b7e1542SZhi Yong Wu int ret; 3565b7e1542SZhi Yong Wu } CreateCo; 3575b7e1542SZhi Yong Wu 3585b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 3595b7e1542SZhi Yong Wu { 3605b7e1542SZhi Yong Wu CreateCo *cco = opaque; 3615b7e1542SZhi Yong Wu assert(cco->drv); 3625b7e1542SZhi Yong Wu 3635b7e1542SZhi Yong Wu cco->ret = cco->drv->bdrv_create(cco->filename, cco->options); 3645b7e1542SZhi Yong Wu } 3655b7e1542SZhi Yong Wu 3660e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 3670e7e1989SKevin Wolf QEMUOptionParameter *options) 368ea2384d3Sbellard { 3695b7e1542SZhi Yong Wu int ret; 3700e7e1989SKevin Wolf 3715b7e1542SZhi Yong Wu Coroutine *co; 3725b7e1542SZhi Yong Wu CreateCo cco = { 3735b7e1542SZhi Yong Wu .drv = drv, 3745b7e1542SZhi Yong Wu .filename = g_strdup(filename), 3755b7e1542SZhi Yong Wu .options = options, 3765b7e1542SZhi Yong Wu .ret = NOT_DONE, 3775b7e1542SZhi Yong Wu }; 3785b7e1542SZhi Yong Wu 3795b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 3805b7e1542SZhi Yong Wu return -ENOTSUP; 3815b7e1542SZhi Yong Wu } 3825b7e1542SZhi Yong Wu 3835b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 3845b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 3855b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 3865b7e1542SZhi Yong Wu } else { 3875b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 3885b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 3895b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 3905b7e1542SZhi Yong Wu qemu_aio_wait(); 3915b7e1542SZhi Yong Wu } 3925b7e1542SZhi Yong Wu } 3935b7e1542SZhi Yong Wu 3945b7e1542SZhi Yong Wu ret = cco.ret; 3955b7e1542SZhi Yong Wu g_free(cco.filename); 3965b7e1542SZhi Yong Wu 3975b7e1542SZhi Yong Wu return ret; 398ea2384d3Sbellard } 399ea2384d3Sbellard 40084a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 40184a12e66SChristoph Hellwig { 40284a12e66SChristoph Hellwig BlockDriver *drv; 40384a12e66SChristoph Hellwig 404b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 40584a12e66SChristoph Hellwig if (drv == NULL) { 40616905d71SStefan Hajnoczi return -ENOENT; 40784a12e66SChristoph Hellwig } 40884a12e66SChristoph Hellwig 40984a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 41084a12e66SChristoph Hellwig } 41184a12e66SChristoph Hellwig 412eba25057SJim Meyering /* 413eba25057SJim Meyering * Create a uniquely-named empty temporary file. 414eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 415eba25057SJim Meyering */ 416eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 417eba25057SJim Meyering { 418d5249393Sbellard #ifdef _WIN32 4193b9f94e1Sbellard char temp_dir[MAX_PATH]; 420eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 421eba25057SJim Meyering have length MAX_PATH or greater. */ 422eba25057SJim Meyering assert(size >= MAX_PATH); 423eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 424eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 425eba25057SJim Meyering ? 0 : -GetLastError()); 426d5249393Sbellard #else 427ea2384d3Sbellard int fd; 4287ccfb2ebSblueswir1 const char *tmpdir; 4290badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 4300badc1eeSaurel32 if (!tmpdir) 4310badc1eeSaurel32 tmpdir = "/tmp"; 432eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 433eba25057SJim Meyering return -EOVERFLOW; 434ea2384d3Sbellard } 435eba25057SJim Meyering fd = mkstemp(filename); 436fe235a06SDunrong Huang if (fd < 0) { 437fe235a06SDunrong Huang return -errno; 438fe235a06SDunrong Huang } 439fe235a06SDunrong Huang if (close(fd) != 0) { 440fe235a06SDunrong Huang unlink(filename); 441eba25057SJim Meyering return -errno; 442eba25057SJim Meyering } 443eba25057SJim Meyering return 0; 444d5249393Sbellard #endif 445eba25057SJim Meyering } 446ea2384d3Sbellard 447f3a5d3f8SChristoph Hellwig /* 448f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 449f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 450f3a5d3f8SChristoph Hellwig */ 451f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 452f3a5d3f8SChristoph Hellwig { 453508c7cb3SChristoph Hellwig int score_max = 0, score; 454508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 455f3a5d3f8SChristoph Hellwig 4568a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 457508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 458508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 459508c7cb3SChristoph Hellwig if (score > score_max) { 460508c7cb3SChristoph Hellwig score_max = score; 461508c7cb3SChristoph Hellwig drv = d; 462f3a5d3f8SChristoph Hellwig } 463508c7cb3SChristoph Hellwig } 464f3a5d3f8SChristoph Hellwig } 465f3a5d3f8SChristoph Hellwig 466508c7cb3SChristoph Hellwig return drv; 467f3a5d3f8SChristoph Hellwig } 468f3a5d3f8SChristoph Hellwig 469b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 47084a12e66SChristoph Hellwig { 47184a12e66SChristoph Hellwig BlockDriver *drv1; 47284a12e66SChristoph Hellwig char protocol[128]; 47384a12e66SChristoph Hellwig int len; 47484a12e66SChristoph Hellwig const char *p; 47584a12e66SChristoph Hellwig 47666f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 47766f82ceeSKevin Wolf 47839508e7aSChristoph Hellwig /* 47939508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 48039508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 48139508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 48239508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 48339508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 48439508e7aSChristoph Hellwig */ 48584a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 48639508e7aSChristoph Hellwig if (drv1) { 48784a12e66SChristoph Hellwig return drv1; 48884a12e66SChristoph Hellwig } 48939508e7aSChristoph Hellwig 4909e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 49139508e7aSChristoph Hellwig return bdrv_find_format("file"); 49239508e7aSChristoph Hellwig } 4939e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 4949e0b22f4SStefan Hajnoczi assert(p != NULL); 49584a12e66SChristoph Hellwig len = p - filename; 49684a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 49784a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 49884a12e66SChristoph Hellwig memcpy(protocol, filename, len); 49984a12e66SChristoph Hellwig protocol[len] = '\0'; 50084a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 50184a12e66SChristoph Hellwig if (drv1->protocol_name && 50284a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 50384a12e66SChristoph Hellwig return drv1; 50484a12e66SChristoph Hellwig } 50584a12e66SChristoph Hellwig } 50684a12e66SChristoph Hellwig return NULL; 50784a12e66SChristoph Hellwig } 50884a12e66SChristoph Hellwig 509c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv) 510ea2384d3Sbellard { 51183f64091Sbellard int ret, score, score_max; 512ea2384d3Sbellard BlockDriver *drv1, *drv; 51383f64091Sbellard uint8_t buf[2048]; 51483f64091Sbellard BlockDriverState *bs; 515ea2384d3Sbellard 516f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 517c98ac35dSStefan Weil if (ret < 0) { 518c98ac35dSStefan Weil *pdrv = NULL; 519c98ac35dSStefan Weil return ret; 520c98ac35dSStefan Weil } 521f8ea0b00SNicholas Bellinger 52208a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 52308a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 5241a396859SNicholas A. Bellinger bdrv_delete(bs); 525c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 526c98ac35dSStefan Weil if (!drv) { 527c98ac35dSStefan Weil ret = -ENOENT; 528c98ac35dSStefan Weil } 529c98ac35dSStefan Weil *pdrv = drv; 530c98ac35dSStefan Weil return ret; 5311a396859SNicholas A. Bellinger } 532f8ea0b00SNicholas Bellinger 53383f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 53483f64091Sbellard bdrv_delete(bs); 535ea2384d3Sbellard if (ret < 0) { 536c98ac35dSStefan Weil *pdrv = NULL; 537c98ac35dSStefan Weil return ret; 538ea2384d3Sbellard } 539ea2384d3Sbellard 540ea2384d3Sbellard score_max = 0; 54184a12e66SChristoph Hellwig drv = NULL; 5428a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 54383f64091Sbellard if (drv1->bdrv_probe) { 544ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 545ea2384d3Sbellard if (score > score_max) { 546ea2384d3Sbellard score_max = score; 547ea2384d3Sbellard drv = drv1; 548ea2384d3Sbellard } 549ea2384d3Sbellard } 55083f64091Sbellard } 551c98ac35dSStefan Weil if (!drv) { 552c98ac35dSStefan Weil ret = -ENOENT; 553c98ac35dSStefan Weil } 554c98ac35dSStefan Weil *pdrv = drv; 555c98ac35dSStefan Weil return ret; 556ea2384d3Sbellard } 557ea2384d3Sbellard 55851762288SStefan Hajnoczi /** 55951762288SStefan Hajnoczi * Set the current 'total_sectors' value 56051762288SStefan Hajnoczi */ 56151762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 56251762288SStefan Hajnoczi { 56351762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 56451762288SStefan Hajnoczi 565396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 566396759adSNicholas Bellinger if (bs->sg) 567396759adSNicholas Bellinger return 0; 568396759adSNicholas Bellinger 56951762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 57051762288SStefan Hajnoczi if (drv->bdrv_getlength) { 57151762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 57251762288SStefan Hajnoczi if (length < 0) { 57351762288SStefan Hajnoczi return length; 57451762288SStefan Hajnoczi } 57551762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 57651762288SStefan Hajnoczi } 57751762288SStefan Hajnoczi 57851762288SStefan Hajnoczi bs->total_sectors = hint; 57951762288SStefan Hajnoczi return 0; 58051762288SStefan Hajnoczi } 58151762288SStefan Hajnoczi 582c3993cdcSStefan Hajnoczi /** 583c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 584c3993cdcSStefan Hajnoczi * 585c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 586c3993cdcSStefan Hajnoczi */ 587c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 588c3993cdcSStefan Hajnoczi { 589c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 590c3993cdcSStefan Hajnoczi 591c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 592c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 59392196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 59492196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 595c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 596c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 597c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 598c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 599c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 600c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 601c3993cdcSStefan Hajnoczi /* this is the default */ 602c3993cdcSStefan Hajnoczi } else { 603c3993cdcSStefan Hajnoczi return -1; 604c3993cdcSStefan Hajnoczi } 605c3993cdcSStefan Hajnoczi 606c3993cdcSStefan Hajnoczi return 0; 607c3993cdcSStefan Hajnoczi } 608c3993cdcSStefan Hajnoczi 60953fec9d3SStefan Hajnoczi /** 61053fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 61153fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 61253fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 61353fec9d3SStefan Hajnoczi */ 61453fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 61553fec9d3SStefan Hajnoczi { 61653fec9d3SStefan Hajnoczi bs->copy_on_read++; 61753fec9d3SStefan Hajnoczi } 61853fec9d3SStefan Hajnoczi 61953fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 62053fec9d3SStefan Hajnoczi { 62153fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 62253fec9d3SStefan Hajnoczi bs->copy_on_read--; 62353fec9d3SStefan Hajnoczi } 62453fec9d3SStefan Hajnoczi 625b6ce07aaSKevin Wolf /* 62657915332SKevin Wolf * Common part for opening disk images and files 62757915332SKevin Wolf */ 62857915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 62957915332SKevin Wolf int flags, BlockDriver *drv) 63057915332SKevin Wolf { 63157915332SKevin Wolf int ret, open_flags; 63257915332SKevin Wolf 63357915332SKevin Wolf assert(drv != NULL); 6346405875cSPaolo Bonzini assert(bs->file == NULL); 63557915332SKevin Wolf 63628dcee10SStefan Hajnoczi trace_bdrv_open_common(bs, filename, flags, drv->format_name); 63728dcee10SStefan Hajnoczi 63857915332SKevin Wolf bs->open_flags = flags; 63957915332SKevin Wolf bs->buffer_alignment = 512; 64057915332SKevin Wolf 64153fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 64253fec9d3SStefan Hajnoczi if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) { 64353fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 64453fec9d3SStefan Hajnoczi } 64553fec9d3SStefan Hajnoczi 64657915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 64757915332SKevin Wolf 64857915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 64957915332SKevin Wolf return -ENOTSUP; 65057915332SKevin Wolf } 65157915332SKevin Wolf 65257915332SKevin Wolf bs->drv = drv; 6537267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 65457915332SKevin Wolf 65503f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 656e1e9b0acSPaolo Bonzini open_flags = flags | BDRV_O_CACHE_WB; 65757915332SKevin Wolf 65857915332SKevin Wolf /* 65957915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 66057915332SKevin Wolf * image. 66157915332SKevin Wolf */ 662e1e9b0acSPaolo Bonzini open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 66357915332SKevin Wolf 66457915332SKevin Wolf /* 665ebabb67aSStefan Weil * Snapshots should be writable. 66657915332SKevin Wolf */ 66757915332SKevin Wolf if (bs->is_temporary) { 66857915332SKevin Wolf open_flags |= BDRV_O_RDWR; 66957915332SKevin Wolf } 67057915332SKevin Wolf 671be028adcSJeff Cody bs->read_only = !(open_flags & BDRV_O_RDWR); 672e7c63796SStefan Hajnoczi 67366f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 67466f82ceeSKevin Wolf if (drv->bdrv_file_open) { 67566f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 67666f82ceeSKevin Wolf } else { 67766f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 67866f82ceeSKevin Wolf if (ret >= 0) { 67966f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 68066f82ceeSKevin Wolf } 68166f82ceeSKevin Wolf } 68266f82ceeSKevin Wolf 68357915332SKevin Wolf if (ret < 0) { 68457915332SKevin Wolf goto free_and_fail; 68557915332SKevin Wolf } 68657915332SKevin Wolf 68751762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 68851762288SStefan Hajnoczi if (ret < 0) { 68951762288SStefan Hajnoczi goto free_and_fail; 69057915332SKevin Wolf } 69151762288SStefan Hajnoczi 69257915332SKevin Wolf #ifndef _WIN32 69357915332SKevin Wolf if (bs->is_temporary) { 69457915332SKevin Wolf unlink(filename); 69557915332SKevin Wolf } 69657915332SKevin Wolf #endif 69757915332SKevin Wolf return 0; 69857915332SKevin Wolf 69957915332SKevin Wolf free_and_fail: 70066f82ceeSKevin Wolf if (bs->file) { 70166f82ceeSKevin Wolf bdrv_delete(bs->file); 70266f82ceeSKevin Wolf bs->file = NULL; 70366f82ceeSKevin Wolf } 7047267c094SAnthony Liguori g_free(bs->opaque); 70557915332SKevin Wolf bs->opaque = NULL; 70657915332SKevin Wolf bs->drv = NULL; 70757915332SKevin Wolf return ret; 70857915332SKevin Wolf } 70957915332SKevin Wolf 71057915332SKevin Wolf /* 711b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 712b6ce07aaSKevin Wolf */ 71383f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 714b338082bSbellard { 71583f64091Sbellard BlockDriverState *bs; 7166db95603SChristoph Hellwig BlockDriver *drv; 71783f64091Sbellard int ret; 7183b0d4f61Sbellard 719b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 7206db95603SChristoph Hellwig if (!drv) { 7216db95603SChristoph Hellwig return -ENOENT; 7226db95603SChristoph Hellwig } 7236db95603SChristoph Hellwig 72483f64091Sbellard bs = bdrv_new(""); 725b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 72683f64091Sbellard if (ret < 0) { 72783f64091Sbellard bdrv_delete(bs); 72883f64091Sbellard return ret; 7293b0d4f61Sbellard } 73071d0770cSaliguori bs->growable = 1; 73183f64091Sbellard *pbs = bs; 73283f64091Sbellard return 0; 7333b0d4f61Sbellard } 7343b0d4f61Sbellard 735b6ce07aaSKevin Wolf /* 736b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 737b6ce07aaSKevin Wolf */ 738d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 739ea2384d3Sbellard BlockDriver *drv) 740ea2384d3Sbellard { 741b6ce07aaSKevin Wolf int ret; 7422b572816SKevin Wolf char tmp_filename[PATH_MAX]; 74333e3963eSbellard 74483f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 745ea2384d3Sbellard BlockDriverState *bs1; 746ea2384d3Sbellard int64_t total_size; 7477c96d46eSaliguori int is_protocol = 0; 74891a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 74991a073a9SKevin Wolf QEMUOptionParameter *options; 750b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 75133e3963eSbellard 752ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 753ea2384d3Sbellard instead of opening 'filename' directly */ 754ea2384d3Sbellard 755ea2384d3Sbellard /* if there is a backing file, use it */ 756ea2384d3Sbellard bs1 = bdrv_new(""); 757d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 75851d7c00cSaliguori if (ret < 0) { 759ea2384d3Sbellard bdrv_delete(bs1); 76051d7c00cSaliguori return ret; 761ea2384d3Sbellard } 7623e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 7637c96d46eSaliguori 7647c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 7657c96d46eSaliguori is_protocol = 1; 7667c96d46eSaliguori 767ea2384d3Sbellard bdrv_delete(bs1); 768ea2384d3Sbellard 769eba25057SJim Meyering ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 770eba25057SJim Meyering if (ret < 0) { 771eba25057SJim Meyering return ret; 772eba25057SJim Meyering } 7737c96d46eSaliguori 7747c96d46eSaliguori /* Real path is meaningless for protocols */ 7757c96d46eSaliguori if (is_protocol) 7767c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 7777c96d46eSaliguori "%s", filename); 778114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 779114cdfa9SKirill A. Shutemov return -errno; 7807c96d46eSaliguori 78191a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 78291a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 78391a073a9SKevin Wolf 7843e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 78591a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 78691a073a9SKevin Wolf if (drv) { 78791a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 78891a073a9SKevin Wolf drv->format_name); 78991a073a9SKevin Wolf } 79091a073a9SKevin Wolf 79191a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 792d748768cSJan Kiszka free_option_parameters(options); 79351d7c00cSaliguori if (ret < 0) { 79451d7c00cSaliguori return ret; 795ea2384d3Sbellard } 79691a073a9SKevin Wolf 797ea2384d3Sbellard filename = tmp_filename; 79891a073a9SKevin Wolf drv = bdrv_qcow2; 799ea2384d3Sbellard bs->is_temporary = 1; 800ea2384d3Sbellard } 801ea2384d3Sbellard 802b6ce07aaSKevin Wolf /* Find the right image format driver */ 8036db95603SChristoph Hellwig if (!drv) { 804c98ac35dSStefan Weil ret = find_image_format(filename, &drv); 805ea2384d3Sbellard } 8066987307cSChristoph Hellwig 80751d7c00cSaliguori if (!drv) { 80851d7c00cSaliguori goto unlink_and_fail; 80983f64091Sbellard } 810b6ce07aaSKevin Wolf 811be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 812be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 813be028adcSJeff Cody } 814be028adcSJeff Cody 815be028adcSJeff Cody bs->keep_read_only = !(flags & BDRV_O_ALLOW_RDWR); 816be028adcSJeff Cody 817b6ce07aaSKevin Wolf /* Open the image */ 818b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 819b6ce07aaSKevin Wolf if (ret < 0) { 8206987307cSChristoph Hellwig goto unlink_and_fail; 8216987307cSChristoph Hellwig } 8226987307cSChristoph Hellwig 823b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 824b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 825b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 826b6ce07aaSKevin Wolf int back_flags; 827b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 828b6ce07aaSKevin Wolf 829b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 830dc5a1371SPaolo Bonzini bdrv_get_full_backing_filename(bs, backing_filename, 831dc5a1371SPaolo Bonzini sizeof(backing_filename)); 832df2dbb4aSStefan Hajnoczi 833df2dbb4aSStefan Hajnoczi if (bs->backing_format[0] != '\0') { 834b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 835df2dbb4aSStefan Hajnoczi } 836b6ce07aaSKevin Wolf 837b6ce07aaSKevin Wolf /* backing files always opened read-only */ 838b6ce07aaSKevin Wolf back_flags = 839b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 840b6ce07aaSKevin Wolf 841b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 842b6ce07aaSKevin Wolf if (ret < 0) { 843b6ce07aaSKevin Wolf bdrv_close(bs); 844b6ce07aaSKevin Wolf return ret; 845b6ce07aaSKevin Wolf } 846b6ce07aaSKevin Wolf } 847b6ce07aaSKevin Wolf 848b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 8497d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 850b6ce07aaSKevin Wolf } 851b6ce07aaSKevin Wolf 85298f90dbaSZhi Yong Wu /* throttling disk I/O limits */ 85398f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 85498f90dbaSZhi Yong Wu bdrv_io_limits_enable(bs); 85598f90dbaSZhi Yong Wu } 85698f90dbaSZhi Yong Wu 857b6ce07aaSKevin Wolf return 0; 858b6ce07aaSKevin Wolf 859b6ce07aaSKevin Wolf unlink_and_fail: 860b6ce07aaSKevin Wolf if (bs->is_temporary) { 861b6ce07aaSKevin Wolf unlink(filename); 862b6ce07aaSKevin Wolf } 863b6ce07aaSKevin Wolf return ret; 864b6ce07aaSKevin Wolf } 865b6ce07aaSKevin Wolf 866fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 867fc01f7e7Sbellard { 86880ccf93bSLiu Yuan bdrv_flush(bs); 86919cb3738Sbellard if (bs->drv) { 8703e914655SPaolo Bonzini if (bs->job) { 8713e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 8723e914655SPaolo Bonzini } 8737094f12fSKevin Wolf bdrv_drain_all(); 8747094f12fSKevin Wolf 875f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 876f9092b10SMarkus Armbruster bs_snapshots = NULL; 877f9092b10SMarkus Armbruster } 878557df6acSStefan Hajnoczi if (bs->backing_hd) { 879ea2384d3Sbellard bdrv_delete(bs->backing_hd); 880557df6acSStefan Hajnoczi bs->backing_hd = NULL; 881557df6acSStefan Hajnoczi } 882ea2384d3Sbellard bs->drv->bdrv_close(bs); 8837267c094SAnthony Liguori g_free(bs->opaque); 884ea2384d3Sbellard #ifdef _WIN32 885ea2384d3Sbellard if (bs->is_temporary) { 886ea2384d3Sbellard unlink(bs->filename); 887ea2384d3Sbellard } 88867b915a5Sbellard #endif 889ea2384d3Sbellard bs->opaque = NULL; 890ea2384d3Sbellard bs->drv = NULL; 89153fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 892a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 893a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 8946405875cSPaolo Bonzini bs->total_sectors = 0; 8956405875cSPaolo Bonzini bs->encrypted = 0; 8966405875cSPaolo Bonzini bs->valid_key = 0; 8976405875cSPaolo Bonzini bs->sg = 0; 8986405875cSPaolo Bonzini bs->growable = 0; 899b338082bSbellard 90066f82ceeSKevin Wolf if (bs->file != NULL) { 9010ac9377dSPaolo Bonzini bdrv_delete(bs->file); 9020ac9377dSPaolo Bonzini bs->file = NULL; 90366f82ceeSKevin Wolf } 9049ca11154SPavel Hrdina } 90566f82ceeSKevin Wolf 9067d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 90798f90dbaSZhi Yong Wu 90898f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 90998f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 91098f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 91198f90dbaSZhi Yong Wu } 912b338082bSbellard } 913b338082bSbellard 9142bc93fedSMORITA Kazutaka void bdrv_close_all(void) 9152bc93fedSMORITA Kazutaka { 9162bc93fedSMORITA Kazutaka BlockDriverState *bs; 9172bc93fedSMORITA Kazutaka 9182bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 9192bc93fedSMORITA Kazutaka bdrv_close(bs); 9202bc93fedSMORITA Kazutaka } 9212bc93fedSMORITA Kazutaka } 9222bc93fedSMORITA Kazutaka 923922453bcSStefan Hajnoczi /* 924922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 925922453bcSStefan Hajnoczi * 926922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 927922453bcSStefan Hajnoczi * after calling this function. 9284c355d53SZhi Yong Wu * 9294c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 9304c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 9314c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 9324c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 9334c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 934922453bcSStefan Hajnoczi */ 935922453bcSStefan Hajnoczi void bdrv_drain_all(void) 936922453bcSStefan Hajnoczi { 937922453bcSStefan Hajnoczi BlockDriverState *bs; 9384c355d53SZhi Yong Wu bool busy; 939922453bcSStefan Hajnoczi 9404c355d53SZhi Yong Wu do { 9414c355d53SZhi Yong Wu busy = qemu_aio_wait(); 9424c355d53SZhi Yong Wu 9434c355d53SZhi Yong Wu /* FIXME: We do not have timer support here, so this is effectively 9444c355d53SZhi Yong Wu * a busy wait. 9454c355d53SZhi Yong Wu */ 9464c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 9474c355d53SZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 9484c355d53SZhi Yong Wu qemu_co_queue_restart_all(&bs->throttled_reqs); 9494c355d53SZhi Yong Wu busy = true; 9504c355d53SZhi Yong Wu } 9514c355d53SZhi Yong Wu } 9524c355d53SZhi Yong Wu } while (busy); 953922453bcSStefan Hajnoczi 954922453bcSStefan Hajnoczi /* If requests are still pending there is a bug somewhere */ 955922453bcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 956922453bcSStefan Hajnoczi assert(QLIST_EMPTY(&bs->tracked_requests)); 957922453bcSStefan Hajnoczi assert(qemu_co_queue_empty(&bs->throttled_reqs)); 958922453bcSStefan Hajnoczi } 959922453bcSStefan Hajnoczi } 960922453bcSStefan Hajnoczi 961d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 962d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 963d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 964d22b2f41SRyan Harper { 965d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 966d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 967d22b2f41SRyan Harper } 968d22b2f41SRyan Harper bs->device_name[0] = '\0'; 969d22b2f41SRyan Harper } 970d22b2f41SRyan Harper 971e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 972e023b2e2SPaolo Bonzini { 973e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 974e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 975e023b2e2SPaolo Bonzini } 976e023b2e2SPaolo Bonzini } 977e023b2e2SPaolo Bonzini 9784ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 9794ddc07caSPaolo Bonzini BlockDriverState *bs_src) 9804ddc07caSPaolo Bonzini { 9814ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 9824ddc07caSPaolo Bonzini bs_dest->open_flags = bs_src->open_flags; 9834ddc07caSPaolo Bonzini 9844ddc07caSPaolo Bonzini /* dev info */ 9854ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 9864ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 9874ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 9884ddc07caSPaolo Bonzini bs_dest->buffer_alignment = bs_src->buffer_alignment; 9894ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 9904ddc07caSPaolo Bonzini 9914ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 9924ddc07caSPaolo Bonzini 9934ddc07caSPaolo Bonzini /* i/o timing parameters */ 9944ddc07caSPaolo Bonzini bs_dest->slice_time = bs_src->slice_time; 9954ddc07caSPaolo Bonzini bs_dest->slice_start = bs_src->slice_start; 9964ddc07caSPaolo Bonzini bs_dest->slice_end = bs_src->slice_end; 9974ddc07caSPaolo Bonzini bs_dest->io_limits = bs_src->io_limits; 9984ddc07caSPaolo Bonzini bs_dest->io_base = bs_src->io_base; 9994ddc07caSPaolo Bonzini bs_dest->throttled_reqs = bs_src->throttled_reqs; 10004ddc07caSPaolo Bonzini bs_dest->block_timer = bs_src->block_timer; 10014ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 10024ddc07caSPaolo Bonzini 10034ddc07caSPaolo Bonzini /* r/w error */ 10044ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 10054ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 10064ddc07caSPaolo Bonzini 10074ddc07caSPaolo Bonzini /* i/o status */ 10084ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 10094ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 10104ddc07caSPaolo Bonzini 10114ddc07caSPaolo Bonzini /* dirty bitmap */ 10124ddc07caSPaolo Bonzini bs_dest->dirty_count = bs_src->dirty_count; 10134ddc07caSPaolo Bonzini bs_dest->dirty_bitmap = bs_src->dirty_bitmap; 10144ddc07caSPaolo Bonzini 10154ddc07caSPaolo Bonzini /* job */ 10164ddc07caSPaolo Bonzini bs_dest->in_use = bs_src->in_use; 10174ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 10184ddc07caSPaolo Bonzini 10194ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 10204ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 10214ddc07caSPaolo Bonzini bs_src->device_name); 10224ddc07caSPaolo Bonzini bs_dest->list = bs_src->list; 10234ddc07caSPaolo Bonzini } 10244ddc07caSPaolo Bonzini 10254ddc07caSPaolo Bonzini /* 10264ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 10274ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 10284ddc07caSPaolo Bonzini * actually attached to a device. 10294ddc07caSPaolo Bonzini * 10304ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 10314ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 10324ddc07caSPaolo Bonzini * 10334ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 10344ddc07caSPaolo Bonzini * 10354ddc07caSPaolo Bonzini * This function does not create any image files. 10364ddc07caSPaolo Bonzini */ 10374ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 10384ddc07caSPaolo Bonzini { 10394ddc07caSPaolo Bonzini BlockDriverState tmp; 10404ddc07caSPaolo Bonzini 10414ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 10424ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 10434ddc07caSPaolo Bonzini assert(bs_new->dirty_bitmap == NULL); 10444ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 10454ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 10464ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 10474ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 10484ddc07caSPaolo Bonzini assert(bs_new->block_timer == NULL); 10494ddc07caSPaolo Bonzini 10504ddc07caSPaolo Bonzini tmp = *bs_new; 10514ddc07caSPaolo Bonzini *bs_new = *bs_old; 10524ddc07caSPaolo Bonzini *bs_old = tmp; 10534ddc07caSPaolo Bonzini 10544ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 10554ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 10564ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 10574ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 10584ddc07caSPaolo Bonzini 10594ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 10604ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 10614ddc07caSPaolo Bonzini 10624ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 10634ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 10644ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 10654ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 10664ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 10674ddc07caSPaolo Bonzini assert(bs_new->block_timer == NULL); 10684ddc07caSPaolo Bonzini 10694ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 10704ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 10714ddc07caSPaolo Bonzini } 10724ddc07caSPaolo Bonzini 10738802d1fdSJeff Cody /* 10748802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 10758802d1fdSJeff Cody * live, while keeping required fields on the top layer. 10768802d1fdSJeff Cody * 10778802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 10788802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 10798802d1fdSJeff Cody * 1080f6801b83SJeff Cody * bs_new is required to be anonymous. 1081f6801b83SJeff Cody * 10828802d1fdSJeff Cody * This function does not create any image files. 10838802d1fdSJeff Cody */ 10848802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 10858802d1fdSJeff Cody { 10864ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 10878802d1fdSJeff Cody 10888802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 10898802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 10904ddc07caSPaolo Bonzini bs_top->backing_hd = bs_new; 10914ddc07caSPaolo Bonzini bs_top->open_flags &= ~BDRV_O_NO_BACKING; 10924ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 10934ddc07caSPaolo Bonzini bs_new->filename); 10944ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 10954ddc07caSPaolo Bonzini bs_new->drv ? bs_new->drv->format_name : ""); 10968802d1fdSJeff Cody } 10978802d1fdSJeff Cody 1098b338082bSbellard void bdrv_delete(BlockDriverState *bs) 1099b338082bSbellard { 1100fa879d62SMarkus Armbruster assert(!bs->dev); 11013e914655SPaolo Bonzini assert(!bs->job); 11023e914655SPaolo Bonzini assert(!bs->in_use); 110318846deeSMarkus Armbruster 11041b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1105d22b2f41SRyan Harper bdrv_make_anon(bs); 110634c6f050Saurel32 1107b338082bSbellard bdrv_close(bs); 110866f82ceeSKevin Wolf 1109f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 11107267c094SAnthony Liguori g_free(bs); 1111fc01f7e7Sbellard } 1112fc01f7e7Sbellard 1113fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1114fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 111518846deeSMarkus Armbruster { 1116fa879d62SMarkus Armbruster if (bs->dev) { 111718846deeSMarkus Armbruster return -EBUSY; 111818846deeSMarkus Armbruster } 1119fa879d62SMarkus Armbruster bs->dev = dev; 112028a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 112118846deeSMarkus Armbruster return 0; 112218846deeSMarkus Armbruster } 112318846deeSMarkus Armbruster 1124fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1125fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 112618846deeSMarkus Armbruster { 1127fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1128fa879d62SMarkus Armbruster abort(); 1129fa879d62SMarkus Armbruster } 1130fa879d62SMarkus Armbruster } 1131fa879d62SMarkus Armbruster 1132fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1133fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1134fa879d62SMarkus Armbruster { 1135fa879d62SMarkus Armbruster assert(bs->dev == dev); 1136fa879d62SMarkus Armbruster bs->dev = NULL; 11370e49de52SMarkus Armbruster bs->dev_ops = NULL; 11380e49de52SMarkus Armbruster bs->dev_opaque = NULL; 113929e05f20SMarkus Armbruster bs->buffer_alignment = 512; 114018846deeSMarkus Armbruster } 114118846deeSMarkus Armbruster 1142fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1143fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 114418846deeSMarkus Armbruster { 1145fa879d62SMarkus Armbruster return bs->dev; 114618846deeSMarkus Armbruster } 114718846deeSMarkus Armbruster 11480e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 11490e49de52SMarkus Armbruster void *opaque) 11500e49de52SMarkus Armbruster { 11510e49de52SMarkus Armbruster bs->dev_ops = ops; 11520e49de52SMarkus Armbruster bs->dev_opaque = opaque; 11532c6942faSMarkus Armbruster if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) { 11542c6942faSMarkus Armbruster bs_snapshots = NULL; 11552c6942faSMarkus Armbruster } 11560e49de52SMarkus Armbruster } 11570e49de52SMarkus Armbruster 1158329c0a48SLuiz Capitulino void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 1159329c0a48SLuiz Capitulino BlockQMPEventAction action, int is_read) 1160329c0a48SLuiz Capitulino { 1161329c0a48SLuiz Capitulino QObject *data; 1162329c0a48SLuiz Capitulino const char *action_str; 1163329c0a48SLuiz Capitulino 1164329c0a48SLuiz Capitulino switch (action) { 1165329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1166329c0a48SLuiz Capitulino action_str = "report"; 1167329c0a48SLuiz Capitulino break; 1168329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1169329c0a48SLuiz Capitulino action_str = "ignore"; 1170329c0a48SLuiz Capitulino break; 1171329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1172329c0a48SLuiz Capitulino action_str = "stop"; 1173329c0a48SLuiz Capitulino break; 1174329c0a48SLuiz Capitulino default: 1175329c0a48SLuiz Capitulino abort(); 1176329c0a48SLuiz Capitulino } 1177329c0a48SLuiz Capitulino 1178329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1179329c0a48SLuiz Capitulino bdrv->device_name, 1180329c0a48SLuiz Capitulino action_str, 1181329c0a48SLuiz Capitulino is_read ? "read" : "write"); 1182329c0a48SLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 1183329c0a48SLuiz Capitulino 1184329c0a48SLuiz Capitulino qobject_decref(data); 1185329c0a48SLuiz Capitulino } 1186329c0a48SLuiz Capitulino 11876f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 11886f382ed2SLuiz Capitulino { 11896f382ed2SLuiz Capitulino QObject *data; 11906f382ed2SLuiz Capitulino 11916f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 11926f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 11936f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 11946f382ed2SLuiz Capitulino 11956f382ed2SLuiz Capitulino qobject_decref(data); 11966f382ed2SLuiz Capitulino } 11976f382ed2SLuiz Capitulino 11987d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 11990e49de52SMarkus Armbruster { 1200145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 12016f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 12027d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 12036f382ed2SLuiz Capitulino if (tray_was_closed) { 12046f382ed2SLuiz Capitulino /* tray open */ 12056f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 12066f382ed2SLuiz Capitulino } 12076f382ed2SLuiz Capitulino if (load) { 12086f382ed2SLuiz Capitulino /* tray close */ 12096f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 12106f382ed2SLuiz Capitulino } 1211145feb17SMarkus Armbruster } 1212145feb17SMarkus Armbruster } 1213145feb17SMarkus Armbruster 12142c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 12152c6942faSMarkus Armbruster { 12162c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 12172c6942faSMarkus Armbruster } 12182c6942faSMarkus Armbruster 1219025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1220025ccaa7SPaolo Bonzini { 1221025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1222025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1223025ccaa7SPaolo Bonzini } 1224025ccaa7SPaolo Bonzini } 1225025ccaa7SPaolo Bonzini 1226e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1227e4def80bSMarkus Armbruster { 1228e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1229e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1230e4def80bSMarkus Armbruster } 1231e4def80bSMarkus Armbruster return false; 1232e4def80bSMarkus Armbruster } 1233e4def80bSMarkus Armbruster 1234145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1235145feb17SMarkus Armbruster { 1236145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1237145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 12380e49de52SMarkus Armbruster } 12390e49de52SMarkus Armbruster } 12400e49de52SMarkus Armbruster 1241f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1242f107639aSMarkus Armbruster { 1243f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1244f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1245f107639aSMarkus Armbruster } 1246f107639aSMarkus Armbruster return false; 1247f107639aSMarkus Armbruster } 1248f107639aSMarkus Armbruster 1249e97fc193Saliguori /* 1250e97fc193Saliguori * Run consistency checks on an image 1251e97fc193Saliguori * 1252e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1253a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1254e076f338SKevin Wolf * check are stored in res. 1255e97fc193Saliguori */ 12564534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 1257e97fc193Saliguori { 1258e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1259e97fc193Saliguori return -ENOTSUP; 1260e97fc193Saliguori } 1261e97fc193Saliguori 1262e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 12634534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 1264e97fc193Saliguori } 1265e97fc193Saliguori 12668a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 12678a426614SKevin Wolf 126833e3963eSbellard /* commit COW file into the raw image */ 126933e3963eSbellard int bdrv_commit(BlockDriverState *bs) 127033e3963eSbellard { 127119cb3738Sbellard BlockDriver *drv = bs->drv; 1272ee181196SKevin Wolf BlockDriver *backing_drv; 12738a426614SKevin Wolf int64_t sector, total_sectors; 12748a426614SKevin Wolf int n, ro, open_flags; 12754dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 12768a426614SKevin Wolf uint8_t *buf; 12774dca4b63SNaphtali Sprei char filename[1024]; 12784dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 127933e3963eSbellard 128019cb3738Sbellard if (!drv) 128119cb3738Sbellard return -ENOMEDIUM; 128233e3963eSbellard 12834dca4b63SNaphtali Sprei if (!bs->backing_hd) { 12844dca4b63SNaphtali Sprei return -ENOTSUP; 12854dca4b63SNaphtali Sprei } 12864dca4b63SNaphtali Sprei 12874dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 1288ea2384d3Sbellard return -EACCES; 128933e3963eSbellard } 129033e3963eSbellard 12912d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 12922d3735d3SStefan Hajnoczi return -EBUSY; 12932d3735d3SStefan Hajnoczi } 12942d3735d3SStefan Hajnoczi 1295ee181196SKevin Wolf backing_drv = bs->backing_hd->drv; 12964dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 12974dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 12984dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 12994dca4b63SNaphtali Sprei 13004dca4b63SNaphtali Sprei if (ro) { 13014dca4b63SNaphtali Sprei /* re-open as RW */ 13024dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 13034dca4b63SNaphtali Sprei bs->backing_hd = NULL; 13044dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 1305ee181196SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, 1306ee181196SKevin Wolf backing_drv); 13074dca4b63SNaphtali Sprei if (rw_ret < 0) { 13084dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 13094dca4b63SNaphtali Sprei /* try to re-open read-only */ 13104dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1311ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1312ee181196SKevin Wolf backing_drv); 13134dca4b63SNaphtali Sprei if (ret < 0) { 13144dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 13154dca4b63SNaphtali Sprei /* drive not functional anymore */ 13164dca4b63SNaphtali Sprei bs->drv = NULL; 13174dca4b63SNaphtali Sprei return ret; 13184dca4b63SNaphtali Sprei } 13194dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 13204dca4b63SNaphtali Sprei return rw_ret; 13214dca4b63SNaphtali Sprei } 13224dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 1323ea2384d3Sbellard } 1324ea2384d3Sbellard 13256ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 13267267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 13278a426614SKevin Wolf 13288a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 132905c4af54SStefan Hajnoczi if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 13308a426614SKevin Wolf 13318a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 13324dca4b63SNaphtali Sprei ret = -EIO; 13334dca4b63SNaphtali Sprei goto ro_cleanup; 133433e3963eSbellard } 133533e3963eSbellard 13368a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 13374dca4b63SNaphtali Sprei ret = -EIO; 13384dca4b63SNaphtali Sprei goto ro_cleanup; 133933e3963eSbellard } 134033e3963eSbellard } 134133e3963eSbellard } 134295389c86Sbellard 13431d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 13441d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 13451d44952fSChristoph Hellwig bdrv_flush(bs); 13461d44952fSChristoph Hellwig } 134795389c86Sbellard 13483f5075aeSChristoph Hellwig /* 13493f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 13503f5075aeSChristoph Hellwig * stable on disk. 13513f5075aeSChristoph Hellwig */ 13523f5075aeSChristoph Hellwig if (bs->backing_hd) 13533f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 13544dca4b63SNaphtali Sprei 13554dca4b63SNaphtali Sprei ro_cleanup: 13567267c094SAnthony Liguori g_free(buf); 13574dca4b63SNaphtali Sprei 13584dca4b63SNaphtali Sprei if (ro) { 13594dca4b63SNaphtali Sprei /* re-open as RO */ 13604dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 13614dca4b63SNaphtali Sprei bs->backing_hd = NULL; 13624dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1363ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1364ee181196SKevin Wolf backing_drv); 13654dca4b63SNaphtali Sprei if (ret < 0) { 13664dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 13674dca4b63SNaphtali Sprei /* drive not functional anymore */ 13684dca4b63SNaphtali Sprei bs->drv = NULL; 13694dca4b63SNaphtali Sprei return ret; 13704dca4b63SNaphtali Sprei } 13714dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 13724dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 13734dca4b63SNaphtali Sprei } 13744dca4b63SNaphtali Sprei 13751d44952fSChristoph Hellwig return ret; 137633e3963eSbellard } 137733e3963eSbellard 1378e8877497SStefan Hajnoczi int bdrv_commit_all(void) 13796ab4b5abSMarkus Armbruster { 13806ab4b5abSMarkus Armbruster BlockDriverState *bs; 13816ab4b5abSMarkus Armbruster 13826ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1383e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1384e8877497SStefan Hajnoczi if (ret < 0) { 1385e8877497SStefan Hajnoczi return ret; 13866ab4b5abSMarkus Armbruster } 13876ab4b5abSMarkus Armbruster } 1388e8877497SStefan Hajnoczi return 0; 1389e8877497SStefan Hajnoczi } 13906ab4b5abSMarkus Armbruster 1391dbffbdcfSStefan Hajnoczi struct BdrvTrackedRequest { 1392dbffbdcfSStefan Hajnoczi BlockDriverState *bs; 1393dbffbdcfSStefan Hajnoczi int64_t sector_num; 1394dbffbdcfSStefan Hajnoczi int nb_sectors; 1395dbffbdcfSStefan Hajnoczi bool is_write; 1396dbffbdcfSStefan Hajnoczi QLIST_ENTRY(BdrvTrackedRequest) list; 13975f8b6491SStefan Hajnoczi Coroutine *co; /* owner, used for deadlock detection */ 1398f4658285SStefan Hajnoczi CoQueue wait_queue; /* coroutines blocked on this request */ 1399dbffbdcfSStefan Hajnoczi }; 1400dbffbdcfSStefan Hajnoczi 1401dbffbdcfSStefan Hajnoczi /** 1402dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1403dbffbdcfSStefan Hajnoczi * 1404dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1405dbffbdcfSStefan Hajnoczi */ 1406dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1407dbffbdcfSStefan Hajnoczi { 1408dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1409f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1410dbffbdcfSStefan Hajnoczi } 1411dbffbdcfSStefan Hajnoczi 1412dbffbdcfSStefan Hajnoczi /** 1413dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1414dbffbdcfSStefan Hajnoczi */ 1415dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1416dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1417dbffbdcfSStefan Hajnoczi int64_t sector_num, 1418dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1419dbffbdcfSStefan Hajnoczi { 1420dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1421dbffbdcfSStefan Hajnoczi .bs = bs, 1422dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1423dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1424dbffbdcfSStefan Hajnoczi .is_write = is_write, 14255f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1426dbffbdcfSStefan Hajnoczi }; 1427dbffbdcfSStefan Hajnoczi 1428f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1429f4658285SStefan Hajnoczi 1430dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1431dbffbdcfSStefan Hajnoczi } 1432dbffbdcfSStefan Hajnoczi 1433d83947acSStefan Hajnoczi /** 1434d83947acSStefan Hajnoczi * Round a region to cluster boundaries 1435d83947acSStefan Hajnoczi */ 1436d83947acSStefan Hajnoczi static void round_to_clusters(BlockDriverState *bs, 1437d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 1438d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 1439d83947acSStefan Hajnoczi int *cluster_nb_sectors) 1440d83947acSStefan Hajnoczi { 1441d83947acSStefan Hajnoczi BlockDriverInfo bdi; 1442d83947acSStefan Hajnoczi 1443d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 1444d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 1445d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 1446d83947acSStefan Hajnoczi } else { 1447d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 1448d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 1449d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 1450d83947acSStefan Hajnoczi nb_sectors, c); 1451d83947acSStefan Hajnoczi } 1452d83947acSStefan Hajnoczi } 1453d83947acSStefan Hajnoczi 1454f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 1455f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 1456d83947acSStefan Hajnoczi /* aaaa bbbb */ 1457d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 1458d83947acSStefan Hajnoczi return false; 1459d83947acSStefan Hajnoczi } 1460d83947acSStefan Hajnoczi /* bbbb aaaa */ 1461d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 1462d83947acSStefan Hajnoczi return false; 1463d83947acSStefan Hajnoczi } 1464d83947acSStefan Hajnoczi return true; 1465f4658285SStefan Hajnoczi } 1466f4658285SStefan Hajnoczi 1467f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 1468f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 1469f4658285SStefan Hajnoczi { 1470f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 1471d83947acSStefan Hajnoczi int64_t cluster_sector_num; 1472d83947acSStefan Hajnoczi int cluster_nb_sectors; 1473f4658285SStefan Hajnoczi bool retry; 1474f4658285SStefan Hajnoczi 1475d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 1476d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 1477d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 1478d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 1479d83947acSStefan Hajnoczi * interleave between them. 1480d83947acSStefan Hajnoczi */ 1481d83947acSStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1482d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1483d83947acSStefan Hajnoczi 1484f4658285SStefan Hajnoczi do { 1485f4658285SStefan Hajnoczi retry = false; 1486f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 1487d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 1488d83947acSStefan Hajnoczi cluster_nb_sectors)) { 14895f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 14905f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 14915f8b6491SStefan Hajnoczi * never happen since it means deadlock. 14925f8b6491SStefan Hajnoczi */ 14935f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 14945f8b6491SStefan Hajnoczi 1495f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 1496f4658285SStefan Hajnoczi retry = true; 1497f4658285SStefan Hajnoczi break; 1498f4658285SStefan Hajnoczi } 1499f4658285SStefan Hajnoczi } 1500f4658285SStefan Hajnoczi } while (retry); 1501f4658285SStefan Hajnoczi } 1502f4658285SStefan Hajnoczi 1503756e6736SKevin Wolf /* 1504756e6736SKevin Wolf * Return values: 1505756e6736SKevin Wolf * 0 - success 1506756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 1507756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 1508756e6736SKevin Wolf * image file header 1509756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 1510756e6736SKevin Wolf */ 1511756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 1512756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1513756e6736SKevin Wolf { 1514756e6736SKevin Wolf BlockDriver *drv = bs->drv; 1515469ef350SPaolo Bonzini int ret; 1516756e6736SKevin Wolf 15175f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 15185f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 15195f377794SPaolo Bonzini return -EINVAL; 15205f377794SPaolo Bonzini } 15215f377794SPaolo Bonzini 1522756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 1523469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 1524756e6736SKevin Wolf } else { 1525469ef350SPaolo Bonzini ret = -ENOTSUP; 1526756e6736SKevin Wolf } 1527469ef350SPaolo Bonzini 1528469ef350SPaolo Bonzini if (ret == 0) { 1529469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 1530469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 1531469ef350SPaolo Bonzini } 1532469ef350SPaolo Bonzini return ret; 1533756e6736SKevin Wolf } 1534756e6736SKevin Wolf 153571d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 153671d0770cSaliguori size_t size) 153771d0770cSaliguori { 153871d0770cSaliguori int64_t len; 153971d0770cSaliguori 154071d0770cSaliguori if (!bdrv_is_inserted(bs)) 154171d0770cSaliguori return -ENOMEDIUM; 154271d0770cSaliguori 154371d0770cSaliguori if (bs->growable) 154471d0770cSaliguori return 0; 154571d0770cSaliguori 154671d0770cSaliguori len = bdrv_getlength(bs); 154771d0770cSaliguori 1548fbb7b4e0SKevin Wolf if (offset < 0) 1549fbb7b4e0SKevin Wolf return -EIO; 1550fbb7b4e0SKevin Wolf 1551fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 155271d0770cSaliguori return -EIO; 155371d0770cSaliguori 155471d0770cSaliguori return 0; 155571d0770cSaliguori } 155671d0770cSaliguori 155771d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 155871d0770cSaliguori int nb_sectors) 155971d0770cSaliguori { 1560eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1561eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 156271d0770cSaliguori } 156371d0770cSaliguori 15641c9805a3SStefan Hajnoczi typedef struct RwCo { 15651c9805a3SStefan Hajnoczi BlockDriverState *bs; 15661c9805a3SStefan Hajnoczi int64_t sector_num; 15671c9805a3SStefan Hajnoczi int nb_sectors; 15681c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 15691c9805a3SStefan Hajnoczi bool is_write; 15701c9805a3SStefan Hajnoczi int ret; 15711c9805a3SStefan Hajnoczi } RwCo; 15721c9805a3SStefan Hajnoczi 15731c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 1574fc01f7e7Sbellard { 15751c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 1576fc01f7e7Sbellard 15771c9805a3SStefan Hajnoczi if (!rwco->is_write) { 15781c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 1579470c0504SStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 15801c9805a3SStefan Hajnoczi } else { 15811c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 1582f08f2ddaSStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 15831c9805a3SStefan Hajnoczi } 15841c9805a3SStefan Hajnoczi } 1585e7a8a783SKevin Wolf 15861c9805a3SStefan Hajnoczi /* 15871c9805a3SStefan Hajnoczi * Process a synchronous request using coroutines 15881c9805a3SStefan Hajnoczi */ 15891c9805a3SStefan Hajnoczi static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 15901c9805a3SStefan Hajnoczi int nb_sectors, bool is_write) 15911c9805a3SStefan Hajnoczi { 1592e7a8a783SKevin Wolf QEMUIOVector qiov; 1593e7a8a783SKevin Wolf struct iovec iov = { 1594e7a8a783SKevin Wolf .iov_base = (void *)buf, 1595e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 1596e7a8a783SKevin Wolf }; 15971c9805a3SStefan Hajnoczi Coroutine *co; 15981c9805a3SStefan Hajnoczi RwCo rwco = { 15991c9805a3SStefan Hajnoczi .bs = bs, 16001c9805a3SStefan Hajnoczi .sector_num = sector_num, 16011c9805a3SStefan Hajnoczi .nb_sectors = nb_sectors, 16021c9805a3SStefan Hajnoczi .qiov = &qiov, 16031c9805a3SStefan Hajnoczi .is_write = is_write, 16041c9805a3SStefan Hajnoczi .ret = NOT_DONE, 16051c9805a3SStefan Hajnoczi }; 1606e7a8a783SKevin Wolf 1607e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 16081c9805a3SStefan Hajnoczi 1609498e386cSZhi Yong Wu /** 1610498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 1611498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 1612498e386cSZhi Yong Wu * if it has been enabled. 1613498e386cSZhi Yong Wu */ 1614498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 1615498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 1616498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 1617498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 1618498e386cSZhi Yong Wu } 1619498e386cSZhi Yong Wu 16201c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 16211c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 16221c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 16231c9805a3SStefan Hajnoczi } else { 16241c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 16251c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 16261c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 16271c9805a3SStefan Hajnoczi qemu_aio_wait(); 16281c9805a3SStefan Hajnoczi } 16291c9805a3SStefan Hajnoczi } 16301c9805a3SStefan Hajnoczi return rwco.ret; 1631e7a8a783SKevin Wolf } 1632e7a8a783SKevin Wolf 16331c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 16341c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 16351c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 16361c9805a3SStefan Hajnoczi { 16371c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); 163883f64091Sbellard } 1639fc01f7e7Sbellard 164007d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 164107d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 164207d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 164307d27a44SMarkus Armbruster { 164407d27a44SMarkus Armbruster bool enabled; 164507d27a44SMarkus Armbruster int ret; 164607d27a44SMarkus Armbruster 164707d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 164807d27a44SMarkus Armbruster bs->io_limits_enabled = false; 164907d27a44SMarkus Armbruster ret = bdrv_read(bs, 0, buf, 1); 165007d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 165107d27a44SMarkus Armbruster return ret; 165207d27a44SMarkus Armbruster } 165307d27a44SMarkus Armbruster 165471df14fcSPaolo Bonzini #define BITS_PER_LONG (sizeof(unsigned long) * 8) 165571df14fcSPaolo Bonzini 16567cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 16577cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 16587cd1e32aSlirans@il.ibm.com { 16597cd1e32aSlirans@il.ibm.com int64_t start, end; 1660c6d22830SJan Kiszka unsigned long val, idx, bit; 1661a55eb92cSJan Kiszka 16626ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 1663c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 16647cd1e32aSlirans@il.ibm.com 16657cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 166671df14fcSPaolo Bonzini idx = start / BITS_PER_LONG; 166771df14fcSPaolo Bonzini bit = start % BITS_PER_LONG; 1668c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 1669c6d22830SJan Kiszka if (dirty) { 16706d59fec1SMarcelo Tosatti if (!(val & (1UL << bit))) { 1671aaa0eb75SLiran Schour bs->dirty_count++; 16726d59fec1SMarcelo Tosatti val |= 1UL << bit; 1673aaa0eb75SLiran Schour } 1674c6d22830SJan Kiszka } else { 16756d59fec1SMarcelo Tosatti if (val & (1UL << bit)) { 1676aaa0eb75SLiran Schour bs->dirty_count--; 16776d59fec1SMarcelo Tosatti val &= ~(1UL << bit); 1678c6d22830SJan Kiszka } 1679aaa0eb75SLiran Schour } 1680c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 16817cd1e32aSlirans@il.ibm.com } 16827cd1e32aSlirans@il.ibm.com } 16837cd1e32aSlirans@il.ibm.com 168419cb3738Sbellard /* Return < 0 if error. Important errors are: 168519cb3738Sbellard -EIO generic I/O error (may happen for all errors) 168619cb3738Sbellard -ENOMEDIUM No media inserted. 168719cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 168819cb3738Sbellard -EACCES Trying to write a read-only device 168919cb3738Sbellard */ 1690fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 1691fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 1692fc01f7e7Sbellard { 16931c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); 169483f64091Sbellard } 169583f64091Sbellard 1696eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 1697eda578e5Saliguori void *buf, int count1) 169883f64091Sbellard { 16996ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 170083f64091Sbellard int len, nb_sectors, count; 170183f64091Sbellard int64_t sector_num; 17029a8c4cceSKevin Wolf int ret; 170383f64091Sbellard 170483f64091Sbellard count = count1; 170583f64091Sbellard /* first read to align to sector start */ 17066ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 170783f64091Sbellard if (len > count) 170883f64091Sbellard len = count; 17096ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 171083f64091Sbellard if (len > 0) { 17119a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 17129a8c4cceSKevin Wolf return ret; 17136ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 171483f64091Sbellard count -= len; 171583f64091Sbellard if (count == 0) 171683f64091Sbellard return count1; 171783f64091Sbellard sector_num++; 171883f64091Sbellard buf += len; 171983f64091Sbellard } 172083f64091Sbellard 172183f64091Sbellard /* read the sectors "in place" */ 17226ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 172383f64091Sbellard if (nb_sectors > 0) { 17249a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 17259a8c4cceSKevin Wolf return ret; 172683f64091Sbellard sector_num += nb_sectors; 17276ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 172883f64091Sbellard buf += len; 172983f64091Sbellard count -= len; 173083f64091Sbellard } 173183f64091Sbellard 173283f64091Sbellard /* add data from the last sector */ 173383f64091Sbellard if (count > 0) { 17349a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 17359a8c4cceSKevin Wolf return ret; 173683f64091Sbellard memcpy(buf, tmp_buf, count); 173783f64091Sbellard } 173883f64091Sbellard return count1; 173983f64091Sbellard } 174083f64091Sbellard 1741eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 1742eda578e5Saliguori const void *buf, int count1) 174383f64091Sbellard { 17446ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 174583f64091Sbellard int len, nb_sectors, count; 174683f64091Sbellard int64_t sector_num; 17479a8c4cceSKevin Wolf int ret; 174883f64091Sbellard 174983f64091Sbellard count = count1; 175083f64091Sbellard /* first write to align to sector start */ 17516ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 175283f64091Sbellard if (len > count) 175383f64091Sbellard len = count; 17546ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 175583f64091Sbellard if (len > 0) { 17569a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 17579a8c4cceSKevin Wolf return ret; 17586ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 17599a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 17609a8c4cceSKevin Wolf return ret; 176183f64091Sbellard count -= len; 176283f64091Sbellard if (count == 0) 176383f64091Sbellard return count1; 176483f64091Sbellard sector_num++; 176583f64091Sbellard buf += len; 176683f64091Sbellard } 176783f64091Sbellard 176883f64091Sbellard /* write the sectors "in place" */ 17696ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 177083f64091Sbellard if (nb_sectors > 0) { 17719a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 17729a8c4cceSKevin Wolf return ret; 177383f64091Sbellard sector_num += nb_sectors; 17746ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 177583f64091Sbellard buf += len; 177683f64091Sbellard count -= len; 177783f64091Sbellard } 177883f64091Sbellard 177983f64091Sbellard /* add data from the last sector */ 178083f64091Sbellard if (count > 0) { 17819a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 17829a8c4cceSKevin Wolf return ret; 178383f64091Sbellard memcpy(tmp_buf, buf, count); 17849a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 17859a8c4cceSKevin Wolf return ret; 178683f64091Sbellard } 178783f64091Sbellard return count1; 178883f64091Sbellard } 178983f64091Sbellard 1790f08145feSKevin Wolf /* 1791f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1792f08145feSKevin Wolf * request (acts as a barrier) 1793f08145feSKevin Wolf * 1794f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1795f08145feSKevin Wolf */ 1796f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1797f08145feSKevin Wolf const void *buf, int count) 1798f08145feSKevin Wolf { 1799f08145feSKevin Wolf int ret; 1800f08145feSKevin Wolf 1801f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1802f08145feSKevin Wolf if (ret < 0) { 1803f08145feSKevin Wolf return ret; 1804f08145feSKevin Wolf } 1805f08145feSKevin Wolf 1806f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 1807f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 1808f08145feSKevin Wolf bdrv_flush(bs); 1809f08145feSKevin Wolf } 1810f08145feSKevin Wolf 1811f08145feSKevin Wolf return 0; 1812f08145feSKevin Wolf } 1813f08145feSKevin Wolf 1814470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 1815ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1816ab185921SStefan Hajnoczi { 1817ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 1818ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 1819ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 1820ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 1821ab185921SStefan Hajnoczi */ 1822ab185921SStefan Hajnoczi void *bounce_buffer; 1823ab185921SStefan Hajnoczi 182479c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 1825ab185921SStefan Hajnoczi struct iovec iov; 1826ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 1827ab185921SStefan Hajnoczi int64_t cluster_sector_num; 1828ab185921SStefan Hajnoczi int cluster_nb_sectors; 1829ab185921SStefan Hajnoczi size_t skip_bytes; 1830ab185921SStefan Hajnoczi int ret; 1831ab185921SStefan Hajnoczi 1832ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 1833ab185921SStefan Hajnoczi * allocating cluster in the image file. 1834ab185921SStefan Hajnoczi */ 1835ab185921SStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1836ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1837ab185921SStefan Hajnoczi 1838470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 1839ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 1840ab185921SStefan Hajnoczi 1841ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 1842ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 1843ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 1844ab185921SStefan Hajnoczi 184579c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 1846ab185921SStefan Hajnoczi &bounce_qiov); 1847ab185921SStefan Hajnoczi if (ret < 0) { 1848ab185921SStefan Hajnoczi goto err; 1849ab185921SStefan Hajnoczi } 1850ab185921SStefan Hajnoczi 185179c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 185279c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 1853621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 185479c053bdSStefan Hajnoczi cluster_nb_sectors); 185579c053bdSStefan Hajnoczi } else { 1856f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 1857f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 1858f05fa4adSPaolo Bonzini */ 185979c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 1860ab185921SStefan Hajnoczi &bounce_qiov); 186179c053bdSStefan Hajnoczi } 186279c053bdSStefan Hajnoczi 1863ab185921SStefan Hajnoczi if (ret < 0) { 1864ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 1865ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 1866ab185921SStefan Hajnoczi * Simply report it in all cases. 1867ab185921SStefan Hajnoczi */ 1868ab185921SStefan Hajnoczi goto err; 1869ab185921SStefan Hajnoczi } 1870ab185921SStefan Hajnoczi 1871ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 187203396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 1873ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 1874ab185921SStefan Hajnoczi 1875ab185921SStefan Hajnoczi err: 1876ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 1877ab185921SStefan Hajnoczi return ret; 1878ab185921SStefan Hajnoczi } 1879ab185921SStefan Hajnoczi 1880c5fbe571SStefan Hajnoczi /* 1881c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 1882c5fbe571SStefan Hajnoczi */ 1883c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 1884470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1885470c0504SStefan Hajnoczi BdrvRequestFlags flags) 1886da1fa91dSKevin Wolf { 1887da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 1888dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 1889dbffbdcfSStefan Hajnoczi int ret; 1890da1fa91dSKevin Wolf 1891da1fa91dSKevin Wolf if (!drv) { 1892da1fa91dSKevin Wolf return -ENOMEDIUM; 1893da1fa91dSKevin Wolf } 1894da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1895da1fa91dSKevin Wolf return -EIO; 1896da1fa91dSKevin Wolf } 1897da1fa91dSKevin Wolf 189898f90dbaSZhi Yong Wu /* throttling disk read I/O */ 189998f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 190098f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, false, nb_sectors); 190198f90dbaSZhi Yong Wu } 190298f90dbaSZhi Yong Wu 1903f4658285SStefan Hajnoczi if (bs->copy_on_read) { 1904470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 1905470c0504SStefan Hajnoczi } 1906470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1907470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 1908470c0504SStefan Hajnoczi } 1909470c0504SStefan Hajnoczi 1910470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1911f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1912f4658285SStefan Hajnoczi } 1913f4658285SStefan Hajnoczi 1914dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 1915ab185921SStefan Hajnoczi 1916470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1917ab185921SStefan Hajnoczi int pnum; 1918ab185921SStefan Hajnoczi 1919ab185921SStefan Hajnoczi ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); 1920ab185921SStefan Hajnoczi if (ret < 0) { 1921ab185921SStefan Hajnoczi goto out; 1922ab185921SStefan Hajnoczi } 1923ab185921SStefan Hajnoczi 1924ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 1925470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 1926ab185921SStefan Hajnoczi goto out; 1927ab185921SStefan Hajnoczi } 1928ab185921SStefan Hajnoczi } 1929ab185921SStefan Hajnoczi 1930dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 1931ab185921SStefan Hajnoczi 1932ab185921SStefan Hajnoczi out: 1933dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1934470c0504SStefan Hajnoczi 1935470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1936470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 1937470c0504SStefan Hajnoczi } 1938470c0504SStefan Hajnoczi 1939dbffbdcfSStefan Hajnoczi return ret; 1940da1fa91dSKevin Wolf } 1941da1fa91dSKevin Wolf 1942c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 1943da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 1944da1fa91dSKevin Wolf { 1945c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 1946da1fa91dSKevin Wolf 1947470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 1948470c0504SStefan Hajnoczi } 1949470c0504SStefan Hajnoczi 1950470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 1951470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1952470c0504SStefan Hajnoczi { 1953470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 1954470c0504SStefan Hajnoczi 1955470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 1956470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 1957c5fbe571SStefan Hajnoczi } 1958c5fbe571SStefan Hajnoczi 1959f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 1960f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1961f08f2ddaSStefan Hajnoczi { 1962f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 1963f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 1964f08f2ddaSStefan Hajnoczi struct iovec iov; 1965f08f2ddaSStefan Hajnoczi int ret; 1966f08f2ddaSStefan Hajnoczi 1967621f0589SKevin Wolf /* TODO Emulate only part of misaligned requests instead of letting block 1968621f0589SKevin Wolf * drivers return -ENOTSUP and emulate everything */ 1969621f0589SKevin Wolf 1970f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 1971f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 1972621f0589SKevin Wolf ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1973621f0589SKevin Wolf if (ret != -ENOTSUP) { 1974621f0589SKevin Wolf return ret; 1975621f0589SKevin Wolf } 1976f08f2ddaSStefan Hajnoczi } 1977f08f2ddaSStefan Hajnoczi 1978f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 1979f08f2ddaSStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 1980f08f2ddaSStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 1981f08f2ddaSStefan Hajnoczi memset(iov.iov_base, 0, iov.iov_len); 1982f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 1983f08f2ddaSStefan Hajnoczi 1984f08f2ddaSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 1985f08f2ddaSStefan Hajnoczi 1986f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 1987f08f2ddaSStefan Hajnoczi return ret; 1988f08f2ddaSStefan Hajnoczi } 1989f08f2ddaSStefan Hajnoczi 1990c5fbe571SStefan Hajnoczi /* 1991c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 1992c5fbe571SStefan Hajnoczi */ 1993c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 1994f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1995f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 1996c5fbe571SStefan Hajnoczi { 1997c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 1998dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 19996b7cb247SStefan Hajnoczi int ret; 2000da1fa91dSKevin Wolf 2001da1fa91dSKevin Wolf if (!bs->drv) { 2002da1fa91dSKevin Wolf return -ENOMEDIUM; 2003da1fa91dSKevin Wolf } 2004da1fa91dSKevin Wolf if (bs->read_only) { 2005da1fa91dSKevin Wolf return -EACCES; 2006da1fa91dSKevin Wolf } 2007da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2008da1fa91dSKevin Wolf return -EIO; 2009da1fa91dSKevin Wolf } 2010da1fa91dSKevin Wolf 201198f90dbaSZhi Yong Wu /* throttling disk write I/O */ 201298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 201398f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, true, nb_sectors); 201498f90dbaSZhi Yong Wu } 201598f90dbaSZhi Yong Wu 2016470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2017f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2018f4658285SStefan Hajnoczi } 2019f4658285SStefan Hajnoczi 2020dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 2021dbffbdcfSStefan Hajnoczi 2022f08f2ddaSStefan Hajnoczi if (flags & BDRV_REQ_ZERO_WRITE) { 2023f08f2ddaSStefan Hajnoczi ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); 2024f08f2ddaSStefan Hajnoczi } else { 20256b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 2026f08f2ddaSStefan Hajnoczi } 20276b7cb247SStefan Hajnoczi 2028f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 2029f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 2030f05fa4adSPaolo Bonzini } 2031f05fa4adSPaolo Bonzini 2032da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 2033da1fa91dSKevin Wolf set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 2034da1fa91dSKevin Wolf } 2035da1fa91dSKevin Wolf 2036da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2037da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2038da1fa91dSKevin Wolf } 2039da1fa91dSKevin Wolf 2040dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2041dbffbdcfSStefan Hajnoczi 20426b7cb247SStefan Hajnoczi return ret; 2043da1fa91dSKevin Wolf } 2044da1fa91dSKevin Wolf 2045c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 2046c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 2047c5fbe571SStefan Hajnoczi { 2048c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 2049c5fbe571SStefan Hajnoczi 2050f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 2051f08f2ddaSStefan Hajnoczi } 2052f08f2ddaSStefan Hajnoczi 2053f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 2054f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 2055f08f2ddaSStefan Hajnoczi { 2056f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 2057f08f2ddaSStefan Hajnoczi 2058f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 2059f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE); 2060c5fbe571SStefan Hajnoczi } 2061c5fbe571SStefan Hajnoczi 206283f64091Sbellard /** 206383f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 206483f64091Sbellard */ 206583f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 206683f64091Sbellard { 206783f64091Sbellard BlockDriver *drv = bs->drv; 206851762288SStefan Hajnoczi int ret; 206983f64091Sbellard if (!drv) 207019cb3738Sbellard return -ENOMEDIUM; 207183f64091Sbellard if (!drv->bdrv_truncate) 207283f64091Sbellard return -ENOTSUP; 207359f2689dSNaphtali Sprei if (bs->read_only) 207459f2689dSNaphtali Sprei return -EACCES; 20758591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 20768591675fSMarcelo Tosatti return -EBUSY; 207751762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 207851762288SStefan Hajnoczi if (ret == 0) { 207951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2080145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 208151762288SStefan Hajnoczi } 208251762288SStefan Hajnoczi return ret; 208383f64091Sbellard } 208483f64091Sbellard 208583f64091Sbellard /** 20864a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 20874a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 20884a1d5e1fSFam Zheng */ 20894a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 20904a1d5e1fSFam Zheng { 20914a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 20924a1d5e1fSFam Zheng if (!drv) { 20934a1d5e1fSFam Zheng return -ENOMEDIUM; 20944a1d5e1fSFam Zheng } 20954a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 20964a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 20974a1d5e1fSFam Zheng } 20984a1d5e1fSFam Zheng if (bs->file) { 20994a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 21004a1d5e1fSFam Zheng } 21014a1d5e1fSFam Zheng return -ENOTSUP; 21024a1d5e1fSFam Zheng } 21034a1d5e1fSFam Zheng 21044a1d5e1fSFam Zheng /** 210583f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 210683f64091Sbellard */ 210783f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 210883f64091Sbellard { 210983f64091Sbellard BlockDriver *drv = bs->drv; 211083f64091Sbellard if (!drv) 211119cb3738Sbellard return -ENOMEDIUM; 211251762288SStefan Hajnoczi 21132c6942faSMarkus Armbruster if (bs->growable || bdrv_dev_has_removable_media(bs)) { 211446a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 211583f64091Sbellard return drv->bdrv_getlength(bs); 2116fc01f7e7Sbellard } 211746a4e4e6SStefan Hajnoczi } 211846a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 211946a4e4e6SStefan Hajnoczi } 2120fc01f7e7Sbellard 212119cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 212296b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2123fc01f7e7Sbellard { 212419cb3738Sbellard int64_t length; 212519cb3738Sbellard length = bdrv_getlength(bs); 212619cb3738Sbellard if (length < 0) 212719cb3738Sbellard length = 0; 212819cb3738Sbellard else 21296ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 213019cb3738Sbellard *nb_sectors_ptr = length; 2131fc01f7e7Sbellard } 2132cf98951bSbellard 21330563e191SZhi Yong Wu /* throttling disk io limits */ 21340563e191SZhi Yong Wu void bdrv_set_io_limits(BlockDriverState *bs, 21350563e191SZhi Yong Wu BlockIOLimit *io_limits) 21360563e191SZhi Yong Wu { 21370563e191SZhi Yong Wu bs->io_limits = *io_limits; 21380563e191SZhi Yong Wu bs->io_limits_enabled = bdrv_io_limits_enabled(bs); 21390563e191SZhi Yong Wu } 21400563e191SZhi Yong Wu 2141abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 2142abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 2143abd7f68dSMarkus Armbruster { 2144abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2145abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2146abd7f68dSMarkus Armbruster } 2147abd7f68dSMarkus Armbruster 2148abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 2149abd7f68dSMarkus Armbruster { 2150abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2151abd7f68dSMarkus Armbruster } 2152abd7f68dSMarkus Armbruster 2153b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2154b338082bSbellard { 2155b338082bSbellard return bs->read_only; 2156b338082bSbellard } 2157b338082bSbellard 2158985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2159985a03b0Sths { 2160985a03b0Sths return bs->sg; 2161985a03b0Sths } 2162985a03b0Sths 2163e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2164e900a7b7SChristoph Hellwig { 2165e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2166e900a7b7SChristoph Hellwig } 2167e900a7b7SChristoph Hellwig 2168425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 2169425b0148SPaolo Bonzini { 2170425b0148SPaolo Bonzini bs->enable_write_cache = wce; 2171*55b110f2SJeff Cody 2172*55b110f2SJeff Cody /* so a reopen() will preserve wce */ 2173*55b110f2SJeff Cody if (wce) { 2174*55b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 2175*55b110f2SJeff Cody } else { 2176*55b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 2177*55b110f2SJeff Cody } 2178425b0148SPaolo Bonzini } 2179425b0148SPaolo Bonzini 2180ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2181ea2384d3Sbellard { 2182ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2183ea2384d3Sbellard return 1; 2184ea2384d3Sbellard return bs->encrypted; 2185ea2384d3Sbellard } 2186ea2384d3Sbellard 2187c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2188c0f4ce77Saliguori { 2189c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2190c0f4ce77Saliguori 2191c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2192c0f4ce77Saliguori return 1; 2193c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2194c0f4ce77Saliguori } 2195c0f4ce77Saliguori 2196ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2197ea2384d3Sbellard { 2198ea2384d3Sbellard int ret; 2199ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2200ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2201ea2384d3Sbellard if (ret < 0) 2202ea2384d3Sbellard return ret; 2203ea2384d3Sbellard if (!bs->encrypted) 2204ea2384d3Sbellard return 0; 2205ea2384d3Sbellard } 2206fd04a2aeSShahar Havivi if (!bs->encrypted) { 2207fd04a2aeSShahar Havivi return -EINVAL; 2208fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2209fd04a2aeSShahar Havivi return -ENOMEDIUM; 2210fd04a2aeSShahar Havivi } 2211c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2212bb5fc20fSaliguori if (ret < 0) { 2213bb5fc20fSaliguori bs->valid_key = 0; 2214bb5fc20fSaliguori } else if (!bs->valid_key) { 2215bb5fc20fSaliguori bs->valid_key = 1; 2216bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 22177d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 2218bb5fc20fSaliguori } 2219c0f4ce77Saliguori return ret; 2220ea2384d3Sbellard } 2221ea2384d3Sbellard 2222f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 2223ea2384d3Sbellard { 2224f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 2225ea2384d3Sbellard } 2226ea2384d3Sbellard 2227ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2228ea2384d3Sbellard void *opaque) 2229ea2384d3Sbellard { 2230ea2384d3Sbellard BlockDriver *drv; 2231ea2384d3Sbellard 22328a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2233ea2384d3Sbellard it(opaque, drv->format_name); 2234ea2384d3Sbellard } 2235ea2384d3Sbellard } 2236ea2384d3Sbellard 2237b338082bSbellard BlockDriverState *bdrv_find(const char *name) 2238b338082bSbellard { 2239b338082bSbellard BlockDriverState *bs; 2240b338082bSbellard 22411b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 22421b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 2243b338082bSbellard return bs; 2244b338082bSbellard } 22451b7bdbc1SStefan Hajnoczi } 2246b338082bSbellard return NULL; 2247b338082bSbellard } 2248b338082bSbellard 22492f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 22502f399b0aSMarkus Armbruster { 22512f399b0aSMarkus Armbruster if (!bs) { 22522f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 22532f399b0aSMarkus Armbruster } 22542f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 22552f399b0aSMarkus Armbruster } 22562f399b0aSMarkus Armbruster 225751de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 225881d0912dSbellard { 225981d0912dSbellard BlockDriverState *bs; 226081d0912dSbellard 22611b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 226251de9760Saliguori it(opaque, bs); 226381d0912dSbellard } 226481d0912dSbellard } 226581d0912dSbellard 2266ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 2267ea2384d3Sbellard { 2268ea2384d3Sbellard return bs->device_name; 2269ea2384d3Sbellard } 2270ea2384d3Sbellard 2271c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 2272c8433287SMarkus Armbruster { 2273c8433287SMarkus Armbruster return bs->open_flags; 2274c8433287SMarkus Armbruster } 2275c8433287SMarkus Armbruster 2276c6ca28d6Saliguori void bdrv_flush_all(void) 2277c6ca28d6Saliguori { 2278c6ca28d6Saliguori BlockDriverState *bs; 2279c6ca28d6Saliguori 22801b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2281c6ca28d6Saliguori bdrv_flush(bs); 2282c6ca28d6Saliguori } 22831b7bdbc1SStefan Hajnoczi } 2284c6ca28d6Saliguori 2285f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2286f2feebbdSKevin Wolf { 2287f2feebbdSKevin Wolf assert(bs->drv); 2288f2feebbdSKevin Wolf 2289336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2290336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2291f2feebbdSKevin Wolf } 2292f2feebbdSKevin Wolf 2293f2feebbdSKevin Wolf return 1; 2294f2feebbdSKevin Wolf } 2295f2feebbdSKevin Wolf 2296376ae3f1SStefan Hajnoczi typedef struct BdrvCoIsAllocatedData { 2297376ae3f1SStefan Hajnoczi BlockDriverState *bs; 2298376ae3f1SStefan Hajnoczi int64_t sector_num; 2299376ae3f1SStefan Hajnoczi int nb_sectors; 2300376ae3f1SStefan Hajnoczi int *pnum; 2301376ae3f1SStefan Hajnoczi int ret; 2302376ae3f1SStefan Hajnoczi bool done; 2303376ae3f1SStefan Hajnoczi } BdrvCoIsAllocatedData; 2304376ae3f1SStefan Hajnoczi 2305f58c7b35Sths /* 2306f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 2307f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 2308f58c7b35Sths * hence all their sectors are reported as allocated. 2309f58c7b35Sths * 2310bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 2311bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 2312bd9533e3SStefan Hajnoczi * 2313f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 2314f58c7b35Sths * the specified sector) that are known to be in the same 2315f58c7b35Sths * allocated/unallocated state. 2316f58c7b35Sths * 2317bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2318bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 2319f58c7b35Sths */ 2320060f51c9SStefan Hajnoczi int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, 2321060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 2322f58c7b35Sths { 2323f58c7b35Sths int64_t n; 2324bd9533e3SStefan Hajnoczi 23256aebab14SStefan Hajnoczi if (sector_num >= bs->total_sectors) { 23266aebab14SStefan Hajnoczi *pnum = 0; 23276aebab14SStefan Hajnoczi return 0; 23286aebab14SStefan Hajnoczi } 2329bd9533e3SStefan Hajnoczi 23306aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 2331bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 2332bd9533e3SStefan Hajnoczi nb_sectors = n; 2333bd9533e3SStefan Hajnoczi } 2334bd9533e3SStefan Hajnoczi 2335bd9533e3SStefan Hajnoczi if (!bs->drv->bdrv_co_is_allocated) { 2336bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 23376aebab14SStefan Hajnoczi return 1; 23386aebab14SStefan Hajnoczi } 23396aebab14SStefan Hajnoczi 2340060f51c9SStefan Hajnoczi return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum); 2341060f51c9SStefan Hajnoczi } 2342060f51c9SStefan Hajnoczi 2343060f51c9SStefan Hajnoczi /* Coroutine wrapper for bdrv_is_allocated() */ 2344060f51c9SStefan Hajnoczi static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) 2345060f51c9SStefan Hajnoczi { 2346060f51c9SStefan Hajnoczi BdrvCoIsAllocatedData *data = opaque; 2347060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 2348060f51c9SStefan Hajnoczi 2349060f51c9SStefan Hajnoczi data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, 2350060f51c9SStefan Hajnoczi data->pnum); 2351060f51c9SStefan Hajnoczi data->done = true; 2352060f51c9SStefan Hajnoczi } 2353060f51c9SStefan Hajnoczi 2354060f51c9SStefan Hajnoczi /* 2355060f51c9SStefan Hajnoczi * Synchronous wrapper around bdrv_co_is_allocated(). 2356060f51c9SStefan Hajnoczi * 2357060f51c9SStefan Hajnoczi * See bdrv_co_is_allocated() for details. 2358060f51c9SStefan Hajnoczi */ 2359060f51c9SStefan Hajnoczi int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 2360060f51c9SStefan Hajnoczi int *pnum) 2361060f51c9SStefan Hajnoczi { 2362376ae3f1SStefan Hajnoczi Coroutine *co; 2363376ae3f1SStefan Hajnoczi BdrvCoIsAllocatedData data = { 2364376ae3f1SStefan Hajnoczi .bs = bs, 2365376ae3f1SStefan Hajnoczi .sector_num = sector_num, 2366376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 2367376ae3f1SStefan Hajnoczi .pnum = pnum, 2368376ae3f1SStefan Hajnoczi .done = false, 2369376ae3f1SStefan Hajnoczi }; 2370376ae3f1SStefan Hajnoczi 2371376ae3f1SStefan Hajnoczi co = qemu_coroutine_create(bdrv_is_allocated_co_entry); 2372376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 2373376ae3f1SStefan Hajnoczi while (!data.done) { 2374376ae3f1SStefan Hajnoczi qemu_aio_wait(); 2375376ae3f1SStefan Hajnoczi } 2376376ae3f1SStefan Hajnoczi return data.ret; 2377376ae3f1SStefan Hajnoczi } 2378f58c7b35Sths 2379188a7bbfSPaolo Bonzini /* 2380188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 2381188a7bbfSPaolo Bonzini * 2382188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 2383188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 2384188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 2385188a7bbfSPaolo Bonzini * 2386188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 2387188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 2388188a7bbfSPaolo Bonzini * allocated/unallocated state. 2389188a7bbfSPaolo Bonzini * 2390188a7bbfSPaolo Bonzini */ 2391188a7bbfSPaolo Bonzini int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top, 2392188a7bbfSPaolo Bonzini BlockDriverState *base, 2393188a7bbfSPaolo Bonzini int64_t sector_num, 2394188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 2395188a7bbfSPaolo Bonzini { 2396188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 2397188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 2398188a7bbfSPaolo Bonzini 2399188a7bbfSPaolo Bonzini intermediate = top; 2400188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 2401188a7bbfSPaolo Bonzini int pnum_inter; 2402188a7bbfSPaolo Bonzini ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors, 2403188a7bbfSPaolo Bonzini &pnum_inter); 2404188a7bbfSPaolo Bonzini if (ret < 0) { 2405188a7bbfSPaolo Bonzini return ret; 2406188a7bbfSPaolo Bonzini } else if (ret) { 2407188a7bbfSPaolo Bonzini *pnum = pnum_inter; 2408188a7bbfSPaolo Bonzini return 1; 2409188a7bbfSPaolo Bonzini } 2410188a7bbfSPaolo Bonzini 2411188a7bbfSPaolo Bonzini /* 2412188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 2413188a7bbfSPaolo Bonzini * might have 2414188a7bbfSPaolo Bonzini * 2415188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 2416188a7bbfSPaolo Bonzini */ 2417188a7bbfSPaolo Bonzini if (n > pnum_inter) { 2418188a7bbfSPaolo Bonzini n = pnum_inter; 2419188a7bbfSPaolo Bonzini } 2420188a7bbfSPaolo Bonzini 2421188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 2422188a7bbfSPaolo Bonzini } 2423188a7bbfSPaolo Bonzini 2424188a7bbfSPaolo Bonzini *pnum = n; 2425188a7bbfSPaolo Bonzini return 0; 2426188a7bbfSPaolo Bonzini } 2427188a7bbfSPaolo Bonzini 2428b2023818SLuiz Capitulino BlockInfoList *qmp_query_block(Error **errp) 2429b338082bSbellard { 2430b2023818SLuiz Capitulino BlockInfoList *head = NULL, *cur_item = NULL; 2431d15e5465SLuiz Capitulino BlockDriverState *bs; 2432d15e5465SLuiz Capitulino 24331b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2434b2023818SLuiz Capitulino BlockInfoList *info = g_malloc0(sizeof(*info)); 2435d15e5465SLuiz Capitulino 2436b2023818SLuiz Capitulino info->value = g_malloc0(sizeof(*info->value)); 2437b2023818SLuiz Capitulino info->value->device = g_strdup(bs->device_name); 2438b2023818SLuiz Capitulino info->value->type = g_strdup("unknown"); 2439b2023818SLuiz Capitulino info->value->locked = bdrv_dev_is_medium_locked(bs); 2440b2023818SLuiz Capitulino info->value->removable = bdrv_dev_has_removable_media(bs); 2441d15e5465SLuiz Capitulino 2442e4def80bSMarkus Armbruster if (bdrv_dev_has_removable_media(bs)) { 2443b2023818SLuiz Capitulino info->value->has_tray_open = true; 2444b2023818SLuiz Capitulino info->value->tray_open = bdrv_dev_is_tray_open(bs); 2445e4def80bSMarkus Armbruster } 2446f04ef601SLuiz Capitulino 2447f04ef601SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 2448b2023818SLuiz Capitulino info->value->has_io_status = true; 2449b2023818SLuiz Capitulino info->value->io_status = bs->iostatus; 2450f04ef601SLuiz Capitulino } 2451f04ef601SLuiz Capitulino 2452d15e5465SLuiz Capitulino if (bs->drv) { 2453b2023818SLuiz Capitulino info->value->has_inserted = true; 2454b2023818SLuiz Capitulino info->value->inserted = g_malloc0(sizeof(*info->value->inserted)); 2455b2023818SLuiz Capitulino info->value->inserted->file = g_strdup(bs->filename); 2456b2023818SLuiz Capitulino info->value->inserted->ro = bs->read_only; 2457b2023818SLuiz Capitulino info->value->inserted->drv = g_strdup(bs->drv->format_name); 2458b2023818SLuiz Capitulino info->value->inserted->encrypted = bs->encrypted; 2459c75a1a8aSLuiz Capitulino info->value->inserted->encryption_key_missing = bdrv_key_required(bs); 2460b2023818SLuiz Capitulino if (bs->backing_file[0]) { 2461b2023818SLuiz Capitulino info->value->inserted->has_backing_file = true; 2462b2023818SLuiz Capitulino info->value->inserted->backing_file = g_strdup(bs->backing_file); 2463b2023818SLuiz Capitulino } 2464727f005eSZhi Yong Wu 24652e3e3317SBenoît Canet info->value->inserted->backing_file_depth = 24662e3e3317SBenoît Canet bdrv_get_backing_file_depth(bs); 24672e3e3317SBenoît Canet 2468727f005eSZhi Yong Wu if (bs->io_limits_enabled) { 2469727f005eSZhi Yong Wu info->value->inserted->bps = 2470727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 2471727f005eSZhi Yong Wu info->value->inserted->bps_rd = 2472727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; 2473727f005eSZhi Yong Wu info->value->inserted->bps_wr = 2474727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; 2475727f005eSZhi Yong Wu info->value->inserted->iops = 2476727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 2477727f005eSZhi Yong Wu info->value->inserted->iops_rd = 2478727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; 2479727f005eSZhi Yong Wu info->value->inserted->iops_wr = 2480727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; 2481727f005eSZhi Yong Wu } 2482d15e5465SLuiz Capitulino } 2483d15e5465SLuiz Capitulino 2484b2023818SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2485b2023818SLuiz Capitulino if (!cur_item) { 2486b2023818SLuiz Capitulino head = cur_item = info; 2487b2023818SLuiz Capitulino } else { 2488b2023818SLuiz Capitulino cur_item->next = info; 2489b2023818SLuiz Capitulino cur_item = info; 2490d15e5465SLuiz Capitulino } 2491d15e5465SLuiz Capitulino } 2492d15e5465SLuiz Capitulino 2493b2023818SLuiz Capitulino return head; 2494b338082bSbellard } 2495a36e69ddSths 2496f11f57e4SLuiz Capitulino /* Consider exposing this as a full fledged QMP command */ 2497f11f57e4SLuiz Capitulino static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp) 2498a36e69ddSths { 2499f11f57e4SLuiz Capitulino BlockStats *s; 2500218a536aSLuiz Capitulino 2501f11f57e4SLuiz Capitulino s = g_malloc0(sizeof(*s)); 2502218a536aSLuiz Capitulino 2503f11f57e4SLuiz Capitulino if (bs->device_name[0]) { 2504f11f57e4SLuiz Capitulino s->has_device = true; 2505f11f57e4SLuiz Capitulino s->device = g_strdup(bs->device_name); 2506218a536aSLuiz Capitulino } 2507218a536aSLuiz Capitulino 2508f11f57e4SLuiz Capitulino s->stats = g_malloc0(sizeof(*s->stats)); 2509f11f57e4SLuiz Capitulino s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; 2510f11f57e4SLuiz Capitulino s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; 2511f11f57e4SLuiz Capitulino s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; 2512f11f57e4SLuiz Capitulino s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; 2513f11f57e4SLuiz Capitulino s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; 2514f11f57e4SLuiz Capitulino s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; 2515f11f57e4SLuiz Capitulino s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; 2516f11f57e4SLuiz Capitulino s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; 2517f11f57e4SLuiz Capitulino s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; 2518294cc35fSKevin Wolf 2519294cc35fSKevin Wolf if (bs->file) { 2520f11f57e4SLuiz Capitulino s->has_parent = true; 2521f11f57e4SLuiz Capitulino s->parent = qmp_query_blockstat(bs->file, NULL); 2522294cc35fSKevin Wolf } 2523294cc35fSKevin Wolf 2524f11f57e4SLuiz Capitulino return s; 2525294cc35fSKevin Wolf } 2526294cc35fSKevin Wolf 2527f11f57e4SLuiz Capitulino BlockStatsList *qmp_query_blockstats(Error **errp) 2528218a536aSLuiz Capitulino { 2529f11f57e4SLuiz Capitulino BlockStatsList *head = NULL, *cur_item = NULL; 2530a36e69ddSths BlockDriverState *bs; 2531a36e69ddSths 25321b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2533f11f57e4SLuiz Capitulino BlockStatsList *info = g_malloc0(sizeof(*info)); 2534f11f57e4SLuiz Capitulino info->value = qmp_query_blockstat(bs, NULL); 2535f11f57e4SLuiz Capitulino 2536f11f57e4SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2537f11f57e4SLuiz Capitulino if (!cur_item) { 2538f11f57e4SLuiz Capitulino head = cur_item = info; 2539f11f57e4SLuiz Capitulino } else { 2540f11f57e4SLuiz Capitulino cur_item->next = info; 2541f11f57e4SLuiz Capitulino cur_item = info; 2542f11f57e4SLuiz Capitulino } 2543a36e69ddSths } 2544218a536aSLuiz Capitulino 2545f11f57e4SLuiz Capitulino return head; 2546a36e69ddSths } 2547ea2384d3Sbellard 2548045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 2549045df330Saliguori { 2550045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 2551045df330Saliguori return bs->backing_file; 2552045df330Saliguori else if (bs->encrypted) 2553045df330Saliguori return bs->filename; 2554045df330Saliguori else 2555045df330Saliguori return NULL; 2556045df330Saliguori } 2557045df330Saliguori 255883f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 255983f64091Sbellard char *filename, int filename_size) 256083f64091Sbellard { 256183f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 256283f64091Sbellard } 256383f64091Sbellard 2564faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 2565faea38e7Sbellard const uint8_t *buf, int nb_sectors) 2566faea38e7Sbellard { 2567faea38e7Sbellard BlockDriver *drv = bs->drv; 2568faea38e7Sbellard if (!drv) 256919cb3738Sbellard return -ENOMEDIUM; 2570faea38e7Sbellard if (!drv->bdrv_write_compressed) 2571faea38e7Sbellard return -ENOTSUP; 2572fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 2573fbb7b4e0SKevin Wolf return -EIO; 25747cd1e32aSlirans@il.ibm.com 2575c6d22830SJan Kiszka if (bs->dirty_bitmap) { 25767cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 25777cd1e32aSlirans@il.ibm.com } 25787cd1e32aSlirans@il.ibm.com 2579faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 2580faea38e7Sbellard } 2581faea38e7Sbellard 2582faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2583faea38e7Sbellard { 2584faea38e7Sbellard BlockDriver *drv = bs->drv; 2585faea38e7Sbellard if (!drv) 258619cb3738Sbellard return -ENOMEDIUM; 2587faea38e7Sbellard if (!drv->bdrv_get_info) 2588faea38e7Sbellard return -ENOTSUP; 2589faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 2590faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 2591faea38e7Sbellard } 2592faea38e7Sbellard 259345566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 259445566e9cSChristoph Hellwig int64_t pos, int size) 2595178e08a5Saliguori { 2596178e08a5Saliguori BlockDriver *drv = bs->drv; 2597178e08a5Saliguori if (!drv) 2598178e08a5Saliguori return -ENOMEDIUM; 25997cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 260045566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 26017cdb1f6dSMORITA Kazutaka if (bs->file) 26027cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 26037cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2604178e08a5Saliguori } 2605178e08a5Saliguori 260645566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 260745566e9cSChristoph Hellwig int64_t pos, int size) 2608178e08a5Saliguori { 2609178e08a5Saliguori BlockDriver *drv = bs->drv; 2610178e08a5Saliguori if (!drv) 2611178e08a5Saliguori return -ENOMEDIUM; 26127cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 261345566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 26147cdb1f6dSMORITA Kazutaka if (bs->file) 26157cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 26167cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2617178e08a5Saliguori } 2618178e08a5Saliguori 26198b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 26208b9b0cc2SKevin Wolf { 26218b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 26228b9b0cc2SKevin Wolf 26238b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 26248b9b0cc2SKevin Wolf return; 26258b9b0cc2SKevin Wolf } 26268b9b0cc2SKevin Wolf 26270ed8b6f6SBlue Swirl drv->bdrv_debug_event(bs, event); 26288b9b0cc2SKevin Wolf 26298b9b0cc2SKevin Wolf } 26308b9b0cc2SKevin Wolf 2631faea38e7Sbellard /**************************************************************/ 2632faea38e7Sbellard /* handling of snapshots */ 2633faea38e7Sbellard 2634feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 2635feeee5acSMiguel Di Ciurcio Filho { 2636feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 263707b70bfbSMarkus Armbruster if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 2638feeee5acSMiguel Di Ciurcio Filho return 0; 2639feeee5acSMiguel Di Ciurcio Filho } 2640feeee5acSMiguel Di Ciurcio Filho 2641feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 2642feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 2643feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 2644feeee5acSMiguel Di Ciurcio Filho } 2645feeee5acSMiguel Di Ciurcio Filho return 0; 2646feeee5acSMiguel Di Ciurcio Filho } 2647feeee5acSMiguel Di Ciurcio Filho 2648feeee5acSMiguel Di Ciurcio Filho return 1; 2649feeee5acSMiguel Di Ciurcio Filho } 2650feeee5acSMiguel Di Ciurcio Filho 2651199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 2652199630b6SBlue Swirl { 2653199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 2654199630b6SBlue Swirl } 2655199630b6SBlue Swirl 2656f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 2657f9092b10SMarkus Armbruster { 2658f9092b10SMarkus Armbruster BlockDriverState *bs; 2659f9092b10SMarkus Armbruster 26603ac906f7SMarkus Armbruster if (bs_snapshots) { 2661f9092b10SMarkus Armbruster return bs_snapshots; 26623ac906f7SMarkus Armbruster } 2663f9092b10SMarkus Armbruster 2664f9092b10SMarkus Armbruster bs = NULL; 2665f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 2666f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 26673ac906f7SMarkus Armbruster bs_snapshots = bs; 26683ac906f7SMarkus Armbruster return bs; 2669f9092b10SMarkus Armbruster } 2670f9092b10SMarkus Armbruster } 2671f9092b10SMarkus Armbruster return NULL; 2672f9092b10SMarkus Armbruster } 2673f9092b10SMarkus Armbruster 2674faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 2675faea38e7Sbellard QEMUSnapshotInfo *sn_info) 2676faea38e7Sbellard { 2677faea38e7Sbellard BlockDriver *drv = bs->drv; 2678faea38e7Sbellard if (!drv) 267919cb3738Sbellard return -ENOMEDIUM; 26807cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 2681faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 26827cdb1f6dSMORITA Kazutaka if (bs->file) 26837cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 26847cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2685faea38e7Sbellard } 2686faea38e7Sbellard 2687faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 2688faea38e7Sbellard const char *snapshot_id) 2689faea38e7Sbellard { 2690faea38e7Sbellard BlockDriver *drv = bs->drv; 26917cdb1f6dSMORITA Kazutaka int ret, open_ret; 26927cdb1f6dSMORITA Kazutaka 2693faea38e7Sbellard if (!drv) 269419cb3738Sbellard return -ENOMEDIUM; 26957cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 2696faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 26977cdb1f6dSMORITA Kazutaka 26987cdb1f6dSMORITA Kazutaka if (bs->file) { 26997cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 27007cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 27017cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 27027cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 27037cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 27047cdb1f6dSMORITA Kazutaka bs->drv = NULL; 27057cdb1f6dSMORITA Kazutaka return open_ret; 27067cdb1f6dSMORITA Kazutaka } 27077cdb1f6dSMORITA Kazutaka return ret; 27087cdb1f6dSMORITA Kazutaka } 27097cdb1f6dSMORITA Kazutaka 27107cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2711faea38e7Sbellard } 2712faea38e7Sbellard 2713faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 2714faea38e7Sbellard { 2715faea38e7Sbellard BlockDriver *drv = bs->drv; 2716faea38e7Sbellard if (!drv) 271719cb3738Sbellard return -ENOMEDIUM; 27187cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 2719faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 27207cdb1f6dSMORITA Kazutaka if (bs->file) 27217cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 27227cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2723faea38e7Sbellard } 2724faea38e7Sbellard 2725faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 2726faea38e7Sbellard QEMUSnapshotInfo **psn_info) 2727faea38e7Sbellard { 2728faea38e7Sbellard BlockDriver *drv = bs->drv; 2729faea38e7Sbellard if (!drv) 273019cb3738Sbellard return -ENOMEDIUM; 27317cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 2732faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 27337cdb1f6dSMORITA Kazutaka if (bs->file) 27347cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 27357cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2736faea38e7Sbellard } 2737faea38e7Sbellard 273851ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 273951ef6727Sedison const char *snapshot_name) 274051ef6727Sedison { 274151ef6727Sedison BlockDriver *drv = bs->drv; 274251ef6727Sedison if (!drv) { 274351ef6727Sedison return -ENOMEDIUM; 274451ef6727Sedison } 274551ef6727Sedison if (!bs->read_only) { 274651ef6727Sedison return -EINVAL; 274751ef6727Sedison } 274851ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 274951ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 275051ef6727Sedison } 275151ef6727Sedison return -ENOTSUP; 275251ef6727Sedison } 275351ef6727Sedison 2754e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 2755e8a6bb9cSMarcelo Tosatti const char *backing_file) 2756e8a6bb9cSMarcelo Tosatti { 2757e8a6bb9cSMarcelo Tosatti if (!bs->drv) { 2758e8a6bb9cSMarcelo Tosatti return NULL; 2759e8a6bb9cSMarcelo Tosatti } 2760e8a6bb9cSMarcelo Tosatti 2761e8a6bb9cSMarcelo Tosatti if (bs->backing_hd) { 2762e8a6bb9cSMarcelo Tosatti if (strcmp(bs->backing_file, backing_file) == 0) { 2763e8a6bb9cSMarcelo Tosatti return bs->backing_hd; 2764e8a6bb9cSMarcelo Tosatti } else { 2765e8a6bb9cSMarcelo Tosatti return bdrv_find_backing_image(bs->backing_hd, backing_file); 2766e8a6bb9cSMarcelo Tosatti } 2767e8a6bb9cSMarcelo Tosatti } 2768e8a6bb9cSMarcelo Tosatti 2769e8a6bb9cSMarcelo Tosatti return NULL; 2770e8a6bb9cSMarcelo Tosatti } 2771e8a6bb9cSMarcelo Tosatti 2772f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 2773f198fd1cSBenoît Canet { 2774f198fd1cSBenoît Canet if (!bs->drv) { 2775f198fd1cSBenoît Canet return 0; 2776f198fd1cSBenoît Canet } 2777f198fd1cSBenoît Canet 2778f198fd1cSBenoît Canet if (!bs->backing_hd) { 2779f198fd1cSBenoît Canet return 0; 2780f198fd1cSBenoît Canet } 2781f198fd1cSBenoît Canet 2782f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 2783f198fd1cSBenoît Canet } 2784f198fd1cSBenoît Canet 2785faea38e7Sbellard #define NB_SUFFIXES 4 2786faea38e7Sbellard 2787faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 2788faea38e7Sbellard { 2789faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 2790faea38e7Sbellard int64_t base; 2791faea38e7Sbellard int i; 2792faea38e7Sbellard 2793faea38e7Sbellard if (size <= 999) { 2794faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 2795faea38e7Sbellard } else { 2796faea38e7Sbellard base = 1024; 2797faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 2798faea38e7Sbellard if (size < (10 * base)) { 2799faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 2800faea38e7Sbellard (double)size / base, 2801faea38e7Sbellard suffixes[i]); 2802faea38e7Sbellard break; 2803faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 2804faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 2805faea38e7Sbellard ((size + (base >> 1)) / base), 2806faea38e7Sbellard suffixes[i]); 2807faea38e7Sbellard break; 2808faea38e7Sbellard } 2809faea38e7Sbellard base = base * 1024; 2810faea38e7Sbellard } 2811faea38e7Sbellard } 2812faea38e7Sbellard return buf; 2813faea38e7Sbellard } 2814faea38e7Sbellard 2815faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 2816faea38e7Sbellard { 2817faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 28183b9f94e1Sbellard #ifdef _WIN32 28193b9f94e1Sbellard struct tm *ptm; 28203b9f94e1Sbellard #else 2821faea38e7Sbellard struct tm tm; 28223b9f94e1Sbellard #endif 2823faea38e7Sbellard time_t ti; 2824faea38e7Sbellard int64_t secs; 2825faea38e7Sbellard 2826faea38e7Sbellard if (!sn) { 2827faea38e7Sbellard snprintf(buf, buf_size, 2828faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2829faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 2830faea38e7Sbellard } else { 2831faea38e7Sbellard ti = sn->date_sec; 28323b9f94e1Sbellard #ifdef _WIN32 28333b9f94e1Sbellard ptm = localtime(&ti); 28343b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 28353b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 28363b9f94e1Sbellard #else 2837faea38e7Sbellard localtime_r(&ti, &tm); 2838faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 2839faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 28403b9f94e1Sbellard #endif 2841faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 2842faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 2843faea38e7Sbellard "%02d:%02d:%02d.%03d", 2844faea38e7Sbellard (int)(secs / 3600), 2845faea38e7Sbellard (int)((secs / 60) % 60), 2846faea38e7Sbellard (int)(secs % 60), 2847faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 2848faea38e7Sbellard snprintf(buf, buf_size, 2849faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2850faea38e7Sbellard sn->id_str, sn->name, 2851faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 2852faea38e7Sbellard date_buf, 2853faea38e7Sbellard clock_buf); 2854faea38e7Sbellard } 2855faea38e7Sbellard return buf; 2856faea38e7Sbellard } 2857faea38e7Sbellard 2858ea2384d3Sbellard /**************************************************************/ 285983f64091Sbellard /* async I/Os */ 2860ea2384d3Sbellard 28613b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 2862f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 286383f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 2864ea2384d3Sbellard { 2865bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 2866bbf0a440SStefan Hajnoczi 2867b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 28688c5873d6SStefan Hajnoczi cb, opaque, false); 286983f64091Sbellard } 287083f64091Sbellard 2871f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 2872f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 287383f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 28747674e7bfSbellard { 2875bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 2876bbf0a440SStefan Hajnoczi 28771a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 28788c5873d6SStefan Hajnoczi cb, opaque, true); 287983f64091Sbellard } 288083f64091Sbellard 288140b4f539SKevin Wolf 288240b4f539SKevin Wolf typedef struct MultiwriteCB { 288340b4f539SKevin Wolf int error; 288440b4f539SKevin Wolf int num_requests; 288540b4f539SKevin Wolf int num_callbacks; 288640b4f539SKevin Wolf struct { 288740b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 288840b4f539SKevin Wolf void *opaque; 288940b4f539SKevin Wolf QEMUIOVector *free_qiov; 289040b4f539SKevin Wolf } callbacks[]; 289140b4f539SKevin Wolf } MultiwriteCB; 289240b4f539SKevin Wolf 289340b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 289440b4f539SKevin Wolf { 289540b4f539SKevin Wolf int i; 289640b4f539SKevin Wolf 289740b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 289840b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 28991e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 29001e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 29011e1ea48dSStefan Hajnoczi } 29027267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 290340b4f539SKevin Wolf } 290440b4f539SKevin Wolf } 290540b4f539SKevin Wolf 290640b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 290740b4f539SKevin Wolf { 290840b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 290940b4f539SKevin Wolf 29106d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 29116d519a5fSStefan Hajnoczi 2912cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 291340b4f539SKevin Wolf mcb->error = ret; 291440b4f539SKevin Wolf } 291540b4f539SKevin Wolf 291640b4f539SKevin Wolf mcb->num_requests--; 291740b4f539SKevin Wolf if (mcb->num_requests == 0) { 291840b4f539SKevin Wolf multiwrite_user_cb(mcb); 29197267c094SAnthony Liguori g_free(mcb); 292040b4f539SKevin Wolf } 292140b4f539SKevin Wolf } 292240b4f539SKevin Wolf 292340b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 292440b4f539SKevin Wolf { 292577be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 292677be4366SChristoph Hellwig 292777be4366SChristoph Hellwig /* 292877be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 292977be4366SChristoph Hellwig * here as that could overflow the return value. 293077be4366SChristoph Hellwig */ 293177be4366SChristoph Hellwig if (req1->sector > req2->sector) { 293277be4366SChristoph Hellwig return 1; 293377be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 293477be4366SChristoph Hellwig return -1; 293577be4366SChristoph Hellwig } else { 293677be4366SChristoph Hellwig return 0; 293777be4366SChristoph Hellwig } 293840b4f539SKevin Wolf } 293940b4f539SKevin Wolf 294040b4f539SKevin Wolf /* 294140b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 294240b4f539SKevin Wolf * requests that remain after merging. 294340b4f539SKevin Wolf */ 294440b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 294540b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 294640b4f539SKevin Wolf { 294740b4f539SKevin Wolf int i, outidx; 294840b4f539SKevin Wolf 294940b4f539SKevin Wolf // Sort requests by start sector 295040b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 295140b4f539SKevin Wolf 295240b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 295340b4f539SKevin Wolf // filling up gaps with zero sectors. 295440b4f539SKevin Wolf outidx = 0; 295540b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 295640b4f539SKevin Wolf int merge = 0; 295740b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 295840b4f539SKevin Wolf 2959b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 296040b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 296140b4f539SKevin Wolf merge = 1; 296240b4f539SKevin Wolf } 296340b4f539SKevin Wolf 2964e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 2965e2a305fbSChristoph Hellwig merge = 0; 2966e2a305fbSChristoph Hellwig } 2967e2a305fbSChristoph Hellwig 296840b4f539SKevin Wolf if (merge) { 296940b4f539SKevin Wolf size_t size; 29707267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 297140b4f539SKevin Wolf qemu_iovec_init(qiov, 297240b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 297340b4f539SKevin Wolf 297440b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 297540b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 297640b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 29771b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 297840b4f539SKevin Wolf 2979b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 2980b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 298140b4f539SKevin Wolf 298240b4f539SKevin Wolf // Add the second request 29831b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 298440b4f539SKevin Wolf 2985cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 298640b4f539SKevin Wolf reqs[outidx].qiov = qiov; 298740b4f539SKevin Wolf 298840b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 298940b4f539SKevin Wolf } else { 299040b4f539SKevin Wolf outidx++; 299140b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 299240b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 299340b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 299440b4f539SKevin Wolf } 299540b4f539SKevin Wolf } 299640b4f539SKevin Wolf 299740b4f539SKevin Wolf return outidx + 1; 299840b4f539SKevin Wolf } 299940b4f539SKevin Wolf 300040b4f539SKevin Wolf /* 300140b4f539SKevin Wolf * Submit multiple AIO write requests at once. 300240b4f539SKevin Wolf * 300340b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 300440b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 300540b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 300640b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 300740b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 300840b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 300940b4f539SKevin Wolf * 301040b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 301140b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 301240b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 301340b4f539SKevin Wolf */ 301440b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 301540b4f539SKevin Wolf { 301640b4f539SKevin Wolf MultiwriteCB *mcb; 301740b4f539SKevin Wolf int i; 301840b4f539SKevin Wolf 3019301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3020301db7c2SRyan Harper if (bs->drv == NULL) { 3021301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3022301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3023301db7c2SRyan Harper } 3024301db7c2SRyan Harper return -1; 3025301db7c2SRyan Harper } 3026301db7c2SRyan Harper 302740b4f539SKevin Wolf if (num_reqs == 0) { 302840b4f539SKevin Wolf return 0; 302940b4f539SKevin Wolf } 303040b4f539SKevin Wolf 303140b4f539SKevin Wolf // Create MultiwriteCB structure 30327267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 303340b4f539SKevin Wolf mcb->num_requests = 0; 303440b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 303540b4f539SKevin Wolf 303640b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 303740b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 303840b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 303940b4f539SKevin Wolf } 304040b4f539SKevin Wolf 304140b4f539SKevin Wolf // Check for mergable requests 304240b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 304340b4f539SKevin Wolf 30446d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 30456d519a5fSStefan Hajnoczi 3046df9309fbSPaolo Bonzini /* Run the aio requests. */ 3047df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 304840b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3049ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 305040b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 305140b4f539SKevin Wolf } 305240b4f539SKevin Wolf 305340b4f539SKevin Wolf return 0; 305440b4f539SKevin Wolf } 305540b4f539SKevin Wolf 305683f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 305783f64091Sbellard { 30586bbff9a0Saliguori acb->pool->cancel(acb); 305983f64091Sbellard } 306083f64091Sbellard 306198f90dbaSZhi Yong Wu /* block I/O throttling */ 306298f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 306398f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait) 306498f90dbaSZhi Yong Wu { 306598f90dbaSZhi Yong Wu uint64_t bps_limit = 0; 306698f90dbaSZhi Yong Wu double bytes_limit, bytes_base, bytes_res; 306798f90dbaSZhi Yong Wu double slice_time, wait_time; 306898f90dbaSZhi Yong Wu 306998f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 307098f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 307198f90dbaSZhi Yong Wu } else if (bs->io_limits.bps[is_write]) { 307298f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[is_write]; 307398f90dbaSZhi Yong Wu } else { 307498f90dbaSZhi Yong Wu if (wait) { 307598f90dbaSZhi Yong Wu *wait = 0; 307698f90dbaSZhi Yong Wu } 307798f90dbaSZhi Yong Wu 307898f90dbaSZhi Yong Wu return false; 307998f90dbaSZhi Yong Wu } 308098f90dbaSZhi Yong Wu 308198f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 308298f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 308398f90dbaSZhi Yong Wu bytes_limit = bps_limit * slice_time; 308498f90dbaSZhi Yong Wu bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; 308598f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 308698f90dbaSZhi Yong Wu bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; 308798f90dbaSZhi Yong Wu } 308898f90dbaSZhi Yong Wu 308998f90dbaSZhi Yong Wu /* bytes_base: the bytes of data which have been read/written; and 309098f90dbaSZhi Yong Wu * it is obtained from the history statistic info. 309198f90dbaSZhi Yong Wu * bytes_res: the remaining bytes of data which need to be read/written. 309298f90dbaSZhi Yong Wu * (bytes_base + bytes_res) / bps_limit: used to calcuate 309398f90dbaSZhi Yong Wu * the total time for completing reading/writting all data. 309498f90dbaSZhi Yong Wu */ 309598f90dbaSZhi Yong Wu bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 309698f90dbaSZhi Yong Wu 309798f90dbaSZhi Yong Wu if (bytes_base + bytes_res <= bytes_limit) { 309898f90dbaSZhi Yong Wu if (wait) { 309998f90dbaSZhi Yong Wu *wait = 0; 310098f90dbaSZhi Yong Wu } 310198f90dbaSZhi Yong Wu 310298f90dbaSZhi Yong Wu return false; 310398f90dbaSZhi Yong Wu } 310498f90dbaSZhi Yong Wu 310598f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 310698f90dbaSZhi Yong Wu wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; 310798f90dbaSZhi Yong Wu 310898f90dbaSZhi Yong Wu /* When the I/O rate at runtime exceeds the limits, 310998f90dbaSZhi Yong Wu * bs->slice_end need to be extended in order that the current statistic 311098f90dbaSZhi Yong Wu * info can be kept until the timer fire, so it is increased and tuned 311198f90dbaSZhi Yong Wu * based on the result of experiment. 311298f90dbaSZhi Yong Wu */ 311398f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 311498f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 311598f90dbaSZhi Yong Wu if (wait) { 311698f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 311798f90dbaSZhi Yong Wu } 311898f90dbaSZhi Yong Wu 311998f90dbaSZhi Yong Wu return true; 312098f90dbaSZhi Yong Wu } 312198f90dbaSZhi Yong Wu 312298f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 312398f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait) 312498f90dbaSZhi Yong Wu { 312598f90dbaSZhi Yong Wu uint64_t iops_limit = 0; 312698f90dbaSZhi Yong Wu double ios_limit, ios_base; 312798f90dbaSZhi Yong Wu double slice_time, wait_time; 312898f90dbaSZhi Yong Wu 312998f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 313098f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 313198f90dbaSZhi Yong Wu } else if (bs->io_limits.iops[is_write]) { 313298f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[is_write]; 313398f90dbaSZhi Yong Wu } else { 313498f90dbaSZhi Yong Wu if (wait) { 313598f90dbaSZhi Yong Wu *wait = 0; 313698f90dbaSZhi Yong Wu } 313798f90dbaSZhi Yong Wu 313898f90dbaSZhi Yong Wu return false; 313998f90dbaSZhi Yong Wu } 314098f90dbaSZhi Yong Wu 314198f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 314298f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 314398f90dbaSZhi Yong Wu ios_limit = iops_limit * slice_time; 314498f90dbaSZhi Yong Wu ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; 314598f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 314698f90dbaSZhi Yong Wu ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; 314798f90dbaSZhi Yong Wu } 314898f90dbaSZhi Yong Wu 314998f90dbaSZhi Yong Wu if (ios_base + 1 <= ios_limit) { 315098f90dbaSZhi Yong Wu if (wait) { 315198f90dbaSZhi Yong Wu *wait = 0; 315298f90dbaSZhi Yong Wu } 315398f90dbaSZhi Yong Wu 315498f90dbaSZhi Yong Wu return false; 315598f90dbaSZhi Yong Wu } 315698f90dbaSZhi Yong Wu 315798f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 315898f90dbaSZhi Yong Wu wait_time = (ios_base + 1) / iops_limit; 315998f90dbaSZhi Yong Wu if (wait_time > elapsed_time) { 316098f90dbaSZhi Yong Wu wait_time = wait_time - elapsed_time; 316198f90dbaSZhi Yong Wu } else { 316298f90dbaSZhi Yong Wu wait_time = 0; 316398f90dbaSZhi Yong Wu } 316498f90dbaSZhi Yong Wu 316598f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 316698f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 316798f90dbaSZhi Yong Wu if (wait) { 316898f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 316998f90dbaSZhi Yong Wu } 317098f90dbaSZhi Yong Wu 317198f90dbaSZhi Yong Wu return true; 317298f90dbaSZhi Yong Wu } 317398f90dbaSZhi Yong Wu 317498f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 317598f90dbaSZhi Yong Wu bool is_write, int64_t *wait) 317698f90dbaSZhi Yong Wu { 317798f90dbaSZhi Yong Wu int64_t now, max_wait; 317898f90dbaSZhi Yong Wu uint64_t bps_wait = 0, iops_wait = 0; 317998f90dbaSZhi Yong Wu double elapsed_time; 318098f90dbaSZhi Yong Wu int bps_ret, iops_ret; 318198f90dbaSZhi Yong Wu 318298f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 318398f90dbaSZhi Yong Wu if ((bs->slice_start < now) 318498f90dbaSZhi Yong Wu && (bs->slice_end > now)) { 318598f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 318698f90dbaSZhi Yong Wu } else { 318798f90dbaSZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 318898f90dbaSZhi Yong Wu bs->slice_start = now; 318998f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 319098f90dbaSZhi Yong Wu 319198f90dbaSZhi Yong Wu bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; 319298f90dbaSZhi Yong Wu bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; 319398f90dbaSZhi Yong Wu 319498f90dbaSZhi Yong Wu bs->io_base.ios[is_write] = bs->nr_ops[is_write]; 319598f90dbaSZhi Yong Wu bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; 319698f90dbaSZhi Yong Wu } 319798f90dbaSZhi Yong Wu 319898f90dbaSZhi Yong Wu elapsed_time = now - bs->slice_start; 319998f90dbaSZhi Yong Wu elapsed_time /= (NANOSECONDS_PER_SECOND); 320098f90dbaSZhi Yong Wu 320198f90dbaSZhi Yong Wu bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, 320298f90dbaSZhi Yong Wu is_write, elapsed_time, &bps_wait); 320398f90dbaSZhi Yong Wu iops_ret = bdrv_exceed_iops_limits(bs, is_write, 320498f90dbaSZhi Yong Wu elapsed_time, &iops_wait); 320598f90dbaSZhi Yong Wu if (bps_ret || iops_ret) { 320698f90dbaSZhi Yong Wu max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; 320798f90dbaSZhi Yong Wu if (wait) { 320898f90dbaSZhi Yong Wu *wait = max_wait; 320998f90dbaSZhi Yong Wu } 321098f90dbaSZhi Yong Wu 321198f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 321298f90dbaSZhi Yong Wu if (bs->slice_end < now + max_wait) { 321398f90dbaSZhi Yong Wu bs->slice_end = now + max_wait; 321498f90dbaSZhi Yong Wu } 321598f90dbaSZhi Yong Wu 321698f90dbaSZhi Yong Wu return true; 321798f90dbaSZhi Yong Wu } 321898f90dbaSZhi Yong Wu 321998f90dbaSZhi Yong Wu if (wait) { 322098f90dbaSZhi Yong Wu *wait = 0; 322198f90dbaSZhi Yong Wu } 322298f90dbaSZhi Yong Wu 322398f90dbaSZhi Yong Wu return false; 322498f90dbaSZhi Yong Wu } 322583f64091Sbellard 322683f64091Sbellard /**************************************************************/ 322783f64091Sbellard /* async block device emulation */ 322883f64091Sbellard 3229c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3230c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3231c16b5a2cSChristoph Hellwig QEMUBH *bh; 3232c16b5a2cSChristoph Hellwig int ret; 3233c16b5a2cSChristoph Hellwig /* vector translation state */ 3234c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3235c16b5a2cSChristoph Hellwig uint8_t *bounce; 3236c16b5a2cSChristoph Hellwig int is_write; 3237c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3238c16b5a2cSChristoph Hellwig 3239c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3240c16b5a2cSChristoph Hellwig { 3241b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3242b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 32436a7ad299SDor Laor qemu_bh_delete(acb->bh); 324436afc451SAvi Kivity acb->bh = NULL; 3245c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3246c16b5a2cSChristoph Hellwig } 3247c16b5a2cSChristoph Hellwig 3248c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 3249c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3250c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3251c16b5a2cSChristoph Hellwig }; 3252c16b5a2cSChristoph Hellwig 325383f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3254beac80cdSbellard { 3255ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3256f141eafeSaliguori 3257f141eafeSaliguori if (!acb->is_write) 325803396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 3259ceb42de8Saliguori qemu_vfree(acb->bounce); 3260ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 32616a7ad299SDor Laor qemu_bh_delete(acb->bh); 326236afc451SAvi Kivity acb->bh = NULL; 3263ce1a14dcSpbrook qemu_aio_release(acb); 3264beac80cdSbellard } 3265beac80cdSbellard 3266f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3267f141eafeSaliguori int64_t sector_num, 3268f141eafeSaliguori QEMUIOVector *qiov, 3269f141eafeSaliguori int nb_sectors, 3270f141eafeSaliguori BlockDriverCompletionFunc *cb, 3271f141eafeSaliguori void *opaque, 3272f141eafeSaliguori int is_write) 3273f141eafeSaliguori 3274ea2384d3Sbellard { 3275ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 327683f64091Sbellard 3277c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 3278f141eafeSaliguori acb->is_write = is_write; 3279f141eafeSaliguori acb->qiov = qiov; 3280e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3281ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3282f141eafeSaliguori 3283f141eafeSaliguori if (is_write) { 3284d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 32851ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3286f141eafeSaliguori } else { 32871ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3288f141eafeSaliguori } 3289f141eafeSaliguori 3290ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3291f141eafeSaliguori 3292ce1a14dcSpbrook return &acb->common; 32937a6cba61Spbrook } 32947a6cba61Spbrook 3295f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3296f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3297ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 329883f64091Sbellard { 3299f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 330083f64091Sbellard } 330183f64091Sbellard 3302f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3303f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3304f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3305f141eafeSaliguori { 3306f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3307f141eafeSaliguori } 3308f141eafeSaliguori 330968485420SKevin Wolf 331068485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 331168485420SKevin Wolf BlockDriverAIOCB common; 331268485420SKevin Wolf BlockRequest req; 331368485420SKevin Wolf bool is_write; 331468485420SKevin Wolf QEMUBH* bh; 331568485420SKevin Wolf } BlockDriverAIOCBCoroutine; 331668485420SKevin Wolf 331768485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 331868485420SKevin Wolf { 331968485420SKevin Wolf qemu_aio_flush(); 332068485420SKevin Wolf } 332168485420SKevin Wolf 332268485420SKevin Wolf static AIOPool bdrv_em_co_aio_pool = { 332368485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 332468485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 332568485420SKevin Wolf }; 332668485420SKevin Wolf 332735246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 332868485420SKevin Wolf { 332968485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 333068485420SKevin Wolf 333168485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 333268485420SKevin Wolf qemu_bh_delete(acb->bh); 333368485420SKevin Wolf qemu_aio_release(acb); 333468485420SKevin Wolf } 333568485420SKevin Wolf 3336b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3337b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3338b2a61371SStefan Hajnoczi { 3339b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3340b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3341b2a61371SStefan Hajnoczi 3342b2a61371SStefan Hajnoczi if (!acb->is_write) { 3343b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 3344470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3345b2a61371SStefan Hajnoczi } else { 3346b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 3347f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3348b2a61371SStefan Hajnoczi } 3349b2a61371SStefan Hajnoczi 335035246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3351b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 3352b2a61371SStefan Hajnoczi } 3353b2a61371SStefan Hajnoczi 335468485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 335568485420SKevin Wolf int64_t sector_num, 335668485420SKevin Wolf QEMUIOVector *qiov, 335768485420SKevin Wolf int nb_sectors, 335868485420SKevin Wolf BlockDriverCompletionFunc *cb, 335968485420SKevin Wolf void *opaque, 33608c5873d6SStefan Hajnoczi bool is_write) 336168485420SKevin Wolf { 336268485420SKevin Wolf Coroutine *co; 336368485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 336468485420SKevin Wolf 336568485420SKevin Wolf acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 336668485420SKevin Wolf acb->req.sector = sector_num; 336768485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 336868485420SKevin Wolf acb->req.qiov = qiov; 336968485420SKevin Wolf acb->is_write = is_write; 337068485420SKevin Wolf 33718c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 337268485420SKevin Wolf qemu_coroutine_enter(co, acb); 337368485420SKevin Wolf 337468485420SKevin Wolf return &acb->common; 337568485420SKevin Wolf } 337668485420SKevin Wolf 337707f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 3378b2e12bc6SChristoph Hellwig { 337907f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 338007f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 3381b2e12bc6SChristoph Hellwig 338207f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 338307f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3384b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 3385b2e12bc6SChristoph Hellwig } 3386b2e12bc6SChristoph Hellwig 338707f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 3388016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 3389016f5cf6SAlexander Graf { 339007f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 3391016f5cf6SAlexander Graf 339207f07615SPaolo Bonzini Coroutine *co; 339307f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 3394016f5cf6SAlexander Graf 339507f07615SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 339607f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 339707f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 3398016f5cf6SAlexander Graf 3399016f5cf6SAlexander Graf return &acb->common; 3400016f5cf6SAlexander Graf } 3401016f5cf6SAlexander Graf 34024265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 34034265d620SPaolo Bonzini { 34044265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 34054265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 34064265d620SPaolo Bonzini 34074265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 34084265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 34094265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 34104265d620SPaolo Bonzini } 34114265d620SPaolo Bonzini 34124265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 34134265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 34144265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 34154265d620SPaolo Bonzini { 34164265d620SPaolo Bonzini Coroutine *co; 34174265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 34184265d620SPaolo Bonzini 34194265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 34204265d620SPaolo Bonzini 34214265d620SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 34224265d620SPaolo Bonzini acb->req.sector = sector_num; 34234265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 34244265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 34254265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 34264265d620SPaolo Bonzini 34274265d620SPaolo Bonzini return &acb->common; 34284265d620SPaolo Bonzini } 34294265d620SPaolo Bonzini 3430ea2384d3Sbellard void bdrv_init(void) 3431ea2384d3Sbellard { 34325efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3433ea2384d3Sbellard } 3434ce1a14dcSpbrook 3435eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3436eb852011SMarkus Armbruster { 3437eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3438eb852011SMarkus Armbruster bdrv_init(); 3439eb852011SMarkus Armbruster } 3440eb852011SMarkus Armbruster 3441c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 34426bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 34436bbff9a0Saliguori { 3444ce1a14dcSpbrook BlockDriverAIOCB *acb; 3445ce1a14dcSpbrook 34466bbff9a0Saliguori if (pool->free_aiocb) { 34476bbff9a0Saliguori acb = pool->free_aiocb; 34486bbff9a0Saliguori pool->free_aiocb = acb->next; 3449ce1a14dcSpbrook } else { 34507267c094SAnthony Liguori acb = g_malloc0(pool->aiocb_size); 34516bbff9a0Saliguori acb->pool = pool; 3452ce1a14dcSpbrook } 3453ce1a14dcSpbrook acb->bs = bs; 3454ce1a14dcSpbrook acb->cb = cb; 3455ce1a14dcSpbrook acb->opaque = opaque; 3456ce1a14dcSpbrook return acb; 3457ce1a14dcSpbrook } 3458ce1a14dcSpbrook 3459ce1a14dcSpbrook void qemu_aio_release(void *p) 3460ce1a14dcSpbrook { 34616bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 34626bbff9a0Saliguori AIOPool *pool = acb->pool; 34636bbff9a0Saliguori acb->next = pool->free_aiocb; 34646bbff9a0Saliguori pool->free_aiocb = acb; 3465ce1a14dcSpbrook } 346619cb3738Sbellard 346719cb3738Sbellard /**************************************************************/ 3468f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 3469f9f05dc5SKevin Wolf 3470f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 3471f9f05dc5SKevin Wolf Coroutine *coroutine; 3472f9f05dc5SKevin Wolf int ret; 3473f9f05dc5SKevin Wolf } CoroutineIOCompletion; 3474f9f05dc5SKevin Wolf 3475f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 3476f9f05dc5SKevin Wolf { 3477f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 3478f9f05dc5SKevin Wolf 3479f9f05dc5SKevin Wolf co->ret = ret; 3480f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 3481f9f05dc5SKevin Wolf } 3482f9f05dc5SKevin Wolf 3483f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 3484f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 3485f9f05dc5SKevin Wolf bool is_write) 3486f9f05dc5SKevin Wolf { 3487f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 3488f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 3489f9f05dc5SKevin Wolf }; 3490f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 3491f9f05dc5SKevin Wolf 3492f9f05dc5SKevin Wolf if (is_write) { 3493a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 3494f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3495f9f05dc5SKevin Wolf } else { 3496a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 3497f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3498f9f05dc5SKevin Wolf } 3499f9f05dc5SKevin Wolf 350059370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 3501f9f05dc5SKevin Wolf if (!acb) { 3502f9f05dc5SKevin Wolf return -EIO; 3503f9f05dc5SKevin Wolf } 3504f9f05dc5SKevin Wolf qemu_coroutine_yield(); 3505f9f05dc5SKevin Wolf 3506f9f05dc5SKevin Wolf return co.ret; 3507f9f05dc5SKevin Wolf } 3508f9f05dc5SKevin Wolf 3509f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 3510f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3511f9f05dc5SKevin Wolf QEMUIOVector *iov) 3512f9f05dc5SKevin Wolf { 3513f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 3514f9f05dc5SKevin Wolf } 3515f9f05dc5SKevin Wolf 3516f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 3517f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3518f9f05dc5SKevin Wolf QEMUIOVector *iov) 3519f9f05dc5SKevin Wolf { 3520f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 3521f9f05dc5SKevin Wolf } 3522f9f05dc5SKevin Wolf 352307f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 3524e7a8a783SKevin Wolf { 352507f07615SPaolo Bonzini RwCo *rwco = opaque; 352607f07615SPaolo Bonzini 352707f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 352807f07615SPaolo Bonzini } 352907f07615SPaolo Bonzini 353007f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 353107f07615SPaolo Bonzini { 3532eb489bb1SKevin Wolf int ret; 3533eb489bb1SKevin Wolf 353429cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 353507f07615SPaolo Bonzini return 0; 3536eb489bb1SKevin Wolf } 3537eb489bb1SKevin Wolf 3538ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 3539eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 3540eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 3541eb489bb1SKevin Wolf if (ret < 0) { 3542eb489bb1SKevin Wolf return ret; 3543eb489bb1SKevin Wolf } 3544eb489bb1SKevin Wolf } 3545eb489bb1SKevin Wolf 3546ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 3547ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 3548d4c82329SKevin Wolf goto flush_parent; 3549ca716364SKevin Wolf } 3550ca716364SKevin Wolf 3551eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 355229cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 355307f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 355407f07615SPaolo Bonzini BlockDriverAIOCB *acb; 3555e7a8a783SKevin Wolf CoroutineIOCompletion co = { 3556e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 3557e7a8a783SKevin Wolf }; 3558e7a8a783SKevin Wolf 355907f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 356007f07615SPaolo Bonzini if (acb == NULL) { 356129cdb251SPaolo Bonzini ret = -EIO; 356207f07615SPaolo Bonzini } else { 3563e7a8a783SKevin Wolf qemu_coroutine_yield(); 356429cdb251SPaolo Bonzini ret = co.ret; 3565e7a8a783SKevin Wolf } 356607f07615SPaolo Bonzini } else { 356707f07615SPaolo Bonzini /* 356807f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 356907f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 357007f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 357107f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 357207f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 357307f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 357407f07615SPaolo Bonzini * mode. 357507f07615SPaolo Bonzini * 357607f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 357707f07615SPaolo Bonzini */ 357829cdb251SPaolo Bonzini ret = 0; 357907f07615SPaolo Bonzini } 358029cdb251SPaolo Bonzini if (ret < 0) { 358129cdb251SPaolo Bonzini return ret; 358229cdb251SPaolo Bonzini } 358329cdb251SPaolo Bonzini 358429cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 358529cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 358629cdb251SPaolo Bonzini */ 3587d4c82329SKevin Wolf flush_parent: 358829cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 358907f07615SPaolo Bonzini } 359007f07615SPaolo Bonzini 35910f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 35920f15423cSAnthony Liguori { 35930f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 35940f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 35950f15423cSAnthony Liguori } 35960f15423cSAnthony Liguori } 35970f15423cSAnthony Liguori 35980f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 35990f15423cSAnthony Liguori { 36000f15423cSAnthony Liguori BlockDriverState *bs; 36010f15423cSAnthony Liguori 36020f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 36030f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 36040f15423cSAnthony Liguori } 36050f15423cSAnthony Liguori } 36060f15423cSAnthony Liguori 360707789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 360807789269SBenoît Canet { 360907789269SBenoît Canet BlockDriverState *bs; 361007789269SBenoît Canet 361107789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 361207789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 361307789269SBenoît Canet } 361407789269SBenoît Canet } 361507789269SBenoît Canet 361607f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 361707f07615SPaolo Bonzini { 361807f07615SPaolo Bonzini Coroutine *co; 361907f07615SPaolo Bonzini RwCo rwco = { 362007f07615SPaolo Bonzini .bs = bs, 362107f07615SPaolo Bonzini .ret = NOT_DONE, 362207f07615SPaolo Bonzini }; 362307f07615SPaolo Bonzini 362407f07615SPaolo Bonzini if (qemu_in_coroutine()) { 362507f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 362607f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 362707f07615SPaolo Bonzini } else { 362807f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 362907f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 363007f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 363107f07615SPaolo Bonzini qemu_aio_wait(); 363207f07615SPaolo Bonzini } 363307f07615SPaolo Bonzini } 363407f07615SPaolo Bonzini 363507f07615SPaolo Bonzini return rwco.ret; 363607f07615SPaolo Bonzini } 3637e7a8a783SKevin Wolf 36384265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 36394265d620SPaolo Bonzini { 36404265d620SPaolo Bonzini RwCo *rwco = opaque; 36414265d620SPaolo Bonzini 36424265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 36434265d620SPaolo Bonzini } 36444265d620SPaolo Bonzini 36454265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 36464265d620SPaolo Bonzini int nb_sectors) 36474265d620SPaolo Bonzini { 36484265d620SPaolo Bonzini if (!bs->drv) { 36494265d620SPaolo Bonzini return -ENOMEDIUM; 36504265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 36514265d620SPaolo Bonzini return -EIO; 36524265d620SPaolo Bonzini } else if (bs->read_only) { 36534265d620SPaolo Bonzini return -EROFS; 36544265d620SPaolo Bonzini } else if (bs->drv->bdrv_co_discard) { 36554265d620SPaolo Bonzini return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors); 36564265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 36574265d620SPaolo Bonzini BlockDriverAIOCB *acb; 36584265d620SPaolo Bonzini CoroutineIOCompletion co = { 36594265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 36604265d620SPaolo Bonzini }; 36614265d620SPaolo Bonzini 36624265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 36634265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 36644265d620SPaolo Bonzini if (acb == NULL) { 36654265d620SPaolo Bonzini return -EIO; 36664265d620SPaolo Bonzini } else { 36674265d620SPaolo Bonzini qemu_coroutine_yield(); 36684265d620SPaolo Bonzini return co.ret; 36694265d620SPaolo Bonzini } 36704265d620SPaolo Bonzini } else { 36714265d620SPaolo Bonzini return 0; 36724265d620SPaolo Bonzini } 36734265d620SPaolo Bonzini } 36744265d620SPaolo Bonzini 36754265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 36764265d620SPaolo Bonzini { 36774265d620SPaolo Bonzini Coroutine *co; 36784265d620SPaolo Bonzini RwCo rwco = { 36794265d620SPaolo Bonzini .bs = bs, 36804265d620SPaolo Bonzini .sector_num = sector_num, 36814265d620SPaolo Bonzini .nb_sectors = nb_sectors, 36824265d620SPaolo Bonzini .ret = NOT_DONE, 36834265d620SPaolo Bonzini }; 36844265d620SPaolo Bonzini 36854265d620SPaolo Bonzini if (qemu_in_coroutine()) { 36864265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 36874265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 36884265d620SPaolo Bonzini } else { 36894265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 36904265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 36914265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 36924265d620SPaolo Bonzini qemu_aio_wait(); 36934265d620SPaolo Bonzini } 36944265d620SPaolo Bonzini } 36954265d620SPaolo Bonzini 36964265d620SPaolo Bonzini return rwco.ret; 36974265d620SPaolo Bonzini } 36984265d620SPaolo Bonzini 3699f9f05dc5SKevin Wolf /**************************************************************/ 370019cb3738Sbellard /* removable device support */ 370119cb3738Sbellard 370219cb3738Sbellard /** 370319cb3738Sbellard * Return TRUE if the media is present 370419cb3738Sbellard */ 370519cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 370619cb3738Sbellard { 370719cb3738Sbellard BlockDriver *drv = bs->drv; 3708a1aff5bfSMarkus Armbruster 370919cb3738Sbellard if (!drv) 371019cb3738Sbellard return 0; 371119cb3738Sbellard if (!drv->bdrv_is_inserted) 3712a1aff5bfSMarkus Armbruster return 1; 3713a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 371419cb3738Sbellard } 371519cb3738Sbellard 371619cb3738Sbellard /** 37178e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 37188e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 371919cb3738Sbellard */ 372019cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 372119cb3738Sbellard { 372219cb3738Sbellard BlockDriver *drv = bs->drv; 372319cb3738Sbellard 37248e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 37258e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 37268e49ca46SMarkus Armbruster } 37278e49ca46SMarkus Armbruster return -ENOTSUP; 372819cb3738Sbellard } 372919cb3738Sbellard 373019cb3738Sbellard /** 373119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 373219cb3738Sbellard */ 3733f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 373419cb3738Sbellard { 373519cb3738Sbellard BlockDriver *drv = bs->drv; 373619cb3738Sbellard 3737822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 3738822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 373919cb3738Sbellard } 37406f382ed2SLuiz Capitulino 37416f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 37426f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 37436f382ed2SLuiz Capitulino } 374419cb3738Sbellard } 374519cb3738Sbellard 374619cb3738Sbellard /** 374719cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 374819cb3738Sbellard * to eject it manually). 374919cb3738Sbellard */ 3750025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 375119cb3738Sbellard { 375219cb3738Sbellard BlockDriver *drv = bs->drv; 375319cb3738Sbellard 3754025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 3755b8c6d095SStefan Hajnoczi 3756025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 3757025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 375819cb3738Sbellard } 375919cb3738Sbellard } 3760985a03b0Sths 3761985a03b0Sths /* needed for generic scsi interface */ 3762985a03b0Sths 3763985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 3764985a03b0Sths { 3765985a03b0Sths BlockDriver *drv = bs->drv; 3766985a03b0Sths 3767985a03b0Sths if (drv && drv->bdrv_ioctl) 3768985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 3769985a03b0Sths return -ENOTSUP; 3770985a03b0Sths } 37717d780669Saliguori 3772221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 3773221f715dSaliguori unsigned long int req, void *buf, 37747d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 37757d780669Saliguori { 3776221f715dSaliguori BlockDriver *drv = bs->drv; 37777d780669Saliguori 3778221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 3779221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 3780221f715dSaliguori return NULL; 37817d780669Saliguori } 3782e268ca52Saliguori 37837b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 37847b6f9300SMarkus Armbruster { 37857b6f9300SMarkus Armbruster bs->buffer_alignment = align; 37867b6f9300SMarkus Armbruster } 37877cd1e32aSlirans@il.ibm.com 3788e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 3789e268ca52Saliguori { 3790e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 3791e268ca52Saliguori } 37927cd1e32aSlirans@il.ibm.com 37937cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 37947cd1e32aSlirans@il.ibm.com { 37957cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 3796a55eb92cSJan Kiszka 3797aaa0eb75SLiran Schour bs->dirty_count = 0; 37987cd1e32aSlirans@il.ibm.com if (enable) { 3799c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 3800c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 380171df14fcSPaolo Bonzini BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG - 1; 380271df14fcSPaolo Bonzini bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * BITS_PER_LONG; 38037cd1e32aSlirans@il.ibm.com 380471df14fcSPaolo Bonzini bs->dirty_bitmap = g_new0(unsigned long, bitmap_size); 38057cd1e32aSlirans@il.ibm.com } 38067cd1e32aSlirans@il.ibm.com } else { 3807c6d22830SJan Kiszka if (bs->dirty_bitmap) { 38087267c094SAnthony Liguori g_free(bs->dirty_bitmap); 3809c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 38107cd1e32aSlirans@il.ibm.com } 38117cd1e32aSlirans@il.ibm.com } 38127cd1e32aSlirans@il.ibm.com } 38137cd1e32aSlirans@il.ibm.com 38147cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 38157cd1e32aSlirans@il.ibm.com { 38166ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 38177cd1e32aSlirans@il.ibm.com 3818c6d22830SJan Kiszka if (bs->dirty_bitmap && 3819c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 38206d59fec1SMarcelo Tosatti return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 38216d59fec1SMarcelo Tosatti (1UL << (chunk % (sizeof(unsigned long) * 8)))); 38227cd1e32aSlirans@il.ibm.com } else { 38237cd1e32aSlirans@il.ibm.com return 0; 38247cd1e32aSlirans@il.ibm.com } 38257cd1e32aSlirans@il.ibm.com } 38267cd1e32aSlirans@il.ibm.com 38277cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 38287cd1e32aSlirans@il.ibm.com int nr_sectors) 38297cd1e32aSlirans@il.ibm.com { 38307cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 38317cd1e32aSlirans@il.ibm.com } 3832aaa0eb75SLiran Schour 3833aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 3834aaa0eb75SLiran Schour { 3835aaa0eb75SLiran Schour return bs->dirty_count; 3836aaa0eb75SLiran Schour } 3837f88e1a42SJes Sorensen 3838db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 3839db593f25SMarcelo Tosatti { 3840db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 3841db593f25SMarcelo Tosatti bs->in_use = in_use; 3842db593f25SMarcelo Tosatti } 3843db593f25SMarcelo Tosatti 3844db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 3845db593f25SMarcelo Tosatti { 3846db593f25SMarcelo Tosatti return bs->in_use; 3847db593f25SMarcelo Tosatti } 3848db593f25SMarcelo Tosatti 384928a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 385028a7282aSLuiz Capitulino { 3851d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 385258e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 385328a7282aSLuiz Capitulino } 385428a7282aSLuiz Capitulino 385528a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 385628a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 385728a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 385828a7282aSLuiz Capitulino { 3859d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 386028a7282aSLuiz Capitulino (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC || 386128a7282aSLuiz Capitulino bs->on_write_error == BLOCK_ERR_STOP_ANY || 386228a7282aSLuiz Capitulino bs->on_read_error == BLOCK_ERR_STOP_ANY)); 386328a7282aSLuiz Capitulino } 386428a7282aSLuiz Capitulino 386528a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 386628a7282aSLuiz Capitulino { 3867d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 386828a7282aSLuiz Capitulino } 386928a7282aSLuiz Capitulino 387028a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 387128a7282aSLuiz Capitulino { 387228a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 387358e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 387428a7282aSLuiz Capitulino } 387528a7282aSLuiz Capitulino } 387628a7282aSLuiz Capitulino 387728a7282aSLuiz Capitulino /* XXX: Today this is set by device models because it makes the implementation 387828a7282aSLuiz Capitulino quite simple. However, the block layer knows about the error, so it's 387928a7282aSLuiz Capitulino possible to implement this without device models being involved */ 388028a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 388128a7282aSLuiz Capitulino { 388258e21ef5SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs) && 388358e21ef5SLuiz Capitulino bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 388428a7282aSLuiz Capitulino assert(error >= 0); 388558e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 388658e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 388728a7282aSLuiz Capitulino } 388828a7282aSLuiz Capitulino } 388928a7282aSLuiz Capitulino 3890a597e79cSChristoph Hellwig void 3891a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 3892a597e79cSChristoph Hellwig enum BlockAcctType type) 3893a597e79cSChristoph Hellwig { 3894a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 3895a597e79cSChristoph Hellwig 3896a597e79cSChristoph Hellwig cookie->bytes = bytes; 3897c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 3898a597e79cSChristoph Hellwig cookie->type = type; 3899a597e79cSChristoph Hellwig } 3900a597e79cSChristoph Hellwig 3901a597e79cSChristoph Hellwig void 3902a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 3903a597e79cSChristoph Hellwig { 3904a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 3905a597e79cSChristoph Hellwig 3906a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 3907a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 3908c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 3909a597e79cSChristoph Hellwig } 3910a597e79cSChristoph Hellwig 3911f88e1a42SJes Sorensen int bdrv_img_create(const char *filename, const char *fmt, 3912f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 3913f88e1a42SJes Sorensen char *options, uint64_t img_size, int flags) 3914f88e1a42SJes Sorensen { 3915f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 3916d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 3917f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 3918f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 391996df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 3920f88e1a42SJes Sorensen int ret = 0; 3921f88e1a42SJes Sorensen 3922f88e1a42SJes Sorensen /* Find driver and parse its options */ 3923f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 3924f88e1a42SJes Sorensen if (!drv) { 3925f88e1a42SJes Sorensen error_report("Unknown file format '%s'", fmt); 39264f70f249SJes Sorensen ret = -EINVAL; 3927f88e1a42SJes Sorensen goto out; 3928f88e1a42SJes Sorensen } 3929f88e1a42SJes Sorensen 3930f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 3931f88e1a42SJes Sorensen if (!proto_drv) { 3932f88e1a42SJes Sorensen error_report("Unknown protocol '%s'", filename); 39334f70f249SJes Sorensen ret = -EINVAL; 3934f88e1a42SJes Sorensen goto out; 3935f88e1a42SJes Sorensen } 3936f88e1a42SJes Sorensen 3937f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 3938f88e1a42SJes Sorensen drv->create_options); 3939f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 3940f88e1a42SJes Sorensen proto_drv->create_options); 3941f88e1a42SJes Sorensen 3942f88e1a42SJes Sorensen /* Create parameter list with default values */ 3943f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 3944f88e1a42SJes Sorensen 3945f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 3946f88e1a42SJes Sorensen 3947f88e1a42SJes Sorensen /* Parse -o options */ 3948f88e1a42SJes Sorensen if (options) { 3949f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 3950f88e1a42SJes Sorensen if (param == NULL) { 3951f88e1a42SJes Sorensen error_report("Invalid options for file format '%s'.", fmt); 39524f70f249SJes Sorensen ret = -EINVAL; 3953f88e1a42SJes Sorensen goto out; 3954f88e1a42SJes Sorensen } 3955f88e1a42SJes Sorensen } 3956f88e1a42SJes Sorensen 3957f88e1a42SJes Sorensen if (base_filename) { 3958f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 3959f88e1a42SJes Sorensen base_filename)) { 3960f88e1a42SJes Sorensen error_report("Backing file not supported for file format '%s'", 3961f88e1a42SJes Sorensen fmt); 39624f70f249SJes Sorensen ret = -EINVAL; 3963f88e1a42SJes Sorensen goto out; 3964f88e1a42SJes Sorensen } 3965f88e1a42SJes Sorensen } 3966f88e1a42SJes Sorensen 3967f88e1a42SJes Sorensen if (base_fmt) { 3968f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 3969f88e1a42SJes Sorensen error_report("Backing file format not supported for file " 3970f88e1a42SJes Sorensen "format '%s'", fmt); 39714f70f249SJes Sorensen ret = -EINVAL; 3972f88e1a42SJes Sorensen goto out; 3973f88e1a42SJes Sorensen } 3974f88e1a42SJes Sorensen } 3975f88e1a42SJes Sorensen 3976792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 3977792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 3978792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 3979792da93aSJes Sorensen error_report("Error: Trying to create an image with the " 3980792da93aSJes Sorensen "same filename as the backing file"); 39814f70f249SJes Sorensen ret = -EINVAL; 3982792da93aSJes Sorensen goto out; 3983792da93aSJes Sorensen } 3984792da93aSJes Sorensen } 3985792da93aSJes Sorensen 3986f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 3987f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 398896df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 398996df67d1SStefan Hajnoczi if (!backing_drv) { 3990f88e1a42SJes Sorensen error_report("Unknown backing file format '%s'", 3991f88e1a42SJes Sorensen backing_fmt->value.s); 39924f70f249SJes Sorensen ret = -EINVAL; 3993f88e1a42SJes Sorensen goto out; 3994f88e1a42SJes Sorensen } 3995f88e1a42SJes Sorensen } 3996f88e1a42SJes Sorensen 3997f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 3998f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 3999d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4000d220894eSKevin Wolf if (size && size->value.n == -1) { 4001f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4002f88e1a42SJes Sorensen uint64_t size; 4003f88e1a42SJes Sorensen char buf[32]; 400463090dacSPaolo Bonzini int back_flags; 400563090dacSPaolo Bonzini 400663090dacSPaolo Bonzini /* backing files always opened read-only */ 400763090dacSPaolo Bonzini back_flags = 400863090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 4009f88e1a42SJes Sorensen 4010f88e1a42SJes Sorensen bs = bdrv_new(""); 4011f88e1a42SJes Sorensen 401263090dacSPaolo Bonzini ret = bdrv_open(bs, backing_file->value.s, back_flags, backing_drv); 4013f88e1a42SJes Sorensen if (ret < 0) { 401496df67d1SStefan Hajnoczi error_report("Could not open '%s'", backing_file->value.s); 4015f88e1a42SJes Sorensen goto out; 4016f88e1a42SJes Sorensen } 4017f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4018f88e1a42SJes Sorensen size *= 512; 4019f88e1a42SJes Sorensen 4020f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4021f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4022f88e1a42SJes Sorensen } else { 4023f88e1a42SJes Sorensen error_report("Image creation needs a size parameter"); 40244f70f249SJes Sorensen ret = -EINVAL; 4025f88e1a42SJes Sorensen goto out; 4026f88e1a42SJes Sorensen } 4027f88e1a42SJes Sorensen } 4028f88e1a42SJes Sorensen 4029f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4030f88e1a42SJes Sorensen print_option_parameters(param); 4031f88e1a42SJes Sorensen puts(""); 4032f88e1a42SJes Sorensen 4033f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 4034f88e1a42SJes Sorensen 4035f88e1a42SJes Sorensen if (ret < 0) { 4036f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 4037f88e1a42SJes Sorensen error_report("Formatting or formatting option not supported for " 4038f88e1a42SJes Sorensen "file format '%s'", fmt); 4039f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 4040f88e1a42SJes Sorensen error_report("The image size is too large for file format '%s'", 4041f88e1a42SJes Sorensen fmt); 4042f88e1a42SJes Sorensen } else { 4043f88e1a42SJes Sorensen error_report("%s: error while creating %s: %s", filename, fmt, 4044f88e1a42SJes Sorensen strerror(-ret)); 4045f88e1a42SJes Sorensen } 4046f88e1a42SJes Sorensen } 4047f88e1a42SJes Sorensen 4048f88e1a42SJes Sorensen out: 4049f88e1a42SJes Sorensen free_option_parameters(create_options); 4050f88e1a42SJes Sorensen free_option_parameters(param); 4051f88e1a42SJes Sorensen 4052f88e1a42SJes Sorensen if (bs) { 4053f88e1a42SJes Sorensen bdrv_delete(bs); 4054f88e1a42SJes Sorensen } 40554f70f249SJes Sorensen 40564f70f249SJes Sorensen return ret; 4057f88e1a42SJes Sorensen } 4058eeec61f2SStefan Hajnoczi 4059eeec61f2SStefan Hajnoczi void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs, 4060c83c66c3SStefan Hajnoczi int64_t speed, BlockDriverCompletionFunc *cb, 4061c83c66c3SStefan Hajnoczi void *opaque, Error **errp) 4062eeec61f2SStefan Hajnoczi { 4063eeec61f2SStefan Hajnoczi BlockJob *job; 4064eeec61f2SStefan Hajnoczi 4065eeec61f2SStefan Hajnoczi if (bs->job || bdrv_in_use(bs)) { 4066fd7f8c65SStefan Hajnoczi error_set(errp, QERR_DEVICE_IN_USE, bdrv_get_device_name(bs)); 4067eeec61f2SStefan Hajnoczi return NULL; 4068eeec61f2SStefan Hajnoczi } 4069eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 1); 4070eeec61f2SStefan Hajnoczi 4071eeec61f2SStefan Hajnoczi job = g_malloc0(job_type->instance_size); 4072eeec61f2SStefan Hajnoczi job->job_type = job_type; 4073eeec61f2SStefan Hajnoczi job->bs = bs; 4074eeec61f2SStefan Hajnoczi job->cb = cb; 4075eeec61f2SStefan Hajnoczi job->opaque = opaque; 40764513eafeSPaolo Bonzini job->busy = true; 4077eeec61f2SStefan Hajnoczi bs->job = job; 4078c83c66c3SStefan Hajnoczi 4079c83c66c3SStefan Hajnoczi /* Only set speed when necessary to avoid NotSupported error */ 4080c83c66c3SStefan Hajnoczi if (speed != 0) { 4081c83c66c3SStefan Hajnoczi Error *local_err = NULL; 4082c83c66c3SStefan Hajnoczi 4083c83c66c3SStefan Hajnoczi block_job_set_speed(job, speed, &local_err); 4084c83c66c3SStefan Hajnoczi if (error_is_set(&local_err)) { 4085c83c66c3SStefan Hajnoczi bs->job = NULL; 4086c83c66c3SStefan Hajnoczi g_free(job); 4087c83c66c3SStefan Hajnoczi bdrv_set_in_use(bs, 0); 4088c83c66c3SStefan Hajnoczi error_propagate(errp, local_err); 4089c83c66c3SStefan Hajnoczi return NULL; 4090c83c66c3SStefan Hajnoczi } 4091c83c66c3SStefan Hajnoczi } 4092eeec61f2SStefan Hajnoczi return job; 4093eeec61f2SStefan Hajnoczi } 4094eeec61f2SStefan Hajnoczi 4095eeec61f2SStefan Hajnoczi void block_job_complete(BlockJob *job, int ret) 4096eeec61f2SStefan Hajnoczi { 4097eeec61f2SStefan Hajnoczi BlockDriverState *bs = job->bs; 4098eeec61f2SStefan Hajnoczi 4099eeec61f2SStefan Hajnoczi assert(bs->job == job); 4100eeec61f2SStefan Hajnoczi job->cb(job->opaque, ret); 4101eeec61f2SStefan Hajnoczi bs->job = NULL; 4102eeec61f2SStefan Hajnoczi g_free(job); 4103eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 0); 4104eeec61f2SStefan Hajnoczi } 4105eeec61f2SStefan Hajnoczi 4106882ec7ceSStefan Hajnoczi void block_job_set_speed(BlockJob *job, int64_t speed, Error **errp) 4107eeec61f2SStefan Hajnoczi { 41089e6636c7SStefan Hajnoczi Error *local_err = NULL; 41099f25ecccSPaolo Bonzini 4110eeec61f2SStefan Hajnoczi if (!job->job_type->set_speed) { 41119e6636c7SStefan Hajnoczi error_set(errp, QERR_NOT_SUPPORTED); 41129e6636c7SStefan Hajnoczi return; 4113eeec61f2SStefan Hajnoczi } 4114882ec7ceSStefan Hajnoczi job->job_type->set_speed(job, speed, &local_err); 41159e6636c7SStefan Hajnoczi if (error_is_set(&local_err)) { 41169e6636c7SStefan Hajnoczi error_propagate(errp, local_err); 41179e6636c7SStefan Hajnoczi return; 41189e6636c7SStefan Hajnoczi } 41199e6636c7SStefan Hajnoczi 4120882ec7ceSStefan Hajnoczi job->speed = speed; 41219f25ecccSPaolo Bonzini } 4122eeec61f2SStefan Hajnoczi 4123eeec61f2SStefan Hajnoczi void block_job_cancel(BlockJob *job) 4124eeec61f2SStefan Hajnoczi { 4125eeec61f2SStefan Hajnoczi job->cancelled = true; 4126fa4478d5SPaolo Bonzini if (job->co && !job->busy) { 4127fa4478d5SPaolo Bonzini qemu_coroutine_enter(job->co, NULL); 4128fa4478d5SPaolo Bonzini } 4129eeec61f2SStefan Hajnoczi } 4130eeec61f2SStefan Hajnoczi 4131eeec61f2SStefan Hajnoczi bool block_job_is_cancelled(BlockJob *job) 4132eeec61f2SStefan Hajnoczi { 4133eeec61f2SStefan Hajnoczi return job->cancelled; 4134eeec61f2SStefan Hajnoczi } 41353e914655SPaolo Bonzini 4136fa4478d5SPaolo Bonzini struct BlockCancelData { 4137fa4478d5SPaolo Bonzini BlockJob *job; 4138fa4478d5SPaolo Bonzini BlockDriverCompletionFunc *cb; 4139fa4478d5SPaolo Bonzini void *opaque; 4140fa4478d5SPaolo Bonzini bool cancelled; 4141fa4478d5SPaolo Bonzini int ret; 4142fa4478d5SPaolo Bonzini }; 4143fa4478d5SPaolo Bonzini 4144fa4478d5SPaolo Bonzini static void block_job_cancel_cb(void *opaque, int ret) 41453e914655SPaolo Bonzini { 4146fa4478d5SPaolo Bonzini struct BlockCancelData *data = opaque; 4147fa4478d5SPaolo Bonzini 4148fa4478d5SPaolo Bonzini data->cancelled = block_job_is_cancelled(data->job); 4149fa4478d5SPaolo Bonzini data->ret = ret; 4150fa4478d5SPaolo Bonzini data->cb(data->opaque, ret); 4151fa4478d5SPaolo Bonzini } 4152fa4478d5SPaolo Bonzini 4153fa4478d5SPaolo Bonzini int block_job_cancel_sync(BlockJob *job) 4154fa4478d5SPaolo Bonzini { 4155fa4478d5SPaolo Bonzini struct BlockCancelData data; 41563e914655SPaolo Bonzini BlockDriverState *bs = job->bs; 41573e914655SPaolo Bonzini 41583e914655SPaolo Bonzini assert(bs->job == job); 4159fa4478d5SPaolo Bonzini 4160fa4478d5SPaolo Bonzini /* Set up our own callback to store the result and chain to 4161fa4478d5SPaolo Bonzini * the original callback. 4162fa4478d5SPaolo Bonzini */ 4163fa4478d5SPaolo Bonzini data.job = job; 4164fa4478d5SPaolo Bonzini data.cb = job->cb; 4165fa4478d5SPaolo Bonzini data.opaque = job->opaque; 4166fa4478d5SPaolo Bonzini data.ret = -EINPROGRESS; 4167fa4478d5SPaolo Bonzini job->cb = block_job_cancel_cb; 4168fa4478d5SPaolo Bonzini job->opaque = &data; 41693e914655SPaolo Bonzini block_job_cancel(job); 4170fa4478d5SPaolo Bonzini while (data.ret == -EINPROGRESS) { 41713e914655SPaolo Bonzini qemu_aio_wait(); 41723e914655SPaolo Bonzini } 4173fa4478d5SPaolo Bonzini return (data.cancelled && data.ret == 0) ? -ECANCELED : data.ret; 41743e914655SPaolo Bonzini } 41754513eafeSPaolo Bonzini 41764513eafeSPaolo Bonzini void block_job_sleep_ns(BlockJob *job, QEMUClock *clock, int64_t ns) 41774513eafeSPaolo Bonzini { 41784513eafeSPaolo Bonzini /* Check cancellation *before* setting busy = false, too! */ 41794513eafeSPaolo Bonzini if (!block_job_is_cancelled(job)) { 41804513eafeSPaolo Bonzini job->busy = false; 41814513eafeSPaolo Bonzini co_sleep_ns(clock, ns); 41824513eafeSPaolo Bonzini job->busy = true; 41834513eafeSPaolo Bonzini } 41844513eafeSPaolo Bonzini } 4185