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); 83ec530c81Sbellard 8498f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 8598f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait); 8698f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 8798f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait); 8898f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 8998f90dbaSZhi Yong Wu bool is_write, int64_t *wait); 9098f90dbaSZhi Yong Wu 911b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 921b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 937ee930d0Sblueswir1 948a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 958a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 96ea2384d3Sbellard 97f9092b10SMarkus Armbruster /* The device to use for VM snapshots */ 98f9092b10SMarkus Armbruster static BlockDriverState *bs_snapshots; 99f9092b10SMarkus Armbruster 100eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 101eb852011SMarkus Armbruster static int use_bdrv_whitelist; 102eb852011SMarkus Armbruster 1039e0b22f4SStefan Hajnoczi #ifdef _WIN32 1049e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 1059e0b22f4SStefan Hajnoczi { 1069e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 1079e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 1089e0b22f4SStefan Hajnoczi filename[1] == ':'); 1099e0b22f4SStefan Hajnoczi } 1109e0b22f4SStefan Hajnoczi 1119e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 1129e0b22f4SStefan Hajnoczi { 1139e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 1149e0b22f4SStefan Hajnoczi filename[2] == '\0') 1159e0b22f4SStefan Hajnoczi return 1; 1169e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1179e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1189e0b22f4SStefan Hajnoczi return 1; 1199e0b22f4SStefan Hajnoczi return 0; 1209e0b22f4SStefan Hajnoczi } 1219e0b22f4SStefan Hajnoczi #endif 1229e0b22f4SStefan Hajnoczi 1230563e191SZhi Yong Wu /* throttling disk I/O limits */ 12498f90dbaSZhi Yong Wu void bdrv_io_limits_disable(BlockDriverState *bs) 12598f90dbaSZhi Yong Wu { 12698f90dbaSZhi Yong Wu bs->io_limits_enabled = false; 12798f90dbaSZhi Yong Wu 12898f90dbaSZhi Yong Wu while (qemu_co_queue_next(&bs->throttled_reqs)); 12998f90dbaSZhi Yong Wu 13098f90dbaSZhi Yong Wu if (bs->block_timer) { 13198f90dbaSZhi Yong Wu qemu_del_timer(bs->block_timer); 13298f90dbaSZhi Yong Wu qemu_free_timer(bs->block_timer); 13398f90dbaSZhi Yong Wu bs->block_timer = NULL; 13498f90dbaSZhi Yong Wu } 13598f90dbaSZhi Yong Wu 13698f90dbaSZhi Yong Wu bs->slice_start = 0; 13798f90dbaSZhi Yong Wu bs->slice_end = 0; 13898f90dbaSZhi Yong Wu bs->slice_time = 0; 13998f90dbaSZhi Yong Wu memset(&bs->io_base, 0, sizeof(bs->io_base)); 14098f90dbaSZhi Yong Wu } 14198f90dbaSZhi Yong Wu 1420563e191SZhi Yong Wu static void bdrv_block_timer(void *opaque) 1430563e191SZhi Yong Wu { 1440563e191SZhi Yong Wu BlockDriverState *bs = opaque; 1450563e191SZhi Yong Wu 1460563e191SZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 1470563e191SZhi Yong Wu } 1480563e191SZhi Yong Wu 1490563e191SZhi Yong Wu void bdrv_io_limits_enable(BlockDriverState *bs) 1500563e191SZhi Yong Wu { 1510563e191SZhi Yong Wu qemu_co_queue_init(&bs->throttled_reqs); 1520563e191SZhi Yong Wu bs->block_timer = qemu_new_timer_ns(vm_clock, bdrv_block_timer, bs); 1530563e191SZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 1540563e191SZhi Yong Wu bs->slice_start = qemu_get_clock_ns(vm_clock); 1550563e191SZhi Yong Wu bs->slice_end = bs->slice_start + bs->slice_time; 1560563e191SZhi Yong Wu memset(&bs->io_base, 0, sizeof(bs->io_base)); 1570563e191SZhi Yong Wu bs->io_limits_enabled = true; 1580563e191SZhi Yong Wu } 1590563e191SZhi Yong Wu 1600563e191SZhi Yong Wu bool bdrv_io_limits_enabled(BlockDriverState *bs) 1610563e191SZhi Yong Wu { 1620563e191SZhi Yong Wu BlockIOLimit *io_limits = &bs->io_limits; 1630563e191SZhi Yong Wu return io_limits->bps[BLOCK_IO_LIMIT_READ] 1640563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_WRITE] 1650563e191SZhi Yong Wu || io_limits->bps[BLOCK_IO_LIMIT_TOTAL] 1660563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_READ] 1670563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_WRITE] 1680563e191SZhi Yong Wu || io_limits->iops[BLOCK_IO_LIMIT_TOTAL]; 1690563e191SZhi Yong Wu } 1700563e191SZhi Yong Wu 17198f90dbaSZhi Yong Wu static void bdrv_io_limits_intercept(BlockDriverState *bs, 17298f90dbaSZhi Yong Wu bool is_write, int nb_sectors) 17398f90dbaSZhi Yong Wu { 17498f90dbaSZhi Yong Wu int64_t wait_time = -1; 17598f90dbaSZhi Yong Wu 17698f90dbaSZhi Yong Wu if (!qemu_co_queue_empty(&bs->throttled_reqs)) { 17798f90dbaSZhi Yong Wu qemu_co_queue_wait(&bs->throttled_reqs); 17898f90dbaSZhi Yong Wu } 17998f90dbaSZhi Yong Wu 18098f90dbaSZhi Yong Wu /* In fact, we hope to keep each request's timing, in FIFO mode. The next 18198f90dbaSZhi Yong Wu * throttled requests will not be dequeued until the current request is 18298f90dbaSZhi Yong Wu * allowed to be serviced. So if the current request still exceeds the 18398f90dbaSZhi Yong Wu * limits, it will be inserted to the head. All requests followed it will 18498f90dbaSZhi Yong Wu * be still in throttled_reqs queue. 18598f90dbaSZhi Yong Wu */ 18698f90dbaSZhi Yong Wu 18798f90dbaSZhi Yong Wu while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) { 18898f90dbaSZhi Yong Wu qemu_mod_timer(bs->block_timer, 18998f90dbaSZhi Yong Wu wait_time + qemu_get_clock_ns(vm_clock)); 19098f90dbaSZhi Yong Wu qemu_co_queue_wait_insert_head(&bs->throttled_reqs); 19198f90dbaSZhi Yong Wu } 19298f90dbaSZhi Yong Wu 19398f90dbaSZhi Yong Wu qemu_co_queue_next(&bs->throttled_reqs); 19498f90dbaSZhi Yong Wu } 19598f90dbaSZhi Yong Wu 1969e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 1979e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 1989e0b22f4SStefan Hajnoczi { 1999e0b22f4SStefan Hajnoczi #ifdef _WIN32 2009e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2019e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2029e0b22f4SStefan Hajnoczi return 0; 2039e0b22f4SStefan Hajnoczi } 2049e0b22f4SStefan Hajnoczi #endif 2059e0b22f4SStefan Hajnoczi 2069e0b22f4SStefan Hajnoczi return strchr(path, ':') != NULL; 2079e0b22f4SStefan Hajnoczi } 2089e0b22f4SStefan Hajnoczi 20983f64091Sbellard int path_is_absolute(const char *path) 21083f64091Sbellard { 21183f64091Sbellard const char *p; 21221664424Sbellard #ifdef _WIN32 21321664424Sbellard /* specific case for names like: "\\.\d:" */ 21421664424Sbellard if (*path == '/' || *path == '\\') 21521664424Sbellard return 1; 21621664424Sbellard #endif 21783f64091Sbellard p = strchr(path, ':'); 21883f64091Sbellard if (p) 21983f64091Sbellard p++; 22083f64091Sbellard else 22183f64091Sbellard p = path; 2223b9f94e1Sbellard #ifdef _WIN32 2233b9f94e1Sbellard return (*p == '/' || *p == '\\'); 2243b9f94e1Sbellard #else 2253b9f94e1Sbellard return (*p == '/'); 2263b9f94e1Sbellard #endif 22783f64091Sbellard } 22883f64091Sbellard 22983f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 23083f64091Sbellard path to it by considering it is relative to base_path. URL are 23183f64091Sbellard supported. */ 23283f64091Sbellard void path_combine(char *dest, int dest_size, 23383f64091Sbellard const char *base_path, 23483f64091Sbellard const char *filename) 23583f64091Sbellard { 23683f64091Sbellard const char *p, *p1; 23783f64091Sbellard int len; 23883f64091Sbellard 23983f64091Sbellard if (dest_size <= 0) 24083f64091Sbellard return; 24183f64091Sbellard if (path_is_absolute(filename)) { 24283f64091Sbellard pstrcpy(dest, dest_size, filename); 24383f64091Sbellard } else { 24483f64091Sbellard p = strchr(base_path, ':'); 24583f64091Sbellard if (p) 24683f64091Sbellard p++; 24783f64091Sbellard else 24883f64091Sbellard p = base_path; 2493b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2503b9f94e1Sbellard #ifdef _WIN32 2513b9f94e1Sbellard { 2523b9f94e1Sbellard const char *p2; 2533b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2543b9f94e1Sbellard if (!p1 || p2 > p1) 2553b9f94e1Sbellard p1 = p2; 2563b9f94e1Sbellard } 2573b9f94e1Sbellard #endif 25883f64091Sbellard if (p1) 25983f64091Sbellard p1++; 26083f64091Sbellard else 26183f64091Sbellard p1 = base_path; 26283f64091Sbellard if (p1 > p) 26383f64091Sbellard p = p1; 26483f64091Sbellard len = p - base_path; 26583f64091Sbellard if (len > dest_size - 1) 26683f64091Sbellard len = dest_size - 1; 26783f64091Sbellard memcpy(dest, base_path, len); 26883f64091Sbellard dest[len] = '\0'; 26983f64091Sbellard pstrcat(dest, dest_size, filename); 27083f64091Sbellard } 27183f64091Sbellard } 27283f64091Sbellard 2735efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 274ea2384d3Sbellard { 2758c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 2768c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 277f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 278f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 279f9f05dc5SKevin Wolf 280f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 281f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 282f8c35c1dSStefan Hajnoczi */ 283f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 28483f64091Sbellard /* add AIO emulation layer */ 285f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 286f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 28783f64091Sbellard } 288f9f05dc5SKevin Wolf } 289b2e12bc6SChristoph Hellwig 2908a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 291ea2384d3Sbellard } 292b338082bSbellard 293b338082bSbellard /* create a new block device (by default it is empty) */ 294b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 295fc01f7e7Sbellard { 2961b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 297b338082bSbellard 2987267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 299b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 300ea2384d3Sbellard if (device_name[0] != '\0') { 3011b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 302ea2384d3Sbellard } 30328a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 304b338082bSbellard return bs; 305b338082bSbellard } 306b338082bSbellard 307ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 308ea2384d3Sbellard { 309ea2384d3Sbellard BlockDriver *drv1; 3108a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3118a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 312ea2384d3Sbellard return drv1; 313ea2384d3Sbellard } 3148a22f02aSStefan Hajnoczi } 315ea2384d3Sbellard return NULL; 316ea2384d3Sbellard } 317ea2384d3Sbellard 318eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 319eb852011SMarkus Armbruster { 320eb852011SMarkus Armbruster static const char *whitelist[] = { 321eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 322eb852011SMarkus Armbruster }; 323eb852011SMarkus Armbruster const char **p; 324eb852011SMarkus Armbruster 325eb852011SMarkus Armbruster if (!whitelist[0]) 326eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 327eb852011SMarkus Armbruster 328eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 329eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 330eb852011SMarkus Armbruster return 1; 331eb852011SMarkus Armbruster } 332eb852011SMarkus Armbruster } 333eb852011SMarkus Armbruster return 0; 334eb852011SMarkus Armbruster } 335eb852011SMarkus Armbruster 336eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 337eb852011SMarkus Armbruster { 338eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 339eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 340eb852011SMarkus Armbruster } 341eb852011SMarkus Armbruster 3420e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 3430e7e1989SKevin Wolf QEMUOptionParameter *options) 344ea2384d3Sbellard { 345ea2384d3Sbellard if (!drv->bdrv_create) 346ea2384d3Sbellard return -ENOTSUP; 3470e7e1989SKevin Wolf 3480e7e1989SKevin Wolf return drv->bdrv_create(filename, options); 349ea2384d3Sbellard } 350ea2384d3Sbellard 35184a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 35284a12e66SChristoph Hellwig { 35384a12e66SChristoph Hellwig BlockDriver *drv; 35484a12e66SChristoph Hellwig 355b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 35684a12e66SChristoph Hellwig if (drv == NULL) { 35716905d71SStefan Hajnoczi return -ENOENT; 35884a12e66SChristoph Hellwig } 35984a12e66SChristoph Hellwig 36084a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 36184a12e66SChristoph Hellwig } 36284a12e66SChristoph Hellwig 363d5249393Sbellard #ifdef _WIN32 36495389c86Sbellard void get_tmp_filename(char *filename, int size) 365d5249393Sbellard { 3663b9f94e1Sbellard char temp_dir[MAX_PATH]; 3673b9f94e1Sbellard 3683b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 3693b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 370d5249393Sbellard } 371d5249393Sbellard #else 37295389c86Sbellard void get_tmp_filename(char *filename, int size) 373ea2384d3Sbellard { 374ea2384d3Sbellard int fd; 3757ccfb2ebSblueswir1 const char *tmpdir; 376d5249393Sbellard /* XXX: race condition possible */ 3770badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 3780badc1eeSaurel32 if (!tmpdir) 3790badc1eeSaurel32 tmpdir = "/tmp"; 3800badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 381ea2384d3Sbellard fd = mkstemp(filename); 382ea2384d3Sbellard close(fd); 383ea2384d3Sbellard } 384d5249393Sbellard #endif 385ea2384d3Sbellard 386f3a5d3f8SChristoph Hellwig /* 387f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 388f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 389f3a5d3f8SChristoph Hellwig */ 390f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 391f3a5d3f8SChristoph Hellwig { 392508c7cb3SChristoph Hellwig int score_max = 0, score; 393508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 394f3a5d3f8SChristoph Hellwig 3958a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 396508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 397508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 398508c7cb3SChristoph Hellwig if (score > score_max) { 399508c7cb3SChristoph Hellwig score_max = score; 400508c7cb3SChristoph Hellwig drv = d; 401f3a5d3f8SChristoph Hellwig } 402508c7cb3SChristoph Hellwig } 403f3a5d3f8SChristoph Hellwig } 404f3a5d3f8SChristoph Hellwig 405508c7cb3SChristoph Hellwig return drv; 406f3a5d3f8SChristoph Hellwig } 407f3a5d3f8SChristoph Hellwig 408b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 40984a12e66SChristoph Hellwig { 41084a12e66SChristoph Hellwig BlockDriver *drv1; 41184a12e66SChristoph Hellwig char protocol[128]; 41284a12e66SChristoph Hellwig int len; 41384a12e66SChristoph Hellwig const char *p; 41484a12e66SChristoph Hellwig 41566f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 41666f82ceeSKevin Wolf 41739508e7aSChristoph Hellwig /* 41839508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 41939508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 42039508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 42139508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 42239508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 42339508e7aSChristoph Hellwig */ 42484a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 42539508e7aSChristoph Hellwig if (drv1) { 42684a12e66SChristoph Hellwig return drv1; 42784a12e66SChristoph Hellwig } 42839508e7aSChristoph Hellwig 4299e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 43039508e7aSChristoph Hellwig return bdrv_find_format("file"); 43139508e7aSChristoph Hellwig } 4329e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 4339e0b22f4SStefan Hajnoczi assert(p != NULL); 43484a12e66SChristoph Hellwig len = p - filename; 43584a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 43684a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 43784a12e66SChristoph Hellwig memcpy(protocol, filename, len); 43884a12e66SChristoph Hellwig protocol[len] = '\0'; 43984a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 44084a12e66SChristoph Hellwig if (drv1->protocol_name && 44184a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 44284a12e66SChristoph Hellwig return drv1; 44384a12e66SChristoph Hellwig } 44484a12e66SChristoph Hellwig } 44584a12e66SChristoph Hellwig return NULL; 44684a12e66SChristoph Hellwig } 44784a12e66SChristoph Hellwig 448c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv) 449ea2384d3Sbellard { 45083f64091Sbellard int ret, score, score_max; 451ea2384d3Sbellard BlockDriver *drv1, *drv; 45283f64091Sbellard uint8_t buf[2048]; 45383f64091Sbellard BlockDriverState *bs; 454ea2384d3Sbellard 455f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 456c98ac35dSStefan Weil if (ret < 0) { 457c98ac35dSStefan Weil *pdrv = NULL; 458c98ac35dSStefan Weil return ret; 459c98ac35dSStefan Weil } 460f8ea0b00SNicholas Bellinger 46108a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 46208a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 4631a396859SNicholas A. Bellinger bdrv_delete(bs); 464c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 465c98ac35dSStefan Weil if (!drv) { 466c98ac35dSStefan Weil ret = -ENOENT; 467c98ac35dSStefan Weil } 468c98ac35dSStefan Weil *pdrv = drv; 469c98ac35dSStefan Weil return ret; 4701a396859SNicholas A. Bellinger } 471f8ea0b00SNicholas Bellinger 47283f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 47383f64091Sbellard bdrv_delete(bs); 474ea2384d3Sbellard if (ret < 0) { 475c98ac35dSStefan Weil *pdrv = NULL; 476c98ac35dSStefan Weil return ret; 477ea2384d3Sbellard } 478ea2384d3Sbellard 479ea2384d3Sbellard score_max = 0; 48084a12e66SChristoph Hellwig drv = NULL; 4818a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 48283f64091Sbellard if (drv1->bdrv_probe) { 483ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 484ea2384d3Sbellard if (score > score_max) { 485ea2384d3Sbellard score_max = score; 486ea2384d3Sbellard drv = drv1; 487ea2384d3Sbellard } 488ea2384d3Sbellard } 48983f64091Sbellard } 490c98ac35dSStefan Weil if (!drv) { 491c98ac35dSStefan Weil ret = -ENOENT; 492c98ac35dSStefan Weil } 493c98ac35dSStefan Weil *pdrv = drv; 494c98ac35dSStefan Weil return ret; 495ea2384d3Sbellard } 496ea2384d3Sbellard 49751762288SStefan Hajnoczi /** 49851762288SStefan Hajnoczi * Set the current 'total_sectors' value 49951762288SStefan Hajnoczi */ 50051762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 50151762288SStefan Hajnoczi { 50251762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 50351762288SStefan Hajnoczi 504396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 505396759adSNicholas Bellinger if (bs->sg) 506396759adSNicholas Bellinger return 0; 507396759adSNicholas Bellinger 50851762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 50951762288SStefan Hajnoczi if (drv->bdrv_getlength) { 51051762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 51151762288SStefan Hajnoczi if (length < 0) { 51251762288SStefan Hajnoczi return length; 51351762288SStefan Hajnoczi } 51451762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 51551762288SStefan Hajnoczi } 51651762288SStefan Hajnoczi 51751762288SStefan Hajnoczi bs->total_sectors = hint; 51851762288SStefan Hajnoczi return 0; 51951762288SStefan Hajnoczi } 52051762288SStefan Hajnoczi 521c3993cdcSStefan Hajnoczi /** 522c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 523c3993cdcSStefan Hajnoczi * 524c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 525c3993cdcSStefan Hajnoczi */ 526c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 527c3993cdcSStefan Hajnoczi { 528c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 529c3993cdcSStefan Hajnoczi 530c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 531c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 53292196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 53392196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 534c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 535c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 536c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 537c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 538c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 539c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 540c3993cdcSStefan Hajnoczi /* this is the default */ 541c3993cdcSStefan Hajnoczi } else { 542c3993cdcSStefan Hajnoczi return -1; 543c3993cdcSStefan Hajnoczi } 544c3993cdcSStefan Hajnoczi 545c3993cdcSStefan Hajnoczi return 0; 546c3993cdcSStefan Hajnoczi } 547c3993cdcSStefan Hajnoczi 54853fec9d3SStefan Hajnoczi /** 54953fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 55053fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 55153fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 55253fec9d3SStefan Hajnoczi */ 55353fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 55453fec9d3SStefan Hajnoczi { 55553fec9d3SStefan Hajnoczi bs->copy_on_read++; 55653fec9d3SStefan Hajnoczi } 55753fec9d3SStefan Hajnoczi 55853fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 55953fec9d3SStefan Hajnoczi { 56053fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 56153fec9d3SStefan Hajnoczi bs->copy_on_read--; 56253fec9d3SStefan Hajnoczi } 56353fec9d3SStefan Hajnoczi 564b6ce07aaSKevin Wolf /* 56557915332SKevin Wolf * Common part for opening disk images and files 56657915332SKevin Wolf */ 56757915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 56857915332SKevin Wolf int flags, BlockDriver *drv) 56957915332SKevin Wolf { 57057915332SKevin Wolf int ret, open_flags; 57157915332SKevin Wolf 57257915332SKevin Wolf assert(drv != NULL); 57357915332SKevin Wolf 57428dcee10SStefan Hajnoczi trace_bdrv_open_common(bs, filename, flags, drv->format_name); 57528dcee10SStefan Hajnoczi 57666f82ceeSKevin Wolf bs->file = NULL; 57751762288SStefan Hajnoczi bs->total_sectors = 0; 57857915332SKevin Wolf bs->encrypted = 0; 57957915332SKevin Wolf bs->valid_key = 0; 58003f541bdSStefan Hajnoczi bs->sg = 0; 58157915332SKevin Wolf bs->open_flags = flags; 58203f541bdSStefan Hajnoczi bs->growable = 0; 58357915332SKevin Wolf bs->buffer_alignment = 512; 58457915332SKevin Wolf 58553fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 58653fec9d3SStefan Hajnoczi if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) { 58753fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 58853fec9d3SStefan Hajnoczi } 58953fec9d3SStefan Hajnoczi 59057915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 59103f541bdSStefan Hajnoczi bs->backing_file[0] = '\0'; 59257915332SKevin Wolf 59357915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 59457915332SKevin Wolf return -ENOTSUP; 59557915332SKevin Wolf } 59657915332SKevin Wolf 59757915332SKevin Wolf bs->drv = drv; 5987267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 59957915332SKevin Wolf 60003f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 60157915332SKevin Wolf 60257915332SKevin Wolf /* 60357915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 60457915332SKevin Wolf * image. 60557915332SKevin Wolf */ 60657915332SKevin Wolf open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 60757915332SKevin Wolf 60857915332SKevin Wolf /* 609ebabb67aSStefan Weil * Snapshots should be writable. 61057915332SKevin Wolf */ 61157915332SKevin Wolf if (bs->is_temporary) { 61257915332SKevin Wolf open_flags |= BDRV_O_RDWR; 61357915332SKevin Wolf } 61457915332SKevin Wolf 615e7c63796SStefan Hajnoczi bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 616e7c63796SStefan Hajnoczi 61766f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 61866f82ceeSKevin Wolf if (drv->bdrv_file_open) { 61966f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 62066f82ceeSKevin Wolf } else { 62166f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 62266f82ceeSKevin Wolf if (ret >= 0) { 62366f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 62466f82ceeSKevin Wolf } 62566f82ceeSKevin Wolf } 62666f82ceeSKevin Wolf 62757915332SKevin Wolf if (ret < 0) { 62857915332SKevin Wolf goto free_and_fail; 62957915332SKevin Wolf } 63057915332SKevin Wolf 63151762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 63251762288SStefan Hajnoczi if (ret < 0) { 63351762288SStefan Hajnoczi goto free_and_fail; 63457915332SKevin Wolf } 63551762288SStefan Hajnoczi 63657915332SKevin Wolf #ifndef _WIN32 63757915332SKevin Wolf if (bs->is_temporary) { 63857915332SKevin Wolf unlink(filename); 63957915332SKevin Wolf } 64057915332SKevin Wolf #endif 64157915332SKevin Wolf return 0; 64257915332SKevin Wolf 64357915332SKevin Wolf free_and_fail: 64466f82ceeSKevin Wolf if (bs->file) { 64566f82ceeSKevin Wolf bdrv_delete(bs->file); 64666f82ceeSKevin Wolf bs->file = NULL; 64766f82ceeSKevin Wolf } 6487267c094SAnthony Liguori g_free(bs->opaque); 64957915332SKevin Wolf bs->opaque = NULL; 65057915332SKevin Wolf bs->drv = NULL; 65157915332SKevin Wolf return ret; 65257915332SKevin Wolf } 65357915332SKevin Wolf 65457915332SKevin Wolf /* 655b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 656b6ce07aaSKevin Wolf */ 65783f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 658b338082bSbellard { 65983f64091Sbellard BlockDriverState *bs; 6606db95603SChristoph Hellwig BlockDriver *drv; 66183f64091Sbellard int ret; 6623b0d4f61Sbellard 663b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 6646db95603SChristoph Hellwig if (!drv) { 6656db95603SChristoph Hellwig return -ENOENT; 6666db95603SChristoph Hellwig } 6676db95603SChristoph Hellwig 66883f64091Sbellard bs = bdrv_new(""); 669b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 67083f64091Sbellard if (ret < 0) { 67183f64091Sbellard bdrv_delete(bs); 67283f64091Sbellard return ret; 6733b0d4f61Sbellard } 67471d0770cSaliguori bs->growable = 1; 67583f64091Sbellard *pbs = bs; 67683f64091Sbellard return 0; 6773b0d4f61Sbellard } 6783b0d4f61Sbellard 679b6ce07aaSKevin Wolf /* 680b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 681b6ce07aaSKevin Wolf */ 682d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 683ea2384d3Sbellard BlockDriver *drv) 684ea2384d3Sbellard { 685b6ce07aaSKevin Wolf int ret; 6862b572816SKevin Wolf char tmp_filename[PATH_MAX]; 68733e3963eSbellard 68883f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 689ea2384d3Sbellard BlockDriverState *bs1; 690ea2384d3Sbellard int64_t total_size; 6917c96d46eSaliguori int is_protocol = 0; 69291a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 69391a073a9SKevin Wolf QEMUOptionParameter *options; 694b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 69533e3963eSbellard 696ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 697ea2384d3Sbellard instead of opening 'filename' directly */ 698ea2384d3Sbellard 699ea2384d3Sbellard /* if there is a backing file, use it */ 700ea2384d3Sbellard bs1 = bdrv_new(""); 701d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 70251d7c00cSaliguori if (ret < 0) { 703ea2384d3Sbellard bdrv_delete(bs1); 70451d7c00cSaliguori return ret; 705ea2384d3Sbellard } 7063e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 7077c96d46eSaliguori 7087c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 7097c96d46eSaliguori is_protocol = 1; 7107c96d46eSaliguori 711ea2384d3Sbellard bdrv_delete(bs1); 712ea2384d3Sbellard 713ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 7147c96d46eSaliguori 7157c96d46eSaliguori /* Real path is meaningless for protocols */ 7167c96d46eSaliguori if (is_protocol) 7177c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 7187c96d46eSaliguori "%s", filename); 719114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 720114cdfa9SKirill A. Shutemov return -errno; 7217c96d46eSaliguori 72291a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 72391a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 72491a073a9SKevin Wolf 7253e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 72691a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 72791a073a9SKevin Wolf if (drv) { 72891a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 72991a073a9SKevin Wolf drv->format_name); 73091a073a9SKevin Wolf } 73191a073a9SKevin Wolf 73291a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 733d748768cSJan Kiszka free_option_parameters(options); 73451d7c00cSaliguori if (ret < 0) { 73551d7c00cSaliguori return ret; 736ea2384d3Sbellard } 73791a073a9SKevin Wolf 738ea2384d3Sbellard filename = tmp_filename; 73991a073a9SKevin Wolf drv = bdrv_qcow2; 740ea2384d3Sbellard bs->is_temporary = 1; 741ea2384d3Sbellard } 742ea2384d3Sbellard 743b6ce07aaSKevin Wolf /* Find the right image format driver */ 7446db95603SChristoph Hellwig if (!drv) { 745c98ac35dSStefan Weil ret = find_image_format(filename, &drv); 746ea2384d3Sbellard } 7476987307cSChristoph Hellwig 74851d7c00cSaliguori if (!drv) { 74951d7c00cSaliguori goto unlink_and_fail; 75083f64091Sbellard } 751b6ce07aaSKevin Wolf 752b6ce07aaSKevin Wolf /* Open the image */ 753b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 754b6ce07aaSKevin Wolf if (ret < 0) { 7556987307cSChristoph Hellwig goto unlink_and_fail; 7566987307cSChristoph Hellwig } 7576987307cSChristoph Hellwig 758b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 759b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 760b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 761b6ce07aaSKevin Wolf int back_flags; 762b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 763b6ce07aaSKevin Wolf 764b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 765df2dbb4aSStefan Hajnoczi 766df2dbb4aSStefan Hajnoczi if (path_has_protocol(bs->backing_file)) { 767df2dbb4aSStefan Hajnoczi pstrcpy(backing_filename, sizeof(backing_filename), 768df2dbb4aSStefan Hajnoczi bs->backing_file); 769df2dbb4aSStefan Hajnoczi } else { 770b6ce07aaSKevin Wolf path_combine(backing_filename, sizeof(backing_filename), 771b6ce07aaSKevin Wolf filename, bs->backing_file); 772df2dbb4aSStefan Hajnoczi } 773df2dbb4aSStefan Hajnoczi 774df2dbb4aSStefan Hajnoczi if (bs->backing_format[0] != '\0') { 775b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 776df2dbb4aSStefan Hajnoczi } 777b6ce07aaSKevin Wolf 778b6ce07aaSKevin Wolf /* backing files always opened read-only */ 779b6ce07aaSKevin Wolf back_flags = 780b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 781b6ce07aaSKevin Wolf 782b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 783b6ce07aaSKevin Wolf if (ret < 0) { 784b6ce07aaSKevin Wolf bdrv_close(bs); 785b6ce07aaSKevin Wolf return ret; 786b6ce07aaSKevin Wolf } 787b6ce07aaSKevin Wolf if (bs->is_temporary) { 788b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 789b6ce07aaSKevin Wolf } else { 790b6ce07aaSKevin Wolf /* base image inherits from "parent" */ 791b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = bs->keep_read_only; 792b6ce07aaSKevin Wolf } 793b6ce07aaSKevin Wolf } 794b6ce07aaSKevin Wolf 795b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 7967d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 797b6ce07aaSKevin Wolf } 798b6ce07aaSKevin Wolf 79998f90dbaSZhi Yong Wu /* throttling disk I/O limits */ 80098f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 80198f90dbaSZhi Yong Wu bdrv_io_limits_enable(bs); 80298f90dbaSZhi Yong Wu } 80398f90dbaSZhi Yong Wu 804b6ce07aaSKevin Wolf return 0; 805b6ce07aaSKevin Wolf 806b6ce07aaSKevin Wolf unlink_and_fail: 807b6ce07aaSKevin Wolf if (bs->is_temporary) { 808b6ce07aaSKevin Wolf unlink(filename); 809b6ce07aaSKevin Wolf } 810b6ce07aaSKevin Wolf return ret; 811b6ce07aaSKevin Wolf } 812b6ce07aaSKevin Wolf 813fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 814fc01f7e7Sbellard { 81519cb3738Sbellard if (bs->drv) { 816f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 817f9092b10SMarkus Armbruster bs_snapshots = NULL; 818f9092b10SMarkus Armbruster } 819557df6acSStefan Hajnoczi if (bs->backing_hd) { 820ea2384d3Sbellard bdrv_delete(bs->backing_hd); 821557df6acSStefan Hajnoczi bs->backing_hd = NULL; 822557df6acSStefan Hajnoczi } 823ea2384d3Sbellard bs->drv->bdrv_close(bs); 8247267c094SAnthony Liguori g_free(bs->opaque); 825ea2384d3Sbellard #ifdef _WIN32 826ea2384d3Sbellard if (bs->is_temporary) { 827ea2384d3Sbellard unlink(bs->filename); 828ea2384d3Sbellard } 82967b915a5Sbellard #endif 830ea2384d3Sbellard bs->opaque = NULL; 831ea2384d3Sbellard bs->drv = NULL; 83253fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 833b338082bSbellard 83466f82ceeSKevin Wolf if (bs->file != NULL) { 83566f82ceeSKevin Wolf bdrv_close(bs->file); 83666f82ceeSKevin Wolf } 83766f82ceeSKevin Wolf 8387d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 839b338082bSbellard } 84098f90dbaSZhi Yong Wu 84198f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 84298f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 84398f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 84498f90dbaSZhi Yong Wu } 845b338082bSbellard } 846b338082bSbellard 8472bc93fedSMORITA Kazutaka void bdrv_close_all(void) 8482bc93fedSMORITA Kazutaka { 8492bc93fedSMORITA Kazutaka BlockDriverState *bs; 8502bc93fedSMORITA Kazutaka 8512bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 8522bc93fedSMORITA Kazutaka bdrv_close(bs); 8532bc93fedSMORITA Kazutaka } 8542bc93fedSMORITA Kazutaka } 8552bc93fedSMORITA Kazutaka 856922453bcSStefan Hajnoczi /* 857922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 858922453bcSStefan Hajnoczi * 859922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 860922453bcSStefan Hajnoczi * after calling this function. 861922453bcSStefan Hajnoczi */ 862922453bcSStefan Hajnoczi void bdrv_drain_all(void) 863922453bcSStefan Hajnoczi { 864922453bcSStefan Hajnoczi BlockDriverState *bs; 865922453bcSStefan Hajnoczi 866922453bcSStefan Hajnoczi qemu_aio_flush(); 867922453bcSStefan Hajnoczi 868922453bcSStefan Hajnoczi /* If requests are still pending there is a bug somewhere */ 869922453bcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 870922453bcSStefan Hajnoczi assert(QLIST_EMPTY(&bs->tracked_requests)); 871922453bcSStefan Hajnoczi assert(qemu_co_queue_empty(&bs->throttled_reqs)); 872922453bcSStefan Hajnoczi } 873922453bcSStefan Hajnoczi } 874922453bcSStefan Hajnoczi 875d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 876d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 877d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 878d22b2f41SRyan Harper { 879d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 880d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 881d22b2f41SRyan Harper } 882d22b2f41SRyan Harper bs->device_name[0] = '\0'; 883d22b2f41SRyan Harper } 884d22b2f41SRyan Harper 885b338082bSbellard void bdrv_delete(BlockDriverState *bs) 886b338082bSbellard { 887fa879d62SMarkus Armbruster assert(!bs->dev); 88818846deeSMarkus Armbruster 8891b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 890d22b2f41SRyan Harper bdrv_make_anon(bs); 89134c6f050Saurel32 892b338082bSbellard bdrv_close(bs); 89366f82ceeSKevin Wolf if (bs->file != NULL) { 89466f82ceeSKevin Wolf bdrv_delete(bs->file); 89566f82ceeSKevin Wolf } 89666f82ceeSKevin Wolf 897f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 8987267c094SAnthony Liguori g_free(bs); 899fc01f7e7Sbellard } 900fc01f7e7Sbellard 901fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 902fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 90318846deeSMarkus Armbruster { 904fa879d62SMarkus Armbruster if (bs->dev) { 90518846deeSMarkus Armbruster return -EBUSY; 90618846deeSMarkus Armbruster } 907fa879d62SMarkus Armbruster bs->dev = dev; 90828a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 90918846deeSMarkus Armbruster return 0; 91018846deeSMarkus Armbruster } 91118846deeSMarkus Armbruster 912fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 913fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 91418846deeSMarkus Armbruster { 915fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 916fa879d62SMarkus Armbruster abort(); 917fa879d62SMarkus Armbruster } 918fa879d62SMarkus Armbruster } 919fa879d62SMarkus Armbruster 920fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 921fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 922fa879d62SMarkus Armbruster { 923fa879d62SMarkus Armbruster assert(bs->dev == dev); 924fa879d62SMarkus Armbruster bs->dev = NULL; 9250e49de52SMarkus Armbruster bs->dev_ops = NULL; 9260e49de52SMarkus Armbruster bs->dev_opaque = NULL; 92729e05f20SMarkus Armbruster bs->buffer_alignment = 512; 92818846deeSMarkus Armbruster } 92918846deeSMarkus Armbruster 930fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 931fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 93218846deeSMarkus Armbruster { 933fa879d62SMarkus Armbruster return bs->dev; 93418846deeSMarkus Armbruster } 93518846deeSMarkus Armbruster 9360e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 9370e49de52SMarkus Armbruster void *opaque) 9380e49de52SMarkus Armbruster { 9390e49de52SMarkus Armbruster bs->dev_ops = ops; 9400e49de52SMarkus Armbruster bs->dev_opaque = opaque; 9412c6942faSMarkus Armbruster if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) { 9422c6942faSMarkus Armbruster bs_snapshots = NULL; 9432c6942faSMarkus Armbruster } 9440e49de52SMarkus Armbruster } 9450e49de52SMarkus Armbruster 946329c0a48SLuiz Capitulino void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 947329c0a48SLuiz Capitulino BlockQMPEventAction action, int is_read) 948329c0a48SLuiz Capitulino { 949329c0a48SLuiz Capitulino QObject *data; 950329c0a48SLuiz Capitulino const char *action_str; 951329c0a48SLuiz Capitulino 952329c0a48SLuiz Capitulino switch (action) { 953329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 954329c0a48SLuiz Capitulino action_str = "report"; 955329c0a48SLuiz Capitulino break; 956329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 957329c0a48SLuiz Capitulino action_str = "ignore"; 958329c0a48SLuiz Capitulino break; 959329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 960329c0a48SLuiz Capitulino action_str = "stop"; 961329c0a48SLuiz Capitulino break; 962329c0a48SLuiz Capitulino default: 963329c0a48SLuiz Capitulino abort(); 964329c0a48SLuiz Capitulino } 965329c0a48SLuiz Capitulino 966329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 967329c0a48SLuiz Capitulino bdrv->device_name, 968329c0a48SLuiz Capitulino action_str, 969329c0a48SLuiz Capitulino is_read ? "read" : "write"); 970329c0a48SLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 971329c0a48SLuiz Capitulino 972329c0a48SLuiz Capitulino qobject_decref(data); 973329c0a48SLuiz Capitulino } 974329c0a48SLuiz Capitulino 9756f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 9766f382ed2SLuiz Capitulino { 9776f382ed2SLuiz Capitulino QObject *data; 9786f382ed2SLuiz Capitulino 9796f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 9806f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 9816f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 9826f382ed2SLuiz Capitulino 9836f382ed2SLuiz Capitulino qobject_decref(data); 9846f382ed2SLuiz Capitulino } 9856f382ed2SLuiz Capitulino 9867d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 9870e49de52SMarkus Armbruster { 988145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 9896f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 9907d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 9916f382ed2SLuiz Capitulino if (tray_was_closed) { 9926f382ed2SLuiz Capitulino /* tray open */ 9936f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 9946f382ed2SLuiz Capitulino } 9956f382ed2SLuiz Capitulino if (load) { 9966f382ed2SLuiz Capitulino /* tray close */ 9976f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 9986f382ed2SLuiz Capitulino } 999145feb17SMarkus Armbruster } 1000145feb17SMarkus Armbruster } 1001145feb17SMarkus Armbruster 10022c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 10032c6942faSMarkus Armbruster { 10042c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 10052c6942faSMarkus Armbruster } 10062c6942faSMarkus Armbruster 1007025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1008025ccaa7SPaolo Bonzini { 1009025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1010025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1011025ccaa7SPaolo Bonzini } 1012025ccaa7SPaolo Bonzini } 1013025ccaa7SPaolo Bonzini 1014e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1015e4def80bSMarkus Armbruster { 1016e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1017e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1018e4def80bSMarkus Armbruster } 1019e4def80bSMarkus Armbruster return false; 1020e4def80bSMarkus Armbruster } 1021e4def80bSMarkus Armbruster 1022145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1023145feb17SMarkus Armbruster { 1024145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1025145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 10260e49de52SMarkus Armbruster } 10270e49de52SMarkus Armbruster } 10280e49de52SMarkus Armbruster 1029f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1030f107639aSMarkus Armbruster { 1031f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1032f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1033f107639aSMarkus Armbruster } 1034f107639aSMarkus Armbruster return false; 1035f107639aSMarkus Armbruster } 1036f107639aSMarkus Armbruster 1037e97fc193Saliguori /* 1038e97fc193Saliguori * Run consistency checks on an image 1039e97fc193Saliguori * 1040e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1041a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1042e076f338SKevin Wolf * check are stored in res. 1043e97fc193Saliguori */ 1044e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) 1045e97fc193Saliguori { 1046e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1047e97fc193Saliguori return -ENOTSUP; 1048e97fc193Saliguori } 1049e97fc193Saliguori 1050e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 10519ac228e0SKevin Wolf return bs->drv->bdrv_check(bs, res); 1052e97fc193Saliguori } 1053e97fc193Saliguori 10548a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 10558a426614SKevin Wolf 105633e3963eSbellard /* commit COW file into the raw image */ 105733e3963eSbellard int bdrv_commit(BlockDriverState *bs) 105833e3963eSbellard { 105919cb3738Sbellard BlockDriver *drv = bs->drv; 1060ee181196SKevin Wolf BlockDriver *backing_drv; 10618a426614SKevin Wolf int64_t sector, total_sectors; 10628a426614SKevin Wolf int n, ro, open_flags; 10634dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 10648a426614SKevin Wolf uint8_t *buf; 10654dca4b63SNaphtali Sprei char filename[1024]; 10664dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 106733e3963eSbellard 106819cb3738Sbellard if (!drv) 106919cb3738Sbellard return -ENOMEDIUM; 107033e3963eSbellard 10714dca4b63SNaphtali Sprei if (!bs->backing_hd) { 10724dca4b63SNaphtali Sprei return -ENOTSUP; 10734dca4b63SNaphtali Sprei } 10744dca4b63SNaphtali Sprei 10754dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 1076ea2384d3Sbellard return -EACCES; 107733e3963eSbellard } 107833e3963eSbellard 10792d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 10802d3735d3SStefan Hajnoczi return -EBUSY; 10812d3735d3SStefan Hajnoczi } 10822d3735d3SStefan Hajnoczi 1083ee181196SKevin Wolf backing_drv = bs->backing_hd->drv; 10844dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 10854dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 10864dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 10874dca4b63SNaphtali Sprei 10884dca4b63SNaphtali Sprei if (ro) { 10894dca4b63SNaphtali Sprei /* re-open as RW */ 10904dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 10914dca4b63SNaphtali Sprei bs->backing_hd = NULL; 10924dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 1093ee181196SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, 1094ee181196SKevin Wolf backing_drv); 10954dca4b63SNaphtali Sprei if (rw_ret < 0) { 10964dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 10974dca4b63SNaphtali Sprei /* try to re-open read-only */ 10984dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1099ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1100ee181196SKevin Wolf backing_drv); 11014dca4b63SNaphtali Sprei if (ret < 0) { 11024dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 11034dca4b63SNaphtali Sprei /* drive not functional anymore */ 11044dca4b63SNaphtali Sprei bs->drv = NULL; 11054dca4b63SNaphtali Sprei return ret; 11064dca4b63SNaphtali Sprei } 11074dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 11084dca4b63SNaphtali Sprei return rw_ret; 11094dca4b63SNaphtali Sprei } 11104dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 1111ea2384d3Sbellard } 1112ea2384d3Sbellard 11136ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 11147267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 11158a426614SKevin Wolf 11168a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 111705c4af54SStefan Hajnoczi if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 11188a426614SKevin Wolf 11198a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 11204dca4b63SNaphtali Sprei ret = -EIO; 11214dca4b63SNaphtali Sprei goto ro_cleanup; 112233e3963eSbellard } 112333e3963eSbellard 11248a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 11254dca4b63SNaphtali Sprei ret = -EIO; 11264dca4b63SNaphtali Sprei goto ro_cleanup; 112733e3963eSbellard } 112833e3963eSbellard } 112933e3963eSbellard } 113095389c86Sbellard 11311d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 11321d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 11331d44952fSChristoph Hellwig bdrv_flush(bs); 11341d44952fSChristoph Hellwig } 113595389c86Sbellard 11363f5075aeSChristoph Hellwig /* 11373f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 11383f5075aeSChristoph Hellwig * stable on disk. 11393f5075aeSChristoph Hellwig */ 11403f5075aeSChristoph Hellwig if (bs->backing_hd) 11413f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 11424dca4b63SNaphtali Sprei 11434dca4b63SNaphtali Sprei ro_cleanup: 11447267c094SAnthony Liguori g_free(buf); 11454dca4b63SNaphtali Sprei 11464dca4b63SNaphtali Sprei if (ro) { 11474dca4b63SNaphtali Sprei /* re-open as RO */ 11484dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 11494dca4b63SNaphtali Sprei bs->backing_hd = NULL; 11504dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 1151ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 1152ee181196SKevin Wolf backing_drv); 11534dca4b63SNaphtali Sprei if (ret < 0) { 11544dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 11554dca4b63SNaphtali Sprei /* drive not functional anymore */ 11564dca4b63SNaphtali Sprei bs->drv = NULL; 11574dca4b63SNaphtali Sprei return ret; 11584dca4b63SNaphtali Sprei } 11594dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 11604dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 11614dca4b63SNaphtali Sprei } 11624dca4b63SNaphtali Sprei 11631d44952fSChristoph Hellwig return ret; 116433e3963eSbellard } 116533e3963eSbellard 11666ab4b5abSMarkus Armbruster void bdrv_commit_all(void) 11676ab4b5abSMarkus Armbruster { 11686ab4b5abSMarkus Armbruster BlockDriverState *bs; 11696ab4b5abSMarkus Armbruster 11706ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 11716ab4b5abSMarkus Armbruster bdrv_commit(bs); 11726ab4b5abSMarkus Armbruster } 11736ab4b5abSMarkus Armbruster } 11746ab4b5abSMarkus Armbruster 1175dbffbdcfSStefan Hajnoczi struct BdrvTrackedRequest { 1176dbffbdcfSStefan Hajnoczi BlockDriverState *bs; 1177dbffbdcfSStefan Hajnoczi int64_t sector_num; 1178dbffbdcfSStefan Hajnoczi int nb_sectors; 1179dbffbdcfSStefan Hajnoczi bool is_write; 1180dbffbdcfSStefan Hajnoczi QLIST_ENTRY(BdrvTrackedRequest) list; 11815f8b6491SStefan Hajnoczi Coroutine *co; /* owner, used for deadlock detection */ 1182f4658285SStefan Hajnoczi CoQueue wait_queue; /* coroutines blocked on this request */ 1183dbffbdcfSStefan Hajnoczi }; 1184dbffbdcfSStefan Hajnoczi 1185dbffbdcfSStefan Hajnoczi /** 1186dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1187dbffbdcfSStefan Hajnoczi * 1188dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1189dbffbdcfSStefan Hajnoczi */ 1190dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1191dbffbdcfSStefan Hajnoczi { 1192dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1193f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1194dbffbdcfSStefan Hajnoczi } 1195dbffbdcfSStefan Hajnoczi 1196dbffbdcfSStefan Hajnoczi /** 1197dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1198dbffbdcfSStefan Hajnoczi */ 1199dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1200dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1201dbffbdcfSStefan Hajnoczi int64_t sector_num, 1202dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1203dbffbdcfSStefan Hajnoczi { 1204dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1205dbffbdcfSStefan Hajnoczi .bs = bs, 1206dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1207dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1208dbffbdcfSStefan Hajnoczi .is_write = is_write, 12095f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1210dbffbdcfSStefan Hajnoczi }; 1211dbffbdcfSStefan Hajnoczi 1212f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1213f4658285SStefan Hajnoczi 1214dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1215dbffbdcfSStefan Hajnoczi } 1216dbffbdcfSStefan Hajnoczi 1217d83947acSStefan Hajnoczi /** 1218d83947acSStefan Hajnoczi * Round a region to cluster boundaries 1219d83947acSStefan Hajnoczi */ 1220d83947acSStefan Hajnoczi static void round_to_clusters(BlockDriverState *bs, 1221d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 1222d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 1223d83947acSStefan Hajnoczi int *cluster_nb_sectors) 1224d83947acSStefan Hajnoczi { 1225d83947acSStefan Hajnoczi BlockDriverInfo bdi; 1226d83947acSStefan Hajnoczi 1227d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 1228d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 1229d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 1230d83947acSStefan Hajnoczi } else { 1231d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 1232d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 1233d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 1234d83947acSStefan Hajnoczi nb_sectors, c); 1235d83947acSStefan Hajnoczi } 1236d83947acSStefan Hajnoczi } 1237d83947acSStefan Hajnoczi 1238f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 1239f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 1240d83947acSStefan Hajnoczi /* aaaa bbbb */ 1241d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 1242d83947acSStefan Hajnoczi return false; 1243d83947acSStefan Hajnoczi } 1244d83947acSStefan Hajnoczi /* bbbb aaaa */ 1245d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 1246d83947acSStefan Hajnoczi return false; 1247d83947acSStefan Hajnoczi } 1248d83947acSStefan Hajnoczi return true; 1249f4658285SStefan Hajnoczi } 1250f4658285SStefan Hajnoczi 1251f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 1252f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 1253f4658285SStefan Hajnoczi { 1254f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 1255d83947acSStefan Hajnoczi int64_t cluster_sector_num; 1256d83947acSStefan Hajnoczi int cluster_nb_sectors; 1257f4658285SStefan Hajnoczi bool retry; 1258f4658285SStefan Hajnoczi 1259d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 1260d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 1261d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 1262d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 1263d83947acSStefan Hajnoczi * interleave between them. 1264d83947acSStefan Hajnoczi */ 1265d83947acSStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1266d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1267d83947acSStefan Hajnoczi 1268f4658285SStefan Hajnoczi do { 1269f4658285SStefan Hajnoczi retry = false; 1270f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 1271d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 1272d83947acSStefan Hajnoczi cluster_nb_sectors)) { 12735f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 12745f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 12755f8b6491SStefan Hajnoczi * never happen since it means deadlock. 12765f8b6491SStefan Hajnoczi */ 12775f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 12785f8b6491SStefan Hajnoczi 1279f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 1280f4658285SStefan Hajnoczi retry = true; 1281f4658285SStefan Hajnoczi break; 1282f4658285SStefan Hajnoczi } 1283f4658285SStefan Hajnoczi } 1284f4658285SStefan Hajnoczi } while (retry); 1285f4658285SStefan Hajnoczi } 1286f4658285SStefan Hajnoczi 1287756e6736SKevin Wolf /* 1288756e6736SKevin Wolf * Return values: 1289756e6736SKevin Wolf * 0 - success 1290756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 1291756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 1292756e6736SKevin Wolf * image file header 1293756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 1294756e6736SKevin Wolf */ 1295756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 1296756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 1297756e6736SKevin Wolf { 1298756e6736SKevin Wolf BlockDriver *drv = bs->drv; 1299756e6736SKevin Wolf 1300756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 1301756e6736SKevin Wolf return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 1302756e6736SKevin Wolf } else { 1303756e6736SKevin Wolf return -ENOTSUP; 1304756e6736SKevin Wolf } 1305756e6736SKevin Wolf } 1306756e6736SKevin Wolf 130771d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 130871d0770cSaliguori size_t size) 130971d0770cSaliguori { 131071d0770cSaliguori int64_t len; 131171d0770cSaliguori 131271d0770cSaliguori if (!bdrv_is_inserted(bs)) 131371d0770cSaliguori return -ENOMEDIUM; 131471d0770cSaliguori 131571d0770cSaliguori if (bs->growable) 131671d0770cSaliguori return 0; 131771d0770cSaliguori 131871d0770cSaliguori len = bdrv_getlength(bs); 131971d0770cSaliguori 1320fbb7b4e0SKevin Wolf if (offset < 0) 1321fbb7b4e0SKevin Wolf return -EIO; 1322fbb7b4e0SKevin Wolf 1323fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 132471d0770cSaliguori return -EIO; 132571d0770cSaliguori 132671d0770cSaliguori return 0; 132771d0770cSaliguori } 132871d0770cSaliguori 132971d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 133071d0770cSaliguori int nb_sectors) 133171d0770cSaliguori { 1332eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 1333eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 133471d0770cSaliguori } 133571d0770cSaliguori 13361c9805a3SStefan Hajnoczi typedef struct RwCo { 13371c9805a3SStefan Hajnoczi BlockDriverState *bs; 13381c9805a3SStefan Hajnoczi int64_t sector_num; 13391c9805a3SStefan Hajnoczi int nb_sectors; 13401c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 13411c9805a3SStefan Hajnoczi bool is_write; 13421c9805a3SStefan Hajnoczi int ret; 13431c9805a3SStefan Hajnoczi } RwCo; 13441c9805a3SStefan Hajnoczi 13451c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 1346fc01f7e7Sbellard { 13471c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 1348fc01f7e7Sbellard 13491c9805a3SStefan Hajnoczi if (!rwco->is_write) { 13501c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 1351470c0504SStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 13521c9805a3SStefan Hajnoczi } else { 13531c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 1354f08f2ddaSStefan Hajnoczi rwco->nb_sectors, rwco->qiov, 0); 13551c9805a3SStefan Hajnoczi } 13561c9805a3SStefan Hajnoczi } 1357e7a8a783SKevin Wolf 13581c9805a3SStefan Hajnoczi /* 13591c9805a3SStefan Hajnoczi * Process a synchronous request using coroutines 13601c9805a3SStefan Hajnoczi */ 13611c9805a3SStefan Hajnoczi static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 13621c9805a3SStefan Hajnoczi int nb_sectors, bool is_write) 13631c9805a3SStefan Hajnoczi { 1364e7a8a783SKevin Wolf QEMUIOVector qiov; 1365e7a8a783SKevin Wolf struct iovec iov = { 1366e7a8a783SKevin Wolf .iov_base = (void *)buf, 1367e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 1368e7a8a783SKevin Wolf }; 13691c9805a3SStefan Hajnoczi Coroutine *co; 13701c9805a3SStefan Hajnoczi RwCo rwco = { 13711c9805a3SStefan Hajnoczi .bs = bs, 13721c9805a3SStefan Hajnoczi .sector_num = sector_num, 13731c9805a3SStefan Hajnoczi .nb_sectors = nb_sectors, 13741c9805a3SStefan Hajnoczi .qiov = &qiov, 13751c9805a3SStefan Hajnoczi .is_write = is_write, 13761c9805a3SStefan Hajnoczi .ret = NOT_DONE, 13771c9805a3SStefan Hajnoczi }; 1378e7a8a783SKevin Wolf 1379e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 13801c9805a3SStefan Hajnoczi 13811c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 13821c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 13831c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 13841c9805a3SStefan Hajnoczi } else { 13851c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 13861c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 13871c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 13881c9805a3SStefan Hajnoczi qemu_aio_wait(); 13891c9805a3SStefan Hajnoczi } 13901c9805a3SStefan Hajnoczi } 13911c9805a3SStefan Hajnoczi return rwco.ret; 1392e7a8a783SKevin Wolf } 1393e7a8a783SKevin Wolf 13941c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 13951c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 13961c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 13971c9805a3SStefan Hajnoczi { 13981c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false); 139983f64091Sbellard } 1400fc01f7e7Sbellard 14017cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 14027cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 14037cd1e32aSlirans@il.ibm.com { 14047cd1e32aSlirans@il.ibm.com int64_t start, end; 1405c6d22830SJan Kiszka unsigned long val, idx, bit; 1406a55eb92cSJan Kiszka 14076ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 1408c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 14097cd1e32aSlirans@il.ibm.com 14107cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 1411c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 1412c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 1413c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 1414c6d22830SJan Kiszka if (dirty) { 14156d59fec1SMarcelo Tosatti if (!(val & (1UL << bit))) { 1416aaa0eb75SLiran Schour bs->dirty_count++; 14176d59fec1SMarcelo Tosatti val |= 1UL << bit; 1418aaa0eb75SLiran Schour } 1419c6d22830SJan Kiszka } else { 14206d59fec1SMarcelo Tosatti if (val & (1UL << bit)) { 1421aaa0eb75SLiran Schour bs->dirty_count--; 14226d59fec1SMarcelo Tosatti val &= ~(1UL << bit); 1423c6d22830SJan Kiszka } 1424aaa0eb75SLiran Schour } 1425c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 14267cd1e32aSlirans@il.ibm.com } 14277cd1e32aSlirans@il.ibm.com } 14287cd1e32aSlirans@il.ibm.com 142919cb3738Sbellard /* Return < 0 if error. Important errors are: 143019cb3738Sbellard -EIO generic I/O error (may happen for all errors) 143119cb3738Sbellard -ENOMEDIUM No media inserted. 143219cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 143319cb3738Sbellard -EACCES Trying to write a read-only device 143419cb3738Sbellard */ 1435fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 1436fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 1437fc01f7e7Sbellard { 14381c9805a3SStefan Hajnoczi return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true); 143983f64091Sbellard } 144083f64091Sbellard 1441eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 1442eda578e5Saliguori void *buf, int count1) 144383f64091Sbellard { 14446ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 144583f64091Sbellard int len, nb_sectors, count; 144683f64091Sbellard int64_t sector_num; 14479a8c4cceSKevin Wolf int ret; 144883f64091Sbellard 144983f64091Sbellard count = count1; 145083f64091Sbellard /* first read to align to sector start */ 14516ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 145283f64091Sbellard if (len > count) 145383f64091Sbellard len = count; 14546ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 145583f64091Sbellard if (len > 0) { 14569a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 14579a8c4cceSKevin Wolf return ret; 14586ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 145983f64091Sbellard count -= len; 146083f64091Sbellard if (count == 0) 146183f64091Sbellard return count1; 146283f64091Sbellard sector_num++; 146383f64091Sbellard buf += len; 146483f64091Sbellard } 146583f64091Sbellard 146683f64091Sbellard /* read the sectors "in place" */ 14676ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 146883f64091Sbellard if (nb_sectors > 0) { 14699a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 14709a8c4cceSKevin Wolf return ret; 147183f64091Sbellard sector_num += nb_sectors; 14726ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 147383f64091Sbellard buf += len; 147483f64091Sbellard count -= len; 147583f64091Sbellard } 147683f64091Sbellard 147783f64091Sbellard /* add data from the last sector */ 147883f64091Sbellard if (count > 0) { 14799a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 14809a8c4cceSKevin Wolf return ret; 148183f64091Sbellard memcpy(buf, tmp_buf, count); 148283f64091Sbellard } 148383f64091Sbellard return count1; 148483f64091Sbellard } 148583f64091Sbellard 1486eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 1487eda578e5Saliguori const void *buf, int count1) 148883f64091Sbellard { 14896ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 149083f64091Sbellard int len, nb_sectors, count; 149183f64091Sbellard int64_t sector_num; 14929a8c4cceSKevin Wolf int ret; 149383f64091Sbellard 149483f64091Sbellard count = count1; 149583f64091Sbellard /* first write to align to sector start */ 14966ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 149783f64091Sbellard if (len > count) 149883f64091Sbellard len = count; 14996ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 150083f64091Sbellard if (len > 0) { 15019a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 15029a8c4cceSKevin Wolf return ret; 15036ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 15049a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 15059a8c4cceSKevin Wolf return ret; 150683f64091Sbellard count -= len; 150783f64091Sbellard if (count == 0) 150883f64091Sbellard return count1; 150983f64091Sbellard sector_num++; 151083f64091Sbellard buf += len; 151183f64091Sbellard } 151283f64091Sbellard 151383f64091Sbellard /* write the sectors "in place" */ 15146ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 151583f64091Sbellard if (nb_sectors > 0) { 15169a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 15179a8c4cceSKevin Wolf return ret; 151883f64091Sbellard sector_num += nb_sectors; 15196ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 152083f64091Sbellard buf += len; 152183f64091Sbellard count -= len; 152283f64091Sbellard } 152383f64091Sbellard 152483f64091Sbellard /* add data from the last sector */ 152583f64091Sbellard if (count > 0) { 15269a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 15279a8c4cceSKevin Wolf return ret; 152883f64091Sbellard memcpy(tmp_buf, buf, count); 15299a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 15309a8c4cceSKevin Wolf return ret; 153183f64091Sbellard } 153283f64091Sbellard return count1; 153383f64091Sbellard } 153483f64091Sbellard 1535f08145feSKevin Wolf /* 1536f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1537f08145feSKevin Wolf * request (acts as a barrier) 1538f08145feSKevin Wolf * 1539f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1540f08145feSKevin Wolf */ 1541f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1542f08145feSKevin Wolf const void *buf, int count) 1543f08145feSKevin Wolf { 1544f08145feSKevin Wolf int ret; 1545f08145feSKevin Wolf 1546f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1547f08145feSKevin Wolf if (ret < 0) { 1548f08145feSKevin Wolf return ret; 1549f08145feSKevin Wolf } 1550f08145feSKevin Wolf 155192196b2fSStefan Hajnoczi /* No flush needed for cache modes that use O_DSYNC */ 155292196b2fSStefan Hajnoczi if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) { 1553f08145feSKevin Wolf bdrv_flush(bs); 1554f08145feSKevin Wolf } 1555f08145feSKevin Wolf 1556f08145feSKevin Wolf return 0; 1557f08145feSKevin Wolf } 1558f08145feSKevin Wolf 1559470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 1560ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1561ab185921SStefan Hajnoczi { 1562ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 1563ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 1564ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 1565ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 1566ab185921SStefan Hajnoczi */ 1567ab185921SStefan Hajnoczi void *bounce_buffer; 1568ab185921SStefan Hajnoczi 156979c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 1570ab185921SStefan Hajnoczi struct iovec iov; 1571ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 1572ab185921SStefan Hajnoczi int64_t cluster_sector_num; 1573ab185921SStefan Hajnoczi int cluster_nb_sectors; 1574ab185921SStefan Hajnoczi size_t skip_bytes; 1575ab185921SStefan Hajnoczi int ret; 1576ab185921SStefan Hajnoczi 1577ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 1578ab185921SStefan Hajnoczi * allocating cluster in the image file. 1579ab185921SStefan Hajnoczi */ 1580ab185921SStefan Hajnoczi round_to_clusters(bs, sector_num, nb_sectors, 1581ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 1582ab185921SStefan Hajnoczi 1583470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 1584ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 1585ab185921SStefan Hajnoczi 1586ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 1587ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 1588ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 1589ab185921SStefan Hajnoczi 159079c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 1591ab185921SStefan Hajnoczi &bounce_qiov); 1592ab185921SStefan Hajnoczi if (ret < 0) { 1593ab185921SStefan Hajnoczi goto err; 1594ab185921SStefan Hajnoczi } 1595ab185921SStefan Hajnoczi 159679c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 159779c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 159879c053bdSStefan Hajnoczi ret = drv->bdrv_co_write_zeroes(bs, cluster_sector_num, 159979c053bdSStefan Hajnoczi cluster_nb_sectors); 160079c053bdSStefan Hajnoczi } else { 160179c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 1602ab185921SStefan Hajnoczi &bounce_qiov); 160379c053bdSStefan Hajnoczi } 160479c053bdSStefan Hajnoczi 1605ab185921SStefan Hajnoczi if (ret < 0) { 1606ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 1607ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 1608ab185921SStefan Hajnoczi * Simply report it in all cases. 1609ab185921SStefan Hajnoczi */ 1610ab185921SStefan Hajnoczi goto err; 1611ab185921SStefan Hajnoczi } 1612ab185921SStefan Hajnoczi 1613ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 1614ab185921SStefan Hajnoczi qemu_iovec_from_buffer(qiov, bounce_buffer + skip_bytes, 1615ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 1616ab185921SStefan Hajnoczi 1617ab185921SStefan Hajnoczi err: 1618ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 1619ab185921SStefan Hajnoczi return ret; 1620ab185921SStefan Hajnoczi } 1621ab185921SStefan Hajnoczi 1622c5fbe571SStefan Hajnoczi /* 1623c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 1624c5fbe571SStefan Hajnoczi */ 1625c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 1626470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1627470c0504SStefan Hajnoczi BdrvRequestFlags flags) 1628da1fa91dSKevin Wolf { 1629da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 1630dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 1631dbffbdcfSStefan Hajnoczi int ret; 1632da1fa91dSKevin Wolf 1633da1fa91dSKevin Wolf if (!drv) { 1634da1fa91dSKevin Wolf return -ENOMEDIUM; 1635da1fa91dSKevin Wolf } 1636da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1637da1fa91dSKevin Wolf return -EIO; 1638da1fa91dSKevin Wolf } 1639da1fa91dSKevin Wolf 164098f90dbaSZhi Yong Wu /* throttling disk read I/O */ 164198f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 164298f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, false, nb_sectors); 164398f90dbaSZhi Yong Wu } 164498f90dbaSZhi Yong Wu 1645f4658285SStefan Hajnoczi if (bs->copy_on_read) { 1646470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 1647470c0504SStefan Hajnoczi } 1648470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1649470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 1650470c0504SStefan Hajnoczi } 1651470c0504SStefan Hajnoczi 1652470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1653f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1654f4658285SStefan Hajnoczi } 1655f4658285SStefan Hajnoczi 1656dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 1657ab185921SStefan Hajnoczi 1658470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1659ab185921SStefan Hajnoczi int pnum; 1660ab185921SStefan Hajnoczi 1661ab185921SStefan Hajnoczi ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum); 1662ab185921SStefan Hajnoczi if (ret < 0) { 1663ab185921SStefan Hajnoczi goto out; 1664ab185921SStefan Hajnoczi } 1665ab185921SStefan Hajnoczi 1666ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 1667470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 1668ab185921SStefan Hajnoczi goto out; 1669ab185921SStefan Hajnoczi } 1670ab185921SStefan Hajnoczi } 1671ab185921SStefan Hajnoczi 1672dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 1673ab185921SStefan Hajnoczi 1674ab185921SStefan Hajnoczi out: 1675dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1676470c0504SStefan Hajnoczi 1677470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 1678470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 1679470c0504SStefan Hajnoczi } 1680470c0504SStefan Hajnoczi 1681dbffbdcfSStefan Hajnoczi return ret; 1682da1fa91dSKevin Wolf } 1683da1fa91dSKevin Wolf 1684c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 1685da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 1686da1fa91dSKevin Wolf { 1687c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 1688da1fa91dSKevin Wolf 1689470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 1690470c0504SStefan Hajnoczi } 1691470c0504SStefan Hajnoczi 1692470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 1693470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 1694470c0504SStefan Hajnoczi { 1695470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 1696470c0504SStefan Hajnoczi 1697470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 1698470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 1699c5fbe571SStefan Hajnoczi } 1700c5fbe571SStefan Hajnoczi 1701f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 1702f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1703f08f2ddaSStefan Hajnoczi { 1704f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 1705f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 1706f08f2ddaSStefan Hajnoczi struct iovec iov; 1707f08f2ddaSStefan Hajnoczi int ret; 1708f08f2ddaSStefan Hajnoczi 1709f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 1710f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 1711f08f2ddaSStefan Hajnoczi return drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1712f08f2ddaSStefan Hajnoczi } 1713f08f2ddaSStefan Hajnoczi 1714f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 1715f08f2ddaSStefan Hajnoczi iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 1716f08f2ddaSStefan Hajnoczi iov.iov_base = qemu_blockalign(bs, iov.iov_len); 1717f08f2ddaSStefan Hajnoczi memset(iov.iov_base, 0, iov.iov_len); 1718f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 1719f08f2ddaSStefan Hajnoczi 1720f08f2ddaSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 1721f08f2ddaSStefan Hajnoczi 1722f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 1723f08f2ddaSStefan Hajnoczi return ret; 1724f08f2ddaSStefan Hajnoczi } 1725f08f2ddaSStefan Hajnoczi 1726c5fbe571SStefan Hajnoczi /* 1727c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 1728c5fbe571SStefan Hajnoczi */ 1729c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 1730f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 1731f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 1732c5fbe571SStefan Hajnoczi { 1733c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 1734dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 17356b7cb247SStefan Hajnoczi int ret; 1736da1fa91dSKevin Wolf 1737da1fa91dSKevin Wolf if (!bs->drv) { 1738da1fa91dSKevin Wolf return -ENOMEDIUM; 1739da1fa91dSKevin Wolf } 1740da1fa91dSKevin Wolf if (bs->read_only) { 1741da1fa91dSKevin Wolf return -EACCES; 1742da1fa91dSKevin Wolf } 1743da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1744da1fa91dSKevin Wolf return -EIO; 1745da1fa91dSKevin Wolf } 1746da1fa91dSKevin Wolf 174798f90dbaSZhi Yong Wu /* throttling disk write I/O */ 174898f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 174998f90dbaSZhi Yong Wu bdrv_io_limits_intercept(bs, true, nb_sectors); 175098f90dbaSZhi Yong Wu } 175198f90dbaSZhi Yong Wu 1752470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 1753f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 1754f4658285SStefan Hajnoczi } 1755f4658285SStefan Hajnoczi 1756dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 1757dbffbdcfSStefan Hajnoczi 1758f08f2ddaSStefan Hajnoczi if (flags & BDRV_REQ_ZERO_WRITE) { 1759f08f2ddaSStefan Hajnoczi ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors); 1760f08f2ddaSStefan Hajnoczi } else { 17616b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 1762f08f2ddaSStefan Hajnoczi } 17636b7cb247SStefan Hajnoczi 1764da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 1765da1fa91dSKevin Wolf set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 1766da1fa91dSKevin Wolf } 1767da1fa91dSKevin Wolf 1768da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 1769da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 1770da1fa91dSKevin Wolf } 1771da1fa91dSKevin Wolf 1772dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 1773dbffbdcfSStefan Hajnoczi 17746b7cb247SStefan Hajnoczi return ret; 1775da1fa91dSKevin Wolf } 1776da1fa91dSKevin Wolf 1777c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 1778c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 1779c5fbe571SStefan Hajnoczi { 1780c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 1781c5fbe571SStefan Hajnoczi 1782f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 1783f08f2ddaSStefan Hajnoczi } 1784f08f2ddaSStefan Hajnoczi 1785f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 1786f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors) 1787f08f2ddaSStefan Hajnoczi { 1788f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 1789f08f2ddaSStefan Hajnoczi 1790f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 1791f08f2ddaSStefan Hajnoczi BDRV_REQ_ZERO_WRITE); 1792c5fbe571SStefan Hajnoczi } 1793c5fbe571SStefan Hajnoczi 179483f64091Sbellard /** 179583f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 179683f64091Sbellard */ 179783f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 179883f64091Sbellard { 179983f64091Sbellard BlockDriver *drv = bs->drv; 180051762288SStefan Hajnoczi int ret; 180183f64091Sbellard if (!drv) 180219cb3738Sbellard return -ENOMEDIUM; 180383f64091Sbellard if (!drv->bdrv_truncate) 180483f64091Sbellard return -ENOTSUP; 180559f2689dSNaphtali Sprei if (bs->read_only) 180659f2689dSNaphtali Sprei return -EACCES; 18078591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 18088591675fSMarcelo Tosatti return -EBUSY; 180951762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 181051762288SStefan Hajnoczi if (ret == 0) { 181151762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 1812145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 181351762288SStefan Hajnoczi } 181451762288SStefan Hajnoczi return ret; 181583f64091Sbellard } 181683f64091Sbellard 181783f64091Sbellard /** 18184a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 18194a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 18204a1d5e1fSFam Zheng */ 18214a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 18224a1d5e1fSFam Zheng { 18234a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 18244a1d5e1fSFam Zheng if (!drv) { 18254a1d5e1fSFam Zheng return -ENOMEDIUM; 18264a1d5e1fSFam Zheng } 18274a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 18284a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 18294a1d5e1fSFam Zheng } 18304a1d5e1fSFam Zheng if (bs->file) { 18314a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 18324a1d5e1fSFam Zheng } 18334a1d5e1fSFam Zheng return -ENOTSUP; 18344a1d5e1fSFam Zheng } 18354a1d5e1fSFam Zheng 18364a1d5e1fSFam Zheng /** 183783f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 183883f64091Sbellard */ 183983f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 184083f64091Sbellard { 184183f64091Sbellard BlockDriver *drv = bs->drv; 184283f64091Sbellard if (!drv) 184319cb3738Sbellard return -ENOMEDIUM; 184451762288SStefan Hajnoczi 18452c6942faSMarkus Armbruster if (bs->growable || bdrv_dev_has_removable_media(bs)) { 184646a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 184783f64091Sbellard return drv->bdrv_getlength(bs); 1848fc01f7e7Sbellard } 184946a4e4e6SStefan Hajnoczi } 185046a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 185146a4e4e6SStefan Hajnoczi } 1852fc01f7e7Sbellard 185319cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 185496b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 1855fc01f7e7Sbellard { 185619cb3738Sbellard int64_t length; 185719cb3738Sbellard length = bdrv_getlength(bs); 185819cb3738Sbellard if (length < 0) 185919cb3738Sbellard length = 0; 186019cb3738Sbellard else 18616ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 186219cb3738Sbellard *nb_sectors_ptr = length; 1863fc01f7e7Sbellard } 1864cf98951bSbellard 1865f3d54fc4Saliguori struct partition { 1866f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 1867f3d54fc4Saliguori uint8_t head; /* starting head */ 1868f3d54fc4Saliguori uint8_t sector; /* starting sector */ 1869f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 1870f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 1871f3d54fc4Saliguori uint8_t end_head; /* end head */ 1872f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 1873f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 1874f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 1875f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 1876541dc0d4SStefan Weil } QEMU_PACKED; 1877f3d54fc4Saliguori 1878f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 1879f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 1880f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 1881f3d54fc4Saliguori { 1882eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 1883f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 1884f3d54fc4Saliguori struct partition *p; 1885f3d54fc4Saliguori uint32_t nr_sects; 1886a38131b6Sblueswir1 uint64_t nb_sectors; 1887f3d54fc4Saliguori 1888f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1889f3d54fc4Saliguori 1890f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 1891f3d54fc4Saliguori if (ret < 0) 1892f3d54fc4Saliguori return -1; 1893f3d54fc4Saliguori /* test msdos magic */ 1894f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 1895f3d54fc4Saliguori return -1; 1896f3d54fc4Saliguori for(i = 0; i < 4; i++) { 1897f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 1898f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 1899f3d54fc4Saliguori if (nr_sects && p->end_head) { 1900f3d54fc4Saliguori /* We make the assumption that the partition terminates on 1901f3d54fc4Saliguori a cylinder boundary */ 1902f3d54fc4Saliguori heads = p->end_head + 1; 1903f3d54fc4Saliguori sectors = p->end_sector & 63; 1904f3d54fc4Saliguori if (sectors == 0) 1905f3d54fc4Saliguori continue; 1906f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 1907f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 1908f3d54fc4Saliguori continue; 1909f3d54fc4Saliguori *pheads = heads; 1910f3d54fc4Saliguori *psectors = sectors; 1911f3d54fc4Saliguori *pcylinders = cylinders; 1912f3d54fc4Saliguori #if 0 1913f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 1914f3d54fc4Saliguori cylinders, heads, sectors); 1915f3d54fc4Saliguori #endif 1916f3d54fc4Saliguori return 0; 1917f3d54fc4Saliguori } 1918f3d54fc4Saliguori } 1919f3d54fc4Saliguori return -1; 1920f3d54fc4Saliguori } 1921f3d54fc4Saliguori 1922f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 1923f3d54fc4Saliguori { 1924f3d54fc4Saliguori int translation, lba_detected = 0; 1925f3d54fc4Saliguori int cylinders, heads, secs; 1926a38131b6Sblueswir1 uint64_t nb_sectors; 1927f3d54fc4Saliguori 1928f3d54fc4Saliguori /* if a geometry hint is available, use it */ 1929f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1930f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 1931f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 1932f3d54fc4Saliguori if (cylinders != 0) { 1933f3d54fc4Saliguori *pcyls = cylinders; 1934f3d54fc4Saliguori *pheads = heads; 1935f3d54fc4Saliguori *psecs = secs; 1936f3d54fc4Saliguori } else { 1937f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 1938f3d54fc4Saliguori if (heads > 16) { 1939f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 1940f3d54fc4Saliguori translation was active, so the default 1941f3d54fc4Saliguori hardware geometry is OK */ 1942f3d54fc4Saliguori lba_detected = 1; 1943f3d54fc4Saliguori goto default_geometry; 1944f3d54fc4Saliguori } else { 1945f3d54fc4Saliguori *pcyls = cylinders; 1946f3d54fc4Saliguori *pheads = heads; 1947f3d54fc4Saliguori *psecs = secs; 1948f3d54fc4Saliguori /* disable any translation to be in sync with 1949f3d54fc4Saliguori the logical geometry */ 1950f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 1951f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1952f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 1953f3d54fc4Saliguori } 1954f3d54fc4Saliguori } 1955f3d54fc4Saliguori } else { 1956f3d54fc4Saliguori default_geometry: 1957f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 1958f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 1959f3d54fc4Saliguori 1960f3d54fc4Saliguori if (cylinders > 16383) 1961f3d54fc4Saliguori cylinders = 16383; 1962f3d54fc4Saliguori else if (cylinders < 2) 1963f3d54fc4Saliguori cylinders = 2; 1964f3d54fc4Saliguori *pcyls = cylinders; 1965f3d54fc4Saliguori *pheads = 16; 1966f3d54fc4Saliguori *psecs = 63; 1967f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 1968f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 1969f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1970f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 1971f3d54fc4Saliguori } else { 1972f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1973f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 1974f3d54fc4Saliguori } 1975f3d54fc4Saliguori } 1976f3d54fc4Saliguori } 1977f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 1978f3d54fc4Saliguori } 1979f3d54fc4Saliguori } 1980f3d54fc4Saliguori 1981b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 1982b338082bSbellard int cyls, int heads, int secs) 1983b338082bSbellard { 1984b338082bSbellard bs->cyls = cyls; 1985b338082bSbellard bs->heads = heads; 1986b338082bSbellard bs->secs = secs; 1987b338082bSbellard } 1988b338082bSbellard 198946d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 199046d4767dSbellard { 199146d4767dSbellard bs->translation = translation; 199246d4767dSbellard } 199346d4767dSbellard 1994b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 1995b338082bSbellard int *pcyls, int *pheads, int *psecs) 1996b338082bSbellard { 1997b338082bSbellard *pcyls = bs->cyls; 1998b338082bSbellard *pheads = bs->heads; 1999b338082bSbellard *psecs = bs->secs; 2000b338082bSbellard } 2001b338082bSbellard 20020563e191SZhi Yong Wu /* throttling disk io limits */ 20030563e191SZhi Yong Wu void bdrv_set_io_limits(BlockDriverState *bs, 20040563e191SZhi Yong Wu BlockIOLimit *io_limits) 20050563e191SZhi Yong Wu { 20060563e191SZhi Yong Wu bs->io_limits = *io_limits; 20070563e191SZhi Yong Wu bs->io_limits_enabled = bdrv_io_limits_enabled(bs); 20080563e191SZhi Yong Wu } 20090563e191SZhi Yong Wu 20105bbdbb46SBlue Swirl /* Recognize floppy formats */ 20115bbdbb46SBlue Swirl typedef struct FDFormat { 20125bbdbb46SBlue Swirl FDriveType drive; 20135bbdbb46SBlue Swirl uint8_t last_sect; 20145bbdbb46SBlue Swirl uint8_t max_track; 20155bbdbb46SBlue Swirl uint8_t max_head; 2016f8d3d128SHervé Poussineau FDriveRate rate; 20175bbdbb46SBlue Swirl } FDFormat; 20185bbdbb46SBlue Swirl 20195bbdbb46SBlue Swirl static const FDFormat fd_formats[] = { 20205bbdbb46SBlue Swirl /* First entry is default format */ 20215bbdbb46SBlue Swirl /* 1.44 MB 3"1/2 floppy disks */ 2022f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 18, 80, 1, FDRIVE_RATE_500K, }, 2023f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 20, 80, 1, FDRIVE_RATE_500K, }, 2024f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 80, 1, FDRIVE_RATE_500K, }, 2025f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 82, 1, FDRIVE_RATE_500K, }, 2026f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 21, 83, 1, FDRIVE_RATE_500K, }, 2027f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 22, 80, 1, FDRIVE_RATE_500K, }, 2028f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 23, 80, 1, FDRIVE_RATE_500K, }, 2029f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 24, 80, 1, FDRIVE_RATE_500K, }, 20305bbdbb46SBlue Swirl /* 2.88 MB 3"1/2 floppy disks */ 2031f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 36, 80, 1, FDRIVE_RATE_1M, }, 2032f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 39, 80, 1, FDRIVE_RATE_1M, }, 2033f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 40, 80, 1, FDRIVE_RATE_1M, }, 2034f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 44, 80, 1, FDRIVE_RATE_1M, }, 2035f8d3d128SHervé Poussineau { FDRIVE_DRV_288, 48, 80, 1, FDRIVE_RATE_1M, }, 20365bbdbb46SBlue Swirl /* 720 kB 3"1/2 floppy disks */ 2037f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 9, 80, 1, FDRIVE_RATE_250K, }, 2038f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 80, 1, FDRIVE_RATE_250K, }, 2039f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 82, 1, FDRIVE_RATE_250K, }, 2040f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 10, 83, 1, FDRIVE_RATE_250K, }, 2041f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 13, 80, 1, FDRIVE_RATE_250K, }, 2042f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 14, 80, 1, FDRIVE_RATE_250K, }, 20435bbdbb46SBlue Swirl /* 1.2 MB 5"1/4 floppy disks */ 2044f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 15, 80, 1, FDRIVE_RATE_500K, }, 2045f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 80, 1, FDRIVE_RATE_500K, }, 2046f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 82, 1, FDRIVE_RATE_500K, }, 2047f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 18, 83, 1, FDRIVE_RATE_500K, }, 2048f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 20, 80, 1, FDRIVE_RATE_500K, }, 20495bbdbb46SBlue Swirl /* 720 kB 5"1/4 floppy disks */ 2050f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 80, 1, FDRIVE_RATE_250K, }, 2051f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 11, 80, 1, FDRIVE_RATE_250K, }, 20525bbdbb46SBlue Swirl /* 360 kB 5"1/4 floppy disks */ 2053f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 40, 1, FDRIVE_RATE_300K, }, 2054f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 9, 40, 0, FDRIVE_RATE_300K, }, 2055f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 10, 41, 1, FDRIVE_RATE_300K, }, 2056f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 10, 42, 1, FDRIVE_RATE_300K, }, 20575bbdbb46SBlue Swirl /* 320 kB 5"1/4 floppy disks */ 2058f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 8, 40, 1, FDRIVE_RATE_250K, }, 2059f8d3d128SHervé Poussineau { FDRIVE_DRV_120, 8, 40, 0, FDRIVE_RATE_250K, }, 20605bbdbb46SBlue Swirl /* 360 kB must match 5"1/4 better than 3"1/2... */ 2061f8d3d128SHervé Poussineau { FDRIVE_DRV_144, 9, 80, 0, FDRIVE_RATE_250K, }, 20625bbdbb46SBlue Swirl /* end */ 2063f8d3d128SHervé Poussineau { FDRIVE_DRV_NONE, -1, -1, 0, 0, }, 20645bbdbb46SBlue Swirl }; 20655bbdbb46SBlue Swirl 20665bbdbb46SBlue Swirl void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, 20675bbdbb46SBlue Swirl int *max_track, int *last_sect, 2068f8d3d128SHervé Poussineau FDriveType drive_in, FDriveType *drive, 2069f8d3d128SHervé Poussineau FDriveRate *rate) 20705bbdbb46SBlue Swirl { 20715bbdbb46SBlue Swirl const FDFormat *parse; 20725bbdbb46SBlue Swirl uint64_t nb_sectors, size; 20735bbdbb46SBlue Swirl int i, first_match, match; 20745bbdbb46SBlue Swirl 20755bbdbb46SBlue Swirl bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); 20765bbdbb46SBlue Swirl if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { 20775bbdbb46SBlue Swirl /* User defined disk */ 2078f8d3d128SHervé Poussineau *rate = FDRIVE_RATE_500K; 20795bbdbb46SBlue Swirl } else { 20805bbdbb46SBlue Swirl bdrv_get_geometry(bs, &nb_sectors); 20815bbdbb46SBlue Swirl match = -1; 20825bbdbb46SBlue Swirl first_match = -1; 20835bbdbb46SBlue Swirl for (i = 0; ; i++) { 20845bbdbb46SBlue Swirl parse = &fd_formats[i]; 20855bbdbb46SBlue Swirl if (parse->drive == FDRIVE_DRV_NONE) { 20865bbdbb46SBlue Swirl break; 20875bbdbb46SBlue Swirl } 20885bbdbb46SBlue Swirl if (drive_in == parse->drive || 20895bbdbb46SBlue Swirl drive_in == FDRIVE_DRV_NONE) { 20905bbdbb46SBlue Swirl size = (parse->max_head + 1) * parse->max_track * 20915bbdbb46SBlue Swirl parse->last_sect; 20925bbdbb46SBlue Swirl if (nb_sectors == size) { 20935bbdbb46SBlue Swirl match = i; 20945bbdbb46SBlue Swirl break; 20955bbdbb46SBlue Swirl } 20965bbdbb46SBlue Swirl if (first_match == -1) { 20975bbdbb46SBlue Swirl first_match = i; 20985bbdbb46SBlue Swirl } 20995bbdbb46SBlue Swirl } 21005bbdbb46SBlue Swirl } 21015bbdbb46SBlue Swirl if (match == -1) { 21025bbdbb46SBlue Swirl if (first_match == -1) { 21035bbdbb46SBlue Swirl match = 1; 21045bbdbb46SBlue Swirl } else { 21055bbdbb46SBlue Swirl match = first_match; 21065bbdbb46SBlue Swirl } 21075bbdbb46SBlue Swirl parse = &fd_formats[match]; 21085bbdbb46SBlue Swirl } 21095bbdbb46SBlue Swirl *nb_heads = parse->max_head + 1; 21105bbdbb46SBlue Swirl *max_track = parse->max_track; 21115bbdbb46SBlue Swirl *last_sect = parse->last_sect; 21125bbdbb46SBlue Swirl *drive = parse->drive; 2113f8d3d128SHervé Poussineau *rate = parse->rate; 21145bbdbb46SBlue Swirl } 21155bbdbb46SBlue Swirl } 21165bbdbb46SBlue Swirl 211746d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 211846d4767dSbellard { 211946d4767dSbellard return bs->translation; 212046d4767dSbellard } 212146d4767dSbellard 2122abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 2123abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 2124abd7f68dSMarkus Armbruster { 2125abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2126abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2127abd7f68dSMarkus Armbruster } 2128abd7f68dSMarkus Armbruster 2129abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 2130abd7f68dSMarkus Armbruster { 2131abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2132abd7f68dSMarkus Armbruster } 2133abd7f68dSMarkus Armbruster 2134b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 2135b338082bSbellard { 2136b338082bSbellard return bs->read_only; 2137b338082bSbellard } 2138b338082bSbellard 2139985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 2140985a03b0Sths { 2141985a03b0Sths return bs->sg; 2142985a03b0Sths } 2143985a03b0Sths 2144e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 2145e900a7b7SChristoph Hellwig { 2146e900a7b7SChristoph Hellwig return bs->enable_write_cache; 2147e900a7b7SChristoph Hellwig } 2148e900a7b7SChristoph Hellwig 2149ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 2150ea2384d3Sbellard { 2151ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 2152ea2384d3Sbellard return 1; 2153ea2384d3Sbellard return bs->encrypted; 2154ea2384d3Sbellard } 2155ea2384d3Sbellard 2156c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 2157c0f4ce77Saliguori { 2158c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 2159c0f4ce77Saliguori 2160c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 2161c0f4ce77Saliguori return 1; 2162c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 2163c0f4ce77Saliguori } 2164c0f4ce77Saliguori 2165ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 2166ea2384d3Sbellard { 2167ea2384d3Sbellard int ret; 2168ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 2169ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 2170ea2384d3Sbellard if (ret < 0) 2171ea2384d3Sbellard return ret; 2172ea2384d3Sbellard if (!bs->encrypted) 2173ea2384d3Sbellard return 0; 2174ea2384d3Sbellard } 2175fd04a2aeSShahar Havivi if (!bs->encrypted) { 2176fd04a2aeSShahar Havivi return -EINVAL; 2177fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 2178fd04a2aeSShahar Havivi return -ENOMEDIUM; 2179fd04a2aeSShahar Havivi } 2180c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 2181bb5fc20fSaliguori if (ret < 0) { 2182bb5fc20fSaliguori bs->valid_key = 0; 2183bb5fc20fSaliguori } else if (!bs->valid_key) { 2184bb5fc20fSaliguori bs->valid_key = 1; 2185bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 21867d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 2187bb5fc20fSaliguori } 2188c0f4ce77Saliguori return ret; 2189ea2384d3Sbellard } 2190ea2384d3Sbellard 2191ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 2192ea2384d3Sbellard { 219319cb3738Sbellard if (!bs->drv) { 2194ea2384d3Sbellard buf[0] = '\0'; 2195ea2384d3Sbellard } else { 2196ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 2197ea2384d3Sbellard } 2198ea2384d3Sbellard } 2199ea2384d3Sbellard 2200ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 2201ea2384d3Sbellard void *opaque) 2202ea2384d3Sbellard { 2203ea2384d3Sbellard BlockDriver *drv; 2204ea2384d3Sbellard 22058a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 2206ea2384d3Sbellard it(opaque, drv->format_name); 2207ea2384d3Sbellard } 2208ea2384d3Sbellard } 2209ea2384d3Sbellard 2210b338082bSbellard BlockDriverState *bdrv_find(const char *name) 2211b338082bSbellard { 2212b338082bSbellard BlockDriverState *bs; 2213b338082bSbellard 22141b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 22151b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 2216b338082bSbellard return bs; 2217b338082bSbellard } 22181b7bdbc1SStefan Hajnoczi } 2219b338082bSbellard return NULL; 2220b338082bSbellard } 2221b338082bSbellard 22222f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 22232f399b0aSMarkus Armbruster { 22242f399b0aSMarkus Armbruster if (!bs) { 22252f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 22262f399b0aSMarkus Armbruster } 22272f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 22282f399b0aSMarkus Armbruster } 22292f399b0aSMarkus Armbruster 223051de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 223181d0912dSbellard { 223281d0912dSbellard BlockDriverState *bs; 223381d0912dSbellard 22341b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 223551de9760Saliguori it(opaque, bs); 223681d0912dSbellard } 223781d0912dSbellard } 223881d0912dSbellard 2239ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 2240ea2384d3Sbellard { 2241ea2384d3Sbellard return bs->device_name; 2242ea2384d3Sbellard } 2243ea2384d3Sbellard 2244c6ca28d6Saliguori void bdrv_flush_all(void) 2245c6ca28d6Saliguori { 2246c6ca28d6Saliguori BlockDriverState *bs; 2247c6ca28d6Saliguori 22481b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2249c602a489SMarkus Armbruster if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) { 2250c6ca28d6Saliguori bdrv_flush(bs); 2251c6ca28d6Saliguori } 22521b7bdbc1SStefan Hajnoczi } 22531b7bdbc1SStefan Hajnoczi } 2254c6ca28d6Saliguori 2255f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 2256f2feebbdSKevin Wolf { 2257f2feebbdSKevin Wolf assert(bs->drv); 2258f2feebbdSKevin Wolf 2259336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 2260336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 2261f2feebbdSKevin Wolf } 2262f2feebbdSKevin Wolf 2263f2feebbdSKevin Wolf return 1; 2264f2feebbdSKevin Wolf } 2265f2feebbdSKevin Wolf 2266376ae3f1SStefan Hajnoczi typedef struct BdrvCoIsAllocatedData { 2267376ae3f1SStefan Hajnoczi BlockDriverState *bs; 2268376ae3f1SStefan Hajnoczi int64_t sector_num; 2269376ae3f1SStefan Hajnoczi int nb_sectors; 2270376ae3f1SStefan Hajnoczi int *pnum; 2271376ae3f1SStefan Hajnoczi int ret; 2272376ae3f1SStefan Hajnoczi bool done; 2273376ae3f1SStefan Hajnoczi } BdrvCoIsAllocatedData; 2274376ae3f1SStefan Hajnoczi 2275f58c7b35Sths /* 2276f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 2277f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 2278f58c7b35Sths * hence all their sectors are reported as allocated. 2279f58c7b35Sths * 2280bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 2281bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 2282bd9533e3SStefan Hajnoczi * 2283f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 2284f58c7b35Sths * the specified sector) that are known to be in the same 2285f58c7b35Sths * allocated/unallocated state. 2286f58c7b35Sths * 2287bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 2288bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 2289f58c7b35Sths */ 2290060f51c9SStefan Hajnoczi int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num, 2291060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 2292f58c7b35Sths { 2293f58c7b35Sths int64_t n; 2294bd9533e3SStefan Hajnoczi 22956aebab14SStefan Hajnoczi if (sector_num >= bs->total_sectors) { 22966aebab14SStefan Hajnoczi *pnum = 0; 22976aebab14SStefan Hajnoczi return 0; 22986aebab14SStefan Hajnoczi } 2299bd9533e3SStefan Hajnoczi 23006aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 2301bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 2302bd9533e3SStefan Hajnoczi nb_sectors = n; 2303bd9533e3SStefan Hajnoczi } 2304bd9533e3SStefan Hajnoczi 2305bd9533e3SStefan Hajnoczi if (!bs->drv->bdrv_co_is_allocated) { 2306bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 23076aebab14SStefan Hajnoczi return 1; 23086aebab14SStefan Hajnoczi } 23096aebab14SStefan Hajnoczi 2310060f51c9SStefan Hajnoczi return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum); 2311060f51c9SStefan Hajnoczi } 2312060f51c9SStefan Hajnoczi 2313060f51c9SStefan Hajnoczi /* Coroutine wrapper for bdrv_is_allocated() */ 2314060f51c9SStefan Hajnoczi static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque) 2315060f51c9SStefan Hajnoczi { 2316060f51c9SStefan Hajnoczi BdrvCoIsAllocatedData *data = opaque; 2317060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 2318060f51c9SStefan Hajnoczi 2319060f51c9SStefan Hajnoczi data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors, 2320060f51c9SStefan Hajnoczi data->pnum); 2321060f51c9SStefan Hajnoczi data->done = true; 2322060f51c9SStefan Hajnoczi } 2323060f51c9SStefan Hajnoczi 2324060f51c9SStefan Hajnoczi /* 2325060f51c9SStefan Hajnoczi * Synchronous wrapper around bdrv_co_is_allocated(). 2326060f51c9SStefan Hajnoczi * 2327060f51c9SStefan Hajnoczi * See bdrv_co_is_allocated() for details. 2328060f51c9SStefan Hajnoczi */ 2329060f51c9SStefan Hajnoczi int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 2330060f51c9SStefan Hajnoczi int *pnum) 2331060f51c9SStefan Hajnoczi { 2332376ae3f1SStefan Hajnoczi Coroutine *co; 2333376ae3f1SStefan Hajnoczi BdrvCoIsAllocatedData data = { 2334376ae3f1SStefan Hajnoczi .bs = bs, 2335376ae3f1SStefan Hajnoczi .sector_num = sector_num, 2336376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 2337376ae3f1SStefan Hajnoczi .pnum = pnum, 2338376ae3f1SStefan Hajnoczi .done = false, 2339376ae3f1SStefan Hajnoczi }; 2340376ae3f1SStefan Hajnoczi 2341376ae3f1SStefan Hajnoczi co = qemu_coroutine_create(bdrv_is_allocated_co_entry); 2342376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 2343376ae3f1SStefan Hajnoczi while (!data.done) { 2344376ae3f1SStefan Hajnoczi qemu_aio_wait(); 2345376ae3f1SStefan Hajnoczi } 2346376ae3f1SStefan Hajnoczi return data.ret; 2347376ae3f1SStefan Hajnoczi } 2348f58c7b35Sths 2349b2023818SLuiz Capitulino BlockInfoList *qmp_query_block(Error **errp) 2350b338082bSbellard { 2351b2023818SLuiz Capitulino BlockInfoList *head = NULL, *cur_item = NULL; 2352d15e5465SLuiz Capitulino BlockDriverState *bs; 2353d15e5465SLuiz Capitulino 23541b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2355b2023818SLuiz Capitulino BlockInfoList *info = g_malloc0(sizeof(*info)); 2356d15e5465SLuiz Capitulino 2357b2023818SLuiz Capitulino info->value = g_malloc0(sizeof(*info->value)); 2358b2023818SLuiz Capitulino info->value->device = g_strdup(bs->device_name); 2359b2023818SLuiz Capitulino info->value->type = g_strdup("unknown"); 2360b2023818SLuiz Capitulino info->value->locked = bdrv_dev_is_medium_locked(bs); 2361b2023818SLuiz Capitulino info->value->removable = bdrv_dev_has_removable_media(bs); 2362d15e5465SLuiz Capitulino 2363e4def80bSMarkus Armbruster if (bdrv_dev_has_removable_media(bs)) { 2364b2023818SLuiz Capitulino info->value->has_tray_open = true; 2365b2023818SLuiz Capitulino info->value->tray_open = bdrv_dev_is_tray_open(bs); 2366e4def80bSMarkus Armbruster } 2367f04ef601SLuiz Capitulino 2368f04ef601SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 2369b2023818SLuiz Capitulino info->value->has_io_status = true; 2370b2023818SLuiz Capitulino info->value->io_status = bs->iostatus; 2371f04ef601SLuiz Capitulino } 2372f04ef601SLuiz Capitulino 2373d15e5465SLuiz Capitulino if (bs->drv) { 2374b2023818SLuiz Capitulino info->value->has_inserted = true; 2375b2023818SLuiz Capitulino info->value->inserted = g_malloc0(sizeof(*info->value->inserted)); 2376b2023818SLuiz Capitulino info->value->inserted->file = g_strdup(bs->filename); 2377b2023818SLuiz Capitulino info->value->inserted->ro = bs->read_only; 2378b2023818SLuiz Capitulino info->value->inserted->drv = g_strdup(bs->drv->format_name); 2379b2023818SLuiz Capitulino info->value->inserted->encrypted = bs->encrypted; 2380b2023818SLuiz Capitulino if (bs->backing_file[0]) { 2381b2023818SLuiz Capitulino info->value->inserted->has_backing_file = true; 2382b2023818SLuiz Capitulino info->value->inserted->backing_file = g_strdup(bs->backing_file); 2383b2023818SLuiz Capitulino } 2384727f005eSZhi Yong Wu 2385727f005eSZhi Yong Wu if (bs->io_limits_enabled) { 2386727f005eSZhi Yong Wu info->value->inserted->bps = 2387727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 2388727f005eSZhi Yong Wu info->value->inserted->bps_rd = 2389727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_READ]; 2390727f005eSZhi Yong Wu info->value->inserted->bps_wr = 2391727f005eSZhi Yong Wu bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE]; 2392727f005eSZhi Yong Wu info->value->inserted->iops = 2393727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 2394727f005eSZhi Yong Wu info->value->inserted->iops_rd = 2395727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_READ]; 2396727f005eSZhi Yong Wu info->value->inserted->iops_wr = 2397727f005eSZhi Yong Wu bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE]; 2398727f005eSZhi Yong Wu } 2399d15e5465SLuiz Capitulino } 2400d15e5465SLuiz Capitulino 2401b2023818SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2402b2023818SLuiz Capitulino if (!cur_item) { 2403b2023818SLuiz Capitulino head = cur_item = info; 2404b2023818SLuiz Capitulino } else { 2405b2023818SLuiz Capitulino cur_item->next = info; 2406b2023818SLuiz Capitulino cur_item = info; 2407d15e5465SLuiz Capitulino } 2408d15e5465SLuiz Capitulino } 2409d15e5465SLuiz Capitulino 2410b2023818SLuiz Capitulino return head; 2411b338082bSbellard } 2412a36e69ddSths 2413f11f57e4SLuiz Capitulino /* Consider exposing this as a full fledged QMP command */ 2414f11f57e4SLuiz Capitulino static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp) 2415a36e69ddSths { 2416f11f57e4SLuiz Capitulino BlockStats *s; 2417218a536aSLuiz Capitulino 2418f11f57e4SLuiz Capitulino s = g_malloc0(sizeof(*s)); 2419218a536aSLuiz Capitulino 2420f11f57e4SLuiz Capitulino if (bs->device_name[0]) { 2421f11f57e4SLuiz Capitulino s->has_device = true; 2422f11f57e4SLuiz Capitulino s->device = g_strdup(bs->device_name); 2423218a536aSLuiz Capitulino } 2424218a536aSLuiz Capitulino 2425f11f57e4SLuiz Capitulino s->stats = g_malloc0(sizeof(*s->stats)); 2426f11f57e4SLuiz Capitulino s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ]; 2427f11f57e4SLuiz Capitulino s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE]; 2428f11f57e4SLuiz Capitulino s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ]; 2429f11f57e4SLuiz Capitulino s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE]; 2430f11f57e4SLuiz Capitulino s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE; 2431f11f57e4SLuiz Capitulino s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH]; 2432f11f57e4SLuiz Capitulino s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE]; 2433f11f57e4SLuiz Capitulino s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ]; 2434f11f57e4SLuiz Capitulino s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH]; 2435294cc35fSKevin Wolf 2436294cc35fSKevin Wolf if (bs->file) { 2437f11f57e4SLuiz Capitulino s->has_parent = true; 2438f11f57e4SLuiz Capitulino s->parent = qmp_query_blockstat(bs->file, NULL); 2439294cc35fSKevin Wolf } 2440294cc35fSKevin Wolf 2441f11f57e4SLuiz Capitulino return s; 2442294cc35fSKevin Wolf } 2443294cc35fSKevin Wolf 2444f11f57e4SLuiz Capitulino BlockStatsList *qmp_query_blockstats(Error **errp) 2445218a536aSLuiz Capitulino { 2446f11f57e4SLuiz Capitulino BlockStatsList *head = NULL, *cur_item = NULL; 2447a36e69ddSths BlockDriverState *bs; 2448a36e69ddSths 24491b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 2450f11f57e4SLuiz Capitulino BlockStatsList *info = g_malloc0(sizeof(*info)); 2451f11f57e4SLuiz Capitulino info->value = qmp_query_blockstat(bs, NULL); 2452f11f57e4SLuiz Capitulino 2453f11f57e4SLuiz Capitulino /* XXX: waiting for the qapi to support GSList */ 2454f11f57e4SLuiz Capitulino if (!cur_item) { 2455f11f57e4SLuiz Capitulino head = cur_item = info; 2456f11f57e4SLuiz Capitulino } else { 2457f11f57e4SLuiz Capitulino cur_item->next = info; 2458f11f57e4SLuiz Capitulino cur_item = info; 2459f11f57e4SLuiz Capitulino } 2460a36e69ddSths } 2461218a536aSLuiz Capitulino 2462f11f57e4SLuiz Capitulino return head; 2463a36e69ddSths } 2464ea2384d3Sbellard 2465045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 2466045df330Saliguori { 2467045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 2468045df330Saliguori return bs->backing_file; 2469045df330Saliguori else if (bs->encrypted) 2470045df330Saliguori return bs->filename; 2471045df330Saliguori else 2472045df330Saliguori return NULL; 2473045df330Saliguori } 2474045df330Saliguori 247583f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 247683f64091Sbellard char *filename, int filename_size) 247783f64091Sbellard { 247883f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 247983f64091Sbellard } 248083f64091Sbellard 2481faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 2482faea38e7Sbellard const uint8_t *buf, int nb_sectors) 2483faea38e7Sbellard { 2484faea38e7Sbellard BlockDriver *drv = bs->drv; 2485faea38e7Sbellard if (!drv) 248619cb3738Sbellard return -ENOMEDIUM; 2487faea38e7Sbellard if (!drv->bdrv_write_compressed) 2488faea38e7Sbellard return -ENOTSUP; 2489fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 2490fbb7b4e0SKevin Wolf return -EIO; 24917cd1e32aSlirans@il.ibm.com 2492c6d22830SJan Kiszka if (bs->dirty_bitmap) { 24937cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 24947cd1e32aSlirans@il.ibm.com } 24957cd1e32aSlirans@il.ibm.com 2496faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 2497faea38e7Sbellard } 2498faea38e7Sbellard 2499faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 2500faea38e7Sbellard { 2501faea38e7Sbellard BlockDriver *drv = bs->drv; 2502faea38e7Sbellard if (!drv) 250319cb3738Sbellard return -ENOMEDIUM; 2504faea38e7Sbellard if (!drv->bdrv_get_info) 2505faea38e7Sbellard return -ENOTSUP; 2506faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 2507faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 2508faea38e7Sbellard } 2509faea38e7Sbellard 251045566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 251145566e9cSChristoph Hellwig int64_t pos, int size) 2512178e08a5Saliguori { 2513178e08a5Saliguori BlockDriver *drv = bs->drv; 2514178e08a5Saliguori if (!drv) 2515178e08a5Saliguori return -ENOMEDIUM; 25167cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 251745566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 25187cdb1f6dSMORITA Kazutaka if (bs->file) 25197cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 25207cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2521178e08a5Saliguori } 2522178e08a5Saliguori 252345566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 252445566e9cSChristoph Hellwig int64_t pos, int size) 2525178e08a5Saliguori { 2526178e08a5Saliguori BlockDriver *drv = bs->drv; 2527178e08a5Saliguori if (!drv) 2528178e08a5Saliguori return -ENOMEDIUM; 25297cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 253045566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 25317cdb1f6dSMORITA Kazutaka if (bs->file) 25327cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 25337cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2534178e08a5Saliguori } 2535178e08a5Saliguori 25368b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 25378b9b0cc2SKevin Wolf { 25388b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 25398b9b0cc2SKevin Wolf 25408b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 25418b9b0cc2SKevin Wolf return; 25428b9b0cc2SKevin Wolf } 25438b9b0cc2SKevin Wolf 25448b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 25458b9b0cc2SKevin Wolf 25468b9b0cc2SKevin Wolf } 25478b9b0cc2SKevin Wolf 2548faea38e7Sbellard /**************************************************************/ 2549faea38e7Sbellard /* handling of snapshots */ 2550faea38e7Sbellard 2551feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 2552feeee5acSMiguel Di Ciurcio Filho { 2553feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 255407b70bfbSMarkus Armbruster if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 2555feeee5acSMiguel Di Ciurcio Filho return 0; 2556feeee5acSMiguel Di Ciurcio Filho } 2557feeee5acSMiguel Di Ciurcio Filho 2558feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 2559feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 2560feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 2561feeee5acSMiguel Di Ciurcio Filho } 2562feeee5acSMiguel Di Ciurcio Filho return 0; 2563feeee5acSMiguel Di Ciurcio Filho } 2564feeee5acSMiguel Di Ciurcio Filho 2565feeee5acSMiguel Di Ciurcio Filho return 1; 2566feeee5acSMiguel Di Ciurcio Filho } 2567feeee5acSMiguel Di Ciurcio Filho 2568199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 2569199630b6SBlue Swirl { 2570199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 2571199630b6SBlue Swirl } 2572199630b6SBlue Swirl 2573f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 2574f9092b10SMarkus Armbruster { 2575f9092b10SMarkus Armbruster BlockDriverState *bs; 2576f9092b10SMarkus Armbruster 25773ac906f7SMarkus Armbruster if (bs_snapshots) { 2578f9092b10SMarkus Armbruster return bs_snapshots; 25793ac906f7SMarkus Armbruster } 2580f9092b10SMarkus Armbruster 2581f9092b10SMarkus Armbruster bs = NULL; 2582f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 2583f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 25843ac906f7SMarkus Armbruster bs_snapshots = bs; 25853ac906f7SMarkus Armbruster return bs; 2586f9092b10SMarkus Armbruster } 2587f9092b10SMarkus Armbruster } 2588f9092b10SMarkus Armbruster return NULL; 2589f9092b10SMarkus Armbruster } 2590f9092b10SMarkus Armbruster 2591faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 2592faea38e7Sbellard QEMUSnapshotInfo *sn_info) 2593faea38e7Sbellard { 2594faea38e7Sbellard BlockDriver *drv = bs->drv; 2595faea38e7Sbellard if (!drv) 259619cb3738Sbellard return -ENOMEDIUM; 25977cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 2598faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 25997cdb1f6dSMORITA Kazutaka if (bs->file) 26007cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 26017cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2602faea38e7Sbellard } 2603faea38e7Sbellard 2604faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 2605faea38e7Sbellard const char *snapshot_id) 2606faea38e7Sbellard { 2607faea38e7Sbellard BlockDriver *drv = bs->drv; 26087cdb1f6dSMORITA Kazutaka int ret, open_ret; 26097cdb1f6dSMORITA Kazutaka 2610faea38e7Sbellard if (!drv) 261119cb3738Sbellard return -ENOMEDIUM; 26127cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 2613faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 26147cdb1f6dSMORITA Kazutaka 26157cdb1f6dSMORITA Kazutaka if (bs->file) { 26167cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 26177cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 26187cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 26197cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 26207cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 26217cdb1f6dSMORITA Kazutaka bs->drv = NULL; 26227cdb1f6dSMORITA Kazutaka return open_ret; 26237cdb1f6dSMORITA Kazutaka } 26247cdb1f6dSMORITA Kazutaka return ret; 26257cdb1f6dSMORITA Kazutaka } 26267cdb1f6dSMORITA Kazutaka 26277cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2628faea38e7Sbellard } 2629faea38e7Sbellard 2630faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 2631faea38e7Sbellard { 2632faea38e7Sbellard BlockDriver *drv = bs->drv; 2633faea38e7Sbellard if (!drv) 263419cb3738Sbellard return -ENOMEDIUM; 26357cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 2636faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 26377cdb1f6dSMORITA Kazutaka if (bs->file) 26387cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 26397cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2640faea38e7Sbellard } 2641faea38e7Sbellard 2642faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 2643faea38e7Sbellard QEMUSnapshotInfo **psn_info) 2644faea38e7Sbellard { 2645faea38e7Sbellard BlockDriver *drv = bs->drv; 2646faea38e7Sbellard if (!drv) 264719cb3738Sbellard return -ENOMEDIUM; 26487cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 2649faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 26507cdb1f6dSMORITA Kazutaka if (bs->file) 26517cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 26527cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2653faea38e7Sbellard } 2654faea38e7Sbellard 265551ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 265651ef6727Sedison const char *snapshot_name) 265751ef6727Sedison { 265851ef6727Sedison BlockDriver *drv = bs->drv; 265951ef6727Sedison if (!drv) { 266051ef6727Sedison return -ENOMEDIUM; 266151ef6727Sedison } 266251ef6727Sedison if (!bs->read_only) { 266351ef6727Sedison return -EINVAL; 266451ef6727Sedison } 266551ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 266651ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 266751ef6727Sedison } 266851ef6727Sedison return -ENOTSUP; 266951ef6727Sedison } 267051ef6727Sedison 2671e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 2672e8a6bb9cSMarcelo Tosatti const char *backing_file) 2673e8a6bb9cSMarcelo Tosatti { 2674e8a6bb9cSMarcelo Tosatti if (!bs->drv) { 2675e8a6bb9cSMarcelo Tosatti return NULL; 2676e8a6bb9cSMarcelo Tosatti } 2677e8a6bb9cSMarcelo Tosatti 2678e8a6bb9cSMarcelo Tosatti if (bs->backing_hd) { 2679e8a6bb9cSMarcelo Tosatti if (strcmp(bs->backing_file, backing_file) == 0) { 2680e8a6bb9cSMarcelo Tosatti return bs->backing_hd; 2681e8a6bb9cSMarcelo Tosatti } else { 2682e8a6bb9cSMarcelo Tosatti return bdrv_find_backing_image(bs->backing_hd, backing_file); 2683e8a6bb9cSMarcelo Tosatti } 2684e8a6bb9cSMarcelo Tosatti } 2685e8a6bb9cSMarcelo Tosatti 2686e8a6bb9cSMarcelo Tosatti return NULL; 2687e8a6bb9cSMarcelo Tosatti } 2688e8a6bb9cSMarcelo Tosatti 2689faea38e7Sbellard #define NB_SUFFIXES 4 2690faea38e7Sbellard 2691faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 2692faea38e7Sbellard { 2693faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 2694faea38e7Sbellard int64_t base; 2695faea38e7Sbellard int i; 2696faea38e7Sbellard 2697faea38e7Sbellard if (size <= 999) { 2698faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 2699faea38e7Sbellard } else { 2700faea38e7Sbellard base = 1024; 2701faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 2702faea38e7Sbellard if (size < (10 * base)) { 2703faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 2704faea38e7Sbellard (double)size / base, 2705faea38e7Sbellard suffixes[i]); 2706faea38e7Sbellard break; 2707faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 2708faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 2709faea38e7Sbellard ((size + (base >> 1)) / base), 2710faea38e7Sbellard suffixes[i]); 2711faea38e7Sbellard break; 2712faea38e7Sbellard } 2713faea38e7Sbellard base = base * 1024; 2714faea38e7Sbellard } 2715faea38e7Sbellard } 2716faea38e7Sbellard return buf; 2717faea38e7Sbellard } 2718faea38e7Sbellard 2719faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 2720faea38e7Sbellard { 2721faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 27223b9f94e1Sbellard #ifdef _WIN32 27233b9f94e1Sbellard struct tm *ptm; 27243b9f94e1Sbellard #else 2725faea38e7Sbellard struct tm tm; 27263b9f94e1Sbellard #endif 2727faea38e7Sbellard time_t ti; 2728faea38e7Sbellard int64_t secs; 2729faea38e7Sbellard 2730faea38e7Sbellard if (!sn) { 2731faea38e7Sbellard snprintf(buf, buf_size, 2732faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2733faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 2734faea38e7Sbellard } else { 2735faea38e7Sbellard ti = sn->date_sec; 27363b9f94e1Sbellard #ifdef _WIN32 27373b9f94e1Sbellard ptm = localtime(&ti); 27383b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 27393b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 27403b9f94e1Sbellard #else 2741faea38e7Sbellard localtime_r(&ti, &tm); 2742faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 2743faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 27443b9f94e1Sbellard #endif 2745faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 2746faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 2747faea38e7Sbellard "%02d:%02d:%02d.%03d", 2748faea38e7Sbellard (int)(secs / 3600), 2749faea38e7Sbellard (int)((secs / 60) % 60), 2750faea38e7Sbellard (int)(secs % 60), 2751faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 2752faea38e7Sbellard snprintf(buf, buf_size, 2753faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2754faea38e7Sbellard sn->id_str, sn->name, 2755faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 2756faea38e7Sbellard date_buf, 2757faea38e7Sbellard clock_buf); 2758faea38e7Sbellard } 2759faea38e7Sbellard return buf; 2760faea38e7Sbellard } 2761faea38e7Sbellard 2762ea2384d3Sbellard /**************************************************************/ 276383f64091Sbellard /* async I/Os */ 2764ea2384d3Sbellard 27653b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 2766f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 276783f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 2768ea2384d3Sbellard { 2769bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 2770bbf0a440SStefan Hajnoczi 2771b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 27728c5873d6SStefan Hajnoczi cb, opaque, false); 277383f64091Sbellard } 277483f64091Sbellard 2775f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 2776f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 277783f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 27787674e7bfSbellard { 2779bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 2780bbf0a440SStefan Hajnoczi 27811a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 27828c5873d6SStefan Hajnoczi cb, opaque, true); 278383f64091Sbellard } 278483f64091Sbellard 278540b4f539SKevin Wolf 278640b4f539SKevin Wolf typedef struct MultiwriteCB { 278740b4f539SKevin Wolf int error; 278840b4f539SKevin Wolf int num_requests; 278940b4f539SKevin Wolf int num_callbacks; 279040b4f539SKevin Wolf struct { 279140b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 279240b4f539SKevin Wolf void *opaque; 279340b4f539SKevin Wolf QEMUIOVector *free_qiov; 279440b4f539SKevin Wolf } callbacks[]; 279540b4f539SKevin Wolf } MultiwriteCB; 279640b4f539SKevin Wolf 279740b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 279840b4f539SKevin Wolf { 279940b4f539SKevin Wolf int i; 280040b4f539SKevin Wolf 280140b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 280240b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 28031e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 28041e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 28051e1ea48dSStefan Hajnoczi } 28067267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 280740b4f539SKevin Wolf } 280840b4f539SKevin Wolf } 280940b4f539SKevin Wolf 281040b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 281140b4f539SKevin Wolf { 281240b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 281340b4f539SKevin Wolf 28146d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 28156d519a5fSStefan Hajnoczi 2816cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 281740b4f539SKevin Wolf mcb->error = ret; 281840b4f539SKevin Wolf } 281940b4f539SKevin Wolf 282040b4f539SKevin Wolf mcb->num_requests--; 282140b4f539SKevin Wolf if (mcb->num_requests == 0) { 282240b4f539SKevin Wolf multiwrite_user_cb(mcb); 28237267c094SAnthony Liguori g_free(mcb); 282440b4f539SKevin Wolf } 282540b4f539SKevin Wolf } 282640b4f539SKevin Wolf 282740b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 282840b4f539SKevin Wolf { 282977be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 283077be4366SChristoph Hellwig 283177be4366SChristoph Hellwig /* 283277be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 283377be4366SChristoph Hellwig * here as that could overflow the return value. 283477be4366SChristoph Hellwig */ 283577be4366SChristoph Hellwig if (req1->sector > req2->sector) { 283677be4366SChristoph Hellwig return 1; 283777be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 283877be4366SChristoph Hellwig return -1; 283977be4366SChristoph Hellwig } else { 284077be4366SChristoph Hellwig return 0; 284177be4366SChristoph Hellwig } 284240b4f539SKevin Wolf } 284340b4f539SKevin Wolf 284440b4f539SKevin Wolf /* 284540b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 284640b4f539SKevin Wolf * requests that remain after merging. 284740b4f539SKevin Wolf */ 284840b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 284940b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 285040b4f539SKevin Wolf { 285140b4f539SKevin Wolf int i, outidx; 285240b4f539SKevin Wolf 285340b4f539SKevin Wolf // Sort requests by start sector 285440b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 285540b4f539SKevin Wolf 285640b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 285740b4f539SKevin Wolf // filling up gaps with zero sectors. 285840b4f539SKevin Wolf outidx = 0; 285940b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 286040b4f539SKevin Wolf int merge = 0; 286140b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 286240b4f539SKevin Wolf 2863*b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 286440b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 286540b4f539SKevin Wolf merge = 1; 286640b4f539SKevin Wolf } 286740b4f539SKevin Wolf 2868e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 2869e2a305fbSChristoph Hellwig merge = 0; 2870e2a305fbSChristoph Hellwig } 2871e2a305fbSChristoph Hellwig 287240b4f539SKevin Wolf if (merge) { 287340b4f539SKevin Wolf size_t size; 28747267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 287540b4f539SKevin Wolf qemu_iovec_init(qiov, 287640b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 287740b4f539SKevin Wolf 287840b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 287940b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 288040b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 288140b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 288240b4f539SKevin Wolf 2883*b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 2884*b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 288540b4f539SKevin Wolf 288640b4f539SKevin Wolf // Add the second request 288740b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 288840b4f539SKevin Wolf 2889cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 289040b4f539SKevin Wolf reqs[outidx].qiov = qiov; 289140b4f539SKevin Wolf 289240b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 289340b4f539SKevin Wolf } else { 289440b4f539SKevin Wolf outidx++; 289540b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 289640b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 289740b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 289840b4f539SKevin Wolf } 289940b4f539SKevin Wolf } 290040b4f539SKevin Wolf 290140b4f539SKevin Wolf return outidx + 1; 290240b4f539SKevin Wolf } 290340b4f539SKevin Wolf 290440b4f539SKevin Wolf /* 290540b4f539SKevin Wolf * Submit multiple AIO write requests at once. 290640b4f539SKevin Wolf * 290740b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 290840b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 290940b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 291040b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 291140b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 291240b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 291340b4f539SKevin Wolf * 291440b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 291540b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 291640b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 291740b4f539SKevin Wolf */ 291840b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 291940b4f539SKevin Wolf { 292040b4f539SKevin Wolf MultiwriteCB *mcb; 292140b4f539SKevin Wolf int i; 292240b4f539SKevin Wolf 2923301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 2924301db7c2SRyan Harper if (bs->drv == NULL) { 2925301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 2926301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 2927301db7c2SRyan Harper } 2928301db7c2SRyan Harper return -1; 2929301db7c2SRyan Harper } 2930301db7c2SRyan Harper 293140b4f539SKevin Wolf if (num_reqs == 0) { 293240b4f539SKevin Wolf return 0; 293340b4f539SKevin Wolf } 293440b4f539SKevin Wolf 293540b4f539SKevin Wolf // Create MultiwriteCB structure 29367267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 293740b4f539SKevin Wolf mcb->num_requests = 0; 293840b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 293940b4f539SKevin Wolf 294040b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 294140b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 294240b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 294340b4f539SKevin Wolf } 294440b4f539SKevin Wolf 294540b4f539SKevin Wolf // Check for mergable requests 294640b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 294740b4f539SKevin Wolf 29486d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 29496d519a5fSStefan Hajnoczi 2950df9309fbSPaolo Bonzini /* Run the aio requests. */ 2951df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 295240b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 2953ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 295440b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 295540b4f539SKevin Wolf } 295640b4f539SKevin Wolf 295740b4f539SKevin Wolf return 0; 295840b4f539SKevin Wolf } 295940b4f539SKevin Wolf 296083f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 296183f64091Sbellard { 29626bbff9a0Saliguori acb->pool->cancel(acb); 296383f64091Sbellard } 296483f64091Sbellard 296598f90dbaSZhi Yong Wu /* block I/O throttling */ 296698f90dbaSZhi Yong Wu static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors, 296798f90dbaSZhi Yong Wu bool is_write, double elapsed_time, uint64_t *wait) 296898f90dbaSZhi Yong Wu { 296998f90dbaSZhi Yong Wu uint64_t bps_limit = 0; 297098f90dbaSZhi Yong Wu double bytes_limit, bytes_base, bytes_res; 297198f90dbaSZhi Yong Wu double slice_time, wait_time; 297298f90dbaSZhi Yong Wu 297398f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 297498f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]; 297598f90dbaSZhi Yong Wu } else if (bs->io_limits.bps[is_write]) { 297698f90dbaSZhi Yong Wu bps_limit = bs->io_limits.bps[is_write]; 297798f90dbaSZhi Yong Wu } else { 297898f90dbaSZhi Yong Wu if (wait) { 297998f90dbaSZhi Yong Wu *wait = 0; 298098f90dbaSZhi Yong Wu } 298198f90dbaSZhi Yong Wu 298298f90dbaSZhi Yong Wu return false; 298398f90dbaSZhi Yong Wu } 298498f90dbaSZhi Yong Wu 298598f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 298698f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 298798f90dbaSZhi Yong Wu bytes_limit = bps_limit * slice_time; 298898f90dbaSZhi Yong Wu bytes_base = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write]; 298998f90dbaSZhi Yong Wu if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) { 299098f90dbaSZhi Yong Wu bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write]; 299198f90dbaSZhi Yong Wu } 299298f90dbaSZhi Yong Wu 299398f90dbaSZhi Yong Wu /* bytes_base: the bytes of data which have been read/written; and 299498f90dbaSZhi Yong Wu * it is obtained from the history statistic info. 299598f90dbaSZhi Yong Wu * bytes_res: the remaining bytes of data which need to be read/written. 299698f90dbaSZhi Yong Wu * (bytes_base + bytes_res) / bps_limit: used to calcuate 299798f90dbaSZhi Yong Wu * the total time for completing reading/writting all data. 299898f90dbaSZhi Yong Wu */ 299998f90dbaSZhi Yong Wu bytes_res = (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 300098f90dbaSZhi Yong Wu 300198f90dbaSZhi Yong Wu if (bytes_base + bytes_res <= bytes_limit) { 300298f90dbaSZhi Yong Wu if (wait) { 300398f90dbaSZhi Yong Wu *wait = 0; 300498f90dbaSZhi Yong Wu } 300598f90dbaSZhi Yong Wu 300698f90dbaSZhi Yong Wu return false; 300798f90dbaSZhi Yong Wu } 300898f90dbaSZhi Yong Wu 300998f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 301098f90dbaSZhi Yong Wu wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time; 301198f90dbaSZhi Yong Wu 301298f90dbaSZhi Yong Wu /* When the I/O rate at runtime exceeds the limits, 301398f90dbaSZhi Yong Wu * bs->slice_end need to be extended in order that the current statistic 301498f90dbaSZhi Yong Wu * info can be kept until the timer fire, so it is increased and tuned 301598f90dbaSZhi Yong Wu * based on the result of experiment. 301698f90dbaSZhi Yong Wu */ 301798f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 301898f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 301998f90dbaSZhi Yong Wu if (wait) { 302098f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 302198f90dbaSZhi Yong Wu } 302298f90dbaSZhi Yong Wu 302398f90dbaSZhi Yong Wu return true; 302498f90dbaSZhi Yong Wu } 302598f90dbaSZhi Yong Wu 302698f90dbaSZhi Yong Wu static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write, 302798f90dbaSZhi Yong Wu double elapsed_time, uint64_t *wait) 302898f90dbaSZhi Yong Wu { 302998f90dbaSZhi Yong Wu uint64_t iops_limit = 0; 303098f90dbaSZhi Yong Wu double ios_limit, ios_base; 303198f90dbaSZhi Yong Wu double slice_time, wait_time; 303298f90dbaSZhi Yong Wu 303398f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 303498f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]; 303598f90dbaSZhi Yong Wu } else if (bs->io_limits.iops[is_write]) { 303698f90dbaSZhi Yong Wu iops_limit = bs->io_limits.iops[is_write]; 303798f90dbaSZhi Yong Wu } else { 303898f90dbaSZhi Yong Wu if (wait) { 303998f90dbaSZhi Yong Wu *wait = 0; 304098f90dbaSZhi Yong Wu } 304198f90dbaSZhi Yong Wu 304298f90dbaSZhi Yong Wu return false; 304398f90dbaSZhi Yong Wu } 304498f90dbaSZhi Yong Wu 304598f90dbaSZhi Yong Wu slice_time = bs->slice_end - bs->slice_start; 304698f90dbaSZhi Yong Wu slice_time /= (NANOSECONDS_PER_SECOND); 304798f90dbaSZhi Yong Wu ios_limit = iops_limit * slice_time; 304898f90dbaSZhi Yong Wu ios_base = bs->nr_ops[is_write] - bs->io_base.ios[is_write]; 304998f90dbaSZhi Yong Wu if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) { 305098f90dbaSZhi Yong Wu ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write]; 305198f90dbaSZhi Yong Wu } 305298f90dbaSZhi Yong Wu 305398f90dbaSZhi Yong Wu if (ios_base + 1 <= ios_limit) { 305498f90dbaSZhi Yong Wu if (wait) { 305598f90dbaSZhi Yong Wu *wait = 0; 305698f90dbaSZhi Yong Wu } 305798f90dbaSZhi Yong Wu 305898f90dbaSZhi Yong Wu return false; 305998f90dbaSZhi Yong Wu } 306098f90dbaSZhi Yong Wu 306198f90dbaSZhi Yong Wu /* Calc approx time to dispatch */ 306298f90dbaSZhi Yong Wu wait_time = (ios_base + 1) / iops_limit; 306398f90dbaSZhi Yong Wu if (wait_time > elapsed_time) { 306498f90dbaSZhi Yong Wu wait_time = wait_time - elapsed_time; 306598f90dbaSZhi Yong Wu } else { 306698f90dbaSZhi Yong Wu wait_time = 0; 306798f90dbaSZhi Yong Wu } 306898f90dbaSZhi Yong Wu 306998f90dbaSZhi Yong Wu bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10; 307098f90dbaSZhi Yong Wu bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME; 307198f90dbaSZhi Yong Wu if (wait) { 307298f90dbaSZhi Yong Wu *wait = wait_time * BLOCK_IO_SLICE_TIME * 10; 307398f90dbaSZhi Yong Wu } 307498f90dbaSZhi Yong Wu 307598f90dbaSZhi Yong Wu return true; 307698f90dbaSZhi Yong Wu } 307798f90dbaSZhi Yong Wu 307898f90dbaSZhi Yong Wu static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors, 307998f90dbaSZhi Yong Wu bool is_write, int64_t *wait) 308098f90dbaSZhi Yong Wu { 308198f90dbaSZhi Yong Wu int64_t now, max_wait; 308298f90dbaSZhi Yong Wu uint64_t bps_wait = 0, iops_wait = 0; 308398f90dbaSZhi Yong Wu double elapsed_time; 308498f90dbaSZhi Yong Wu int bps_ret, iops_ret; 308598f90dbaSZhi Yong Wu 308698f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 308798f90dbaSZhi Yong Wu if ((bs->slice_start < now) 308898f90dbaSZhi Yong Wu && (bs->slice_end > now)) { 308998f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 309098f90dbaSZhi Yong Wu } else { 309198f90dbaSZhi Yong Wu bs->slice_time = 5 * BLOCK_IO_SLICE_TIME; 309298f90dbaSZhi Yong Wu bs->slice_start = now; 309398f90dbaSZhi Yong Wu bs->slice_end = now + bs->slice_time; 309498f90dbaSZhi Yong Wu 309598f90dbaSZhi Yong Wu bs->io_base.bytes[is_write] = bs->nr_bytes[is_write]; 309698f90dbaSZhi Yong Wu bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write]; 309798f90dbaSZhi Yong Wu 309898f90dbaSZhi Yong Wu bs->io_base.ios[is_write] = bs->nr_ops[is_write]; 309998f90dbaSZhi Yong Wu bs->io_base.ios[!is_write] = bs->nr_ops[!is_write]; 310098f90dbaSZhi Yong Wu } 310198f90dbaSZhi Yong Wu 310298f90dbaSZhi Yong Wu elapsed_time = now - bs->slice_start; 310398f90dbaSZhi Yong Wu elapsed_time /= (NANOSECONDS_PER_SECOND); 310498f90dbaSZhi Yong Wu 310598f90dbaSZhi Yong Wu bps_ret = bdrv_exceed_bps_limits(bs, nb_sectors, 310698f90dbaSZhi Yong Wu is_write, elapsed_time, &bps_wait); 310798f90dbaSZhi Yong Wu iops_ret = bdrv_exceed_iops_limits(bs, is_write, 310898f90dbaSZhi Yong Wu elapsed_time, &iops_wait); 310998f90dbaSZhi Yong Wu if (bps_ret || iops_ret) { 311098f90dbaSZhi Yong Wu max_wait = bps_wait > iops_wait ? bps_wait : iops_wait; 311198f90dbaSZhi Yong Wu if (wait) { 311298f90dbaSZhi Yong Wu *wait = max_wait; 311398f90dbaSZhi Yong Wu } 311498f90dbaSZhi Yong Wu 311598f90dbaSZhi Yong Wu now = qemu_get_clock_ns(vm_clock); 311698f90dbaSZhi Yong Wu if (bs->slice_end < now + max_wait) { 311798f90dbaSZhi Yong Wu bs->slice_end = now + max_wait; 311898f90dbaSZhi Yong Wu } 311998f90dbaSZhi Yong Wu 312098f90dbaSZhi Yong Wu return true; 312198f90dbaSZhi Yong Wu } 312298f90dbaSZhi Yong Wu 312398f90dbaSZhi Yong Wu if (wait) { 312498f90dbaSZhi Yong Wu *wait = 0; 312598f90dbaSZhi Yong Wu } 312698f90dbaSZhi Yong Wu 312798f90dbaSZhi Yong Wu return false; 312898f90dbaSZhi Yong Wu } 312983f64091Sbellard 313083f64091Sbellard /**************************************************************/ 313183f64091Sbellard /* async block device emulation */ 313283f64091Sbellard 3133c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3134c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3135c16b5a2cSChristoph Hellwig QEMUBH *bh; 3136c16b5a2cSChristoph Hellwig int ret; 3137c16b5a2cSChristoph Hellwig /* vector translation state */ 3138c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3139c16b5a2cSChristoph Hellwig uint8_t *bounce; 3140c16b5a2cSChristoph Hellwig int is_write; 3141c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3142c16b5a2cSChristoph Hellwig 3143c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3144c16b5a2cSChristoph Hellwig { 3145b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3146b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 31476a7ad299SDor Laor qemu_bh_delete(acb->bh); 314836afc451SAvi Kivity acb->bh = NULL; 3149c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3150c16b5a2cSChristoph Hellwig } 3151c16b5a2cSChristoph Hellwig 3152c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 3153c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3154c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3155c16b5a2cSChristoph Hellwig }; 3156c16b5a2cSChristoph Hellwig 315783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3158beac80cdSbellard { 3159ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3160f141eafeSaliguori 3161f141eafeSaliguori if (!acb->is_write) 3162f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 3163ceb42de8Saliguori qemu_vfree(acb->bounce); 3164ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 31656a7ad299SDor Laor qemu_bh_delete(acb->bh); 316636afc451SAvi Kivity acb->bh = NULL; 3167ce1a14dcSpbrook qemu_aio_release(acb); 3168beac80cdSbellard } 3169beac80cdSbellard 3170f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3171f141eafeSaliguori int64_t sector_num, 3172f141eafeSaliguori QEMUIOVector *qiov, 3173f141eafeSaliguori int nb_sectors, 3174f141eafeSaliguori BlockDriverCompletionFunc *cb, 3175f141eafeSaliguori void *opaque, 3176f141eafeSaliguori int is_write) 3177f141eafeSaliguori 3178ea2384d3Sbellard { 3179ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 318083f64091Sbellard 3181c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 3182f141eafeSaliguori acb->is_write = is_write; 3183f141eafeSaliguori acb->qiov = qiov; 3184e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3185ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3186f141eafeSaliguori 3187f141eafeSaliguori if (is_write) { 3188f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 31891ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3190f141eafeSaliguori } else { 31911ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3192f141eafeSaliguori } 3193f141eafeSaliguori 3194ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3195f141eafeSaliguori 3196ce1a14dcSpbrook return &acb->common; 31977a6cba61Spbrook } 31987a6cba61Spbrook 3199f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3200f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3201ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 320283f64091Sbellard { 3203f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 320483f64091Sbellard } 320583f64091Sbellard 3206f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3207f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3208f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3209f141eafeSaliguori { 3210f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3211f141eafeSaliguori } 3212f141eafeSaliguori 321368485420SKevin Wolf 321468485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 321568485420SKevin Wolf BlockDriverAIOCB common; 321668485420SKevin Wolf BlockRequest req; 321768485420SKevin Wolf bool is_write; 321868485420SKevin Wolf QEMUBH* bh; 321968485420SKevin Wolf } BlockDriverAIOCBCoroutine; 322068485420SKevin Wolf 322168485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 322268485420SKevin Wolf { 322368485420SKevin Wolf qemu_aio_flush(); 322468485420SKevin Wolf } 322568485420SKevin Wolf 322668485420SKevin Wolf static AIOPool bdrv_em_co_aio_pool = { 322768485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 322868485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 322968485420SKevin Wolf }; 323068485420SKevin Wolf 323135246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 323268485420SKevin Wolf { 323368485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 323468485420SKevin Wolf 323568485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 323668485420SKevin Wolf qemu_bh_delete(acb->bh); 323768485420SKevin Wolf qemu_aio_release(acb); 323868485420SKevin Wolf } 323968485420SKevin Wolf 3240b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3241b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3242b2a61371SStefan Hajnoczi { 3243b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3244b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3245b2a61371SStefan Hajnoczi 3246b2a61371SStefan Hajnoczi if (!acb->is_write) { 3247b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 3248470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3249b2a61371SStefan Hajnoczi } else { 3250b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 3251f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 3252b2a61371SStefan Hajnoczi } 3253b2a61371SStefan Hajnoczi 325435246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3255b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 3256b2a61371SStefan Hajnoczi } 3257b2a61371SStefan Hajnoczi 325868485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 325968485420SKevin Wolf int64_t sector_num, 326068485420SKevin Wolf QEMUIOVector *qiov, 326168485420SKevin Wolf int nb_sectors, 326268485420SKevin Wolf BlockDriverCompletionFunc *cb, 326368485420SKevin Wolf void *opaque, 32648c5873d6SStefan Hajnoczi bool is_write) 326568485420SKevin Wolf { 326668485420SKevin Wolf Coroutine *co; 326768485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 326868485420SKevin Wolf 326968485420SKevin Wolf acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 327068485420SKevin Wolf acb->req.sector = sector_num; 327168485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 327268485420SKevin Wolf acb->req.qiov = qiov; 327368485420SKevin Wolf acb->is_write = is_write; 327468485420SKevin Wolf 32758c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 327668485420SKevin Wolf qemu_coroutine_enter(co, acb); 327768485420SKevin Wolf 327868485420SKevin Wolf return &acb->common; 327968485420SKevin Wolf } 328068485420SKevin Wolf 328107f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 3282b2e12bc6SChristoph Hellwig { 328307f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 328407f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 3285b2e12bc6SChristoph Hellwig 328607f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 328707f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 3288b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 3289b2e12bc6SChristoph Hellwig } 3290b2e12bc6SChristoph Hellwig 329107f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 3292016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 3293016f5cf6SAlexander Graf { 329407f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 3295016f5cf6SAlexander Graf 329607f07615SPaolo Bonzini Coroutine *co; 329707f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 3298016f5cf6SAlexander Graf 329907f07615SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 330007f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 330107f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 3302016f5cf6SAlexander Graf 3303016f5cf6SAlexander Graf return &acb->common; 3304016f5cf6SAlexander Graf } 3305016f5cf6SAlexander Graf 33064265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 33074265d620SPaolo Bonzini { 33084265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 33094265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 33104265d620SPaolo Bonzini 33114265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 33124265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 33134265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 33144265d620SPaolo Bonzini } 33154265d620SPaolo Bonzini 33164265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 33174265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 33184265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 33194265d620SPaolo Bonzini { 33204265d620SPaolo Bonzini Coroutine *co; 33214265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 33224265d620SPaolo Bonzini 33234265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 33244265d620SPaolo Bonzini 33254265d620SPaolo Bonzini acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 33264265d620SPaolo Bonzini acb->req.sector = sector_num; 33274265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 33284265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 33294265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 33304265d620SPaolo Bonzini 33314265d620SPaolo Bonzini return &acb->common; 33324265d620SPaolo Bonzini } 33334265d620SPaolo Bonzini 3334ea2384d3Sbellard void bdrv_init(void) 3335ea2384d3Sbellard { 33365efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 3337ea2384d3Sbellard } 3338ce1a14dcSpbrook 3339eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 3340eb852011SMarkus Armbruster { 3341eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 3342eb852011SMarkus Armbruster bdrv_init(); 3343eb852011SMarkus Armbruster } 3344eb852011SMarkus Armbruster 3345c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 33466bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 33476bbff9a0Saliguori { 3348ce1a14dcSpbrook BlockDriverAIOCB *acb; 3349ce1a14dcSpbrook 33506bbff9a0Saliguori if (pool->free_aiocb) { 33516bbff9a0Saliguori acb = pool->free_aiocb; 33526bbff9a0Saliguori pool->free_aiocb = acb->next; 3353ce1a14dcSpbrook } else { 33547267c094SAnthony Liguori acb = g_malloc0(pool->aiocb_size); 33556bbff9a0Saliguori acb->pool = pool; 3356ce1a14dcSpbrook } 3357ce1a14dcSpbrook acb->bs = bs; 3358ce1a14dcSpbrook acb->cb = cb; 3359ce1a14dcSpbrook acb->opaque = opaque; 3360ce1a14dcSpbrook return acb; 3361ce1a14dcSpbrook } 3362ce1a14dcSpbrook 3363ce1a14dcSpbrook void qemu_aio_release(void *p) 3364ce1a14dcSpbrook { 33656bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 33666bbff9a0Saliguori AIOPool *pool = acb->pool; 33676bbff9a0Saliguori acb->next = pool->free_aiocb; 33686bbff9a0Saliguori pool->free_aiocb = acb; 3369ce1a14dcSpbrook } 337019cb3738Sbellard 337119cb3738Sbellard /**************************************************************/ 3372f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 3373f9f05dc5SKevin Wolf 3374f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 3375f9f05dc5SKevin Wolf Coroutine *coroutine; 3376f9f05dc5SKevin Wolf int ret; 3377f9f05dc5SKevin Wolf } CoroutineIOCompletion; 3378f9f05dc5SKevin Wolf 3379f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 3380f9f05dc5SKevin Wolf { 3381f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 3382f9f05dc5SKevin Wolf 3383f9f05dc5SKevin Wolf co->ret = ret; 3384f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 3385f9f05dc5SKevin Wolf } 3386f9f05dc5SKevin Wolf 3387f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 3388f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 3389f9f05dc5SKevin Wolf bool is_write) 3390f9f05dc5SKevin Wolf { 3391f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 3392f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 3393f9f05dc5SKevin Wolf }; 3394f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 3395f9f05dc5SKevin Wolf 3396f9f05dc5SKevin Wolf if (is_write) { 3397a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 3398f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3399f9f05dc5SKevin Wolf } else { 3400a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 3401f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 3402f9f05dc5SKevin Wolf } 3403f9f05dc5SKevin Wolf 340459370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 3405f9f05dc5SKevin Wolf if (!acb) { 3406f9f05dc5SKevin Wolf return -EIO; 3407f9f05dc5SKevin Wolf } 3408f9f05dc5SKevin Wolf qemu_coroutine_yield(); 3409f9f05dc5SKevin Wolf 3410f9f05dc5SKevin Wolf return co.ret; 3411f9f05dc5SKevin Wolf } 3412f9f05dc5SKevin Wolf 3413f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 3414f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3415f9f05dc5SKevin Wolf QEMUIOVector *iov) 3416f9f05dc5SKevin Wolf { 3417f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 3418f9f05dc5SKevin Wolf } 3419f9f05dc5SKevin Wolf 3420f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 3421f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 3422f9f05dc5SKevin Wolf QEMUIOVector *iov) 3423f9f05dc5SKevin Wolf { 3424f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 3425f9f05dc5SKevin Wolf } 3426f9f05dc5SKevin Wolf 342707f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 3428e7a8a783SKevin Wolf { 342907f07615SPaolo Bonzini RwCo *rwco = opaque; 343007f07615SPaolo Bonzini 343107f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 343207f07615SPaolo Bonzini } 343307f07615SPaolo Bonzini 343407f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 343507f07615SPaolo Bonzini { 3436eb489bb1SKevin Wolf int ret; 3437eb489bb1SKevin Wolf 3438ca716364SKevin Wolf if (!bs->drv) { 343907f07615SPaolo Bonzini return 0; 3440eb489bb1SKevin Wolf } 3441eb489bb1SKevin Wolf 3442ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 3443eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 3444eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 3445eb489bb1SKevin Wolf if (ret < 0) { 3446eb489bb1SKevin Wolf return ret; 3447eb489bb1SKevin Wolf } 3448eb489bb1SKevin Wolf } 3449eb489bb1SKevin Wolf 3450ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 3451ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 3452ca716364SKevin Wolf return 0; 3453ca716364SKevin Wolf } 3454ca716364SKevin Wolf 3455eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 3456c68b89acSKevin Wolf return bs->drv->bdrv_co_flush_to_disk(bs); 345707f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 345807f07615SPaolo Bonzini BlockDriverAIOCB *acb; 3459e7a8a783SKevin Wolf CoroutineIOCompletion co = { 3460e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 3461e7a8a783SKevin Wolf }; 3462e7a8a783SKevin Wolf 346307f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 346407f07615SPaolo Bonzini if (acb == NULL) { 3465e7a8a783SKevin Wolf return -EIO; 346607f07615SPaolo Bonzini } else { 3467e7a8a783SKevin Wolf qemu_coroutine_yield(); 3468e7a8a783SKevin Wolf return co.ret; 3469e7a8a783SKevin Wolf } 347007f07615SPaolo Bonzini } else { 347107f07615SPaolo Bonzini /* 347207f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 347307f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 347407f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 347507f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 347607f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 347707f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 347807f07615SPaolo Bonzini * mode. 347907f07615SPaolo Bonzini * 348007f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 348107f07615SPaolo Bonzini */ 348207f07615SPaolo Bonzini return 0; 348307f07615SPaolo Bonzini } 348407f07615SPaolo Bonzini } 348507f07615SPaolo Bonzini 34860f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 34870f15423cSAnthony Liguori { 34880f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 34890f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 34900f15423cSAnthony Liguori } 34910f15423cSAnthony Liguori } 34920f15423cSAnthony Liguori 34930f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 34940f15423cSAnthony Liguori { 34950f15423cSAnthony Liguori BlockDriverState *bs; 34960f15423cSAnthony Liguori 34970f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 34980f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 34990f15423cSAnthony Liguori } 35000f15423cSAnthony Liguori } 35010f15423cSAnthony Liguori 350207f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 350307f07615SPaolo Bonzini { 350407f07615SPaolo Bonzini Coroutine *co; 350507f07615SPaolo Bonzini RwCo rwco = { 350607f07615SPaolo Bonzini .bs = bs, 350707f07615SPaolo Bonzini .ret = NOT_DONE, 350807f07615SPaolo Bonzini }; 350907f07615SPaolo Bonzini 351007f07615SPaolo Bonzini if (qemu_in_coroutine()) { 351107f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 351207f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 351307f07615SPaolo Bonzini } else { 351407f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 351507f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 351607f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 351707f07615SPaolo Bonzini qemu_aio_wait(); 351807f07615SPaolo Bonzini } 351907f07615SPaolo Bonzini } 352007f07615SPaolo Bonzini 352107f07615SPaolo Bonzini return rwco.ret; 352207f07615SPaolo Bonzini } 3523e7a8a783SKevin Wolf 35244265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 35254265d620SPaolo Bonzini { 35264265d620SPaolo Bonzini RwCo *rwco = opaque; 35274265d620SPaolo Bonzini 35284265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 35294265d620SPaolo Bonzini } 35304265d620SPaolo Bonzini 35314265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 35324265d620SPaolo Bonzini int nb_sectors) 35334265d620SPaolo Bonzini { 35344265d620SPaolo Bonzini if (!bs->drv) { 35354265d620SPaolo Bonzini return -ENOMEDIUM; 35364265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 35374265d620SPaolo Bonzini return -EIO; 35384265d620SPaolo Bonzini } else if (bs->read_only) { 35394265d620SPaolo Bonzini return -EROFS; 35404265d620SPaolo Bonzini } else if (bs->drv->bdrv_co_discard) { 35414265d620SPaolo Bonzini return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors); 35424265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 35434265d620SPaolo Bonzini BlockDriverAIOCB *acb; 35444265d620SPaolo Bonzini CoroutineIOCompletion co = { 35454265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 35464265d620SPaolo Bonzini }; 35474265d620SPaolo Bonzini 35484265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 35494265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 35504265d620SPaolo Bonzini if (acb == NULL) { 35514265d620SPaolo Bonzini return -EIO; 35524265d620SPaolo Bonzini } else { 35534265d620SPaolo Bonzini qemu_coroutine_yield(); 35544265d620SPaolo Bonzini return co.ret; 35554265d620SPaolo Bonzini } 35564265d620SPaolo Bonzini } else { 35574265d620SPaolo Bonzini return 0; 35584265d620SPaolo Bonzini } 35594265d620SPaolo Bonzini } 35604265d620SPaolo Bonzini 35614265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 35624265d620SPaolo Bonzini { 35634265d620SPaolo Bonzini Coroutine *co; 35644265d620SPaolo Bonzini RwCo rwco = { 35654265d620SPaolo Bonzini .bs = bs, 35664265d620SPaolo Bonzini .sector_num = sector_num, 35674265d620SPaolo Bonzini .nb_sectors = nb_sectors, 35684265d620SPaolo Bonzini .ret = NOT_DONE, 35694265d620SPaolo Bonzini }; 35704265d620SPaolo Bonzini 35714265d620SPaolo Bonzini if (qemu_in_coroutine()) { 35724265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 35734265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 35744265d620SPaolo Bonzini } else { 35754265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 35764265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 35774265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 35784265d620SPaolo Bonzini qemu_aio_wait(); 35794265d620SPaolo Bonzini } 35804265d620SPaolo Bonzini } 35814265d620SPaolo Bonzini 35824265d620SPaolo Bonzini return rwco.ret; 35834265d620SPaolo Bonzini } 35844265d620SPaolo Bonzini 3585f9f05dc5SKevin Wolf /**************************************************************/ 358619cb3738Sbellard /* removable device support */ 358719cb3738Sbellard 358819cb3738Sbellard /** 358919cb3738Sbellard * Return TRUE if the media is present 359019cb3738Sbellard */ 359119cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 359219cb3738Sbellard { 359319cb3738Sbellard BlockDriver *drv = bs->drv; 3594a1aff5bfSMarkus Armbruster 359519cb3738Sbellard if (!drv) 359619cb3738Sbellard return 0; 359719cb3738Sbellard if (!drv->bdrv_is_inserted) 3598a1aff5bfSMarkus Armbruster return 1; 3599a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 360019cb3738Sbellard } 360119cb3738Sbellard 360219cb3738Sbellard /** 36038e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 36048e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 360519cb3738Sbellard */ 360619cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 360719cb3738Sbellard { 360819cb3738Sbellard BlockDriver *drv = bs->drv; 360919cb3738Sbellard 36108e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 36118e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 36128e49ca46SMarkus Armbruster } 36138e49ca46SMarkus Armbruster return -ENOTSUP; 361419cb3738Sbellard } 361519cb3738Sbellard 361619cb3738Sbellard /** 361719cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 361819cb3738Sbellard */ 3619f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 362019cb3738Sbellard { 362119cb3738Sbellard BlockDriver *drv = bs->drv; 362219cb3738Sbellard 3623822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 3624822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 362519cb3738Sbellard } 36266f382ed2SLuiz Capitulino 36276f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 36286f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 36296f382ed2SLuiz Capitulino } 363019cb3738Sbellard } 363119cb3738Sbellard 363219cb3738Sbellard /** 363319cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 363419cb3738Sbellard * to eject it manually). 363519cb3738Sbellard */ 3636025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 363719cb3738Sbellard { 363819cb3738Sbellard BlockDriver *drv = bs->drv; 363919cb3738Sbellard 3640025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 3641b8c6d095SStefan Hajnoczi 3642025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 3643025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 364419cb3738Sbellard } 364519cb3738Sbellard } 3646985a03b0Sths 3647985a03b0Sths /* needed for generic scsi interface */ 3648985a03b0Sths 3649985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 3650985a03b0Sths { 3651985a03b0Sths BlockDriver *drv = bs->drv; 3652985a03b0Sths 3653985a03b0Sths if (drv && drv->bdrv_ioctl) 3654985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 3655985a03b0Sths return -ENOTSUP; 3656985a03b0Sths } 36577d780669Saliguori 3658221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 3659221f715dSaliguori unsigned long int req, void *buf, 36607d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 36617d780669Saliguori { 3662221f715dSaliguori BlockDriver *drv = bs->drv; 36637d780669Saliguori 3664221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 3665221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 3666221f715dSaliguori return NULL; 36677d780669Saliguori } 3668e268ca52Saliguori 36697b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 36707b6f9300SMarkus Armbruster { 36717b6f9300SMarkus Armbruster bs->buffer_alignment = align; 36727b6f9300SMarkus Armbruster } 36737cd1e32aSlirans@il.ibm.com 3674e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 3675e268ca52Saliguori { 3676e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 3677e268ca52Saliguori } 36787cd1e32aSlirans@il.ibm.com 36797cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 36807cd1e32aSlirans@il.ibm.com { 36817cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 3682a55eb92cSJan Kiszka 3683aaa0eb75SLiran Schour bs->dirty_count = 0; 36847cd1e32aSlirans@il.ibm.com if (enable) { 3685c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 3686c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 3687c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 3688c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 36897cd1e32aSlirans@il.ibm.com 36907267c094SAnthony Liguori bs->dirty_bitmap = g_malloc0(bitmap_size); 36917cd1e32aSlirans@il.ibm.com } 36927cd1e32aSlirans@il.ibm.com } else { 3693c6d22830SJan Kiszka if (bs->dirty_bitmap) { 36947267c094SAnthony Liguori g_free(bs->dirty_bitmap); 3695c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 36967cd1e32aSlirans@il.ibm.com } 36977cd1e32aSlirans@il.ibm.com } 36987cd1e32aSlirans@il.ibm.com } 36997cd1e32aSlirans@il.ibm.com 37007cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 37017cd1e32aSlirans@il.ibm.com { 37026ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 37037cd1e32aSlirans@il.ibm.com 3704c6d22830SJan Kiszka if (bs->dirty_bitmap && 3705c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 37066d59fec1SMarcelo Tosatti return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 37076d59fec1SMarcelo Tosatti (1UL << (chunk % (sizeof(unsigned long) * 8)))); 37087cd1e32aSlirans@il.ibm.com } else { 37097cd1e32aSlirans@il.ibm.com return 0; 37107cd1e32aSlirans@il.ibm.com } 37117cd1e32aSlirans@il.ibm.com } 37127cd1e32aSlirans@il.ibm.com 37137cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 37147cd1e32aSlirans@il.ibm.com int nr_sectors) 37157cd1e32aSlirans@il.ibm.com { 37167cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 37177cd1e32aSlirans@il.ibm.com } 3718aaa0eb75SLiran Schour 3719aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 3720aaa0eb75SLiran Schour { 3721aaa0eb75SLiran Schour return bs->dirty_count; 3722aaa0eb75SLiran Schour } 3723f88e1a42SJes Sorensen 3724db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 3725db593f25SMarcelo Tosatti { 3726db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 3727db593f25SMarcelo Tosatti bs->in_use = in_use; 3728db593f25SMarcelo Tosatti } 3729db593f25SMarcelo Tosatti 3730db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 3731db593f25SMarcelo Tosatti { 3732db593f25SMarcelo Tosatti return bs->in_use; 3733db593f25SMarcelo Tosatti } 3734db593f25SMarcelo Tosatti 373528a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 373628a7282aSLuiz Capitulino { 3737d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 373858e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 373928a7282aSLuiz Capitulino } 374028a7282aSLuiz Capitulino 374128a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 374228a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 374328a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 374428a7282aSLuiz Capitulino { 3745d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 374628a7282aSLuiz Capitulino (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC || 374728a7282aSLuiz Capitulino bs->on_write_error == BLOCK_ERR_STOP_ANY || 374828a7282aSLuiz Capitulino bs->on_read_error == BLOCK_ERR_STOP_ANY)); 374928a7282aSLuiz Capitulino } 375028a7282aSLuiz Capitulino 375128a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 375228a7282aSLuiz Capitulino { 3753d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 375428a7282aSLuiz Capitulino } 375528a7282aSLuiz Capitulino 375628a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 375728a7282aSLuiz Capitulino { 375828a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 375958e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 376028a7282aSLuiz Capitulino } 376128a7282aSLuiz Capitulino } 376228a7282aSLuiz Capitulino 376328a7282aSLuiz Capitulino /* XXX: Today this is set by device models because it makes the implementation 376428a7282aSLuiz Capitulino quite simple. However, the block layer knows about the error, so it's 376528a7282aSLuiz Capitulino possible to implement this without device models being involved */ 376628a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 376728a7282aSLuiz Capitulino { 376858e21ef5SLuiz Capitulino if (bdrv_iostatus_is_enabled(bs) && 376958e21ef5SLuiz Capitulino bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 377028a7282aSLuiz Capitulino assert(error >= 0); 377158e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 377258e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 377328a7282aSLuiz Capitulino } 377428a7282aSLuiz Capitulino } 377528a7282aSLuiz Capitulino 3776a597e79cSChristoph Hellwig void 3777a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 3778a597e79cSChristoph Hellwig enum BlockAcctType type) 3779a597e79cSChristoph Hellwig { 3780a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 3781a597e79cSChristoph Hellwig 3782a597e79cSChristoph Hellwig cookie->bytes = bytes; 3783c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 3784a597e79cSChristoph Hellwig cookie->type = type; 3785a597e79cSChristoph Hellwig } 3786a597e79cSChristoph Hellwig 3787a597e79cSChristoph Hellwig void 3788a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 3789a597e79cSChristoph Hellwig { 3790a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 3791a597e79cSChristoph Hellwig 3792a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 3793a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 3794c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 3795a597e79cSChristoph Hellwig } 3796a597e79cSChristoph Hellwig 3797f88e1a42SJes Sorensen int bdrv_img_create(const char *filename, const char *fmt, 3798f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 3799f88e1a42SJes Sorensen char *options, uint64_t img_size, int flags) 3800f88e1a42SJes Sorensen { 3801f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 3802d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 3803f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 3804f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 380596df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 3806f88e1a42SJes Sorensen int ret = 0; 3807f88e1a42SJes Sorensen 3808f88e1a42SJes Sorensen /* Find driver and parse its options */ 3809f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 3810f88e1a42SJes Sorensen if (!drv) { 3811f88e1a42SJes Sorensen error_report("Unknown file format '%s'", fmt); 38124f70f249SJes Sorensen ret = -EINVAL; 3813f88e1a42SJes Sorensen goto out; 3814f88e1a42SJes Sorensen } 3815f88e1a42SJes Sorensen 3816f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 3817f88e1a42SJes Sorensen if (!proto_drv) { 3818f88e1a42SJes Sorensen error_report("Unknown protocol '%s'", filename); 38194f70f249SJes Sorensen ret = -EINVAL; 3820f88e1a42SJes Sorensen goto out; 3821f88e1a42SJes Sorensen } 3822f88e1a42SJes Sorensen 3823f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 3824f88e1a42SJes Sorensen drv->create_options); 3825f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 3826f88e1a42SJes Sorensen proto_drv->create_options); 3827f88e1a42SJes Sorensen 3828f88e1a42SJes Sorensen /* Create parameter list with default values */ 3829f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 3830f88e1a42SJes Sorensen 3831f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 3832f88e1a42SJes Sorensen 3833f88e1a42SJes Sorensen /* Parse -o options */ 3834f88e1a42SJes Sorensen if (options) { 3835f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 3836f88e1a42SJes Sorensen if (param == NULL) { 3837f88e1a42SJes Sorensen error_report("Invalid options for file format '%s'.", fmt); 38384f70f249SJes Sorensen ret = -EINVAL; 3839f88e1a42SJes Sorensen goto out; 3840f88e1a42SJes Sorensen } 3841f88e1a42SJes Sorensen } 3842f88e1a42SJes Sorensen 3843f88e1a42SJes Sorensen if (base_filename) { 3844f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 3845f88e1a42SJes Sorensen base_filename)) { 3846f88e1a42SJes Sorensen error_report("Backing file not supported for file format '%s'", 3847f88e1a42SJes Sorensen fmt); 38484f70f249SJes Sorensen ret = -EINVAL; 3849f88e1a42SJes Sorensen goto out; 3850f88e1a42SJes Sorensen } 3851f88e1a42SJes Sorensen } 3852f88e1a42SJes Sorensen 3853f88e1a42SJes Sorensen if (base_fmt) { 3854f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 3855f88e1a42SJes Sorensen error_report("Backing file format not supported for file " 3856f88e1a42SJes Sorensen "format '%s'", fmt); 38574f70f249SJes Sorensen ret = -EINVAL; 3858f88e1a42SJes Sorensen goto out; 3859f88e1a42SJes Sorensen } 3860f88e1a42SJes Sorensen } 3861f88e1a42SJes Sorensen 3862792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 3863792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 3864792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 3865792da93aSJes Sorensen error_report("Error: Trying to create an image with the " 3866792da93aSJes Sorensen "same filename as the backing file"); 38674f70f249SJes Sorensen ret = -EINVAL; 3868792da93aSJes Sorensen goto out; 3869792da93aSJes Sorensen } 3870792da93aSJes Sorensen } 3871792da93aSJes Sorensen 3872f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 3873f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 387496df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 387596df67d1SStefan Hajnoczi if (!backing_drv) { 3876f88e1a42SJes Sorensen error_report("Unknown backing file format '%s'", 3877f88e1a42SJes Sorensen backing_fmt->value.s); 38784f70f249SJes Sorensen ret = -EINVAL; 3879f88e1a42SJes Sorensen goto out; 3880f88e1a42SJes Sorensen } 3881f88e1a42SJes Sorensen } 3882f88e1a42SJes Sorensen 3883f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 3884f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 3885d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 3886d220894eSKevin Wolf if (size && size->value.n == -1) { 3887f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 3888f88e1a42SJes Sorensen uint64_t size; 3889f88e1a42SJes Sorensen char buf[32]; 3890f88e1a42SJes Sorensen 3891f88e1a42SJes Sorensen bs = bdrv_new(""); 3892f88e1a42SJes Sorensen 389396df67d1SStefan Hajnoczi ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); 3894f88e1a42SJes Sorensen if (ret < 0) { 389596df67d1SStefan Hajnoczi error_report("Could not open '%s'", backing_file->value.s); 3896f88e1a42SJes Sorensen goto out; 3897f88e1a42SJes Sorensen } 3898f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 3899f88e1a42SJes Sorensen size *= 512; 3900f88e1a42SJes Sorensen 3901f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 3902f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 3903f88e1a42SJes Sorensen } else { 3904f88e1a42SJes Sorensen error_report("Image creation needs a size parameter"); 39054f70f249SJes Sorensen ret = -EINVAL; 3906f88e1a42SJes Sorensen goto out; 3907f88e1a42SJes Sorensen } 3908f88e1a42SJes Sorensen } 3909f88e1a42SJes Sorensen 3910f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 3911f88e1a42SJes Sorensen print_option_parameters(param); 3912f88e1a42SJes Sorensen puts(""); 3913f88e1a42SJes Sorensen 3914f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 3915f88e1a42SJes Sorensen 3916f88e1a42SJes Sorensen if (ret < 0) { 3917f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 3918f88e1a42SJes Sorensen error_report("Formatting or formatting option not supported for " 3919f88e1a42SJes Sorensen "file format '%s'", fmt); 3920f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 3921f88e1a42SJes Sorensen error_report("The image size is too large for file format '%s'", 3922f88e1a42SJes Sorensen fmt); 3923f88e1a42SJes Sorensen } else { 3924f88e1a42SJes Sorensen error_report("%s: error while creating %s: %s", filename, fmt, 3925f88e1a42SJes Sorensen strerror(-ret)); 3926f88e1a42SJes Sorensen } 3927f88e1a42SJes Sorensen } 3928f88e1a42SJes Sorensen 3929f88e1a42SJes Sorensen out: 3930f88e1a42SJes Sorensen free_option_parameters(create_options); 3931f88e1a42SJes Sorensen free_option_parameters(param); 3932f88e1a42SJes Sorensen 3933f88e1a42SJes Sorensen if (bs) { 3934f88e1a42SJes Sorensen bdrv_delete(bs); 3935f88e1a42SJes Sorensen } 39364f70f249SJes Sorensen 39374f70f249SJes Sorensen return ret; 3938f88e1a42SJes Sorensen } 3939eeec61f2SStefan Hajnoczi 3940eeec61f2SStefan Hajnoczi void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs, 3941eeec61f2SStefan Hajnoczi BlockDriverCompletionFunc *cb, void *opaque) 3942eeec61f2SStefan Hajnoczi { 3943eeec61f2SStefan Hajnoczi BlockJob *job; 3944eeec61f2SStefan Hajnoczi 3945eeec61f2SStefan Hajnoczi if (bs->job || bdrv_in_use(bs)) { 3946eeec61f2SStefan Hajnoczi return NULL; 3947eeec61f2SStefan Hajnoczi } 3948eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 1); 3949eeec61f2SStefan Hajnoczi 3950eeec61f2SStefan Hajnoczi job = g_malloc0(job_type->instance_size); 3951eeec61f2SStefan Hajnoczi job->job_type = job_type; 3952eeec61f2SStefan Hajnoczi job->bs = bs; 3953eeec61f2SStefan Hajnoczi job->cb = cb; 3954eeec61f2SStefan Hajnoczi job->opaque = opaque; 3955eeec61f2SStefan Hajnoczi bs->job = job; 3956eeec61f2SStefan Hajnoczi return job; 3957eeec61f2SStefan Hajnoczi } 3958eeec61f2SStefan Hajnoczi 3959eeec61f2SStefan Hajnoczi void block_job_complete(BlockJob *job, int ret) 3960eeec61f2SStefan Hajnoczi { 3961eeec61f2SStefan Hajnoczi BlockDriverState *bs = job->bs; 3962eeec61f2SStefan Hajnoczi 3963eeec61f2SStefan Hajnoczi assert(bs->job == job); 3964eeec61f2SStefan Hajnoczi job->cb(job->opaque, ret); 3965eeec61f2SStefan Hajnoczi bs->job = NULL; 3966eeec61f2SStefan Hajnoczi g_free(job); 3967eeec61f2SStefan Hajnoczi bdrv_set_in_use(bs, 0); 3968eeec61f2SStefan Hajnoczi } 3969eeec61f2SStefan Hajnoczi 3970eeec61f2SStefan Hajnoczi int block_job_set_speed(BlockJob *job, int64_t value) 3971eeec61f2SStefan Hajnoczi { 3972eeec61f2SStefan Hajnoczi if (!job->job_type->set_speed) { 3973eeec61f2SStefan Hajnoczi return -ENOTSUP; 3974eeec61f2SStefan Hajnoczi } 3975eeec61f2SStefan Hajnoczi return job->job_type->set_speed(job, value); 3976eeec61f2SStefan Hajnoczi } 3977eeec61f2SStefan Hajnoczi 3978eeec61f2SStefan Hajnoczi void block_job_cancel(BlockJob *job) 3979eeec61f2SStefan Hajnoczi { 3980eeec61f2SStefan Hajnoczi job->cancelled = true; 3981eeec61f2SStefan Hajnoczi } 3982eeec61f2SStefan Hajnoczi 3983eeec61f2SStefan Hajnoczi bool block_job_is_cancelled(BlockJob *job) 3984eeec61f2SStefan Hajnoczi { 3985eeec61f2SStefan Hajnoczi return job->cancelled; 3986eeec61f2SStefan Hajnoczi } 3987