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" 2783c9089eSPaolo Bonzini #include "monitor/monitor.h" 28737e150eSPaolo Bonzini #include "block/block_int.h" 29737e150eSPaolo Bonzini #include "block/blockjob.h" 301de7afc9SPaolo Bonzini #include "qemu/module.h" 317b1b5d19SPaolo Bonzini #include "qapi/qmp/qjson.h" 329c17d615SPaolo Bonzini #include "sysemu/sysemu.h" 331de7afc9SPaolo Bonzini #include "qemu/notify.h" 34737e150eSPaolo Bonzini #include "block/coroutine.h" 35b2023818SLuiz Capitulino #include "qmp-commands.h" 361de7afc9SPaolo Bonzini #include "qemu/timer.h" 37fc01f7e7Sbellard 3871e72a19SJuan Quintela #ifdef CONFIG_BSD 397674e7bfSbellard #include <sys/types.h> 407674e7bfSbellard #include <sys/stat.h> 417674e7bfSbellard #include <sys/ioctl.h> 4272cf2d4fSBlue Swirl #include <sys/queue.h> 43c5e97233Sblueswir1 #ifndef __DragonFly__ 447674e7bfSbellard #include <sys/disk.h> 457674e7bfSbellard #endif 46c5e97233Sblueswir1 #endif 477674e7bfSbellard 4849dc768dSaliguori #ifdef _WIN32 4949dc768dSaliguori #include <windows.h> 5049dc768dSaliguori #endif 5149dc768dSaliguori 52e4654d2dSFam Zheng struct BdrvDirtyBitmap { 53e4654d2dSFam Zheng HBitmap *bitmap; 54e4654d2dSFam Zheng QLIST_ENTRY(BdrvDirtyBitmap) list; 55e4654d2dSFam Zheng }; 56e4654d2dSFam Zheng 571c9805a3SStefan Hajnoczi #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 581c9805a3SStefan Hajnoczi 597d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); 60f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 61f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 62c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 63f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 64f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 65ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 66f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 67f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 68f9f05dc5SKevin Wolf QEMUIOVector *iov); 69f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 70f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 71f9f05dc5SKevin Wolf QEMUIOVector *iov); 72c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 73470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 74470c0504SStefan Hajnoczi BdrvRequestFlags flags); 751c9805a3SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 76f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 77f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags); 78b2a61371SStefan Hajnoczi static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 79b2a61371SStefan Hajnoczi int64_t sector_num, 80b2a61371SStefan Hajnoczi QEMUIOVector *qiov, 81b2a61371SStefan Hajnoczi int nb_sectors, 82d20d9b7cSPaolo Bonzini BdrvRequestFlags flags, 83b2a61371SStefan Hajnoczi BlockDriverCompletionFunc *cb, 84b2a61371SStefan Hajnoczi void *opaque, 858c5873d6SStefan Hajnoczi bool is_write); 86b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque); 87621f0589SKevin Wolf static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 88aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags); 89ec530c81Sbellard 901b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 911b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 927ee930d0Sblueswir1 938a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 948a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 95ea2384d3Sbellard 96eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 97eb852011SMarkus Armbruster static int use_bdrv_whitelist; 98eb852011SMarkus Armbruster 999e0b22f4SStefan Hajnoczi #ifdef _WIN32 1009e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 1019e0b22f4SStefan Hajnoczi { 1029e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 1039e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 1049e0b22f4SStefan Hajnoczi filename[1] == ':'); 1059e0b22f4SStefan Hajnoczi } 1069e0b22f4SStefan Hajnoczi 1079e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 1089e0b22f4SStefan Hajnoczi { 1099e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 1109e0b22f4SStefan Hajnoczi filename[2] == '\0') 1119e0b22f4SStefan Hajnoczi return 1; 1129e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1139e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1149e0b22f4SStefan Hajnoczi return 1; 1159e0b22f4SStefan Hajnoczi return 0; 1169e0b22f4SStefan Hajnoczi } 1179e0b22f4SStefan Hajnoczi #endif 1189e0b22f4SStefan Hajnoczi 1190563e191SZhi Yong Wu /* throttling disk I/O limits */ 120cc0681c4SBenoît Canet void bdrv_set_io_limits(BlockDriverState *bs, 121cc0681c4SBenoît Canet ThrottleConfig *cfg) 122cc0681c4SBenoît Canet { 123cc0681c4SBenoît Canet int i; 124cc0681c4SBenoît Canet 125cc0681c4SBenoît Canet throttle_config(&bs->throttle_state, cfg); 126cc0681c4SBenoît Canet 127cc0681c4SBenoît Canet for (i = 0; i < 2; i++) { 128cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[i]); 129cc0681c4SBenoît Canet } 130cc0681c4SBenoît Canet } 131cc0681c4SBenoît Canet 132cc0681c4SBenoît Canet /* this function drain all the throttled IOs */ 133cc0681c4SBenoît Canet static bool bdrv_start_throttled_reqs(BlockDriverState *bs) 134cc0681c4SBenoît Canet { 135cc0681c4SBenoît Canet bool drained = false; 136cc0681c4SBenoît Canet bool enabled = bs->io_limits_enabled; 137cc0681c4SBenoît Canet int i; 138cc0681c4SBenoît Canet 139cc0681c4SBenoît Canet bs->io_limits_enabled = false; 140cc0681c4SBenoît Canet 141cc0681c4SBenoît Canet for (i = 0; i < 2; i++) { 142cc0681c4SBenoît Canet while (qemu_co_enter_next(&bs->throttled_reqs[i])) { 143cc0681c4SBenoît Canet drained = true; 144cc0681c4SBenoît Canet } 145cc0681c4SBenoît Canet } 146cc0681c4SBenoît Canet 147cc0681c4SBenoît Canet bs->io_limits_enabled = enabled; 148cc0681c4SBenoît Canet 149cc0681c4SBenoît Canet return drained; 150cc0681c4SBenoît Canet } 151cc0681c4SBenoît Canet 15298f90dbaSZhi Yong Wu void bdrv_io_limits_disable(BlockDriverState *bs) 15398f90dbaSZhi Yong Wu { 15498f90dbaSZhi Yong Wu bs->io_limits_enabled = false; 15598f90dbaSZhi Yong Wu 156cc0681c4SBenoît Canet bdrv_start_throttled_reqs(bs); 15798f90dbaSZhi Yong Wu 158cc0681c4SBenoît Canet throttle_destroy(&bs->throttle_state); 15998f90dbaSZhi Yong Wu } 16098f90dbaSZhi Yong Wu 161cc0681c4SBenoît Canet static void bdrv_throttle_read_timer_cb(void *opaque) 1620563e191SZhi Yong Wu { 1630563e191SZhi Yong Wu BlockDriverState *bs = opaque; 164cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[0]); 1650563e191SZhi Yong Wu } 1660563e191SZhi Yong Wu 167cc0681c4SBenoît Canet static void bdrv_throttle_write_timer_cb(void *opaque) 168cc0681c4SBenoît Canet { 169cc0681c4SBenoît Canet BlockDriverState *bs = opaque; 170cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[1]); 171cc0681c4SBenoît Canet } 172cc0681c4SBenoît Canet 173cc0681c4SBenoît Canet /* should be called before bdrv_set_io_limits if a limit is set */ 1740563e191SZhi Yong Wu void bdrv_io_limits_enable(BlockDriverState *bs) 1750563e191SZhi Yong Wu { 176cc0681c4SBenoît Canet assert(!bs->io_limits_enabled); 177cc0681c4SBenoît Canet throttle_init(&bs->throttle_state, 178cc0681c4SBenoît Canet QEMU_CLOCK_VIRTUAL, 179cc0681c4SBenoît Canet bdrv_throttle_read_timer_cb, 180cc0681c4SBenoît Canet bdrv_throttle_write_timer_cb, 181cc0681c4SBenoît Canet bs); 1820563e191SZhi Yong Wu bs->io_limits_enabled = true; 1830563e191SZhi Yong Wu } 1840563e191SZhi Yong Wu 185cc0681c4SBenoît Canet /* This function makes an IO wait if needed 186cc0681c4SBenoît Canet * 187cc0681c4SBenoît Canet * @nb_sectors: the number of sectors of the IO 188cc0681c4SBenoît Canet * @is_write: is the IO a write 18998f90dbaSZhi Yong Wu */ 190cc0681c4SBenoît Canet static void bdrv_io_limits_intercept(BlockDriverState *bs, 191cc0681c4SBenoît Canet int nb_sectors, 192cc0681c4SBenoît Canet bool is_write) 193cc0681c4SBenoît Canet { 194cc0681c4SBenoît Canet /* does this io must wait */ 195cc0681c4SBenoît Canet bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write); 19698f90dbaSZhi Yong Wu 197cc0681c4SBenoît Canet /* if must wait or any request of this type throttled queue the IO */ 198cc0681c4SBenoît Canet if (must_wait || 199cc0681c4SBenoît Canet !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) { 200cc0681c4SBenoît Canet qemu_co_queue_wait(&bs->throttled_reqs[is_write]); 20198f90dbaSZhi Yong Wu } 20298f90dbaSZhi Yong Wu 203cc0681c4SBenoît Canet /* the IO will be executed, do the accounting */ 204cc0681c4SBenoît Canet throttle_account(&bs->throttle_state, 205cc0681c4SBenoît Canet is_write, 206cc0681c4SBenoît Canet nb_sectors * BDRV_SECTOR_SIZE); 207cc0681c4SBenoît Canet 208cc0681c4SBenoît Canet /* if the next request must wait -> do nothing */ 209cc0681c4SBenoît Canet if (throttle_schedule_timer(&bs->throttle_state, is_write)) { 210cc0681c4SBenoît Canet return; 211cc0681c4SBenoît Canet } 212cc0681c4SBenoît Canet 213cc0681c4SBenoît Canet /* else queue next request for execution */ 214cc0681c4SBenoît Canet qemu_co_queue_next(&bs->throttled_reqs[is_write]); 21598f90dbaSZhi Yong Wu } 21698f90dbaSZhi Yong Wu 2179e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 2189e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 2199e0b22f4SStefan Hajnoczi { 220947995c0SPaolo Bonzini const char *p; 221947995c0SPaolo Bonzini 2229e0b22f4SStefan Hajnoczi #ifdef _WIN32 2239e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2249e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2259e0b22f4SStefan Hajnoczi return 0; 2269e0b22f4SStefan Hajnoczi } 227947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 228947995c0SPaolo Bonzini #else 229947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2309e0b22f4SStefan Hajnoczi #endif 2319e0b22f4SStefan Hajnoczi 232947995c0SPaolo Bonzini return *p == ':'; 2339e0b22f4SStefan Hajnoczi } 2349e0b22f4SStefan Hajnoczi 23583f64091Sbellard int path_is_absolute(const char *path) 23683f64091Sbellard { 23721664424Sbellard #ifdef _WIN32 23821664424Sbellard /* specific case for names like: "\\.\d:" */ 239f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 24021664424Sbellard return 1; 241f53f4da9SPaolo Bonzini } 242f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 2433b9f94e1Sbellard #else 244f53f4da9SPaolo Bonzini return (*path == '/'); 2453b9f94e1Sbellard #endif 24683f64091Sbellard } 24783f64091Sbellard 24883f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 24983f64091Sbellard path to it by considering it is relative to base_path. URL are 25083f64091Sbellard supported. */ 25183f64091Sbellard void path_combine(char *dest, int dest_size, 25283f64091Sbellard const char *base_path, 25383f64091Sbellard const char *filename) 25483f64091Sbellard { 25583f64091Sbellard const char *p, *p1; 25683f64091Sbellard int len; 25783f64091Sbellard 25883f64091Sbellard if (dest_size <= 0) 25983f64091Sbellard return; 26083f64091Sbellard if (path_is_absolute(filename)) { 26183f64091Sbellard pstrcpy(dest, dest_size, filename); 26283f64091Sbellard } else { 26383f64091Sbellard p = strchr(base_path, ':'); 26483f64091Sbellard if (p) 26583f64091Sbellard p++; 26683f64091Sbellard else 26783f64091Sbellard p = base_path; 2683b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2693b9f94e1Sbellard #ifdef _WIN32 2703b9f94e1Sbellard { 2713b9f94e1Sbellard const char *p2; 2723b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2733b9f94e1Sbellard if (!p1 || p2 > p1) 2743b9f94e1Sbellard p1 = p2; 2753b9f94e1Sbellard } 2763b9f94e1Sbellard #endif 27783f64091Sbellard if (p1) 27883f64091Sbellard p1++; 27983f64091Sbellard else 28083f64091Sbellard p1 = base_path; 28183f64091Sbellard if (p1 > p) 28283f64091Sbellard p = p1; 28383f64091Sbellard len = p - base_path; 28483f64091Sbellard if (len > dest_size - 1) 28583f64091Sbellard len = dest_size - 1; 28683f64091Sbellard memcpy(dest, base_path, len); 28783f64091Sbellard dest[len] = '\0'; 28883f64091Sbellard pstrcat(dest, dest_size, filename); 28983f64091Sbellard } 29083f64091Sbellard } 29183f64091Sbellard 292dc5a1371SPaolo Bonzini void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) 293dc5a1371SPaolo Bonzini { 294dc5a1371SPaolo Bonzini if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { 295dc5a1371SPaolo Bonzini pstrcpy(dest, sz, bs->backing_file); 296dc5a1371SPaolo Bonzini } else { 297dc5a1371SPaolo Bonzini path_combine(dest, sz, bs->filename, bs->backing_file); 298dc5a1371SPaolo Bonzini } 299dc5a1371SPaolo Bonzini } 300dc5a1371SPaolo Bonzini 3015efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 302ea2384d3Sbellard { 3038c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 3048c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 305f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 306f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 307f9f05dc5SKevin Wolf 308f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 309f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 310f8c35c1dSStefan Hajnoczi */ 311f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 31283f64091Sbellard /* add AIO emulation layer */ 313f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 314f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 31583f64091Sbellard } 316f9f05dc5SKevin Wolf } 317b2e12bc6SChristoph Hellwig 3188a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 319ea2384d3Sbellard } 320b338082bSbellard 321b338082bSbellard /* create a new block device (by default it is empty) */ 322b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 323fc01f7e7Sbellard { 3241b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 325b338082bSbellard 3267267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 327e4654d2dSFam Zheng QLIST_INIT(&bs->dirty_bitmaps); 328b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 329ea2384d3Sbellard if (device_name[0] != '\0') { 3301b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 331ea2384d3Sbellard } 33228a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 333d7d512f6SPaolo Bonzini notifier_list_init(&bs->close_notifiers); 334d616b224SStefan Hajnoczi notifier_with_return_list_init(&bs->before_write_notifiers); 335cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[0]); 336cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[1]); 3379fcb0251SFam Zheng bs->refcnt = 1; 338d7d512f6SPaolo Bonzini 339b338082bSbellard return bs; 340b338082bSbellard } 341b338082bSbellard 342d7d512f6SPaolo Bonzini void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) 343d7d512f6SPaolo Bonzini { 344d7d512f6SPaolo Bonzini notifier_list_add(&bs->close_notifiers, notify); 345d7d512f6SPaolo Bonzini } 346d7d512f6SPaolo Bonzini 347ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 348ea2384d3Sbellard { 349ea2384d3Sbellard BlockDriver *drv1; 3508a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3518a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 352ea2384d3Sbellard return drv1; 353ea2384d3Sbellard } 3548a22f02aSStefan Hajnoczi } 355ea2384d3Sbellard return NULL; 356ea2384d3Sbellard } 357ea2384d3Sbellard 358b64ec4e4SFam Zheng static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) 359eb852011SMarkus Armbruster { 360b64ec4e4SFam Zheng static const char *whitelist_rw[] = { 361b64ec4e4SFam Zheng CONFIG_BDRV_RW_WHITELIST 362b64ec4e4SFam Zheng }; 363b64ec4e4SFam Zheng static const char *whitelist_ro[] = { 364b64ec4e4SFam Zheng CONFIG_BDRV_RO_WHITELIST 365eb852011SMarkus Armbruster }; 366eb852011SMarkus Armbruster const char **p; 367eb852011SMarkus Armbruster 368b64ec4e4SFam Zheng if (!whitelist_rw[0] && !whitelist_ro[0]) { 369eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 370b64ec4e4SFam Zheng } 371eb852011SMarkus Armbruster 372b64ec4e4SFam Zheng for (p = whitelist_rw; *p; p++) { 373eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 374eb852011SMarkus Armbruster return 1; 375eb852011SMarkus Armbruster } 376eb852011SMarkus Armbruster } 377b64ec4e4SFam Zheng if (read_only) { 378b64ec4e4SFam Zheng for (p = whitelist_ro; *p; p++) { 379b64ec4e4SFam Zheng if (!strcmp(drv->format_name, *p)) { 380b64ec4e4SFam Zheng return 1; 381b64ec4e4SFam Zheng } 382b64ec4e4SFam Zheng } 383b64ec4e4SFam Zheng } 384eb852011SMarkus Armbruster return 0; 385eb852011SMarkus Armbruster } 386eb852011SMarkus Armbruster 387b64ec4e4SFam Zheng BlockDriver *bdrv_find_whitelisted_format(const char *format_name, 388b64ec4e4SFam Zheng bool read_only) 389eb852011SMarkus Armbruster { 390eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 391b64ec4e4SFam Zheng return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL; 392eb852011SMarkus Armbruster } 393eb852011SMarkus Armbruster 3945b7e1542SZhi Yong Wu typedef struct CreateCo { 3955b7e1542SZhi Yong Wu BlockDriver *drv; 3965b7e1542SZhi Yong Wu char *filename; 3975b7e1542SZhi Yong Wu QEMUOptionParameter *options; 3985b7e1542SZhi Yong Wu int ret; 399cc84d90fSMax Reitz Error *err; 4005b7e1542SZhi Yong Wu } CreateCo; 4015b7e1542SZhi Yong Wu 4025b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 4035b7e1542SZhi Yong Wu { 404cc84d90fSMax Reitz Error *local_err = NULL; 405cc84d90fSMax Reitz int ret; 406cc84d90fSMax Reitz 4075b7e1542SZhi Yong Wu CreateCo *cco = opaque; 4085b7e1542SZhi Yong Wu assert(cco->drv); 4095b7e1542SZhi Yong Wu 410cc84d90fSMax Reitz ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err); 411cc84d90fSMax Reitz if (error_is_set(&local_err)) { 412cc84d90fSMax Reitz error_propagate(&cco->err, local_err); 413cc84d90fSMax Reitz } 414cc84d90fSMax Reitz cco->ret = ret; 4155b7e1542SZhi Yong Wu } 4165b7e1542SZhi Yong Wu 4170e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 418cc84d90fSMax Reitz QEMUOptionParameter *options, Error **errp) 419ea2384d3Sbellard { 4205b7e1542SZhi Yong Wu int ret; 4210e7e1989SKevin Wolf 4225b7e1542SZhi Yong Wu Coroutine *co; 4235b7e1542SZhi Yong Wu CreateCo cco = { 4245b7e1542SZhi Yong Wu .drv = drv, 4255b7e1542SZhi Yong Wu .filename = g_strdup(filename), 4265b7e1542SZhi Yong Wu .options = options, 4275b7e1542SZhi Yong Wu .ret = NOT_DONE, 428cc84d90fSMax Reitz .err = NULL, 4295b7e1542SZhi Yong Wu }; 4305b7e1542SZhi Yong Wu 4315b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 432cc84d90fSMax Reitz error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); 43380168bffSLuiz Capitulino ret = -ENOTSUP; 43480168bffSLuiz Capitulino goto out; 4355b7e1542SZhi Yong Wu } 4365b7e1542SZhi Yong Wu 4375b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 4385b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 4395b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 4405b7e1542SZhi Yong Wu } else { 4415b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 4425b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 4435b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 4445b7e1542SZhi Yong Wu qemu_aio_wait(); 4455b7e1542SZhi Yong Wu } 4465b7e1542SZhi Yong Wu } 4475b7e1542SZhi Yong Wu 4485b7e1542SZhi Yong Wu ret = cco.ret; 449cc84d90fSMax Reitz if (ret < 0) { 450cc84d90fSMax Reitz if (error_is_set(&cco.err)) { 451cc84d90fSMax Reitz error_propagate(errp, cco.err); 452cc84d90fSMax Reitz } else { 453cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not create image"); 454cc84d90fSMax Reitz } 455cc84d90fSMax Reitz } 4565b7e1542SZhi Yong Wu 45780168bffSLuiz Capitulino out: 45880168bffSLuiz Capitulino g_free(cco.filename); 4595b7e1542SZhi Yong Wu return ret; 460ea2384d3Sbellard } 461ea2384d3Sbellard 462cc84d90fSMax Reitz int bdrv_create_file(const char* filename, QEMUOptionParameter *options, 463cc84d90fSMax Reitz Error **errp) 46484a12e66SChristoph Hellwig { 46584a12e66SChristoph Hellwig BlockDriver *drv; 466cc84d90fSMax Reitz Error *local_err = NULL; 467cc84d90fSMax Reitz int ret; 46884a12e66SChristoph Hellwig 46998289620SKevin Wolf drv = bdrv_find_protocol(filename, true); 47084a12e66SChristoph Hellwig if (drv == NULL) { 471cc84d90fSMax Reitz error_setg(errp, "Could not find protocol for file '%s'", filename); 47216905d71SStefan Hajnoczi return -ENOENT; 47384a12e66SChristoph Hellwig } 47484a12e66SChristoph Hellwig 475cc84d90fSMax Reitz ret = bdrv_create(drv, filename, options, &local_err); 476cc84d90fSMax Reitz if (error_is_set(&local_err)) { 477cc84d90fSMax Reitz error_propagate(errp, local_err); 478cc84d90fSMax Reitz } 479cc84d90fSMax Reitz return ret; 48084a12e66SChristoph Hellwig } 48184a12e66SChristoph Hellwig 482eba25057SJim Meyering /* 483eba25057SJim Meyering * Create a uniquely-named empty temporary file. 484eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 485eba25057SJim Meyering */ 486eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 487eba25057SJim Meyering { 488d5249393Sbellard #ifdef _WIN32 4893b9f94e1Sbellard char temp_dir[MAX_PATH]; 490eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 491eba25057SJim Meyering have length MAX_PATH or greater. */ 492eba25057SJim Meyering assert(size >= MAX_PATH); 493eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 494eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 495eba25057SJim Meyering ? 0 : -GetLastError()); 496d5249393Sbellard #else 497ea2384d3Sbellard int fd; 4987ccfb2ebSblueswir1 const char *tmpdir; 4990badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 5000badc1eeSaurel32 if (!tmpdir) 5010badc1eeSaurel32 tmpdir = "/tmp"; 502eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 503eba25057SJim Meyering return -EOVERFLOW; 504ea2384d3Sbellard } 505eba25057SJim Meyering fd = mkstemp(filename); 506fe235a06SDunrong Huang if (fd < 0) { 507fe235a06SDunrong Huang return -errno; 508fe235a06SDunrong Huang } 509fe235a06SDunrong Huang if (close(fd) != 0) { 510fe235a06SDunrong Huang unlink(filename); 511eba25057SJim Meyering return -errno; 512eba25057SJim Meyering } 513eba25057SJim Meyering return 0; 514d5249393Sbellard #endif 515eba25057SJim Meyering } 516ea2384d3Sbellard 517f3a5d3f8SChristoph Hellwig /* 518f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 519f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 520f3a5d3f8SChristoph Hellwig */ 521f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 522f3a5d3f8SChristoph Hellwig { 523508c7cb3SChristoph Hellwig int score_max = 0, score; 524508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 525f3a5d3f8SChristoph Hellwig 5268a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 527508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 528508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 529508c7cb3SChristoph Hellwig if (score > score_max) { 530508c7cb3SChristoph Hellwig score_max = score; 531508c7cb3SChristoph Hellwig drv = d; 532f3a5d3f8SChristoph Hellwig } 533508c7cb3SChristoph Hellwig } 534f3a5d3f8SChristoph Hellwig } 535f3a5d3f8SChristoph Hellwig 536508c7cb3SChristoph Hellwig return drv; 537f3a5d3f8SChristoph Hellwig } 538f3a5d3f8SChristoph Hellwig 53998289620SKevin Wolf BlockDriver *bdrv_find_protocol(const char *filename, 54098289620SKevin Wolf bool allow_protocol_prefix) 54184a12e66SChristoph Hellwig { 54284a12e66SChristoph Hellwig BlockDriver *drv1; 54384a12e66SChristoph Hellwig char protocol[128]; 54484a12e66SChristoph Hellwig int len; 54584a12e66SChristoph Hellwig const char *p; 54684a12e66SChristoph Hellwig 54766f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 54866f82ceeSKevin Wolf 54939508e7aSChristoph Hellwig /* 55039508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 55139508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 55239508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 55339508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 55439508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 55539508e7aSChristoph Hellwig */ 55684a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 55739508e7aSChristoph Hellwig if (drv1) { 55884a12e66SChristoph Hellwig return drv1; 55984a12e66SChristoph Hellwig } 56039508e7aSChristoph Hellwig 56198289620SKevin Wolf if (!path_has_protocol(filename) || !allow_protocol_prefix) { 56239508e7aSChristoph Hellwig return bdrv_find_format("file"); 56339508e7aSChristoph Hellwig } 56498289620SKevin Wolf 5659e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 5669e0b22f4SStefan Hajnoczi assert(p != NULL); 56784a12e66SChristoph Hellwig len = p - filename; 56884a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 56984a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 57084a12e66SChristoph Hellwig memcpy(protocol, filename, len); 57184a12e66SChristoph Hellwig protocol[len] = '\0'; 57284a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 57384a12e66SChristoph Hellwig if (drv1->protocol_name && 57484a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 57584a12e66SChristoph Hellwig return drv1; 57684a12e66SChristoph Hellwig } 57784a12e66SChristoph Hellwig } 57884a12e66SChristoph Hellwig return NULL; 57984a12e66SChristoph Hellwig } 58084a12e66SChristoph Hellwig 581f500a6d3SKevin Wolf static int find_image_format(BlockDriverState *bs, const char *filename, 58234b5d2c6SMax Reitz BlockDriver **pdrv, Error **errp) 583ea2384d3Sbellard { 584f500a6d3SKevin Wolf int score, score_max; 585ea2384d3Sbellard BlockDriver *drv1, *drv; 58683f64091Sbellard uint8_t buf[2048]; 587f500a6d3SKevin Wolf int ret = 0; 588f8ea0b00SNicholas Bellinger 58908a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 5908e895599SPaolo Bonzini if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { 591c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 592c98ac35dSStefan Weil if (!drv) { 59334b5d2c6SMax Reitz error_setg(errp, "Could not find raw image format"); 594c98ac35dSStefan Weil ret = -ENOENT; 595c98ac35dSStefan Weil } 596c98ac35dSStefan Weil *pdrv = drv; 597c98ac35dSStefan Weil return ret; 5981a396859SNicholas A. Bellinger } 599f8ea0b00SNicholas Bellinger 60083f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 601ea2384d3Sbellard if (ret < 0) { 60234b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not read image for determining its " 60334b5d2c6SMax Reitz "format"); 604c98ac35dSStefan Weil *pdrv = NULL; 605c98ac35dSStefan Weil return ret; 606ea2384d3Sbellard } 607ea2384d3Sbellard 608ea2384d3Sbellard score_max = 0; 60984a12e66SChristoph Hellwig drv = NULL; 6108a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 61183f64091Sbellard if (drv1->bdrv_probe) { 612ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 613ea2384d3Sbellard if (score > score_max) { 614ea2384d3Sbellard score_max = score; 615ea2384d3Sbellard drv = drv1; 616ea2384d3Sbellard } 617ea2384d3Sbellard } 61883f64091Sbellard } 619c98ac35dSStefan Weil if (!drv) { 62034b5d2c6SMax Reitz error_setg(errp, "Could not determine image format: No compatible " 62134b5d2c6SMax Reitz "driver found"); 622c98ac35dSStefan Weil ret = -ENOENT; 623c98ac35dSStefan Weil } 624c98ac35dSStefan Weil *pdrv = drv; 625c98ac35dSStefan Weil return ret; 626ea2384d3Sbellard } 627ea2384d3Sbellard 62851762288SStefan Hajnoczi /** 62951762288SStefan Hajnoczi * Set the current 'total_sectors' value 63051762288SStefan Hajnoczi */ 63151762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 63251762288SStefan Hajnoczi { 63351762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 63451762288SStefan Hajnoczi 635396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 636396759adSNicholas Bellinger if (bs->sg) 637396759adSNicholas Bellinger return 0; 638396759adSNicholas Bellinger 63951762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 64051762288SStefan Hajnoczi if (drv->bdrv_getlength) { 64151762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 64251762288SStefan Hajnoczi if (length < 0) { 64351762288SStefan Hajnoczi return length; 64451762288SStefan Hajnoczi } 6457e382003SFam Zheng hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); 64651762288SStefan Hajnoczi } 64751762288SStefan Hajnoczi 64851762288SStefan Hajnoczi bs->total_sectors = hint; 64951762288SStefan Hajnoczi return 0; 65051762288SStefan Hajnoczi } 65151762288SStefan Hajnoczi 652c3993cdcSStefan Hajnoczi /** 6539e8f1835SPaolo Bonzini * Set open flags for a given discard mode 6549e8f1835SPaolo Bonzini * 6559e8f1835SPaolo Bonzini * Return 0 on success, -1 if the discard mode was invalid. 6569e8f1835SPaolo Bonzini */ 6579e8f1835SPaolo Bonzini int bdrv_parse_discard_flags(const char *mode, int *flags) 6589e8f1835SPaolo Bonzini { 6599e8f1835SPaolo Bonzini *flags &= ~BDRV_O_UNMAP; 6609e8f1835SPaolo Bonzini 6619e8f1835SPaolo Bonzini if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { 6629e8f1835SPaolo Bonzini /* do nothing */ 6639e8f1835SPaolo Bonzini } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { 6649e8f1835SPaolo Bonzini *flags |= BDRV_O_UNMAP; 6659e8f1835SPaolo Bonzini } else { 6669e8f1835SPaolo Bonzini return -1; 6679e8f1835SPaolo Bonzini } 6689e8f1835SPaolo Bonzini 6699e8f1835SPaolo Bonzini return 0; 6709e8f1835SPaolo Bonzini } 6719e8f1835SPaolo Bonzini 6729e8f1835SPaolo Bonzini /** 673c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 674c3993cdcSStefan Hajnoczi * 675c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 676c3993cdcSStefan Hajnoczi */ 677c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 678c3993cdcSStefan Hajnoczi { 679c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 680c3993cdcSStefan Hajnoczi 681c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 682c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 68392196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 68492196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 685c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 686c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 687c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 688c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 689c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 690c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 691c3993cdcSStefan Hajnoczi /* this is the default */ 692c3993cdcSStefan Hajnoczi } else { 693c3993cdcSStefan Hajnoczi return -1; 694c3993cdcSStefan Hajnoczi } 695c3993cdcSStefan Hajnoczi 696c3993cdcSStefan Hajnoczi return 0; 697c3993cdcSStefan Hajnoczi } 698c3993cdcSStefan Hajnoczi 69953fec9d3SStefan Hajnoczi /** 70053fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 70153fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 70253fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 70353fec9d3SStefan Hajnoczi */ 70453fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 70553fec9d3SStefan Hajnoczi { 70653fec9d3SStefan Hajnoczi bs->copy_on_read++; 70753fec9d3SStefan Hajnoczi } 70853fec9d3SStefan Hajnoczi 70953fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 71053fec9d3SStefan Hajnoczi { 71153fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 71253fec9d3SStefan Hajnoczi bs->copy_on_read--; 71353fec9d3SStefan Hajnoczi } 71453fec9d3SStefan Hajnoczi 7157b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 7167b272452SKevin Wolf { 7177b272452SKevin Wolf int open_flags = flags | BDRV_O_CACHE_WB; 7187b272452SKevin Wolf 7197b272452SKevin Wolf /* 7207b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 7217b272452SKevin Wolf * image. 7227b272452SKevin Wolf */ 7237b272452SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 7247b272452SKevin Wolf 7257b272452SKevin Wolf /* 7267b272452SKevin Wolf * Snapshots should be writable. 7277b272452SKevin Wolf */ 7287b272452SKevin Wolf if (bs->is_temporary) { 7297b272452SKevin Wolf open_flags |= BDRV_O_RDWR; 7307b272452SKevin Wolf } 7317b272452SKevin Wolf 7327b272452SKevin Wolf return open_flags; 7337b272452SKevin Wolf } 7347b272452SKevin Wolf 735b6ce07aaSKevin Wolf /* 73657915332SKevin Wolf * Common part for opening disk images and files 737b6ad491aSKevin Wolf * 738b6ad491aSKevin Wolf * Removes all processed options from *options. 73957915332SKevin Wolf */ 740f500a6d3SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, 74134b5d2c6SMax Reitz QDict *options, int flags, BlockDriver *drv, Error **errp) 74257915332SKevin Wolf { 74357915332SKevin Wolf int ret, open_flags; 744035fccdfSKevin Wolf const char *filename; 74534b5d2c6SMax Reitz Error *local_err = NULL; 74657915332SKevin Wolf 74757915332SKevin Wolf assert(drv != NULL); 7486405875cSPaolo Bonzini assert(bs->file == NULL); 749707ff828SKevin Wolf assert(options != NULL && bs->options != options); 75057915332SKevin Wolf 75145673671SKevin Wolf if (file != NULL) { 75245673671SKevin Wolf filename = file->filename; 75345673671SKevin Wolf } else { 75445673671SKevin Wolf filename = qdict_get_try_str(options, "filename"); 75545673671SKevin Wolf } 75645673671SKevin Wolf 75745673671SKevin Wolf trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name); 75828dcee10SStefan Hajnoczi 7595d186eb0SKevin Wolf /* bdrv_open() with directly using a protocol as drv. This layer is already 7605d186eb0SKevin Wolf * opened, so assign it to bs (while file becomes a closed BlockDriverState) 7615d186eb0SKevin Wolf * and return immediately. */ 7625d186eb0SKevin Wolf if (file != NULL && drv->bdrv_file_open) { 7635d186eb0SKevin Wolf bdrv_swap(file, bs); 7645d186eb0SKevin Wolf return 0; 7655d186eb0SKevin Wolf } 7665d186eb0SKevin Wolf 76757915332SKevin Wolf bs->open_flags = flags; 76857915332SKevin Wolf bs->buffer_alignment = 512; 7690d51b4deSAsias He bs->zero_beyond_eof = true; 770b64ec4e4SFam Zheng open_flags = bdrv_open_flags(bs, flags); 771b64ec4e4SFam Zheng bs->read_only = !(open_flags & BDRV_O_RDWR); 772b64ec4e4SFam Zheng 773b64ec4e4SFam Zheng if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { 7748f94a6e4SKevin Wolf error_setg(errp, 7758f94a6e4SKevin Wolf !bs->read_only && bdrv_is_whitelisted(drv, true) 7768f94a6e4SKevin Wolf ? "Driver '%s' can only be used for read-only devices" 7778f94a6e4SKevin Wolf : "Driver '%s' is not whitelisted", 7788f94a6e4SKevin Wolf drv->format_name); 779b64ec4e4SFam Zheng return -ENOTSUP; 780b64ec4e4SFam Zheng } 78157915332SKevin Wolf 78253fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 7830ebd24e0SKevin Wolf if (flags & BDRV_O_COPY_ON_READ) { 7840ebd24e0SKevin Wolf if (!bs->read_only) { 78553fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 7860ebd24e0SKevin Wolf } else { 7870ebd24e0SKevin Wolf error_setg(errp, "Can't use copy-on-read on read-only device"); 7880ebd24e0SKevin Wolf return -EINVAL; 7890ebd24e0SKevin Wolf } 79053fec9d3SStefan Hajnoczi } 79153fec9d3SStefan Hajnoczi 792c2ad1b0cSKevin Wolf if (filename != NULL) { 79357915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 794c2ad1b0cSKevin Wolf } else { 795c2ad1b0cSKevin Wolf bs->filename[0] = '\0'; 796c2ad1b0cSKevin Wolf } 79757915332SKevin Wolf 79857915332SKevin Wolf bs->drv = drv; 7997267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 80057915332SKevin Wolf 80103f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 802e7c63796SStefan Hajnoczi 80366f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 80466f82ceeSKevin Wolf if (drv->bdrv_file_open) { 8055d186eb0SKevin Wolf assert(file == NULL); 806030be321SBenoît Canet assert(!drv->bdrv_needs_filename || filename != NULL); 80734b5d2c6SMax Reitz ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); 808f500a6d3SKevin Wolf } else { 8092af5ef70SKevin Wolf if (file == NULL) { 81034b5d2c6SMax Reitz error_setg(errp, "Can't use '%s' as a block driver for the " 81134b5d2c6SMax Reitz "protocol level", drv->format_name); 8122af5ef70SKevin Wolf ret = -EINVAL; 8132af5ef70SKevin Wolf goto free_and_fail; 8142af5ef70SKevin Wolf } 815f500a6d3SKevin Wolf bs->file = file; 81634b5d2c6SMax Reitz ret = drv->bdrv_open(bs, options, open_flags, &local_err); 81766f82ceeSKevin Wolf } 81866f82ceeSKevin Wolf 81957915332SKevin Wolf if (ret < 0) { 82034b5d2c6SMax Reitz if (error_is_set(&local_err)) { 82134b5d2c6SMax Reitz error_propagate(errp, local_err); 8222fa9aa59SDunrong Huang } else if (bs->filename[0]) { 8232fa9aa59SDunrong Huang error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); 82434b5d2c6SMax Reitz } else { 82534b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not open image"); 82634b5d2c6SMax Reitz } 82757915332SKevin Wolf goto free_and_fail; 82857915332SKevin Wolf } 82957915332SKevin Wolf 83051762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 83151762288SStefan Hajnoczi if (ret < 0) { 83234b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not refresh total sector count"); 83351762288SStefan Hajnoczi goto free_and_fail; 83457915332SKevin Wolf } 83551762288SStefan Hajnoczi 83657915332SKevin Wolf #ifndef _WIN32 83757915332SKevin Wolf if (bs->is_temporary) { 838d4cea8dfSDunrong Huang assert(bs->filename[0] != '\0'); 839d4cea8dfSDunrong Huang unlink(bs->filename); 84057915332SKevin Wolf } 84157915332SKevin Wolf #endif 84257915332SKevin Wolf return 0; 84357915332SKevin Wolf 84457915332SKevin Wolf free_and_fail: 84566f82ceeSKevin Wolf bs->file = NULL; 8467267c094SAnthony Liguori g_free(bs->opaque); 84757915332SKevin Wolf bs->opaque = NULL; 84857915332SKevin Wolf bs->drv = NULL; 84957915332SKevin Wolf return ret; 85057915332SKevin Wolf } 85157915332SKevin Wolf 85257915332SKevin Wolf /* 853b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 854787e4a85SKevin Wolf * 855787e4a85SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 856787e4a85SKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 857787e4a85SKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 858787e4a85SKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_file_open. 859b6ce07aaSKevin Wolf */ 860787e4a85SKevin Wolf int bdrv_file_open(BlockDriverState **pbs, const char *filename, 86134b5d2c6SMax Reitz QDict *options, int flags, Error **errp) 862b338082bSbellard { 86383f64091Sbellard BlockDriverState *bs; 8646db95603SChristoph Hellwig BlockDriver *drv; 865c2ad1b0cSKevin Wolf const char *drvname; 86698289620SKevin Wolf bool allow_protocol_prefix = false; 86734b5d2c6SMax Reitz Error *local_err = NULL; 86883f64091Sbellard int ret; 8693b0d4f61Sbellard 870707ff828SKevin Wolf /* NULL means an empty set of options */ 871707ff828SKevin Wolf if (options == NULL) { 872707ff828SKevin Wolf options = qdict_new(); 8733b0d4f61Sbellard } 874707ff828SKevin Wolf 875707ff828SKevin Wolf bs = bdrv_new(""); 876707ff828SKevin Wolf bs->options = options; 877707ff828SKevin Wolf options = qdict_clone_shallow(options); 878707ff828SKevin Wolf 879035fccdfSKevin Wolf /* Fetch the file name from the options QDict if necessary */ 880035fccdfSKevin Wolf if (!filename) { 881035fccdfSKevin Wolf filename = qdict_get_try_str(options, "filename"); 882035fccdfSKevin Wolf } else if (filename && !qdict_haskey(options, "filename")) { 883035fccdfSKevin Wolf qdict_put(options, "filename", qstring_from_str(filename)); 88498289620SKevin Wolf allow_protocol_prefix = true; 885035fccdfSKevin Wolf } else { 88634b5d2c6SMax Reitz error_setg(errp, "Can't specify 'file' and 'filename' options at the " 88734b5d2c6SMax Reitz "same time"); 888035fccdfSKevin Wolf ret = -EINVAL; 889035fccdfSKevin Wolf goto fail; 890035fccdfSKevin Wolf } 891035fccdfSKevin Wolf 892c2ad1b0cSKevin Wolf /* Find the right block driver */ 893c2ad1b0cSKevin Wolf drvname = qdict_get_try_str(options, "driver"); 894c2ad1b0cSKevin Wolf if (drvname) { 8958f94a6e4SKevin Wolf drv = bdrv_find_format(drvname); 89634b5d2c6SMax Reitz if (!drv) { 89734b5d2c6SMax Reitz error_setg(errp, "Unknown driver '%s'", drvname); 89834b5d2c6SMax Reitz } 899c2ad1b0cSKevin Wolf qdict_del(options, "driver"); 900c2ad1b0cSKevin Wolf } else if (filename) { 90198289620SKevin Wolf drv = bdrv_find_protocol(filename, allow_protocol_prefix); 90298289620SKevin Wolf if (!drv) { 90334b5d2c6SMax Reitz error_setg(errp, "Unknown protocol"); 90498289620SKevin Wolf } 905c2ad1b0cSKevin Wolf } else { 90634b5d2c6SMax Reitz error_setg(errp, "Must specify either driver or file"); 907c2ad1b0cSKevin Wolf drv = NULL; 908c2ad1b0cSKevin Wolf } 909c2ad1b0cSKevin Wolf 910c2ad1b0cSKevin Wolf if (!drv) { 91134b5d2c6SMax Reitz /* errp has been set already */ 912c2ad1b0cSKevin Wolf ret = -ENOENT; 913c2ad1b0cSKevin Wolf goto fail; 914c2ad1b0cSKevin Wolf } 915c2ad1b0cSKevin Wolf 916c2ad1b0cSKevin Wolf /* Parse the filename and open it */ 917c2ad1b0cSKevin Wolf if (drv->bdrv_parse_filename && filename) { 9186963a30dSKevin Wolf drv->bdrv_parse_filename(filename, options, &local_err); 9196963a30dSKevin Wolf if (error_is_set(&local_err)) { 92034b5d2c6SMax Reitz error_propagate(errp, local_err); 9216963a30dSKevin Wolf ret = -EINVAL; 9226963a30dSKevin Wolf goto fail; 9236963a30dSKevin Wolf } 92456d1b4d2SKevin Wolf qdict_del(options, "filename"); 925030be321SBenoît Canet } else if (drv->bdrv_needs_filename && !filename) { 92634b5d2c6SMax Reitz error_setg(errp, "The '%s' block driver requires a file name", 927c2ad1b0cSKevin Wolf drv->format_name); 928c2ad1b0cSKevin Wolf ret = -EINVAL; 929c2ad1b0cSKevin Wolf goto fail; 9306963a30dSKevin Wolf } 9316963a30dSKevin Wolf 93234b5d2c6SMax Reitz ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err); 933707ff828SKevin Wolf if (ret < 0) { 93434b5d2c6SMax Reitz error_propagate(errp, local_err); 935707ff828SKevin Wolf goto fail; 936707ff828SKevin Wolf } 937707ff828SKevin Wolf 938707ff828SKevin Wolf /* Check if any unknown options were used */ 939707ff828SKevin Wolf if (qdict_size(options) != 0) { 940707ff828SKevin Wolf const QDictEntry *entry = qdict_first(options); 94134b5d2c6SMax Reitz error_setg(errp, "Block protocol '%s' doesn't support the option '%s'", 942707ff828SKevin Wolf drv->format_name, entry->key); 943707ff828SKevin Wolf ret = -EINVAL; 944707ff828SKevin Wolf goto fail; 945707ff828SKevin Wolf } 946707ff828SKevin Wolf QDECREF(options); 947707ff828SKevin Wolf 94871d0770cSaliguori bs->growable = 1; 94983f64091Sbellard *pbs = bs; 95083f64091Sbellard return 0; 951707ff828SKevin Wolf 952707ff828SKevin Wolf fail: 953707ff828SKevin Wolf QDECREF(options); 954707ff828SKevin Wolf if (!bs->drv) { 955707ff828SKevin Wolf QDECREF(bs->options); 956707ff828SKevin Wolf } 9574f6fd349SFam Zheng bdrv_unref(bs); 958707ff828SKevin Wolf return ret; 9593b0d4f61Sbellard } 9603b0d4f61Sbellard 96131ca6d07SKevin Wolf /* 96231ca6d07SKevin Wolf * Opens the backing file for a BlockDriverState if not yet open 96331ca6d07SKevin Wolf * 96431ca6d07SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 96531ca6d07SKevin Wolf * empty set of options. The reference to the QDict is transferred to this 96631ca6d07SKevin Wolf * function (even on failure), so if the caller intends to reuse the dictionary, 96731ca6d07SKevin Wolf * it needs to use QINCREF() before calling bdrv_file_open. 96831ca6d07SKevin Wolf */ 96934b5d2c6SMax Reitz int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) 9709156df12SPaolo Bonzini { 9719156df12SPaolo Bonzini char backing_filename[PATH_MAX]; 9729156df12SPaolo Bonzini int back_flags, ret; 9739156df12SPaolo Bonzini BlockDriver *back_drv = NULL; 97434b5d2c6SMax Reitz Error *local_err = NULL; 9759156df12SPaolo Bonzini 9769156df12SPaolo Bonzini if (bs->backing_hd != NULL) { 97731ca6d07SKevin Wolf QDECREF(options); 9789156df12SPaolo Bonzini return 0; 9799156df12SPaolo Bonzini } 9809156df12SPaolo Bonzini 98131ca6d07SKevin Wolf /* NULL means an empty set of options */ 98231ca6d07SKevin Wolf if (options == NULL) { 98331ca6d07SKevin Wolf options = qdict_new(); 98431ca6d07SKevin Wolf } 98531ca6d07SKevin Wolf 9869156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 9871cb6f506SKevin Wolf if (qdict_haskey(options, "file.filename")) { 9881cb6f506SKevin Wolf backing_filename[0] = '\0'; 9891cb6f506SKevin Wolf } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { 99031ca6d07SKevin Wolf QDECREF(options); 9919156df12SPaolo Bonzini return 0; 992dbecebddSFam Zheng } else { 993dbecebddSFam Zheng bdrv_get_full_backing_filename(bs, backing_filename, 994dbecebddSFam Zheng sizeof(backing_filename)); 9959156df12SPaolo Bonzini } 9969156df12SPaolo Bonzini 9979156df12SPaolo Bonzini bs->backing_hd = bdrv_new(""); 9989156df12SPaolo Bonzini 9999156df12SPaolo Bonzini if (bs->backing_format[0] != '\0') { 10009156df12SPaolo Bonzini back_drv = bdrv_find_format(bs->backing_format); 10019156df12SPaolo Bonzini } 10029156df12SPaolo Bonzini 10039156df12SPaolo Bonzini /* backing files always opened read-only */ 100487a5debdSThibaut LAURENT back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | 100587a5debdSThibaut LAURENT BDRV_O_COPY_ON_READ); 10069156df12SPaolo Bonzini 100731ca6d07SKevin Wolf ret = bdrv_open(bs->backing_hd, 100831ca6d07SKevin Wolf *backing_filename ? backing_filename : NULL, options, 100934b5d2c6SMax Reitz back_flags, back_drv, &local_err); 10109156df12SPaolo Bonzini if (ret < 0) { 10114f6fd349SFam Zheng bdrv_unref(bs->backing_hd); 10129156df12SPaolo Bonzini bs->backing_hd = NULL; 10139156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 1014b04b6b6eSFam Zheng error_setg(errp, "Could not open backing file: %s", 1015b04b6b6eSFam Zheng error_get_pretty(local_err)); 1016b04b6b6eSFam Zheng error_free(local_err); 10179156df12SPaolo Bonzini return ret; 10189156df12SPaolo Bonzini } 101961ed2684SMax Reitz pstrcpy(bs->backing_file, sizeof(bs->backing_file), 102061ed2684SMax Reitz bs->backing_hd->file->filename); 10219156df12SPaolo Bonzini return 0; 10229156df12SPaolo Bonzini } 10239156df12SPaolo Bonzini 1024b6ce07aaSKevin Wolf /* 1025b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 1026de9c0cecSKevin Wolf * 1027de9c0cecSKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 1028de9c0cecSKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 1029de9c0cecSKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 1030de9c0cecSKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_open. 1031b6ce07aaSKevin Wolf */ 1032de9c0cecSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, 103334b5d2c6SMax Reitz int flags, BlockDriver *drv, Error **errp) 1034ea2384d3Sbellard { 1035b6ce07aaSKevin Wolf int ret; 103689c9bc3dSStefan Weil /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 103789c9bc3dSStefan Weil char tmp_filename[PATH_MAX + 1]; 1038f500a6d3SKevin Wolf BlockDriverState *file = NULL; 1039707ff828SKevin Wolf QDict *file_options = NULL; 104074fe54f2SKevin Wolf const char *drvname; 104134b5d2c6SMax Reitz Error *local_err = NULL; 104233e3963eSbellard 1043de9c0cecSKevin Wolf /* NULL means an empty set of options */ 1044de9c0cecSKevin Wolf if (options == NULL) { 1045de9c0cecSKevin Wolf options = qdict_new(); 1046de9c0cecSKevin Wolf } 1047de9c0cecSKevin Wolf 1048de9c0cecSKevin Wolf bs->options = options; 1049b6ad491aSKevin Wolf options = qdict_clone_shallow(options); 1050de9c0cecSKevin Wolf 1051de9c0cecSKevin Wolf /* For snapshot=on, create a temporary qcow2 overlay */ 105283f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 1053ea2384d3Sbellard BlockDriverState *bs1; 1054ea2384d3Sbellard int64_t total_size; 105591a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 105608b392e1SKevin Wolf QEMUOptionParameter *create_options; 10579fd3171aSKevin Wolf QDict *snapshot_options; 1058c2ad1b0cSKevin Wolf 1059ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 1060ea2384d3Sbellard instead of opening 'filename' directly */ 1061ea2384d3Sbellard 10629fd3171aSKevin Wolf /* Get the required size from the image */ 1063ea2384d3Sbellard bs1 = bdrv_new(""); 10649fd3171aSKevin Wolf QINCREF(options); 1065c9fbb99dSKevin Wolf ret = bdrv_open(bs1, filename, options, BDRV_O_NO_BACKING, 1066c9fbb99dSKevin Wolf drv, &local_err); 106751d7c00cSaliguori if (ret < 0) { 10684f6fd349SFam Zheng bdrv_unref(bs1); 1069de9c0cecSKevin Wolf goto fail; 1070ea2384d3Sbellard } 10713e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 10727c96d46eSaliguori 10734f6fd349SFam Zheng bdrv_unref(bs1); 1074ea2384d3Sbellard 10759fd3171aSKevin Wolf /* Create the temporary image */ 1076eba25057SJim Meyering ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 1077eba25057SJim Meyering if (ret < 0) { 107834b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not get temporary filename"); 1079de9c0cecSKevin Wolf goto fail; 1080eba25057SJim Meyering } 10817c96d46eSaliguori 108291a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 108308b392e1SKevin Wolf create_options = parse_option_parameters("", bdrv_qcow2->create_options, 108408b392e1SKevin Wolf NULL); 108591a073a9SKevin Wolf 108608b392e1SKevin Wolf set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); 108791a073a9SKevin Wolf 1088cc84d90fSMax Reitz ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err); 108908b392e1SKevin Wolf free_option_parameters(create_options); 109051d7c00cSaliguori if (ret < 0) { 109134b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not create temporary overlay " 1092cc84d90fSMax Reitz "'%s': %s", tmp_filename, 1093cc84d90fSMax Reitz error_get_pretty(local_err)); 1094cc84d90fSMax Reitz error_free(local_err); 1095cc84d90fSMax Reitz local_err = NULL; 1096de9c0cecSKevin Wolf goto fail; 1097ea2384d3Sbellard } 109891a073a9SKevin Wolf 10999fd3171aSKevin Wolf /* Prepare a new options QDict for the temporary file, where user 11009fd3171aSKevin Wolf * options refer to the backing file */ 11019fd3171aSKevin Wolf if (filename) { 11029fd3171aSKevin Wolf qdict_put(options, "file.filename", qstring_from_str(filename)); 11039fd3171aSKevin Wolf } 11049fd3171aSKevin Wolf if (drv) { 11059fd3171aSKevin Wolf qdict_put(options, "driver", qstring_from_str(drv->format_name)); 11069fd3171aSKevin Wolf } 11079fd3171aSKevin Wolf 11089fd3171aSKevin Wolf snapshot_options = qdict_new(); 11099fd3171aSKevin Wolf qdict_put(snapshot_options, "backing", options); 11109fd3171aSKevin Wolf qdict_flatten(snapshot_options); 11119fd3171aSKevin Wolf 11129fd3171aSKevin Wolf bs->options = snapshot_options; 11139fd3171aSKevin Wolf options = qdict_clone_shallow(bs->options); 11149fd3171aSKevin Wolf 1115ea2384d3Sbellard filename = tmp_filename; 111691a073a9SKevin Wolf drv = bdrv_qcow2; 1117ea2384d3Sbellard bs->is_temporary = 1; 1118ea2384d3Sbellard } 1119ea2384d3Sbellard 1120f500a6d3SKevin Wolf /* Open image file without format layer */ 1121be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 1122be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 1123be028adcSJeff Cody } 1124be028adcSJeff Cody 11255726d872SBenoît Canet qdict_extract_subqdict(options, &file_options, "file."); 1126707ff828SKevin Wolf 1127707ff828SKevin Wolf ret = bdrv_file_open(&file, filename, file_options, 112834b5d2c6SMax Reitz bdrv_open_flags(bs, flags | BDRV_O_UNMAP), &local_err); 1129f500a6d3SKevin Wolf if (ret < 0) { 1130de9c0cecSKevin Wolf goto fail; 1131f500a6d3SKevin Wolf } 1132f500a6d3SKevin Wolf 1133f500a6d3SKevin Wolf /* Find the right image format driver */ 113474fe54f2SKevin Wolf drvname = qdict_get_try_str(options, "driver"); 113574fe54f2SKevin Wolf if (drvname) { 11368f94a6e4SKevin Wolf drv = bdrv_find_format(drvname); 113774fe54f2SKevin Wolf qdict_del(options, "driver"); 113806d22aa3SKevin Wolf if (!drv) { 113906d22aa3SKevin Wolf error_setg(errp, "Invalid driver: '%s'", drvname); 114006d22aa3SKevin Wolf ret = -EINVAL; 114106d22aa3SKevin Wolf goto unlink_and_fail; 114206d22aa3SKevin Wolf } 114374fe54f2SKevin Wolf } 114474fe54f2SKevin Wolf 1145f500a6d3SKevin Wolf if (!drv) { 114634b5d2c6SMax Reitz ret = find_image_format(file, filename, &drv, &local_err); 1147f500a6d3SKevin Wolf } 1148f500a6d3SKevin Wolf 1149f500a6d3SKevin Wolf if (!drv) { 1150f500a6d3SKevin Wolf goto unlink_and_fail; 1151f500a6d3SKevin Wolf } 1152f500a6d3SKevin Wolf 1153b6ce07aaSKevin Wolf /* Open the image */ 115434b5d2c6SMax Reitz ret = bdrv_open_common(bs, file, options, flags, drv, &local_err); 1155b6ce07aaSKevin Wolf if (ret < 0) { 11566987307cSChristoph Hellwig goto unlink_and_fail; 11576987307cSChristoph Hellwig } 11586987307cSChristoph Hellwig 1159f500a6d3SKevin Wolf if (bs->file != file) { 11604f6fd349SFam Zheng bdrv_unref(file); 1161f500a6d3SKevin Wolf file = NULL; 1162f500a6d3SKevin Wolf } 1163f500a6d3SKevin Wolf 1164b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 11659156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 116631ca6d07SKevin Wolf QDict *backing_options; 116731ca6d07SKevin Wolf 11685726d872SBenoît Canet qdict_extract_subqdict(options, &backing_options, "backing."); 116934b5d2c6SMax Reitz ret = bdrv_open_backing_file(bs, backing_options, &local_err); 1170b6ce07aaSKevin Wolf if (ret < 0) { 1171b6ad491aSKevin Wolf goto close_and_fail; 1172b6ce07aaSKevin Wolf } 1173b6ce07aaSKevin Wolf } 1174b6ce07aaSKevin Wolf 1175b6ad491aSKevin Wolf /* Check if any unknown options were used */ 1176b6ad491aSKevin Wolf if (qdict_size(options) != 0) { 1177b6ad491aSKevin Wolf const QDictEntry *entry = qdict_first(options); 117834b5d2c6SMax Reitz error_setg(errp, "Block format '%s' used by device '%s' doesn't " 117934b5d2c6SMax Reitz "support the option '%s'", drv->format_name, bs->device_name, 118034b5d2c6SMax Reitz entry->key); 1181b6ad491aSKevin Wolf 1182b6ad491aSKevin Wolf ret = -EINVAL; 1183b6ad491aSKevin Wolf goto close_and_fail; 1184b6ad491aSKevin Wolf } 1185b6ad491aSKevin Wolf QDECREF(options); 1186b6ad491aSKevin Wolf 1187b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 11887d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 1189b6ce07aaSKevin Wolf } 1190b6ce07aaSKevin Wolf 1191b6ce07aaSKevin Wolf return 0; 1192b6ce07aaSKevin Wolf 1193b6ce07aaSKevin Wolf unlink_and_fail: 1194f500a6d3SKevin Wolf if (file != NULL) { 11954f6fd349SFam Zheng bdrv_unref(file); 1196f500a6d3SKevin Wolf } 1197b6ce07aaSKevin Wolf if (bs->is_temporary) { 1198b6ce07aaSKevin Wolf unlink(filename); 1199b6ce07aaSKevin Wolf } 1200de9c0cecSKevin Wolf fail: 1201de9c0cecSKevin Wolf QDECREF(bs->options); 1202b6ad491aSKevin Wolf QDECREF(options); 1203de9c0cecSKevin Wolf bs->options = NULL; 120434b5d2c6SMax Reitz if (error_is_set(&local_err)) { 120534b5d2c6SMax Reitz error_propagate(errp, local_err); 120634b5d2c6SMax Reitz } 1207b6ad491aSKevin Wolf return ret; 1208de9c0cecSKevin Wolf 1209b6ad491aSKevin Wolf close_and_fail: 1210b6ad491aSKevin Wolf bdrv_close(bs); 1211b6ad491aSKevin Wolf QDECREF(options); 121234b5d2c6SMax Reitz if (error_is_set(&local_err)) { 121334b5d2c6SMax Reitz error_propagate(errp, local_err); 121434b5d2c6SMax Reitz } 1215b6ce07aaSKevin Wolf return ret; 1216b6ce07aaSKevin Wolf } 1217b6ce07aaSKevin Wolf 1218e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 1219e971aa12SJeff Cody bool prepared; 1220e971aa12SJeff Cody BDRVReopenState state; 1221e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 1222e971aa12SJeff Cody } BlockReopenQueueEntry; 1223e971aa12SJeff Cody 1224e971aa12SJeff Cody /* 1225e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 1226e971aa12SJeff Cody * reopen of multiple devices. 1227e971aa12SJeff Cody * 1228e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 1229e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 1230e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 1231e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 1232e971aa12SJeff Cody * atomic 'set'. 1233e971aa12SJeff Cody * 1234e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 1235e971aa12SJeff Cody * 1236e971aa12SJeff Cody * flags contains the open flags for the associated bs 1237e971aa12SJeff Cody * 1238e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 1239e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 1240e971aa12SJeff Cody * 1241e971aa12SJeff Cody */ 1242e971aa12SJeff Cody BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 1243e971aa12SJeff Cody BlockDriverState *bs, int flags) 1244e971aa12SJeff Cody { 1245e971aa12SJeff Cody assert(bs != NULL); 1246e971aa12SJeff Cody 1247e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 1248e971aa12SJeff Cody if (bs_queue == NULL) { 1249e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 1250e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 1251e971aa12SJeff Cody } 1252e971aa12SJeff Cody 1253e971aa12SJeff Cody if (bs->file) { 1254e971aa12SJeff Cody bdrv_reopen_queue(bs_queue, bs->file, flags); 1255e971aa12SJeff Cody } 1256e971aa12SJeff Cody 1257e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 1258e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 1259e971aa12SJeff Cody 1260e971aa12SJeff Cody bs_entry->state.bs = bs; 1261e971aa12SJeff Cody bs_entry->state.flags = flags; 1262e971aa12SJeff Cody 1263e971aa12SJeff Cody return bs_queue; 1264e971aa12SJeff Cody } 1265e971aa12SJeff Cody 1266e971aa12SJeff Cody /* 1267e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 1268e971aa12SJeff Cody * 1269e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 1270e971aa12SJeff Cody * via bdrv_reopen_queue(). 1271e971aa12SJeff Cody * 1272e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 1273e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 1274e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 1275e971aa12SJeff Cody * data cleaned up. 1276e971aa12SJeff Cody * 1277e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 1278e971aa12SJeff Cody * to all devices. 1279e971aa12SJeff Cody * 1280e971aa12SJeff Cody */ 1281e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 1282e971aa12SJeff Cody { 1283e971aa12SJeff Cody int ret = -1; 1284e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 1285e971aa12SJeff Cody Error *local_err = NULL; 1286e971aa12SJeff Cody 1287e971aa12SJeff Cody assert(bs_queue != NULL); 1288e971aa12SJeff Cody 1289e971aa12SJeff Cody bdrv_drain_all(); 1290e971aa12SJeff Cody 1291e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1292e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 1293e971aa12SJeff Cody error_propagate(errp, local_err); 1294e971aa12SJeff Cody goto cleanup; 1295e971aa12SJeff Cody } 1296e971aa12SJeff Cody bs_entry->prepared = true; 1297e971aa12SJeff Cody } 1298e971aa12SJeff Cody 1299e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 1300e971aa12SJeff Cody * changes 1301e971aa12SJeff Cody */ 1302e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1303e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 1304e971aa12SJeff Cody } 1305e971aa12SJeff Cody 1306e971aa12SJeff Cody ret = 0; 1307e971aa12SJeff Cody 1308e971aa12SJeff Cody cleanup: 1309e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 1310e971aa12SJeff Cody if (ret && bs_entry->prepared) { 1311e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 1312e971aa12SJeff Cody } 1313e971aa12SJeff Cody g_free(bs_entry); 1314e971aa12SJeff Cody } 1315e971aa12SJeff Cody g_free(bs_queue); 1316e971aa12SJeff Cody return ret; 1317e971aa12SJeff Cody } 1318e971aa12SJeff Cody 1319e971aa12SJeff Cody 1320e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 1321e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1322e971aa12SJeff Cody { 1323e971aa12SJeff Cody int ret = -1; 1324e971aa12SJeff Cody Error *local_err = NULL; 1325e971aa12SJeff Cody BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); 1326e971aa12SJeff Cody 1327e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1328e971aa12SJeff Cody if (local_err != NULL) { 1329e971aa12SJeff Cody error_propagate(errp, local_err); 1330e971aa12SJeff Cody } 1331e971aa12SJeff Cody return ret; 1332e971aa12SJeff Cody } 1333e971aa12SJeff Cody 1334e971aa12SJeff Cody 1335e971aa12SJeff Cody /* 1336e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1337e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1338e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1339e971aa12SJeff Cody * 1340e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1341e971aa12SJeff Cody * flags are the new open flags 1342e971aa12SJeff Cody * queue is the reopen queue 1343e971aa12SJeff Cody * 1344e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1345e971aa12SJeff Cody * as well. 1346e971aa12SJeff Cody * 1347e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1348e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1349e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1350e971aa12SJeff Cody * 1351e971aa12SJeff Cody */ 1352e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1353e971aa12SJeff Cody Error **errp) 1354e971aa12SJeff Cody { 1355e971aa12SJeff Cody int ret = -1; 1356e971aa12SJeff Cody Error *local_err = NULL; 1357e971aa12SJeff Cody BlockDriver *drv; 1358e971aa12SJeff Cody 1359e971aa12SJeff Cody assert(reopen_state != NULL); 1360e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1361e971aa12SJeff Cody drv = reopen_state->bs->drv; 1362e971aa12SJeff Cody 1363e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 1364e971aa12SJeff Cody * to r/w */ 1365e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 1366e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 1367e971aa12SJeff Cody error_set(errp, QERR_DEVICE_IS_READ_ONLY, 1368e971aa12SJeff Cody reopen_state->bs->device_name); 1369e971aa12SJeff Cody goto error; 1370e971aa12SJeff Cody } 1371e971aa12SJeff Cody 1372e971aa12SJeff Cody 1373e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 1374e971aa12SJeff Cody if (ret) { 1375e971aa12SJeff Cody error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", 1376e971aa12SJeff Cody strerror(-ret)); 1377e971aa12SJeff Cody goto error; 1378e971aa12SJeff Cody } 1379e971aa12SJeff Cody 1380e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 1381e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 1382e971aa12SJeff Cody if (ret) { 1383e971aa12SJeff Cody if (local_err != NULL) { 1384e971aa12SJeff Cody error_propagate(errp, local_err); 1385e971aa12SJeff Cody } else { 1386d8b6895fSLuiz Capitulino error_setg(errp, "failed while preparing to reopen image '%s'", 1387e971aa12SJeff Cody reopen_state->bs->filename); 1388e971aa12SJeff Cody } 1389e971aa12SJeff Cody goto error; 1390e971aa12SJeff Cody } 1391e971aa12SJeff Cody } else { 1392e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 1393e971aa12SJeff Cody * handler for each supported drv. */ 1394e971aa12SJeff Cody error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 1395e971aa12SJeff Cody drv->format_name, reopen_state->bs->device_name, 1396e971aa12SJeff Cody "reopening of file"); 1397e971aa12SJeff Cody ret = -1; 1398e971aa12SJeff Cody goto error; 1399e971aa12SJeff Cody } 1400e971aa12SJeff Cody 1401e971aa12SJeff Cody ret = 0; 1402e971aa12SJeff Cody 1403e971aa12SJeff Cody error: 1404e971aa12SJeff Cody return ret; 1405e971aa12SJeff Cody } 1406e971aa12SJeff Cody 1407e971aa12SJeff Cody /* 1408e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 1409e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 1410e971aa12SJeff Cody * the active BlockDriverState contents. 1411e971aa12SJeff Cody */ 1412e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 1413e971aa12SJeff Cody { 1414e971aa12SJeff Cody BlockDriver *drv; 1415e971aa12SJeff Cody 1416e971aa12SJeff Cody assert(reopen_state != NULL); 1417e971aa12SJeff Cody drv = reopen_state->bs->drv; 1418e971aa12SJeff Cody assert(drv != NULL); 1419e971aa12SJeff Cody 1420e971aa12SJeff Cody /* If there are any driver level actions to take */ 1421e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 1422e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 1423e971aa12SJeff Cody } 1424e971aa12SJeff Cody 1425e971aa12SJeff Cody /* set BDS specific flags now */ 1426e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 1427e971aa12SJeff Cody reopen_state->bs->enable_write_cache = !!(reopen_state->flags & 1428e971aa12SJeff Cody BDRV_O_CACHE_WB); 1429e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 1430e971aa12SJeff Cody } 1431e971aa12SJeff Cody 1432e971aa12SJeff Cody /* 1433e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 1434e971aa12SJeff Cody * reopen_state 1435e971aa12SJeff Cody */ 1436e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 1437e971aa12SJeff Cody { 1438e971aa12SJeff Cody BlockDriver *drv; 1439e971aa12SJeff Cody 1440e971aa12SJeff Cody assert(reopen_state != NULL); 1441e971aa12SJeff Cody drv = reopen_state->bs->drv; 1442e971aa12SJeff Cody assert(drv != NULL); 1443e971aa12SJeff Cody 1444e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 1445e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 1446e971aa12SJeff Cody } 1447e971aa12SJeff Cody } 1448e971aa12SJeff Cody 1449e971aa12SJeff Cody 1450fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 1451fc01f7e7Sbellard { 14523e914655SPaolo Bonzini if (bs->job) { 14533e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 14543e914655SPaolo Bonzini } 145558fda173SStefan Hajnoczi bdrv_drain_all(); /* complete I/O */ 145658fda173SStefan Hajnoczi bdrv_flush(bs); 145758fda173SStefan Hajnoczi bdrv_drain_all(); /* in case flush left pending I/O */ 1458d7d512f6SPaolo Bonzini notifier_list_notify(&bs->close_notifiers, bs); 14597094f12fSKevin Wolf 14603cbc002cSPaolo Bonzini if (bs->drv) { 1461557df6acSStefan Hajnoczi if (bs->backing_hd) { 14624f6fd349SFam Zheng bdrv_unref(bs->backing_hd); 1463557df6acSStefan Hajnoczi bs->backing_hd = NULL; 1464557df6acSStefan Hajnoczi } 1465ea2384d3Sbellard bs->drv->bdrv_close(bs); 14667267c094SAnthony Liguori g_free(bs->opaque); 1467ea2384d3Sbellard #ifdef _WIN32 1468ea2384d3Sbellard if (bs->is_temporary) { 1469ea2384d3Sbellard unlink(bs->filename); 1470ea2384d3Sbellard } 147167b915a5Sbellard #endif 1472ea2384d3Sbellard bs->opaque = NULL; 1473ea2384d3Sbellard bs->drv = NULL; 147453fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 1475a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 1476a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 14776405875cSPaolo Bonzini bs->total_sectors = 0; 14786405875cSPaolo Bonzini bs->encrypted = 0; 14796405875cSPaolo Bonzini bs->valid_key = 0; 14806405875cSPaolo Bonzini bs->sg = 0; 14816405875cSPaolo Bonzini bs->growable = 0; 14820d51b4deSAsias He bs->zero_beyond_eof = false; 1483de9c0cecSKevin Wolf QDECREF(bs->options); 1484de9c0cecSKevin Wolf bs->options = NULL; 1485b338082bSbellard 148666f82ceeSKevin Wolf if (bs->file != NULL) { 14874f6fd349SFam Zheng bdrv_unref(bs->file); 14880ac9377dSPaolo Bonzini bs->file = NULL; 148966f82ceeSKevin Wolf } 14909ca11154SPavel Hrdina } 149166f82ceeSKevin Wolf 14927d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 149398f90dbaSZhi Yong Wu 149498f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 149598f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 149698f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 149798f90dbaSZhi Yong Wu } 1498b338082bSbellard } 1499b338082bSbellard 15002bc93fedSMORITA Kazutaka void bdrv_close_all(void) 15012bc93fedSMORITA Kazutaka { 15022bc93fedSMORITA Kazutaka BlockDriverState *bs; 15032bc93fedSMORITA Kazutaka 15042bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 15052bc93fedSMORITA Kazutaka bdrv_close(bs); 15062bc93fedSMORITA Kazutaka } 15072bc93fedSMORITA Kazutaka } 15082bc93fedSMORITA Kazutaka 150988266f5aSStefan Hajnoczi /* Check if any requests are in-flight (including throttled requests) */ 151088266f5aSStefan Hajnoczi static bool bdrv_requests_pending(BlockDriverState *bs) 151188266f5aSStefan Hajnoczi { 151288266f5aSStefan Hajnoczi if (!QLIST_EMPTY(&bs->tracked_requests)) { 151388266f5aSStefan Hajnoczi return true; 151488266f5aSStefan Hajnoczi } 1515cc0681c4SBenoît Canet if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) { 1516cc0681c4SBenoît Canet return true; 1517cc0681c4SBenoît Canet } 1518cc0681c4SBenoît Canet if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) { 151988266f5aSStefan Hajnoczi return true; 152088266f5aSStefan Hajnoczi } 152188266f5aSStefan Hajnoczi if (bs->file && bdrv_requests_pending(bs->file)) { 152288266f5aSStefan Hajnoczi return true; 152388266f5aSStefan Hajnoczi } 152488266f5aSStefan Hajnoczi if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) { 152588266f5aSStefan Hajnoczi return true; 152688266f5aSStefan Hajnoczi } 152788266f5aSStefan Hajnoczi return false; 152888266f5aSStefan Hajnoczi } 152988266f5aSStefan Hajnoczi 153088266f5aSStefan Hajnoczi static bool bdrv_requests_pending_all(void) 153188266f5aSStefan Hajnoczi { 153288266f5aSStefan Hajnoczi BlockDriverState *bs; 153388266f5aSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 153488266f5aSStefan Hajnoczi if (bdrv_requests_pending(bs)) { 153588266f5aSStefan Hajnoczi return true; 153688266f5aSStefan Hajnoczi } 153788266f5aSStefan Hajnoczi } 153888266f5aSStefan Hajnoczi return false; 153988266f5aSStefan Hajnoczi } 154088266f5aSStefan Hajnoczi 1541922453bcSStefan Hajnoczi /* 1542922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 1543922453bcSStefan Hajnoczi * 1544922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 1545922453bcSStefan Hajnoczi * after calling this function. 15464c355d53SZhi Yong Wu * 15474c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 15484c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 15494c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 15504c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 15514c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 1552922453bcSStefan Hajnoczi */ 1553922453bcSStefan Hajnoczi void bdrv_drain_all(void) 1554922453bcSStefan Hajnoczi { 155588266f5aSStefan Hajnoczi /* Always run first iteration so any pending completion BHs run */ 155688266f5aSStefan Hajnoczi bool busy = true; 1557922453bcSStefan Hajnoczi BlockDriverState *bs; 1558922453bcSStefan Hajnoczi 155988266f5aSStefan Hajnoczi while (busy) { 15604c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 1561*0b06ef3bSStefan Hajnoczi bdrv_start_throttled_reqs(bs); 15624c355d53SZhi Yong Wu } 1563922453bcSStefan Hajnoczi 156488266f5aSStefan Hajnoczi busy = bdrv_requests_pending_all(); 156588266f5aSStefan Hajnoczi busy |= aio_poll(qemu_get_aio_context(), busy); 1566922453bcSStefan Hajnoczi } 1567922453bcSStefan Hajnoczi } 1568922453bcSStefan Hajnoczi 1569d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 1570d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1571d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1572d22b2f41SRyan Harper { 1573d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 1574d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 1575d22b2f41SRyan Harper } 1576d22b2f41SRyan Harper bs->device_name[0] = '\0'; 1577d22b2f41SRyan Harper } 1578d22b2f41SRyan Harper 1579e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1580e023b2e2SPaolo Bonzini { 1581e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1582e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1583e023b2e2SPaolo Bonzini } 1584e023b2e2SPaolo Bonzini } 1585e023b2e2SPaolo Bonzini 15864ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 15874ddc07caSPaolo Bonzini BlockDriverState *bs_src) 15884ddc07caSPaolo Bonzini { 15894ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 15904ddc07caSPaolo Bonzini bs_dest->open_flags = bs_src->open_flags; 15914ddc07caSPaolo Bonzini 15924ddc07caSPaolo Bonzini /* dev info */ 15934ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 15944ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 15954ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 15964ddc07caSPaolo Bonzini bs_dest->buffer_alignment = bs_src->buffer_alignment; 15974ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 15984ddc07caSPaolo Bonzini 15994ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 16004ddc07caSPaolo Bonzini 1601cc0681c4SBenoît Canet /* i/o throttled req */ 1602cc0681c4SBenoît Canet memcpy(&bs_dest->throttle_state, 1603cc0681c4SBenoît Canet &bs_src->throttle_state, 1604cc0681c4SBenoît Canet sizeof(ThrottleState)); 1605cc0681c4SBenoît Canet bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0]; 1606cc0681c4SBenoît Canet bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1]; 16074ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 16084ddc07caSPaolo Bonzini 16094ddc07caSPaolo Bonzini /* r/w error */ 16104ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 16114ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 16124ddc07caSPaolo Bonzini 16134ddc07caSPaolo Bonzini /* i/o status */ 16144ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 16154ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 16164ddc07caSPaolo Bonzini 16174ddc07caSPaolo Bonzini /* dirty bitmap */ 1618e4654d2dSFam Zheng bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps; 16194ddc07caSPaolo Bonzini 16209fcb0251SFam Zheng /* reference count */ 16219fcb0251SFam Zheng bs_dest->refcnt = bs_src->refcnt; 16229fcb0251SFam Zheng 16234ddc07caSPaolo Bonzini /* job */ 16244ddc07caSPaolo Bonzini bs_dest->in_use = bs_src->in_use; 16254ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 16264ddc07caSPaolo Bonzini 16274ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 16284ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 16294ddc07caSPaolo Bonzini bs_src->device_name); 16304ddc07caSPaolo Bonzini bs_dest->list = bs_src->list; 16314ddc07caSPaolo Bonzini } 16324ddc07caSPaolo Bonzini 16334ddc07caSPaolo Bonzini /* 16344ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 16354ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 16364ddc07caSPaolo Bonzini * actually attached to a device. 16374ddc07caSPaolo Bonzini * 16384ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 16394ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 16404ddc07caSPaolo Bonzini * 16414ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 16424ddc07caSPaolo Bonzini * 16434ddc07caSPaolo Bonzini * This function does not create any image files. 16444ddc07caSPaolo Bonzini */ 16454ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 16464ddc07caSPaolo Bonzini { 16474ddc07caSPaolo Bonzini BlockDriverState tmp; 16484ddc07caSPaolo Bonzini 16494ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 16504ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 1651e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs_new->dirty_bitmaps)); 16524ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 16534ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 16544ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 16554ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1656cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 16574ddc07caSPaolo Bonzini 16584ddc07caSPaolo Bonzini tmp = *bs_new; 16594ddc07caSPaolo Bonzini *bs_new = *bs_old; 16604ddc07caSPaolo Bonzini *bs_old = tmp; 16614ddc07caSPaolo Bonzini 16624ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 16634ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 16644ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 16654ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 16664ddc07caSPaolo Bonzini 16674ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 16684ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 16694ddc07caSPaolo Bonzini 16704ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 16714ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 16724ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 16734ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 16744ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1675cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 16764ddc07caSPaolo Bonzini 16774ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 16784ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 16794ddc07caSPaolo Bonzini } 16804ddc07caSPaolo Bonzini 16818802d1fdSJeff Cody /* 16828802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 16838802d1fdSJeff Cody * live, while keeping required fields on the top layer. 16848802d1fdSJeff Cody * 16858802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 16868802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 16878802d1fdSJeff Cody * 1688f6801b83SJeff Cody * bs_new is required to be anonymous. 1689f6801b83SJeff Cody * 16908802d1fdSJeff Cody * This function does not create any image files. 16918802d1fdSJeff Cody */ 16928802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 16938802d1fdSJeff Cody { 16944ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 16958802d1fdSJeff Cody 16968802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 16978802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 16984ddc07caSPaolo Bonzini bs_top->backing_hd = bs_new; 16994ddc07caSPaolo Bonzini bs_top->open_flags &= ~BDRV_O_NO_BACKING; 17004ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 17014ddc07caSPaolo Bonzini bs_new->filename); 17024ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 17034ddc07caSPaolo Bonzini bs_new->drv ? bs_new->drv->format_name : ""); 17048802d1fdSJeff Cody } 17058802d1fdSJeff Cody 17064f6fd349SFam Zheng static void bdrv_delete(BlockDriverState *bs) 1707b338082bSbellard { 1708fa879d62SMarkus Armbruster assert(!bs->dev); 17093e914655SPaolo Bonzini assert(!bs->job); 17103e914655SPaolo Bonzini assert(!bs->in_use); 17114f6fd349SFam Zheng assert(!bs->refcnt); 1712e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 171318846deeSMarkus Armbruster 1714e1b5c52eSStefan Hajnoczi bdrv_close(bs); 1715e1b5c52eSStefan Hajnoczi 17161b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1717d22b2f41SRyan Harper bdrv_make_anon(bs); 171834c6f050Saurel32 17197267c094SAnthony Liguori g_free(bs); 1720fc01f7e7Sbellard } 1721fc01f7e7Sbellard 1722fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1723fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 172418846deeSMarkus Armbruster { 1725fa879d62SMarkus Armbruster if (bs->dev) { 172618846deeSMarkus Armbruster return -EBUSY; 172718846deeSMarkus Armbruster } 1728fa879d62SMarkus Armbruster bs->dev = dev; 172928a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 173018846deeSMarkus Armbruster return 0; 173118846deeSMarkus Armbruster } 173218846deeSMarkus Armbruster 1733fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1734fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 173518846deeSMarkus Armbruster { 1736fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1737fa879d62SMarkus Armbruster abort(); 1738fa879d62SMarkus Armbruster } 1739fa879d62SMarkus Armbruster } 1740fa879d62SMarkus Armbruster 1741fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1742fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1743fa879d62SMarkus Armbruster { 1744fa879d62SMarkus Armbruster assert(bs->dev == dev); 1745fa879d62SMarkus Armbruster bs->dev = NULL; 17460e49de52SMarkus Armbruster bs->dev_ops = NULL; 17470e49de52SMarkus Armbruster bs->dev_opaque = NULL; 174829e05f20SMarkus Armbruster bs->buffer_alignment = 512; 174918846deeSMarkus Armbruster } 175018846deeSMarkus Armbruster 1751fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1752fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 175318846deeSMarkus Armbruster { 1754fa879d62SMarkus Armbruster return bs->dev; 175518846deeSMarkus Armbruster } 175618846deeSMarkus Armbruster 17570e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 17580e49de52SMarkus Armbruster void *opaque) 17590e49de52SMarkus Armbruster { 17600e49de52SMarkus Armbruster bs->dev_ops = ops; 17610e49de52SMarkus Armbruster bs->dev_opaque = opaque; 17620e49de52SMarkus Armbruster } 17630e49de52SMarkus Armbruster 176432c81a4aSPaolo Bonzini void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 176532c81a4aSPaolo Bonzini enum MonitorEvent ev, 17661ceee0d5SPaolo Bonzini BlockErrorAction action, bool is_read) 1767329c0a48SLuiz Capitulino { 1768329c0a48SLuiz Capitulino QObject *data; 1769329c0a48SLuiz Capitulino const char *action_str; 1770329c0a48SLuiz Capitulino 1771329c0a48SLuiz Capitulino switch (action) { 1772329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1773329c0a48SLuiz Capitulino action_str = "report"; 1774329c0a48SLuiz Capitulino break; 1775329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1776329c0a48SLuiz Capitulino action_str = "ignore"; 1777329c0a48SLuiz Capitulino break; 1778329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1779329c0a48SLuiz Capitulino action_str = "stop"; 1780329c0a48SLuiz Capitulino break; 1781329c0a48SLuiz Capitulino default: 1782329c0a48SLuiz Capitulino abort(); 1783329c0a48SLuiz Capitulino } 1784329c0a48SLuiz Capitulino 1785329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1786329c0a48SLuiz Capitulino bdrv->device_name, 1787329c0a48SLuiz Capitulino action_str, 1788329c0a48SLuiz Capitulino is_read ? "read" : "write"); 178932c81a4aSPaolo Bonzini monitor_protocol_event(ev, data); 1790329c0a48SLuiz Capitulino 1791329c0a48SLuiz Capitulino qobject_decref(data); 1792329c0a48SLuiz Capitulino } 1793329c0a48SLuiz Capitulino 17946f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 17956f382ed2SLuiz Capitulino { 17966f382ed2SLuiz Capitulino QObject *data; 17976f382ed2SLuiz Capitulino 17986f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 17996f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 18006f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 18016f382ed2SLuiz Capitulino 18026f382ed2SLuiz Capitulino qobject_decref(data); 18036f382ed2SLuiz Capitulino } 18046f382ed2SLuiz Capitulino 18057d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 18060e49de52SMarkus Armbruster { 1807145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 18086f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 18097d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 18106f382ed2SLuiz Capitulino if (tray_was_closed) { 18116f382ed2SLuiz Capitulino /* tray open */ 18126f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 18136f382ed2SLuiz Capitulino } 18146f382ed2SLuiz Capitulino if (load) { 18156f382ed2SLuiz Capitulino /* tray close */ 18166f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 18176f382ed2SLuiz Capitulino } 1818145feb17SMarkus Armbruster } 1819145feb17SMarkus Armbruster } 1820145feb17SMarkus Armbruster 18212c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 18222c6942faSMarkus Armbruster { 18232c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 18242c6942faSMarkus Armbruster } 18252c6942faSMarkus Armbruster 1826025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1827025ccaa7SPaolo Bonzini { 1828025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1829025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1830025ccaa7SPaolo Bonzini } 1831025ccaa7SPaolo Bonzini } 1832025ccaa7SPaolo Bonzini 1833e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1834e4def80bSMarkus Armbruster { 1835e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1836e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1837e4def80bSMarkus Armbruster } 1838e4def80bSMarkus Armbruster return false; 1839e4def80bSMarkus Armbruster } 1840e4def80bSMarkus Armbruster 1841145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1842145feb17SMarkus Armbruster { 1843145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1844145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 18450e49de52SMarkus Armbruster } 18460e49de52SMarkus Armbruster } 18470e49de52SMarkus Armbruster 1848f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1849f107639aSMarkus Armbruster { 1850f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1851f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1852f107639aSMarkus Armbruster } 1853f107639aSMarkus Armbruster return false; 1854f107639aSMarkus Armbruster } 1855f107639aSMarkus Armbruster 1856e97fc193Saliguori /* 1857e97fc193Saliguori * Run consistency checks on an image 1858e97fc193Saliguori * 1859e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1860a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1861e076f338SKevin Wolf * check are stored in res. 1862e97fc193Saliguori */ 18634534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 1864e97fc193Saliguori { 1865e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1866e97fc193Saliguori return -ENOTSUP; 1867e97fc193Saliguori } 1868e97fc193Saliguori 1869e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 18704534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 1871e97fc193Saliguori } 1872e97fc193Saliguori 18738a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 18748a426614SKevin Wolf 187533e3963eSbellard /* commit COW file into the raw image */ 187633e3963eSbellard int bdrv_commit(BlockDriverState *bs) 187733e3963eSbellard { 187819cb3738Sbellard BlockDriver *drv = bs->drv; 18798a426614SKevin Wolf int64_t sector, total_sectors; 18808a426614SKevin Wolf int n, ro, open_flags; 18810bce597dSJeff Cody int ret = 0; 18828a426614SKevin Wolf uint8_t *buf; 1883c2cba3d9SJim Meyering char filename[PATH_MAX]; 188433e3963eSbellard 188519cb3738Sbellard if (!drv) 188619cb3738Sbellard return -ENOMEDIUM; 188733e3963eSbellard 18884dca4b63SNaphtali Sprei if (!bs->backing_hd) { 18894dca4b63SNaphtali Sprei return -ENOTSUP; 18904dca4b63SNaphtali Sprei } 18914dca4b63SNaphtali Sprei 18922d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 18932d3735d3SStefan Hajnoczi return -EBUSY; 18942d3735d3SStefan Hajnoczi } 18952d3735d3SStefan Hajnoczi 18964dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 1897c2cba3d9SJim Meyering /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ 1898c2cba3d9SJim Meyering pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); 18994dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 19004dca4b63SNaphtali Sprei 19014dca4b63SNaphtali Sprei if (ro) { 19020bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 19030bce597dSJeff Cody return -EACCES; 19044dca4b63SNaphtali Sprei } 1905ea2384d3Sbellard } 1906ea2384d3Sbellard 19076ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 19087267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 19098a426614SKevin Wolf 19108a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 1911d663640cSPaolo Bonzini ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); 1912d663640cSPaolo Bonzini if (ret < 0) { 1913d663640cSPaolo Bonzini goto ro_cleanup; 1914d663640cSPaolo Bonzini } 1915d663640cSPaolo Bonzini if (ret) { 19168a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 19174dca4b63SNaphtali Sprei ret = -EIO; 19184dca4b63SNaphtali Sprei goto ro_cleanup; 191933e3963eSbellard } 192033e3963eSbellard 19218a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 19224dca4b63SNaphtali Sprei ret = -EIO; 19234dca4b63SNaphtali Sprei goto ro_cleanup; 192433e3963eSbellard } 192533e3963eSbellard } 192633e3963eSbellard } 192795389c86Sbellard 19281d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 19291d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 19301d44952fSChristoph Hellwig bdrv_flush(bs); 19311d44952fSChristoph Hellwig } 193295389c86Sbellard 19333f5075aeSChristoph Hellwig /* 19343f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 19353f5075aeSChristoph Hellwig * stable on disk. 19363f5075aeSChristoph Hellwig */ 19373f5075aeSChristoph Hellwig if (bs->backing_hd) 19383f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 19394dca4b63SNaphtali Sprei 19404dca4b63SNaphtali Sprei ro_cleanup: 19417267c094SAnthony Liguori g_free(buf); 19424dca4b63SNaphtali Sprei 19434dca4b63SNaphtali Sprei if (ro) { 19440bce597dSJeff Cody /* ignoring error return here */ 19450bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 19464dca4b63SNaphtali Sprei } 19474dca4b63SNaphtali Sprei 19481d44952fSChristoph Hellwig return ret; 194933e3963eSbellard } 195033e3963eSbellard 1951e8877497SStefan Hajnoczi int bdrv_commit_all(void) 19526ab4b5abSMarkus Armbruster { 19536ab4b5abSMarkus Armbruster BlockDriverState *bs; 19546ab4b5abSMarkus Armbruster 19556ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1956272d2d8eSJeff Cody if (bs->drv && bs->backing_hd) { 1957e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1958e8877497SStefan Hajnoczi if (ret < 0) { 1959e8877497SStefan Hajnoczi return ret; 19606ab4b5abSMarkus Armbruster } 19616ab4b5abSMarkus Armbruster } 1962272d2d8eSJeff Cody } 1963e8877497SStefan Hajnoczi return 0; 1964e8877497SStefan Hajnoczi } 19656ab4b5abSMarkus Armbruster 1966dbffbdcfSStefan Hajnoczi /** 1967dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1968dbffbdcfSStefan Hajnoczi * 1969dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1970dbffbdcfSStefan Hajnoczi */ 1971dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1972dbffbdcfSStefan Hajnoczi { 1973dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1974f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1975dbffbdcfSStefan Hajnoczi } 1976dbffbdcfSStefan Hajnoczi 1977dbffbdcfSStefan Hajnoczi /** 1978dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1979dbffbdcfSStefan Hajnoczi */ 1980dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1981dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1982dbffbdcfSStefan Hajnoczi int64_t sector_num, 1983dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1984dbffbdcfSStefan Hajnoczi { 1985dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1986dbffbdcfSStefan Hajnoczi .bs = bs, 1987dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1988dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1989dbffbdcfSStefan Hajnoczi .is_write = is_write, 19905f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1991dbffbdcfSStefan Hajnoczi }; 1992dbffbdcfSStefan Hajnoczi 1993f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1994f4658285SStefan Hajnoczi 1995dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 1996dbffbdcfSStefan Hajnoczi } 1997dbffbdcfSStefan Hajnoczi 1998d83947acSStefan Hajnoczi /** 1999d83947acSStefan Hajnoczi * Round a region to cluster boundaries 2000d83947acSStefan Hajnoczi */ 2001343bded4SPaolo Bonzini void bdrv_round_to_clusters(BlockDriverState *bs, 2002d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 2003d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 2004d83947acSStefan Hajnoczi int *cluster_nb_sectors) 2005d83947acSStefan Hajnoczi { 2006d83947acSStefan Hajnoczi BlockDriverInfo bdi; 2007d83947acSStefan Hajnoczi 2008d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 2009d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 2010d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 2011d83947acSStefan Hajnoczi } else { 2012d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 2013d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 2014d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 2015d83947acSStefan Hajnoczi nb_sectors, c); 2016d83947acSStefan Hajnoczi } 2017d83947acSStefan Hajnoczi } 2018d83947acSStefan Hajnoczi 2019f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 2020f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 2021d83947acSStefan Hajnoczi /* aaaa bbbb */ 2022d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 2023d83947acSStefan Hajnoczi return false; 2024d83947acSStefan Hajnoczi } 2025d83947acSStefan Hajnoczi /* bbbb aaaa */ 2026d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 2027d83947acSStefan Hajnoczi return false; 2028d83947acSStefan Hajnoczi } 2029d83947acSStefan Hajnoczi return true; 2030f4658285SStefan Hajnoczi } 2031f4658285SStefan Hajnoczi 2032f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 2033f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 2034f4658285SStefan Hajnoczi { 2035f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 2036d83947acSStefan Hajnoczi int64_t cluster_sector_num; 2037d83947acSStefan Hajnoczi int cluster_nb_sectors; 2038f4658285SStefan Hajnoczi bool retry; 2039f4658285SStefan Hajnoczi 2040d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 2041d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 2042d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 2043d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 2044d83947acSStefan Hajnoczi * interleave between them. 2045d83947acSStefan Hajnoczi */ 2046343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2047d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2048d83947acSStefan Hajnoczi 2049f4658285SStefan Hajnoczi do { 2050f4658285SStefan Hajnoczi retry = false; 2051f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 2052d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 2053d83947acSStefan Hajnoczi cluster_nb_sectors)) { 20545f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 20555f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 20565f8b6491SStefan Hajnoczi * never happen since it means deadlock. 20575f8b6491SStefan Hajnoczi */ 20585f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 20595f8b6491SStefan Hajnoczi 2060f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 2061f4658285SStefan Hajnoczi retry = true; 2062f4658285SStefan Hajnoczi break; 2063f4658285SStefan Hajnoczi } 2064f4658285SStefan Hajnoczi } 2065f4658285SStefan Hajnoczi } while (retry); 2066f4658285SStefan Hajnoczi } 2067f4658285SStefan Hajnoczi 2068756e6736SKevin Wolf /* 2069756e6736SKevin Wolf * Return values: 2070756e6736SKevin Wolf * 0 - success 2071756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 2072756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 2073756e6736SKevin Wolf * image file header 2074756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 2075756e6736SKevin Wolf */ 2076756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 2077756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 2078756e6736SKevin Wolf { 2079756e6736SKevin Wolf BlockDriver *drv = bs->drv; 2080469ef350SPaolo Bonzini int ret; 2081756e6736SKevin Wolf 20825f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 20835f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 20845f377794SPaolo Bonzini return -EINVAL; 20855f377794SPaolo Bonzini } 20865f377794SPaolo Bonzini 2087756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 2088469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 2089756e6736SKevin Wolf } else { 2090469ef350SPaolo Bonzini ret = -ENOTSUP; 2091756e6736SKevin Wolf } 2092469ef350SPaolo Bonzini 2093469ef350SPaolo Bonzini if (ret == 0) { 2094469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 2095469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 2096469ef350SPaolo Bonzini } 2097469ef350SPaolo Bonzini return ret; 2098756e6736SKevin Wolf } 2099756e6736SKevin Wolf 21006ebdcee2SJeff Cody /* 21016ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 21026ebdcee2SJeff Cody * 21036ebdcee2SJeff Cody * active is the current topmost image. 21046ebdcee2SJeff Cody * 21056ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 21066ebdcee2SJeff Cody * or if active == bs. 21076ebdcee2SJeff Cody */ 21086ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 21096ebdcee2SJeff Cody BlockDriverState *bs) 21106ebdcee2SJeff Cody { 21116ebdcee2SJeff Cody BlockDriverState *overlay = NULL; 21126ebdcee2SJeff Cody BlockDriverState *intermediate; 21136ebdcee2SJeff Cody 21146ebdcee2SJeff Cody assert(active != NULL); 21156ebdcee2SJeff Cody assert(bs != NULL); 21166ebdcee2SJeff Cody 21176ebdcee2SJeff Cody /* if bs is the same as active, then by definition it has no overlay 21186ebdcee2SJeff Cody */ 21196ebdcee2SJeff Cody if (active == bs) { 21206ebdcee2SJeff Cody return NULL; 21216ebdcee2SJeff Cody } 21226ebdcee2SJeff Cody 21236ebdcee2SJeff Cody intermediate = active; 21246ebdcee2SJeff Cody while (intermediate->backing_hd) { 21256ebdcee2SJeff Cody if (intermediate->backing_hd == bs) { 21266ebdcee2SJeff Cody overlay = intermediate; 21276ebdcee2SJeff Cody break; 21286ebdcee2SJeff Cody } 21296ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 21306ebdcee2SJeff Cody } 21316ebdcee2SJeff Cody 21326ebdcee2SJeff Cody return overlay; 21336ebdcee2SJeff Cody } 21346ebdcee2SJeff Cody 21356ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 21366ebdcee2SJeff Cody BlockDriverState *bs; 21376ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 21386ebdcee2SJeff Cody } BlkIntermediateStates; 21396ebdcee2SJeff Cody 21406ebdcee2SJeff Cody 21416ebdcee2SJeff Cody /* 21426ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 21436ebdcee2SJeff Cody * above 'top' to have base as its backing file. 21446ebdcee2SJeff Cody * 21456ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 21466ebdcee2SJeff Cody * information in 'bs' can be properly updated. 21476ebdcee2SJeff Cody * 21486ebdcee2SJeff Cody * E.g., this will convert the following chain: 21496ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 21506ebdcee2SJeff Cody * 21516ebdcee2SJeff Cody * to 21526ebdcee2SJeff Cody * 21536ebdcee2SJeff Cody * bottom <- base <- active 21546ebdcee2SJeff Cody * 21556ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 21566ebdcee2SJeff Cody * 21576ebdcee2SJeff Cody * base <- intermediate <- top <- active 21586ebdcee2SJeff Cody * 21596ebdcee2SJeff Cody * to 21606ebdcee2SJeff Cody * 21616ebdcee2SJeff Cody * base <- active 21626ebdcee2SJeff Cody * 21636ebdcee2SJeff Cody * Error conditions: 21646ebdcee2SJeff Cody * if active == top, that is considered an error 21656ebdcee2SJeff Cody * 21666ebdcee2SJeff Cody */ 21676ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 21686ebdcee2SJeff Cody BlockDriverState *base) 21696ebdcee2SJeff Cody { 21706ebdcee2SJeff Cody BlockDriverState *intermediate; 21716ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 21726ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 21736ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 21746ebdcee2SJeff Cody int ret = -EIO; 21756ebdcee2SJeff Cody 21766ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 21776ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 21786ebdcee2SJeff Cody 21796ebdcee2SJeff Cody if (!top->drv || !base->drv) { 21806ebdcee2SJeff Cody goto exit; 21816ebdcee2SJeff Cody } 21826ebdcee2SJeff Cody 21836ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 21846ebdcee2SJeff Cody 21856ebdcee2SJeff Cody if (new_top_bs == NULL) { 21866ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 21876ebdcee2SJeff Cody goto exit; 21886ebdcee2SJeff Cody } 21896ebdcee2SJeff Cody 21906ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 21916ebdcee2SJeff Cody * to do, no intermediate images */ 21926ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 21936ebdcee2SJeff Cody ret = 0; 21946ebdcee2SJeff Cody goto exit; 21956ebdcee2SJeff Cody } 21966ebdcee2SJeff Cody 21976ebdcee2SJeff Cody intermediate = top; 21986ebdcee2SJeff Cody 21996ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 22006ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 22016ebdcee2SJeff Cody */ 22026ebdcee2SJeff Cody while (intermediate) { 22036ebdcee2SJeff Cody intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); 22046ebdcee2SJeff Cody intermediate_state->bs = intermediate; 22056ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 22066ebdcee2SJeff Cody 22076ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 22086ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 22096ebdcee2SJeff Cody break; 22106ebdcee2SJeff Cody } 22116ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 22126ebdcee2SJeff Cody } 22136ebdcee2SJeff Cody if (base_bs == NULL) { 22146ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 22156ebdcee2SJeff Cody * unravel everything, and exit with error */ 22166ebdcee2SJeff Cody goto exit; 22176ebdcee2SJeff Cody } 22186ebdcee2SJeff Cody 22196ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 22206ebdcee2SJeff Cody ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, 22216ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 22226ebdcee2SJeff Cody if (ret) { 22236ebdcee2SJeff Cody goto exit; 22246ebdcee2SJeff Cody } 22256ebdcee2SJeff Cody new_top_bs->backing_hd = base_bs; 22266ebdcee2SJeff Cody 22276ebdcee2SJeff Cody 22286ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 22296ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 22306ebdcee2SJeff Cody intermediate_state->bs->backing_hd = NULL; 22314f6fd349SFam Zheng bdrv_unref(intermediate_state->bs); 22326ebdcee2SJeff Cody } 22336ebdcee2SJeff Cody ret = 0; 22346ebdcee2SJeff Cody 22356ebdcee2SJeff Cody exit: 22366ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 22376ebdcee2SJeff Cody g_free(intermediate_state); 22386ebdcee2SJeff Cody } 22396ebdcee2SJeff Cody return ret; 22406ebdcee2SJeff Cody } 22416ebdcee2SJeff Cody 22426ebdcee2SJeff Cody 224371d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 224471d0770cSaliguori size_t size) 224571d0770cSaliguori { 224671d0770cSaliguori int64_t len; 224771d0770cSaliguori 224871d0770cSaliguori if (!bdrv_is_inserted(bs)) 224971d0770cSaliguori return -ENOMEDIUM; 225071d0770cSaliguori 225171d0770cSaliguori if (bs->growable) 225271d0770cSaliguori return 0; 225371d0770cSaliguori 225471d0770cSaliguori len = bdrv_getlength(bs); 225571d0770cSaliguori 2256fbb7b4e0SKevin Wolf if (offset < 0) 2257fbb7b4e0SKevin Wolf return -EIO; 2258fbb7b4e0SKevin Wolf 2259fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 226071d0770cSaliguori return -EIO; 226171d0770cSaliguori 226271d0770cSaliguori return 0; 226371d0770cSaliguori } 226471d0770cSaliguori 226571d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 226671d0770cSaliguori int nb_sectors) 226771d0770cSaliguori { 2268eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 2269eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 227071d0770cSaliguori } 227171d0770cSaliguori 22721c9805a3SStefan Hajnoczi typedef struct RwCo { 22731c9805a3SStefan Hajnoczi BlockDriverState *bs; 22741c9805a3SStefan Hajnoczi int64_t sector_num; 22751c9805a3SStefan Hajnoczi int nb_sectors; 22761c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 22771c9805a3SStefan Hajnoczi bool is_write; 22781c9805a3SStefan Hajnoczi int ret; 22794105eaaaSPeter Lieven BdrvRequestFlags flags; 22801c9805a3SStefan Hajnoczi } RwCo; 22811c9805a3SStefan Hajnoczi 22821c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 2283fc01f7e7Sbellard { 22841c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 2285fc01f7e7Sbellard 22861c9805a3SStefan Hajnoczi if (!rwco->is_write) { 22871c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 22884105eaaaSPeter Lieven rwco->nb_sectors, rwco->qiov, 22894105eaaaSPeter Lieven rwco->flags); 22901c9805a3SStefan Hajnoczi } else { 22911c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 22924105eaaaSPeter Lieven rwco->nb_sectors, rwco->qiov, 22934105eaaaSPeter Lieven rwco->flags); 22941c9805a3SStefan Hajnoczi } 22951c9805a3SStefan Hajnoczi } 2296e7a8a783SKevin Wolf 22971c9805a3SStefan Hajnoczi /* 22988d3b1a2dSKevin Wolf * Process a vectored synchronous request using coroutines 22991c9805a3SStefan Hajnoczi */ 23008d3b1a2dSKevin Wolf static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, 23014105eaaaSPeter Lieven QEMUIOVector *qiov, bool is_write, 23024105eaaaSPeter Lieven BdrvRequestFlags flags) 23031c9805a3SStefan Hajnoczi { 23041c9805a3SStefan Hajnoczi Coroutine *co; 23051c9805a3SStefan Hajnoczi RwCo rwco = { 23061c9805a3SStefan Hajnoczi .bs = bs, 23071c9805a3SStefan Hajnoczi .sector_num = sector_num, 23088d3b1a2dSKevin Wolf .nb_sectors = qiov->size >> BDRV_SECTOR_BITS, 23098d3b1a2dSKevin Wolf .qiov = qiov, 23101c9805a3SStefan Hajnoczi .is_write = is_write, 23111c9805a3SStefan Hajnoczi .ret = NOT_DONE, 23124105eaaaSPeter Lieven .flags = flags, 23131c9805a3SStefan Hajnoczi }; 23148d3b1a2dSKevin Wolf assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0); 23151c9805a3SStefan Hajnoczi 2316498e386cSZhi Yong Wu /** 2317498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 2318498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 2319498e386cSZhi Yong Wu * if it has been enabled. 2320498e386cSZhi Yong Wu */ 2321498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 2322498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 2323498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 2324498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 2325498e386cSZhi Yong Wu } 2326498e386cSZhi Yong Wu 23271c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 23281c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 23291c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 23301c9805a3SStefan Hajnoczi } else { 23311c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 23321c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 23331c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 23341c9805a3SStefan Hajnoczi qemu_aio_wait(); 23351c9805a3SStefan Hajnoczi } 23361c9805a3SStefan Hajnoczi } 23371c9805a3SStefan Hajnoczi return rwco.ret; 2338e7a8a783SKevin Wolf } 2339e7a8a783SKevin Wolf 23408d3b1a2dSKevin Wolf /* 23418d3b1a2dSKevin Wolf * Process a synchronous request using coroutines 23428d3b1a2dSKevin Wolf */ 23438d3b1a2dSKevin Wolf static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 23444105eaaaSPeter Lieven int nb_sectors, bool is_write, BdrvRequestFlags flags) 23458d3b1a2dSKevin Wolf { 23468d3b1a2dSKevin Wolf QEMUIOVector qiov; 23478d3b1a2dSKevin Wolf struct iovec iov = { 23488d3b1a2dSKevin Wolf .iov_base = (void *)buf, 23498d3b1a2dSKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 23508d3b1a2dSKevin Wolf }; 23518d3b1a2dSKevin Wolf 23528d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 23534105eaaaSPeter Lieven return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags); 23548d3b1a2dSKevin Wolf } 23558d3b1a2dSKevin Wolf 23561c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 23571c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 23581c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 23591c9805a3SStefan Hajnoczi { 23604105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0); 236183f64091Sbellard } 2362fc01f7e7Sbellard 236307d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 236407d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 236507d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 236607d27a44SMarkus Armbruster { 236707d27a44SMarkus Armbruster bool enabled; 236807d27a44SMarkus Armbruster int ret; 236907d27a44SMarkus Armbruster 237007d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 237107d27a44SMarkus Armbruster bs->io_limits_enabled = false; 23724e7395e8SPeter Lieven ret = bdrv_read(bs, sector_num, buf, nb_sectors); 237307d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 237407d27a44SMarkus Armbruster return ret; 237507d27a44SMarkus Armbruster } 237607d27a44SMarkus Armbruster 237719cb3738Sbellard /* Return < 0 if error. Important errors are: 237819cb3738Sbellard -EIO generic I/O error (may happen for all errors) 237919cb3738Sbellard -ENOMEDIUM No media inserted. 238019cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 238119cb3738Sbellard -EACCES Trying to write a read-only device 238219cb3738Sbellard */ 2383fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 2384fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 2385fc01f7e7Sbellard { 23864105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0); 238783f64091Sbellard } 238883f64091Sbellard 23898d3b1a2dSKevin Wolf int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov) 23908d3b1a2dSKevin Wolf { 23914105eaaaSPeter Lieven return bdrv_rwv_co(bs, sector_num, qiov, true, 0); 23924105eaaaSPeter Lieven } 23934105eaaaSPeter Lieven 2394aa7bfbffSPeter Lieven int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, 2395aa7bfbffSPeter Lieven int nb_sectors, BdrvRequestFlags flags) 23964105eaaaSPeter Lieven { 23974105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true, 2398aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 23998d3b1a2dSKevin Wolf } 24008d3b1a2dSKevin Wolf 2401d75cbb5eSPeter Lieven /* 2402d75cbb5eSPeter Lieven * Completely zero out a block device with the help of bdrv_write_zeroes. 2403d75cbb5eSPeter Lieven * The operation is sped up by checking the block status and only writing 2404d75cbb5eSPeter Lieven * zeroes to the device if they currently do not return zeroes. Optional 2405d75cbb5eSPeter Lieven * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP). 2406d75cbb5eSPeter Lieven * 2407d75cbb5eSPeter Lieven * Returns < 0 on error, 0 on success. For error codes see bdrv_write(). 2408d75cbb5eSPeter Lieven */ 2409d75cbb5eSPeter Lieven int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) 2410d75cbb5eSPeter Lieven { 2411d75cbb5eSPeter Lieven int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE; 2412d75cbb5eSPeter Lieven int64_t ret, nb_sectors, sector_num = 0; 2413d75cbb5eSPeter Lieven int n; 2414d75cbb5eSPeter Lieven 2415d75cbb5eSPeter Lieven for (;;) { 2416d75cbb5eSPeter Lieven nb_sectors = target_size - sector_num; 2417d75cbb5eSPeter Lieven if (nb_sectors <= 0) { 2418d75cbb5eSPeter Lieven return 0; 2419d75cbb5eSPeter Lieven } 2420d75cbb5eSPeter Lieven if (nb_sectors > INT_MAX) { 2421d75cbb5eSPeter Lieven nb_sectors = INT_MAX; 2422d75cbb5eSPeter Lieven } 2423d75cbb5eSPeter Lieven ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n); 2424d75cbb5eSPeter Lieven if (ret & BDRV_BLOCK_ZERO) { 2425d75cbb5eSPeter Lieven sector_num += n; 2426d75cbb5eSPeter Lieven continue; 2427d75cbb5eSPeter Lieven } 2428d75cbb5eSPeter Lieven ret = bdrv_write_zeroes(bs, sector_num, n, flags); 2429d75cbb5eSPeter Lieven if (ret < 0) { 2430d75cbb5eSPeter Lieven error_report("error writing zeroes at sector %" PRId64 ": %s", 2431d75cbb5eSPeter Lieven sector_num, strerror(-ret)); 2432d75cbb5eSPeter Lieven return ret; 2433d75cbb5eSPeter Lieven } 2434d75cbb5eSPeter Lieven sector_num += n; 2435d75cbb5eSPeter Lieven } 2436d75cbb5eSPeter Lieven } 2437d75cbb5eSPeter Lieven 2438eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 2439eda578e5Saliguori void *buf, int count1) 244083f64091Sbellard { 24416ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 244283f64091Sbellard int len, nb_sectors, count; 244383f64091Sbellard int64_t sector_num; 24449a8c4cceSKevin Wolf int ret; 244583f64091Sbellard 244683f64091Sbellard count = count1; 244783f64091Sbellard /* first read to align to sector start */ 24486ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 244983f64091Sbellard if (len > count) 245083f64091Sbellard len = count; 24516ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 245283f64091Sbellard if (len > 0) { 24539a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 24549a8c4cceSKevin Wolf return ret; 24556ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 245683f64091Sbellard count -= len; 245783f64091Sbellard if (count == 0) 245883f64091Sbellard return count1; 245983f64091Sbellard sector_num++; 246083f64091Sbellard buf += len; 246183f64091Sbellard } 246283f64091Sbellard 246383f64091Sbellard /* read the sectors "in place" */ 24646ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 246583f64091Sbellard if (nb_sectors > 0) { 24669a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 24679a8c4cceSKevin Wolf return ret; 246883f64091Sbellard sector_num += nb_sectors; 24696ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 247083f64091Sbellard buf += len; 247183f64091Sbellard count -= len; 247283f64091Sbellard } 247383f64091Sbellard 247483f64091Sbellard /* add data from the last sector */ 247583f64091Sbellard if (count > 0) { 24769a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 24779a8c4cceSKevin Wolf return ret; 247883f64091Sbellard memcpy(buf, tmp_buf, count); 247983f64091Sbellard } 248083f64091Sbellard return count1; 248183f64091Sbellard } 248283f64091Sbellard 24838d3b1a2dSKevin Wolf int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov) 248483f64091Sbellard { 24856ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 248683f64091Sbellard int len, nb_sectors, count; 248783f64091Sbellard int64_t sector_num; 24889a8c4cceSKevin Wolf int ret; 248983f64091Sbellard 24908d3b1a2dSKevin Wolf count = qiov->size; 24918d3b1a2dSKevin Wolf 249283f64091Sbellard /* first write to align to sector start */ 24936ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 249483f64091Sbellard if (len > count) 249583f64091Sbellard len = count; 24966ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 249783f64091Sbellard if (len > 0) { 24989a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 24999a8c4cceSKevin Wolf return ret; 25008d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), 25018d3b1a2dSKevin Wolf len); 25029a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 25039a8c4cceSKevin Wolf return ret; 250483f64091Sbellard count -= len; 250583f64091Sbellard if (count == 0) 25068d3b1a2dSKevin Wolf return qiov->size; 250783f64091Sbellard sector_num++; 250883f64091Sbellard } 250983f64091Sbellard 251083f64091Sbellard /* write the sectors "in place" */ 25116ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 251283f64091Sbellard if (nb_sectors > 0) { 25138d3b1a2dSKevin Wolf QEMUIOVector qiov_inplace; 25148d3b1a2dSKevin Wolf 25158d3b1a2dSKevin Wolf qemu_iovec_init(&qiov_inplace, qiov->niov); 25168d3b1a2dSKevin Wolf qemu_iovec_concat(&qiov_inplace, qiov, len, 25178d3b1a2dSKevin Wolf nb_sectors << BDRV_SECTOR_BITS); 25188d3b1a2dSKevin Wolf ret = bdrv_writev(bs, sector_num, &qiov_inplace); 25198d3b1a2dSKevin Wolf qemu_iovec_destroy(&qiov_inplace); 25208d3b1a2dSKevin Wolf if (ret < 0) { 25219a8c4cceSKevin Wolf return ret; 25228d3b1a2dSKevin Wolf } 25238d3b1a2dSKevin Wolf 252483f64091Sbellard sector_num += nb_sectors; 25256ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 252683f64091Sbellard count -= len; 252783f64091Sbellard } 252883f64091Sbellard 252983f64091Sbellard /* add data from the last sector */ 253083f64091Sbellard if (count > 0) { 25319a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 25329a8c4cceSKevin Wolf return ret; 25338d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count); 25349a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 25359a8c4cceSKevin Wolf return ret; 253683f64091Sbellard } 25378d3b1a2dSKevin Wolf return qiov->size; 25388d3b1a2dSKevin Wolf } 25398d3b1a2dSKevin Wolf 25408d3b1a2dSKevin Wolf int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 25418d3b1a2dSKevin Wolf const void *buf, int count1) 25428d3b1a2dSKevin Wolf { 25438d3b1a2dSKevin Wolf QEMUIOVector qiov; 25448d3b1a2dSKevin Wolf struct iovec iov = { 25458d3b1a2dSKevin Wolf .iov_base = (void *) buf, 25468d3b1a2dSKevin Wolf .iov_len = count1, 25478d3b1a2dSKevin Wolf }; 25488d3b1a2dSKevin Wolf 25498d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 25508d3b1a2dSKevin Wolf return bdrv_pwritev(bs, offset, &qiov); 255183f64091Sbellard } 255283f64091Sbellard 2553f08145feSKevin Wolf /* 2554f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 2555f08145feSKevin Wolf * request (acts as a barrier) 2556f08145feSKevin Wolf * 2557f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 2558f08145feSKevin Wolf */ 2559f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 2560f08145feSKevin Wolf const void *buf, int count) 2561f08145feSKevin Wolf { 2562f08145feSKevin Wolf int ret; 2563f08145feSKevin Wolf 2564f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 2565f08145feSKevin Wolf if (ret < 0) { 2566f08145feSKevin Wolf return ret; 2567f08145feSKevin Wolf } 2568f08145feSKevin Wolf 2569f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 2570f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 2571f08145feSKevin Wolf bdrv_flush(bs); 2572f08145feSKevin Wolf } 2573f08145feSKevin Wolf 2574f08145feSKevin Wolf return 0; 2575f08145feSKevin Wolf } 2576f08145feSKevin Wolf 2577470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 2578ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2579ab185921SStefan Hajnoczi { 2580ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 2581ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 2582ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 2583ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 2584ab185921SStefan Hajnoczi */ 2585ab185921SStefan Hajnoczi void *bounce_buffer; 2586ab185921SStefan Hajnoczi 258779c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 2588ab185921SStefan Hajnoczi struct iovec iov; 2589ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 2590ab185921SStefan Hajnoczi int64_t cluster_sector_num; 2591ab185921SStefan Hajnoczi int cluster_nb_sectors; 2592ab185921SStefan Hajnoczi size_t skip_bytes; 2593ab185921SStefan Hajnoczi int ret; 2594ab185921SStefan Hajnoczi 2595ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 2596ab185921SStefan Hajnoczi * allocating cluster in the image file. 2597ab185921SStefan Hajnoczi */ 2598343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2599ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2600ab185921SStefan Hajnoczi 2601470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 2602ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 2603ab185921SStefan Hajnoczi 2604ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 2605ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 2606ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 2607ab185921SStefan Hajnoczi 260879c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 2609ab185921SStefan Hajnoczi &bounce_qiov); 2610ab185921SStefan Hajnoczi if (ret < 0) { 2611ab185921SStefan Hajnoczi goto err; 2612ab185921SStefan Hajnoczi } 2613ab185921SStefan Hajnoczi 261479c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 261579c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 2616621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 2617aa7bfbffSPeter Lieven cluster_nb_sectors, 0); 261879c053bdSStefan Hajnoczi } else { 2619f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 2620f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 2621f05fa4adSPaolo Bonzini */ 262279c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 2623ab185921SStefan Hajnoczi &bounce_qiov); 262479c053bdSStefan Hajnoczi } 262579c053bdSStefan Hajnoczi 2626ab185921SStefan Hajnoczi if (ret < 0) { 2627ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 2628ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 2629ab185921SStefan Hajnoczi * Simply report it in all cases. 2630ab185921SStefan Hajnoczi */ 2631ab185921SStefan Hajnoczi goto err; 2632ab185921SStefan Hajnoczi } 2633ab185921SStefan Hajnoczi 2634ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 263503396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 2636ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 2637ab185921SStefan Hajnoczi 2638ab185921SStefan Hajnoczi err: 2639ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 2640ab185921SStefan Hajnoczi return ret; 2641ab185921SStefan Hajnoczi } 2642ab185921SStefan Hajnoczi 2643c5fbe571SStefan Hajnoczi /* 2644c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 2645c5fbe571SStefan Hajnoczi */ 2646c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 2647470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2648470c0504SStefan Hajnoczi BdrvRequestFlags flags) 2649da1fa91dSKevin Wolf { 2650da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 2651dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 2652dbffbdcfSStefan Hajnoczi int ret; 2653da1fa91dSKevin Wolf 2654da1fa91dSKevin Wolf if (!drv) { 2655da1fa91dSKevin Wolf return -ENOMEDIUM; 2656da1fa91dSKevin Wolf } 2657da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2658da1fa91dSKevin Wolf return -EIO; 2659da1fa91dSKevin Wolf } 2660da1fa91dSKevin Wolf 2661f4658285SStefan Hajnoczi if (bs->copy_on_read) { 2662470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 2663470c0504SStefan Hajnoczi } 2664470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2665470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 2666470c0504SStefan Hajnoczi } 2667470c0504SStefan Hajnoczi 2668470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2669f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2670f4658285SStefan Hajnoczi } 2671f4658285SStefan Hajnoczi 2672cc0681c4SBenoît Canet /* throttling disk I/O */ 2673cc0681c4SBenoît Canet if (bs->io_limits_enabled) { 2674cc0681c4SBenoît Canet bdrv_io_limits_intercept(bs, nb_sectors, false); 2675cc0681c4SBenoît Canet } 2676cc0681c4SBenoît Canet 2677dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 2678ab185921SStefan Hajnoczi 2679470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2680ab185921SStefan Hajnoczi int pnum; 2681ab185921SStefan Hajnoczi 2682bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum); 2683ab185921SStefan Hajnoczi if (ret < 0) { 2684ab185921SStefan Hajnoczi goto out; 2685ab185921SStefan Hajnoczi } 2686ab185921SStefan Hajnoczi 2687ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 2688470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 2689ab185921SStefan Hajnoczi goto out; 2690ab185921SStefan Hajnoczi } 2691ab185921SStefan Hajnoczi } 2692ab185921SStefan Hajnoczi 2693893a8f62SMORITA Kazutaka if (!(bs->zero_beyond_eof && bs->growable)) { 2694dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 2695893a8f62SMORITA Kazutaka } else { 2696893a8f62SMORITA Kazutaka /* Read zeros after EOF of growable BDSes */ 2697893a8f62SMORITA Kazutaka int64_t len, total_sectors, max_nb_sectors; 2698893a8f62SMORITA Kazutaka 2699893a8f62SMORITA Kazutaka len = bdrv_getlength(bs); 2700893a8f62SMORITA Kazutaka if (len < 0) { 2701893a8f62SMORITA Kazutaka ret = len; 2702893a8f62SMORITA Kazutaka goto out; 2703893a8f62SMORITA Kazutaka } 2704893a8f62SMORITA Kazutaka 2705d055a1feSFam Zheng total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE); 2706893a8f62SMORITA Kazutaka max_nb_sectors = MAX(0, total_sectors - sector_num); 2707893a8f62SMORITA Kazutaka if (max_nb_sectors > 0) { 2708893a8f62SMORITA Kazutaka ret = drv->bdrv_co_readv(bs, sector_num, 2709893a8f62SMORITA Kazutaka MIN(nb_sectors, max_nb_sectors), qiov); 2710893a8f62SMORITA Kazutaka } else { 2711893a8f62SMORITA Kazutaka ret = 0; 2712893a8f62SMORITA Kazutaka } 2713893a8f62SMORITA Kazutaka 2714893a8f62SMORITA Kazutaka /* Reading beyond end of file is supposed to produce zeroes */ 2715893a8f62SMORITA Kazutaka if (ret == 0 && total_sectors < sector_num + nb_sectors) { 2716893a8f62SMORITA Kazutaka uint64_t offset = MAX(0, total_sectors - sector_num); 2717893a8f62SMORITA Kazutaka uint64_t bytes = (sector_num + nb_sectors - offset) * 2718893a8f62SMORITA Kazutaka BDRV_SECTOR_SIZE; 2719893a8f62SMORITA Kazutaka qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes); 2720893a8f62SMORITA Kazutaka } 2721893a8f62SMORITA Kazutaka } 2722ab185921SStefan Hajnoczi 2723ab185921SStefan Hajnoczi out: 2724dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2725470c0504SStefan Hajnoczi 2726470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2727470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 2728470c0504SStefan Hajnoczi } 2729470c0504SStefan Hajnoczi 2730dbffbdcfSStefan Hajnoczi return ret; 2731da1fa91dSKevin Wolf } 2732da1fa91dSKevin Wolf 2733c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 2734da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 2735da1fa91dSKevin Wolf { 2736c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 2737da1fa91dSKevin Wolf 2738470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 2739470c0504SStefan Hajnoczi } 2740470c0504SStefan Hajnoczi 2741470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 2742470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2743470c0504SStefan Hajnoczi { 2744470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 2745470c0504SStefan Hajnoczi 2746470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 2747470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 2748c5fbe571SStefan Hajnoczi } 2749c5fbe571SStefan Hajnoczi 2750c31cb707SPeter Lieven /* if no limit is specified in the BlockLimits use a default 2751c31cb707SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 2752c31cb707SPeter Lieven */ 2753c31cb707SPeter Lieven #define MAX_WRITE_ZEROES_DEFAULT 32768 2754c31cb707SPeter Lieven 2755f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 2756aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) 2757f08f2ddaSStefan Hajnoczi { 2758f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 2759f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 2760c31cb707SPeter Lieven struct iovec iov = {0}; 2761c31cb707SPeter Lieven int ret = 0; 2762f08f2ddaSStefan Hajnoczi 2763c31cb707SPeter Lieven int max_write_zeroes = bs->bl.max_write_zeroes ? 2764c31cb707SPeter Lieven bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT; 2765621f0589SKevin Wolf 2766c31cb707SPeter Lieven while (nb_sectors > 0 && !ret) { 2767c31cb707SPeter Lieven int num = nb_sectors; 2768c31cb707SPeter Lieven 2769b8d71c09SPaolo Bonzini /* Align request. Block drivers can expect the "bulk" of the request 2770b8d71c09SPaolo Bonzini * to be aligned. 2771b8d71c09SPaolo Bonzini */ 2772b8d71c09SPaolo Bonzini if (bs->bl.write_zeroes_alignment 2773b8d71c09SPaolo Bonzini && num > bs->bl.write_zeroes_alignment) { 2774b8d71c09SPaolo Bonzini if (sector_num % bs->bl.write_zeroes_alignment != 0) { 2775b8d71c09SPaolo Bonzini /* Make a small request up to the first aligned sector. */ 2776c31cb707SPeter Lieven num = bs->bl.write_zeroes_alignment; 2777c31cb707SPeter Lieven num -= sector_num % bs->bl.write_zeroes_alignment; 2778b8d71c09SPaolo Bonzini } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) { 2779b8d71c09SPaolo Bonzini /* Shorten the request to the last aligned sector. num cannot 2780b8d71c09SPaolo Bonzini * underflow because num > bs->bl.write_zeroes_alignment. 2781b8d71c09SPaolo Bonzini */ 2782b8d71c09SPaolo Bonzini num -= (sector_num + num) % bs->bl.write_zeroes_alignment; 2783b8d71c09SPaolo Bonzini } 2784c31cb707SPeter Lieven } 2785c31cb707SPeter Lieven 2786c31cb707SPeter Lieven /* limit request size */ 2787c31cb707SPeter Lieven if (num > max_write_zeroes) { 2788c31cb707SPeter Lieven num = max_write_zeroes; 2789c31cb707SPeter Lieven } 2790c31cb707SPeter Lieven 2791c31cb707SPeter Lieven ret = -ENOTSUP; 2792f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 2793f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 2794c31cb707SPeter Lieven ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags); 2795f08f2ddaSStefan Hajnoczi } 2796f08f2ddaSStefan Hajnoczi 2797c31cb707SPeter Lieven if (ret == -ENOTSUP) { 2798f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 2799c31cb707SPeter Lieven iov.iov_len = num * BDRV_SECTOR_SIZE; 2800c31cb707SPeter Lieven if (iov.iov_base == NULL) { 2801b8d71c09SPaolo Bonzini iov.iov_base = qemu_blockalign(bs, num * BDRV_SECTOR_SIZE); 2802b8d71c09SPaolo Bonzini memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE); 2803c31cb707SPeter Lieven } 2804f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 2805f08f2ddaSStefan Hajnoczi 2806c31cb707SPeter Lieven ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov); 2807b8d71c09SPaolo Bonzini 2808b8d71c09SPaolo Bonzini /* Keep bounce buffer around if it is big enough for all 2809b8d71c09SPaolo Bonzini * all future requests. 2810b8d71c09SPaolo Bonzini */ 2811b8d71c09SPaolo Bonzini if (num < max_write_zeroes) { 2812b8d71c09SPaolo Bonzini qemu_vfree(iov.iov_base); 2813b8d71c09SPaolo Bonzini iov.iov_base = NULL; 2814b8d71c09SPaolo Bonzini } 2815c31cb707SPeter Lieven } 2816c31cb707SPeter Lieven 2817c31cb707SPeter Lieven sector_num += num; 2818c31cb707SPeter Lieven nb_sectors -= num; 2819c31cb707SPeter Lieven } 2820f08f2ddaSStefan Hajnoczi 2821f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 2822f08f2ddaSStefan Hajnoczi return ret; 2823f08f2ddaSStefan Hajnoczi } 2824f08f2ddaSStefan Hajnoczi 2825c5fbe571SStefan Hajnoczi /* 2826c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 2827c5fbe571SStefan Hajnoczi */ 2828c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 2829f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2830f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 2831c5fbe571SStefan Hajnoczi { 2832c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 2833dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 28346b7cb247SStefan Hajnoczi int ret; 2835da1fa91dSKevin Wolf 2836da1fa91dSKevin Wolf if (!bs->drv) { 2837da1fa91dSKevin Wolf return -ENOMEDIUM; 2838da1fa91dSKevin Wolf } 2839da1fa91dSKevin Wolf if (bs->read_only) { 2840da1fa91dSKevin Wolf return -EACCES; 2841da1fa91dSKevin Wolf } 2842da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2843da1fa91dSKevin Wolf return -EIO; 2844da1fa91dSKevin Wolf } 2845da1fa91dSKevin Wolf 2846470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2847f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2848f4658285SStefan Hajnoczi } 2849f4658285SStefan Hajnoczi 2850cc0681c4SBenoît Canet /* throttling disk I/O */ 2851cc0681c4SBenoît Canet if (bs->io_limits_enabled) { 2852cc0681c4SBenoît Canet bdrv_io_limits_intercept(bs, nb_sectors, true); 2853cc0681c4SBenoît Canet } 2854cc0681c4SBenoît Canet 2855dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 2856dbffbdcfSStefan Hajnoczi 2857d616b224SStefan Hajnoczi ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); 2858d616b224SStefan Hajnoczi 2859d616b224SStefan Hajnoczi if (ret < 0) { 2860d616b224SStefan Hajnoczi /* Do nothing, write notifier decided to fail this request */ 2861d616b224SStefan Hajnoczi } else if (flags & BDRV_REQ_ZERO_WRITE) { 2862aa7bfbffSPeter Lieven ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags); 2863f08f2ddaSStefan Hajnoczi } else { 28646b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 2865f08f2ddaSStefan Hajnoczi } 28666b7cb247SStefan Hajnoczi 2867f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 2868f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 2869f05fa4adSPaolo Bonzini } 2870f05fa4adSPaolo Bonzini 28711755da16SPaolo Bonzini bdrv_set_dirty(bs, sector_num, nb_sectors); 2872da1fa91dSKevin Wolf 2873da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2874da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2875da1fa91dSKevin Wolf } 2876df2a6f29SPaolo Bonzini if (bs->growable && ret >= 0) { 2877df2a6f29SPaolo Bonzini bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors); 2878df2a6f29SPaolo Bonzini } 2879da1fa91dSKevin Wolf 2880dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2881dbffbdcfSStefan Hajnoczi 28826b7cb247SStefan Hajnoczi return ret; 2883da1fa91dSKevin Wolf } 2884da1fa91dSKevin Wolf 2885c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 2886c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 2887c5fbe571SStefan Hajnoczi { 2888c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 2889c5fbe571SStefan Hajnoczi 2890f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 2891f08f2ddaSStefan Hajnoczi } 2892f08f2ddaSStefan Hajnoczi 2893f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 2894aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, 2895aa7bfbffSPeter Lieven BdrvRequestFlags flags) 2896f08f2ddaSStefan Hajnoczi { 289794d6ff21SPaolo Bonzini trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags); 2898f08f2ddaSStefan Hajnoczi 2899d32f35cbSPeter Lieven if (!(bs->open_flags & BDRV_O_UNMAP)) { 2900d32f35cbSPeter Lieven flags &= ~BDRV_REQ_MAY_UNMAP; 2901d32f35cbSPeter Lieven } 2902d32f35cbSPeter Lieven 2903f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 2904aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 2905c5fbe571SStefan Hajnoczi } 2906c5fbe571SStefan Hajnoczi 290783f64091Sbellard /** 290883f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 290983f64091Sbellard */ 291083f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 291183f64091Sbellard { 291283f64091Sbellard BlockDriver *drv = bs->drv; 291351762288SStefan Hajnoczi int ret; 291483f64091Sbellard if (!drv) 291519cb3738Sbellard return -ENOMEDIUM; 291683f64091Sbellard if (!drv->bdrv_truncate) 291783f64091Sbellard return -ENOTSUP; 291859f2689dSNaphtali Sprei if (bs->read_only) 291959f2689dSNaphtali Sprei return -EACCES; 29208591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 29218591675fSMarcelo Tosatti return -EBUSY; 292251762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 292351762288SStefan Hajnoczi if (ret == 0) { 292451762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2925145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 292651762288SStefan Hajnoczi } 292751762288SStefan Hajnoczi return ret; 292883f64091Sbellard } 292983f64091Sbellard 293083f64091Sbellard /** 29314a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 29324a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 29334a1d5e1fSFam Zheng */ 29344a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 29354a1d5e1fSFam Zheng { 29364a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 29374a1d5e1fSFam Zheng if (!drv) { 29384a1d5e1fSFam Zheng return -ENOMEDIUM; 29394a1d5e1fSFam Zheng } 29404a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 29414a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 29424a1d5e1fSFam Zheng } 29434a1d5e1fSFam Zheng if (bs->file) { 29444a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 29454a1d5e1fSFam Zheng } 29464a1d5e1fSFam Zheng return -ENOTSUP; 29474a1d5e1fSFam Zheng } 29484a1d5e1fSFam Zheng 29494a1d5e1fSFam Zheng /** 295083f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 295183f64091Sbellard */ 295283f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 295383f64091Sbellard { 295483f64091Sbellard BlockDriver *drv = bs->drv; 295583f64091Sbellard if (!drv) 295619cb3738Sbellard return -ENOMEDIUM; 295751762288SStefan Hajnoczi 2958b94a2610SKevin Wolf if (drv->has_variable_length) { 2959b94a2610SKevin Wolf int ret = refresh_total_sectors(bs, bs->total_sectors); 2960b94a2610SKevin Wolf if (ret < 0) { 2961b94a2610SKevin Wolf return ret; 2962fc01f7e7Sbellard } 296346a4e4e6SStefan Hajnoczi } 296446a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 296546a4e4e6SStefan Hajnoczi } 2966fc01f7e7Sbellard 296719cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 296896b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2969fc01f7e7Sbellard { 297019cb3738Sbellard int64_t length; 297119cb3738Sbellard length = bdrv_getlength(bs); 297219cb3738Sbellard if (length < 0) 297319cb3738Sbellard length = 0; 297419cb3738Sbellard else 29756ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 297619cb3738Sbellard *nb_sectors_ptr = length; 2977fc01f7e7Sbellard } 2978cf98951bSbellard 2979ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 2980ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 2981abd7f68dSMarkus Armbruster { 2982abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2983abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2984abd7f68dSMarkus Armbruster } 2985abd7f68dSMarkus Armbruster 29861ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 2987abd7f68dSMarkus Armbruster { 2988abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2989abd7f68dSMarkus Armbruster } 2990abd7f68dSMarkus Armbruster 29913e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 29923e1caa5fSPaolo Bonzini { 29933e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 29943e1caa5fSPaolo Bonzini 29953e1caa5fSPaolo Bonzini switch (on_err) { 29963e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 29973e1caa5fSPaolo Bonzini return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; 29983e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 29993e1caa5fSPaolo Bonzini return BDRV_ACTION_STOP; 30003e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 30013e1caa5fSPaolo Bonzini return BDRV_ACTION_REPORT; 30023e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 30033e1caa5fSPaolo Bonzini return BDRV_ACTION_IGNORE; 30043e1caa5fSPaolo Bonzini default: 30053e1caa5fSPaolo Bonzini abort(); 30063e1caa5fSPaolo Bonzini } 30073e1caa5fSPaolo Bonzini } 30083e1caa5fSPaolo Bonzini 30093e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 30103e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 30113e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 30123e1caa5fSPaolo Bonzini */ 30133e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 30143e1caa5fSPaolo Bonzini bool is_read, int error) 30153e1caa5fSPaolo Bonzini { 30163e1caa5fSPaolo Bonzini assert(error >= 0); 301732c81a4aSPaolo Bonzini bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); 30183e1caa5fSPaolo Bonzini if (action == BDRV_ACTION_STOP) { 30193e1caa5fSPaolo Bonzini vm_stop(RUN_STATE_IO_ERROR); 30203e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 30213e1caa5fSPaolo Bonzini } 30223e1caa5fSPaolo Bonzini } 30233e1caa5fSPaolo Bonzini 3024b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 3025b338082bSbellard { 3026b338082bSbellard return bs->read_only; 3027b338082bSbellard } 3028b338082bSbellard 3029985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 3030985a03b0Sths { 3031985a03b0Sths return bs->sg; 3032985a03b0Sths } 3033985a03b0Sths 3034e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 3035e900a7b7SChristoph Hellwig { 3036e900a7b7SChristoph Hellwig return bs->enable_write_cache; 3037e900a7b7SChristoph Hellwig } 3038e900a7b7SChristoph Hellwig 3039425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 3040425b0148SPaolo Bonzini { 3041425b0148SPaolo Bonzini bs->enable_write_cache = wce; 304255b110f2SJeff Cody 304355b110f2SJeff Cody /* so a reopen() will preserve wce */ 304455b110f2SJeff Cody if (wce) { 304555b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 304655b110f2SJeff Cody } else { 304755b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 304855b110f2SJeff Cody } 3049425b0148SPaolo Bonzini } 3050425b0148SPaolo Bonzini 3051ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 3052ea2384d3Sbellard { 3053ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 3054ea2384d3Sbellard return 1; 3055ea2384d3Sbellard return bs->encrypted; 3056ea2384d3Sbellard } 3057ea2384d3Sbellard 3058c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 3059c0f4ce77Saliguori { 3060c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 3061c0f4ce77Saliguori 3062c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 3063c0f4ce77Saliguori return 1; 3064c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 3065c0f4ce77Saliguori } 3066c0f4ce77Saliguori 3067ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 3068ea2384d3Sbellard { 3069ea2384d3Sbellard int ret; 3070ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 3071ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 3072ea2384d3Sbellard if (ret < 0) 3073ea2384d3Sbellard return ret; 3074ea2384d3Sbellard if (!bs->encrypted) 3075ea2384d3Sbellard return 0; 3076ea2384d3Sbellard } 3077fd04a2aeSShahar Havivi if (!bs->encrypted) { 3078fd04a2aeSShahar Havivi return -EINVAL; 3079fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 3080fd04a2aeSShahar Havivi return -ENOMEDIUM; 3081fd04a2aeSShahar Havivi } 3082c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 3083bb5fc20fSaliguori if (ret < 0) { 3084bb5fc20fSaliguori bs->valid_key = 0; 3085bb5fc20fSaliguori } else if (!bs->valid_key) { 3086bb5fc20fSaliguori bs->valid_key = 1; 3087bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 30887d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 3089bb5fc20fSaliguori } 3090c0f4ce77Saliguori return ret; 3091ea2384d3Sbellard } 3092ea2384d3Sbellard 3093f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 3094ea2384d3Sbellard { 3095f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 3096ea2384d3Sbellard } 3097ea2384d3Sbellard 3098ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 3099ea2384d3Sbellard void *opaque) 3100ea2384d3Sbellard { 3101ea2384d3Sbellard BlockDriver *drv; 3102ea2384d3Sbellard 31038a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 3104ea2384d3Sbellard it(opaque, drv->format_name); 3105ea2384d3Sbellard } 3106ea2384d3Sbellard } 3107ea2384d3Sbellard 3108b338082bSbellard BlockDriverState *bdrv_find(const char *name) 3109b338082bSbellard { 3110b338082bSbellard BlockDriverState *bs; 3111b338082bSbellard 31121b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 31131b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 3114b338082bSbellard return bs; 3115b338082bSbellard } 31161b7bdbc1SStefan Hajnoczi } 3117b338082bSbellard return NULL; 3118b338082bSbellard } 3119b338082bSbellard 31202f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 31212f399b0aSMarkus Armbruster { 31222f399b0aSMarkus Armbruster if (!bs) { 31232f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 31242f399b0aSMarkus Armbruster } 31252f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 31262f399b0aSMarkus Armbruster } 31272f399b0aSMarkus Armbruster 312851de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 312981d0912dSbellard { 313081d0912dSbellard BlockDriverState *bs; 313181d0912dSbellard 31321b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 313351de9760Saliguori it(opaque, bs); 313481d0912dSbellard } 313581d0912dSbellard } 313681d0912dSbellard 3137ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 3138ea2384d3Sbellard { 3139ea2384d3Sbellard return bs->device_name; 3140ea2384d3Sbellard } 3141ea2384d3Sbellard 3142c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 3143c8433287SMarkus Armbruster { 3144c8433287SMarkus Armbruster return bs->open_flags; 3145c8433287SMarkus Armbruster } 3146c8433287SMarkus Armbruster 3147f0f0fdfeSKevin Wolf int bdrv_flush_all(void) 3148c6ca28d6Saliguori { 3149c6ca28d6Saliguori BlockDriverState *bs; 3150f0f0fdfeSKevin Wolf int result = 0; 3151c6ca28d6Saliguori 31521b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 3153f0f0fdfeSKevin Wolf int ret = bdrv_flush(bs); 3154f0f0fdfeSKevin Wolf if (ret < 0 && !result) { 3155f0f0fdfeSKevin Wolf result = ret; 3156c6ca28d6Saliguori } 31571b7bdbc1SStefan Hajnoczi } 3158c6ca28d6Saliguori 3159f0f0fdfeSKevin Wolf return result; 3160f0f0fdfeSKevin Wolf } 3161f0f0fdfeSKevin Wolf 31623ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 31633ac21627SPeter Lieven { 31643ac21627SPeter Lieven return 1; 31653ac21627SPeter Lieven } 31663ac21627SPeter Lieven 3167f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 3168f2feebbdSKevin Wolf { 3169f2feebbdSKevin Wolf assert(bs->drv); 3170f2feebbdSKevin Wolf 317111212d8fSPaolo Bonzini /* If BS is a copy on write image, it is initialized to 317211212d8fSPaolo Bonzini the contents of the base image, which may not be zeroes. */ 317311212d8fSPaolo Bonzini if (bs->backing_hd) { 317411212d8fSPaolo Bonzini return 0; 317511212d8fSPaolo Bonzini } 3176336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 3177336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 3178f2feebbdSKevin Wolf } 3179f2feebbdSKevin Wolf 31803ac21627SPeter Lieven /* safe default */ 31813ac21627SPeter Lieven return 0; 3182f2feebbdSKevin Wolf } 3183f2feebbdSKevin Wolf 31844ce78691SPeter Lieven bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) 31854ce78691SPeter Lieven { 31864ce78691SPeter Lieven BlockDriverInfo bdi; 31874ce78691SPeter Lieven 31884ce78691SPeter Lieven if (bs->backing_hd) { 31894ce78691SPeter Lieven return false; 31904ce78691SPeter Lieven } 31914ce78691SPeter Lieven 31924ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 31934ce78691SPeter Lieven return bdi.unallocated_blocks_are_zero; 31944ce78691SPeter Lieven } 31954ce78691SPeter Lieven 31964ce78691SPeter Lieven return false; 31974ce78691SPeter Lieven } 31984ce78691SPeter Lieven 31994ce78691SPeter Lieven bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) 32004ce78691SPeter Lieven { 32014ce78691SPeter Lieven BlockDriverInfo bdi; 32024ce78691SPeter Lieven 32034ce78691SPeter Lieven if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { 32044ce78691SPeter Lieven return false; 32054ce78691SPeter Lieven } 32064ce78691SPeter Lieven 32074ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 32084ce78691SPeter Lieven return bdi.can_write_zeroes_with_unmap; 32094ce78691SPeter Lieven } 32104ce78691SPeter Lieven 32114ce78691SPeter Lieven return false; 32124ce78691SPeter Lieven } 32134ce78691SPeter Lieven 3214b6b8a333SPaolo Bonzini typedef struct BdrvCoGetBlockStatusData { 3215376ae3f1SStefan Hajnoczi BlockDriverState *bs; 3216b35b2bbaSMiroslav Rezanina BlockDriverState *base; 3217376ae3f1SStefan Hajnoczi int64_t sector_num; 3218376ae3f1SStefan Hajnoczi int nb_sectors; 3219376ae3f1SStefan Hajnoczi int *pnum; 3220b6b8a333SPaolo Bonzini int64_t ret; 3221376ae3f1SStefan Hajnoczi bool done; 3222b6b8a333SPaolo Bonzini } BdrvCoGetBlockStatusData; 3223376ae3f1SStefan Hajnoczi 3224f58c7b35Sths /* 3225f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 3226f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 3227f58c7b35Sths * hence all their sectors are reported as allocated. 3228f58c7b35Sths * 3229bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 3230bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 3231bd9533e3SStefan Hajnoczi * 3232f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 3233f58c7b35Sths * the specified sector) that are known to be in the same 3234f58c7b35Sths * allocated/unallocated state. 3235f58c7b35Sths * 3236bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 3237bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 3238f58c7b35Sths */ 3239b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, 3240bdad13b9SPaolo Bonzini int64_t sector_num, 3241060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 3242f58c7b35Sths { 3243617ccb46SPaolo Bonzini int64_t length; 3244f58c7b35Sths int64_t n; 32455daa74a6SPaolo Bonzini int64_t ret, ret2; 3246bd9533e3SStefan Hajnoczi 3247617ccb46SPaolo Bonzini length = bdrv_getlength(bs); 3248617ccb46SPaolo Bonzini if (length < 0) { 3249617ccb46SPaolo Bonzini return length; 3250617ccb46SPaolo Bonzini } 3251617ccb46SPaolo Bonzini 3252617ccb46SPaolo Bonzini if (sector_num >= (length >> BDRV_SECTOR_BITS)) { 32536aebab14SStefan Hajnoczi *pnum = 0; 32546aebab14SStefan Hajnoczi return 0; 32556aebab14SStefan Hajnoczi } 3256bd9533e3SStefan Hajnoczi 32576aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 3258bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 3259bd9533e3SStefan Hajnoczi nb_sectors = n; 3260bd9533e3SStefan Hajnoczi } 3261bd9533e3SStefan Hajnoczi 3262b6b8a333SPaolo Bonzini if (!bs->drv->bdrv_co_get_block_status) { 3263bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 3264918e92d7SPaolo Bonzini ret = BDRV_BLOCK_DATA; 3265918e92d7SPaolo Bonzini if (bs->drv->protocol_name) { 3266918e92d7SPaolo Bonzini ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE); 3267918e92d7SPaolo Bonzini } 3268918e92d7SPaolo Bonzini return ret; 32696aebab14SStefan Hajnoczi } 32706aebab14SStefan Hajnoczi 3271415b5b01SPaolo Bonzini ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum); 3272415b5b01SPaolo Bonzini if (ret < 0) { 32733e0a233dSPeter Lieven *pnum = 0; 3274415b5b01SPaolo Bonzini return ret; 3275415b5b01SPaolo Bonzini } 3276415b5b01SPaolo Bonzini 327792bc50a5SPeter Lieven if (ret & BDRV_BLOCK_RAW) { 327892bc50a5SPeter Lieven assert(ret & BDRV_BLOCK_OFFSET_VALID); 327992bc50a5SPeter Lieven return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 328092bc50a5SPeter Lieven *pnum, pnum); 328192bc50a5SPeter Lieven } 328292bc50a5SPeter Lieven 3283c3d86884SPeter Lieven if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) { 3284c3d86884SPeter Lieven if (bdrv_unallocated_blocks_are_zero(bs)) { 3285415b5b01SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 32861f9db224SPeter Lieven } else if (bs->backing_hd) { 3287f0ad5712SPaolo Bonzini BlockDriverState *bs2 = bs->backing_hd; 3288f0ad5712SPaolo Bonzini int64_t length2 = bdrv_getlength(bs2); 3289f0ad5712SPaolo Bonzini if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) { 3290f0ad5712SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 3291f0ad5712SPaolo Bonzini } 3292f0ad5712SPaolo Bonzini } 3293415b5b01SPaolo Bonzini } 32945daa74a6SPaolo Bonzini 32955daa74a6SPaolo Bonzini if (bs->file && 32965daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && 32975daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_OFFSET_VALID)) { 32985daa74a6SPaolo Bonzini ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 32995daa74a6SPaolo Bonzini *pnum, pnum); 33005daa74a6SPaolo Bonzini if (ret2 >= 0) { 33015daa74a6SPaolo Bonzini /* Ignore errors. This is just providing extra information, it 33025daa74a6SPaolo Bonzini * is useful but not necessary. 33035daa74a6SPaolo Bonzini */ 33045daa74a6SPaolo Bonzini ret |= (ret2 & BDRV_BLOCK_ZERO); 33055daa74a6SPaolo Bonzini } 33065daa74a6SPaolo Bonzini } 33075daa74a6SPaolo Bonzini 3308415b5b01SPaolo Bonzini return ret; 3309060f51c9SStefan Hajnoczi } 3310060f51c9SStefan Hajnoczi 3311b6b8a333SPaolo Bonzini /* Coroutine wrapper for bdrv_get_block_status() */ 3312b6b8a333SPaolo Bonzini static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque) 3313060f51c9SStefan Hajnoczi { 3314b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData *data = opaque; 3315060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 3316060f51c9SStefan Hajnoczi 3317b6b8a333SPaolo Bonzini data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors, 3318060f51c9SStefan Hajnoczi data->pnum); 3319060f51c9SStefan Hajnoczi data->done = true; 3320060f51c9SStefan Hajnoczi } 3321060f51c9SStefan Hajnoczi 3322060f51c9SStefan Hajnoczi /* 3323b6b8a333SPaolo Bonzini * Synchronous wrapper around bdrv_co_get_block_status(). 3324060f51c9SStefan Hajnoczi * 3325b6b8a333SPaolo Bonzini * See bdrv_co_get_block_status() for details. 3326060f51c9SStefan Hajnoczi */ 3327b6b8a333SPaolo Bonzini int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num, 3328b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 3329060f51c9SStefan Hajnoczi { 3330376ae3f1SStefan Hajnoczi Coroutine *co; 3331b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData data = { 3332376ae3f1SStefan Hajnoczi .bs = bs, 3333376ae3f1SStefan Hajnoczi .sector_num = sector_num, 3334376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 3335376ae3f1SStefan Hajnoczi .pnum = pnum, 3336376ae3f1SStefan Hajnoczi .done = false, 3337376ae3f1SStefan Hajnoczi }; 3338376ae3f1SStefan Hajnoczi 3339bdad13b9SPaolo Bonzini if (qemu_in_coroutine()) { 3340bdad13b9SPaolo Bonzini /* Fast-path if already in coroutine context */ 3341b6b8a333SPaolo Bonzini bdrv_get_block_status_co_entry(&data); 3342bdad13b9SPaolo Bonzini } else { 3343b6b8a333SPaolo Bonzini co = qemu_coroutine_create(bdrv_get_block_status_co_entry); 3344376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 3345376ae3f1SStefan Hajnoczi while (!data.done) { 3346376ae3f1SStefan Hajnoczi qemu_aio_wait(); 3347376ae3f1SStefan Hajnoczi } 3348bdad13b9SPaolo Bonzini } 3349376ae3f1SStefan Hajnoczi return data.ret; 3350376ae3f1SStefan Hajnoczi } 3351f58c7b35Sths 3352b6b8a333SPaolo Bonzini int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, 3353b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 3354b6b8a333SPaolo Bonzini { 33554333bb71SPaolo Bonzini int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum); 33564333bb71SPaolo Bonzini if (ret < 0) { 33574333bb71SPaolo Bonzini return ret; 33584333bb71SPaolo Bonzini } 33594333bb71SPaolo Bonzini return 33604333bb71SPaolo Bonzini (ret & BDRV_BLOCK_DATA) || 33614333bb71SPaolo Bonzini ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs)); 3362b6b8a333SPaolo Bonzini } 3363b6b8a333SPaolo Bonzini 3364188a7bbfSPaolo Bonzini /* 3365188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 3366188a7bbfSPaolo Bonzini * 3367188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 3368188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 3369188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 3370188a7bbfSPaolo Bonzini * 3371188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 3372188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 3373188a7bbfSPaolo Bonzini * allocated/unallocated state. 3374188a7bbfSPaolo Bonzini * 3375188a7bbfSPaolo Bonzini */ 33764f578637SPaolo Bonzini int bdrv_is_allocated_above(BlockDriverState *top, 3377188a7bbfSPaolo Bonzini BlockDriverState *base, 3378188a7bbfSPaolo Bonzini int64_t sector_num, 3379188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 3380188a7bbfSPaolo Bonzini { 3381188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 3382188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 3383188a7bbfSPaolo Bonzini 3384188a7bbfSPaolo Bonzini intermediate = top; 3385188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 3386188a7bbfSPaolo Bonzini int pnum_inter; 3387bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors, 3388188a7bbfSPaolo Bonzini &pnum_inter); 3389188a7bbfSPaolo Bonzini if (ret < 0) { 3390188a7bbfSPaolo Bonzini return ret; 3391188a7bbfSPaolo Bonzini } else if (ret) { 3392188a7bbfSPaolo Bonzini *pnum = pnum_inter; 3393188a7bbfSPaolo Bonzini return 1; 3394188a7bbfSPaolo Bonzini } 3395188a7bbfSPaolo Bonzini 3396188a7bbfSPaolo Bonzini /* 3397188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 3398188a7bbfSPaolo Bonzini * might have 3399188a7bbfSPaolo Bonzini * 3400188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 3401188a7bbfSPaolo Bonzini */ 340263ba17d3SVishvananda Ishaya if (n > pnum_inter && 340363ba17d3SVishvananda Ishaya (intermediate == top || 340463ba17d3SVishvananda Ishaya sector_num + pnum_inter < intermediate->total_sectors)) { 3405188a7bbfSPaolo Bonzini n = pnum_inter; 3406188a7bbfSPaolo Bonzini } 3407188a7bbfSPaolo Bonzini 3408188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 3409188a7bbfSPaolo Bonzini } 3410188a7bbfSPaolo Bonzini 3411188a7bbfSPaolo Bonzini *pnum = n; 3412188a7bbfSPaolo Bonzini return 0; 3413188a7bbfSPaolo Bonzini } 3414188a7bbfSPaolo Bonzini 3415045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 3416045df330Saliguori { 3417045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 3418045df330Saliguori return bs->backing_file; 3419045df330Saliguori else if (bs->encrypted) 3420045df330Saliguori return bs->filename; 3421045df330Saliguori else 3422045df330Saliguori return NULL; 3423045df330Saliguori } 3424045df330Saliguori 342583f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 342683f64091Sbellard char *filename, int filename_size) 342783f64091Sbellard { 342883f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 342983f64091Sbellard } 343083f64091Sbellard 3431faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 3432faea38e7Sbellard const uint8_t *buf, int nb_sectors) 3433faea38e7Sbellard { 3434faea38e7Sbellard BlockDriver *drv = bs->drv; 3435faea38e7Sbellard if (!drv) 343619cb3738Sbellard return -ENOMEDIUM; 3437faea38e7Sbellard if (!drv->bdrv_write_compressed) 3438faea38e7Sbellard return -ENOTSUP; 3439fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 3440fbb7b4e0SKevin Wolf return -EIO; 34417cd1e32aSlirans@il.ibm.com 3442e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 34437cd1e32aSlirans@il.ibm.com 3444faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 3445faea38e7Sbellard } 3446faea38e7Sbellard 3447faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 3448faea38e7Sbellard { 3449faea38e7Sbellard BlockDriver *drv = bs->drv; 3450faea38e7Sbellard if (!drv) 345119cb3738Sbellard return -ENOMEDIUM; 3452faea38e7Sbellard if (!drv->bdrv_get_info) 3453faea38e7Sbellard return -ENOTSUP; 3454faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 3455faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 3456faea38e7Sbellard } 3457faea38e7Sbellard 3458eae041feSMax Reitz ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) 3459eae041feSMax Reitz { 3460eae041feSMax Reitz BlockDriver *drv = bs->drv; 3461eae041feSMax Reitz if (drv && drv->bdrv_get_specific_info) { 3462eae041feSMax Reitz return drv->bdrv_get_specific_info(bs); 3463eae041feSMax Reitz } 3464eae041feSMax Reitz return NULL; 3465eae041feSMax Reitz } 3466eae041feSMax Reitz 346745566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 346845566e9cSChristoph Hellwig int64_t pos, int size) 3469178e08a5Saliguori { 3470cf8074b3SKevin Wolf QEMUIOVector qiov; 3471cf8074b3SKevin Wolf struct iovec iov = { 3472cf8074b3SKevin Wolf .iov_base = (void *) buf, 3473cf8074b3SKevin Wolf .iov_len = size, 3474cf8074b3SKevin Wolf }; 3475cf8074b3SKevin Wolf 3476cf8074b3SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 3477cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs, &qiov, pos); 3478cf8074b3SKevin Wolf } 3479cf8074b3SKevin Wolf 3480cf8074b3SKevin Wolf int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) 3481cf8074b3SKevin Wolf { 3482178e08a5Saliguori BlockDriver *drv = bs->drv; 3483cf8074b3SKevin Wolf 3484cf8074b3SKevin Wolf if (!drv) { 3485178e08a5Saliguori return -ENOMEDIUM; 3486cf8074b3SKevin Wolf } else if (drv->bdrv_save_vmstate) { 3487cf8074b3SKevin Wolf return drv->bdrv_save_vmstate(bs, qiov, pos); 3488cf8074b3SKevin Wolf } else if (bs->file) { 3489cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs->file, qiov, pos); 3490cf8074b3SKevin Wolf } 3491cf8074b3SKevin Wolf 34927cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3493178e08a5Saliguori } 3494178e08a5Saliguori 349545566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 349645566e9cSChristoph Hellwig int64_t pos, int size) 3497178e08a5Saliguori { 3498178e08a5Saliguori BlockDriver *drv = bs->drv; 3499178e08a5Saliguori if (!drv) 3500178e08a5Saliguori return -ENOMEDIUM; 35017cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 350245566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 35037cdb1f6dSMORITA Kazutaka if (bs->file) 35047cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 35057cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3506178e08a5Saliguori } 3507178e08a5Saliguori 35088b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 35098b9b0cc2SKevin Wolf { 3510bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 35118b9b0cc2SKevin Wolf return; 35128b9b0cc2SKevin Wolf } 35138b9b0cc2SKevin Wolf 3514bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 351541c695c7SKevin Wolf } 35168b9b0cc2SKevin Wolf 351741c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 351841c695c7SKevin Wolf const char *tag) 351941c695c7SKevin Wolf { 352041c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 352141c695c7SKevin Wolf bs = bs->file; 352241c695c7SKevin Wolf } 352341c695c7SKevin Wolf 352441c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 352541c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 352641c695c7SKevin Wolf } 352741c695c7SKevin Wolf 352841c695c7SKevin Wolf return -ENOTSUP; 352941c695c7SKevin Wolf } 353041c695c7SKevin Wolf 35314cc70e93SFam Zheng int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) 35324cc70e93SFam Zheng { 35334cc70e93SFam Zheng while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { 35344cc70e93SFam Zheng bs = bs->file; 35354cc70e93SFam Zheng } 35364cc70e93SFam Zheng 35374cc70e93SFam Zheng if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { 35384cc70e93SFam Zheng return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); 35394cc70e93SFam Zheng } 35404cc70e93SFam Zheng 35414cc70e93SFam Zheng return -ENOTSUP; 35424cc70e93SFam Zheng } 35434cc70e93SFam Zheng 354441c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 354541c695c7SKevin Wolf { 354641c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_resume) { 354741c695c7SKevin Wolf bs = bs->file; 354841c695c7SKevin Wolf } 354941c695c7SKevin Wolf 355041c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 355141c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 355241c695c7SKevin Wolf } 355341c695c7SKevin Wolf 355441c695c7SKevin Wolf return -ENOTSUP; 355541c695c7SKevin Wolf } 355641c695c7SKevin Wolf 355741c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 355841c695c7SKevin Wolf { 355941c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 356041c695c7SKevin Wolf bs = bs->file; 356141c695c7SKevin Wolf } 356241c695c7SKevin Wolf 356341c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 356441c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 356541c695c7SKevin Wolf } 356641c695c7SKevin Wolf 356741c695c7SKevin Wolf return false; 35688b9b0cc2SKevin Wolf } 35698b9b0cc2SKevin Wolf 3570199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 3571199630b6SBlue Swirl { 3572199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 3573199630b6SBlue Swirl } 3574199630b6SBlue Swirl 3575b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 3576b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 3577b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 3578b1b1d783SJeff Cody * the CWD rather than the chain. */ 3579e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 3580e8a6bb9cSMarcelo Tosatti const char *backing_file) 3581e8a6bb9cSMarcelo Tosatti { 3582b1b1d783SJeff Cody char *filename_full = NULL; 3583b1b1d783SJeff Cody char *backing_file_full = NULL; 3584b1b1d783SJeff Cody char *filename_tmp = NULL; 3585b1b1d783SJeff Cody int is_protocol = 0; 3586b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 3587b1b1d783SJeff Cody BlockDriverState *retval = NULL; 3588b1b1d783SJeff Cody 3589b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 3590e8a6bb9cSMarcelo Tosatti return NULL; 3591e8a6bb9cSMarcelo Tosatti } 3592e8a6bb9cSMarcelo Tosatti 3593b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 3594b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 3595b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 3596b1b1d783SJeff Cody 3597b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 3598b1b1d783SJeff Cody 3599b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 3600b1b1d783SJeff Cody 3601b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 3602b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 3603b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 3604b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 3605b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3606b1b1d783SJeff Cody break; 3607b1b1d783SJeff Cody } 3608e8a6bb9cSMarcelo Tosatti } else { 3609b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 3610b1b1d783SJeff Cody * image's filename path */ 3611b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3612b1b1d783SJeff Cody backing_file); 3613b1b1d783SJeff Cody 3614b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 3615b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 3616b1b1d783SJeff Cody continue; 3617b1b1d783SJeff Cody } 3618b1b1d783SJeff Cody 3619b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 3620b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 3621b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3622b1b1d783SJeff Cody curr_bs->backing_file); 3623b1b1d783SJeff Cody 3624b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 3625b1b1d783SJeff Cody continue; 3626b1b1d783SJeff Cody } 3627b1b1d783SJeff Cody 3628b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 3629b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3630b1b1d783SJeff Cody break; 3631b1b1d783SJeff Cody } 3632e8a6bb9cSMarcelo Tosatti } 3633e8a6bb9cSMarcelo Tosatti } 3634e8a6bb9cSMarcelo Tosatti 3635b1b1d783SJeff Cody g_free(filename_full); 3636b1b1d783SJeff Cody g_free(backing_file_full); 3637b1b1d783SJeff Cody g_free(filename_tmp); 3638b1b1d783SJeff Cody return retval; 3639e8a6bb9cSMarcelo Tosatti } 3640e8a6bb9cSMarcelo Tosatti 3641f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 3642f198fd1cSBenoît Canet { 3643f198fd1cSBenoît Canet if (!bs->drv) { 3644f198fd1cSBenoît Canet return 0; 3645f198fd1cSBenoît Canet } 3646f198fd1cSBenoît Canet 3647f198fd1cSBenoît Canet if (!bs->backing_hd) { 3648f198fd1cSBenoît Canet return 0; 3649f198fd1cSBenoît Canet } 3650f198fd1cSBenoît Canet 3651f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 3652f198fd1cSBenoît Canet } 3653f198fd1cSBenoît Canet 365479fac568SJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 365579fac568SJeff Cody { 365679fac568SJeff Cody BlockDriverState *curr_bs = NULL; 365779fac568SJeff Cody 365879fac568SJeff Cody if (!bs) { 365979fac568SJeff Cody return NULL; 366079fac568SJeff Cody } 366179fac568SJeff Cody 366279fac568SJeff Cody curr_bs = bs; 366379fac568SJeff Cody 366479fac568SJeff Cody while (curr_bs->backing_hd) { 366579fac568SJeff Cody curr_bs = curr_bs->backing_hd; 366679fac568SJeff Cody } 366779fac568SJeff Cody return curr_bs; 366879fac568SJeff Cody } 366979fac568SJeff Cody 3670ea2384d3Sbellard /**************************************************************/ 367183f64091Sbellard /* async I/Os */ 3672ea2384d3Sbellard 36733b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 3674f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 367583f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 3676ea2384d3Sbellard { 3677bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 3678bbf0a440SStefan Hajnoczi 3679d20d9b7cSPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, 36808c5873d6SStefan Hajnoczi cb, opaque, false); 368183f64091Sbellard } 368283f64091Sbellard 3683f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 3684f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 368583f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 36867674e7bfSbellard { 3687bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 3688bbf0a440SStefan Hajnoczi 3689d20d9b7cSPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, 36908c5873d6SStefan Hajnoczi cb, opaque, true); 369183f64091Sbellard } 369283f64091Sbellard 3693d5ef94d4SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs, 3694d5ef94d4SPaolo Bonzini int64_t sector_num, int nb_sectors, BdrvRequestFlags flags, 3695d5ef94d4SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 3696d5ef94d4SPaolo Bonzini { 3697d5ef94d4SPaolo Bonzini trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque); 3698d5ef94d4SPaolo Bonzini 3699d5ef94d4SPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors, 3700d5ef94d4SPaolo Bonzini BDRV_REQ_ZERO_WRITE | flags, 3701d5ef94d4SPaolo Bonzini cb, opaque, true); 3702d5ef94d4SPaolo Bonzini } 3703d5ef94d4SPaolo Bonzini 370440b4f539SKevin Wolf 370540b4f539SKevin Wolf typedef struct MultiwriteCB { 370640b4f539SKevin Wolf int error; 370740b4f539SKevin Wolf int num_requests; 370840b4f539SKevin Wolf int num_callbacks; 370940b4f539SKevin Wolf struct { 371040b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 371140b4f539SKevin Wolf void *opaque; 371240b4f539SKevin Wolf QEMUIOVector *free_qiov; 371340b4f539SKevin Wolf } callbacks[]; 371440b4f539SKevin Wolf } MultiwriteCB; 371540b4f539SKevin Wolf 371640b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 371740b4f539SKevin Wolf { 371840b4f539SKevin Wolf int i; 371940b4f539SKevin Wolf 372040b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 372140b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 37221e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 37231e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 37241e1ea48dSStefan Hajnoczi } 37257267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 372640b4f539SKevin Wolf } 372740b4f539SKevin Wolf } 372840b4f539SKevin Wolf 372940b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 373040b4f539SKevin Wolf { 373140b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 373240b4f539SKevin Wolf 37336d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 37346d519a5fSStefan Hajnoczi 3735cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 373640b4f539SKevin Wolf mcb->error = ret; 373740b4f539SKevin Wolf } 373840b4f539SKevin Wolf 373940b4f539SKevin Wolf mcb->num_requests--; 374040b4f539SKevin Wolf if (mcb->num_requests == 0) { 374140b4f539SKevin Wolf multiwrite_user_cb(mcb); 37427267c094SAnthony Liguori g_free(mcb); 374340b4f539SKevin Wolf } 374440b4f539SKevin Wolf } 374540b4f539SKevin Wolf 374640b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 374740b4f539SKevin Wolf { 374877be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 374977be4366SChristoph Hellwig 375077be4366SChristoph Hellwig /* 375177be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 375277be4366SChristoph Hellwig * here as that could overflow the return value. 375377be4366SChristoph Hellwig */ 375477be4366SChristoph Hellwig if (req1->sector > req2->sector) { 375577be4366SChristoph Hellwig return 1; 375677be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 375777be4366SChristoph Hellwig return -1; 375877be4366SChristoph Hellwig } else { 375977be4366SChristoph Hellwig return 0; 376077be4366SChristoph Hellwig } 376140b4f539SKevin Wolf } 376240b4f539SKevin Wolf 376340b4f539SKevin Wolf /* 376440b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 376540b4f539SKevin Wolf * requests that remain after merging. 376640b4f539SKevin Wolf */ 376740b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 376840b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 376940b4f539SKevin Wolf { 377040b4f539SKevin Wolf int i, outidx; 377140b4f539SKevin Wolf 377240b4f539SKevin Wolf // Sort requests by start sector 377340b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 377440b4f539SKevin Wolf 377540b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 377640b4f539SKevin Wolf // filling up gaps with zero sectors. 377740b4f539SKevin Wolf outidx = 0; 377840b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 377940b4f539SKevin Wolf int merge = 0; 378040b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 378140b4f539SKevin Wolf 3782b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 378340b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 378440b4f539SKevin Wolf merge = 1; 378540b4f539SKevin Wolf } 378640b4f539SKevin Wolf 3787e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3788e2a305fbSChristoph Hellwig merge = 0; 3789e2a305fbSChristoph Hellwig } 3790e2a305fbSChristoph Hellwig 379140b4f539SKevin Wolf if (merge) { 379240b4f539SKevin Wolf size_t size; 37937267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 379440b4f539SKevin Wolf qemu_iovec_init(qiov, 379540b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 379640b4f539SKevin Wolf 379740b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 379840b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 379940b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 38001b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 380140b4f539SKevin Wolf 3802b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3803b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 380440b4f539SKevin Wolf 380540b4f539SKevin Wolf // Add the second request 38061b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 380740b4f539SKevin Wolf 3808cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 380940b4f539SKevin Wolf reqs[outidx].qiov = qiov; 381040b4f539SKevin Wolf 381140b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 381240b4f539SKevin Wolf } else { 381340b4f539SKevin Wolf outidx++; 381440b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 381540b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 381640b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 381740b4f539SKevin Wolf } 381840b4f539SKevin Wolf } 381940b4f539SKevin Wolf 382040b4f539SKevin Wolf return outidx + 1; 382140b4f539SKevin Wolf } 382240b4f539SKevin Wolf 382340b4f539SKevin Wolf /* 382440b4f539SKevin Wolf * Submit multiple AIO write requests at once. 382540b4f539SKevin Wolf * 382640b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 382740b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 382840b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 382940b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 383040b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 383140b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 383240b4f539SKevin Wolf * 383340b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 383440b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 383540b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 383640b4f539SKevin Wolf */ 383740b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 383840b4f539SKevin Wolf { 383940b4f539SKevin Wolf MultiwriteCB *mcb; 384040b4f539SKevin Wolf int i; 384140b4f539SKevin Wolf 3842301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3843301db7c2SRyan Harper if (bs->drv == NULL) { 3844301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3845301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3846301db7c2SRyan Harper } 3847301db7c2SRyan Harper return -1; 3848301db7c2SRyan Harper } 3849301db7c2SRyan Harper 385040b4f539SKevin Wolf if (num_reqs == 0) { 385140b4f539SKevin Wolf return 0; 385240b4f539SKevin Wolf } 385340b4f539SKevin Wolf 385440b4f539SKevin Wolf // Create MultiwriteCB structure 38557267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 385640b4f539SKevin Wolf mcb->num_requests = 0; 385740b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 385840b4f539SKevin Wolf 385940b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 386040b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 386140b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 386240b4f539SKevin Wolf } 386340b4f539SKevin Wolf 386440b4f539SKevin Wolf // Check for mergable requests 386540b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 386640b4f539SKevin Wolf 38676d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 38686d519a5fSStefan Hajnoczi 3869df9309fbSPaolo Bonzini /* Run the aio requests. */ 3870df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 387140b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3872d20d9b7cSPaolo Bonzini bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov, 3873d20d9b7cSPaolo Bonzini reqs[i].nb_sectors, reqs[i].flags, 3874d20d9b7cSPaolo Bonzini multiwrite_cb, mcb, 3875d20d9b7cSPaolo Bonzini true); 387640b4f539SKevin Wolf } 387740b4f539SKevin Wolf 387840b4f539SKevin Wolf return 0; 387940b4f539SKevin Wolf } 388040b4f539SKevin Wolf 388183f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 388283f64091Sbellard { 3883d7331bedSStefan Hajnoczi acb->aiocb_info->cancel(acb); 388483f64091Sbellard } 388583f64091Sbellard 388683f64091Sbellard /**************************************************************/ 388783f64091Sbellard /* async block device emulation */ 388883f64091Sbellard 3889c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3890c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3891c16b5a2cSChristoph Hellwig QEMUBH *bh; 3892c16b5a2cSChristoph Hellwig int ret; 3893c16b5a2cSChristoph Hellwig /* vector translation state */ 3894c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3895c16b5a2cSChristoph Hellwig uint8_t *bounce; 3896c16b5a2cSChristoph Hellwig int is_write; 3897c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3898c16b5a2cSChristoph Hellwig 3899c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3900c16b5a2cSChristoph Hellwig { 3901b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3902b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 39036a7ad299SDor Laor qemu_bh_delete(acb->bh); 390436afc451SAvi Kivity acb->bh = NULL; 3905c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3906c16b5a2cSChristoph Hellwig } 3907c16b5a2cSChristoph Hellwig 3908d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_aiocb_info = { 3909c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3910c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3911c16b5a2cSChristoph Hellwig }; 3912c16b5a2cSChristoph Hellwig 391383f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3914beac80cdSbellard { 3915ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3916f141eafeSaliguori 3917f141eafeSaliguori if (!acb->is_write) 391803396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 3919ceb42de8Saliguori qemu_vfree(acb->bounce); 3920ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 39216a7ad299SDor Laor qemu_bh_delete(acb->bh); 392236afc451SAvi Kivity acb->bh = NULL; 3923ce1a14dcSpbrook qemu_aio_release(acb); 3924beac80cdSbellard } 3925beac80cdSbellard 3926f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3927f141eafeSaliguori int64_t sector_num, 3928f141eafeSaliguori QEMUIOVector *qiov, 3929f141eafeSaliguori int nb_sectors, 3930f141eafeSaliguori BlockDriverCompletionFunc *cb, 3931f141eafeSaliguori void *opaque, 3932f141eafeSaliguori int is_write) 3933f141eafeSaliguori 3934ea2384d3Sbellard { 3935ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 393683f64091Sbellard 3937d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); 3938f141eafeSaliguori acb->is_write = is_write; 3939f141eafeSaliguori acb->qiov = qiov; 3940e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3941ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3942f141eafeSaliguori 3943f141eafeSaliguori if (is_write) { 3944d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 39451ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3946f141eafeSaliguori } else { 39471ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3948f141eafeSaliguori } 3949f141eafeSaliguori 3950ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3951f141eafeSaliguori 3952ce1a14dcSpbrook return &acb->common; 39537a6cba61Spbrook } 39547a6cba61Spbrook 3955f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3956f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3957ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 395883f64091Sbellard { 3959f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 396083f64091Sbellard } 396183f64091Sbellard 3962f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3963f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3964f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3965f141eafeSaliguori { 3966f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3967f141eafeSaliguori } 3968f141eafeSaliguori 396968485420SKevin Wolf 397068485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 397168485420SKevin Wolf BlockDriverAIOCB common; 397268485420SKevin Wolf BlockRequest req; 397368485420SKevin Wolf bool is_write; 3974d318aea9SKevin Wolf bool *done; 397568485420SKevin Wolf QEMUBH* bh; 397668485420SKevin Wolf } BlockDriverAIOCBCoroutine; 397768485420SKevin Wolf 397868485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 397968485420SKevin Wolf { 3980d318aea9SKevin Wolf BlockDriverAIOCBCoroutine *acb = 3981d318aea9SKevin Wolf container_of(blockacb, BlockDriverAIOCBCoroutine, common); 3982d318aea9SKevin Wolf bool done = false; 3983d318aea9SKevin Wolf 3984d318aea9SKevin Wolf acb->done = &done; 3985d318aea9SKevin Wolf while (!done) { 3986d318aea9SKevin Wolf qemu_aio_wait(); 3987d318aea9SKevin Wolf } 398868485420SKevin Wolf } 398968485420SKevin Wolf 3990d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_co_aiocb_info = { 399168485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 399268485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 399368485420SKevin Wolf }; 399468485420SKevin Wolf 399535246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 399668485420SKevin Wolf { 399768485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 399868485420SKevin Wolf 399968485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 4000d318aea9SKevin Wolf 4001d318aea9SKevin Wolf if (acb->done) { 4002d318aea9SKevin Wolf *acb->done = true; 4003d318aea9SKevin Wolf } 4004d318aea9SKevin Wolf 400568485420SKevin Wolf qemu_bh_delete(acb->bh); 400668485420SKevin Wolf qemu_aio_release(acb); 400768485420SKevin Wolf } 400868485420SKevin Wolf 4009b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 4010b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 4011b2a61371SStefan Hajnoczi { 4012b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 4013b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 4014b2a61371SStefan Hajnoczi 4015b2a61371SStefan Hajnoczi if (!acb->is_write) { 4016b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 4017d20d9b7cSPaolo Bonzini acb->req.nb_sectors, acb->req.qiov, acb->req.flags); 4018b2a61371SStefan Hajnoczi } else { 4019b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 4020d20d9b7cSPaolo Bonzini acb->req.nb_sectors, acb->req.qiov, acb->req.flags); 4021b2a61371SStefan Hajnoczi } 4022b2a61371SStefan Hajnoczi 402335246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 4024b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 4025b2a61371SStefan Hajnoczi } 4026b2a61371SStefan Hajnoczi 402768485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 402868485420SKevin Wolf int64_t sector_num, 402968485420SKevin Wolf QEMUIOVector *qiov, 403068485420SKevin Wolf int nb_sectors, 4031d20d9b7cSPaolo Bonzini BdrvRequestFlags flags, 403268485420SKevin Wolf BlockDriverCompletionFunc *cb, 403368485420SKevin Wolf void *opaque, 40348c5873d6SStefan Hajnoczi bool is_write) 403568485420SKevin Wolf { 403668485420SKevin Wolf Coroutine *co; 403768485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 403868485420SKevin Wolf 4039d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 404068485420SKevin Wolf acb->req.sector = sector_num; 404168485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 404268485420SKevin Wolf acb->req.qiov = qiov; 4043d20d9b7cSPaolo Bonzini acb->req.flags = flags; 404468485420SKevin Wolf acb->is_write = is_write; 4045d318aea9SKevin Wolf acb->done = NULL; 404668485420SKevin Wolf 40478c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 404868485420SKevin Wolf qemu_coroutine_enter(co, acb); 404968485420SKevin Wolf 405068485420SKevin Wolf return &acb->common; 405168485420SKevin Wolf } 405268485420SKevin Wolf 405307f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 4054b2e12bc6SChristoph Hellwig { 405507f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 405607f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 4057b2e12bc6SChristoph Hellwig 405807f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 405907f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 4060b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 4061b2e12bc6SChristoph Hellwig } 4062b2e12bc6SChristoph Hellwig 406307f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 4064016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 4065016f5cf6SAlexander Graf { 406607f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 4067016f5cf6SAlexander Graf 406807f07615SPaolo Bonzini Coroutine *co; 406907f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 4070016f5cf6SAlexander Graf 4071d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 4072d318aea9SKevin Wolf acb->done = NULL; 4073d318aea9SKevin Wolf 407407f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 407507f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 4076016f5cf6SAlexander Graf 4077016f5cf6SAlexander Graf return &acb->common; 4078016f5cf6SAlexander Graf } 4079016f5cf6SAlexander Graf 40804265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 40814265d620SPaolo Bonzini { 40824265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 40834265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 40844265d620SPaolo Bonzini 40854265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 40864265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 40874265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 40884265d620SPaolo Bonzini } 40894265d620SPaolo Bonzini 40904265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 40914265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 40924265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 40934265d620SPaolo Bonzini { 40944265d620SPaolo Bonzini Coroutine *co; 40954265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 40964265d620SPaolo Bonzini 40974265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 40984265d620SPaolo Bonzini 4099d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 41004265d620SPaolo Bonzini acb->req.sector = sector_num; 41014265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 4102d318aea9SKevin Wolf acb->done = NULL; 41034265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 41044265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 41054265d620SPaolo Bonzini 41064265d620SPaolo Bonzini return &acb->common; 41074265d620SPaolo Bonzini } 41084265d620SPaolo Bonzini 4109ea2384d3Sbellard void bdrv_init(void) 4110ea2384d3Sbellard { 41115efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 4112ea2384d3Sbellard } 4113ce1a14dcSpbrook 4114eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 4115eb852011SMarkus Armbruster { 4116eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 4117eb852011SMarkus Armbruster bdrv_init(); 4118eb852011SMarkus Armbruster } 4119eb852011SMarkus Armbruster 4120d7331bedSStefan Hajnoczi void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 41216bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 41226bbff9a0Saliguori { 4123ce1a14dcSpbrook BlockDriverAIOCB *acb; 4124ce1a14dcSpbrook 4125d7331bedSStefan Hajnoczi acb = g_slice_alloc(aiocb_info->aiocb_size); 4126d7331bedSStefan Hajnoczi acb->aiocb_info = aiocb_info; 4127ce1a14dcSpbrook acb->bs = bs; 4128ce1a14dcSpbrook acb->cb = cb; 4129ce1a14dcSpbrook acb->opaque = opaque; 4130ce1a14dcSpbrook return acb; 4131ce1a14dcSpbrook } 4132ce1a14dcSpbrook 4133ce1a14dcSpbrook void qemu_aio_release(void *p) 4134ce1a14dcSpbrook { 4135d37c975fSStefan Hajnoczi BlockDriverAIOCB *acb = p; 4136d7331bedSStefan Hajnoczi g_slice_free1(acb->aiocb_info->aiocb_size, acb); 4137ce1a14dcSpbrook } 413819cb3738Sbellard 413919cb3738Sbellard /**************************************************************/ 4140f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 4141f9f05dc5SKevin Wolf 4142f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 4143f9f05dc5SKevin Wolf Coroutine *coroutine; 4144f9f05dc5SKevin Wolf int ret; 4145f9f05dc5SKevin Wolf } CoroutineIOCompletion; 4146f9f05dc5SKevin Wolf 4147f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 4148f9f05dc5SKevin Wolf { 4149f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 4150f9f05dc5SKevin Wolf 4151f9f05dc5SKevin Wolf co->ret = ret; 4152f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 4153f9f05dc5SKevin Wolf } 4154f9f05dc5SKevin Wolf 4155f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 4156f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 4157f9f05dc5SKevin Wolf bool is_write) 4158f9f05dc5SKevin Wolf { 4159f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 4160f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 4161f9f05dc5SKevin Wolf }; 4162f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 4163f9f05dc5SKevin Wolf 4164f9f05dc5SKevin Wolf if (is_write) { 4165a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 4166f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4167f9f05dc5SKevin Wolf } else { 4168a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 4169f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4170f9f05dc5SKevin Wolf } 4171f9f05dc5SKevin Wolf 417259370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 4173f9f05dc5SKevin Wolf if (!acb) { 4174f9f05dc5SKevin Wolf return -EIO; 4175f9f05dc5SKevin Wolf } 4176f9f05dc5SKevin Wolf qemu_coroutine_yield(); 4177f9f05dc5SKevin Wolf 4178f9f05dc5SKevin Wolf return co.ret; 4179f9f05dc5SKevin Wolf } 4180f9f05dc5SKevin Wolf 4181f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 4182f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4183f9f05dc5SKevin Wolf QEMUIOVector *iov) 4184f9f05dc5SKevin Wolf { 4185f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 4186f9f05dc5SKevin Wolf } 4187f9f05dc5SKevin Wolf 4188f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 4189f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4190f9f05dc5SKevin Wolf QEMUIOVector *iov) 4191f9f05dc5SKevin Wolf { 4192f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 4193f9f05dc5SKevin Wolf } 4194f9f05dc5SKevin Wolf 419507f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 4196e7a8a783SKevin Wolf { 419707f07615SPaolo Bonzini RwCo *rwco = opaque; 419807f07615SPaolo Bonzini 419907f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 420007f07615SPaolo Bonzini } 420107f07615SPaolo Bonzini 420207f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 420307f07615SPaolo Bonzini { 4204eb489bb1SKevin Wolf int ret; 4205eb489bb1SKevin Wolf 420629cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 420707f07615SPaolo Bonzini return 0; 4208eb489bb1SKevin Wolf } 4209eb489bb1SKevin Wolf 4210ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 4211bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); 4212eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 4213eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 4214eb489bb1SKevin Wolf if (ret < 0) { 4215eb489bb1SKevin Wolf return ret; 4216eb489bb1SKevin Wolf } 4217eb489bb1SKevin Wolf } 4218eb489bb1SKevin Wolf 4219ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 4220ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 4221d4c82329SKevin Wolf goto flush_parent; 4222ca716364SKevin Wolf } 4223ca716364SKevin Wolf 4224bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); 4225eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 422629cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 422707f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 422807f07615SPaolo Bonzini BlockDriverAIOCB *acb; 4229e7a8a783SKevin Wolf CoroutineIOCompletion co = { 4230e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 4231e7a8a783SKevin Wolf }; 4232e7a8a783SKevin Wolf 423307f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 423407f07615SPaolo Bonzini if (acb == NULL) { 423529cdb251SPaolo Bonzini ret = -EIO; 423607f07615SPaolo Bonzini } else { 4237e7a8a783SKevin Wolf qemu_coroutine_yield(); 423829cdb251SPaolo Bonzini ret = co.ret; 4239e7a8a783SKevin Wolf } 424007f07615SPaolo Bonzini } else { 424107f07615SPaolo Bonzini /* 424207f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 424307f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 424407f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 424507f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 424607f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 424707f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 424807f07615SPaolo Bonzini * mode. 424907f07615SPaolo Bonzini * 425007f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 425107f07615SPaolo Bonzini */ 425229cdb251SPaolo Bonzini ret = 0; 425307f07615SPaolo Bonzini } 425429cdb251SPaolo Bonzini if (ret < 0) { 425529cdb251SPaolo Bonzini return ret; 425629cdb251SPaolo Bonzini } 425729cdb251SPaolo Bonzini 425829cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 425929cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 426029cdb251SPaolo Bonzini */ 4261d4c82329SKevin Wolf flush_parent: 426229cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 426307f07615SPaolo Bonzini } 426407f07615SPaolo Bonzini 42650f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 42660f15423cSAnthony Liguori { 42670f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 42680f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 42690f15423cSAnthony Liguori } 42700f15423cSAnthony Liguori } 42710f15423cSAnthony Liguori 42720f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 42730f15423cSAnthony Liguori { 42740f15423cSAnthony Liguori BlockDriverState *bs; 42750f15423cSAnthony Liguori 42760f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 42770f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 42780f15423cSAnthony Liguori } 42790f15423cSAnthony Liguori } 42800f15423cSAnthony Liguori 428107789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 428207789269SBenoît Canet { 428307789269SBenoît Canet BlockDriverState *bs; 428407789269SBenoît Canet 428507789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 428607789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 428707789269SBenoît Canet } 428807789269SBenoît Canet } 428907789269SBenoît Canet 429007f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 429107f07615SPaolo Bonzini { 429207f07615SPaolo Bonzini Coroutine *co; 429307f07615SPaolo Bonzini RwCo rwco = { 429407f07615SPaolo Bonzini .bs = bs, 429507f07615SPaolo Bonzini .ret = NOT_DONE, 429607f07615SPaolo Bonzini }; 429707f07615SPaolo Bonzini 429807f07615SPaolo Bonzini if (qemu_in_coroutine()) { 429907f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 430007f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 430107f07615SPaolo Bonzini } else { 430207f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 430307f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 430407f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 430507f07615SPaolo Bonzini qemu_aio_wait(); 430607f07615SPaolo Bonzini } 430707f07615SPaolo Bonzini } 430807f07615SPaolo Bonzini 430907f07615SPaolo Bonzini return rwco.ret; 431007f07615SPaolo Bonzini } 4311e7a8a783SKevin Wolf 43124265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 43134265d620SPaolo Bonzini { 43144265d620SPaolo Bonzini RwCo *rwco = opaque; 43154265d620SPaolo Bonzini 43164265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 43174265d620SPaolo Bonzini } 43184265d620SPaolo Bonzini 43196f14da52SPeter Lieven /* if no limit is specified in the BlockLimits use a default 43206f14da52SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 43216f14da52SPeter Lieven */ 43226f14da52SPeter Lieven #define MAX_DISCARD_DEFAULT 32768 43236f14da52SPeter Lieven 43244265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 43254265d620SPaolo Bonzini int nb_sectors) 43264265d620SPaolo Bonzini { 4327d51e9fe5SPaolo Bonzini int max_discard; 4328d51e9fe5SPaolo Bonzini 43294265d620SPaolo Bonzini if (!bs->drv) { 43304265d620SPaolo Bonzini return -ENOMEDIUM; 43314265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 43324265d620SPaolo Bonzini return -EIO; 43334265d620SPaolo Bonzini } else if (bs->read_only) { 43344265d620SPaolo Bonzini return -EROFS; 4335df702c9bSPaolo Bonzini } 4336df702c9bSPaolo Bonzini 43378f0720ecSPaolo Bonzini bdrv_reset_dirty(bs, sector_num, nb_sectors); 4338df702c9bSPaolo Bonzini 43399e8f1835SPaolo Bonzini /* Do nothing if disabled. */ 43409e8f1835SPaolo Bonzini if (!(bs->open_flags & BDRV_O_UNMAP)) { 43419e8f1835SPaolo Bonzini return 0; 43429e8f1835SPaolo Bonzini } 43439e8f1835SPaolo Bonzini 4344d51e9fe5SPaolo Bonzini if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) { 4345d51e9fe5SPaolo Bonzini return 0; 4346d51e9fe5SPaolo Bonzini } 43476f14da52SPeter Lieven 4348d51e9fe5SPaolo Bonzini max_discard = bs->bl.max_discard ? bs->bl.max_discard : MAX_DISCARD_DEFAULT; 43496f14da52SPeter Lieven while (nb_sectors > 0) { 43506f14da52SPeter Lieven int ret; 43516f14da52SPeter Lieven int num = nb_sectors; 43526f14da52SPeter Lieven 43536f14da52SPeter Lieven /* align request */ 43546f14da52SPeter Lieven if (bs->bl.discard_alignment && 43556f14da52SPeter Lieven num >= bs->bl.discard_alignment && 43566f14da52SPeter Lieven sector_num % bs->bl.discard_alignment) { 43576f14da52SPeter Lieven if (num > bs->bl.discard_alignment) { 43586f14da52SPeter Lieven num = bs->bl.discard_alignment; 43596f14da52SPeter Lieven } 43606f14da52SPeter Lieven num -= sector_num % bs->bl.discard_alignment; 43616f14da52SPeter Lieven } 43626f14da52SPeter Lieven 43636f14da52SPeter Lieven /* limit request size */ 43646f14da52SPeter Lieven if (num > max_discard) { 43656f14da52SPeter Lieven num = max_discard; 43666f14da52SPeter Lieven } 43676f14da52SPeter Lieven 4368d51e9fe5SPaolo Bonzini if (bs->drv->bdrv_co_discard) { 43696f14da52SPeter Lieven ret = bs->drv->bdrv_co_discard(bs, sector_num, num); 4370d51e9fe5SPaolo Bonzini } else { 43714265d620SPaolo Bonzini BlockDriverAIOCB *acb; 43724265d620SPaolo Bonzini CoroutineIOCompletion co = { 43734265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 43744265d620SPaolo Bonzini }; 43754265d620SPaolo Bonzini 43764265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 43774265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 43784265d620SPaolo Bonzini if (acb == NULL) { 43794265d620SPaolo Bonzini return -EIO; 43804265d620SPaolo Bonzini } else { 43814265d620SPaolo Bonzini qemu_coroutine_yield(); 4382d51e9fe5SPaolo Bonzini ret = co.ret; 43834265d620SPaolo Bonzini } 4384d51e9fe5SPaolo Bonzini } 43857ce21016SPaolo Bonzini if (ret && ret != -ENOTSUP) { 4386d51e9fe5SPaolo Bonzini return ret; 4387d51e9fe5SPaolo Bonzini } 4388d51e9fe5SPaolo Bonzini 4389d51e9fe5SPaolo Bonzini sector_num += num; 4390d51e9fe5SPaolo Bonzini nb_sectors -= num; 4391d51e9fe5SPaolo Bonzini } 43924265d620SPaolo Bonzini return 0; 43934265d620SPaolo Bonzini } 43944265d620SPaolo Bonzini 43954265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 43964265d620SPaolo Bonzini { 43974265d620SPaolo Bonzini Coroutine *co; 43984265d620SPaolo Bonzini RwCo rwco = { 43994265d620SPaolo Bonzini .bs = bs, 44004265d620SPaolo Bonzini .sector_num = sector_num, 44014265d620SPaolo Bonzini .nb_sectors = nb_sectors, 44024265d620SPaolo Bonzini .ret = NOT_DONE, 44034265d620SPaolo Bonzini }; 44044265d620SPaolo Bonzini 44054265d620SPaolo Bonzini if (qemu_in_coroutine()) { 44064265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 44074265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 44084265d620SPaolo Bonzini } else { 44094265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 44104265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 44114265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 44124265d620SPaolo Bonzini qemu_aio_wait(); 44134265d620SPaolo Bonzini } 44144265d620SPaolo Bonzini } 44154265d620SPaolo Bonzini 44164265d620SPaolo Bonzini return rwco.ret; 44174265d620SPaolo Bonzini } 44184265d620SPaolo Bonzini 4419f9f05dc5SKevin Wolf /**************************************************************/ 442019cb3738Sbellard /* removable device support */ 442119cb3738Sbellard 442219cb3738Sbellard /** 442319cb3738Sbellard * Return TRUE if the media is present 442419cb3738Sbellard */ 442519cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 442619cb3738Sbellard { 442719cb3738Sbellard BlockDriver *drv = bs->drv; 4428a1aff5bfSMarkus Armbruster 442919cb3738Sbellard if (!drv) 443019cb3738Sbellard return 0; 443119cb3738Sbellard if (!drv->bdrv_is_inserted) 4432a1aff5bfSMarkus Armbruster return 1; 4433a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 443419cb3738Sbellard } 443519cb3738Sbellard 443619cb3738Sbellard /** 44378e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 44388e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 443919cb3738Sbellard */ 444019cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 444119cb3738Sbellard { 444219cb3738Sbellard BlockDriver *drv = bs->drv; 444319cb3738Sbellard 44448e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 44458e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 44468e49ca46SMarkus Armbruster } 44478e49ca46SMarkus Armbruster return -ENOTSUP; 444819cb3738Sbellard } 444919cb3738Sbellard 445019cb3738Sbellard /** 445119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 445219cb3738Sbellard */ 4453f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 445419cb3738Sbellard { 445519cb3738Sbellard BlockDriver *drv = bs->drv; 445619cb3738Sbellard 4457822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 4458822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 445919cb3738Sbellard } 44606f382ed2SLuiz Capitulino 44616f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 44626f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 44636f382ed2SLuiz Capitulino } 446419cb3738Sbellard } 446519cb3738Sbellard 446619cb3738Sbellard /** 446719cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 446819cb3738Sbellard * to eject it manually). 446919cb3738Sbellard */ 4470025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 447119cb3738Sbellard { 447219cb3738Sbellard BlockDriver *drv = bs->drv; 447319cb3738Sbellard 4474025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 4475b8c6d095SStefan Hajnoczi 4476025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 4477025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 447819cb3738Sbellard } 447919cb3738Sbellard } 4480985a03b0Sths 4481985a03b0Sths /* needed for generic scsi interface */ 4482985a03b0Sths 4483985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 4484985a03b0Sths { 4485985a03b0Sths BlockDriver *drv = bs->drv; 4486985a03b0Sths 4487985a03b0Sths if (drv && drv->bdrv_ioctl) 4488985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 4489985a03b0Sths return -ENOTSUP; 4490985a03b0Sths } 44917d780669Saliguori 4492221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 4493221f715dSaliguori unsigned long int req, void *buf, 44947d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 44957d780669Saliguori { 4496221f715dSaliguori BlockDriver *drv = bs->drv; 44977d780669Saliguori 4498221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 4499221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 4500221f715dSaliguori return NULL; 45017d780669Saliguori } 4502e268ca52Saliguori 45037b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 45047b6f9300SMarkus Armbruster { 45057b6f9300SMarkus Armbruster bs->buffer_alignment = align; 45067b6f9300SMarkus Armbruster } 45077cd1e32aSlirans@il.ibm.com 4508e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 4509e268ca52Saliguori { 4510e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 4511e268ca52Saliguori } 45127cd1e32aSlirans@il.ibm.com 4513c53b1c51SStefan Hajnoczi /* 4514c53b1c51SStefan Hajnoczi * Check if all memory in this vector is sector aligned. 4515c53b1c51SStefan Hajnoczi */ 4516c53b1c51SStefan Hajnoczi bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) 4517c53b1c51SStefan Hajnoczi { 4518c53b1c51SStefan Hajnoczi int i; 4519c53b1c51SStefan Hajnoczi 4520c53b1c51SStefan Hajnoczi for (i = 0; i < qiov->niov; i++) { 4521c53b1c51SStefan Hajnoczi if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { 4522c53b1c51SStefan Hajnoczi return false; 4523c53b1c51SStefan Hajnoczi } 4524c53b1c51SStefan Hajnoczi } 4525c53b1c51SStefan Hajnoczi 4526c53b1c51SStefan Hajnoczi return true; 4527c53b1c51SStefan Hajnoczi } 4528c53b1c51SStefan Hajnoczi 4529e4654d2dSFam Zheng BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity) 45307cd1e32aSlirans@il.ibm.com { 45317cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 4532e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4533a55eb92cSJan Kiszka 453450717e94SPaolo Bonzini assert((granularity & (granularity - 1)) == 0); 453550717e94SPaolo Bonzini 453650717e94SPaolo Bonzini granularity >>= BDRV_SECTOR_BITS; 4537e4654d2dSFam Zheng assert(granularity); 45388f0720ecSPaolo Bonzini bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); 4539e4654d2dSFam Zheng bitmap = g_malloc0(sizeof(BdrvDirtyBitmap)); 4540e4654d2dSFam Zheng bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); 4541e4654d2dSFam Zheng QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); 4542e4654d2dSFam Zheng return bitmap; 4543e4654d2dSFam Zheng } 4544e4654d2dSFam Zheng 4545e4654d2dSFam Zheng void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 4546e4654d2dSFam Zheng { 4547e4654d2dSFam Zheng BdrvDirtyBitmap *bm, *next; 4548e4654d2dSFam Zheng QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { 4549e4654d2dSFam Zheng if (bm == bitmap) { 4550e4654d2dSFam Zheng QLIST_REMOVE(bitmap, list); 4551e4654d2dSFam Zheng hbitmap_free(bitmap->bitmap); 4552e4654d2dSFam Zheng g_free(bitmap); 4553e4654d2dSFam Zheng return; 45547cd1e32aSlirans@il.ibm.com } 45557cd1e32aSlirans@il.ibm.com } 45567cd1e32aSlirans@il.ibm.com } 45577cd1e32aSlirans@il.ibm.com 455821b56835SFam Zheng BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 455921b56835SFam Zheng { 456021b56835SFam Zheng BdrvDirtyBitmap *bm; 456121b56835SFam Zheng BlockDirtyInfoList *list = NULL; 456221b56835SFam Zheng BlockDirtyInfoList **plist = &list; 456321b56835SFam Zheng 456421b56835SFam Zheng QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 456521b56835SFam Zheng BlockDirtyInfo *info = g_malloc0(sizeof(BlockDirtyInfo)); 456621b56835SFam Zheng BlockDirtyInfoList *entry = g_malloc0(sizeof(BlockDirtyInfoList)); 456721b56835SFam Zheng info->count = bdrv_get_dirty_count(bs, bm); 456821b56835SFam Zheng info->granularity = 456921b56835SFam Zheng ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bm->bitmap)); 457021b56835SFam Zheng entry->value = info; 457121b56835SFam Zheng *plist = entry; 457221b56835SFam Zheng plist = &entry->next; 457321b56835SFam Zheng } 457421b56835SFam Zheng 457521b56835SFam Zheng return list; 457621b56835SFam Zheng } 457721b56835SFam Zheng 4578e4654d2dSFam Zheng int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector) 45797cd1e32aSlirans@il.ibm.com { 4580e4654d2dSFam Zheng if (bitmap) { 4581e4654d2dSFam Zheng return hbitmap_get(bitmap->bitmap, sector); 45827cd1e32aSlirans@il.ibm.com } else { 45837cd1e32aSlirans@il.ibm.com return 0; 45847cd1e32aSlirans@il.ibm.com } 45857cd1e32aSlirans@il.ibm.com } 45867cd1e32aSlirans@il.ibm.com 4587e4654d2dSFam Zheng void bdrv_dirty_iter_init(BlockDriverState *bs, 4588e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap, HBitmapIter *hbi) 45891755da16SPaolo Bonzini { 4590e4654d2dSFam Zheng hbitmap_iter_init(hbi, bitmap->bitmap, 0); 45911755da16SPaolo Bonzini } 45921755da16SPaolo Bonzini 45931755da16SPaolo Bonzini void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 45941755da16SPaolo Bonzini int nr_sectors) 45951755da16SPaolo Bonzini { 4596e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4597e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 4598e4654d2dSFam Zheng hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 4599e4654d2dSFam Zheng } 46001755da16SPaolo Bonzini } 46011755da16SPaolo Bonzini 4602e4654d2dSFam Zheng void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors) 46037cd1e32aSlirans@il.ibm.com { 4604e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4605e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 4606e4654d2dSFam Zheng hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); 4607e4654d2dSFam Zheng } 46087cd1e32aSlirans@il.ibm.com } 4609aaa0eb75SLiran Schour 4610e4654d2dSFam Zheng int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 4611aaa0eb75SLiran Schour { 4612e4654d2dSFam Zheng return hbitmap_count(bitmap->bitmap); 4613aaa0eb75SLiran Schour } 4614f88e1a42SJes Sorensen 46159fcb0251SFam Zheng /* Get a reference to bs */ 46169fcb0251SFam Zheng void bdrv_ref(BlockDriverState *bs) 46179fcb0251SFam Zheng { 46189fcb0251SFam Zheng bs->refcnt++; 46199fcb0251SFam Zheng } 46209fcb0251SFam Zheng 46219fcb0251SFam Zheng /* Release a previously grabbed reference to bs. 46229fcb0251SFam Zheng * If after releasing, reference count is zero, the BlockDriverState is 46239fcb0251SFam Zheng * deleted. */ 46249fcb0251SFam Zheng void bdrv_unref(BlockDriverState *bs) 46259fcb0251SFam Zheng { 46269fcb0251SFam Zheng assert(bs->refcnt > 0); 46279fcb0251SFam Zheng if (--bs->refcnt == 0) { 46289fcb0251SFam Zheng bdrv_delete(bs); 46299fcb0251SFam Zheng } 46309fcb0251SFam Zheng } 46319fcb0251SFam Zheng 4632db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 4633db593f25SMarcelo Tosatti { 4634db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 4635db593f25SMarcelo Tosatti bs->in_use = in_use; 4636db593f25SMarcelo Tosatti } 4637db593f25SMarcelo Tosatti 4638db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 4639db593f25SMarcelo Tosatti { 4640db593f25SMarcelo Tosatti return bs->in_use; 4641db593f25SMarcelo Tosatti } 4642db593f25SMarcelo Tosatti 464328a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 464428a7282aSLuiz Capitulino { 4645d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 464658e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 464728a7282aSLuiz Capitulino } 464828a7282aSLuiz Capitulino 464928a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 465028a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 465128a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 465228a7282aSLuiz Capitulino { 4653d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 465492aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 465592aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 465692aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 465728a7282aSLuiz Capitulino } 465828a7282aSLuiz Capitulino 465928a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 466028a7282aSLuiz Capitulino { 4661d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 466228a7282aSLuiz Capitulino } 466328a7282aSLuiz Capitulino 466428a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 466528a7282aSLuiz Capitulino { 466628a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 466758e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 46683bd293c3SPaolo Bonzini if (bs->job) { 46693bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 46703bd293c3SPaolo Bonzini } 467128a7282aSLuiz Capitulino } 467228a7282aSLuiz Capitulino } 467328a7282aSLuiz Capitulino 467428a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 467528a7282aSLuiz Capitulino { 46763e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 46773e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 467858e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 467958e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 468028a7282aSLuiz Capitulino } 468128a7282aSLuiz Capitulino } 468228a7282aSLuiz Capitulino 4683a597e79cSChristoph Hellwig void 4684a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 4685a597e79cSChristoph Hellwig enum BlockAcctType type) 4686a597e79cSChristoph Hellwig { 4687a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 4688a597e79cSChristoph Hellwig 4689a597e79cSChristoph Hellwig cookie->bytes = bytes; 4690c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4691a597e79cSChristoph Hellwig cookie->type = type; 4692a597e79cSChristoph Hellwig } 4693a597e79cSChristoph Hellwig 4694a597e79cSChristoph Hellwig void 4695a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4696a597e79cSChristoph Hellwig { 4697a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4698a597e79cSChristoph Hellwig 4699a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4700a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4701c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4702a597e79cSChristoph Hellwig } 4703a597e79cSChristoph Hellwig 4704d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 4705f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 4706f382d43aSMiroslav Rezanina char *options, uint64_t img_size, int flags, 4707f382d43aSMiroslav Rezanina Error **errp, bool quiet) 4708f88e1a42SJes Sorensen { 4709f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4710d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4711f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 471296df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4713cc84d90fSMax Reitz Error *local_err = NULL; 4714f88e1a42SJes Sorensen int ret = 0; 4715f88e1a42SJes Sorensen 4716f88e1a42SJes Sorensen /* Find driver and parse its options */ 4717f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4718f88e1a42SJes Sorensen if (!drv) { 471971c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 4720d92ada22SLuiz Capitulino return; 4721f88e1a42SJes Sorensen } 4722f88e1a42SJes Sorensen 472398289620SKevin Wolf proto_drv = bdrv_find_protocol(filename, true); 4724f88e1a42SJes Sorensen if (!proto_drv) { 472571c79813SLuiz Capitulino error_setg(errp, "Unknown protocol '%s'", filename); 4726d92ada22SLuiz Capitulino return; 4727f88e1a42SJes Sorensen } 4728f88e1a42SJes Sorensen 4729f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4730f88e1a42SJes Sorensen drv->create_options); 4731f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4732f88e1a42SJes Sorensen proto_drv->create_options); 4733f88e1a42SJes Sorensen 4734f88e1a42SJes Sorensen /* Create parameter list with default values */ 4735f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4736f88e1a42SJes Sorensen 4737f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4738f88e1a42SJes Sorensen 4739f88e1a42SJes Sorensen /* Parse -o options */ 4740f88e1a42SJes Sorensen if (options) { 4741f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4742f88e1a42SJes Sorensen if (param == NULL) { 474371c79813SLuiz Capitulino error_setg(errp, "Invalid options for file format '%s'.", fmt); 4744f88e1a42SJes Sorensen goto out; 4745f88e1a42SJes Sorensen } 4746f88e1a42SJes Sorensen } 4747f88e1a42SJes Sorensen 4748f88e1a42SJes Sorensen if (base_filename) { 4749f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4750f88e1a42SJes Sorensen base_filename)) { 475171c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 475271c79813SLuiz Capitulino fmt); 4753f88e1a42SJes Sorensen goto out; 4754f88e1a42SJes Sorensen } 4755f88e1a42SJes Sorensen } 4756f88e1a42SJes Sorensen 4757f88e1a42SJes Sorensen if (base_fmt) { 4758f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 475971c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 476071c79813SLuiz Capitulino "format '%s'", fmt); 4761f88e1a42SJes Sorensen goto out; 4762f88e1a42SJes Sorensen } 4763f88e1a42SJes Sorensen } 4764f88e1a42SJes Sorensen 4765792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4766792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4767792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 476871c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 476971c79813SLuiz Capitulino "same filename as the backing file"); 4770792da93aSJes Sorensen goto out; 4771792da93aSJes Sorensen } 4772792da93aSJes Sorensen } 4773792da93aSJes Sorensen 4774f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4775f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 477696df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 477796df67d1SStefan Hajnoczi if (!backing_drv) { 477871c79813SLuiz Capitulino error_setg(errp, "Unknown backing file format '%s'", 477971c79813SLuiz Capitulino backing_fmt->value.s); 4780f88e1a42SJes Sorensen goto out; 4781f88e1a42SJes Sorensen } 4782f88e1a42SJes Sorensen } 4783f88e1a42SJes Sorensen 4784f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4785f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4786d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4787d220894eSKevin Wolf if (size && size->value.n == -1) { 4788f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 478966f6b814SMax Reitz BlockDriverState *bs; 4790f88e1a42SJes Sorensen uint64_t size; 4791f88e1a42SJes Sorensen char buf[32]; 479263090dacSPaolo Bonzini int back_flags; 479363090dacSPaolo Bonzini 479463090dacSPaolo Bonzini /* backing files always opened read-only */ 479563090dacSPaolo Bonzini back_flags = 479663090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 4797f88e1a42SJes Sorensen 4798f88e1a42SJes Sorensen bs = bdrv_new(""); 4799f88e1a42SJes Sorensen 4800de9c0cecSKevin Wolf ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags, 4801cc84d90fSMax Reitz backing_drv, &local_err); 4802f88e1a42SJes Sorensen if (ret < 0) { 4803cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not open '%s': %s", 4804cc84d90fSMax Reitz backing_file->value.s, 4805cc84d90fSMax Reitz error_get_pretty(local_err)); 4806cc84d90fSMax Reitz error_free(local_err); 4807cc84d90fSMax Reitz local_err = NULL; 480866f6b814SMax Reitz bdrv_unref(bs); 4809f88e1a42SJes Sorensen goto out; 4810f88e1a42SJes Sorensen } 4811f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4812f88e1a42SJes Sorensen size *= 512; 4813f88e1a42SJes Sorensen 4814f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4815f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 481666f6b814SMax Reitz 481766f6b814SMax Reitz bdrv_unref(bs); 4818f88e1a42SJes Sorensen } else { 481971c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 4820f88e1a42SJes Sorensen goto out; 4821f88e1a42SJes Sorensen } 4822f88e1a42SJes Sorensen } 4823f88e1a42SJes Sorensen 4824f382d43aSMiroslav Rezanina if (!quiet) { 4825f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4826f88e1a42SJes Sorensen print_option_parameters(param); 4827f88e1a42SJes Sorensen puts(""); 4828f382d43aSMiroslav Rezanina } 4829cc84d90fSMax Reitz ret = bdrv_create(drv, filename, param, &local_err); 4830cc84d90fSMax Reitz if (ret == -EFBIG) { 4831cc84d90fSMax Reitz /* This is generally a better message than whatever the driver would 4832cc84d90fSMax Reitz * deliver (especially because of the cluster_size_hint), since that 4833cc84d90fSMax Reitz * is most probably not much different from "image too large". */ 4834f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 4835f3f4d2c0SKevin Wolf if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) { 4836f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 4837f3f4d2c0SKevin Wolf } 4838cc84d90fSMax Reitz error_setg(errp, "The image size is too large for file format '%s'" 4839cc84d90fSMax Reitz "%s", fmt, cluster_size_hint); 4840cc84d90fSMax Reitz error_free(local_err); 4841cc84d90fSMax Reitz local_err = NULL; 4842f88e1a42SJes Sorensen } 4843f88e1a42SJes Sorensen 4844f88e1a42SJes Sorensen out: 4845f88e1a42SJes Sorensen free_option_parameters(create_options); 4846f88e1a42SJes Sorensen free_option_parameters(param); 4847f88e1a42SJes Sorensen 4848cc84d90fSMax Reitz if (error_is_set(&local_err)) { 4849cc84d90fSMax Reitz error_propagate(errp, local_err); 4850cc84d90fSMax Reitz } 4851f88e1a42SJes Sorensen } 485285d126f3SStefan Hajnoczi 485385d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 485485d126f3SStefan Hajnoczi { 485585d126f3SStefan Hajnoczi /* Currently BlockDriverState always uses the main loop AioContext */ 485685d126f3SStefan Hajnoczi return qemu_get_aio_context(); 485785d126f3SStefan Hajnoczi } 4858d616b224SStefan Hajnoczi 4859d616b224SStefan Hajnoczi void bdrv_add_before_write_notifier(BlockDriverState *bs, 4860d616b224SStefan Hajnoczi NotifierWithReturn *notifier) 4861d616b224SStefan Hajnoczi { 4862d616b224SStefan Hajnoczi notifier_with_return_list_add(&bs->before_write_notifiers, notifier); 4863d616b224SStefan Hajnoczi } 48646f176b48SMax Reitz 48656f176b48SMax Reitz int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options) 48666f176b48SMax Reitz { 48676f176b48SMax Reitz if (bs->drv->bdrv_amend_options == NULL) { 48686f176b48SMax Reitz return -ENOTSUP; 48696f176b48SMax Reitz } 48706f176b48SMax Reitz return bs->drv->bdrv_amend_options(bs, options); 48716f176b48SMax Reitz } 4872f6186f49SBenoît Canet 4873f6186f49SBenoît Canet ExtSnapshotPerm bdrv_check_ext_snapshot(BlockDriverState *bs) 4874f6186f49SBenoît Canet { 4875f6186f49SBenoît Canet if (bs->drv->bdrv_check_ext_snapshot) { 4876f6186f49SBenoît Canet return bs->drv->bdrv_check_ext_snapshot(bs); 4877f6186f49SBenoît Canet } 4878f6186f49SBenoît Canet 4879f6186f49SBenoît Canet if (bs->file && bs->file->drv && bs->file->drv->bdrv_check_ext_snapshot) { 4880f6186f49SBenoît Canet return bs->file->drv->bdrv_check_ext_snapshot(bs); 4881f6186f49SBenoît Canet } 4882f6186f49SBenoît Canet 4883f6186f49SBenoît Canet /* external snapshots are allowed by default */ 4884f6186f49SBenoît Canet return EXT_SNAPSHOT_ALLOWED; 4885f6186f49SBenoît Canet } 4886f6186f49SBenoît Canet 4887f6186f49SBenoît Canet ExtSnapshotPerm bdrv_check_ext_snapshot_forbidden(BlockDriverState *bs) 4888f6186f49SBenoît Canet { 4889f6186f49SBenoît Canet return EXT_SNAPSHOT_FORBIDDEN; 4890f6186f49SBenoît Canet } 4891