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" 27737e150eSPaolo Bonzini #include "block/block_int.h" 28737e150eSPaolo Bonzini #include "block/blockjob.h" 291de7afc9SPaolo Bonzini #include "qemu/module.h" 307b1b5d19SPaolo Bonzini #include "qapi/qmp/qjson.h" 319c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 321de7afc9SPaolo Bonzini #include "qemu/notify.h" 33737e150eSPaolo Bonzini #include "block/coroutine.h" 34c13163fbSBenoît Canet #include "block/qapi.h" 35b2023818SLuiz Capitulino #include "qmp-commands.h" 361de7afc9SPaolo Bonzini #include "qemu/timer.h" 37a5ee7bd4SWenchao Xia #include "qapi-event.h" 38fc01f7e7Sbellard 3971e72a19SJuan Quintela #ifdef CONFIG_BSD 407674e7bfSbellard #include <sys/types.h> 417674e7bfSbellard #include <sys/stat.h> 427674e7bfSbellard #include <sys/ioctl.h> 4372cf2d4fSBlue Swirl #include <sys/queue.h> 44c5e97233Sblueswir1 #ifndef __DragonFly__ 457674e7bfSbellard #include <sys/disk.h> 467674e7bfSbellard #endif 47c5e97233Sblueswir1 #endif 487674e7bfSbellard 4949dc768dSaliguori #ifdef _WIN32 5049dc768dSaliguori #include <windows.h> 5149dc768dSaliguori #endif 5249dc768dSaliguori 53e4654d2dSFam Zheng struct BdrvDirtyBitmap { 54e4654d2dSFam Zheng HBitmap *bitmap; 55e4654d2dSFam Zheng QLIST_ENTRY(BdrvDirtyBitmap) list; 56e4654d2dSFam Zheng }; 57e4654d2dSFam Zheng 581c9805a3SStefan Hajnoczi #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 591c9805a3SStefan Hajnoczi 607d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); 61f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 62f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 63c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 64f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 65f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 66ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 67f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 68f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 69f9f05dc5SKevin Wolf QEMUIOVector *iov); 70f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 71f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 72f9f05dc5SKevin Wolf QEMUIOVector *iov); 73775aa8b6SKevin Wolf static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs, 74775aa8b6SKevin Wolf int64_t offset, unsigned int bytes, QEMUIOVector *qiov, 75470c0504SStefan Hajnoczi BdrvRequestFlags flags); 76775aa8b6SKevin Wolf static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs, 77775aa8b6SKevin Wolf int64_t offset, unsigned int bytes, QEMUIOVector *qiov, 78f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags); 79b2a61371SStefan Hajnoczi static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 80b2a61371SStefan Hajnoczi int64_t sector_num, 81b2a61371SStefan Hajnoczi QEMUIOVector *qiov, 82b2a61371SStefan Hajnoczi int nb_sectors, 83d20d9b7cSPaolo Bonzini BdrvRequestFlags flags, 84b2a61371SStefan Hajnoczi BlockDriverCompletionFunc *cb, 85b2a61371SStefan Hajnoczi void *opaque, 868c5873d6SStefan Hajnoczi bool is_write); 87b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque); 88621f0589SKevin Wolf static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 89aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags); 90ec530c81Sbellard 911b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 921b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 937ee930d0Sblueswir1 94dc364f4cSBenoît Canet static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = 95dc364f4cSBenoît Canet QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); 96dc364f4cSBenoît Canet 978a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 988a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 99ea2384d3Sbellard 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 */ 124cc0681c4SBenoît Canet void bdrv_set_io_limits(BlockDriverState *bs, 125cc0681c4SBenoît Canet ThrottleConfig *cfg) 126cc0681c4SBenoît Canet { 127cc0681c4SBenoît Canet int i; 128cc0681c4SBenoît Canet 129cc0681c4SBenoît Canet throttle_config(&bs->throttle_state, cfg); 130cc0681c4SBenoît Canet 131cc0681c4SBenoît Canet for (i = 0; i < 2; i++) { 132cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[i]); 133cc0681c4SBenoît Canet } 134cc0681c4SBenoît Canet } 135cc0681c4SBenoît Canet 136cc0681c4SBenoît Canet /* this function drain all the throttled IOs */ 137cc0681c4SBenoît Canet static bool bdrv_start_throttled_reqs(BlockDriverState *bs) 138cc0681c4SBenoît Canet { 139cc0681c4SBenoît Canet bool drained = false; 140cc0681c4SBenoît Canet bool enabled = bs->io_limits_enabled; 141cc0681c4SBenoît Canet int i; 142cc0681c4SBenoît Canet 143cc0681c4SBenoît Canet bs->io_limits_enabled = false; 144cc0681c4SBenoît Canet 145cc0681c4SBenoît Canet for (i = 0; i < 2; i++) { 146cc0681c4SBenoît Canet while (qemu_co_enter_next(&bs->throttled_reqs[i])) { 147cc0681c4SBenoît Canet drained = true; 148cc0681c4SBenoît Canet } 149cc0681c4SBenoît Canet } 150cc0681c4SBenoît Canet 151cc0681c4SBenoît Canet bs->io_limits_enabled = enabled; 152cc0681c4SBenoît Canet 153cc0681c4SBenoît Canet return drained; 154cc0681c4SBenoît Canet } 155cc0681c4SBenoît Canet 15698f90dbaSZhi Yong Wu void bdrv_io_limits_disable(BlockDriverState *bs) 15798f90dbaSZhi Yong Wu { 15898f90dbaSZhi Yong Wu bs->io_limits_enabled = false; 15998f90dbaSZhi Yong Wu 160cc0681c4SBenoît Canet bdrv_start_throttled_reqs(bs); 16198f90dbaSZhi Yong Wu 162cc0681c4SBenoît Canet throttle_destroy(&bs->throttle_state); 16398f90dbaSZhi Yong Wu } 16498f90dbaSZhi Yong Wu 165cc0681c4SBenoît Canet static void bdrv_throttle_read_timer_cb(void *opaque) 1660563e191SZhi Yong Wu { 1670563e191SZhi Yong Wu BlockDriverState *bs = opaque; 168cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[0]); 1690563e191SZhi Yong Wu } 1700563e191SZhi Yong Wu 171cc0681c4SBenoît Canet static void bdrv_throttle_write_timer_cb(void *opaque) 172cc0681c4SBenoît Canet { 173cc0681c4SBenoît Canet BlockDriverState *bs = opaque; 174cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[1]); 175cc0681c4SBenoît Canet } 176cc0681c4SBenoît Canet 177cc0681c4SBenoît Canet /* should be called before bdrv_set_io_limits if a limit is set */ 1780563e191SZhi Yong Wu void bdrv_io_limits_enable(BlockDriverState *bs) 1790563e191SZhi Yong Wu { 180cc0681c4SBenoît Canet assert(!bs->io_limits_enabled); 181cc0681c4SBenoît Canet throttle_init(&bs->throttle_state, 18213af91ebSStefan Hajnoczi bdrv_get_aio_context(bs), 183cc0681c4SBenoît Canet QEMU_CLOCK_VIRTUAL, 184cc0681c4SBenoît Canet bdrv_throttle_read_timer_cb, 185cc0681c4SBenoît Canet bdrv_throttle_write_timer_cb, 186cc0681c4SBenoît Canet bs); 1870563e191SZhi Yong Wu bs->io_limits_enabled = true; 1880563e191SZhi Yong Wu } 1890563e191SZhi Yong Wu 190cc0681c4SBenoît Canet /* This function makes an IO wait if needed 191cc0681c4SBenoît Canet * 192cc0681c4SBenoît Canet * @nb_sectors: the number of sectors of the IO 193cc0681c4SBenoît Canet * @is_write: is the IO a write 19498f90dbaSZhi Yong Wu */ 195cc0681c4SBenoît Canet static void bdrv_io_limits_intercept(BlockDriverState *bs, 196d5103588SKevin Wolf unsigned int bytes, 197cc0681c4SBenoît Canet bool is_write) 198cc0681c4SBenoît Canet { 199cc0681c4SBenoît Canet /* does this io must wait */ 200cc0681c4SBenoît Canet bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write); 20198f90dbaSZhi Yong Wu 202cc0681c4SBenoît Canet /* if must wait or any request of this type throttled queue the IO */ 203cc0681c4SBenoît Canet if (must_wait || 204cc0681c4SBenoît Canet !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) { 205cc0681c4SBenoît Canet qemu_co_queue_wait(&bs->throttled_reqs[is_write]); 20698f90dbaSZhi Yong Wu } 20798f90dbaSZhi Yong Wu 208cc0681c4SBenoît Canet /* the IO will be executed, do the accounting */ 209d5103588SKevin Wolf throttle_account(&bs->throttle_state, is_write, bytes); 210d5103588SKevin Wolf 211cc0681c4SBenoît Canet 212cc0681c4SBenoît Canet /* if the next request must wait -> do nothing */ 213cc0681c4SBenoît Canet if (throttle_schedule_timer(&bs->throttle_state, is_write)) { 214cc0681c4SBenoît Canet return; 215cc0681c4SBenoît Canet } 216cc0681c4SBenoît Canet 217cc0681c4SBenoît Canet /* else queue next request for execution */ 218cc0681c4SBenoît Canet qemu_co_queue_next(&bs->throttled_reqs[is_write]); 21998f90dbaSZhi Yong Wu } 22098f90dbaSZhi Yong Wu 221339064d5SKevin Wolf size_t bdrv_opt_mem_align(BlockDriverState *bs) 222339064d5SKevin Wolf { 223339064d5SKevin Wolf if (!bs || !bs->drv) { 224339064d5SKevin Wolf /* 4k should be on the safe side */ 225339064d5SKevin Wolf return 4096; 226339064d5SKevin Wolf } 227339064d5SKevin Wolf 228339064d5SKevin Wolf return bs->bl.opt_mem_alignment; 229339064d5SKevin Wolf } 230339064d5SKevin Wolf 2319e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 2329e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 2339e0b22f4SStefan Hajnoczi { 234947995c0SPaolo Bonzini const char *p; 235947995c0SPaolo Bonzini 2369e0b22f4SStefan Hajnoczi #ifdef _WIN32 2379e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2389e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2399e0b22f4SStefan Hajnoczi return 0; 2409e0b22f4SStefan Hajnoczi } 241947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 242947995c0SPaolo Bonzini #else 243947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2449e0b22f4SStefan Hajnoczi #endif 2459e0b22f4SStefan Hajnoczi 246947995c0SPaolo Bonzini return *p == ':'; 2479e0b22f4SStefan Hajnoczi } 2489e0b22f4SStefan Hajnoczi 24983f64091Sbellard int path_is_absolute(const char *path) 25083f64091Sbellard { 25121664424Sbellard #ifdef _WIN32 25221664424Sbellard /* specific case for names like: "\\.\d:" */ 253f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 25421664424Sbellard return 1; 255f53f4da9SPaolo Bonzini } 256f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 2573b9f94e1Sbellard #else 258f53f4da9SPaolo Bonzini return (*path == '/'); 2593b9f94e1Sbellard #endif 26083f64091Sbellard } 26183f64091Sbellard 26283f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 26383f64091Sbellard path to it by considering it is relative to base_path. URL are 26483f64091Sbellard supported. */ 26583f64091Sbellard void path_combine(char *dest, int dest_size, 26683f64091Sbellard const char *base_path, 26783f64091Sbellard const char *filename) 26883f64091Sbellard { 26983f64091Sbellard const char *p, *p1; 27083f64091Sbellard int len; 27183f64091Sbellard 27283f64091Sbellard if (dest_size <= 0) 27383f64091Sbellard return; 27483f64091Sbellard if (path_is_absolute(filename)) { 27583f64091Sbellard pstrcpy(dest, dest_size, filename); 27683f64091Sbellard } else { 27783f64091Sbellard p = strchr(base_path, ':'); 27883f64091Sbellard if (p) 27983f64091Sbellard p++; 28083f64091Sbellard else 28183f64091Sbellard p = base_path; 2823b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2833b9f94e1Sbellard #ifdef _WIN32 2843b9f94e1Sbellard { 2853b9f94e1Sbellard const char *p2; 2863b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2873b9f94e1Sbellard if (!p1 || p2 > p1) 2883b9f94e1Sbellard p1 = p2; 2893b9f94e1Sbellard } 2903b9f94e1Sbellard #endif 29183f64091Sbellard if (p1) 29283f64091Sbellard p1++; 29383f64091Sbellard else 29483f64091Sbellard p1 = base_path; 29583f64091Sbellard if (p1 > p) 29683f64091Sbellard p = p1; 29783f64091Sbellard len = p - base_path; 29883f64091Sbellard if (len > dest_size - 1) 29983f64091Sbellard len = dest_size - 1; 30083f64091Sbellard memcpy(dest, base_path, len); 30183f64091Sbellard dest[len] = '\0'; 30283f64091Sbellard pstrcat(dest, dest_size, filename); 30383f64091Sbellard } 30483f64091Sbellard } 30583f64091Sbellard 306dc5a1371SPaolo Bonzini void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) 307dc5a1371SPaolo Bonzini { 308dc5a1371SPaolo Bonzini if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { 309dc5a1371SPaolo Bonzini pstrcpy(dest, sz, bs->backing_file); 310dc5a1371SPaolo Bonzini } else { 311dc5a1371SPaolo Bonzini path_combine(dest, sz, bs->filename, bs->backing_file); 312dc5a1371SPaolo Bonzini } 313dc5a1371SPaolo Bonzini } 314dc5a1371SPaolo Bonzini 3155efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 316ea2384d3Sbellard { 3178c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 3188c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 319f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 320f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 321f9f05dc5SKevin Wolf 322f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 323f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 324f8c35c1dSStefan Hajnoczi */ 325f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 32683f64091Sbellard /* add AIO emulation layer */ 327f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 328f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 32983f64091Sbellard } 330f9f05dc5SKevin Wolf } 331b2e12bc6SChristoph Hellwig 3328a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 333ea2384d3Sbellard } 334b338082bSbellard 335b338082bSbellard /* create a new block device (by default it is empty) */ 33698522f63SKevin Wolf BlockDriverState *bdrv_new(const char *device_name, Error **errp) 337fc01f7e7Sbellard { 3381b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 339fbe40ff7SFam Zheng int i; 340b338082bSbellard 341f2d953ecSKevin Wolf if (bdrv_find(device_name)) { 342f2d953ecSKevin Wolf error_setg(errp, "Device with id '%s' already exists", 343f2d953ecSKevin Wolf device_name); 344f2d953ecSKevin Wolf return NULL; 345f2d953ecSKevin Wolf } 346f2d953ecSKevin Wolf if (bdrv_find_node(device_name)) { 347f2d953ecSKevin Wolf error_setg(errp, "Device with node-name '%s' already exists", 348f2d953ecSKevin Wolf device_name); 349f2d953ecSKevin Wolf return NULL; 350f2d953ecSKevin Wolf } 351f2d953ecSKevin Wolf 3527267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 353e4654d2dSFam Zheng QLIST_INIT(&bs->dirty_bitmaps); 354b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 355ea2384d3Sbellard if (device_name[0] != '\0') { 356dc364f4cSBenoît Canet QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); 357ea2384d3Sbellard } 358fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 359fbe40ff7SFam Zheng QLIST_INIT(&bs->op_blockers[i]); 360fbe40ff7SFam Zheng } 36128a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 362d7d512f6SPaolo Bonzini notifier_list_init(&bs->close_notifiers); 363d616b224SStefan Hajnoczi notifier_with_return_list_init(&bs->before_write_notifiers); 364cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[0]); 365cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[1]); 3669fcb0251SFam Zheng bs->refcnt = 1; 367dcd04228SStefan Hajnoczi bs->aio_context = qemu_get_aio_context(); 368d7d512f6SPaolo Bonzini 369b338082bSbellard return bs; 370b338082bSbellard } 371b338082bSbellard 372d7d512f6SPaolo Bonzini void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) 373d7d512f6SPaolo Bonzini { 374d7d512f6SPaolo Bonzini notifier_list_add(&bs->close_notifiers, notify); 375d7d512f6SPaolo Bonzini } 376d7d512f6SPaolo Bonzini 377ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 378ea2384d3Sbellard { 379ea2384d3Sbellard BlockDriver *drv1; 3808a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3818a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 382ea2384d3Sbellard return drv1; 383ea2384d3Sbellard } 3848a22f02aSStefan Hajnoczi } 385ea2384d3Sbellard return NULL; 386ea2384d3Sbellard } 387ea2384d3Sbellard 388b64ec4e4SFam Zheng static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) 389eb852011SMarkus Armbruster { 390b64ec4e4SFam Zheng static const char *whitelist_rw[] = { 391b64ec4e4SFam Zheng CONFIG_BDRV_RW_WHITELIST 392b64ec4e4SFam Zheng }; 393b64ec4e4SFam Zheng static const char *whitelist_ro[] = { 394b64ec4e4SFam Zheng CONFIG_BDRV_RO_WHITELIST 395eb852011SMarkus Armbruster }; 396eb852011SMarkus Armbruster const char **p; 397eb852011SMarkus Armbruster 398b64ec4e4SFam Zheng if (!whitelist_rw[0] && !whitelist_ro[0]) { 399eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 400b64ec4e4SFam Zheng } 401eb852011SMarkus Armbruster 402b64ec4e4SFam Zheng for (p = whitelist_rw; *p; p++) { 403eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 404eb852011SMarkus Armbruster return 1; 405eb852011SMarkus Armbruster } 406eb852011SMarkus Armbruster } 407b64ec4e4SFam Zheng if (read_only) { 408b64ec4e4SFam Zheng for (p = whitelist_ro; *p; p++) { 409b64ec4e4SFam Zheng if (!strcmp(drv->format_name, *p)) { 410b64ec4e4SFam Zheng return 1; 411b64ec4e4SFam Zheng } 412b64ec4e4SFam Zheng } 413b64ec4e4SFam Zheng } 414eb852011SMarkus Armbruster return 0; 415eb852011SMarkus Armbruster } 416eb852011SMarkus Armbruster 417b64ec4e4SFam Zheng BlockDriver *bdrv_find_whitelisted_format(const char *format_name, 418b64ec4e4SFam Zheng bool read_only) 419eb852011SMarkus Armbruster { 420eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 421b64ec4e4SFam Zheng return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL; 422eb852011SMarkus Armbruster } 423eb852011SMarkus Armbruster 4245b7e1542SZhi Yong Wu typedef struct CreateCo { 4255b7e1542SZhi Yong Wu BlockDriver *drv; 4265b7e1542SZhi Yong Wu char *filename; 42783d0521aSChunyan Liu QemuOpts *opts; 4285b7e1542SZhi Yong Wu int ret; 429cc84d90fSMax Reitz Error *err; 4305b7e1542SZhi Yong Wu } CreateCo; 4315b7e1542SZhi Yong Wu 4325b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 4335b7e1542SZhi Yong Wu { 434cc84d90fSMax Reitz Error *local_err = NULL; 435cc84d90fSMax Reitz int ret; 436cc84d90fSMax Reitz 4375b7e1542SZhi Yong Wu CreateCo *cco = opaque; 4385b7e1542SZhi Yong Wu assert(cco->drv); 4395b7e1542SZhi Yong Wu 440c282e1fdSChunyan Liu ret = cco->drv->bdrv_create(cco->filename, cco->opts, &local_err); 44184d18f06SMarkus Armbruster if (local_err) { 442cc84d90fSMax Reitz error_propagate(&cco->err, local_err); 443cc84d90fSMax Reitz } 444cc84d90fSMax Reitz cco->ret = ret; 4455b7e1542SZhi Yong Wu } 4465b7e1542SZhi Yong Wu 4470e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 44883d0521aSChunyan Liu QemuOpts *opts, Error **errp) 449ea2384d3Sbellard { 4505b7e1542SZhi Yong Wu int ret; 4510e7e1989SKevin Wolf 4525b7e1542SZhi Yong Wu Coroutine *co; 4535b7e1542SZhi Yong Wu CreateCo cco = { 4545b7e1542SZhi Yong Wu .drv = drv, 4555b7e1542SZhi Yong Wu .filename = g_strdup(filename), 45683d0521aSChunyan Liu .opts = opts, 4575b7e1542SZhi Yong Wu .ret = NOT_DONE, 458cc84d90fSMax Reitz .err = NULL, 4595b7e1542SZhi Yong Wu }; 4605b7e1542SZhi Yong Wu 461c282e1fdSChunyan Liu if (!drv->bdrv_create) { 462cc84d90fSMax Reitz error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); 46380168bffSLuiz Capitulino ret = -ENOTSUP; 46480168bffSLuiz Capitulino goto out; 4655b7e1542SZhi Yong Wu } 4665b7e1542SZhi Yong Wu 4675b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 4685b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 4695b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 4705b7e1542SZhi Yong Wu } else { 4715b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 4725b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 4735b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 4745b7e1542SZhi Yong Wu qemu_aio_wait(); 4755b7e1542SZhi Yong Wu } 4765b7e1542SZhi Yong Wu } 4775b7e1542SZhi Yong Wu 4785b7e1542SZhi Yong Wu ret = cco.ret; 479cc84d90fSMax Reitz if (ret < 0) { 48084d18f06SMarkus Armbruster if (cco.err) { 481cc84d90fSMax Reitz error_propagate(errp, cco.err); 482cc84d90fSMax Reitz } else { 483cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not create image"); 484cc84d90fSMax Reitz } 485cc84d90fSMax Reitz } 4865b7e1542SZhi Yong Wu 48780168bffSLuiz Capitulino out: 48880168bffSLuiz Capitulino g_free(cco.filename); 4895b7e1542SZhi Yong Wu return ret; 490ea2384d3Sbellard } 491ea2384d3Sbellard 492c282e1fdSChunyan Liu int bdrv_create_file(const char *filename, QemuOpts *opts, Error **errp) 49384a12e66SChristoph Hellwig { 49484a12e66SChristoph Hellwig BlockDriver *drv; 495cc84d90fSMax Reitz Error *local_err = NULL; 496cc84d90fSMax Reitz int ret; 49784a12e66SChristoph Hellwig 49898289620SKevin Wolf drv = bdrv_find_protocol(filename, true); 49984a12e66SChristoph Hellwig if (drv == NULL) { 500cc84d90fSMax Reitz error_setg(errp, "Could not find protocol for file '%s'", filename); 50116905d71SStefan Hajnoczi return -ENOENT; 50284a12e66SChristoph Hellwig } 50384a12e66SChristoph Hellwig 504c282e1fdSChunyan Liu ret = bdrv_create(drv, filename, opts, &local_err); 50584d18f06SMarkus Armbruster if (local_err) { 506cc84d90fSMax Reitz error_propagate(errp, local_err); 507cc84d90fSMax Reitz } 508cc84d90fSMax Reitz return ret; 50984a12e66SChristoph Hellwig } 51084a12e66SChristoph Hellwig 511355ef4acSKevin Wolf int bdrv_refresh_limits(BlockDriverState *bs) 512d34682cdSKevin Wolf { 513d34682cdSKevin Wolf BlockDriver *drv = bs->drv; 514d34682cdSKevin Wolf 515d34682cdSKevin Wolf memset(&bs->bl, 0, sizeof(bs->bl)); 516d34682cdSKevin Wolf 517466ad822SKevin Wolf if (!drv) { 518466ad822SKevin Wolf return 0; 519466ad822SKevin Wolf } 520466ad822SKevin Wolf 521466ad822SKevin Wolf /* Take some limits from the children as a default */ 522466ad822SKevin Wolf if (bs->file) { 523466ad822SKevin Wolf bdrv_refresh_limits(bs->file); 524466ad822SKevin Wolf bs->bl.opt_transfer_length = bs->file->bl.opt_transfer_length; 525339064d5SKevin Wolf bs->bl.opt_mem_alignment = bs->file->bl.opt_mem_alignment; 526339064d5SKevin Wolf } else { 527339064d5SKevin Wolf bs->bl.opt_mem_alignment = 512; 528466ad822SKevin Wolf } 529466ad822SKevin Wolf 530466ad822SKevin Wolf if (bs->backing_hd) { 531466ad822SKevin Wolf bdrv_refresh_limits(bs->backing_hd); 532466ad822SKevin Wolf bs->bl.opt_transfer_length = 533466ad822SKevin Wolf MAX(bs->bl.opt_transfer_length, 534466ad822SKevin Wolf bs->backing_hd->bl.opt_transfer_length); 535339064d5SKevin Wolf bs->bl.opt_mem_alignment = 536339064d5SKevin Wolf MAX(bs->bl.opt_mem_alignment, 537339064d5SKevin Wolf bs->backing_hd->bl.opt_mem_alignment); 538466ad822SKevin Wolf } 539466ad822SKevin Wolf 540466ad822SKevin Wolf /* Then let the driver override it */ 541466ad822SKevin Wolf if (drv->bdrv_refresh_limits) { 542d34682cdSKevin Wolf return drv->bdrv_refresh_limits(bs); 543d34682cdSKevin Wolf } 544d34682cdSKevin Wolf 545d34682cdSKevin Wolf return 0; 546d34682cdSKevin Wolf } 547d34682cdSKevin Wolf 548eba25057SJim Meyering /* 549eba25057SJim Meyering * Create a uniquely-named empty temporary file. 550eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 551eba25057SJim Meyering */ 552eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 553eba25057SJim Meyering { 554d5249393Sbellard #ifdef _WIN32 5553b9f94e1Sbellard char temp_dir[MAX_PATH]; 556eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 557eba25057SJim Meyering have length MAX_PATH or greater. */ 558eba25057SJim Meyering assert(size >= MAX_PATH); 559eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 560eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 561eba25057SJim Meyering ? 0 : -GetLastError()); 562d5249393Sbellard #else 563ea2384d3Sbellard int fd; 5647ccfb2ebSblueswir1 const char *tmpdir; 5650badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 56669bef793SAmit Shah if (!tmpdir) { 56769bef793SAmit Shah tmpdir = "/var/tmp"; 56869bef793SAmit Shah } 569eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 570eba25057SJim Meyering return -EOVERFLOW; 571ea2384d3Sbellard } 572eba25057SJim Meyering fd = mkstemp(filename); 573fe235a06SDunrong Huang if (fd < 0) { 574fe235a06SDunrong Huang return -errno; 575fe235a06SDunrong Huang } 576fe235a06SDunrong Huang if (close(fd) != 0) { 577fe235a06SDunrong Huang unlink(filename); 578eba25057SJim Meyering return -errno; 579eba25057SJim Meyering } 580eba25057SJim Meyering return 0; 581d5249393Sbellard #endif 582eba25057SJim Meyering } 583ea2384d3Sbellard 584f3a5d3f8SChristoph Hellwig /* 585f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 586f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 587f3a5d3f8SChristoph Hellwig */ 588f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 589f3a5d3f8SChristoph Hellwig { 590508c7cb3SChristoph Hellwig int score_max = 0, score; 591508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 592f3a5d3f8SChristoph Hellwig 5938a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 594508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 595508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 596508c7cb3SChristoph Hellwig if (score > score_max) { 597508c7cb3SChristoph Hellwig score_max = score; 598508c7cb3SChristoph Hellwig drv = d; 599f3a5d3f8SChristoph Hellwig } 600508c7cb3SChristoph Hellwig } 601f3a5d3f8SChristoph Hellwig } 602f3a5d3f8SChristoph Hellwig 603508c7cb3SChristoph Hellwig return drv; 604f3a5d3f8SChristoph Hellwig } 605f3a5d3f8SChristoph Hellwig 60698289620SKevin Wolf BlockDriver *bdrv_find_protocol(const char *filename, 60798289620SKevin Wolf bool allow_protocol_prefix) 60884a12e66SChristoph Hellwig { 60984a12e66SChristoph Hellwig BlockDriver *drv1; 61084a12e66SChristoph Hellwig char protocol[128]; 61184a12e66SChristoph Hellwig int len; 61284a12e66SChristoph Hellwig const char *p; 61384a12e66SChristoph Hellwig 61466f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 61566f82ceeSKevin Wolf 61639508e7aSChristoph Hellwig /* 61739508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 61839508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 61939508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 62039508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 62139508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 62239508e7aSChristoph Hellwig */ 62384a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 62439508e7aSChristoph Hellwig if (drv1) { 62584a12e66SChristoph Hellwig return drv1; 62684a12e66SChristoph Hellwig } 62739508e7aSChristoph Hellwig 62898289620SKevin Wolf if (!path_has_protocol(filename) || !allow_protocol_prefix) { 62939508e7aSChristoph Hellwig return bdrv_find_format("file"); 63039508e7aSChristoph Hellwig } 63198289620SKevin Wolf 6329e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 6339e0b22f4SStefan Hajnoczi assert(p != NULL); 63484a12e66SChristoph Hellwig len = p - filename; 63584a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 63684a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 63784a12e66SChristoph Hellwig memcpy(protocol, filename, len); 63884a12e66SChristoph Hellwig protocol[len] = '\0'; 63984a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 64084a12e66SChristoph Hellwig if (drv1->protocol_name && 64184a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 64284a12e66SChristoph Hellwig return drv1; 64384a12e66SChristoph Hellwig } 64484a12e66SChristoph Hellwig } 64584a12e66SChristoph Hellwig return NULL; 64684a12e66SChristoph Hellwig } 64784a12e66SChristoph Hellwig 648f500a6d3SKevin Wolf static int find_image_format(BlockDriverState *bs, const char *filename, 64934b5d2c6SMax Reitz BlockDriver **pdrv, Error **errp) 650ea2384d3Sbellard { 651f500a6d3SKevin Wolf int score, score_max; 652ea2384d3Sbellard BlockDriver *drv1, *drv; 65383f64091Sbellard uint8_t buf[2048]; 654f500a6d3SKevin Wolf int ret = 0; 655f8ea0b00SNicholas Bellinger 65608a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 6578e895599SPaolo Bonzini if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { 658c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 659c98ac35dSStefan Weil if (!drv) { 66034b5d2c6SMax Reitz error_setg(errp, "Could not find raw image format"); 661c98ac35dSStefan Weil ret = -ENOENT; 662c98ac35dSStefan Weil } 663c98ac35dSStefan Weil *pdrv = drv; 664c98ac35dSStefan Weil return ret; 6651a396859SNicholas A. Bellinger } 666f8ea0b00SNicholas Bellinger 66783f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 668ea2384d3Sbellard if (ret < 0) { 66934b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not read image for determining its " 67034b5d2c6SMax Reitz "format"); 671c98ac35dSStefan Weil *pdrv = NULL; 672c98ac35dSStefan Weil return ret; 673ea2384d3Sbellard } 674ea2384d3Sbellard 675ea2384d3Sbellard score_max = 0; 67684a12e66SChristoph Hellwig drv = NULL; 6778a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 67883f64091Sbellard if (drv1->bdrv_probe) { 679ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 680ea2384d3Sbellard if (score > score_max) { 681ea2384d3Sbellard score_max = score; 682ea2384d3Sbellard drv = drv1; 683ea2384d3Sbellard } 684ea2384d3Sbellard } 68583f64091Sbellard } 686c98ac35dSStefan Weil if (!drv) { 68734b5d2c6SMax Reitz error_setg(errp, "Could not determine image format: No compatible " 68834b5d2c6SMax Reitz "driver found"); 689c98ac35dSStefan Weil ret = -ENOENT; 690c98ac35dSStefan Weil } 691c98ac35dSStefan Weil *pdrv = drv; 692c98ac35dSStefan Weil return ret; 693ea2384d3Sbellard } 694ea2384d3Sbellard 69551762288SStefan Hajnoczi /** 69651762288SStefan Hajnoczi * Set the current 'total_sectors' value 69751762288SStefan Hajnoczi */ 69851762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 69951762288SStefan Hajnoczi { 70051762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 70151762288SStefan Hajnoczi 702396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 703396759adSNicholas Bellinger if (bs->sg) 704396759adSNicholas Bellinger return 0; 705396759adSNicholas Bellinger 70651762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 70751762288SStefan Hajnoczi if (drv->bdrv_getlength) { 70851762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 70951762288SStefan Hajnoczi if (length < 0) { 71051762288SStefan Hajnoczi return length; 71151762288SStefan Hajnoczi } 7127e382003SFam Zheng hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); 71351762288SStefan Hajnoczi } 71451762288SStefan Hajnoczi 71551762288SStefan Hajnoczi bs->total_sectors = hint; 71651762288SStefan Hajnoczi return 0; 71751762288SStefan Hajnoczi } 71851762288SStefan Hajnoczi 719c3993cdcSStefan Hajnoczi /** 7209e8f1835SPaolo Bonzini * Set open flags for a given discard mode 7219e8f1835SPaolo Bonzini * 7229e8f1835SPaolo Bonzini * Return 0 on success, -1 if the discard mode was invalid. 7239e8f1835SPaolo Bonzini */ 7249e8f1835SPaolo Bonzini int bdrv_parse_discard_flags(const char *mode, int *flags) 7259e8f1835SPaolo Bonzini { 7269e8f1835SPaolo Bonzini *flags &= ~BDRV_O_UNMAP; 7279e8f1835SPaolo Bonzini 7289e8f1835SPaolo Bonzini if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { 7299e8f1835SPaolo Bonzini /* do nothing */ 7309e8f1835SPaolo Bonzini } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { 7319e8f1835SPaolo Bonzini *flags |= BDRV_O_UNMAP; 7329e8f1835SPaolo Bonzini } else { 7339e8f1835SPaolo Bonzini return -1; 7349e8f1835SPaolo Bonzini } 7359e8f1835SPaolo Bonzini 7369e8f1835SPaolo Bonzini return 0; 7379e8f1835SPaolo Bonzini } 7389e8f1835SPaolo Bonzini 7399e8f1835SPaolo Bonzini /** 740c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 741c3993cdcSStefan Hajnoczi * 742c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 743c3993cdcSStefan Hajnoczi */ 744c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 745c3993cdcSStefan Hajnoczi { 746c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 747c3993cdcSStefan Hajnoczi 748c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 749c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 75092196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 75192196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 752c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 753c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 754c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 755c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 756c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 757c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 758c3993cdcSStefan Hajnoczi /* this is the default */ 759c3993cdcSStefan Hajnoczi } else { 760c3993cdcSStefan Hajnoczi return -1; 761c3993cdcSStefan Hajnoczi } 762c3993cdcSStefan Hajnoczi 763c3993cdcSStefan Hajnoczi return 0; 764c3993cdcSStefan Hajnoczi } 765c3993cdcSStefan Hajnoczi 76653fec9d3SStefan Hajnoczi /** 76753fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 76853fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 76953fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 77053fec9d3SStefan Hajnoczi */ 77153fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 77253fec9d3SStefan Hajnoczi { 77353fec9d3SStefan Hajnoczi bs->copy_on_read++; 77453fec9d3SStefan Hajnoczi } 77553fec9d3SStefan Hajnoczi 77653fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 77753fec9d3SStefan Hajnoczi { 77853fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 77953fec9d3SStefan Hajnoczi bs->copy_on_read--; 78053fec9d3SStefan Hajnoczi } 78153fec9d3SStefan Hajnoczi 7820b50cc88SKevin Wolf /* 783b1e6fc08SKevin Wolf * Returns the flags that a temporary snapshot should get, based on the 784b1e6fc08SKevin Wolf * originally requested flags (the originally requested image will have flags 785b1e6fc08SKevin Wolf * like a backing file) 786b1e6fc08SKevin Wolf */ 787b1e6fc08SKevin Wolf static int bdrv_temp_snapshot_flags(int flags) 788b1e6fc08SKevin Wolf { 789b1e6fc08SKevin Wolf return (flags & ~BDRV_O_SNAPSHOT) | BDRV_O_TEMPORARY; 790b1e6fc08SKevin Wolf } 791b1e6fc08SKevin Wolf 792b1e6fc08SKevin Wolf /* 7930b50cc88SKevin Wolf * Returns the flags that bs->file should get, based on the given flags for 7940b50cc88SKevin Wolf * the parent BDS 7950b50cc88SKevin Wolf */ 7960b50cc88SKevin Wolf static int bdrv_inherited_flags(int flags) 7970b50cc88SKevin Wolf { 7980b50cc88SKevin Wolf /* Enable protocol handling, disable format probing for bs->file */ 7990b50cc88SKevin Wolf flags |= BDRV_O_PROTOCOL; 8000b50cc88SKevin Wolf 8010b50cc88SKevin Wolf /* Our block drivers take care to send flushes and respect unmap policy, 8020b50cc88SKevin Wolf * so we can enable both unconditionally on lower layers. */ 8030b50cc88SKevin Wolf flags |= BDRV_O_CACHE_WB | BDRV_O_UNMAP; 8040b50cc88SKevin Wolf 8050b50cc88SKevin Wolf /* Clear flags that only apply to the top layer */ 8065669b44dSKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING | BDRV_O_COPY_ON_READ); 8070b50cc88SKevin Wolf 8080b50cc88SKevin Wolf return flags; 8090b50cc88SKevin Wolf } 8100b50cc88SKevin Wolf 811317fc44eSKevin Wolf /* 812317fc44eSKevin Wolf * Returns the flags that bs->backing_hd should get, based on the given flags 813317fc44eSKevin Wolf * for the parent BDS 814317fc44eSKevin Wolf */ 815317fc44eSKevin Wolf static int bdrv_backing_flags(int flags) 816317fc44eSKevin Wolf { 817317fc44eSKevin Wolf /* backing files always opened read-only */ 818317fc44eSKevin Wolf flags &= ~(BDRV_O_RDWR | BDRV_O_COPY_ON_READ); 819317fc44eSKevin Wolf 820317fc44eSKevin Wolf /* snapshot=on is handled on the top layer */ 8218bfea15dSKevin Wolf flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_TEMPORARY); 822317fc44eSKevin Wolf 823317fc44eSKevin Wolf return flags; 824317fc44eSKevin Wolf } 825317fc44eSKevin Wolf 8267b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 8277b272452SKevin Wolf { 8287b272452SKevin Wolf int open_flags = flags | BDRV_O_CACHE_WB; 8297b272452SKevin Wolf 8307b272452SKevin Wolf /* 8317b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 8327b272452SKevin Wolf * image. 8337b272452SKevin Wolf */ 8347b272452SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 8357b272452SKevin Wolf 8367b272452SKevin Wolf /* 8377b272452SKevin Wolf * Snapshots should be writable. 8387b272452SKevin Wolf */ 8398bfea15dSKevin Wolf if (flags & BDRV_O_TEMPORARY) { 8407b272452SKevin Wolf open_flags |= BDRV_O_RDWR; 8417b272452SKevin Wolf } 8427b272452SKevin Wolf 8437b272452SKevin Wolf return open_flags; 8447b272452SKevin Wolf } 8457b272452SKevin Wolf 846636ea370SKevin Wolf static void bdrv_assign_node_name(BlockDriverState *bs, 8476913c0c2SBenoît Canet const char *node_name, 8486913c0c2SBenoît Canet Error **errp) 8496913c0c2SBenoît Canet { 8506913c0c2SBenoît Canet if (!node_name) { 851636ea370SKevin Wolf return; 8526913c0c2SBenoît Canet } 8536913c0c2SBenoît Canet 8546913c0c2SBenoît Canet /* empty string node name is invalid */ 8556913c0c2SBenoît Canet if (node_name[0] == '\0') { 8566913c0c2SBenoît Canet error_setg(errp, "Empty node name"); 857636ea370SKevin Wolf return; 8586913c0c2SBenoît Canet } 8596913c0c2SBenoît Canet 8600c5e94eeSBenoît Canet /* takes care of avoiding namespaces collisions */ 8610c5e94eeSBenoît Canet if (bdrv_find(node_name)) { 8620c5e94eeSBenoît Canet error_setg(errp, "node-name=%s is conflicting with a device id", 8630c5e94eeSBenoît Canet node_name); 864636ea370SKevin Wolf return; 8650c5e94eeSBenoît Canet } 8660c5e94eeSBenoît Canet 8676913c0c2SBenoît Canet /* takes care of avoiding duplicates node names */ 8686913c0c2SBenoît Canet if (bdrv_find_node(node_name)) { 8696913c0c2SBenoît Canet error_setg(errp, "Duplicate node name"); 870636ea370SKevin Wolf return; 8716913c0c2SBenoît Canet } 8726913c0c2SBenoît Canet 8736913c0c2SBenoît Canet /* copy node name into the bs and insert it into the graph list */ 8746913c0c2SBenoît Canet pstrcpy(bs->node_name, sizeof(bs->node_name), node_name); 8756913c0c2SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list); 8766913c0c2SBenoît Canet } 8776913c0c2SBenoît Canet 878b6ce07aaSKevin Wolf /* 87957915332SKevin Wolf * Common part for opening disk images and files 880b6ad491aSKevin Wolf * 881b6ad491aSKevin Wolf * Removes all processed options from *options. 88257915332SKevin Wolf */ 883f500a6d3SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, 88434b5d2c6SMax Reitz QDict *options, int flags, BlockDriver *drv, Error **errp) 88557915332SKevin Wolf { 88657915332SKevin Wolf int ret, open_flags; 887035fccdfSKevin Wolf const char *filename; 8886913c0c2SBenoît Canet const char *node_name = NULL; 88934b5d2c6SMax Reitz Error *local_err = NULL; 89057915332SKevin Wolf 89157915332SKevin Wolf assert(drv != NULL); 8926405875cSPaolo Bonzini assert(bs->file == NULL); 893707ff828SKevin Wolf assert(options != NULL && bs->options != options); 89457915332SKevin Wolf 89545673671SKevin Wolf if (file != NULL) { 89645673671SKevin Wolf filename = file->filename; 89745673671SKevin Wolf } else { 89845673671SKevin Wolf filename = qdict_get_try_str(options, "filename"); 89945673671SKevin Wolf } 90045673671SKevin Wolf 901765003dbSKevin Wolf if (drv->bdrv_needs_filename && !filename) { 902765003dbSKevin Wolf error_setg(errp, "The '%s' block driver requires a file name", 903765003dbSKevin Wolf drv->format_name); 904765003dbSKevin Wolf return -EINVAL; 905765003dbSKevin Wolf } 906765003dbSKevin Wolf 90745673671SKevin Wolf trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name); 90828dcee10SStefan Hajnoczi 9096913c0c2SBenoît Canet node_name = qdict_get_try_str(options, "node-name"); 910636ea370SKevin Wolf bdrv_assign_node_name(bs, node_name, &local_err); 9110fb6395cSMarkus Armbruster if (local_err) { 912636ea370SKevin Wolf error_propagate(errp, local_err); 913636ea370SKevin Wolf return -EINVAL; 9146913c0c2SBenoît Canet } 9156913c0c2SBenoît Canet qdict_del(options, "node-name"); 9166913c0c2SBenoît Canet 9175d186eb0SKevin Wolf /* bdrv_open() with directly using a protocol as drv. This layer is already 9185d186eb0SKevin Wolf * opened, so assign it to bs (while file becomes a closed BlockDriverState) 9195d186eb0SKevin Wolf * and return immediately. */ 9205d186eb0SKevin Wolf if (file != NULL && drv->bdrv_file_open) { 9215d186eb0SKevin Wolf bdrv_swap(file, bs); 9225d186eb0SKevin Wolf return 0; 9235d186eb0SKevin Wolf } 9245d186eb0SKevin Wolf 92557915332SKevin Wolf bs->open_flags = flags; 9261b7fd729SPaolo Bonzini bs->guest_block_size = 512; 927c25f53b0SPaolo Bonzini bs->request_alignment = 512; 9280d51b4deSAsias He bs->zero_beyond_eof = true; 929b64ec4e4SFam Zheng open_flags = bdrv_open_flags(bs, flags); 930b64ec4e4SFam Zheng bs->read_only = !(open_flags & BDRV_O_RDWR); 931b64ec4e4SFam Zheng 932b64ec4e4SFam Zheng if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { 9338f94a6e4SKevin Wolf error_setg(errp, 9348f94a6e4SKevin Wolf !bs->read_only && bdrv_is_whitelisted(drv, true) 9358f94a6e4SKevin Wolf ? "Driver '%s' can only be used for read-only devices" 9368f94a6e4SKevin Wolf : "Driver '%s' is not whitelisted", 9378f94a6e4SKevin Wolf drv->format_name); 938b64ec4e4SFam Zheng return -ENOTSUP; 939b64ec4e4SFam Zheng } 94057915332SKevin Wolf 94153fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 9420ebd24e0SKevin Wolf if (flags & BDRV_O_COPY_ON_READ) { 9430ebd24e0SKevin Wolf if (!bs->read_only) { 94453fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 9450ebd24e0SKevin Wolf } else { 9460ebd24e0SKevin Wolf error_setg(errp, "Can't use copy-on-read on read-only device"); 9470ebd24e0SKevin Wolf return -EINVAL; 9480ebd24e0SKevin Wolf } 94953fec9d3SStefan Hajnoczi } 95053fec9d3SStefan Hajnoczi 951c2ad1b0cSKevin Wolf if (filename != NULL) { 95257915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 953c2ad1b0cSKevin Wolf } else { 954c2ad1b0cSKevin Wolf bs->filename[0] = '\0'; 955c2ad1b0cSKevin Wolf } 95657915332SKevin Wolf 95757915332SKevin Wolf bs->drv = drv; 9587267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 95957915332SKevin Wolf 96003f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 961e7c63796SStefan Hajnoczi 96266f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 96366f82ceeSKevin Wolf if (drv->bdrv_file_open) { 9645d186eb0SKevin Wolf assert(file == NULL); 965030be321SBenoît Canet assert(!drv->bdrv_needs_filename || filename != NULL); 96634b5d2c6SMax Reitz ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); 967f500a6d3SKevin Wolf } else { 9682af5ef70SKevin Wolf if (file == NULL) { 96934b5d2c6SMax Reitz error_setg(errp, "Can't use '%s' as a block driver for the " 97034b5d2c6SMax Reitz "protocol level", drv->format_name); 9712af5ef70SKevin Wolf ret = -EINVAL; 9722af5ef70SKevin Wolf goto free_and_fail; 9732af5ef70SKevin Wolf } 974f500a6d3SKevin Wolf bs->file = file; 97534b5d2c6SMax Reitz ret = drv->bdrv_open(bs, options, open_flags, &local_err); 97666f82ceeSKevin Wolf } 97766f82ceeSKevin Wolf 97857915332SKevin Wolf if (ret < 0) { 97984d18f06SMarkus Armbruster if (local_err) { 98034b5d2c6SMax Reitz error_propagate(errp, local_err); 9812fa9aa59SDunrong Huang } else if (bs->filename[0]) { 9822fa9aa59SDunrong Huang error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); 98334b5d2c6SMax Reitz } else { 98434b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not open image"); 98534b5d2c6SMax Reitz } 98657915332SKevin Wolf goto free_and_fail; 98757915332SKevin Wolf } 98857915332SKevin Wolf 98951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 99051762288SStefan Hajnoczi if (ret < 0) { 99134b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not refresh total sector count"); 99251762288SStefan Hajnoczi goto free_and_fail; 99357915332SKevin Wolf } 99451762288SStefan Hajnoczi 995d34682cdSKevin Wolf bdrv_refresh_limits(bs); 996c25f53b0SPaolo Bonzini assert(bdrv_opt_mem_align(bs) != 0); 99747ea2de2SKevin Wolf assert((bs->request_alignment != 0) || bs->sg); 99857915332SKevin Wolf return 0; 99957915332SKevin Wolf 100057915332SKevin Wolf free_and_fail: 100166f82ceeSKevin Wolf bs->file = NULL; 10027267c094SAnthony Liguori g_free(bs->opaque); 100357915332SKevin Wolf bs->opaque = NULL; 100457915332SKevin Wolf bs->drv = NULL; 100557915332SKevin Wolf return ret; 100657915332SKevin Wolf } 100757915332SKevin Wolf 10085e5c4f63SKevin Wolf static QDict *parse_json_filename(const char *filename, Error **errp) 10095e5c4f63SKevin Wolf { 10105e5c4f63SKevin Wolf QObject *options_obj; 10115e5c4f63SKevin Wolf QDict *options; 10125e5c4f63SKevin Wolf int ret; 10135e5c4f63SKevin Wolf 10145e5c4f63SKevin Wolf ret = strstart(filename, "json:", &filename); 10155e5c4f63SKevin Wolf assert(ret); 10165e5c4f63SKevin Wolf 10175e5c4f63SKevin Wolf options_obj = qobject_from_json(filename); 10185e5c4f63SKevin Wolf if (!options_obj) { 10195e5c4f63SKevin Wolf error_setg(errp, "Could not parse the JSON options"); 10205e5c4f63SKevin Wolf return NULL; 10215e5c4f63SKevin Wolf } 10225e5c4f63SKevin Wolf 10235e5c4f63SKevin Wolf if (qobject_type(options_obj) != QTYPE_QDICT) { 10245e5c4f63SKevin Wolf qobject_decref(options_obj); 10255e5c4f63SKevin Wolf error_setg(errp, "Invalid JSON object given"); 10265e5c4f63SKevin Wolf return NULL; 10275e5c4f63SKevin Wolf } 10285e5c4f63SKevin Wolf 10295e5c4f63SKevin Wolf options = qobject_to_qdict(options_obj); 10305e5c4f63SKevin Wolf qdict_flatten(options); 10315e5c4f63SKevin Wolf 10325e5c4f63SKevin Wolf return options; 10335e5c4f63SKevin Wolf } 10345e5c4f63SKevin Wolf 103557915332SKevin Wolf /* 1036f54120ffSKevin Wolf * Fills in default options for opening images and converts the legacy 1037f54120ffSKevin Wolf * filename/flags pair to option QDict entries. 1038f54120ffSKevin Wolf */ 10395e5c4f63SKevin Wolf static int bdrv_fill_options(QDict **options, const char **pfilename, int flags, 104017b005f1SKevin Wolf BlockDriver *drv, Error **errp) 1041f54120ffSKevin Wolf { 10425e5c4f63SKevin Wolf const char *filename = *pfilename; 1043f54120ffSKevin Wolf const char *drvname; 1044462f5bcfSKevin Wolf bool protocol = flags & BDRV_O_PROTOCOL; 1045f54120ffSKevin Wolf bool parse_filename = false; 1046f54120ffSKevin Wolf Error *local_err = NULL; 1047f54120ffSKevin Wolf 10485e5c4f63SKevin Wolf /* Parse json: pseudo-protocol */ 10495e5c4f63SKevin Wolf if (filename && g_str_has_prefix(filename, "json:")) { 10505e5c4f63SKevin Wolf QDict *json_options = parse_json_filename(filename, &local_err); 10515e5c4f63SKevin Wolf if (local_err) { 10525e5c4f63SKevin Wolf error_propagate(errp, local_err); 10535e5c4f63SKevin Wolf return -EINVAL; 10545e5c4f63SKevin Wolf } 10555e5c4f63SKevin Wolf 10565e5c4f63SKevin Wolf /* Options given in the filename have lower priority than options 10575e5c4f63SKevin Wolf * specified directly */ 10585e5c4f63SKevin Wolf qdict_join(*options, json_options, false); 10595e5c4f63SKevin Wolf QDECREF(json_options); 10605e5c4f63SKevin Wolf *pfilename = filename = NULL; 10615e5c4f63SKevin Wolf } 10625e5c4f63SKevin Wolf 1063f54120ffSKevin Wolf /* Fetch the file name from the options QDict if necessary */ 106417b005f1SKevin Wolf if (protocol && filename) { 1065f54120ffSKevin Wolf if (!qdict_haskey(*options, "filename")) { 1066f54120ffSKevin Wolf qdict_put(*options, "filename", qstring_from_str(filename)); 1067f54120ffSKevin Wolf parse_filename = true; 1068f54120ffSKevin Wolf } else { 1069f54120ffSKevin Wolf error_setg(errp, "Can't specify 'file' and 'filename' options at " 1070f54120ffSKevin Wolf "the same time"); 1071f54120ffSKevin Wolf return -EINVAL; 1072f54120ffSKevin Wolf } 1073f54120ffSKevin Wolf } 1074f54120ffSKevin Wolf 1075f54120ffSKevin Wolf /* Find the right block driver */ 1076f54120ffSKevin Wolf filename = qdict_get_try_str(*options, "filename"); 1077f54120ffSKevin Wolf drvname = qdict_get_try_str(*options, "driver"); 1078f54120ffSKevin Wolf 107917b005f1SKevin Wolf if (drv) { 108017b005f1SKevin Wolf if (drvname) { 108117b005f1SKevin Wolf error_setg(errp, "Driver specified twice"); 108217b005f1SKevin Wolf return -EINVAL; 108317b005f1SKevin Wolf } 108417b005f1SKevin Wolf drvname = drv->format_name; 108517b005f1SKevin Wolf qdict_put(*options, "driver", qstring_from_str(drvname)); 108617b005f1SKevin Wolf } else { 108717b005f1SKevin Wolf if (!drvname && protocol) { 1088f54120ffSKevin Wolf if (filename) { 1089f54120ffSKevin Wolf drv = bdrv_find_protocol(filename, parse_filename); 1090f54120ffSKevin Wolf if (!drv) { 1091f54120ffSKevin Wolf error_setg(errp, "Unknown protocol"); 1092f54120ffSKevin Wolf return -EINVAL; 1093f54120ffSKevin Wolf } 1094f54120ffSKevin Wolf 1095f54120ffSKevin Wolf drvname = drv->format_name; 1096f54120ffSKevin Wolf qdict_put(*options, "driver", qstring_from_str(drvname)); 1097f54120ffSKevin Wolf } else { 1098f54120ffSKevin Wolf error_setg(errp, "Must specify either driver or file"); 1099f54120ffSKevin Wolf return -EINVAL; 1100f54120ffSKevin Wolf } 110117b005f1SKevin Wolf } else if (drvname) { 1102f54120ffSKevin Wolf drv = bdrv_find_format(drvname); 1103f54120ffSKevin Wolf if (!drv) { 1104f54120ffSKevin Wolf error_setg(errp, "Unknown driver '%s'", drvname); 1105f54120ffSKevin Wolf return -ENOENT; 1106f54120ffSKevin Wolf } 110717b005f1SKevin Wolf } 110817b005f1SKevin Wolf } 110917b005f1SKevin Wolf 111017b005f1SKevin Wolf assert(drv || !protocol); 1111f54120ffSKevin Wolf 1112f54120ffSKevin Wolf /* Driver-specific filename parsing */ 111317b005f1SKevin Wolf if (drv && drv->bdrv_parse_filename && parse_filename) { 1114f54120ffSKevin Wolf drv->bdrv_parse_filename(filename, *options, &local_err); 1115f54120ffSKevin Wolf if (local_err) { 1116f54120ffSKevin Wolf error_propagate(errp, local_err); 1117f54120ffSKevin Wolf return -EINVAL; 1118f54120ffSKevin Wolf } 1119f54120ffSKevin Wolf 1120f54120ffSKevin Wolf if (!drv->bdrv_needs_filename) { 1121f54120ffSKevin Wolf qdict_del(*options, "filename"); 1122f54120ffSKevin Wolf } 1123f54120ffSKevin Wolf } 1124f54120ffSKevin Wolf 1125f54120ffSKevin Wolf return 0; 1126f54120ffSKevin Wolf } 1127f54120ffSKevin Wolf 11288d24cce1SFam Zheng void bdrv_set_backing_hd(BlockDriverState *bs, BlockDriverState *backing_hd) 11298d24cce1SFam Zheng { 11308d24cce1SFam Zheng 1131826b6ca0SFam Zheng if (bs->backing_hd) { 1132826b6ca0SFam Zheng assert(bs->backing_blocker); 1133826b6ca0SFam Zheng bdrv_op_unblock_all(bs->backing_hd, bs->backing_blocker); 1134826b6ca0SFam Zheng } else if (backing_hd) { 1135826b6ca0SFam Zheng error_setg(&bs->backing_blocker, 1136826b6ca0SFam Zheng "device is used as backing hd of '%s'", 1137826b6ca0SFam Zheng bs->device_name); 1138826b6ca0SFam Zheng } 1139826b6ca0SFam Zheng 11408d24cce1SFam Zheng bs->backing_hd = backing_hd; 11418d24cce1SFam Zheng if (!backing_hd) { 1142826b6ca0SFam Zheng error_free(bs->backing_blocker); 1143826b6ca0SFam Zheng bs->backing_blocker = NULL; 11448d24cce1SFam Zheng goto out; 11458d24cce1SFam Zheng } 11468d24cce1SFam Zheng bs->open_flags &= ~BDRV_O_NO_BACKING; 11478d24cce1SFam Zheng pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_hd->filename); 11488d24cce1SFam Zheng pstrcpy(bs->backing_format, sizeof(bs->backing_format), 11498d24cce1SFam Zheng backing_hd->drv ? backing_hd->drv->format_name : ""); 1150826b6ca0SFam Zheng 1151826b6ca0SFam Zheng bdrv_op_block_all(bs->backing_hd, bs->backing_blocker); 1152826b6ca0SFam Zheng /* Otherwise we won't be able to commit due to check in bdrv_commit */ 1153826b6ca0SFam Zheng bdrv_op_unblock(bs->backing_hd, BLOCK_OP_TYPE_COMMIT, 1154826b6ca0SFam Zheng bs->backing_blocker); 11558d24cce1SFam Zheng out: 11568d24cce1SFam Zheng bdrv_refresh_limits(bs); 11578d24cce1SFam Zheng } 11588d24cce1SFam Zheng 115931ca6d07SKevin Wolf /* 116031ca6d07SKevin Wolf * Opens the backing file for a BlockDriverState if not yet open 116131ca6d07SKevin Wolf * 116231ca6d07SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 116331ca6d07SKevin Wolf * empty set of options. The reference to the QDict is transferred to this 116431ca6d07SKevin Wolf * function (even on failure), so if the caller intends to reuse the dictionary, 116531ca6d07SKevin Wolf * it needs to use QINCREF() before calling bdrv_file_open. 116631ca6d07SKevin Wolf */ 116734b5d2c6SMax Reitz int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) 11689156df12SPaolo Bonzini { 11691ba4b6a5SBenoît Canet char *backing_filename = g_malloc0(PATH_MAX); 1170317fc44eSKevin Wolf int ret = 0; 11719156df12SPaolo Bonzini BlockDriver *back_drv = NULL; 11728d24cce1SFam Zheng BlockDriverState *backing_hd; 117334b5d2c6SMax Reitz Error *local_err = NULL; 11749156df12SPaolo Bonzini 11759156df12SPaolo Bonzini if (bs->backing_hd != NULL) { 117631ca6d07SKevin Wolf QDECREF(options); 11771ba4b6a5SBenoît Canet goto free_exit; 11789156df12SPaolo Bonzini } 11799156df12SPaolo Bonzini 118031ca6d07SKevin Wolf /* NULL means an empty set of options */ 118131ca6d07SKevin Wolf if (options == NULL) { 118231ca6d07SKevin Wolf options = qdict_new(); 118331ca6d07SKevin Wolf } 118431ca6d07SKevin Wolf 11859156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 11861cb6f506SKevin Wolf if (qdict_haskey(options, "file.filename")) { 11871cb6f506SKevin Wolf backing_filename[0] = '\0'; 11881cb6f506SKevin Wolf } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { 118931ca6d07SKevin Wolf QDECREF(options); 11901ba4b6a5SBenoît Canet goto free_exit; 1191dbecebddSFam Zheng } else { 11921ba4b6a5SBenoît Canet bdrv_get_full_backing_filename(bs, backing_filename, PATH_MAX); 11939156df12SPaolo Bonzini } 11949156df12SPaolo Bonzini 11958d24cce1SFam Zheng backing_hd = bdrv_new("", errp); 11968d24cce1SFam Zheng 11979156df12SPaolo Bonzini if (bs->backing_format[0] != '\0') { 11989156df12SPaolo Bonzini back_drv = bdrv_find_format(bs->backing_format); 11999156df12SPaolo Bonzini } 12009156df12SPaolo Bonzini 1201f67503e5SMax Reitz assert(bs->backing_hd == NULL); 12028d24cce1SFam Zheng ret = bdrv_open(&backing_hd, 1203ddf5636dSMax Reitz *backing_filename ? backing_filename : NULL, NULL, options, 1204317fc44eSKevin Wolf bdrv_backing_flags(bs->open_flags), back_drv, &local_err); 12059156df12SPaolo Bonzini if (ret < 0) { 12068d24cce1SFam Zheng bdrv_unref(backing_hd); 12078d24cce1SFam Zheng backing_hd = NULL; 12089156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 1209b04b6b6eSFam Zheng error_setg(errp, "Could not open backing file: %s", 1210b04b6b6eSFam Zheng error_get_pretty(local_err)); 1211b04b6b6eSFam Zheng error_free(local_err); 12121ba4b6a5SBenoît Canet goto free_exit; 12139156df12SPaolo Bonzini } 12148d24cce1SFam Zheng bdrv_set_backing_hd(bs, backing_hd); 1215d80ac658SPeter Feiner 12161ba4b6a5SBenoît Canet free_exit: 12171ba4b6a5SBenoît Canet g_free(backing_filename); 12181ba4b6a5SBenoît Canet return ret; 12199156df12SPaolo Bonzini } 12209156df12SPaolo Bonzini 1221b6ce07aaSKevin Wolf /* 1222da557aacSMax Reitz * Opens a disk image whose options are given as BlockdevRef in another block 1223da557aacSMax Reitz * device's options. 1224da557aacSMax Reitz * 1225da557aacSMax Reitz * If allow_none is true, no image will be opened if filename is false and no 1226da557aacSMax Reitz * BlockdevRef is given. *pbs will remain unchanged and 0 will be returned. 1227da557aacSMax Reitz * 1228da557aacSMax Reitz * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. 1229da557aacSMax Reitz * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict 1230da557aacSMax Reitz * itself, all options starting with "${bdref_key}." are considered part of the 1231da557aacSMax Reitz * BlockdevRef. 1232da557aacSMax Reitz * 1233da557aacSMax Reitz * The BlockdevRef will be removed from the options QDict. 1234f67503e5SMax Reitz * 1235f67503e5SMax Reitz * To conform with the behavior of bdrv_open(), *pbs has to be NULL. 1236da557aacSMax Reitz */ 1237da557aacSMax Reitz int bdrv_open_image(BlockDriverState **pbs, const char *filename, 1238da557aacSMax Reitz QDict *options, const char *bdref_key, int flags, 1239f7d9fd8cSMax Reitz bool allow_none, Error **errp) 1240da557aacSMax Reitz { 1241da557aacSMax Reitz QDict *image_options; 1242da557aacSMax Reitz int ret; 1243da557aacSMax Reitz char *bdref_key_dot; 1244da557aacSMax Reitz const char *reference; 1245da557aacSMax Reitz 1246f67503e5SMax Reitz assert(pbs); 1247f67503e5SMax Reitz assert(*pbs == NULL); 1248f67503e5SMax Reitz 1249da557aacSMax Reitz bdref_key_dot = g_strdup_printf("%s.", bdref_key); 1250da557aacSMax Reitz qdict_extract_subqdict(options, &image_options, bdref_key_dot); 1251da557aacSMax Reitz g_free(bdref_key_dot); 1252da557aacSMax Reitz 1253da557aacSMax Reitz reference = qdict_get_try_str(options, bdref_key); 1254da557aacSMax Reitz if (!filename && !reference && !qdict_size(image_options)) { 1255da557aacSMax Reitz if (allow_none) { 1256da557aacSMax Reitz ret = 0; 1257da557aacSMax Reitz } else { 1258da557aacSMax Reitz error_setg(errp, "A block device must be specified for \"%s\"", 1259da557aacSMax Reitz bdref_key); 1260da557aacSMax Reitz ret = -EINVAL; 1261da557aacSMax Reitz } 1262b20e61e0SMarkus Armbruster QDECREF(image_options); 1263da557aacSMax Reitz goto done; 1264da557aacSMax Reitz } 1265da557aacSMax Reitz 1266f7d9fd8cSMax Reitz ret = bdrv_open(pbs, filename, reference, image_options, flags, NULL, errp); 1267da557aacSMax Reitz 1268da557aacSMax Reitz done: 1269da557aacSMax Reitz qdict_del(options, bdref_key); 1270da557aacSMax Reitz return ret; 1271da557aacSMax Reitz } 1272da557aacSMax Reitz 1273b1e6fc08SKevin Wolf void bdrv_append_temp_snapshot(BlockDriverState *bs, int flags, Error **errp) 1274b998875dSKevin Wolf { 1275b998875dSKevin Wolf /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 12761ba4b6a5SBenoît Canet char *tmp_filename = g_malloc0(PATH_MAX + 1); 1277b998875dSKevin Wolf int64_t total_size; 1278b998875dSKevin Wolf BlockDriver *bdrv_qcow2; 127983d0521aSChunyan Liu QemuOpts *opts = NULL; 1280b998875dSKevin Wolf QDict *snapshot_options; 1281b998875dSKevin Wolf BlockDriverState *bs_snapshot; 1282b998875dSKevin Wolf Error *local_err; 1283b998875dSKevin Wolf int ret; 1284b998875dSKevin Wolf 1285b998875dSKevin Wolf /* if snapshot, we create a temporary backing file and open it 1286b998875dSKevin Wolf instead of opening 'filename' directly */ 1287b998875dSKevin Wolf 1288b998875dSKevin Wolf /* Get the required size from the image */ 1289f187743aSKevin Wolf total_size = bdrv_getlength(bs); 1290f187743aSKevin Wolf if (total_size < 0) { 1291f187743aSKevin Wolf error_setg_errno(errp, -total_size, "Could not get image size"); 12921ba4b6a5SBenoît Canet goto out; 1293f187743aSKevin Wolf } 1294f187743aSKevin Wolf total_size &= BDRV_SECTOR_MASK; 1295b998875dSKevin Wolf 1296b998875dSKevin Wolf /* Create the temporary image */ 12971ba4b6a5SBenoît Canet ret = get_tmp_filename(tmp_filename, PATH_MAX + 1); 1298b998875dSKevin Wolf if (ret < 0) { 1299b998875dSKevin Wolf error_setg_errno(errp, -ret, "Could not get temporary filename"); 13001ba4b6a5SBenoît Canet goto out; 1301b998875dSKevin Wolf } 1302b998875dSKevin Wolf 1303b998875dSKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 1304c282e1fdSChunyan Liu opts = qemu_opts_create(bdrv_qcow2->create_opts, NULL, 0, 1305c282e1fdSChunyan Liu &error_abort); 130683d0521aSChunyan Liu qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_size); 1307c282e1fdSChunyan Liu ret = bdrv_create(bdrv_qcow2, tmp_filename, opts, &local_err); 130883d0521aSChunyan Liu qemu_opts_del(opts); 1309b998875dSKevin Wolf if (ret < 0) { 1310b998875dSKevin Wolf error_setg_errno(errp, -ret, "Could not create temporary overlay " 1311b998875dSKevin Wolf "'%s': %s", tmp_filename, 1312b998875dSKevin Wolf error_get_pretty(local_err)); 1313b998875dSKevin Wolf error_free(local_err); 13141ba4b6a5SBenoît Canet goto out; 1315b998875dSKevin Wolf } 1316b998875dSKevin Wolf 1317b998875dSKevin Wolf /* Prepare a new options QDict for the temporary file */ 1318b998875dSKevin Wolf snapshot_options = qdict_new(); 1319b998875dSKevin Wolf qdict_put(snapshot_options, "file.driver", 1320b998875dSKevin Wolf qstring_from_str("file")); 1321b998875dSKevin Wolf qdict_put(snapshot_options, "file.filename", 1322b998875dSKevin Wolf qstring_from_str(tmp_filename)); 1323b998875dSKevin Wolf 132498522f63SKevin Wolf bs_snapshot = bdrv_new("", &error_abort); 1325b998875dSKevin Wolf 1326b998875dSKevin Wolf ret = bdrv_open(&bs_snapshot, NULL, NULL, snapshot_options, 1327b1e6fc08SKevin Wolf flags, bdrv_qcow2, &local_err); 1328b998875dSKevin Wolf if (ret < 0) { 1329b998875dSKevin Wolf error_propagate(errp, local_err); 13301ba4b6a5SBenoît Canet goto out; 1331b998875dSKevin Wolf } 1332b998875dSKevin Wolf 1333b998875dSKevin Wolf bdrv_append(bs_snapshot, bs); 13341ba4b6a5SBenoît Canet 13351ba4b6a5SBenoît Canet out: 13361ba4b6a5SBenoît Canet g_free(tmp_filename); 1337b998875dSKevin Wolf } 1338b998875dSKevin Wolf 1339da557aacSMax Reitz /* 1340b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 1341de9c0cecSKevin Wolf * 1342de9c0cecSKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 1343de9c0cecSKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 1344de9c0cecSKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 1345de9c0cecSKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_open. 1346f67503e5SMax Reitz * 1347f67503e5SMax Reitz * If *pbs is NULL, a new BDS will be created with a pointer to it stored there. 1348f67503e5SMax Reitz * If it is not NULL, the referenced BDS will be reused. 1349ddf5636dSMax Reitz * 1350ddf5636dSMax Reitz * The reference parameter may be used to specify an existing block device which 1351ddf5636dSMax Reitz * should be opened. If specified, neither options nor a filename may be given, 1352ddf5636dSMax Reitz * nor can an existing BDS be reused (that is, *pbs has to be NULL). 1353b6ce07aaSKevin Wolf */ 1354ddf5636dSMax Reitz int bdrv_open(BlockDriverState **pbs, const char *filename, 1355ddf5636dSMax Reitz const char *reference, QDict *options, int flags, 1356ddf5636dSMax Reitz BlockDriver *drv, Error **errp) 1357ea2384d3Sbellard { 1358b6ce07aaSKevin Wolf int ret; 1359f67503e5SMax Reitz BlockDriverState *file = NULL, *bs; 136074fe54f2SKevin Wolf const char *drvname; 136134b5d2c6SMax Reitz Error *local_err = NULL; 1362b1e6fc08SKevin Wolf int snapshot_flags = 0; 136333e3963eSbellard 1364f67503e5SMax Reitz assert(pbs); 1365f67503e5SMax Reitz 1366ddf5636dSMax Reitz if (reference) { 1367ddf5636dSMax Reitz bool options_non_empty = options ? qdict_size(options) : false; 1368ddf5636dSMax Reitz QDECREF(options); 1369ddf5636dSMax Reitz 1370ddf5636dSMax Reitz if (*pbs) { 1371ddf5636dSMax Reitz error_setg(errp, "Cannot reuse an existing BDS when referencing " 1372ddf5636dSMax Reitz "another block device"); 1373ddf5636dSMax Reitz return -EINVAL; 1374ddf5636dSMax Reitz } 1375ddf5636dSMax Reitz 1376ddf5636dSMax Reitz if (filename || options_non_empty) { 1377ddf5636dSMax Reitz error_setg(errp, "Cannot reference an existing block device with " 1378ddf5636dSMax Reitz "additional options or a new filename"); 1379ddf5636dSMax Reitz return -EINVAL; 1380ddf5636dSMax Reitz } 1381ddf5636dSMax Reitz 1382ddf5636dSMax Reitz bs = bdrv_lookup_bs(reference, reference, errp); 1383ddf5636dSMax Reitz if (!bs) { 1384ddf5636dSMax Reitz return -ENODEV; 1385ddf5636dSMax Reitz } 1386ddf5636dSMax Reitz bdrv_ref(bs); 1387ddf5636dSMax Reitz *pbs = bs; 1388ddf5636dSMax Reitz return 0; 1389ddf5636dSMax Reitz } 1390ddf5636dSMax Reitz 1391f67503e5SMax Reitz if (*pbs) { 1392f67503e5SMax Reitz bs = *pbs; 1393f67503e5SMax Reitz } else { 139498522f63SKevin Wolf bs = bdrv_new("", &error_abort); 1395f67503e5SMax Reitz } 1396f67503e5SMax Reitz 1397de9c0cecSKevin Wolf /* NULL means an empty set of options */ 1398de9c0cecSKevin Wolf if (options == NULL) { 1399de9c0cecSKevin Wolf options = qdict_new(); 1400de9c0cecSKevin Wolf } 1401de9c0cecSKevin Wolf 140217b005f1SKevin Wolf ret = bdrv_fill_options(&options, &filename, flags, drv, &local_err); 1403462f5bcfSKevin Wolf if (local_err) { 1404462f5bcfSKevin Wolf goto fail; 1405462f5bcfSKevin Wolf } 1406462f5bcfSKevin Wolf 1407*76c591b0SKevin Wolf /* Find the right image format driver */ 1408*76c591b0SKevin Wolf drv = NULL; 1409*76c591b0SKevin Wolf drvname = qdict_get_try_str(options, "driver"); 1410*76c591b0SKevin Wolf if (drvname) { 1411*76c591b0SKevin Wolf drv = bdrv_find_format(drvname); 1412*76c591b0SKevin Wolf qdict_del(options, "driver"); 1413*76c591b0SKevin Wolf if (!drv) { 1414*76c591b0SKevin Wolf error_setg(errp, "Unknown driver: '%s'", drvname); 1415*76c591b0SKevin Wolf ret = -EINVAL; 1416*76c591b0SKevin Wolf goto fail; 1417*76c591b0SKevin Wolf } 1418*76c591b0SKevin Wolf } 1419*76c591b0SKevin Wolf 1420*76c591b0SKevin Wolf assert(drvname || !(flags & BDRV_O_PROTOCOL)); 1421*76c591b0SKevin Wolf if (drv && !drv->bdrv_file_open) { 1422*76c591b0SKevin Wolf /* If the user explicitly wants a format driver here, we'll need to add 1423*76c591b0SKevin Wolf * another layer for the protocol in bs->file */ 1424*76c591b0SKevin Wolf flags &= ~BDRV_O_PROTOCOL; 1425*76c591b0SKevin Wolf } 1426*76c591b0SKevin Wolf 1427de9c0cecSKevin Wolf bs->options = options; 1428b6ad491aSKevin Wolf options = qdict_clone_shallow(options); 1429de9c0cecSKevin Wolf 1430f500a6d3SKevin Wolf /* Open image file without format layer */ 1431f4788adcSKevin Wolf if ((flags & BDRV_O_PROTOCOL) == 0) { 1432be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 1433be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 1434be028adcSJeff Cody } 1435b1e6fc08SKevin Wolf if (flags & BDRV_O_SNAPSHOT) { 1436b1e6fc08SKevin Wolf snapshot_flags = bdrv_temp_snapshot_flags(flags); 1437b1e6fc08SKevin Wolf flags = bdrv_backing_flags(flags); 1438b1e6fc08SKevin Wolf } 1439be028adcSJeff Cody 1440f67503e5SMax Reitz assert(file == NULL); 1441054963f8SMax Reitz ret = bdrv_open_image(&file, filename, options, "file", 14420b50cc88SKevin Wolf bdrv_inherited_flags(flags), 14430b50cc88SKevin Wolf true, &local_err); 1444f500a6d3SKevin Wolf if (ret < 0) { 14458bfea15dSKevin Wolf goto fail; 1446f500a6d3SKevin Wolf } 1447f4788adcSKevin Wolf } 1448f500a6d3SKevin Wolf 1449*76c591b0SKevin Wolf /* Image format probing */ 1450*76c591b0SKevin Wolf if (!drv && file) { 145134b5d2c6SMax Reitz ret = find_image_format(file, filename, &drv, &local_err); 145217b005f1SKevin Wolf if (ret < 0) { 145317b005f1SKevin Wolf goto fail; 145417b005f1SKevin Wolf } 1455*76c591b0SKevin Wolf } else if (!drv) { 14562a05cbe4SMax Reitz error_setg(errp, "Must specify either driver or file"); 14572a05cbe4SMax Reitz ret = -EINVAL; 14588bfea15dSKevin Wolf goto fail; 14592a05cbe4SMax Reitz } 1460f500a6d3SKevin Wolf 1461b6ce07aaSKevin Wolf /* Open the image */ 1462f4788adcSKevin Wolf if (flags & BDRV_O_PROTOCOL) { 1463b348f331SKevin Wolf ret = bdrv_open_common(bs, NULL, options, 1464b348f331SKevin Wolf flags & ~BDRV_O_PROTOCOL, drv, &local_err); 1465f4788adcSKevin Wolf if (!ret) { 1466b348f331SKevin Wolf bs->growable = 1; 1467f4788adcSKevin Wolf goto done; 1468f4788adcSKevin Wolf } else if (bs->drv) { 1469f4788adcSKevin Wolf goto close_and_fail; 1470f4788adcSKevin Wolf } else { 1471f4788adcSKevin Wolf goto fail; 1472f4788adcSKevin Wolf } 1473f4788adcSKevin Wolf } 1474f4788adcSKevin Wolf 147534b5d2c6SMax Reitz ret = bdrv_open_common(bs, file, options, flags, drv, &local_err); 1476b6ce07aaSKevin Wolf if (ret < 0) { 14778bfea15dSKevin Wolf goto fail; 14786987307cSChristoph Hellwig } 14796987307cSChristoph Hellwig 14802a05cbe4SMax Reitz if (file && (bs->file != file)) { 14814f6fd349SFam Zheng bdrv_unref(file); 1482f500a6d3SKevin Wolf file = NULL; 1483f500a6d3SKevin Wolf } 1484f500a6d3SKevin Wolf 1485b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 14869156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 148731ca6d07SKevin Wolf QDict *backing_options; 148831ca6d07SKevin Wolf 14895726d872SBenoît Canet qdict_extract_subqdict(options, &backing_options, "backing."); 149034b5d2c6SMax Reitz ret = bdrv_open_backing_file(bs, backing_options, &local_err); 1491b6ce07aaSKevin Wolf if (ret < 0) { 1492b6ad491aSKevin Wolf goto close_and_fail; 1493b6ce07aaSKevin Wolf } 1494b6ce07aaSKevin Wolf } 1495b6ce07aaSKevin Wolf 1496b998875dSKevin Wolf /* For snapshot=on, create a temporary qcow2 overlay. bs points to the 1497b998875dSKevin Wolf * temporary snapshot afterwards. */ 1498b1e6fc08SKevin Wolf if (snapshot_flags) { 1499b1e6fc08SKevin Wolf bdrv_append_temp_snapshot(bs, snapshot_flags, &local_err); 1500b998875dSKevin Wolf if (local_err) { 1501b998875dSKevin Wolf error_propagate(errp, local_err); 1502b998875dSKevin Wolf goto close_and_fail; 1503b998875dSKevin Wolf } 1504b998875dSKevin Wolf } 1505b998875dSKevin Wolf 1506b998875dSKevin Wolf 15075acd9d81SMax Reitz done: 1508b6ad491aSKevin Wolf /* Check if any unknown options were used */ 15095acd9d81SMax Reitz if (options && (qdict_size(options) != 0)) { 1510b6ad491aSKevin Wolf const QDictEntry *entry = qdict_first(options); 15115acd9d81SMax Reitz if (flags & BDRV_O_PROTOCOL) { 15125acd9d81SMax Reitz error_setg(errp, "Block protocol '%s' doesn't support the option " 15135acd9d81SMax Reitz "'%s'", drv->format_name, entry->key); 15145acd9d81SMax Reitz } else { 151534b5d2c6SMax Reitz error_setg(errp, "Block format '%s' used by device '%s' doesn't " 15165acd9d81SMax Reitz "support the option '%s'", drv->format_name, 15175acd9d81SMax Reitz bs->device_name, entry->key); 15185acd9d81SMax Reitz } 1519b6ad491aSKevin Wolf 1520b6ad491aSKevin Wolf ret = -EINVAL; 1521b6ad491aSKevin Wolf goto close_and_fail; 1522b6ad491aSKevin Wolf } 1523b6ad491aSKevin Wolf 1524b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 15257d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 1526c3adb58fSMarkus Armbruster } else if (!runstate_check(RUN_STATE_PRELAUNCH) 1527c3adb58fSMarkus Armbruster && !runstate_check(RUN_STATE_INMIGRATE) 1528c3adb58fSMarkus Armbruster && !runstate_check(RUN_STATE_PAUSED)) { /* HACK */ 1529c3adb58fSMarkus Armbruster error_setg(errp, 1530c3adb58fSMarkus Armbruster "Guest must be stopped for opening of encrypted image"); 1531c3adb58fSMarkus Armbruster ret = -EBUSY; 1532c3adb58fSMarkus Armbruster goto close_and_fail; 1533b6ce07aaSKevin Wolf } 1534b6ce07aaSKevin Wolf 1535c3adb58fSMarkus Armbruster QDECREF(options); 1536f67503e5SMax Reitz *pbs = bs; 1537b6ce07aaSKevin Wolf return 0; 1538b6ce07aaSKevin Wolf 15398bfea15dSKevin Wolf fail: 1540f500a6d3SKevin Wolf if (file != NULL) { 15414f6fd349SFam Zheng bdrv_unref(file); 1542f500a6d3SKevin Wolf } 1543de9c0cecSKevin Wolf QDECREF(bs->options); 1544b6ad491aSKevin Wolf QDECREF(options); 1545de9c0cecSKevin Wolf bs->options = NULL; 1546f67503e5SMax Reitz if (!*pbs) { 1547f67503e5SMax Reitz /* If *pbs is NULL, a new BDS has been created in this function and 1548f67503e5SMax Reitz needs to be freed now. Otherwise, it does not need to be closed, 1549f67503e5SMax Reitz since it has not really been opened yet. */ 1550f67503e5SMax Reitz bdrv_unref(bs); 1551f67503e5SMax Reitz } 155284d18f06SMarkus Armbruster if (local_err) { 155334b5d2c6SMax Reitz error_propagate(errp, local_err); 155434b5d2c6SMax Reitz } 1555b6ad491aSKevin Wolf return ret; 1556de9c0cecSKevin Wolf 1557b6ad491aSKevin Wolf close_and_fail: 1558f67503e5SMax Reitz /* See fail path, but now the BDS has to be always closed */ 1559f67503e5SMax Reitz if (*pbs) { 1560b6ad491aSKevin Wolf bdrv_close(bs); 1561f67503e5SMax Reitz } else { 1562f67503e5SMax Reitz bdrv_unref(bs); 1563f67503e5SMax Reitz } 1564b6ad491aSKevin Wolf QDECREF(options); 156584d18f06SMarkus Armbruster if (local_err) { 156634b5d2c6SMax Reitz error_propagate(errp, local_err); 156734b5d2c6SMax Reitz } 1568b6ce07aaSKevin Wolf return ret; 1569b6ce07aaSKevin Wolf } 1570b6ce07aaSKevin Wolf 1571e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 1572e971aa12SJeff Cody bool prepared; 1573e971aa12SJeff Cody BDRVReopenState state; 1574e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 1575e971aa12SJeff Cody } BlockReopenQueueEntry; 1576e971aa12SJeff Cody 1577e971aa12SJeff Cody /* 1578e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 1579e971aa12SJeff Cody * reopen of multiple devices. 1580e971aa12SJeff Cody * 1581e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 1582e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 1583e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 1584e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 1585e971aa12SJeff Cody * atomic 'set'. 1586e971aa12SJeff Cody * 1587e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 1588e971aa12SJeff Cody * 1589e971aa12SJeff Cody * flags contains the open flags for the associated bs 1590e971aa12SJeff Cody * 1591e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 1592e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 1593e971aa12SJeff Cody * 1594e971aa12SJeff Cody */ 1595e971aa12SJeff Cody BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 1596e971aa12SJeff Cody BlockDriverState *bs, int flags) 1597e971aa12SJeff Cody { 1598e971aa12SJeff Cody assert(bs != NULL); 1599e971aa12SJeff Cody 1600e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 1601e971aa12SJeff Cody if (bs_queue == NULL) { 1602e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 1603e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 1604e971aa12SJeff Cody } 1605e971aa12SJeff Cody 1606f1f25a2eSKevin Wolf /* bdrv_open() masks this flag out */ 1607f1f25a2eSKevin Wolf flags &= ~BDRV_O_PROTOCOL; 1608f1f25a2eSKevin Wolf 1609e971aa12SJeff Cody if (bs->file) { 1610f1f25a2eSKevin Wolf bdrv_reopen_queue(bs_queue, bs->file, bdrv_inherited_flags(flags)); 1611e971aa12SJeff Cody } 1612e971aa12SJeff Cody 1613e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 1614e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 1615e971aa12SJeff Cody 1616e971aa12SJeff Cody bs_entry->state.bs = bs; 1617e971aa12SJeff Cody bs_entry->state.flags = flags; 1618e971aa12SJeff Cody 1619e971aa12SJeff Cody return bs_queue; 1620e971aa12SJeff Cody } 1621e971aa12SJeff Cody 1622e971aa12SJeff Cody /* 1623e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 1624e971aa12SJeff Cody * 1625e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 1626e971aa12SJeff Cody * via bdrv_reopen_queue(). 1627e971aa12SJeff Cody * 1628e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 1629e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 1630e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 1631e971aa12SJeff Cody * data cleaned up. 1632e971aa12SJeff Cody * 1633e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 1634e971aa12SJeff Cody * to all devices. 1635e971aa12SJeff Cody * 1636e971aa12SJeff Cody */ 1637e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 1638e971aa12SJeff Cody { 1639e971aa12SJeff Cody int ret = -1; 1640e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 1641e971aa12SJeff Cody Error *local_err = NULL; 1642e971aa12SJeff Cody 1643e971aa12SJeff Cody assert(bs_queue != NULL); 1644e971aa12SJeff Cody 1645e971aa12SJeff Cody bdrv_drain_all(); 1646e971aa12SJeff Cody 1647e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1648e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 1649e971aa12SJeff Cody error_propagate(errp, local_err); 1650e971aa12SJeff Cody goto cleanup; 1651e971aa12SJeff Cody } 1652e971aa12SJeff Cody bs_entry->prepared = true; 1653e971aa12SJeff Cody } 1654e971aa12SJeff Cody 1655e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 1656e971aa12SJeff Cody * changes 1657e971aa12SJeff Cody */ 1658e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1659e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 1660e971aa12SJeff Cody } 1661e971aa12SJeff Cody 1662e971aa12SJeff Cody ret = 0; 1663e971aa12SJeff Cody 1664e971aa12SJeff Cody cleanup: 1665e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 1666e971aa12SJeff Cody if (ret && bs_entry->prepared) { 1667e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 1668e971aa12SJeff Cody } 1669e971aa12SJeff Cody g_free(bs_entry); 1670e971aa12SJeff Cody } 1671e971aa12SJeff Cody g_free(bs_queue); 1672e971aa12SJeff Cody return ret; 1673e971aa12SJeff Cody } 1674e971aa12SJeff Cody 1675e971aa12SJeff Cody 1676e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 1677e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1678e971aa12SJeff Cody { 1679e971aa12SJeff Cody int ret = -1; 1680e971aa12SJeff Cody Error *local_err = NULL; 1681e971aa12SJeff Cody BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); 1682e971aa12SJeff Cody 1683e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1684e971aa12SJeff Cody if (local_err != NULL) { 1685e971aa12SJeff Cody error_propagate(errp, local_err); 1686e971aa12SJeff Cody } 1687e971aa12SJeff Cody return ret; 1688e971aa12SJeff Cody } 1689e971aa12SJeff Cody 1690e971aa12SJeff Cody 1691e971aa12SJeff Cody /* 1692e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1693e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1694e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1695e971aa12SJeff Cody * 1696e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1697e971aa12SJeff Cody * flags are the new open flags 1698e971aa12SJeff Cody * queue is the reopen queue 1699e971aa12SJeff Cody * 1700e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1701e971aa12SJeff Cody * as well. 1702e971aa12SJeff Cody * 1703e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1704e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1705e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1706e971aa12SJeff Cody * 1707e971aa12SJeff Cody */ 1708e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1709e971aa12SJeff Cody Error **errp) 1710e971aa12SJeff Cody { 1711e971aa12SJeff Cody int ret = -1; 1712e971aa12SJeff Cody Error *local_err = NULL; 1713e971aa12SJeff Cody BlockDriver *drv; 1714e971aa12SJeff Cody 1715e971aa12SJeff Cody assert(reopen_state != NULL); 1716e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1717e971aa12SJeff Cody drv = reopen_state->bs->drv; 1718e971aa12SJeff Cody 1719e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 1720e971aa12SJeff Cody * to r/w */ 1721e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 1722e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 1723e971aa12SJeff Cody error_set(errp, QERR_DEVICE_IS_READ_ONLY, 1724e971aa12SJeff Cody reopen_state->bs->device_name); 1725e971aa12SJeff Cody goto error; 1726e971aa12SJeff Cody } 1727e971aa12SJeff Cody 1728e971aa12SJeff Cody 1729e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 1730e971aa12SJeff Cody if (ret) { 1731e971aa12SJeff Cody error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", 1732e971aa12SJeff Cody strerror(-ret)); 1733e971aa12SJeff Cody goto error; 1734e971aa12SJeff Cody } 1735e971aa12SJeff Cody 1736e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 1737e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 1738e971aa12SJeff Cody if (ret) { 1739e971aa12SJeff Cody if (local_err != NULL) { 1740e971aa12SJeff Cody error_propagate(errp, local_err); 1741e971aa12SJeff Cody } else { 1742d8b6895fSLuiz Capitulino error_setg(errp, "failed while preparing to reopen image '%s'", 1743e971aa12SJeff Cody reopen_state->bs->filename); 1744e971aa12SJeff Cody } 1745e971aa12SJeff Cody goto error; 1746e971aa12SJeff Cody } 1747e971aa12SJeff Cody } else { 1748e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 1749e971aa12SJeff Cody * handler for each supported drv. */ 1750e971aa12SJeff Cody error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 1751e971aa12SJeff Cody drv->format_name, reopen_state->bs->device_name, 1752e971aa12SJeff Cody "reopening of file"); 1753e971aa12SJeff Cody ret = -1; 1754e971aa12SJeff Cody goto error; 1755e971aa12SJeff Cody } 1756e971aa12SJeff Cody 1757e971aa12SJeff Cody ret = 0; 1758e971aa12SJeff Cody 1759e971aa12SJeff Cody error: 1760e971aa12SJeff Cody return ret; 1761e971aa12SJeff Cody } 1762e971aa12SJeff Cody 1763e971aa12SJeff Cody /* 1764e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 1765e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 1766e971aa12SJeff Cody * the active BlockDriverState contents. 1767e971aa12SJeff Cody */ 1768e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 1769e971aa12SJeff Cody { 1770e971aa12SJeff Cody BlockDriver *drv; 1771e971aa12SJeff Cody 1772e971aa12SJeff Cody assert(reopen_state != NULL); 1773e971aa12SJeff Cody drv = reopen_state->bs->drv; 1774e971aa12SJeff Cody assert(drv != NULL); 1775e971aa12SJeff Cody 1776e971aa12SJeff Cody /* If there are any driver level actions to take */ 1777e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 1778e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 1779e971aa12SJeff Cody } 1780e971aa12SJeff Cody 1781e971aa12SJeff Cody /* set BDS specific flags now */ 1782e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 1783e971aa12SJeff Cody reopen_state->bs->enable_write_cache = !!(reopen_state->flags & 1784e971aa12SJeff Cody BDRV_O_CACHE_WB); 1785e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 1786355ef4acSKevin Wolf 1787355ef4acSKevin Wolf bdrv_refresh_limits(reopen_state->bs); 1788e971aa12SJeff Cody } 1789e971aa12SJeff Cody 1790e971aa12SJeff Cody /* 1791e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 1792e971aa12SJeff Cody * reopen_state 1793e971aa12SJeff Cody */ 1794e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 1795e971aa12SJeff Cody { 1796e971aa12SJeff Cody BlockDriver *drv; 1797e971aa12SJeff Cody 1798e971aa12SJeff Cody assert(reopen_state != NULL); 1799e971aa12SJeff Cody drv = reopen_state->bs->drv; 1800e971aa12SJeff Cody assert(drv != NULL); 1801e971aa12SJeff Cody 1802e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 1803e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 1804e971aa12SJeff Cody } 1805e971aa12SJeff Cody } 1806e971aa12SJeff Cody 1807e971aa12SJeff Cody 1808fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 1809fc01f7e7Sbellard { 18103e914655SPaolo Bonzini if (bs->job) { 18113e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 18123e914655SPaolo Bonzini } 181358fda173SStefan Hajnoczi bdrv_drain_all(); /* complete I/O */ 181458fda173SStefan Hajnoczi bdrv_flush(bs); 181558fda173SStefan Hajnoczi bdrv_drain_all(); /* in case flush left pending I/O */ 1816d7d512f6SPaolo Bonzini notifier_list_notify(&bs->close_notifiers, bs); 18177094f12fSKevin Wolf 18183cbc002cSPaolo Bonzini if (bs->drv) { 1819557df6acSStefan Hajnoczi if (bs->backing_hd) { 1820826b6ca0SFam Zheng BlockDriverState *backing_hd = bs->backing_hd; 1821826b6ca0SFam Zheng bdrv_set_backing_hd(bs, NULL); 1822826b6ca0SFam Zheng bdrv_unref(backing_hd); 1823557df6acSStefan Hajnoczi } 1824ea2384d3Sbellard bs->drv->bdrv_close(bs); 18257267c094SAnthony Liguori g_free(bs->opaque); 1826ea2384d3Sbellard bs->opaque = NULL; 1827ea2384d3Sbellard bs->drv = NULL; 182853fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 1829a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 1830a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 18316405875cSPaolo Bonzini bs->total_sectors = 0; 18326405875cSPaolo Bonzini bs->encrypted = 0; 18336405875cSPaolo Bonzini bs->valid_key = 0; 18346405875cSPaolo Bonzini bs->sg = 0; 18356405875cSPaolo Bonzini bs->growable = 0; 18360d51b4deSAsias He bs->zero_beyond_eof = false; 1837de9c0cecSKevin Wolf QDECREF(bs->options); 1838de9c0cecSKevin Wolf bs->options = NULL; 1839b338082bSbellard 184066f82ceeSKevin Wolf if (bs->file != NULL) { 18414f6fd349SFam Zheng bdrv_unref(bs->file); 18420ac9377dSPaolo Bonzini bs->file = NULL; 184366f82ceeSKevin Wolf } 18449ca11154SPavel Hrdina } 184566f82ceeSKevin Wolf 18467d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 184798f90dbaSZhi Yong Wu 184898f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 184998f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 185098f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 185198f90dbaSZhi Yong Wu } 1852b338082bSbellard } 1853b338082bSbellard 18542bc93fedSMORITA Kazutaka void bdrv_close_all(void) 18552bc93fedSMORITA Kazutaka { 18562bc93fedSMORITA Kazutaka BlockDriverState *bs; 18572bc93fedSMORITA Kazutaka 1858dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 1859ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 1860ed78cda3SStefan Hajnoczi 1861ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 18622bc93fedSMORITA Kazutaka bdrv_close(bs); 1863ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 18642bc93fedSMORITA Kazutaka } 18652bc93fedSMORITA Kazutaka } 18662bc93fedSMORITA Kazutaka 186788266f5aSStefan Hajnoczi /* Check if any requests are in-flight (including throttled requests) */ 186888266f5aSStefan Hajnoczi static bool bdrv_requests_pending(BlockDriverState *bs) 186988266f5aSStefan Hajnoczi { 187088266f5aSStefan Hajnoczi if (!QLIST_EMPTY(&bs->tracked_requests)) { 187188266f5aSStefan Hajnoczi return true; 187288266f5aSStefan Hajnoczi } 1873cc0681c4SBenoît Canet if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) { 1874cc0681c4SBenoît Canet return true; 1875cc0681c4SBenoît Canet } 1876cc0681c4SBenoît Canet if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) { 187788266f5aSStefan Hajnoczi return true; 187888266f5aSStefan Hajnoczi } 187988266f5aSStefan Hajnoczi if (bs->file && bdrv_requests_pending(bs->file)) { 188088266f5aSStefan Hajnoczi return true; 188188266f5aSStefan Hajnoczi } 188288266f5aSStefan Hajnoczi if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) { 188388266f5aSStefan Hajnoczi return true; 188488266f5aSStefan Hajnoczi } 188588266f5aSStefan Hajnoczi return false; 188688266f5aSStefan Hajnoczi } 188788266f5aSStefan Hajnoczi 1888922453bcSStefan Hajnoczi /* 1889922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 1890922453bcSStefan Hajnoczi * 1891922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 1892922453bcSStefan Hajnoczi * after calling this function. 18934c355d53SZhi Yong Wu * 18944c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 18954c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 18964c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 18974c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 18984c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 1899922453bcSStefan Hajnoczi */ 1900922453bcSStefan Hajnoczi void bdrv_drain_all(void) 1901922453bcSStefan Hajnoczi { 190288266f5aSStefan Hajnoczi /* Always run first iteration so any pending completion BHs run */ 190388266f5aSStefan Hajnoczi bool busy = true; 1904922453bcSStefan Hajnoczi BlockDriverState *bs; 1905922453bcSStefan Hajnoczi 190688266f5aSStefan Hajnoczi while (busy) { 19079b536adcSStefan Hajnoczi busy = false; 1908922453bcSStefan Hajnoczi 19099b536adcSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 19109b536adcSStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 19119b536adcSStefan Hajnoczi bool bs_busy; 19129b536adcSStefan Hajnoczi 19139b536adcSStefan Hajnoczi aio_context_acquire(aio_context); 19149b536adcSStefan Hajnoczi bdrv_start_throttled_reqs(bs); 19159b536adcSStefan Hajnoczi bs_busy = bdrv_requests_pending(bs); 19169b536adcSStefan Hajnoczi bs_busy |= aio_poll(aio_context, bs_busy); 19179b536adcSStefan Hajnoczi aio_context_release(aio_context); 19189b536adcSStefan Hajnoczi 19199b536adcSStefan Hajnoczi busy |= bs_busy; 19209b536adcSStefan Hajnoczi } 1921922453bcSStefan Hajnoczi } 1922922453bcSStefan Hajnoczi } 1923922453bcSStefan Hajnoczi 1924dc364f4cSBenoît Canet /* make a BlockDriverState anonymous by removing from bdrv_state and 1925dc364f4cSBenoît Canet * graph_bdrv_state list. 1926d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1927d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1928d22b2f41SRyan Harper { 1929d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 1930dc364f4cSBenoît Canet QTAILQ_REMOVE(&bdrv_states, bs, device_list); 1931d22b2f41SRyan Harper } 1932d22b2f41SRyan Harper bs->device_name[0] = '\0'; 1933dc364f4cSBenoît Canet if (bs->node_name[0] != '\0') { 1934dc364f4cSBenoît Canet QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); 1935dc364f4cSBenoît Canet } 1936dc364f4cSBenoît Canet bs->node_name[0] = '\0'; 1937d22b2f41SRyan Harper } 1938d22b2f41SRyan Harper 1939e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1940e023b2e2SPaolo Bonzini { 1941e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1942e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1943e023b2e2SPaolo Bonzini } 1944e023b2e2SPaolo Bonzini } 1945e023b2e2SPaolo Bonzini 19464ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 19474ddc07caSPaolo Bonzini BlockDriverState *bs_src) 19484ddc07caSPaolo Bonzini { 19494ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 19504ddc07caSPaolo Bonzini 19514ddc07caSPaolo Bonzini /* dev info */ 19524ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 19534ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 19544ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 19551b7fd729SPaolo Bonzini bs_dest->guest_block_size = bs_src->guest_block_size; 19564ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 19574ddc07caSPaolo Bonzini 19584ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 19594ddc07caSPaolo Bonzini 1960cc0681c4SBenoît Canet /* i/o throttled req */ 1961cc0681c4SBenoît Canet memcpy(&bs_dest->throttle_state, 1962cc0681c4SBenoît Canet &bs_src->throttle_state, 1963cc0681c4SBenoît Canet sizeof(ThrottleState)); 1964cc0681c4SBenoît Canet bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0]; 1965cc0681c4SBenoît Canet bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1]; 19664ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 19674ddc07caSPaolo Bonzini 19684ddc07caSPaolo Bonzini /* r/w error */ 19694ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 19704ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 19714ddc07caSPaolo Bonzini 19724ddc07caSPaolo Bonzini /* i/o status */ 19734ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 19744ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 19754ddc07caSPaolo Bonzini 19764ddc07caSPaolo Bonzini /* dirty bitmap */ 1977e4654d2dSFam Zheng bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps; 19784ddc07caSPaolo Bonzini 19799fcb0251SFam Zheng /* reference count */ 19809fcb0251SFam Zheng bs_dest->refcnt = bs_src->refcnt; 19819fcb0251SFam Zheng 19824ddc07caSPaolo Bonzini /* job */ 19834ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 19844ddc07caSPaolo Bonzini 19854ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 19864ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 19874ddc07caSPaolo Bonzini bs_src->device_name); 1988dc364f4cSBenoît Canet bs_dest->device_list = bs_src->device_list; 1989fbe40ff7SFam Zheng memcpy(bs_dest->op_blockers, bs_src->op_blockers, 1990fbe40ff7SFam Zheng sizeof(bs_dest->op_blockers)); 19914ddc07caSPaolo Bonzini } 19924ddc07caSPaolo Bonzini 19934ddc07caSPaolo Bonzini /* 19944ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 19954ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 19964ddc07caSPaolo Bonzini * actually attached to a device. 19974ddc07caSPaolo Bonzini * 19984ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 19994ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 20004ddc07caSPaolo Bonzini * 20014ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 20024ddc07caSPaolo Bonzini * 20034ddc07caSPaolo Bonzini * This function does not create any image files. 20044ddc07caSPaolo Bonzini */ 20054ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 20064ddc07caSPaolo Bonzini { 20074ddc07caSPaolo Bonzini BlockDriverState tmp; 20084ddc07caSPaolo Bonzini 200990ce8a06SBenoît Canet /* The code needs to swap the node_name but simply swapping node_list won't 201090ce8a06SBenoît Canet * work so first remove the nodes from the graph list, do the swap then 201190ce8a06SBenoît Canet * insert them back if needed. 201290ce8a06SBenoît Canet */ 201390ce8a06SBenoît Canet if (bs_new->node_name[0] != '\0') { 201490ce8a06SBenoît Canet QTAILQ_REMOVE(&graph_bdrv_states, bs_new, node_list); 201590ce8a06SBenoît Canet } 201690ce8a06SBenoît Canet if (bs_old->node_name[0] != '\0') { 201790ce8a06SBenoît Canet QTAILQ_REMOVE(&graph_bdrv_states, bs_old, node_list); 201890ce8a06SBenoît Canet } 201990ce8a06SBenoît Canet 20204ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 20214ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 2022e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs_new->dirty_bitmaps)); 20234ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 20244ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 20254ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 2026cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 20274ddc07caSPaolo Bonzini 20284ddc07caSPaolo Bonzini tmp = *bs_new; 20294ddc07caSPaolo Bonzini *bs_new = *bs_old; 20304ddc07caSPaolo Bonzini *bs_old = tmp; 20314ddc07caSPaolo Bonzini 20324ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 20334ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 20344ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 20354ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 20364ddc07caSPaolo Bonzini 20374ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 20384ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 20394ddc07caSPaolo Bonzini 20404ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 20414ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 20424ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 20434ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 2044cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 20454ddc07caSPaolo Bonzini 204690ce8a06SBenoît Canet /* insert the nodes back into the graph node list if needed */ 204790ce8a06SBenoît Canet if (bs_new->node_name[0] != '\0') { 204890ce8a06SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs_new, node_list); 204990ce8a06SBenoît Canet } 205090ce8a06SBenoît Canet if (bs_old->node_name[0] != '\0') { 205190ce8a06SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs_old, node_list); 205290ce8a06SBenoît Canet } 205390ce8a06SBenoît Canet 20544ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 20554ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 20564ddc07caSPaolo Bonzini } 20574ddc07caSPaolo Bonzini 20588802d1fdSJeff Cody /* 20598802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 20608802d1fdSJeff Cody * live, while keeping required fields on the top layer. 20618802d1fdSJeff Cody * 20628802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 20638802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 20648802d1fdSJeff Cody * 2065f6801b83SJeff Cody * bs_new is required to be anonymous. 2066f6801b83SJeff Cody * 20678802d1fdSJeff Cody * This function does not create any image files. 20688802d1fdSJeff Cody */ 20698802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 20708802d1fdSJeff Cody { 20714ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 20728802d1fdSJeff Cody 20738802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 20748802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 20758d24cce1SFam Zheng bdrv_set_backing_hd(bs_top, bs_new); 20768802d1fdSJeff Cody } 20778802d1fdSJeff Cody 20784f6fd349SFam Zheng static void bdrv_delete(BlockDriverState *bs) 2079b338082bSbellard { 2080fa879d62SMarkus Armbruster assert(!bs->dev); 20813e914655SPaolo Bonzini assert(!bs->job); 20823718d8abSFam Zheng assert(bdrv_op_blocker_is_empty(bs)); 20834f6fd349SFam Zheng assert(!bs->refcnt); 2084e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 208518846deeSMarkus Armbruster 2086e1b5c52eSStefan Hajnoczi bdrv_close(bs); 2087e1b5c52eSStefan Hajnoczi 20881b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 2089d22b2f41SRyan Harper bdrv_make_anon(bs); 209034c6f050Saurel32 20917267c094SAnthony Liguori g_free(bs); 2092fc01f7e7Sbellard } 2093fc01f7e7Sbellard 2094fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 2095fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 209618846deeSMarkus Armbruster { 2097fa879d62SMarkus Armbruster if (bs->dev) { 209818846deeSMarkus Armbruster return -EBUSY; 209918846deeSMarkus Armbruster } 2100fa879d62SMarkus Armbruster bs->dev = dev; 210128a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 210218846deeSMarkus Armbruster return 0; 210318846deeSMarkus Armbruster } 210418846deeSMarkus Armbruster 2105fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 2106fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 210718846deeSMarkus Armbruster { 2108fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 2109fa879d62SMarkus Armbruster abort(); 2110fa879d62SMarkus Armbruster } 2111fa879d62SMarkus Armbruster } 2112fa879d62SMarkus Armbruster 2113fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 2114fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 2115fa879d62SMarkus Armbruster { 2116fa879d62SMarkus Armbruster assert(bs->dev == dev); 2117fa879d62SMarkus Armbruster bs->dev = NULL; 21180e49de52SMarkus Armbruster bs->dev_ops = NULL; 21190e49de52SMarkus Armbruster bs->dev_opaque = NULL; 21201b7fd729SPaolo Bonzini bs->guest_block_size = 512; 212118846deeSMarkus Armbruster } 212218846deeSMarkus Armbruster 2123fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 2124fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 212518846deeSMarkus Armbruster { 2126fa879d62SMarkus Armbruster return bs->dev; 212718846deeSMarkus Armbruster } 212818846deeSMarkus Armbruster 21290e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 21300e49de52SMarkus Armbruster void *opaque) 21310e49de52SMarkus Armbruster { 21320e49de52SMarkus Armbruster bs->dev_ops = ops; 21330e49de52SMarkus Armbruster bs->dev_opaque = opaque; 21340e49de52SMarkus Armbruster } 21350e49de52SMarkus Armbruster 21367d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 21370e49de52SMarkus Armbruster { 2138145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 21396f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 21407d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 21416f382ed2SLuiz Capitulino if (tray_was_closed) { 21426f382ed2SLuiz Capitulino /* tray open */ 2143a5ee7bd4SWenchao Xia qapi_event_send_device_tray_moved(bdrv_get_device_name(bs), 2144a5ee7bd4SWenchao Xia true, &error_abort); 21456f382ed2SLuiz Capitulino } 21466f382ed2SLuiz Capitulino if (load) { 21476f382ed2SLuiz Capitulino /* tray close */ 2148a5ee7bd4SWenchao Xia qapi_event_send_device_tray_moved(bdrv_get_device_name(bs), 2149a5ee7bd4SWenchao Xia false, &error_abort); 21506f382ed2SLuiz Capitulino } 2151145feb17SMarkus Armbruster } 2152145feb17SMarkus Armbruster } 2153145feb17SMarkus Armbruster 21542c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 21552c6942faSMarkus Armbruster { 21562c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 21572c6942faSMarkus Armbruster } 21582c6942faSMarkus Armbruster 2159025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 2160025ccaa7SPaolo Bonzini { 2161025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 2162025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 2163025ccaa7SPaolo Bonzini } 2164025ccaa7SPaolo Bonzini } 2165025ccaa7SPaolo Bonzini 2166e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 2167e4def80bSMarkus Armbruster { 2168e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 2169e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 2170e4def80bSMarkus Armbruster } 2171e4def80bSMarkus Armbruster return false; 2172e4def80bSMarkus Armbruster } 2173e4def80bSMarkus Armbruster 2174145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 2175145feb17SMarkus Armbruster { 2176145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 2177145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 21780e49de52SMarkus Armbruster } 21790e49de52SMarkus Armbruster } 21800e49de52SMarkus Armbruster 2181f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 2182f107639aSMarkus Armbruster { 2183f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 2184f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 2185f107639aSMarkus Armbruster } 2186f107639aSMarkus Armbruster return false; 2187f107639aSMarkus Armbruster } 2188f107639aSMarkus Armbruster 2189e97fc193Saliguori /* 2190e97fc193Saliguori * Run consistency checks on an image 2191e97fc193Saliguori * 2192e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 2193a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 2194e076f338SKevin Wolf * check are stored in res. 2195e97fc193Saliguori */ 21964534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 2197e97fc193Saliguori { 2198e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 2199e97fc193Saliguori return -ENOTSUP; 2200e97fc193Saliguori } 2201e97fc193Saliguori 2202e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 22034534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 2204e97fc193Saliguori } 2205e97fc193Saliguori 22068a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 22078a426614SKevin Wolf 220833e3963eSbellard /* commit COW file into the raw image */ 220933e3963eSbellard int bdrv_commit(BlockDriverState *bs) 221033e3963eSbellard { 221119cb3738Sbellard BlockDriver *drv = bs->drv; 221272706ea4SJeff Cody int64_t sector, total_sectors, length, backing_length; 22138a426614SKevin Wolf int n, ro, open_flags; 22140bce597dSJeff Cody int ret = 0; 221572706ea4SJeff Cody uint8_t *buf = NULL; 2216c2cba3d9SJim Meyering char filename[PATH_MAX]; 221733e3963eSbellard 221819cb3738Sbellard if (!drv) 221919cb3738Sbellard return -ENOMEDIUM; 222033e3963eSbellard 22214dca4b63SNaphtali Sprei if (!bs->backing_hd) { 22224dca4b63SNaphtali Sprei return -ENOTSUP; 22234dca4b63SNaphtali Sprei } 22244dca4b63SNaphtali Sprei 22253718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_COMMIT, NULL) || 22263718d8abSFam Zheng bdrv_op_is_blocked(bs->backing_hd, BLOCK_OP_TYPE_COMMIT, NULL)) { 22272d3735d3SStefan Hajnoczi return -EBUSY; 22282d3735d3SStefan Hajnoczi } 22292d3735d3SStefan Hajnoczi 22304dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 2231c2cba3d9SJim Meyering /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ 2232c2cba3d9SJim Meyering pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); 22334dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 22344dca4b63SNaphtali Sprei 22354dca4b63SNaphtali Sprei if (ro) { 22360bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 22370bce597dSJeff Cody return -EACCES; 22384dca4b63SNaphtali Sprei } 2239ea2384d3Sbellard } 2240ea2384d3Sbellard 224172706ea4SJeff Cody length = bdrv_getlength(bs); 224272706ea4SJeff Cody if (length < 0) { 224372706ea4SJeff Cody ret = length; 224472706ea4SJeff Cody goto ro_cleanup; 224572706ea4SJeff Cody } 224672706ea4SJeff Cody 224772706ea4SJeff Cody backing_length = bdrv_getlength(bs->backing_hd); 224872706ea4SJeff Cody if (backing_length < 0) { 224972706ea4SJeff Cody ret = backing_length; 225072706ea4SJeff Cody goto ro_cleanup; 225172706ea4SJeff Cody } 225272706ea4SJeff Cody 225372706ea4SJeff Cody /* If our top snapshot is larger than the backing file image, 225472706ea4SJeff Cody * grow the backing file image if possible. If not possible, 225572706ea4SJeff Cody * we must return an error */ 225672706ea4SJeff Cody if (length > backing_length) { 225772706ea4SJeff Cody ret = bdrv_truncate(bs->backing_hd, length); 225872706ea4SJeff Cody if (ret < 0) { 225972706ea4SJeff Cody goto ro_cleanup; 226072706ea4SJeff Cody } 226172706ea4SJeff Cody } 226272706ea4SJeff Cody 226372706ea4SJeff Cody total_sectors = length >> BDRV_SECTOR_BITS; 22647267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 22658a426614SKevin Wolf 22668a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 2267d663640cSPaolo Bonzini ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); 2268d663640cSPaolo Bonzini if (ret < 0) { 2269d663640cSPaolo Bonzini goto ro_cleanup; 2270d663640cSPaolo Bonzini } 2271d663640cSPaolo Bonzini if (ret) { 2272dabfa6ccSKevin Wolf ret = bdrv_read(bs, sector, buf, n); 2273dabfa6ccSKevin Wolf if (ret < 0) { 22744dca4b63SNaphtali Sprei goto ro_cleanup; 227533e3963eSbellard } 227633e3963eSbellard 2277dabfa6ccSKevin Wolf ret = bdrv_write(bs->backing_hd, sector, buf, n); 2278dabfa6ccSKevin Wolf if (ret < 0) { 22794dca4b63SNaphtali Sprei goto ro_cleanup; 228033e3963eSbellard } 228133e3963eSbellard } 228233e3963eSbellard } 228395389c86Sbellard 22841d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 22851d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 2286dabfa6ccSKevin Wolf if (ret < 0) { 2287dabfa6ccSKevin Wolf goto ro_cleanup; 2288dabfa6ccSKevin Wolf } 22891d44952fSChristoph Hellwig bdrv_flush(bs); 22901d44952fSChristoph Hellwig } 229195389c86Sbellard 22923f5075aeSChristoph Hellwig /* 22933f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 22943f5075aeSChristoph Hellwig * stable on disk. 22953f5075aeSChristoph Hellwig */ 2296dabfa6ccSKevin Wolf if (bs->backing_hd) { 22973f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 2298dabfa6ccSKevin Wolf } 22994dca4b63SNaphtali Sprei 2300dabfa6ccSKevin Wolf ret = 0; 23014dca4b63SNaphtali Sprei ro_cleanup: 23027267c094SAnthony Liguori g_free(buf); 23034dca4b63SNaphtali Sprei 23044dca4b63SNaphtali Sprei if (ro) { 23050bce597dSJeff Cody /* ignoring error return here */ 23060bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 23074dca4b63SNaphtali Sprei } 23084dca4b63SNaphtali Sprei 23091d44952fSChristoph Hellwig return ret; 231033e3963eSbellard } 231133e3963eSbellard 2312e8877497SStefan Hajnoczi int bdrv_commit_all(void) 23136ab4b5abSMarkus Armbruster { 23146ab4b5abSMarkus Armbruster BlockDriverState *bs; 23156ab4b5abSMarkus Armbruster 2316dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 2317ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 2318ed78cda3SStefan Hajnoczi 2319ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 2320272d2d8eSJeff Cody if (bs->drv && bs->backing_hd) { 2321e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 2322e8877497SStefan Hajnoczi if (ret < 0) { 2323ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 2324e8877497SStefan Hajnoczi return ret; 23256ab4b5abSMarkus Armbruster } 23266ab4b5abSMarkus Armbruster } 2327ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 2328272d2d8eSJeff Cody } 2329e8877497SStefan Hajnoczi return 0; 2330e8877497SStefan Hajnoczi } 23316ab4b5abSMarkus Armbruster 2332dbffbdcfSStefan Hajnoczi /** 2333dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 2334dbffbdcfSStefan Hajnoczi * 2335dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 2336dbffbdcfSStefan Hajnoczi */ 2337dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 2338dbffbdcfSStefan Hajnoczi { 23392dbafdc0SKevin Wolf if (req->serialising) { 23402dbafdc0SKevin Wolf req->bs->serialising_in_flight--; 23412dbafdc0SKevin Wolf } 23422dbafdc0SKevin Wolf 2343dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 2344f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 2345dbffbdcfSStefan Hajnoczi } 2346dbffbdcfSStefan Hajnoczi 2347dbffbdcfSStefan Hajnoczi /** 2348dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 2349dbffbdcfSStefan Hajnoczi */ 2350dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 2351dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 2352793ed47aSKevin Wolf int64_t offset, 2353793ed47aSKevin Wolf unsigned int bytes, bool is_write) 2354dbffbdcfSStefan Hajnoczi { 2355dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 2356dbffbdcfSStefan Hajnoczi .bs = bs, 2357793ed47aSKevin Wolf .offset = offset, 2358793ed47aSKevin Wolf .bytes = bytes, 2359dbffbdcfSStefan Hajnoczi .is_write = is_write, 23605f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 23612dbafdc0SKevin Wolf .serialising = false, 23627327145fSKevin Wolf .overlap_offset = offset, 23637327145fSKevin Wolf .overlap_bytes = bytes, 2364dbffbdcfSStefan Hajnoczi }; 2365dbffbdcfSStefan Hajnoczi 2366f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 2367f4658285SStefan Hajnoczi 2368dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 2369dbffbdcfSStefan Hajnoczi } 2370dbffbdcfSStefan Hajnoczi 2371e96126ffSKevin Wolf static void mark_request_serialising(BdrvTrackedRequest *req, uint64_t align) 23722dbafdc0SKevin Wolf { 23737327145fSKevin Wolf int64_t overlap_offset = req->offset & ~(align - 1); 2374e96126ffSKevin Wolf unsigned int overlap_bytes = ROUND_UP(req->offset + req->bytes, align) 23757327145fSKevin Wolf - overlap_offset; 23767327145fSKevin Wolf 23772dbafdc0SKevin Wolf if (!req->serialising) { 23782dbafdc0SKevin Wolf req->bs->serialising_in_flight++; 23792dbafdc0SKevin Wolf req->serialising = true; 23802dbafdc0SKevin Wolf } 23817327145fSKevin Wolf 23827327145fSKevin Wolf req->overlap_offset = MIN(req->overlap_offset, overlap_offset); 23837327145fSKevin Wolf req->overlap_bytes = MAX(req->overlap_bytes, overlap_bytes); 23842dbafdc0SKevin Wolf } 23852dbafdc0SKevin Wolf 2386d83947acSStefan Hajnoczi /** 2387d83947acSStefan Hajnoczi * Round a region to cluster boundaries 2388d83947acSStefan Hajnoczi */ 2389343bded4SPaolo Bonzini void bdrv_round_to_clusters(BlockDriverState *bs, 2390d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 2391d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 2392d83947acSStefan Hajnoczi int *cluster_nb_sectors) 2393d83947acSStefan Hajnoczi { 2394d83947acSStefan Hajnoczi BlockDriverInfo bdi; 2395d83947acSStefan Hajnoczi 2396d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 2397d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 2398d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 2399d83947acSStefan Hajnoczi } else { 2400d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 2401d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 2402d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 2403d83947acSStefan Hajnoczi nb_sectors, c); 2404d83947acSStefan Hajnoczi } 2405d83947acSStefan Hajnoczi } 2406d83947acSStefan Hajnoczi 24077327145fSKevin Wolf static int bdrv_get_cluster_size(BlockDriverState *bs) 2408793ed47aSKevin Wolf { 2409793ed47aSKevin Wolf BlockDriverInfo bdi; 24107327145fSKevin Wolf int ret; 2411793ed47aSKevin Wolf 24127327145fSKevin Wolf ret = bdrv_get_info(bs, &bdi); 24137327145fSKevin Wolf if (ret < 0 || bdi.cluster_size == 0) { 24147327145fSKevin Wolf return bs->request_alignment; 2415793ed47aSKevin Wolf } else { 24167327145fSKevin Wolf return bdi.cluster_size; 2417793ed47aSKevin Wolf } 2418793ed47aSKevin Wolf } 2419793ed47aSKevin Wolf 2420f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 2421793ed47aSKevin Wolf int64_t offset, unsigned int bytes) 2422793ed47aSKevin Wolf { 2423d83947acSStefan Hajnoczi /* aaaa bbbb */ 24247327145fSKevin Wolf if (offset >= req->overlap_offset + req->overlap_bytes) { 2425d83947acSStefan Hajnoczi return false; 2426d83947acSStefan Hajnoczi } 2427d83947acSStefan Hajnoczi /* bbbb aaaa */ 24287327145fSKevin Wolf if (req->overlap_offset >= offset + bytes) { 2429d83947acSStefan Hajnoczi return false; 2430d83947acSStefan Hajnoczi } 2431d83947acSStefan Hajnoczi return true; 2432f4658285SStefan Hajnoczi } 2433f4658285SStefan Hajnoczi 243428de2dcdSKevin Wolf static bool coroutine_fn wait_serialising_requests(BdrvTrackedRequest *self) 2435f4658285SStefan Hajnoczi { 24362dbafdc0SKevin Wolf BlockDriverState *bs = self->bs; 2437f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 2438f4658285SStefan Hajnoczi bool retry; 243928de2dcdSKevin Wolf bool waited = false; 2440f4658285SStefan Hajnoczi 24412dbafdc0SKevin Wolf if (!bs->serialising_in_flight) { 244228de2dcdSKevin Wolf return false; 24432dbafdc0SKevin Wolf } 24442dbafdc0SKevin Wolf 2445f4658285SStefan Hajnoczi do { 2446f4658285SStefan Hajnoczi retry = false; 2447f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 24482dbafdc0SKevin Wolf if (req == self || (!req->serialising && !self->serialising)) { 244965afd211SKevin Wolf continue; 245065afd211SKevin Wolf } 24517327145fSKevin Wolf if (tracked_request_overlaps(req, self->overlap_offset, 24527327145fSKevin Wolf self->overlap_bytes)) 24537327145fSKevin Wolf { 24545f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 24555f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 24565f8b6491SStefan Hajnoczi * never happen since it means deadlock. 24575f8b6491SStefan Hajnoczi */ 24585f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 24595f8b6491SStefan Hajnoczi 24606460440fSKevin Wolf /* If the request is already (indirectly) waiting for us, or 24616460440fSKevin Wolf * will wait for us as soon as it wakes up, then just go on 24626460440fSKevin Wolf * (instead of producing a deadlock in the former case). */ 24636460440fSKevin Wolf if (!req->waiting_for) { 24646460440fSKevin Wolf self->waiting_for = req; 2465f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 24666460440fSKevin Wolf self->waiting_for = NULL; 2467f4658285SStefan Hajnoczi retry = true; 246828de2dcdSKevin Wolf waited = true; 2469f4658285SStefan Hajnoczi break; 2470f4658285SStefan Hajnoczi } 2471f4658285SStefan Hajnoczi } 24726460440fSKevin Wolf } 2473f4658285SStefan Hajnoczi } while (retry); 247428de2dcdSKevin Wolf 247528de2dcdSKevin Wolf return waited; 2476f4658285SStefan Hajnoczi } 2477f4658285SStefan Hajnoczi 2478756e6736SKevin Wolf /* 2479756e6736SKevin Wolf * Return values: 2480756e6736SKevin Wolf * 0 - success 2481756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 2482756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 2483756e6736SKevin Wolf * image file header 2484756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 2485756e6736SKevin Wolf */ 2486756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 2487756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 2488756e6736SKevin Wolf { 2489756e6736SKevin Wolf BlockDriver *drv = bs->drv; 2490469ef350SPaolo Bonzini int ret; 2491756e6736SKevin Wolf 24925f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 24935f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 24945f377794SPaolo Bonzini return -EINVAL; 24955f377794SPaolo Bonzini } 24965f377794SPaolo Bonzini 2497756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 2498469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 2499756e6736SKevin Wolf } else { 2500469ef350SPaolo Bonzini ret = -ENOTSUP; 2501756e6736SKevin Wolf } 2502469ef350SPaolo Bonzini 2503469ef350SPaolo Bonzini if (ret == 0) { 2504469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 2505469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 2506469ef350SPaolo Bonzini } 2507469ef350SPaolo Bonzini return ret; 2508756e6736SKevin Wolf } 2509756e6736SKevin Wolf 25106ebdcee2SJeff Cody /* 25116ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 25126ebdcee2SJeff Cody * 25136ebdcee2SJeff Cody * active is the current topmost image. 25146ebdcee2SJeff Cody * 25156ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 25166ebdcee2SJeff Cody * or if active == bs. 25176ebdcee2SJeff Cody */ 25186ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 25196ebdcee2SJeff Cody BlockDriverState *bs) 25206ebdcee2SJeff Cody { 25216ebdcee2SJeff Cody BlockDriverState *overlay = NULL; 25226ebdcee2SJeff Cody BlockDriverState *intermediate; 25236ebdcee2SJeff Cody 25246ebdcee2SJeff Cody assert(active != NULL); 25256ebdcee2SJeff Cody assert(bs != NULL); 25266ebdcee2SJeff Cody 25276ebdcee2SJeff Cody /* if bs is the same as active, then by definition it has no overlay 25286ebdcee2SJeff Cody */ 25296ebdcee2SJeff Cody if (active == bs) { 25306ebdcee2SJeff Cody return NULL; 25316ebdcee2SJeff Cody } 25326ebdcee2SJeff Cody 25336ebdcee2SJeff Cody intermediate = active; 25346ebdcee2SJeff Cody while (intermediate->backing_hd) { 25356ebdcee2SJeff Cody if (intermediate->backing_hd == bs) { 25366ebdcee2SJeff Cody overlay = intermediate; 25376ebdcee2SJeff Cody break; 25386ebdcee2SJeff Cody } 25396ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 25406ebdcee2SJeff Cody } 25416ebdcee2SJeff Cody 25426ebdcee2SJeff Cody return overlay; 25436ebdcee2SJeff Cody } 25446ebdcee2SJeff Cody 25456ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 25466ebdcee2SJeff Cody BlockDriverState *bs; 25476ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 25486ebdcee2SJeff Cody } BlkIntermediateStates; 25496ebdcee2SJeff Cody 25506ebdcee2SJeff Cody 25516ebdcee2SJeff Cody /* 25526ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 25536ebdcee2SJeff Cody * above 'top' to have base as its backing file. 25546ebdcee2SJeff Cody * 25556ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 25566ebdcee2SJeff Cody * information in 'bs' can be properly updated. 25576ebdcee2SJeff Cody * 25586ebdcee2SJeff Cody * E.g., this will convert the following chain: 25596ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 25606ebdcee2SJeff Cody * 25616ebdcee2SJeff Cody * to 25626ebdcee2SJeff Cody * 25636ebdcee2SJeff Cody * bottom <- base <- active 25646ebdcee2SJeff Cody * 25656ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 25666ebdcee2SJeff Cody * 25676ebdcee2SJeff Cody * base <- intermediate <- top <- active 25686ebdcee2SJeff Cody * 25696ebdcee2SJeff Cody * to 25706ebdcee2SJeff Cody * 25716ebdcee2SJeff Cody * base <- active 25726ebdcee2SJeff Cody * 25736ebdcee2SJeff Cody * Error conditions: 25746ebdcee2SJeff Cody * if active == top, that is considered an error 25756ebdcee2SJeff Cody * 25766ebdcee2SJeff Cody */ 25776ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 25786ebdcee2SJeff Cody BlockDriverState *base) 25796ebdcee2SJeff Cody { 25806ebdcee2SJeff Cody BlockDriverState *intermediate; 25816ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 25826ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 25836ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 25846ebdcee2SJeff Cody int ret = -EIO; 25856ebdcee2SJeff Cody 25866ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 25876ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 25886ebdcee2SJeff Cody 25896ebdcee2SJeff Cody if (!top->drv || !base->drv) { 25906ebdcee2SJeff Cody goto exit; 25916ebdcee2SJeff Cody } 25926ebdcee2SJeff Cody 25936ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 25946ebdcee2SJeff Cody 25956ebdcee2SJeff Cody if (new_top_bs == NULL) { 25966ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 25976ebdcee2SJeff Cody goto exit; 25986ebdcee2SJeff Cody } 25996ebdcee2SJeff Cody 26006ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 26016ebdcee2SJeff Cody * to do, no intermediate images */ 26026ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 26036ebdcee2SJeff Cody ret = 0; 26046ebdcee2SJeff Cody goto exit; 26056ebdcee2SJeff Cody } 26066ebdcee2SJeff Cody 26076ebdcee2SJeff Cody intermediate = top; 26086ebdcee2SJeff Cody 26096ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 26106ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 26116ebdcee2SJeff Cody */ 26126ebdcee2SJeff Cody while (intermediate) { 26136ebdcee2SJeff Cody intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); 26146ebdcee2SJeff Cody intermediate_state->bs = intermediate; 26156ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 26166ebdcee2SJeff Cody 26176ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 26186ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 26196ebdcee2SJeff Cody break; 26206ebdcee2SJeff Cody } 26216ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 26226ebdcee2SJeff Cody } 26236ebdcee2SJeff Cody if (base_bs == NULL) { 26246ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 26256ebdcee2SJeff Cody * unravel everything, and exit with error */ 26266ebdcee2SJeff Cody goto exit; 26276ebdcee2SJeff Cody } 26286ebdcee2SJeff Cody 26296ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 26306ebdcee2SJeff Cody ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, 26316ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 26326ebdcee2SJeff Cody if (ret) { 26336ebdcee2SJeff Cody goto exit; 26346ebdcee2SJeff Cody } 2635920beae1SFam Zheng bdrv_set_backing_hd(new_top_bs, base_bs); 26366ebdcee2SJeff Cody 26376ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 26386ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 2639920beae1SFam Zheng bdrv_set_backing_hd(intermediate_state->bs, NULL); 26404f6fd349SFam Zheng bdrv_unref(intermediate_state->bs); 26416ebdcee2SJeff Cody } 26426ebdcee2SJeff Cody ret = 0; 26436ebdcee2SJeff Cody 26446ebdcee2SJeff Cody exit: 26456ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 26466ebdcee2SJeff Cody g_free(intermediate_state); 26476ebdcee2SJeff Cody } 26486ebdcee2SJeff Cody return ret; 26496ebdcee2SJeff Cody } 26506ebdcee2SJeff Cody 26516ebdcee2SJeff Cody 265271d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 265371d0770cSaliguori size_t size) 265471d0770cSaliguori { 265571d0770cSaliguori int64_t len; 265671d0770cSaliguori 26571dd3a447SKevin Wolf if (size > INT_MAX) { 26581dd3a447SKevin Wolf return -EIO; 26591dd3a447SKevin Wolf } 26601dd3a447SKevin Wolf 266171d0770cSaliguori if (!bdrv_is_inserted(bs)) 266271d0770cSaliguori return -ENOMEDIUM; 266371d0770cSaliguori 266471d0770cSaliguori if (bs->growable) 266571d0770cSaliguori return 0; 266671d0770cSaliguori 266771d0770cSaliguori len = bdrv_getlength(bs); 266871d0770cSaliguori 2669fbb7b4e0SKevin Wolf if (offset < 0) 2670fbb7b4e0SKevin Wolf return -EIO; 2671fbb7b4e0SKevin Wolf 2672fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 267371d0770cSaliguori return -EIO; 267471d0770cSaliguori 267571d0770cSaliguori return 0; 267671d0770cSaliguori } 267771d0770cSaliguori 267871d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 267971d0770cSaliguori int nb_sectors) 268071d0770cSaliguori { 268154db38a4SKevin Wolf if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) { 26828f4754edSKevin Wolf return -EIO; 26838f4754edSKevin Wolf } 26848f4754edSKevin Wolf 2685eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 2686eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 268771d0770cSaliguori } 268871d0770cSaliguori 26891c9805a3SStefan Hajnoczi typedef struct RwCo { 26901c9805a3SStefan Hajnoczi BlockDriverState *bs; 2691775aa8b6SKevin Wolf int64_t offset; 26921c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 26931c9805a3SStefan Hajnoczi bool is_write; 26941c9805a3SStefan Hajnoczi int ret; 26954105eaaaSPeter Lieven BdrvRequestFlags flags; 26961c9805a3SStefan Hajnoczi } RwCo; 26971c9805a3SStefan Hajnoczi 26981c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 2699fc01f7e7Sbellard { 27001c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 2701fc01f7e7Sbellard 27021c9805a3SStefan Hajnoczi if (!rwco->is_write) { 2703775aa8b6SKevin Wolf rwco->ret = bdrv_co_do_preadv(rwco->bs, rwco->offset, 2704775aa8b6SKevin Wolf rwco->qiov->size, rwco->qiov, 27054105eaaaSPeter Lieven rwco->flags); 27061c9805a3SStefan Hajnoczi } else { 2707775aa8b6SKevin Wolf rwco->ret = bdrv_co_do_pwritev(rwco->bs, rwco->offset, 2708775aa8b6SKevin Wolf rwco->qiov->size, rwco->qiov, 27094105eaaaSPeter Lieven rwco->flags); 27101c9805a3SStefan Hajnoczi } 27111c9805a3SStefan Hajnoczi } 2712e7a8a783SKevin Wolf 27131c9805a3SStefan Hajnoczi /* 27148d3b1a2dSKevin Wolf * Process a vectored synchronous request using coroutines 27151c9805a3SStefan Hajnoczi */ 2716775aa8b6SKevin Wolf static int bdrv_prwv_co(BlockDriverState *bs, int64_t offset, 27174105eaaaSPeter Lieven QEMUIOVector *qiov, bool is_write, 27184105eaaaSPeter Lieven BdrvRequestFlags flags) 27191c9805a3SStefan Hajnoczi { 27201c9805a3SStefan Hajnoczi Coroutine *co; 27211c9805a3SStefan Hajnoczi RwCo rwco = { 27221c9805a3SStefan Hajnoczi .bs = bs, 2723775aa8b6SKevin Wolf .offset = offset, 27248d3b1a2dSKevin Wolf .qiov = qiov, 27251c9805a3SStefan Hajnoczi .is_write = is_write, 27261c9805a3SStefan Hajnoczi .ret = NOT_DONE, 27274105eaaaSPeter Lieven .flags = flags, 27281c9805a3SStefan Hajnoczi }; 27291c9805a3SStefan Hajnoczi 2730498e386cSZhi Yong Wu /** 2731498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 2732498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 2733498e386cSZhi Yong Wu * if it has been enabled. 2734498e386cSZhi Yong Wu */ 2735498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 2736498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 2737498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 2738498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 2739498e386cSZhi Yong Wu } 2740498e386cSZhi Yong Wu 27411c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 27421c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 27431c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 27441c9805a3SStefan Hajnoczi } else { 27452572b37aSStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 27462572b37aSStefan Hajnoczi 27471c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 27481c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 27491c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 27502572b37aSStefan Hajnoczi aio_poll(aio_context, true); 27511c9805a3SStefan Hajnoczi } 27521c9805a3SStefan Hajnoczi } 27531c9805a3SStefan Hajnoczi return rwco.ret; 2754e7a8a783SKevin Wolf } 2755e7a8a783SKevin Wolf 27568d3b1a2dSKevin Wolf /* 27578d3b1a2dSKevin Wolf * Process a synchronous request using coroutines 27588d3b1a2dSKevin Wolf */ 27598d3b1a2dSKevin Wolf static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 27604105eaaaSPeter Lieven int nb_sectors, bool is_write, BdrvRequestFlags flags) 27618d3b1a2dSKevin Wolf { 27628d3b1a2dSKevin Wolf QEMUIOVector qiov; 27638d3b1a2dSKevin Wolf struct iovec iov = { 27648d3b1a2dSKevin Wolf .iov_base = (void *)buf, 27658d3b1a2dSKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 27668d3b1a2dSKevin Wolf }; 27678d3b1a2dSKevin Wolf 2768da15ee51SKevin Wolf if (nb_sectors < 0 || nb_sectors > INT_MAX / BDRV_SECTOR_SIZE) { 2769da15ee51SKevin Wolf return -EINVAL; 2770da15ee51SKevin Wolf } 2771da15ee51SKevin Wolf 27728d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 2773775aa8b6SKevin Wolf return bdrv_prwv_co(bs, sector_num << BDRV_SECTOR_BITS, 2774775aa8b6SKevin Wolf &qiov, is_write, flags); 27758d3b1a2dSKevin Wolf } 27768d3b1a2dSKevin Wolf 27771c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 27781c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 27791c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 27801c9805a3SStefan Hajnoczi { 27814105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0); 278283f64091Sbellard } 2783fc01f7e7Sbellard 278407d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 278507d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 278607d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 278707d27a44SMarkus Armbruster { 278807d27a44SMarkus Armbruster bool enabled; 278907d27a44SMarkus Armbruster int ret; 279007d27a44SMarkus Armbruster 279107d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 279207d27a44SMarkus Armbruster bs->io_limits_enabled = false; 27934e7395e8SPeter Lieven ret = bdrv_read(bs, sector_num, buf, nb_sectors); 279407d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 279507d27a44SMarkus Armbruster return ret; 279607d27a44SMarkus Armbruster } 279707d27a44SMarkus Armbruster 279819cb3738Sbellard /* Return < 0 if error. Important errors are: 279919cb3738Sbellard -EIO generic I/O error (may happen for all errors) 280019cb3738Sbellard -ENOMEDIUM No media inserted. 280119cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 280219cb3738Sbellard -EACCES Trying to write a read-only device 280319cb3738Sbellard */ 2804fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 2805fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 2806fc01f7e7Sbellard { 28074105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0); 280883f64091Sbellard } 280983f64091Sbellard 2810aa7bfbffSPeter Lieven int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, 2811aa7bfbffSPeter Lieven int nb_sectors, BdrvRequestFlags flags) 28124105eaaaSPeter Lieven { 28134105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true, 2814aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 28158d3b1a2dSKevin Wolf } 28168d3b1a2dSKevin Wolf 2817d75cbb5eSPeter Lieven /* 2818d75cbb5eSPeter Lieven * Completely zero out a block device with the help of bdrv_write_zeroes. 2819d75cbb5eSPeter Lieven * The operation is sped up by checking the block status and only writing 2820d75cbb5eSPeter Lieven * zeroes to the device if they currently do not return zeroes. Optional 2821d75cbb5eSPeter Lieven * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP). 2822d75cbb5eSPeter Lieven * 2823d75cbb5eSPeter Lieven * Returns < 0 on error, 0 on success. For error codes see bdrv_write(). 2824d75cbb5eSPeter Lieven */ 2825d75cbb5eSPeter Lieven int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) 2826d75cbb5eSPeter Lieven { 28279ce10c0bSKevin Wolf int64_t target_size; 2828d75cbb5eSPeter Lieven int64_t ret, nb_sectors, sector_num = 0; 2829d75cbb5eSPeter Lieven int n; 2830d75cbb5eSPeter Lieven 28319ce10c0bSKevin Wolf target_size = bdrv_getlength(bs); 28329ce10c0bSKevin Wolf if (target_size < 0) { 28339ce10c0bSKevin Wolf return target_size; 28349ce10c0bSKevin Wolf } 28359ce10c0bSKevin Wolf target_size /= BDRV_SECTOR_SIZE; 28369ce10c0bSKevin Wolf 2837d75cbb5eSPeter Lieven for (;;) { 2838d75cbb5eSPeter Lieven nb_sectors = target_size - sector_num; 2839d75cbb5eSPeter Lieven if (nb_sectors <= 0) { 2840d75cbb5eSPeter Lieven return 0; 2841d75cbb5eSPeter Lieven } 2842d75cbb5eSPeter Lieven if (nb_sectors > INT_MAX) { 2843d75cbb5eSPeter Lieven nb_sectors = INT_MAX; 2844d75cbb5eSPeter Lieven } 2845d75cbb5eSPeter Lieven ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n); 28463d94ce60SPeter Lieven if (ret < 0) { 28473d94ce60SPeter Lieven error_report("error getting block status at sector %" PRId64 ": %s", 28483d94ce60SPeter Lieven sector_num, strerror(-ret)); 28493d94ce60SPeter Lieven return ret; 28503d94ce60SPeter Lieven } 2851d75cbb5eSPeter Lieven if (ret & BDRV_BLOCK_ZERO) { 2852d75cbb5eSPeter Lieven sector_num += n; 2853d75cbb5eSPeter Lieven continue; 2854d75cbb5eSPeter Lieven } 2855d75cbb5eSPeter Lieven ret = bdrv_write_zeroes(bs, sector_num, n, flags); 2856d75cbb5eSPeter Lieven if (ret < 0) { 2857d75cbb5eSPeter Lieven error_report("error writing zeroes at sector %" PRId64 ": %s", 2858d75cbb5eSPeter Lieven sector_num, strerror(-ret)); 2859d75cbb5eSPeter Lieven return ret; 2860d75cbb5eSPeter Lieven } 2861d75cbb5eSPeter Lieven sector_num += n; 2862d75cbb5eSPeter Lieven } 2863d75cbb5eSPeter Lieven } 2864d75cbb5eSPeter Lieven 2865a3ef6571SKevin Wolf int bdrv_pread(BlockDriverState *bs, int64_t offset, void *buf, int bytes) 286683f64091Sbellard { 2867a3ef6571SKevin Wolf QEMUIOVector qiov; 2868a3ef6571SKevin Wolf struct iovec iov = { 2869a3ef6571SKevin Wolf .iov_base = (void *)buf, 2870a3ef6571SKevin Wolf .iov_len = bytes, 2871a3ef6571SKevin Wolf }; 28729a8c4cceSKevin Wolf int ret; 287383f64091Sbellard 2874a3ef6571SKevin Wolf if (bytes < 0) { 2875a3ef6571SKevin Wolf return -EINVAL; 287683f64091Sbellard } 287783f64091Sbellard 2878a3ef6571SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 2879a3ef6571SKevin Wolf ret = bdrv_prwv_co(bs, offset, &qiov, false, 0); 2880a3ef6571SKevin Wolf if (ret < 0) { 28819a8c4cceSKevin Wolf return ret; 288283f64091Sbellard } 288383f64091Sbellard 2884a3ef6571SKevin Wolf return bytes; 288583f64091Sbellard } 288683f64091Sbellard 28878d3b1a2dSKevin Wolf int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov) 288883f64091Sbellard { 28899a8c4cceSKevin Wolf int ret; 289083f64091Sbellard 28918407d5d7SKevin Wolf ret = bdrv_prwv_co(bs, offset, qiov, true, 0); 28928d3b1a2dSKevin Wolf if (ret < 0) { 28939a8c4cceSKevin Wolf return ret; 28948d3b1a2dSKevin Wolf } 28958d3b1a2dSKevin Wolf 28968d3b1a2dSKevin Wolf return qiov->size; 28978d3b1a2dSKevin Wolf } 28988d3b1a2dSKevin Wolf 28998d3b1a2dSKevin Wolf int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 29008407d5d7SKevin Wolf const void *buf, int bytes) 29018d3b1a2dSKevin Wolf { 29028d3b1a2dSKevin Wolf QEMUIOVector qiov; 29038d3b1a2dSKevin Wolf struct iovec iov = { 29048d3b1a2dSKevin Wolf .iov_base = (void *) buf, 29058407d5d7SKevin Wolf .iov_len = bytes, 29068d3b1a2dSKevin Wolf }; 29078d3b1a2dSKevin Wolf 29088407d5d7SKevin Wolf if (bytes < 0) { 29098407d5d7SKevin Wolf return -EINVAL; 29108407d5d7SKevin Wolf } 29118407d5d7SKevin Wolf 29128d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 29138d3b1a2dSKevin Wolf return bdrv_pwritev(bs, offset, &qiov); 291483f64091Sbellard } 291583f64091Sbellard 2916f08145feSKevin Wolf /* 2917f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 2918f08145feSKevin Wolf * request (acts as a barrier) 2919f08145feSKevin Wolf * 2920f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 2921f08145feSKevin Wolf */ 2922f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 2923f08145feSKevin Wolf const void *buf, int count) 2924f08145feSKevin Wolf { 2925f08145feSKevin Wolf int ret; 2926f08145feSKevin Wolf 2927f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 2928f08145feSKevin Wolf if (ret < 0) { 2929f08145feSKevin Wolf return ret; 2930f08145feSKevin Wolf } 2931f08145feSKevin Wolf 2932f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 2933f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 2934f08145feSKevin Wolf bdrv_flush(bs); 2935f08145feSKevin Wolf } 2936f08145feSKevin Wolf 2937f08145feSKevin Wolf return 0; 2938f08145feSKevin Wolf } 2939f08145feSKevin Wolf 2940470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 2941ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2942ab185921SStefan Hajnoczi { 2943ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 2944ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 2945ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 2946ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 2947ab185921SStefan Hajnoczi */ 2948ab185921SStefan Hajnoczi void *bounce_buffer; 2949ab185921SStefan Hajnoczi 295079c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 2951ab185921SStefan Hajnoczi struct iovec iov; 2952ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 2953ab185921SStefan Hajnoczi int64_t cluster_sector_num; 2954ab185921SStefan Hajnoczi int cluster_nb_sectors; 2955ab185921SStefan Hajnoczi size_t skip_bytes; 2956ab185921SStefan Hajnoczi int ret; 2957ab185921SStefan Hajnoczi 2958ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 2959ab185921SStefan Hajnoczi * allocating cluster in the image file. 2960ab185921SStefan Hajnoczi */ 2961343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2962ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2963ab185921SStefan Hajnoczi 2964470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 2965ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 2966ab185921SStefan Hajnoczi 2967ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 2968ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 2969ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 2970ab185921SStefan Hajnoczi 297179c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 2972ab185921SStefan Hajnoczi &bounce_qiov); 2973ab185921SStefan Hajnoczi if (ret < 0) { 2974ab185921SStefan Hajnoczi goto err; 2975ab185921SStefan Hajnoczi } 2976ab185921SStefan Hajnoczi 297779c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 297879c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 2979621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 2980aa7bfbffSPeter Lieven cluster_nb_sectors, 0); 298179c053bdSStefan Hajnoczi } else { 2982f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 2983f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 2984f05fa4adSPaolo Bonzini */ 298579c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 2986ab185921SStefan Hajnoczi &bounce_qiov); 298779c053bdSStefan Hajnoczi } 298879c053bdSStefan Hajnoczi 2989ab185921SStefan Hajnoczi if (ret < 0) { 2990ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 2991ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 2992ab185921SStefan Hajnoczi * Simply report it in all cases. 2993ab185921SStefan Hajnoczi */ 2994ab185921SStefan Hajnoczi goto err; 2995ab185921SStefan Hajnoczi } 2996ab185921SStefan Hajnoczi 2997ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 299803396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 2999ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 3000ab185921SStefan Hajnoczi 3001ab185921SStefan Hajnoczi err: 3002ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 3003ab185921SStefan Hajnoczi return ret; 3004ab185921SStefan Hajnoczi } 3005ab185921SStefan Hajnoczi 3006c5fbe571SStefan Hajnoczi /* 3007d0c7f642SKevin Wolf * Forwards an already correctly aligned request to the BlockDriver. This 3008d0c7f642SKevin Wolf * handles copy on read and zeroing after EOF; any other features must be 3009d0c7f642SKevin Wolf * implemented by the caller. 3010c5fbe571SStefan Hajnoczi */ 3011d0c7f642SKevin Wolf static int coroutine_fn bdrv_aligned_preadv(BlockDriverState *bs, 301265afd211SKevin Wolf BdrvTrackedRequest *req, int64_t offset, unsigned int bytes, 3013ec746e10SKevin Wolf int64_t align, QEMUIOVector *qiov, int flags) 3014da1fa91dSKevin Wolf { 3015da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 3016dbffbdcfSStefan Hajnoczi int ret; 3017da1fa91dSKevin Wolf 3018d0c7f642SKevin Wolf int64_t sector_num = offset >> BDRV_SECTOR_BITS; 3019d0c7f642SKevin Wolf unsigned int nb_sectors = bytes >> BDRV_SECTOR_BITS; 3020da1fa91dSKevin Wolf 3021d0c7f642SKevin Wolf assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0); 3022d0c7f642SKevin Wolf assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0); 3023d0c7f642SKevin Wolf 3024d0c7f642SKevin Wolf /* Handle Copy on Read and associated serialisation */ 3025470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 30267327145fSKevin Wolf /* If we touch the same cluster it counts as an overlap. This 30277327145fSKevin Wolf * guarantees that allocating writes will be serialized and not race 30287327145fSKevin Wolf * with each other for the same cluster. For example, in copy-on-read 30297327145fSKevin Wolf * it ensures that the CoR read and write operations are atomic and 30307327145fSKevin Wolf * guest writes cannot interleave between them. */ 30317327145fSKevin Wolf mark_request_serialising(req, bdrv_get_cluster_size(bs)); 3032470c0504SStefan Hajnoczi } 3033470c0504SStefan Hajnoczi 30342dbafdc0SKevin Wolf wait_serialising_requests(req); 3035f4658285SStefan Hajnoczi 3036470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 3037ab185921SStefan Hajnoczi int pnum; 3038ab185921SStefan Hajnoczi 3039bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum); 3040ab185921SStefan Hajnoczi if (ret < 0) { 3041ab185921SStefan Hajnoczi goto out; 3042ab185921SStefan Hajnoczi } 3043ab185921SStefan Hajnoczi 3044ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 3045470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 3046ab185921SStefan Hajnoczi goto out; 3047ab185921SStefan Hajnoczi } 3048ab185921SStefan Hajnoczi } 3049ab185921SStefan Hajnoczi 3050d0c7f642SKevin Wolf /* Forward the request to the BlockDriver */ 3051893a8f62SMORITA Kazutaka if (!(bs->zero_beyond_eof && bs->growable)) { 3052dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 3053893a8f62SMORITA Kazutaka } else { 3054893a8f62SMORITA Kazutaka /* Read zeros after EOF of growable BDSes */ 3055893a8f62SMORITA Kazutaka int64_t len, total_sectors, max_nb_sectors; 3056893a8f62SMORITA Kazutaka 3057893a8f62SMORITA Kazutaka len = bdrv_getlength(bs); 3058893a8f62SMORITA Kazutaka if (len < 0) { 3059893a8f62SMORITA Kazutaka ret = len; 3060893a8f62SMORITA Kazutaka goto out; 3061893a8f62SMORITA Kazutaka } 3062893a8f62SMORITA Kazutaka 3063d055a1feSFam Zheng total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE); 30645f5bcd80SKevin Wolf max_nb_sectors = ROUND_UP(MAX(0, total_sectors - sector_num), 30655f5bcd80SKevin Wolf align >> BDRV_SECTOR_BITS); 3066893a8f62SMORITA Kazutaka if (max_nb_sectors > 0) { 3067893a8f62SMORITA Kazutaka ret = drv->bdrv_co_readv(bs, sector_num, 3068893a8f62SMORITA Kazutaka MIN(nb_sectors, max_nb_sectors), qiov); 3069893a8f62SMORITA Kazutaka } else { 3070893a8f62SMORITA Kazutaka ret = 0; 3071893a8f62SMORITA Kazutaka } 3072893a8f62SMORITA Kazutaka 3073893a8f62SMORITA Kazutaka /* Reading beyond end of file is supposed to produce zeroes */ 3074893a8f62SMORITA Kazutaka if (ret == 0 && total_sectors < sector_num + nb_sectors) { 3075893a8f62SMORITA Kazutaka uint64_t offset = MAX(0, total_sectors - sector_num); 3076893a8f62SMORITA Kazutaka uint64_t bytes = (sector_num + nb_sectors - offset) * 3077893a8f62SMORITA Kazutaka BDRV_SECTOR_SIZE; 3078893a8f62SMORITA Kazutaka qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes); 3079893a8f62SMORITA Kazutaka } 3080893a8f62SMORITA Kazutaka } 3081ab185921SStefan Hajnoczi 3082ab185921SStefan Hajnoczi out: 3083dbffbdcfSStefan Hajnoczi return ret; 3084da1fa91dSKevin Wolf } 3085da1fa91dSKevin Wolf 3086d0c7f642SKevin Wolf /* 3087d0c7f642SKevin Wolf * Handle a read request in coroutine context 3088d0c7f642SKevin Wolf */ 30891b0288aeSKevin Wolf static int coroutine_fn bdrv_co_do_preadv(BlockDriverState *bs, 30901b0288aeSKevin Wolf int64_t offset, unsigned int bytes, QEMUIOVector *qiov, 3091d0c7f642SKevin Wolf BdrvRequestFlags flags) 3092d0c7f642SKevin Wolf { 3093d0c7f642SKevin Wolf BlockDriver *drv = bs->drv; 309465afd211SKevin Wolf BdrvTrackedRequest req; 309565afd211SKevin Wolf 30961b0288aeSKevin Wolf /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */ 30971b0288aeSKevin Wolf uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment); 30981b0288aeSKevin Wolf uint8_t *head_buf = NULL; 30991b0288aeSKevin Wolf uint8_t *tail_buf = NULL; 31001b0288aeSKevin Wolf QEMUIOVector local_qiov; 31011b0288aeSKevin Wolf bool use_local_qiov = false; 3102d0c7f642SKevin Wolf int ret; 3103d0c7f642SKevin Wolf 3104d0c7f642SKevin Wolf if (!drv) { 3105d0c7f642SKevin Wolf return -ENOMEDIUM; 3106d0c7f642SKevin Wolf } 31071b0288aeSKevin Wolf if (bdrv_check_byte_request(bs, offset, bytes)) { 3108d0c7f642SKevin Wolf return -EIO; 3109d0c7f642SKevin Wolf } 3110d0c7f642SKevin Wolf 3111d0c7f642SKevin Wolf if (bs->copy_on_read) { 3112d0c7f642SKevin Wolf flags |= BDRV_REQ_COPY_ON_READ; 3113d0c7f642SKevin Wolf } 3114d0c7f642SKevin Wolf 3115d0c7f642SKevin Wolf /* throttling disk I/O */ 3116d0c7f642SKevin Wolf if (bs->io_limits_enabled) { 3117d5103588SKevin Wolf bdrv_io_limits_intercept(bs, bytes, false); 3118d0c7f642SKevin Wolf } 3119d0c7f642SKevin Wolf 31201b0288aeSKevin Wolf /* Align read if necessary by padding qiov */ 31211b0288aeSKevin Wolf if (offset & (align - 1)) { 31221b0288aeSKevin Wolf head_buf = qemu_blockalign(bs, align); 31231b0288aeSKevin Wolf qemu_iovec_init(&local_qiov, qiov->niov + 2); 31241b0288aeSKevin Wolf qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1)); 31251b0288aeSKevin Wolf qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); 31261b0288aeSKevin Wolf use_local_qiov = true; 31271b0288aeSKevin Wolf 31281b0288aeSKevin Wolf bytes += offset & (align - 1); 31291b0288aeSKevin Wolf offset = offset & ~(align - 1); 31301b0288aeSKevin Wolf } 31311b0288aeSKevin Wolf 31321b0288aeSKevin Wolf if ((offset + bytes) & (align - 1)) { 31331b0288aeSKevin Wolf if (!use_local_qiov) { 31341b0288aeSKevin Wolf qemu_iovec_init(&local_qiov, qiov->niov + 1); 31351b0288aeSKevin Wolf qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); 31361b0288aeSKevin Wolf use_local_qiov = true; 31371b0288aeSKevin Wolf } 31381b0288aeSKevin Wolf tail_buf = qemu_blockalign(bs, align); 31391b0288aeSKevin Wolf qemu_iovec_add(&local_qiov, tail_buf, 31401b0288aeSKevin Wolf align - ((offset + bytes) & (align - 1))); 31411b0288aeSKevin Wolf 31421b0288aeSKevin Wolf bytes = ROUND_UP(bytes, align); 31431b0288aeSKevin Wolf } 31441b0288aeSKevin Wolf 314565afd211SKevin Wolf tracked_request_begin(&req, bs, offset, bytes, false); 3146ec746e10SKevin Wolf ret = bdrv_aligned_preadv(bs, &req, offset, bytes, align, 31471b0288aeSKevin Wolf use_local_qiov ? &local_qiov : qiov, 31481b0288aeSKevin Wolf flags); 314965afd211SKevin Wolf tracked_request_end(&req); 31501b0288aeSKevin Wolf 31511b0288aeSKevin Wolf if (use_local_qiov) { 31521b0288aeSKevin Wolf qemu_iovec_destroy(&local_qiov); 31531b0288aeSKevin Wolf qemu_vfree(head_buf); 31541b0288aeSKevin Wolf qemu_vfree(tail_buf); 31551b0288aeSKevin Wolf } 31561b0288aeSKevin Wolf 3157d0c7f642SKevin Wolf return ret; 3158d0c7f642SKevin Wolf } 3159d0c7f642SKevin Wolf 31601b0288aeSKevin Wolf static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 31611b0288aeSKevin Wolf int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 31621b0288aeSKevin Wolf BdrvRequestFlags flags) 31631b0288aeSKevin Wolf { 31641b0288aeSKevin Wolf if (nb_sectors < 0 || nb_sectors > (UINT_MAX >> BDRV_SECTOR_BITS)) { 31651b0288aeSKevin Wolf return -EINVAL; 31661b0288aeSKevin Wolf } 31671b0288aeSKevin Wolf 31681b0288aeSKevin Wolf return bdrv_co_do_preadv(bs, sector_num << BDRV_SECTOR_BITS, 31691b0288aeSKevin Wolf nb_sectors << BDRV_SECTOR_BITS, qiov, flags); 31701b0288aeSKevin Wolf } 31711b0288aeSKevin Wolf 3172c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 3173da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 3174da1fa91dSKevin Wolf { 3175c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 3176da1fa91dSKevin Wolf 3177470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 3178470c0504SStefan Hajnoczi } 3179470c0504SStefan Hajnoczi 3180470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 3181470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 3182470c0504SStefan Hajnoczi { 3183470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 3184470c0504SStefan Hajnoczi 3185470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 3186470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 3187c5fbe571SStefan Hajnoczi } 3188c5fbe571SStefan Hajnoczi 3189c31cb707SPeter Lieven /* if no limit is specified in the BlockLimits use a default 3190c31cb707SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 3191c31cb707SPeter Lieven */ 3192c31cb707SPeter Lieven #define MAX_WRITE_ZEROES_DEFAULT 32768 3193c31cb707SPeter Lieven 3194f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 3195aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) 3196f08f2ddaSStefan Hajnoczi { 3197f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 3198f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 3199c31cb707SPeter Lieven struct iovec iov = {0}; 3200c31cb707SPeter Lieven int ret = 0; 3201f08f2ddaSStefan Hajnoczi 3202c31cb707SPeter Lieven int max_write_zeroes = bs->bl.max_write_zeroes ? 3203c31cb707SPeter Lieven bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT; 3204621f0589SKevin Wolf 3205c31cb707SPeter Lieven while (nb_sectors > 0 && !ret) { 3206c31cb707SPeter Lieven int num = nb_sectors; 3207c31cb707SPeter Lieven 3208b8d71c09SPaolo Bonzini /* Align request. Block drivers can expect the "bulk" of the request 3209b8d71c09SPaolo Bonzini * to be aligned. 3210b8d71c09SPaolo Bonzini */ 3211b8d71c09SPaolo Bonzini if (bs->bl.write_zeroes_alignment 3212b8d71c09SPaolo Bonzini && num > bs->bl.write_zeroes_alignment) { 3213b8d71c09SPaolo Bonzini if (sector_num % bs->bl.write_zeroes_alignment != 0) { 3214b8d71c09SPaolo Bonzini /* Make a small request up to the first aligned sector. */ 3215c31cb707SPeter Lieven num = bs->bl.write_zeroes_alignment; 3216c31cb707SPeter Lieven num -= sector_num % bs->bl.write_zeroes_alignment; 3217b8d71c09SPaolo Bonzini } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) { 3218b8d71c09SPaolo Bonzini /* Shorten the request to the last aligned sector. num cannot 3219b8d71c09SPaolo Bonzini * underflow because num > bs->bl.write_zeroes_alignment. 3220b8d71c09SPaolo Bonzini */ 3221b8d71c09SPaolo Bonzini num -= (sector_num + num) % bs->bl.write_zeroes_alignment; 3222b8d71c09SPaolo Bonzini } 3223c31cb707SPeter Lieven } 3224c31cb707SPeter Lieven 3225c31cb707SPeter Lieven /* limit request size */ 3226c31cb707SPeter Lieven if (num > max_write_zeroes) { 3227c31cb707SPeter Lieven num = max_write_zeroes; 3228c31cb707SPeter Lieven } 3229c31cb707SPeter Lieven 3230c31cb707SPeter Lieven ret = -ENOTSUP; 3231f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 3232f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 3233c31cb707SPeter Lieven ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags); 3234f08f2ddaSStefan Hajnoczi } 3235f08f2ddaSStefan Hajnoczi 3236c31cb707SPeter Lieven if (ret == -ENOTSUP) { 3237f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 3238c31cb707SPeter Lieven iov.iov_len = num * BDRV_SECTOR_SIZE; 3239c31cb707SPeter Lieven if (iov.iov_base == NULL) { 3240b8d71c09SPaolo Bonzini iov.iov_base = qemu_blockalign(bs, num * BDRV_SECTOR_SIZE); 3241b8d71c09SPaolo Bonzini memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE); 3242c31cb707SPeter Lieven } 3243f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 3244f08f2ddaSStefan Hajnoczi 3245c31cb707SPeter Lieven ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov); 3246b8d71c09SPaolo Bonzini 3247b8d71c09SPaolo Bonzini /* Keep bounce buffer around if it is big enough for all 3248b8d71c09SPaolo Bonzini * all future requests. 3249b8d71c09SPaolo Bonzini */ 3250b8d71c09SPaolo Bonzini if (num < max_write_zeroes) { 3251b8d71c09SPaolo Bonzini qemu_vfree(iov.iov_base); 3252b8d71c09SPaolo Bonzini iov.iov_base = NULL; 3253b8d71c09SPaolo Bonzini } 3254c31cb707SPeter Lieven } 3255c31cb707SPeter Lieven 3256c31cb707SPeter Lieven sector_num += num; 3257c31cb707SPeter Lieven nb_sectors -= num; 3258c31cb707SPeter Lieven } 3259f08f2ddaSStefan Hajnoczi 3260f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 3261f08f2ddaSStefan Hajnoczi return ret; 3262f08f2ddaSStefan Hajnoczi } 3263f08f2ddaSStefan Hajnoczi 3264c5fbe571SStefan Hajnoczi /* 3265b404f720SKevin Wolf * Forwards an already correctly aligned write request to the BlockDriver. 3266c5fbe571SStefan Hajnoczi */ 3267b404f720SKevin Wolf static int coroutine_fn bdrv_aligned_pwritev(BlockDriverState *bs, 326865afd211SKevin Wolf BdrvTrackedRequest *req, int64_t offset, unsigned int bytes, 326965afd211SKevin Wolf QEMUIOVector *qiov, int flags) 3270c5fbe571SStefan Hajnoczi { 3271c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 327228de2dcdSKevin Wolf bool waited; 32736b7cb247SStefan Hajnoczi int ret; 3274da1fa91dSKevin Wolf 3275b404f720SKevin Wolf int64_t sector_num = offset >> BDRV_SECTOR_BITS; 3276b404f720SKevin Wolf unsigned int nb_sectors = bytes >> BDRV_SECTOR_BITS; 3277da1fa91dSKevin Wolf 3278b404f720SKevin Wolf assert((offset & (BDRV_SECTOR_SIZE - 1)) == 0); 3279b404f720SKevin Wolf assert((bytes & (BDRV_SECTOR_SIZE - 1)) == 0); 3280cc0681c4SBenoît Canet 328128de2dcdSKevin Wolf waited = wait_serialising_requests(req); 328228de2dcdSKevin Wolf assert(!waited || !req->serialising); 3283af91f9a7SKevin Wolf assert(req->overlap_offset <= offset); 3284af91f9a7SKevin Wolf assert(offset + bytes <= req->overlap_offset + req->overlap_bytes); 3285244eadefSKevin Wolf 328665afd211SKevin Wolf ret = notifier_with_return_list_notify(&bs->before_write_notifiers, req); 3287d616b224SStefan Hajnoczi 3288465bee1dSPeter Lieven if (!ret && bs->detect_zeroes != BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF && 3289465bee1dSPeter Lieven !(flags & BDRV_REQ_ZERO_WRITE) && drv->bdrv_co_write_zeroes && 3290465bee1dSPeter Lieven qemu_iovec_is_zero(qiov)) { 3291465bee1dSPeter Lieven flags |= BDRV_REQ_ZERO_WRITE; 3292465bee1dSPeter Lieven if (bs->detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP) { 3293465bee1dSPeter Lieven flags |= BDRV_REQ_MAY_UNMAP; 3294465bee1dSPeter Lieven } 3295465bee1dSPeter Lieven } 3296465bee1dSPeter Lieven 3297d616b224SStefan Hajnoczi if (ret < 0) { 3298d616b224SStefan Hajnoczi /* Do nothing, write notifier decided to fail this request */ 3299d616b224SStefan Hajnoczi } else if (flags & BDRV_REQ_ZERO_WRITE) { 33009e1cb96dSKevin Wolf BLKDBG_EVENT(bs, BLKDBG_PWRITEV_ZERO); 3301aa7bfbffSPeter Lieven ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags); 3302f08f2ddaSStefan Hajnoczi } else { 33039e1cb96dSKevin Wolf BLKDBG_EVENT(bs, BLKDBG_PWRITEV); 33046b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 3305f08f2ddaSStefan Hajnoczi } 33069e1cb96dSKevin Wolf BLKDBG_EVENT(bs, BLKDBG_PWRITEV_DONE); 33076b7cb247SStefan Hajnoczi 3308f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 3309f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 3310f05fa4adSPaolo Bonzini } 3311f05fa4adSPaolo Bonzini 33121755da16SPaolo Bonzini bdrv_set_dirty(bs, sector_num, nb_sectors); 3313da1fa91dSKevin Wolf 3314da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 3315da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 3316da1fa91dSKevin Wolf } 3317df2a6f29SPaolo Bonzini if (bs->growable && ret >= 0) { 3318df2a6f29SPaolo Bonzini bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors); 3319df2a6f29SPaolo Bonzini } 3320da1fa91dSKevin Wolf 33216b7cb247SStefan Hajnoczi return ret; 3322da1fa91dSKevin Wolf } 3323da1fa91dSKevin Wolf 3324b404f720SKevin Wolf /* 3325b404f720SKevin Wolf * Handle a write request in coroutine context 3326b404f720SKevin Wolf */ 33276601553eSKevin Wolf static int coroutine_fn bdrv_co_do_pwritev(BlockDriverState *bs, 33286601553eSKevin Wolf int64_t offset, unsigned int bytes, QEMUIOVector *qiov, 3329b404f720SKevin Wolf BdrvRequestFlags flags) 3330b404f720SKevin Wolf { 333165afd211SKevin Wolf BdrvTrackedRequest req; 33323b8242e0SKevin Wolf /* TODO Lift BDRV_SECTOR_SIZE restriction in BlockDriver interface */ 33333b8242e0SKevin Wolf uint64_t align = MAX(BDRV_SECTOR_SIZE, bs->request_alignment); 33343b8242e0SKevin Wolf uint8_t *head_buf = NULL; 33353b8242e0SKevin Wolf uint8_t *tail_buf = NULL; 33363b8242e0SKevin Wolf QEMUIOVector local_qiov; 33373b8242e0SKevin Wolf bool use_local_qiov = false; 3338b404f720SKevin Wolf int ret; 3339b404f720SKevin Wolf 3340b404f720SKevin Wolf if (!bs->drv) { 3341b404f720SKevin Wolf return -ENOMEDIUM; 3342b404f720SKevin Wolf } 3343b404f720SKevin Wolf if (bs->read_only) { 3344b404f720SKevin Wolf return -EACCES; 3345b404f720SKevin Wolf } 33466601553eSKevin Wolf if (bdrv_check_byte_request(bs, offset, bytes)) { 3347b404f720SKevin Wolf return -EIO; 3348b404f720SKevin Wolf } 3349b404f720SKevin Wolf 3350b404f720SKevin Wolf /* throttling disk I/O */ 3351b404f720SKevin Wolf if (bs->io_limits_enabled) { 3352d5103588SKevin Wolf bdrv_io_limits_intercept(bs, bytes, true); 3353b404f720SKevin Wolf } 3354b404f720SKevin Wolf 33553b8242e0SKevin Wolf /* 33563b8242e0SKevin Wolf * Align write if necessary by performing a read-modify-write cycle. 33573b8242e0SKevin Wolf * Pad qiov with the read parts and be sure to have a tracked request not 33583b8242e0SKevin Wolf * only for bdrv_aligned_pwritev, but also for the reads of the RMW cycle. 33593b8242e0SKevin Wolf */ 336065afd211SKevin Wolf tracked_request_begin(&req, bs, offset, bytes, true); 33613b8242e0SKevin Wolf 33623b8242e0SKevin Wolf if (offset & (align - 1)) { 33633b8242e0SKevin Wolf QEMUIOVector head_qiov; 33643b8242e0SKevin Wolf struct iovec head_iov; 33653b8242e0SKevin Wolf 33663b8242e0SKevin Wolf mark_request_serialising(&req, align); 33673b8242e0SKevin Wolf wait_serialising_requests(&req); 33683b8242e0SKevin Wolf 33693b8242e0SKevin Wolf head_buf = qemu_blockalign(bs, align); 33703b8242e0SKevin Wolf head_iov = (struct iovec) { 33713b8242e0SKevin Wolf .iov_base = head_buf, 33723b8242e0SKevin Wolf .iov_len = align, 33733b8242e0SKevin Wolf }; 33743b8242e0SKevin Wolf qemu_iovec_init_external(&head_qiov, &head_iov, 1); 33753b8242e0SKevin Wolf 33769e1cb96dSKevin Wolf BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_HEAD); 33773b8242e0SKevin Wolf ret = bdrv_aligned_preadv(bs, &req, offset & ~(align - 1), align, 33783b8242e0SKevin Wolf align, &head_qiov, 0); 33793b8242e0SKevin Wolf if (ret < 0) { 33803b8242e0SKevin Wolf goto fail; 33813b8242e0SKevin Wolf } 33829e1cb96dSKevin Wolf BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_HEAD); 33833b8242e0SKevin Wolf 33843b8242e0SKevin Wolf qemu_iovec_init(&local_qiov, qiov->niov + 2); 33853b8242e0SKevin Wolf qemu_iovec_add(&local_qiov, head_buf, offset & (align - 1)); 33863b8242e0SKevin Wolf qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); 33873b8242e0SKevin Wolf use_local_qiov = true; 33883b8242e0SKevin Wolf 33893b8242e0SKevin Wolf bytes += offset & (align - 1); 33903b8242e0SKevin Wolf offset = offset & ~(align - 1); 33913b8242e0SKevin Wolf } 33923b8242e0SKevin Wolf 33933b8242e0SKevin Wolf if ((offset + bytes) & (align - 1)) { 33943b8242e0SKevin Wolf QEMUIOVector tail_qiov; 33953b8242e0SKevin Wolf struct iovec tail_iov; 33963b8242e0SKevin Wolf size_t tail_bytes; 339728de2dcdSKevin Wolf bool waited; 33983b8242e0SKevin Wolf 33993b8242e0SKevin Wolf mark_request_serialising(&req, align); 340028de2dcdSKevin Wolf waited = wait_serialising_requests(&req); 340128de2dcdSKevin Wolf assert(!waited || !use_local_qiov); 34023b8242e0SKevin Wolf 34033b8242e0SKevin Wolf tail_buf = qemu_blockalign(bs, align); 34043b8242e0SKevin Wolf tail_iov = (struct iovec) { 34053b8242e0SKevin Wolf .iov_base = tail_buf, 34063b8242e0SKevin Wolf .iov_len = align, 34073b8242e0SKevin Wolf }; 34083b8242e0SKevin Wolf qemu_iovec_init_external(&tail_qiov, &tail_iov, 1); 34093b8242e0SKevin Wolf 34109e1cb96dSKevin Wolf BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_TAIL); 34113b8242e0SKevin Wolf ret = bdrv_aligned_preadv(bs, &req, (offset + bytes) & ~(align - 1), align, 34123b8242e0SKevin Wolf align, &tail_qiov, 0); 34133b8242e0SKevin Wolf if (ret < 0) { 34143b8242e0SKevin Wolf goto fail; 34153b8242e0SKevin Wolf } 34169e1cb96dSKevin Wolf BLKDBG_EVENT(bs, BLKDBG_PWRITEV_RMW_AFTER_TAIL); 34173b8242e0SKevin Wolf 34183b8242e0SKevin Wolf if (!use_local_qiov) { 34193b8242e0SKevin Wolf qemu_iovec_init(&local_qiov, qiov->niov + 1); 34203b8242e0SKevin Wolf qemu_iovec_concat(&local_qiov, qiov, 0, qiov->size); 34213b8242e0SKevin Wolf use_local_qiov = true; 34223b8242e0SKevin Wolf } 34233b8242e0SKevin Wolf 34243b8242e0SKevin Wolf tail_bytes = (offset + bytes) & (align - 1); 34253b8242e0SKevin Wolf qemu_iovec_add(&local_qiov, tail_buf + tail_bytes, align - tail_bytes); 34263b8242e0SKevin Wolf 34273b8242e0SKevin Wolf bytes = ROUND_UP(bytes, align); 34283b8242e0SKevin Wolf } 34293b8242e0SKevin Wolf 34303b8242e0SKevin Wolf ret = bdrv_aligned_pwritev(bs, &req, offset, bytes, 34313b8242e0SKevin Wolf use_local_qiov ? &local_qiov : qiov, 34323b8242e0SKevin Wolf flags); 34333b8242e0SKevin Wolf 34343b8242e0SKevin Wolf fail: 343565afd211SKevin Wolf tracked_request_end(&req); 3436b404f720SKevin Wolf 34373b8242e0SKevin Wolf if (use_local_qiov) { 34383b8242e0SKevin Wolf qemu_iovec_destroy(&local_qiov); 343999c4a85cSKevin Wolf } 34403b8242e0SKevin Wolf qemu_vfree(head_buf); 34413b8242e0SKevin Wolf qemu_vfree(tail_buf); 34423b8242e0SKevin Wolf 3443b404f720SKevin Wolf return ret; 3444b404f720SKevin Wolf } 3445b404f720SKevin Wolf 34466601553eSKevin Wolf static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 34476601553eSKevin Wolf int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 34486601553eSKevin Wolf BdrvRequestFlags flags) 34496601553eSKevin Wolf { 34506601553eSKevin Wolf if (nb_sectors < 0 || nb_sectors > (INT_MAX >> BDRV_SECTOR_BITS)) { 34516601553eSKevin Wolf return -EINVAL; 34526601553eSKevin Wolf } 34536601553eSKevin Wolf 34546601553eSKevin Wolf return bdrv_co_do_pwritev(bs, sector_num << BDRV_SECTOR_BITS, 34556601553eSKevin Wolf nb_sectors << BDRV_SECTOR_BITS, qiov, flags); 34566601553eSKevin Wolf } 34576601553eSKevin Wolf 3458c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 3459c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 3460c5fbe571SStefan Hajnoczi { 3461c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 3462c5fbe571SStefan Hajnoczi 3463f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 3464f08f2ddaSStefan Hajnoczi } 3465f08f2ddaSStefan Hajnoczi 3466f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 3467aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, 3468aa7bfbffSPeter Lieven BdrvRequestFlags flags) 3469f08f2ddaSStefan Hajnoczi { 347094d6ff21SPaolo Bonzini trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags); 3471f08f2ddaSStefan Hajnoczi 3472d32f35cbSPeter Lieven if (!(bs->open_flags & BDRV_O_UNMAP)) { 3473d32f35cbSPeter Lieven flags &= ~BDRV_REQ_MAY_UNMAP; 3474d32f35cbSPeter Lieven } 3475d32f35cbSPeter Lieven 3476f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 3477aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 3478c5fbe571SStefan Hajnoczi } 3479c5fbe571SStefan Hajnoczi 348083f64091Sbellard /** 348183f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 348283f64091Sbellard */ 348383f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 348483f64091Sbellard { 348583f64091Sbellard BlockDriver *drv = bs->drv; 348651762288SStefan Hajnoczi int ret; 348783f64091Sbellard if (!drv) 348819cb3738Sbellard return -ENOMEDIUM; 348983f64091Sbellard if (!drv->bdrv_truncate) 349083f64091Sbellard return -ENOTSUP; 349159f2689dSNaphtali Sprei if (bs->read_only) 349259f2689dSNaphtali Sprei return -EACCES; 34933718d8abSFam Zheng if (bdrv_op_is_blocked(bs, BLOCK_OP_TYPE_RESIZE, NULL)) { 34948591675fSMarcelo Tosatti return -EBUSY; 34953718d8abSFam Zheng } 349651762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 349751762288SStefan Hajnoczi if (ret == 0) { 349851762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 3499145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 350051762288SStefan Hajnoczi } 350151762288SStefan Hajnoczi return ret; 350283f64091Sbellard } 350383f64091Sbellard 350483f64091Sbellard /** 35054a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 35064a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 35074a1d5e1fSFam Zheng */ 35084a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 35094a1d5e1fSFam Zheng { 35104a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 35114a1d5e1fSFam Zheng if (!drv) { 35124a1d5e1fSFam Zheng return -ENOMEDIUM; 35134a1d5e1fSFam Zheng } 35144a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 35154a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 35164a1d5e1fSFam Zheng } 35174a1d5e1fSFam Zheng if (bs->file) { 35184a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 35194a1d5e1fSFam Zheng } 35204a1d5e1fSFam Zheng return -ENOTSUP; 35214a1d5e1fSFam Zheng } 35224a1d5e1fSFam Zheng 35234a1d5e1fSFam Zheng /** 352483f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 352583f64091Sbellard */ 352683f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 352783f64091Sbellard { 352883f64091Sbellard BlockDriver *drv = bs->drv; 352983f64091Sbellard if (!drv) 353019cb3738Sbellard return -ENOMEDIUM; 353151762288SStefan Hajnoczi 3532b94a2610SKevin Wolf if (drv->has_variable_length) { 3533b94a2610SKevin Wolf int ret = refresh_total_sectors(bs, bs->total_sectors); 3534b94a2610SKevin Wolf if (ret < 0) { 3535b94a2610SKevin Wolf return ret; 3536fc01f7e7Sbellard } 353746a4e4e6SStefan Hajnoczi } 353846a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 353946a4e4e6SStefan Hajnoczi } 3540fc01f7e7Sbellard 354119cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 354296b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 3543fc01f7e7Sbellard { 354419cb3738Sbellard int64_t length; 354519cb3738Sbellard length = bdrv_getlength(bs); 354619cb3738Sbellard if (length < 0) 354719cb3738Sbellard length = 0; 354819cb3738Sbellard else 35496ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 355019cb3738Sbellard *nb_sectors_ptr = length; 3551fc01f7e7Sbellard } 3552cf98951bSbellard 3553ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 3554ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 3555abd7f68dSMarkus Armbruster { 3556abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 3557abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 3558abd7f68dSMarkus Armbruster } 3559abd7f68dSMarkus Armbruster 35601ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 3561abd7f68dSMarkus Armbruster { 3562abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 3563abd7f68dSMarkus Armbruster } 3564abd7f68dSMarkus Armbruster 35653e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 35663e1caa5fSPaolo Bonzini { 35673e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 35683e1caa5fSPaolo Bonzini 35693e1caa5fSPaolo Bonzini switch (on_err) { 35703e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 3571a589569fSWenchao Xia return (error == ENOSPC) ? 3572a589569fSWenchao Xia BLOCK_ERROR_ACTION_STOP : BLOCK_ERROR_ACTION_REPORT; 35733e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 3574a589569fSWenchao Xia return BLOCK_ERROR_ACTION_STOP; 35753e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 3576a589569fSWenchao Xia return BLOCK_ERROR_ACTION_REPORT; 35773e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 3578a589569fSWenchao Xia return BLOCK_ERROR_ACTION_IGNORE; 35793e1caa5fSPaolo Bonzini default: 35803e1caa5fSPaolo Bonzini abort(); 35813e1caa5fSPaolo Bonzini } 35823e1caa5fSPaolo Bonzini } 35833e1caa5fSPaolo Bonzini 35843e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 35853e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 35863e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 35873e1caa5fSPaolo Bonzini */ 35883e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 35893e1caa5fSPaolo Bonzini bool is_read, int error) 35903e1caa5fSPaolo Bonzini { 35913e1caa5fSPaolo Bonzini assert(error >= 0); 35922bd3bce8SPaolo Bonzini 3593a589569fSWenchao Xia if (action == BLOCK_ERROR_ACTION_STOP) { 35942bd3bce8SPaolo Bonzini /* First set the iostatus, so that "info block" returns an iostatus 35952bd3bce8SPaolo Bonzini * that matches the events raised so far (an additional error iostatus 35962bd3bce8SPaolo Bonzini * is fine, but not a lost one). 35972bd3bce8SPaolo Bonzini */ 35983e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 35992bd3bce8SPaolo Bonzini 36002bd3bce8SPaolo Bonzini /* Then raise the request to stop the VM and the event. 36012bd3bce8SPaolo Bonzini * qemu_system_vmstop_request_prepare has two effects. First, 36022bd3bce8SPaolo Bonzini * it ensures that the STOP event always comes after the 36032bd3bce8SPaolo Bonzini * BLOCK_IO_ERROR event. Second, it ensures that even if management 36042bd3bce8SPaolo Bonzini * can observe the STOP event and do a "cont" before the STOP 36052bd3bce8SPaolo Bonzini * event is issued, the VM will not stop. In this case, vm_start() 36062bd3bce8SPaolo Bonzini * also ensures that the STOP/RESUME pair of events is emitted. 36072bd3bce8SPaolo Bonzini */ 36082bd3bce8SPaolo Bonzini qemu_system_vmstop_request_prepare(); 36095a2d2cbdSWenchao Xia qapi_event_send_block_io_error(bdrv_get_device_name(bs), 36105a2d2cbdSWenchao Xia is_read ? IO_OPERATION_TYPE_READ : 36115a2d2cbdSWenchao Xia IO_OPERATION_TYPE_WRITE, 36125a2d2cbdSWenchao Xia action, &error_abort); 36132bd3bce8SPaolo Bonzini qemu_system_vmstop_request(RUN_STATE_IO_ERROR); 36142bd3bce8SPaolo Bonzini } else { 36155a2d2cbdSWenchao Xia qapi_event_send_block_io_error(bdrv_get_device_name(bs), 36165a2d2cbdSWenchao Xia is_read ? IO_OPERATION_TYPE_READ : 36175a2d2cbdSWenchao Xia IO_OPERATION_TYPE_WRITE, 36185a2d2cbdSWenchao Xia action, &error_abort); 36193e1caa5fSPaolo Bonzini } 36203e1caa5fSPaolo Bonzini } 36213e1caa5fSPaolo Bonzini 3622b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 3623b338082bSbellard { 3624b338082bSbellard return bs->read_only; 3625b338082bSbellard } 3626b338082bSbellard 3627985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 3628985a03b0Sths { 3629985a03b0Sths return bs->sg; 3630985a03b0Sths } 3631985a03b0Sths 3632e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 3633e900a7b7SChristoph Hellwig { 3634e900a7b7SChristoph Hellwig return bs->enable_write_cache; 3635e900a7b7SChristoph Hellwig } 3636e900a7b7SChristoph Hellwig 3637425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 3638425b0148SPaolo Bonzini { 3639425b0148SPaolo Bonzini bs->enable_write_cache = wce; 364055b110f2SJeff Cody 364155b110f2SJeff Cody /* so a reopen() will preserve wce */ 364255b110f2SJeff Cody if (wce) { 364355b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 364455b110f2SJeff Cody } else { 364555b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 364655b110f2SJeff Cody } 3647425b0148SPaolo Bonzini } 3648425b0148SPaolo Bonzini 3649ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 3650ea2384d3Sbellard { 3651ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 3652ea2384d3Sbellard return 1; 3653ea2384d3Sbellard return bs->encrypted; 3654ea2384d3Sbellard } 3655ea2384d3Sbellard 3656c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 3657c0f4ce77Saliguori { 3658c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 3659c0f4ce77Saliguori 3660c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 3661c0f4ce77Saliguori return 1; 3662c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 3663c0f4ce77Saliguori } 3664c0f4ce77Saliguori 3665ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 3666ea2384d3Sbellard { 3667ea2384d3Sbellard int ret; 3668ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 3669ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 3670ea2384d3Sbellard if (ret < 0) 3671ea2384d3Sbellard return ret; 3672ea2384d3Sbellard if (!bs->encrypted) 3673ea2384d3Sbellard return 0; 3674ea2384d3Sbellard } 3675fd04a2aeSShahar Havivi if (!bs->encrypted) { 3676fd04a2aeSShahar Havivi return -EINVAL; 3677fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 3678fd04a2aeSShahar Havivi return -ENOMEDIUM; 3679fd04a2aeSShahar Havivi } 3680c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 3681bb5fc20fSaliguori if (ret < 0) { 3682bb5fc20fSaliguori bs->valid_key = 0; 3683bb5fc20fSaliguori } else if (!bs->valid_key) { 3684bb5fc20fSaliguori bs->valid_key = 1; 3685bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 36867d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 3687bb5fc20fSaliguori } 3688c0f4ce77Saliguori return ret; 3689ea2384d3Sbellard } 3690ea2384d3Sbellard 3691f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 3692ea2384d3Sbellard { 3693f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 3694ea2384d3Sbellard } 3695ea2384d3Sbellard 3696ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 3697ea2384d3Sbellard void *opaque) 3698ea2384d3Sbellard { 3699ea2384d3Sbellard BlockDriver *drv; 3700e855e4fbSJeff Cody int count = 0; 3701e855e4fbSJeff Cody const char **formats = NULL; 3702ea2384d3Sbellard 37038a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 3704e855e4fbSJeff Cody if (drv->format_name) { 3705e855e4fbSJeff Cody bool found = false; 3706e855e4fbSJeff Cody int i = count; 3707e855e4fbSJeff Cody while (formats && i && !found) { 3708e855e4fbSJeff Cody found = !strcmp(formats[--i], drv->format_name); 3709e855e4fbSJeff Cody } 3710e855e4fbSJeff Cody 3711e855e4fbSJeff Cody if (!found) { 3712e855e4fbSJeff Cody formats = g_realloc(formats, (count + 1) * sizeof(char *)); 3713e855e4fbSJeff Cody formats[count++] = drv->format_name; 3714ea2384d3Sbellard it(opaque, drv->format_name); 3715ea2384d3Sbellard } 3716ea2384d3Sbellard } 3717e855e4fbSJeff Cody } 3718e855e4fbSJeff Cody g_free(formats); 3719e855e4fbSJeff Cody } 3720ea2384d3Sbellard 3721dc364f4cSBenoît Canet /* This function is to find block backend bs */ 3722b338082bSbellard BlockDriverState *bdrv_find(const char *name) 3723b338082bSbellard { 3724b338082bSbellard BlockDriverState *bs; 3725b338082bSbellard 3726dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 37271b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 3728b338082bSbellard return bs; 3729b338082bSbellard } 37301b7bdbc1SStefan Hajnoczi } 3731b338082bSbellard return NULL; 3732b338082bSbellard } 3733b338082bSbellard 3734dc364f4cSBenoît Canet /* This function is to find a node in the bs graph */ 3735dc364f4cSBenoît Canet BlockDriverState *bdrv_find_node(const char *node_name) 3736dc364f4cSBenoît Canet { 3737dc364f4cSBenoît Canet BlockDriverState *bs; 3738dc364f4cSBenoît Canet 3739dc364f4cSBenoît Canet assert(node_name); 3740dc364f4cSBenoît Canet 3741dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 3742dc364f4cSBenoît Canet if (!strcmp(node_name, bs->node_name)) { 3743dc364f4cSBenoît Canet return bs; 3744dc364f4cSBenoît Canet } 3745dc364f4cSBenoît Canet } 3746dc364f4cSBenoît Canet return NULL; 3747dc364f4cSBenoît Canet } 3748dc364f4cSBenoît Canet 3749c13163fbSBenoît Canet /* Put this QMP function here so it can access the static graph_bdrv_states. */ 3750c13163fbSBenoît Canet BlockDeviceInfoList *bdrv_named_nodes_list(void) 3751c13163fbSBenoît Canet { 3752c13163fbSBenoît Canet BlockDeviceInfoList *list, *entry; 3753c13163fbSBenoît Canet BlockDriverState *bs; 3754c13163fbSBenoît Canet 3755c13163fbSBenoît Canet list = NULL; 3756c13163fbSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 3757c13163fbSBenoît Canet entry = g_malloc0(sizeof(*entry)); 3758c13163fbSBenoît Canet entry->value = bdrv_block_device_info(bs); 3759c13163fbSBenoît Canet entry->next = list; 3760c13163fbSBenoît Canet list = entry; 3761c13163fbSBenoît Canet } 3762c13163fbSBenoît Canet 3763c13163fbSBenoît Canet return list; 3764c13163fbSBenoît Canet } 3765c13163fbSBenoît Canet 376612d3ba82SBenoît Canet BlockDriverState *bdrv_lookup_bs(const char *device, 376712d3ba82SBenoît Canet const char *node_name, 376812d3ba82SBenoît Canet Error **errp) 376912d3ba82SBenoît Canet { 377012d3ba82SBenoît Canet BlockDriverState *bs = NULL; 377112d3ba82SBenoît Canet 377212d3ba82SBenoît Canet if (device) { 377312d3ba82SBenoît Canet bs = bdrv_find(device); 377412d3ba82SBenoît Canet 3775dd67fa50SBenoît Canet if (bs) { 377612d3ba82SBenoît Canet return bs; 377712d3ba82SBenoît Canet } 3778dd67fa50SBenoît Canet } 377912d3ba82SBenoît Canet 3780dd67fa50SBenoît Canet if (node_name) { 378112d3ba82SBenoît Canet bs = bdrv_find_node(node_name); 378212d3ba82SBenoît Canet 3783dd67fa50SBenoît Canet if (bs) { 3784dd67fa50SBenoît Canet return bs; 3785dd67fa50SBenoît Canet } 378612d3ba82SBenoît Canet } 378712d3ba82SBenoît Canet 3788dd67fa50SBenoît Canet error_setg(errp, "Cannot find device=%s nor node_name=%s", 3789dd67fa50SBenoît Canet device ? device : "", 3790dd67fa50SBenoît Canet node_name ? node_name : ""); 3791dd67fa50SBenoît Canet return NULL; 379212d3ba82SBenoît Canet } 379312d3ba82SBenoît Canet 37942f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 37952f399b0aSMarkus Armbruster { 37962f399b0aSMarkus Armbruster if (!bs) { 37972f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 37982f399b0aSMarkus Armbruster } 3799dc364f4cSBenoît Canet return QTAILQ_NEXT(bs, device_list); 38002f399b0aSMarkus Armbruster } 38012f399b0aSMarkus Armbruster 380251de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 380381d0912dSbellard { 380481d0912dSbellard BlockDriverState *bs; 380581d0912dSbellard 3806dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 380751de9760Saliguori it(opaque, bs); 380881d0912dSbellard } 380981d0912dSbellard } 381081d0912dSbellard 3811ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 3812ea2384d3Sbellard { 3813ea2384d3Sbellard return bs->device_name; 3814ea2384d3Sbellard } 3815ea2384d3Sbellard 3816c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 3817c8433287SMarkus Armbruster { 3818c8433287SMarkus Armbruster return bs->open_flags; 3819c8433287SMarkus Armbruster } 3820c8433287SMarkus Armbruster 3821f0f0fdfeSKevin Wolf int bdrv_flush_all(void) 3822c6ca28d6Saliguori { 3823c6ca28d6Saliguori BlockDriverState *bs; 3824f0f0fdfeSKevin Wolf int result = 0; 3825c6ca28d6Saliguori 3826dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 3827ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 3828ed78cda3SStefan Hajnoczi int ret; 3829ed78cda3SStefan Hajnoczi 3830ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 3831ed78cda3SStefan Hajnoczi ret = bdrv_flush(bs); 3832f0f0fdfeSKevin Wolf if (ret < 0 && !result) { 3833f0f0fdfeSKevin Wolf result = ret; 3834c6ca28d6Saliguori } 3835ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 38361b7bdbc1SStefan Hajnoczi } 3837c6ca28d6Saliguori 3838f0f0fdfeSKevin Wolf return result; 3839f0f0fdfeSKevin Wolf } 3840f0f0fdfeSKevin Wolf 38413ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 38423ac21627SPeter Lieven { 38433ac21627SPeter Lieven return 1; 38443ac21627SPeter Lieven } 38453ac21627SPeter Lieven 3846f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 3847f2feebbdSKevin Wolf { 3848f2feebbdSKevin Wolf assert(bs->drv); 3849f2feebbdSKevin Wolf 385011212d8fSPaolo Bonzini /* If BS is a copy on write image, it is initialized to 385111212d8fSPaolo Bonzini the contents of the base image, which may not be zeroes. */ 385211212d8fSPaolo Bonzini if (bs->backing_hd) { 385311212d8fSPaolo Bonzini return 0; 385411212d8fSPaolo Bonzini } 3855336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 3856336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 3857f2feebbdSKevin Wolf } 3858f2feebbdSKevin Wolf 38593ac21627SPeter Lieven /* safe default */ 38603ac21627SPeter Lieven return 0; 3861f2feebbdSKevin Wolf } 3862f2feebbdSKevin Wolf 38634ce78691SPeter Lieven bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) 38644ce78691SPeter Lieven { 38654ce78691SPeter Lieven BlockDriverInfo bdi; 38664ce78691SPeter Lieven 38674ce78691SPeter Lieven if (bs->backing_hd) { 38684ce78691SPeter Lieven return false; 38694ce78691SPeter Lieven } 38704ce78691SPeter Lieven 38714ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 38724ce78691SPeter Lieven return bdi.unallocated_blocks_are_zero; 38734ce78691SPeter Lieven } 38744ce78691SPeter Lieven 38754ce78691SPeter Lieven return false; 38764ce78691SPeter Lieven } 38774ce78691SPeter Lieven 38784ce78691SPeter Lieven bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) 38794ce78691SPeter Lieven { 38804ce78691SPeter Lieven BlockDriverInfo bdi; 38814ce78691SPeter Lieven 38824ce78691SPeter Lieven if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { 38834ce78691SPeter Lieven return false; 38844ce78691SPeter Lieven } 38854ce78691SPeter Lieven 38864ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 38874ce78691SPeter Lieven return bdi.can_write_zeroes_with_unmap; 38884ce78691SPeter Lieven } 38894ce78691SPeter Lieven 38904ce78691SPeter Lieven return false; 38914ce78691SPeter Lieven } 38924ce78691SPeter Lieven 3893b6b8a333SPaolo Bonzini typedef struct BdrvCoGetBlockStatusData { 3894376ae3f1SStefan Hajnoczi BlockDriverState *bs; 3895b35b2bbaSMiroslav Rezanina BlockDriverState *base; 3896376ae3f1SStefan Hajnoczi int64_t sector_num; 3897376ae3f1SStefan Hajnoczi int nb_sectors; 3898376ae3f1SStefan Hajnoczi int *pnum; 3899b6b8a333SPaolo Bonzini int64_t ret; 3900376ae3f1SStefan Hajnoczi bool done; 3901b6b8a333SPaolo Bonzini } BdrvCoGetBlockStatusData; 3902376ae3f1SStefan Hajnoczi 3903f58c7b35Sths /* 3904f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 3905f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 3906f58c7b35Sths * hence all their sectors are reported as allocated. 3907f58c7b35Sths * 3908bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 3909bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 3910bd9533e3SStefan Hajnoczi * 3911f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 3912f58c7b35Sths * the specified sector) that are known to be in the same 3913f58c7b35Sths * allocated/unallocated state. 3914f58c7b35Sths * 3915bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 3916bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 3917f58c7b35Sths */ 3918b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, 3919bdad13b9SPaolo Bonzini int64_t sector_num, 3920060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 3921f58c7b35Sths { 3922617ccb46SPaolo Bonzini int64_t length; 3923f58c7b35Sths int64_t n; 39245daa74a6SPaolo Bonzini int64_t ret, ret2; 3925bd9533e3SStefan Hajnoczi 3926617ccb46SPaolo Bonzini length = bdrv_getlength(bs); 3927617ccb46SPaolo Bonzini if (length < 0) { 3928617ccb46SPaolo Bonzini return length; 3929617ccb46SPaolo Bonzini } 3930617ccb46SPaolo Bonzini 3931617ccb46SPaolo Bonzini if (sector_num >= (length >> BDRV_SECTOR_BITS)) { 39326aebab14SStefan Hajnoczi *pnum = 0; 39336aebab14SStefan Hajnoczi return 0; 39346aebab14SStefan Hajnoczi } 3935bd9533e3SStefan Hajnoczi 39366aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 3937bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 3938bd9533e3SStefan Hajnoczi nb_sectors = n; 3939bd9533e3SStefan Hajnoczi } 3940bd9533e3SStefan Hajnoczi 3941b6b8a333SPaolo Bonzini if (!bs->drv->bdrv_co_get_block_status) { 3942bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 3943e88ae226SKevin Wolf ret = BDRV_BLOCK_DATA | BDRV_BLOCK_ALLOCATED; 3944918e92d7SPaolo Bonzini if (bs->drv->protocol_name) { 3945918e92d7SPaolo Bonzini ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE); 3946918e92d7SPaolo Bonzini } 3947918e92d7SPaolo Bonzini return ret; 39486aebab14SStefan Hajnoczi } 39496aebab14SStefan Hajnoczi 3950415b5b01SPaolo Bonzini ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum); 3951415b5b01SPaolo Bonzini if (ret < 0) { 39523e0a233dSPeter Lieven *pnum = 0; 3953415b5b01SPaolo Bonzini return ret; 3954415b5b01SPaolo Bonzini } 3955415b5b01SPaolo Bonzini 395692bc50a5SPeter Lieven if (ret & BDRV_BLOCK_RAW) { 395792bc50a5SPeter Lieven assert(ret & BDRV_BLOCK_OFFSET_VALID); 395892bc50a5SPeter Lieven return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 395992bc50a5SPeter Lieven *pnum, pnum); 396092bc50a5SPeter Lieven } 396192bc50a5SPeter Lieven 3962e88ae226SKevin Wolf if (ret & (BDRV_BLOCK_DATA | BDRV_BLOCK_ZERO)) { 3963e88ae226SKevin Wolf ret |= BDRV_BLOCK_ALLOCATED; 3964e88ae226SKevin Wolf } 3965e88ae226SKevin Wolf 3966c3d86884SPeter Lieven if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) { 3967c3d86884SPeter Lieven if (bdrv_unallocated_blocks_are_zero(bs)) { 3968415b5b01SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 39691f9db224SPeter Lieven } else if (bs->backing_hd) { 3970f0ad5712SPaolo Bonzini BlockDriverState *bs2 = bs->backing_hd; 3971f0ad5712SPaolo Bonzini int64_t length2 = bdrv_getlength(bs2); 3972f0ad5712SPaolo Bonzini if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) { 3973f0ad5712SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 3974f0ad5712SPaolo Bonzini } 3975f0ad5712SPaolo Bonzini } 3976415b5b01SPaolo Bonzini } 39775daa74a6SPaolo Bonzini 39785daa74a6SPaolo Bonzini if (bs->file && 39795daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && 39805daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_OFFSET_VALID)) { 39815daa74a6SPaolo Bonzini ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 39825daa74a6SPaolo Bonzini *pnum, pnum); 39835daa74a6SPaolo Bonzini if (ret2 >= 0) { 39845daa74a6SPaolo Bonzini /* Ignore errors. This is just providing extra information, it 39855daa74a6SPaolo Bonzini * is useful but not necessary. 39865daa74a6SPaolo Bonzini */ 39875daa74a6SPaolo Bonzini ret |= (ret2 & BDRV_BLOCK_ZERO); 39885daa74a6SPaolo Bonzini } 39895daa74a6SPaolo Bonzini } 39905daa74a6SPaolo Bonzini 3991415b5b01SPaolo Bonzini return ret; 3992060f51c9SStefan Hajnoczi } 3993060f51c9SStefan Hajnoczi 3994b6b8a333SPaolo Bonzini /* Coroutine wrapper for bdrv_get_block_status() */ 3995b6b8a333SPaolo Bonzini static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque) 3996060f51c9SStefan Hajnoczi { 3997b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData *data = opaque; 3998060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 3999060f51c9SStefan Hajnoczi 4000b6b8a333SPaolo Bonzini data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors, 4001060f51c9SStefan Hajnoczi data->pnum); 4002060f51c9SStefan Hajnoczi data->done = true; 4003060f51c9SStefan Hajnoczi } 4004060f51c9SStefan Hajnoczi 4005060f51c9SStefan Hajnoczi /* 4006b6b8a333SPaolo Bonzini * Synchronous wrapper around bdrv_co_get_block_status(). 4007060f51c9SStefan Hajnoczi * 4008b6b8a333SPaolo Bonzini * See bdrv_co_get_block_status() for details. 4009060f51c9SStefan Hajnoczi */ 4010b6b8a333SPaolo Bonzini int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num, 4011b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 4012060f51c9SStefan Hajnoczi { 4013376ae3f1SStefan Hajnoczi Coroutine *co; 4014b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData data = { 4015376ae3f1SStefan Hajnoczi .bs = bs, 4016376ae3f1SStefan Hajnoczi .sector_num = sector_num, 4017376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 4018376ae3f1SStefan Hajnoczi .pnum = pnum, 4019376ae3f1SStefan Hajnoczi .done = false, 4020376ae3f1SStefan Hajnoczi }; 4021376ae3f1SStefan Hajnoczi 4022bdad13b9SPaolo Bonzini if (qemu_in_coroutine()) { 4023bdad13b9SPaolo Bonzini /* Fast-path if already in coroutine context */ 4024b6b8a333SPaolo Bonzini bdrv_get_block_status_co_entry(&data); 4025bdad13b9SPaolo Bonzini } else { 40262572b37aSStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 40272572b37aSStefan Hajnoczi 4028b6b8a333SPaolo Bonzini co = qemu_coroutine_create(bdrv_get_block_status_co_entry); 4029376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 4030376ae3f1SStefan Hajnoczi while (!data.done) { 40312572b37aSStefan Hajnoczi aio_poll(aio_context, true); 4032376ae3f1SStefan Hajnoczi } 4033bdad13b9SPaolo Bonzini } 4034376ae3f1SStefan Hajnoczi return data.ret; 4035376ae3f1SStefan Hajnoczi } 4036f58c7b35Sths 4037b6b8a333SPaolo Bonzini int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, 4038b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 4039b6b8a333SPaolo Bonzini { 40404333bb71SPaolo Bonzini int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum); 40414333bb71SPaolo Bonzini if (ret < 0) { 40424333bb71SPaolo Bonzini return ret; 40434333bb71SPaolo Bonzini } 4044e88ae226SKevin Wolf return (ret & BDRV_BLOCK_ALLOCATED); 4045b6b8a333SPaolo Bonzini } 4046b6b8a333SPaolo Bonzini 4047188a7bbfSPaolo Bonzini /* 4048188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 4049188a7bbfSPaolo Bonzini * 4050188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 4051188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 4052188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 4053188a7bbfSPaolo Bonzini * 4054188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 4055188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 4056188a7bbfSPaolo Bonzini * allocated/unallocated state. 4057188a7bbfSPaolo Bonzini * 4058188a7bbfSPaolo Bonzini */ 40594f578637SPaolo Bonzini int bdrv_is_allocated_above(BlockDriverState *top, 4060188a7bbfSPaolo Bonzini BlockDriverState *base, 4061188a7bbfSPaolo Bonzini int64_t sector_num, 4062188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 4063188a7bbfSPaolo Bonzini { 4064188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 4065188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 4066188a7bbfSPaolo Bonzini 4067188a7bbfSPaolo Bonzini intermediate = top; 4068188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 4069188a7bbfSPaolo Bonzini int pnum_inter; 4070bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors, 4071188a7bbfSPaolo Bonzini &pnum_inter); 4072188a7bbfSPaolo Bonzini if (ret < 0) { 4073188a7bbfSPaolo Bonzini return ret; 4074188a7bbfSPaolo Bonzini } else if (ret) { 4075188a7bbfSPaolo Bonzini *pnum = pnum_inter; 4076188a7bbfSPaolo Bonzini return 1; 4077188a7bbfSPaolo Bonzini } 4078188a7bbfSPaolo Bonzini 4079188a7bbfSPaolo Bonzini /* 4080188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 4081188a7bbfSPaolo Bonzini * might have 4082188a7bbfSPaolo Bonzini * 4083188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 4084188a7bbfSPaolo Bonzini */ 408563ba17d3SVishvananda Ishaya if (n > pnum_inter && 408663ba17d3SVishvananda Ishaya (intermediate == top || 408763ba17d3SVishvananda Ishaya sector_num + pnum_inter < intermediate->total_sectors)) { 4088188a7bbfSPaolo Bonzini n = pnum_inter; 4089188a7bbfSPaolo Bonzini } 4090188a7bbfSPaolo Bonzini 4091188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 4092188a7bbfSPaolo Bonzini } 4093188a7bbfSPaolo Bonzini 4094188a7bbfSPaolo Bonzini *pnum = n; 4095188a7bbfSPaolo Bonzini return 0; 4096188a7bbfSPaolo Bonzini } 4097188a7bbfSPaolo Bonzini 4098045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 4099045df330Saliguori { 4100045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 4101045df330Saliguori return bs->backing_file; 4102045df330Saliguori else if (bs->encrypted) 4103045df330Saliguori return bs->filename; 4104045df330Saliguori else 4105045df330Saliguori return NULL; 4106045df330Saliguori } 4107045df330Saliguori 410883f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 410983f64091Sbellard char *filename, int filename_size) 411083f64091Sbellard { 411183f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 411283f64091Sbellard } 411383f64091Sbellard 4114faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 4115faea38e7Sbellard const uint8_t *buf, int nb_sectors) 4116faea38e7Sbellard { 4117faea38e7Sbellard BlockDriver *drv = bs->drv; 4118faea38e7Sbellard if (!drv) 411919cb3738Sbellard return -ENOMEDIUM; 4120faea38e7Sbellard if (!drv->bdrv_write_compressed) 4121faea38e7Sbellard return -ENOTSUP; 4122fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 4123fbb7b4e0SKevin Wolf return -EIO; 41247cd1e32aSlirans@il.ibm.com 4125e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 41267cd1e32aSlirans@il.ibm.com 4127faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 4128faea38e7Sbellard } 4129faea38e7Sbellard 4130faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 4131faea38e7Sbellard { 4132faea38e7Sbellard BlockDriver *drv = bs->drv; 4133faea38e7Sbellard if (!drv) 413419cb3738Sbellard return -ENOMEDIUM; 4135faea38e7Sbellard if (!drv->bdrv_get_info) 4136faea38e7Sbellard return -ENOTSUP; 4137faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 4138faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 4139faea38e7Sbellard } 4140faea38e7Sbellard 4141eae041feSMax Reitz ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) 4142eae041feSMax Reitz { 4143eae041feSMax Reitz BlockDriver *drv = bs->drv; 4144eae041feSMax Reitz if (drv && drv->bdrv_get_specific_info) { 4145eae041feSMax Reitz return drv->bdrv_get_specific_info(bs); 4146eae041feSMax Reitz } 4147eae041feSMax Reitz return NULL; 4148eae041feSMax Reitz } 4149eae041feSMax Reitz 415045566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 415145566e9cSChristoph Hellwig int64_t pos, int size) 4152178e08a5Saliguori { 4153cf8074b3SKevin Wolf QEMUIOVector qiov; 4154cf8074b3SKevin Wolf struct iovec iov = { 4155cf8074b3SKevin Wolf .iov_base = (void *) buf, 4156cf8074b3SKevin Wolf .iov_len = size, 4157cf8074b3SKevin Wolf }; 4158cf8074b3SKevin Wolf 4159cf8074b3SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 4160cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs, &qiov, pos); 4161cf8074b3SKevin Wolf } 4162cf8074b3SKevin Wolf 4163cf8074b3SKevin Wolf int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) 4164cf8074b3SKevin Wolf { 4165178e08a5Saliguori BlockDriver *drv = bs->drv; 4166cf8074b3SKevin Wolf 4167cf8074b3SKevin Wolf if (!drv) { 4168178e08a5Saliguori return -ENOMEDIUM; 4169cf8074b3SKevin Wolf } else if (drv->bdrv_save_vmstate) { 4170cf8074b3SKevin Wolf return drv->bdrv_save_vmstate(bs, qiov, pos); 4171cf8074b3SKevin Wolf } else if (bs->file) { 4172cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs->file, qiov, pos); 4173cf8074b3SKevin Wolf } 4174cf8074b3SKevin Wolf 41757cdb1f6dSMORITA Kazutaka return -ENOTSUP; 4176178e08a5Saliguori } 4177178e08a5Saliguori 417845566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 417945566e9cSChristoph Hellwig int64_t pos, int size) 4180178e08a5Saliguori { 4181178e08a5Saliguori BlockDriver *drv = bs->drv; 4182178e08a5Saliguori if (!drv) 4183178e08a5Saliguori return -ENOMEDIUM; 41847cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 418545566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 41867cdb1f6dSMORITA Kazutaka if (bs->file) 41877cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 41887cdb1f6dSMORITA Kazutaka return -ENOTSUP; 4189178e08a5Saliguori } 4190178e08a5Saliguori 41918b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 41928b9b0cc2SKevin Wolf { 4193bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 41948b9b0cc2SKevin Wolf return; 41958b9b0cc2SKevin Wolf } 41968b9b0cc2SKevin Wolf 4197bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 419841c695c7SKevin Wolf } 41998b9b0cc2SKevin Wolf 420041c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 420141c695c7SKevin Wolf const char *tag) 420241c695c7SKevin Wolf { 420341c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 420441c695c7SKevin Wolf bs = bs->file; 420541c695c7SKevin Wolf } 420641c695c7SKevin Wolf 420741c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 420841c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 420941c695c7SKevin Wolf } 421041c695c7SKevin Wolf 421141c695c7SKevin Wolf return -ENOTSUP; 421241c695c7SKevin Wolf } 421341c695c7SKevin Wolf 42144cc70e93SFam Zheng int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) 42154cc70e93SFam Zheng { 42164cc70e93SFam Zheng while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { 42174cc70e93SFam Zheng bs = bs->file; 42184cc70e93SFam Zheng } 42194cc70e93SFam Zheng 42204cc70e93SFam Zheng if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { 42214cc70e93SFam Zheng return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); 42224cc70e93SFam Zheng } 42234cc70e93SFam Zheng 42244cc70e93SFam Zheng return -ENOTSUP; 42254cc70e93SFam Zheng } 42264cc70e93SFam Zheng 422741c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 422841c695c7SKevin Wolf { 4229938789eaSMax Reitz while (bs && (!bs->drv || !bs->drv->bdrv_debug_resume)) { 423041c695c7SKevin Wolf bs = bs->file; 423141c695c7SKevin Wolf } 423241c695c7SKevin Wolf 423341c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 423441c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 423541c695c7SKevin Wolf } 423641c695c7SKevin Wolf 423741c695c7SKevin Wolf return -ENOTSUP; 423841c695c7SKevin Wolf } 423941c695c7SKevin Wolf 424041c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 424141c695c7SKevin Wolf { 424241c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 424341c695c7SKevin Wolf bs = bs->file; 424441c695c7SKevin Wolf } 424541c695c7SKevin Wolf 424641c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 424741c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 424841c695c7SKevin Wolf } 424941c695c7SKevin Wolf 425041c695c7SKevin Wolf return false; 42518b9b0cc2SKevin Wolf } 42528b9b0cc2SKevin Wolf 4253199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 4254199630b6SBlue Swirl { 4255199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 4256199630b6SBlue Swirl } 4257199630b6SBlue Swirl 4258b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 4259b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 4260b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 4261b1b1d783SJeff Cody * the CWD rather than the chain. */ 4262e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 4263e8a6bb9cSMarcelo Tosatti const char *backing_file) 4264e8a6bb9cSMarcelo Tosatti { 4265b1b1d783SJeff Cody char *filename_full = NULL; 4266b1b1d783SJeff Cody char *backing_file_full = NULL; 4267b1b1d783SJeff Cody char *filename_tmp = NULL; 4268b1b1d783SJeff Cody int is_protocol = 0; 4269b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 4270b1b1d783SJeff Cody BlockDriverState *retval = NULL; 4271b1b1d783SJeff Cody 4272b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 4273e8a6bb9cSMarcelo Tosatti return NULL; 4274e8a6bb9cSMarcelo Tosatti } 4275e8a6bb9cSMarcelo Tosatti 4276b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 4277b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 4278b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 4279b1b1d783SJeff Cody 4280b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 4281b1b1d783SJeff Cody 4282b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 4283b1b1d783SJeff Cody 4284b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 4285b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 4286b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 4287b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 4288b1b1d783SJeff Cody retval = curr_bs->backing_hd; 4289b1b1d783SJeff Cody break; 4290b1b1d783SJeff Cody } 4291e8a6bb9cSMarcelo Tosatti } else { 4292b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 4293b1b1d783SJeff Cody * image's filename path */ 4294b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 4295b1b1d783SJeff Cody backing_file); 4296b1b1d783SJeff Cody 4297b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 4298b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 4299b1b1d783SJeff Cody continue; 4300b1b1d783SJeff Cody } 4301b1b1d783SJeff Cody 4302b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 4303b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 4304b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 4305b1b1d783SJeff Cody curr_bs->backing_file); 4306b1b1d783SJeff Cody 4307b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 4308b1b1d783SJeff Cody continue; 4309b1b1d783SJeff Cody } 4310b1b1d783SJeff Cody 4311b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 4312b1b1d783SJeff Cody retval = curr_bs->backing_hd; 4313b1b1d783SJeff Cody break; 4314b1b1d783SJeff Cody } 4315e8a6bb9cSMarcelo Tosatti } 4316e8a6bb9cSMarcelo Tosatti } 4317e8a6bb9cSMarcelo Tosatti 4318b1b1d783SJeff Cody g_free(filename_full); 4319b1b1d783SJeff Cody g_free(backing_file_full); 4320b1b1d783SJeff Cody g_free(filename_tmp); 4321b1b1d783SJeff Cody return retval; 4322e8a6bb9cSMarcelo Tosatti } 4323e8a6bb9cSMarcelo Tosatti 4324f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 4325f198fd1cSBenoît Canet { 4326f198fd1cSBenoît Canet if (!bs->drv) { 4327f198fd1cSBenoît Canet return 0; 4328f198fd1cSBenoît Canet } 4329f198fd1cSBenoît Canet 4330f198fd1cSBenoît Canet if (!bs->backing_hd) { 4331f198fd1cSBenoît Canet return 0; 4332f198fd1cSBenoît Canet } 4333f198fd1cSBenoît Canet 4334f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 4335f198fd1cSBenoît Canet } 4336f198fd1cSBenoît Canet 433779fac568SJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 433879fac568SJeff Cody { 433979fac568SJeff Cody BlockDriverState *curr_bs = NULL; 434079fac568SJeff Cody 434179fac568SJeff Cody if (!bs) { 434279fac568SJeff Cody return NULL; 434379fac568SJeff Cody } 434479fac568SJeff Cody 434579fac568SJeff Cody curr_bs = bs; 434679fac568SJeff Cody 434779fac568SJeff Cody while (curr_bs->backing_hd) { 434879fac568SJeff Cody curr_bs = curr_bs->backing_hd; 434979fac568SJeff Cody } 435079fac568SJeff Cody return curr_bs; 435179fac568SJeff Cody } 435279fac568SJeff Cody 4353ea2384d3Sbellard /**************************************************************/ 435483f64091Sbellard /* async I/Os */ 4355ea2384d3Sbellard 43563b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 4357f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 435883f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 4359ea2384d3Sbellard { 4360bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 4361bbf0a440SStefan Hajnoczi 4362d20d9b7cSPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, 43638c5873d6SStefan Hajnoczi cb, opaque, false); 436483f64091Sbellard } 436583f64091Sbellard 4366f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 4367f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 436883f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 43697674e7bfSbellard { 4370bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 4371bbf0a440SStefan Hajnoczi 4372d20d9b7cSPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, 43738c5873d6SStefan Hajnoczi cb, opaque, true); 437483f64091Sbellard } 437583f64091Sbellard 4376d5ef94d4SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs, 4377d5ef94d4SPaolo Bonzini int64_t sector_num, int nb_sectors, BdrvRequestFlags flags, 4378d5ef94d4SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 4379d5ef94d4SPaolo Bonzini { 4380d5ef94d4SPaolo Bonzini trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque); 4381d5ef94d4SPaolo Bonzini 4382d5ef94d4SPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors, 4383d5ef94d4SPaolo Bonzini BDRV_REQ_ZERO_WRITE | flags, 4384d5ef94d4SPaolo Bonzini cb, opaque, true); 4385d5ef94d4SPaolo Bonzini } 4386d5ef94d4SPaolo Bonzini 438740b4f539SKevin Wolf 438840b4f539SKevin Wolf typedef struct MultiwriteCB { 438940b4f539SKevin Wolf int error; 439040b4f539SKevin Wolf int num_requests; 439140b4f539SKevin Wolf int num_callbacks; 439240b4f539SKevin Wolf struct { 439340b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 439440b4f539SKevin Wolf void *opaque; 439540b4f539SKevin Wolf QEMUIOVector *free_qiov; 439640b4f539SKevin Wolf } callbacks[]; 439740b4f539SKevin Wolf } MultiwriteCB; 439840b4f539SKevin Wolf 439940b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 440040b4f539SKevin Wolf { 440140b4f539SKevin Wolf int i; 440240b4f539SKevin Wolf 440340b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 440440b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 44051e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 44061e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 44071e1ea48dSStefan Hajnoczi } 44087267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 440940b4f539SKevin Wolf } 441040b4f539SKevin Wolf } 441140b4f539SKevin Wolf 441240b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 441340b4f539SKevin Wolf { 441440b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 441540b4f539SKevin Wolf 44166d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 44176d519a5fSStefan Hajnoczi 4418cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 441940b4f539SKevin Wolf mcb->error = ret; 442040b4f539SKevin Wolf } 442140b4f539SKevin Wolf 442240b4f539SKevin Wolf mcb->num_requests--; 442340b4f539SKevin Wolf if (mcb->num_requests == 0) { 442440b4f539SKevin Wolf multiwrite_user_cb(mcb); 44257267c094SAnthony Liguori g_free(mcb); 442640b4f539SKevin Wolf } 442740b4f539SKevin Wolf } 442840b4f539SKevin Wolf 442940b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 443040b4f539SKevin Wolf { 443177be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 443277be4366SChristoph Hellwig 443377be4366SChristoph Hellwig /* 443477be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 443577be4366SChristoph Hellwig * here as that could overflow the return value. 443677be4366SChristoph Hellwig */ 443777be4366SChristoph Hellwig if (req1->sector > req2->sector) { 443877be4366SChristoph Hellwig return 1; 443977be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 444077be4366SChristoph Hellwig return -1; 444177be4366SChristoph Hellwig } else { 444277be4366SChristoph Hellwig return 0; 444377be4366SChristoph Hellwig } 444440b4f539SKevin Wolf } 444540b4f539SKevin Wolf 444640b4f539SKevin Wolf /* 444740b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 444840b4f539SKevin Wolf * requests that remain after merging. 444940b4f539SKevin Wolf */ 445040b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 445140b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 445240b4f539SKevin Wolf { 445340b4f539SKevin Wolf int i, outidx; 445440b4f539SKevin Wolf 445540b4f539SKevin Wolf // Sort requests by start sector 445640b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 445740b4f539SKevin Wolf 445840b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 445940b4f539SKevin Wolf // filling up gaps with zero sectors. 446040b4f539SKevin Wolf outidx = 0; 446140b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 446240b4f539SKevin Wolf int merge = 0; 446340b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 446440b4f539SKevin Wolf 4465b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 446640b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 446740b4f539SKevin Wolf merge = 1; 446840b4f539SKevin Wolf } 446940b4f539SKevin Wolf 4470e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 4471e2a305fbSChristoph Hellwig merge = 0; 4472e2a305fbSChristoph Hellwig } 4473e2a305fbSChristoph Hellwig 447440b4f539SKevin Wolf if (merge) { 447540b4f539SKevin Wolf size_t size; 44767267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 447740b4f539SKevin Wolf qemu_iovec_init(qiov, 447840b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 447940b4f539SKevin Wolf 448040b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 448140b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 448240b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 44831b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 448440b4f539SKevin Wolf 4485b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 4486b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 448740b4f539SKevin Wolf 448840b4f539SKevin Wolf // Add the second request 44891b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 449040b4f539SKevin Wolf 4491cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 449240b4f539SKevin Wolf reqs[outidx].qiov = qiov; 449340b4f539SKevin Wolf 449440b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 449540b4f539SKevin Wolf } else { 449640b4f539SKevin Wolf outidx++; 449740b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 449840b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 449940b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 450040b4f539SKevin Wolf } 450140b4f539SKevin Wolf } 450240b4f539SKevin Wolf 450340b4f539SKevin Wolf return outidx + 1; 450440b4f539SKevin Wolf } 450540b4f539SKevin Wolf 450640b4f539SKevin Wolf /* 450740b4f539SKevin Wolf * Submit multiple AIO write requests at once. 450840b4f539SKevin Wolf * 450940b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 451040b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 451140b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 451240b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 451340b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 451440b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 451540b4f539SKevin Wolf * 451640b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 451740b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 451840b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 451940b4f539SKevin Wolf */ 452040b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 452140b4f539SKevin Wolf { 452240b4f539SKevin Wolf MultiwriteCB *mcb; 452340b4f539SKevin Wolf int i; 452440b4f539SKevin Wolf 4525301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 4526301db7c2SRyan Harper if (bs->drv == NULL) { 4527301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 4528301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 4529301db7c2SRyan Harper } 4530301db7c2SRyan Harper return -1; 4531301db7c2SRyan Harper } 4532301db7c2SRyan Harper 453340b4f539SKevin Wolf if (num_reqs == 0) { 453440b4f539SKevin Wolf return 0; 453540b4f539SKevin Wolf } 453640b4f539SKevin Wolf 453740b4f539SKevin Wolf // Create MultiwriteCB structure 45387267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 453940b4f539SKevin Wolf mcb->num_requests = 0; 454040b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 454140b4f539SKevin Wolf 454240b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 454340b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 454440b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 454540b4f539SKevin Wolf } 454640b4f539SKevin Wolf 454740b4f539SKevin Wolf // Check for mergable requests 454840b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 454940b4f539SKevin Wolf 45506d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 45516d519a5fSStefan Hajnoczi 4552df9309fbSPaolo Bonzini /* Run the aio requests. */ 4553df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 455440b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 4555d20d9b7cSPaolo Bonzini bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov, 4556d20d9b7cSPaolo Bonzini reqs[i].nb_sectors, reqs[i].flags, 4557d20d9b7cSPaolo Bonzini multiwrite_cb, mcb, 4558d20d9b7cSPaolo Bonzini true); 455940b4f539SKevin Wolf } 456040b4f539SKevin Wolf 456140b4f539SKevin Wolf return 0; 456240b4f539SKevin Wolf } 456340b4f539SKevin Wolf 456483f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 456583f64091Sbellard { 4566d7331bedSStefan Hajnoczi acb->aiocb_info->cancel(acb); 456783f64091Sbellard } 456883f64091Sbellard 456983f64091Sbellard /**************************************************************/ 457083f64091Sbellard /* async block device emulation */ 457183f64091Sbellard 4572c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 4573c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 4574c16b5a2cSChristoph Hellwig QEMUBH *bh; 4575c16b5a2cSChristoph Hellwig int ret; 4576c16b5a2cSChristoph Hellwig /* vector translation state */ 4577c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 4578c16b5a2cSChristoph Hellwig uint8_t *bounce; 4579c16b5a2cSChristoph Hellwig int is_write; 4580c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 4581c16b5a2cSChristoph Hellwig 4582c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 4583c16b5a2cSChristoph Hellwig { 4584b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 4585b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 45866a7ad299SDor Laor qemu_bh_delete(acb->bh); 458736afc451SAvi Kivity acb->bh = NULL; 4588c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 4589c16b5a2cSChristoph Hellwig } 4590c16b5a2cSChristoph Hellwig 4591d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_aiocb_info = { 4592c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 4593c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 4594c16b5a2cSChristoph Hellwig }; 4595c16b5a2cSChristoph Hellwig 459683f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 4597beac80cdSbellard { 4598ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 4599f141eafeSaliguori 4600f141eafeSaliguori if (!acb->is_write) 460103396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 4602ceb42de8Saliguori qemu_vfree(acb->bounce); 4603ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 46046a7ad299SDor Laor qemu_bh_delete(acb->bh); 460536afc451SAvi Kivity acb->bh = NULL; 4606ce1a14dcSpbrook qemu_aio_release(acb); 4607beac80cdSbellard } 4608beac80cdSbellard 4609f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 4610f141eafeSaliguori int64_t sector_num, 4611f141eafeSaliguori QEMUIOVector *qiov, 4612f141eafeSaliguori int nb_sectors, 4613f141eafeSaliguori BlockDriverCompletionFunc *cb, 4614f141eafeSaliguori void *opaque, 4615f141eafeSaliguori int is_write) 4616f141eafeSaliguori 4617ea2384d3Sbellard { 4618ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 461983f64091Sbellard 4620d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); 4621f141eafeSaliguori acb->is_write = is_write; 4622f141eafeSaliguori acb->qiov = qiov; 4623e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 46242572b37aSStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_aio_bh_cb, acb); 4625f141eafeSaliguori 4626f141eafeSaliguori if (is_write) { 4627d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 46281ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 4629f141eafeSaliguori } else { 46301ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 4631f141eafeSaliguori } 4632f141eafeSaliguori 4633ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 4634f141eafeSaliguori 4635ce1a14dcSpbrook return &acb->common; 46367a6cba61Spbrook } 46377a6cba61Spbrook 4638f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 4639f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 4640ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 464183f64091Sbellard { 4642f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 464383f64091Sbellard } 464483f64091Sbellard 4645f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 4646f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 4647f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 4648f141eafeSaliguori { 4649f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 4650f141eafeSaliguori } 4651f141eafeSaliguori 465268485420SKevin Wolf 465368485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 465468485420SKevin Wolf BlockDriverAIOCB common; 465568485420SKevin Wolf BlockRequest req; 465668485420SKevin Wolf bool is_write; 4657d318aea9SKevin Wolf bool *done; 465868485420SKevin Wolf QEMUBH* bh; 465968485420SKevin Wolf } BlockDriverAIOCBCoroutine; 466068485420SKevin Wolf 466168485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 466268485420SKevin Wolf { 46632572b37aSStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(blockacb->bs); 4664d318aea9SKevin Wolf BlockDriverAIOCBCoroutine *acb = 4665d318aea9SKevin Wolf container_of(blockacb, BlockDriverAIOCBCoroutine, common); 4666d318aea9SKevin Wolf bool done = false; 4667d318aea9SKevin Wolf 4668d318aea9SKevin Wolf acb->done = &done; 4669d318aea9SKevin Wolf while (!done) { 46702572b37aSStefan Hajnoczi aio_poll(aio_context, true); 4671d318aea9SKevin Wolf } 467268485420SKevin Wolf } 467368485420SKevin Wolf 4674d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_co_aiocb_info = { 467568485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 467668485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 467768485420SKevin Wolf }; 467868485420SKevin Wolf 467935246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 468068485420SKevin Wolf { 468168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 468268485420SKevin Wolf 468368485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 4684d318aea9SKevin Wolf 4685d318aea9SKevin Wolf if (acb->done) { 4686d318aea9SKevin Wolf *acb->done = true; 4687d318aea9SKevin Wolf } 4688d318aea9SKevin Wolf 468968485420SKevin Wolf qemu_bh_delete(acb->bh); 469068485420SKevin Wolf qemu_aio_release(acb); 469168485420SKevin Wolf } 469268485420SKevin Wolf 4693b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 4694b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 4695b2a61371SStefan Hajnoczi { 4696b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 4697b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 4698b2a61371SStefan Hajnoczi 4699b2a61371SStefan Hajnoczi if (!acb->is_write) { 4700b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 4701d20d9b7cSPaolo Bonzini acb->req.nb_sectors, acb->req.qiov, acb->req.flags); 4702b2a61371SStefan Hajnoczi } else { 4703b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 4704d20d9b7cSPaolo Bonzini acb->req.nb_sectors, acb->req.qiov, acb->req.flags); 4705b2a61371SStefan Hajnoczi } 4706b2a61371SStefan Hajnoczi 47072572b37aSStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_co_em_bh, acb); 4708b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 4709b2a61371SStefan Hajnoczi } 4710b2a61371SStefan Hajnoczi 471168485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 471268485420SKevin Wolf int64_t sector_num, 471368485420SKevin Wolf QEMUIOVector *qiov, 471468485420SKevin Wolf int nb_sectors, 4715d20d9b7cSPaolo Bonzini BdrvRequestFlags flags, 471668485420SKevin Wolf BlockDriverCompletionFunc *cb, 471768485420SKevin Wolf void *opaque, 47188c5873d6SStefan Hajnoczi bool is_write) 471968485420SKevin Wolf { 472068485420SKevin Wolf Coroutine *co; 472168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 472268485420SKevin Wolf 4723d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 472468485420SKevin Wolf acb->req.sector = sector_num; 472568485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 472668485420SKevin Wolf acb->req.qiov = qiov; 4727d20d9b7cSPaolo Bonzini acb->req.flags = flags; 472868485420SKevin Wolf acb->is_write = is_write; 4729d318aea9SKevin Wolf acb->done = NULL; 473068485420SKevin Wolf 47318c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 473268485420SKevin Wolf qemu_coroutine_enter(co, acb); 473368485420SKevin Wolf 473468485420SKevin Wolf return &acb->common; 473568485420SKevin Wolf } 473668485420SKevin Wolf 473707f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 4738b2e12bc6SChristoph Hellwig { 473907f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 474007f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 4741b2e12bc6SChristoph Hellwig 474207f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 47432572b37aSStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_co_em_bh, acb); 4744b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 4745b2e12bc6SChristoph Hellwig } 4746b2e12bc6SChristoph Hellwig 474707f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 4748016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 4749016f5cf6SAlexander Graf { 475007f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 4751016f5cf6SAlexander Graf 475207f07615SPaolo Bonzini Coroutine *co; 475307f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 4754016f5cf6SAlexander Graf 4755d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 4756d318aea9SKevin Wolf acb->done = NULL; 4757d318aea9SKevin Wolf 475807f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 475907f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 4760016f5cf6SAlexander Graf 4761016f5cf6SAlexander Graf return &acb->common; 4762016f5cf6SAlexander Graf } 4763016f5cf6SAlexander Graf 47644265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 47654265d620SPaolo Bonzini { 47664265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 47674265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 47684265d620SPaolo Bonzini 47694265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 47702572b37aSStefan Hajnoczi acb->bh = aio_bh_new(bdrv_get_aio_context(bs), bdrv_co_em_bh, acb); 47714265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 47724265d620SPaolo Bonzini } 47734265d620SPaolo Bonzini 47744265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 47754265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 47764265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 47774265d620SPaolo Bonzini { 47784265d620SPaolo Bonzini Coroutine *co; 47794265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 47804265d620SPaolo Bonzini 47814265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 47824265d620SPaolo Bonzini 4783d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 47844265d620SPaolo Bonzini acb->req.sector = sector_num; 47854265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 4786d318aea9SKevin Wolf acb->done = NULL; 47874265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 47884265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 47894265d620SPaolo Bonzini 47904265d620SPaolo Bonzini return &acb->common; 47914265d620SPaolo Bonzini } 47924265d620SPaolo Bonzini 4793ea2384d3Sbellard void bdrv_init(void) 4794ea2384d3Sbellard { 47955efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 4796ea2384d3Sbellard } 4797ce1a14dcSpbrook 4798eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 4799eb852011SMarkus Armbruster { 4800eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 4801eb852011SMarkus Armbruster bdrv_init(); 4802eb852011SMarkus Armbruster } 4803eb852011SMarkus Armbruster 4804d7331bedSStefan Hajnoczi void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 48056bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 48066bbff9a0Saliguori { 4807ce1a14dcSpbrook BlockDriverAIOCB *acb; 4808ce1a14dcSpbrook 4809d7331bedSStefan Hajnoczi acb = g_slice_alloc(aiocb_info->aiocb_size); 4810d7331bedSStefan Hajnoczi acb->aiocb_info = aiocb_info; 4811ce1a14dcSpbrook acb->bs = bs; 4812ce1a14dcSpbrook acb->cb = cb; 4813ce1a14dcSpbrook acb->opaque = opaque; 4814ce1a14dcSpbrook return acb; 4815ce1a14dcSpbrook } 4816ce1a14dcSpbrook 4817ce1a14dcSpbrook void qemu_aio_release(void *p) 4818ce1a14dcSpbrook { 4819d37c975fSStefan Hajnoczi BlockDriverAIOCB *acb = p; 4820d7331bedSStefan Hajnoczi g_slice_free1(acb->aiocb_info->aiocb_size, acb); 4821ce1a14dcSpbrook } 482219cb3738Sbellard 482319cb3738Sbellard /**************************************************************/ 4824f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 4825f9f05dc5SKevin Wolf 4826f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 4827f9f05dc5SKevin Wolf Coroutine *coroutine; 4828f9f05dc5SKevin Wolf int ret; 4829f9f05dc5SKevin Wolf } CoroutineIOCompletion; 4830f9f05dc5SKevin Wolf 4831f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 4832f9f05dc5SKevin Wolf { 4833f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 4834f9f05dc5SKevin Wolf 4835f9f05dc5SKevin Wolf co->ret = ret; 4836f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 4837f9f05dc5SKevin Wolf } 4838f9f05dc5SKevin Wolf 4839f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 4840f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 4841f9f05dc5SKevin Wolf bool is_write) 4842f9f05dc5SKevin Wolf { 4843f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 4844f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 4845f9f05dc5SKevin Wolf }; 4846f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 4847f9f05dc5SKevin Wolf 4848f9f05dc5SKevin Wolf if (is_write) { 4849a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 4850f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4851f9f05dc5SKevin Wolf } else { 4852a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 4853f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4854f9f05dc5SKevin Wolf } 4855f9f05dc5SKevin Wolf 485659370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 4857f9f05dc5SKevin Wolf if (!acb) { 4858f9f05dc5SKevin Wolf return -EIO; 4859f9f05dc5SKevin Wolf } 4860f9f05dc5SKevin Wolf qemu_coroutine_yield(); 4861f9f05dc5SKevin Wolf 4862f9f05dc5SKevin Wolf return co.ret; 4863f9f05dc5SKevin Wolf } 4864f9f05dc5SKevin Wolf 4865f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 4866f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4867f9f05dc5SKevin Wolf QEMUIOVector *iov) 4868f9f05dc5SKevin Wolf { 4869f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 4870f9f05dc5SKevin Wolf } 4871f9f05dc5SKevin Wolf 4872f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 4873f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4874f9f05dc5SKevin Wolf QEMUIOVector *iov) 4875f9f05dc5SKevin Wolf { 4876f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 4877f9f05dc5SKevin Wolf } 4878f9f05dc5SKevin Wolf 487907f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 4880e7a8a783SKevin Wolf { 488107f07615SPaolo Bonzini RwCo *rwco = opaque; 488207f07615SPaolo Bonzini 488307f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 488407f07615SPaolo Bonzini } 488507f07615SPaolo Bonzini 488607f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 488707f07615SPaolo Bonzini { 4888eb489bb1SKevin Wolf int ret; 4889eb489bb1SKevin Wolf 489029cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 489107f07615SPaolo Bonzini return 0; 4892eb489bb1SKevin Wolf } 4893eb489bb1SKevin Wolf 4894ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 4895bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); 4896eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 4897eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 4898eb489bb1SKevin Wolf if (ret < 0) { 4899eb489bb1SKevin Wolf return ret; 4900eb489bb1SKevin Wolf } 4901eb489bb1SKevin Wolf } 4902eb489bb1SKevin Wolf 4903ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 4904ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 4905d4c82329SKevin Wolf goto flush_parent; 4906ca716364SKevin Wolf } 4907ca716364SKevin Wolf 4908bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); 4909eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 491029cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 491107f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 491207f07615SPaolo Bonzini BlockDriverAIOCB *acb; 4913e7a8a783SKevin Wolf CoroutineIOCompletion co = { 4914e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 4915e7a8a783SKevin Wolf }; 4916e7a8a783SKevin Wolf 491707f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 491807f07615SPaolo Bonzini if (acb == NULL) { 491929cdb251SPaolo Bonzini ret = -EIO; 492007f07615SPaolo Bonzini } else { 4921e7a8a783SKevin Wolf qemu_coroutine_yield(); 492229cdb251SPaolo Bonzini ret = co.ret; 4923e7a8a783SKevin Wolf } 492407f07615SPaolo Bonzini } else { 492507f07615SPaolo Bonzini /* 492607f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 492707f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 492807f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 492907f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 493007f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 493107f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 493207f07615SPaolo Bonzini * mode. 493307f07615SPaolo Bonzini * 493407f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 493507f07615SPaolo Bonzini */ 493629cdb251SPaolo Bonzini ret = 0; 493707f07615SPaolo Bonzini } 493829cdb251SPaolo Bonzini if (ret < 0) { 493929cdb251SPaolo Bonzini return ret; 494029cdb251SPaolo Bonzini } 494129cdb251SPaolo Bonzini 494229cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 494329cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 494429cdb251SPaolo Bonzini */ 4945d4c82329SKevin Wolf flush_parent: 494629cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 494707f07615SPaolo Bonzini } 494807f07615SPaolo Bonzini 49495a8a30dbSKevin Wolf void bdrv_invalidate_cache(BlockDriverState *bs, Error **errp) 49500f15423cSAnthony Liguori { 49515a8a30dbSKevin Wolf Error *local_err = NULL; 49525a8a30dbSKevin Wolf int ret; 49535a8a30dbSKevin Wolf 49543456a8d1SKevin Wolf if (!bs->drv) { 49553456a8d1SKevin Wolf return; 49560f15423cSAnthony Liguori } 49573456a8d1SKevin Wolf 49583456a8d1SKevin Wolf if (bs->drv->bdrv_invalidate_cache) { 49595a8a30dbSKevin Wolf bs->drv->bdrv_invalidate_cache(bs, &local_err); 49603456a8d1SKevin Wolf } else if (bs->file) { 49615a8a30dbSKevin Wolf bdrv_invalidate_cache(bs->file, &local_err); 49625a8a30dbSKevin Wolf } 49635a8a30dbSKevin Wolf if (local_err) { 49645a8a30dbSKevin Wolf error_propagate(errp, local_err); 49655a8a30dbSKevin Wolf return; 49663456a8d1SKevin Wolf } 49673456a8d1SKevin Wolf 49685a8a30dbSKevin Wolf ret = refresh_total_sectors(bs, bs->total_sectors); 49695a8a30dbSKevin Wolf if (ret < 0) { 49705a8a30dbSKevin Wolf error_setg_errno(errp, -ret, "Could not refresh total sector count"); 49715a8a30dbSKevin Wolf return; 49725a8a30dbSKevin Wolf } 49730f15423cSAnthony Liguori } 49740f15423cSAnthony Liguori 49755a8a30dbSKevin Wolf void bdrv_invalidate_cache_all(Error **errp) 49760f15423cSAnthony Liguori { 49770f15423cSAnthony Liguori BlockDriverState *bs; 49785a8a30dbSKevin Wolf Error *local_err = NULL; 49790f15423cSAnthony Liguori 4980dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 4981ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 4982ed78cda3SStefan Hajnoczi 4983ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 49845a8a30dbSKevin Wolf bdrv_invalidate_cache(bs, &local_err); 4985ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 49865a8a30dbSKevin Wolf if (local_err) { 49875a8a30dbSKevin Wolf error_propagate(errp, local_err); 49885a8a30dbSKevin Wolf return; 49895a8a30dbSKevin Wolf } 49900f15423cSAnthony Liguori } 49910f15423cSAnthony Liguori } 49920f15423cSAnthony Liguori 499307789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 499407789269SBenoît Canet { 499507789269SBenoît Canet BlockDriverState *bs; 499607789269SBenoît Canet 4997dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 4998ed78cda3SStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 4999ed78cda3SStefan Hajnoczi 5000ed78cda3SStefan Hajnoczi aio_context_acquire(aio_context); 500107789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 5002ed78cda3SStefan Hajnoczi aio_context_release(aio_context); 500307789269SBenoît Canet } 500407789269SBenoît Canet } 500507789269SBenoît Canet 500607f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 500707f07615SPaolo Bonzini { 500807f07615SPaolo Bonzini Coroutine *co; 500907f07615SPaolo Bonzini RwCo rwco = { 501007f07615SPaolo Bonzini .bs = bs, 501107f07615SPaolo Bonzini .ret = NOT_DONE, 501207f07615SPaolo Bonzini }; 501307f07615SPaolo Bonzini 501407f07615SPaolo Bonzini if (qemu_in_coroutine()) { 501507f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 501607f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 501707f07615SPaolo Bonzini } else { 50182572b37aSStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 50192572b37aSStefan Hajnoczi 502007f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 502107f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 502207f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 50232572b37aSStefan Hajnoczi aio_poll(aio_context, true); 502407f07615SPaolo Bonzini } 502507f07615SPaolo Bonzini } 502607f07615SPaolo Bonzini 502707f07615SPaolo Bonzini return rwco.ret; 502807f07615SPaolo Bonzini } 5029e7a8a783SKevin Wolf 5030775aa8b6SKevin Wolf typedef struct DiscardCo { 5031775aa8b6SKevin Wolf BlockDriverState *bs; 5032775aa8b6SKevin Wolf int64_t sector_num; 5033775aa8b6SKevin Wolf int nb_sectors; 5034775aa8b6SKevin Wolf int ret; 5035775aa8b6SKevin Wolf } DiscardCo; 50364265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 50374265d620SPaolo Bonzini { 5038775aa8b6SKevin Wolf DiscardCo *rwco = opaque; 50394265d620SPaolo Bonzini 50404265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 50414265d620SPaolo Bonzini } 50424265d620SPaolo Bonzini 50436f14da52SPeter Lieven /* if no limit is specified in the BlockLimits use a default 50446f14da52SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 50456f14da52SPeter Lieven */ 50466f14da52SPeter Lieven #define MAX_DISCARD_DEFAULT 32768 50476f14da52SPeter Lieven 50484265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 50494265d620SPaolo Bonzini int nb_sectors) 50504265d620SPaolo Bonzini { 5051d51e9fe5SPaolo Bonzini int max_discard; 5052d51e9fe5SPaolo Bonzini 50534265d620SPaolo Bonzini if (!bs->drv) { 50544265d620SPaolo Bonzini return -ENOMEDIUM; 50554265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 50564265d620SPaolo Bonzini return -EIO; 50574265d620SPaolo Bonzini } else if (bs->read_only) { 50584265d620SPaolo Bonzini return -EROFS; 5059df702c9bSPaolo Bonzini } 5060df702c9bSPaolo Bonzini 50618f0720ecSPaolo Bonzini bdrv_reset_dirty(bs, sector_num, nb_sectors); 5062df702c9bSPaolo Bonzini 50639e8f1835SPaolo Bonzini /* Do nothing if disabled. */ 50649e8f1835SPaolo Bonzini if (!(bs->open_flags & BDRV_O_UNMAP)) { 50659e8f1835SPaolo Bonzini return 0; 50669e8f1835SPaolo Bonzini } 50679e8f1835SPaolo Bonzini 5068d51e9fe5SPaolo Bonzini if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) { 5069d51e9fe5SPaolo Bonzini return 0; 5070d51e9fe5SPaolo Bonzini } 50716f14da52SPeter Lieven 5072d51e9fe5SPaolo Bonzini max_discard = bs->bl.max_discard ? bs->bl.max_discard : MAX_DISCARD_DEFAULT; 50736f14da52SPeter Lieven while (nb_sectors > 0) { 50746f14da52SPeter Lieven int ret; 50756f14da52SPeter Lieven int num = nb_sectors; 50766f14da52SPeter Lieven 50776f14da52SPeter Lieven /* align request */ 50786f14da52SPeter Lieven if (bs->bl.discard_alignment && 50796f14da52SPeter Lieven num >= bs->bl.discard_alignment && 50806f14da52SPeter Lieven sector_num % bs->bl.discard_alignment) { 50816f14da52SPeter Lieven if (num > bs->bl.discard_alignment) { 50826f14da52SPeter Lieven num = bs->bl.discard_alignment; 50836f14da52SPeter Lieven } 50846f14da52SPeter Lieven num -= sector_num % bs->bl.discard_alignment; 50856f14da52SPeter Lieven } 50866f14da52SPeter Lieven 50876f14da52SPeter Lieven /* limit request size */ 50886f14da52SPeter Lieven if (num > max_discard) { 50896f14da52SPeter Lieven num = max_discard; 50906f14da52SPeter Lieven } 50916f14da52SPeter Lieven 5092d51e9fe5SPaolo Bonzini if (bs->drv->bdrv_co_discard) { 50936f14da52SPeter Lieven ret = bs->drv->bdrv_co_discard(bs, sector_num, num); 5094d51e9fe5SPaolo Bonzini } else { 50954265d620SPaolo Bonzini BlockDriverAIOCB *acb; 50964265d620SPaolo Bonzini CoroutineIOCompletion co = { 50974265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 50984265d620SPaolo Bonzini }; 50994265d620SPaolo Bonzini 51004265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 51014265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 51024265d620SPaolo Bonzini if (acb == NULL) { 51034265d620SPaolo Bonzini return -EIO; 51044265d620SPaolo Bonzini } else { 51054265d620SPaolo Bonzini qemu_coroutine_yield(); 5106d51e9fe5SPaolo Bonzini ret = co.ret; 51074265d620SPaolo Bonzini } 5108d51e9fe5SPaolo Bonzini } 51097ce21016SPaolo Bonzini if (ret && ret != -ENOTSUP) { 5110d51e9fe5SPaolo Bonzini return ret; 5111d51e9fe5SPaolo Bonzini } 5112d51e9fe5SPaolo Bonzini 5113d51e9fe5SPaolo Bonzini sector_num += num; 5114d51e9fe5SPaolo Bonzini nb_sectors -= num; 5115d51e9fe5SPaolo Bonzini } 51164265d620SPaolo Bonzini return 0; 51174265d620SPaolo Bonzini } 51184265d620SPaolo Bonzini 51194265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 51204265d620SPaolo Bonzini { 51214265d620SPaolo Bonzini Coroutine *co; 5122775aa8b6SKevin Wolf DiscardCo rwco = { 51234265d620SPaolo Bonzini .bs = bs, 51244265d620SPaolo Bonzini .sector_num = sector_num, 51254265d620SPaolo Bonzini .nb_sectors = nb_sectors, 51264265d620SPaolo Bonzini .ret = NOT_DONE, 51274265d620SPaolo Bonzini }; 51284265d620SPaolo Bonzini 51294265d620SPaolo Bonzini if (qemu_in_coroutine()) { 51304265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 51314265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 51324265d620SPaolo Bonzini } else { 51332572b37aSStefan Hajnoczi AioContext *aio_context = bdrv_get_aio_context(bs); 51342572b37aSStefan Hajnoczi 51354265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 51364265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 51374265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 51382572b37aSStefan Hajnoczi aio_poll(aio_context, true); 51394265d620SPaolo Bonzini } 51404265d620SPaolo Bonzini } 51414265d620SPaolo Bonzini 51424265d620SPaolo Bonzini return rwco.ret; 51434265d620SPaolo Bonzini } 51444265d620SPaolo Bonzini 5145f9f05dc5SKevin Wolf /**************************************************************/ 514619cb3738Sbellard /* removable device support */ 514719cb3738Sbellard 514819cb3738Sbellard /** 514919cb3738Sbellard * Return TRUE if the media is present 515019cb3738Sbellard */ 515119cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 515219cb3738Sbellard { 515319cb3738Sbellard BlockDriver *drv = bs->drv; 5154a1aff5bfSMarkus Armbruster 515519cb3738Sbellard if (!drv) 515619cb3738Sbellard return 0; 515719cb3738Sbellard if (!drv->bdrv_is_inserted) 5158a1aff5bfSMarkus Armbruster return 1; 5159a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 516019cb3738Sbellard } 516119cb3738Sbellard 516219cb3738Sbellard /** 51638e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 51648e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 516519cb3738Sbellard */ 516619cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 516719cb3738Sbellard { 516819cb3738Sbellard BlockDriver *drv = bs->drv; 516919cb3738Sbellard 51708e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 51718e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 51728e49ca46SMarkus Armbruster } 51738e49ca46SMarkus Armbruster return -ENOTSUP; 517419cb3738Sbellard } 517519cb3738Sbellard 517619cb3738Sbellard /** 517719cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 517819cb3738Sbellard */ 5179f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 518019cb3738Sbellard { 518119cb3738Sbellard BlockDriver *drv = bs->drv; 518219cb3738Sbellard 5183822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 5184822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 518519cb3738Sbellard } 51866f382ed2SLuiz Capitulino 51876f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 5188a5ee7bd4SWenchao Xia qapi_event_send_device_tray_moved(bdrv_get_device_name(bs), 5189a5ee7bd4SWenchao Xia eject_flag, &error_abort); 51906f382ed2SLuiz Capitulino } 519119cb3738Sbellard } 519219cb3738Sbellard 519319cb3738Sbellard /** 519419cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 519519cb3738Sbellard * to eject it manually). 519619cb3738Sbellard */ 5197025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 519819cb3738Sbellard { 519919cb3738Sbellard BlockDriver *drv = bs->drv; 520019cb3738Sbellard 5201025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 5202b8c6d095SStefan Hajnoczi 5203025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 5204025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 520519cb3738Sbellard } 520619cb3738Sbellard } 5207985a03b0Sths 5208985a03b0Sths /* needed for generic scsi interface */ 5209985a03b0Sths 5210985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 5211985a03b0Sths { 5212985a03b0Sths BlockDriver *drv = bs->drv; 5213985a03b0Sths 5214985a03b0Sths if (drv && drv->bdrv_ioctl) 5215985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 5216985a03b0Sths return -ENOTSUP; 5217985a03b0Sths } 52187d780669Saliguori 5219221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 5220221f715dSaliguori unsigned long int req, void *buf, 52217d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 52227d780669Saliguori { 5223221f715dSaliguori BlockDriver *drv = bs->drv; 52247d780669Saliguori 5225221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 5226221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 5227221f715dSaliguori return NULL; 52287d780669Saliguori } 5229e268ca52Saliguori 52301b7fd729SPaolo Bonzini void bdrv_set_guest_block_size(BlockDriverState *bs, int align) 52317b6f9300SMarkus Armbruster { 52321b7fd729SPaolo Bonzini bs->guest_block_size = align; 52337b6f9300SMarkus Armbruster } 52347cd1e32aSlirans@il.ibm.com 5235e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 5236e268ca52Saliguori { 5237339064d5SKevin Wolf return qemu_memalign(bdrv_opt_mem_align(bs), size); 5238e268ca52Saliguori } 52397cd1e32aSlirans@il.ibm.com 5240c53b1c51SStefan Hajnoczi /* 5241c53b1c51SStefan Hajnoczi * Check if all memory in this vector is sector aligned. 5242c53b1c51SStefan Hajnoczi */ 5243c53b1c51SStefan Hajnoczi bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) 5244c53b1c51SStefan Hajnoczi { 5245c53b1c51SStefan Hajnoczi int i; 5246339064d5SKevin Wolf size_t alignment = bdrv_opt_mem_align(bs); 5247c53b1c51SStefan Hajnoczi 5248c53b1c51SStefan Hajnoczi for (i = 0; i < qiov->niov; i++) { 5249339064d5SKevin Wolf if ((uintptr_t) qiov->iov[i].iov_base % alignment) { 5250c53b1c51SStefan Hajnoczi return false; 5251c53b1c51SStefan Hajnoczi } 5252339064d5SKevin Wolf if (qiov->iov[i].iov_len % alignment) { 52531ff735bdSKevin Wolf return false; 52541ff735bdSKevin Wolf } 5255c53b1c51SStefan Hajnoczi } 5256c53b1c51SStefan Hajnoczi 5257c53b1c51SStefan Hajnoczi return true; 5258c53b1c51SStefan Hajnoczi } 5259c53b1c51SStefan Hajnoczi 5260b8afb520SFam Zheng BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity, 5261b8afb520SFam Zheng Error **errp) 52627cd1e32aSlirans@il.ibm.com { 52637cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 5264e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 5265a55eb92cSJan Kiszka 526650717e94SPaolo Bonzini assert((granularity & (granularity - 1)) == 0); 526750717e94SPaolo Bonzini 526850717e94SPaolo Bonzini granularity >>= BDRV_SECTOR_BITS; 5269e4654d2dSFam Zheng assert(granularity); 5270b8afb520SFam Zheng bitmap_size = bdrv_getlength(bs); 5271b8afb520SFam Zheng if (bitmap_size < 0) { 5272b8afb520SFam Zheng error_setg_errno(errp, -bitmap_size, "could not get length of device"); 5273b8afb520SFam Zheng errno = -bitmap_size; 5274b8afb520SFam Zheng return NULL; 5275b8afb520SFam Zheng } 5276b8afb520SFam Zheng bitmap_size >>= BDRV_SECTOR_BITS; 5277e4654d2dSFam Zheng bitmap = g_malloc0(sizeof(BdrvDirtyBitmap)); 5278e4654d2dSFam Zheng bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); 5279e4654d2dSFam Zheng QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); 5280e4654d2dSFam Zheng return bitmap; 5281e4654d2dSFam Zheng } 5282e4654d2dSFam Zheng 5283e4654d2dSFam Zheng void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 5284e4654d2dSFam Zheng { 5285e4654d2dSFam Zheng BdrvDirtyBitmap *bm, *next; 5286e4654d2dSFam Zheng QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { 5287e4654d2dSFam Zheng if (bm == bitmap) { 5288e4654d2dSFam Zheng QLIST_REMOVE(bitmap, list); 5289e4654d2dSFam Zheng hbitmap_free(bitmap->bitmap); 5290e4654d2dSFam Zheng g_free(bitmap); 5291e4654d2dSFam Zheng return; 52927cd1e32aSlirans@il.ibm.com } 52937cd1e32aSlirans@il.ibm.com } 52947cd1e32aSlirans@il.ibm.com } 52957cd1e32aSlirans@il.ibm.com 529621b56835SFam Zheng BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 529721b56835SFam Zheng { 529821b56835SFam Zheng BdrvDirtyBitmap *bm; 529921b56835SFam Zheng BlockDirtyInfoList *list = NULL; 530021b56835SFam Zheng BlockDirtyInfoList **plist = &list; 530121b56835SFam Zheng 530221b56835SFam Zheng QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 530321b56835SFam Zheng BlockDirtyInfo *info = g_malloc0(sizeof(BlockDirtyInfo)); 530421b56835SFam Zheng BlockDirtyInfoList *entry = g_malloc0(sizeof(BlockDirtyInfoList)); 530521b56835SFam Zheng info->count = bdrv_get_dirty_count(bs, bm); 530621b56835SFam Zheng info->granularity = 530721b56835SFam Zheng ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bm->bitmap)); 530821b56835SFam Zheng entry->value = info; 530921b56835SFam Zheng *plist = entry; 531021b56835SFam Zheng plist = &entry->next; 531121b56835SFam Zheng } 531221b56835SFam Zheng 531321b56835SFam Zheng return list; 531421b56835SFam Zheng } 531521b56835SFam Zheng 5316e4654d2dSFam Zheng int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector) 53177cd1e32aSlirans@il.ibm.com { 5318e4654d2dSFam Zheng if (bitmap) { 5319e4654d2dSFam Zheng return hbitmap_get(bitmap->bitmap, sector); 53207cd1e32aSlirans@il.ibm.com } else { 53217cd1e32aSlirans@il.ibm.com return 0; 53227cd1e32aSlirans@il.ibm.com } 53237cd1e32aSlirans@il.ibm.com } 53247cd1e32aSlirans@il.ibm.com 5325e4654d2dSFam Zheng void bdrv_dirty_iter_init(BlockDriverState *bs, 5326e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap, HBitmapIter *hbi) 53271755da16SPaolo Bonzini { 5328e4654d2dSFam Zheng hbitmap_iter_init(hbi, bitmap->bitmap, 0); 53291755da16SPaolo Bonzini } 53301755da16SPaolo Bonzini 53311755da16SPaolo Bonzini void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 53321755da16SPaolo Bonzini int nr_sectors) 53331755da16SPaolo Bonzini { 5334e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 5335e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 5336e4654d2dSFam Zheng hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 5337e4654d2dSFam Zheng } 53381755da16SPaolo Bonzini } 53391755da16SPaolo Bonzini 5340e4654d2dSFam Zheng void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors) 53417cd1e32aSlirans@il.ibm.com { 5342e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 5343e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 5344e4654d2dSFam Zheng hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); 5345e4654d2dSFam Zheng } 53467cd1e32aSlirans@il.ibm.com } 5347aaa0eb75SLiran Schour 5348e4654d2dSFam Zheng int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 5349aaa0eb75SLiran Schour { 5350e4654d2dSFam Zheng return hbitmap_count(bitmap->bitmap); 5351aaa0eb75SLiran Schour } 5352f88e1a42SJes Sorensen 53539fcb0251SFam Zheng /* Get a reference to bs */ 53549fcb0251SFam Zheng void bdrv_ref(BlockDriverState *bs) 53559fcb0251SFam Zheng { 53569fcb0251SFam Zheng bs->refcnt++; 53579fcb0251SFam Zheng } 53589fcb0251SFam Zheng 53599fcb0251SFam Zheng /* Release a previously grabbed reference to bs. 53609fcb0251SFam Zheng * If after releasing, reference count is zero, the BlockDriverState is 53619fcb0251SFam Zheng * deleted. */ 53629fcb0251SFam Zheng void bdrv_unref(BlockDriverState *bs) 53639fcb0251SFam Zheng { 53649fcb0251SFam Zheng assert(bs->refcnt > 0); 53659fcb0251SFam Zheng if (--bs->refcnt == 0) { 53669fcb0251SFam Zheng bdrv_delete(bs); 53679fcb0251SFam Zheng } 53689fcb0251SFam Zheng } 53699fcb0251SFam Zheng 5370fbe40ff7SFam Zheng struct BdrvOpBlocker { 5371fbe40ff7SFam Zheng Error *reason; 5372fbe40ff7SFam Zheng QLIST_ENTRY(BdrvOpBlocker) list; 5373fbe40ff7SFam Zheng }; 5374fbe40ff7SFam Zheng 5375fbe40ff7SFam Zheng bool bdrv_op_is_blocked(BlockDriverState *bs, BlockOpType op, Error **errp) 5376fbe40ff7SFam Zheng { 5377fbe40ff7SFam Zheng BdrvOpBlocker *blocker; 5378fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 5379fbe40ff7SFam Zheng if (!QLIST_EMPTY(&bs->op_blockers[op])) { 5380fbe40ff7SFam Zheng blocker = QLIST_FIRST(&bs->op_blockers[op]); 5381fbe40ff7SFam Zheng if (errp) { 5382fbe40ff7SFam Zheng error_setg(errp, "Device '%s' is busy: %s", 5383fbe40ff7SFam Zheng bs->device_name, error_get_pretty(blocker->reason)); 5384fbe40ff7SFam Zheng } 5385fbe40ff7SFam Zheng return true; 5386fbe40ff7SFam Zheng } 5387fbe40ff7SFam Zheng return false; 5388fbe40ff7SFam Zheng } 5389fbe40ff7SFam Zheng 5390fbe40ff7SFam Zheng void bdrv_op_block(BlockDriverState *bs, BlockOpType op, Error *reason) 5391fbe40ff7SFam Zheng { 5392fbe40ff7SFam Zheng BdrvOpBlocker *blocker; 5393fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 5394fbe40ff7SFam Zheng 5395fbe40ff7SFam Zheng blocker = g_malloc0(sizeof(BdrvOpBlocker)); 5396fbe40ff7SFam Zheng blocker->reason = reason; 5397fbe40ff7SFam Zheng QLIST_INSERT_HEAD(&bs->op_blockers[op], blocker, list); 5398fbe40ff7SFam Zheng } 5399fbe40ff7SFam Zheng 5400fbe40ff7SFam Zheng void bdrv_op_unblock(BlockDriverState *bs, BlockOpType op, Error *reason) 5401fbe40ff7SFam Zheng { 5402fbe40ff7SFam Zheng BdrvOpBlocker *blocker, *next; 5403fbe40ff7SFam Zheng assert((int) op >= 0 && op < BLOCK_OP_TYPE_MAX); 5404fbe40ff7SFam Zheng QLIST_FOREACH_SAFE(blocker, &bs->op_blockers[op], list, next) { 5405fbe40ff7SFam Zheng if (blocker->reason == reason) { 5406fbe40ff7SFam Zheng QLIST_REMOVE(blocker, list); 5407fbe40ff7SFam Zheng g_free(blocker); 5408fbe40ff7SFam Zheng } 5409fbe40ff7SFam Zheng } 5410fbe40ff7SFam Zheng } 5411fbe40ff7SFam Zheng 5412fbe40ff7SFam Zheng void bdrv_op_block_all(BlockDriverState *bs, Error *reason) 5413fbe40ff7SFam Zheng { 5414fbe40ff7SFam Zheng int i; 5415fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 5416fbe40ff7SFam Zheng bdrv_op_block(bs, i, reason); 5417fbe40ff7SFam Zheng } 5418fbe40ff7SFam Zheng } 5419fbe40ff7SFam Zheng 5420fbe40ff7SFam Zheng void bdrv_op_unblock_all(BlockDriverState *bs, Error *reason) 5421fbe40ff7SFam Zheng { 5422fbe40ff7SFam Zheng int i; 5423fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 5424fbe40ff7SFam Zheng bdrv_op_unblock(bs, i, reason); 5425fbe40ff7SFam Zheng } 5426fbe40ff7SFam Zheng } 5427fbe40ff7SFam Zheng 5428fbe40ff7SFam Zheng bool bdrv_op_blocker_is_empty(BlockDriverState *bs) 5429fbe40ff7SFam Zheng { 5430fbe40ff7SFam Zheng int i; 5431fbe40ff7SFam Zheng 5432fbe40ff7SFam Zheng for (i = 0; i < BLOCK_OP_TYPE_MAX; i++) { 5433fbe40ff7SFam Zheng if (!QLIST_EMPTY(&bs->op_blockers[i])) { 5434fbe40ff7SFam Zheng return false; 5435fbe40ff7SFam Zheng } 5436fbe40ff7SFam Zheng } 5437fbe40ff7SFam Zheng return true; 5438fbe40ff7SFam Zheng } 5439fbe40ff7SFam Zheng 544028a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 544128a7282aSLuiz Capitulino { 5442d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 544358e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 544428a7282aSLuiz Capitulino } 544528a7282aSLuiz Capitulino 544628a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 544728a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 544828a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 544928a7282aSLuiz Capitulino { 5450d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 545192aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 545292aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 545392aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 545428a7282aSLuiz Capitulino } 545528a7282aSLuiz Capitulino 545628a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 545728a7282aSLuiz Capitulino { 5458d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 545928a7282aSLuiz Capitulino } 546028a7282aSLuiz Capitulino 546128a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 546228a7282aSLuiz Capitulino { 546328a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 546458e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 54653bd293c3SPaolo Bonzini if (bs->job) { 54663bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 54673bd293c3SPaolo Bonzini } 546828a7282aSLuiz Capitulino } 546928a7282aSLuiz Capitulino } 547028a7282aSLuiz Capitulino 547128a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 547228a7282aSLuiz Capitulino { 54733e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 54743e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 547558e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 547658e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 547728a7282aSLuiz Capitulino } 547828a7282aSLuiz Capitulino } 547928a7282aSLuiz Capitulino 5480a597e79cSChristoph Hellwig void 5481a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 5482a597e79cSChristoph Hellwig enum BlockAcctType type) 5483a597e79cSChristoph Hellwig { 5484a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 5485a597e79cSChristoph Hellwig 5486a597e79cSChristoph Hellwig cookie->bytes = bytes; 5487c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 5488a597e79cSChristoph Hellwig cookie->type = type; 5489a597e79cSChristoph Hellwig } 5490a597e79cSChristoph Hellwig 5491a597e79cSChristoph Hellwig void 5492a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 5493a597e79cSChristoph Hellwig { 5494a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 5495a597e79cSChristoph Hellwig 5496a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 5497a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 5498c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 5499a597e79cSChristoph Hellwig } 5500a597e79cSChristoph Hellwig 5501d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 5502f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 5503f382d43aSMiroslav Rezanina char *options, uint64_t img_size, int flags, 5504f382d43aSMiroslav Rezanina Error **errp, bool quiet) 5505f88e1a42SJes Sorensen { 550683d0521aSChunyan Liu QemuOptsList *create_opts = NULL; 550783d0521aSChunyan Liu QemuOpts *opts = NULL; 550883d0521aSChunyan Liu const char *backing_fmt, *backing_file; 550983d0521aSChunyan Liu int64_t size; 5510f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 551196df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 5512cc84d90fSMax Reitz Error *local_err = NULL; 5513f88e1a42SJes Sorensen int ret = 0; 5514f88e1a42SJes Sorensen 5515f88e1a42SJes Sorensen /* Find driver and parse its options */ 5516f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 5517f88e1a42SJes Sorensen if (!drv) { 551871c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 5519d92ada22SLuiz Capitulino return; 5520f88e1a42SJes Sorensen } 5521f88e1a42SJes Sorensen 552298289620SKevin Wolf proto_drv = bdrv_find_protocol(filename, true); 5523f88e1a42SJes Sorensen if (!proto_drv) { 552471c79813SLuiz Capitulino error_setg(errp, "Unknown protocol '%s'", filename); 5525d92ada22SLuiz Capitulino return; 5526f88e1a42SJes Sorensen } 5527f88e1a42SJes Sorensen 5528c282e1fdSChunyan Liu create_opts = qemu_opts_append(create_opts, drv->create_opts); 5529c282e1fdSChunyan Liu create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); 5530f88e1a42SJes Sorensen 5531f88e1a42SJes Sorensen /* Create parameter list with default values */ 553283d0521aSChunyan Liu opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); 553383d0521aSChunyan Liu qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size); 5534f88e1a42SJes Sorensen 5535f88e1a42SJes Sorensen /* Parse -o options */ 5536f88e1a42SJes Sorensen if (options) { 553783d0521aSChunyan Liu if (qemu_opts_do_parse(opts, options, NULL) != 0) { 553883d0521aSChunyan Liu error_setg(errp, "Invalid options for file format '%s'", fmt); 5539f88e1a42SJes Sorensen goto out; 5540f88e1a42SJes Sorensen } 5541f88e1a42SJes Sorensen } 5542f88e1a42SJes Sorensen 5543f88e1a42SJes Sorensen if (base_filename) { 554483d0521aSChunyan Liu if (qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename)) { 554571c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 554671c79813SLuiz Capitulino fmt); 5547f88e1a42SJes Sorensen goto out; 5548f88e1a42SJes Sorensen } 5549f88e1a42SJes Sorensen } 5550f88e1a42SJes Sorensen 5551f88e1a42SJes Sorensen if (base_fmt) { 555283d0521aSChunyan Liu if (qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt)) { 555371c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 555471c79813SLuiz Capitulino "format '%s'", fmt); 5555f88e1a42SJes Sorensen goto out; 5556f88e1a42SJes Sorensen } 5557f88e1a42SJes Sorensen } 5558f88e1a42SJes Sorensen 555983d0521aSChunyan Liu backing_file = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); 556083d0521aSChunyan Liu if (backing_file) { 556183d0521aSChunyan Liu if (!strcmp(filename, backing_file)) { 556271c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 556371c79813SLuiz Capitulino "same filename as the backing file"); 5564792da93aSJes Sorensen goto out; 5565792da93aSJes Sorensen } 5566792da93aSJes Sorensen } 5567792da93aSJes Sorensen 556883d0521aSChunyan Liu backing_fmt = qemu_opt_get(opts, BLOCK_OPT_BACKING_FMT); 556983d0521aSChunyan Liu if (backing_fmt) { 557083d0521aSChunyan Liu backing_drv = bdrv_find_format(backing_fmt); 557196df67d1SStefan Hajnoczi if (!backing_drv) { 557271c79813SLuiz Capitulino error_setg(errp, "Unknown backing file format '%s'", 557383d0521aSChunyan Liu backing_fmt); 5574f88e1a42SJes Sorensen goto out; 5575f88e1a42SJes Sorensen } 5576f88e1a42SJes Sorensen } 5577f88e1a42SJes Sorensen 5578f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 5579f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 558083d0521aSChunyan Liu size = qemu_opt_get_size(opts, BLOCK_OPT_SIZE, 0); 558183d0521aSChunyan Liu if (size == -1) { 558283d0521aSChunyan Liu if (backing_file) { 558366f6b814SMax Reitz BlockDriverState *bs; 5584f88e1a42SJes Sorensen uint64_t size; 558563090dacSPaolo Bonzini int back_flags; 558663090dacSPaolo Bonzini 558763090dacSPaolo Bonzini /* backing files always opened read-only */ 558863090dacSPaolo Bonzini back_flags = 558963090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 5590f88e1a42SJes Sorensen 5591f67503e5SMax Reitz bs = NULL; 559283d0521aSChunyan Liu ret = bdrv_open(&bs, backing_file, NULL, NULL, back_flags, 5593cc84d90fSMax Reitz backing_drv, &local_err); 5594f88e1a42SJes Sorensen if (ret < 0) { 5595cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not open '%s': %s", 559683d0521aSChunyan Liu backing_file, 5597cc84d90fSMax Reitz error_get_pretty(local_err)); 5598cc84d90fSMax Reitz error_free(local_err); 5599cc84d90fSMax Reitz local_err = NULL; 5600f88e1a42SJes Sorensen goto out; 5601f88e1a42SJes Sorensen } 5602f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 5603f88e1a42SJes Sorensen size *= 512; 5604f88e1a42SJes Sorensen 560583d0521aSChunyan Liu qemu_opt_set_number(opts, BLOCK_OPT_SIZE, size); 560666f6b814SMax Reitz 560766f6b814SMax Reitz bdrv_unref(bs); 5608f88e1a42SJes Sorensen } else { 560971c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 5610f88e1a42SJes Sorensen goto out; 5611f88e1a42SJes Sorensen } 5612f88e1a42SJes Sorensen } 5613f88e1a42SJes Sorensen 5614f382d43aSMiroslav Rezanina if (!quiet) { 5615f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 561683d0521aSChunyan Liu qemu_opts_print(opts); 5617f88e1a42SJes Sorensen puts(""); 5618f382d43aSMiroslav Rezanina } 561983d0521aSChunyan Liu 5620c282e1fdSChunyan Liu ret = bdrv_create(drv, filename, opts, &local_err); 562183d0521aSChunyan Liu 5622cc84d90fSMax Reitz if (ret == -EFBIG) { 5623cc84d90fSMax Reitz /* This is generally a better message than whatever the driver would 5624cc84d90fSMax Reitz * deliver (especially because of the cluster_size_hint), since that 5625cc84d90fSMax Reitz * is most probably not much different from "image too large". */ 5626f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 562783d0521aSChunyan Liu if (qemu_opt_get_size(opts, BLOCK_OPT_CLUSTER_SIZE, 0)) { 5628f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 5629f3f4d2c0SKevin Wolf } 5630cc84d90fSMax Reitz error_setg(errp, "The image size is too large for file format '%s'" 5631cc84d90fSMax Reitz "%s", fmt, cluster_size_hint); 5632cc84d90fSMax Reitz error_free(local_err); 5633cc84d90fSMax Reitz local_err = NULL; 5634f88e1a42SJes Sorensen } 5635f88e1a42SJes Sorensen 5636f88e1a42SJes Sorensen out: 563783d0521aSChunyan Liu qemu_opts_del(opts); 563883d0521aSChunyan Liu qemu_opts_free(create_opts); 563984d18f06SMarkus Armbruster if (local_err) { 5640cc84d90fSMax Reitz error_propagate(errp, local_err); 5641cc84d90fSMax Reitz } 5642f88e1a42SJes Sorensen } 564385d126f3SStefan Hajnoczi 564485d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 564585d126f3SStefan Hajnoczi { 5646dcd04228SStefan Hajnoczi return bs->aio_context; 5647dcd04228SStefan Hajnoczi } 5648dcd04228SStefan Hajnoczi 5649dcd04228SStefan Hajnoczi void bdrv_detach_aio_context(BlockDriverState *bs) 5650dcd04228SStefan Hajnoczi { 5651dcd04228SStefan Hajnoczi if (!bs->drv) { 5652dcd04228SStefan Hajnoczi return; 5653dcd04228SStefan Hajnoczi } 5654dcd04228SStefan Hajnoczi 565513af91ebSStefan Hajnoczi if (bs->io_limits_enabled) { 565613af91ebSStefan Hajnoczi throttle_detach_aio_context(&bs->throttle_state); 565713af91ebSStefan Hajnoczi } 5658dcd04228SStefan Hajnoczi if (bs->drv->bdrv_detach_aio_context) { 5659dcd04228SStefan Hajnoczi bs->drv->bdrv_detach_aio_context(bs); 5660dcd04228SStefan Hajnoczi } 5661dcd04228SStefan Hajnoczi if (bs->file) { 5662dcd04228SStefan Hajnoczi bdrv_detach_aio_context(bs->file); 5663dcd04228SStefan Hajnoczi } 5664dcd04228SStefan Hajnoczi if (bs->backing_hd) { 5665dcd04228SStefan Hajnoczi bdrv_detach_aio_context(bs->backing_hd); 5666dcd04228SStefan Hajnoczi } 5667dcd04228SStefan Hajnoczi 5668dcd04228SStefan Hajnoczi bs->aio_context = NULL; 5669dcd04228SStefan Hajnoczi } 5670dcd04228SStefan Hajnoczi 5671dcd04228SStefan Hajnoczi void bdrv_attach_aio_context(BlockDriverState *bs, 5672dcd04228SStefan Hajnoczi AioContext *new_context) 5673dcd04228SStefan Hajnoczi { 5674dcd04228SStefan Hajnoczi if (!bs->drv) { 5675dcd04228SStefan Hajnoczi return; 5676dcd04228SStefan Hajnoczi } 5677dcd04228SStefan Hajnoczi 5678dcd04228SStefan Hajnoczi bs->aio_context = new_context; 5679dcd04228SStefan Hajnoczi 5680dcd04228SStefan Hajnoczi if (bs->backing_hd) { 5681dcd04228SStefan Hajnoczi bdrv_attach_aio_context(bs->backing_hd, new_context); 5682dcd04228SStefan Hajnoczi } 5683dcd04228SStefan Hajnoczi if (bs->file) { 5684dcd04228SStefan Hajnoczi bdrv_attach_aio_context(bs->file, new_context); 5685dcd04228SStefan Hajnoczi } 5686dcd04228SStefan Hajnoczi if (bs->drv->bdrv_attach_aio_context) { 5687dcd04228SStefan Hajnoczi bs->drv->bdrv_attach_aio_context(bs, new_context); 5688dcd04228SStefan Hajnoczi } 568913af91ebSStefan Hajnoczi if (bs->io_limits_enabled) { 569013af91ebSStefan Hajnoczi throttle_attach_aio_context(&bs->throttle_state, new_context); 569113af91ebSStefan Hajnoczi } 5692dcd04228SStefan Hajnoczi } 5693dcd04228SStefan Hajnoczi 5694dcd04228SStefan Hajnoczi void bdrv_set_aio_context(BlockDriverState *bs, AioContext *new_context) 5695dcd04228SStefan Hajnoczi { 5696dcd04228SStefan Hajnoczi bdrv_drain_all(); /* ensure there are no in-flight requests */ 5697dcd04228SStefan Hajnoczi 5698dcd04228SStefan Hajnoczi bdrv_detach_aio_context(bs); 5699dcd04228SStefan Hajnoczi 5700dcd04228SStefan Hajnoczi /* This function executes in the old AioContext so acquire the new one in 5701dcd04228SStefan Hajnoczi * case it runs in a different thread. 5702dcd04228SStefan Hajnoczi */ 5703dcd04228SStefan Hajnoczi aio_context_acquire(new_context); 5704dcd04228SStefan Hajnoczi bdrv_attach_aio_context(bs, new_context); 5705dcd04228SStefan Hajnoczi aio_context_release(new_context); 570685d126f3SStefan Hajnoczi } 5707d616b224SStefan Hajnoczi 5708d616b224SStefan Hajnoczi void bdrv_add_before_write_notifier(BlockDriverState *bs, 5709d616b224SStefan Hajnoczi NotifierWithReturn *notifier) 5710d616b224SStefan Hajnoczi { 5711d616b224SStefan Hajnoczi notifier_with_return_list_add(&bs->before_write_notifiers, notifier); 5712d616b224SStefan Hajnoczi } 57136f176b48SMax Reitz 5714c282e1fdSChunyan Liu int bdrv_amend_options(BlockDriverState *bs, QemuOpts *opts) 57156f176b48SMax Reitz { 5716c282e1fdSChunyan Liu if (!bs->drv->bdrv_amend_options) { 57176f176b48SMax Reitz return -ENOTSUP; 57186f176b48SMax Reitz } 5719c282e1fdSChunyan Liu return bs->drv->bdrv_amend_options(bs, opts); 57206f176b48SMax Reitz } 5721f6186f49SBenoît Canet 5722b5042a36SBenoît Canet /* This function will be called by the bdrv_recurse_is_first_non_filter method 5723b5042a36SBenoît Canet * of block filter and by bdrv_is_first_non_filter. 5724b5042a36SBenoît Canet * It is used to test if the given bs is the candidate or recurse more in the 5725b5042a36SBenoît Canet * node graph. 5726212a5a8fSBenoît Canet */ 5727212a5a8fSBenoît Canet bool bdrv_recurse_is_first_non_filter(BlockDriverState *bs, 5728212a5a8fSBenoît Canet BlockDriverState *candidate) 5729f6186f49SBenoît Canet { 5730b5042a36SBenoît Canet /* return false if basic checks fails */ 5731b5042a36SBenoît Canet if (!bs || !bs->drv) { 5732b5042a36SBenoît Canet return false; 5733b5042a36SBenoît Canet } 5734b5042a36SBenoît Canet 5735b5042a36SBenoît Canet /* the code reached a non block filter driver -> check if the bs is 5736b5042a36SBenoît Canet * the same as the candidate. It's the recursion termination condition. 5737b5042a36SBenoît Canet */ 5738b5042a36SBenoît Canet if (!bs->drv->is_filter) { 5739b5042a36SBenoît Canet return bs == candidate; 5740b5042a36SBenoît Canet } 5741b5042a36SBenoît Canet /* Down this path the driver is a block filter driver */ 5742b5042a36SBenoît Canet 5743b5042a36SBenoît Canet /* If the block filter recursion method is defined use it to recurse down 5744b5042a36SBenoît Canet * the node graph. 5745b5042a36SBenoît Canet */ 5746b5042a36SBenoît Canet if (bs->drv->bdrv_recurse_is_first_non_filter) { 5747212a5a8fSBenoît Canet return bs->drv->bdrv_recurse_is_first_non_filter(bs, candidate); 5748212a5a8fSBenoît Canet } 5749212a5a8fSBenoît Canet 5750b5042a36SBenoît Canet /* the driver is a block filter but don't allow to recurse -> return false 5751b5042a36SBenoît Canet */ 5752b5042a36SBenoît Canet return false; 5753212a5a8fSBenoît Canet } 5754212a5a8fSBenoît Canet 5755212a5a8fSBenoît Canet /* This function checks if the candidate is the first non filter bs down it's 5756212a5a8fSBenoît Canet * bs chain. Since we don't have pointers to parents it explore all bs chains 5757212a5a8fSBenoît Canet * from the top. Some filters can choose not to pass down the recursion. 5758212a5a8fSBenoît Canet */ 5759212a5a8fSBenoît Canet bool bdrv_is_first_non_filter(BlockDriverState *candidate) 5760212a5a8fSBenoît Canet { 5761212a5a8fSBenoît Canet BlockDriverState *bs; 5762212a5a8fSBenoît Canet 5763212a5a8fSBenoît Canet /* walk down the bs forest recursively */ 5764212a5a8fSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 5765212a5a8fSBenoît Canet bool perm; 5766212a5a8fSBenoît Canet 5767b5042a36SBenoît Canet /* try to recurse in this top level bs */ 5768e6dc8a1fSKevin Wolf perm = bdrv_recurse_is_first_non_filter(bs, candidate); 5769212a5a8fSBenoît Canet 5770212a5a8fSBenoît Canet /* candidate is the first non filter */ 5771212a5a8fSBenoît Canet if (perm) { 5772212a5a8fSBenoît Canet return true; 5773212a5a8fSBenoît Canet } 5774212a5a8fSBenoît Canet } 5775212a5a8fSBenoît Canet 5776212a5a8fSBenoît Canet return false; 5777f6186f49SBenoît Canet } 5778