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 /* FIXME: We do not have timer support here, so this is effectively 15614c355d53SZhi Yong Wu * a busy wait. 15624c355d53SZhi Yong Wu */ 15634c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 1564cc0681c4SBenoît Canet if (bdrv_start_throttled_reqs(bs)) { 15654c355d53SZhi Yong Wu busy = true; 15664c355d53SZhi Yong Wu } 15674c355d53SZhi Yong Wu } 1568922453bcSStefan Hajnoczi 156988266f5aSStefan Hajnoczi busy = bdrv_requests_pending_all(); 157088266f5aSStefan Hajnoczi busy |= aio_poll(qemu_get_aio_context(), busy); 1571922453bcSStefan Hajnoczi } 1572922453bcSStefan Hajnoczi } 1573922453bcSStefan Hajnoczi 1574d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 1575d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1576d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1577d22b2f41SRyan Harper { 1578d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 1579d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 1580d22b2f41SRyan Harper } 1581d22b2f41SRyan Harper bs->device_name[0] = '\0'; 1582d22b2f41SRyan Harper } 1583d22b2f41SRyan Harper 1584e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1585e023b2e2SPaolo Bonzini { 1586e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1587e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1588e023b2e2SPaolo Bonzini } 1589e023b2e2SPaolo Bonzini } 1590e023b2e2SPaolo Bonzini 15914ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 15924ddc07caSPaolo Bonzini BlockDriverState *bs_src) 15934ddc07caSPaolo Bonzini { 15944ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 15954ddc07caSPaolo Bonzini bs_dest->open_flags = bs_src->open_flags; 15964ddc07caSPaolo Bonzini 15974ddc07caSPaolo Bonzini /* dev info */ 15984ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 15994ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 16004ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 16014ddc07caSPaolo Bonzini bs_dest->buffer_alignment = bs_src->buffer_alignment; 16024ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 16034ddc07caSPaolo Bonzini 16044ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 16054ddc07caSPaolo Bonzini 1606cc0681c4SBenoît Canet /* i/o throttled req */ 1607cc0681c4SBenoît Canet memcpy(&bs_dest->throttle_state, 1608cc0681c4SBenoît Canet &bs_src->throttle_state, 1609cc0681c4SBenoît Canet sizeof(ThrottleState)); 1610cc0681c4SBenoît Canet bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0]; 1611cc0681c4SBenoît Canet bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1]; 16124ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 16134ddc07caSPaolo Bonzini 16144ddc07caSPaolo Bonzini /* r/w error */ 16154ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 16164ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 16174ddc07caSPaolo Bonzini 16184ddc07caSPaolo Bonzini /* i/o status */ 16194ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 16204ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 16214ddc07caSPaolo Bonzini 16224ddc07caSPaolo Bonzini /* dirty bitmap */ 1623e4654d2dSFam Zheng bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps; 16244ddc07caSPaolo Bonzini 16259fcb0251SFam Zheng /* reference count */ 16269fcb0251SFam Zheng bs_dest->refcnt = bs_src->refcnt; 16279fcb0251SFam Zheng 16284ddc07caSPaolo Bonzini /* job */ 16294ddc07caSPaolo Bonzini bs_dest->in_use = bs_src->in_use; 16304ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 16314ddc07caSPaolo Bonzini 16324ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 16334ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 16344ddc07caSPaolo Bonzini bs_src->device_name); 16354ddc07caSPaolo Bonzini bs_dest->list = bs_src->list; 16364ddc07caSPaolo Bonzini } 16374ddc07caSPaolo Bonzini 16384ddc07caSPaolo Bonzini /* 16394ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 16404ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 16414ddc07caSPaolo Bonzini * actually attached to a device. 16424ddc07caSPaolo Bonzini * 16434ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 16444ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 16454ddc07caSPaolo Bonzini * 16464ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 16474ddc07caSPaolo Bonzini * 16484ddc07caSPaolo Bonzini * This function does not create any image files. 16494ddc07caSPaolo Bonzini */ 16504ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 16514ddc07caSPaolo Bonzini { 16524ddc07caSPaolo Bonzini BlockDriverState tmp; 16534ddc07caSPaolo Bonzini 16544ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 16554ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 1656e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs_new->dirty_bitmaps)); 16574ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 16584ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 16594ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 16604ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1661cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 16624ddc07caSPaolo Bonzini 16634ddc07caSPaolo Bonzini tmp = *bs_new; 16644ddc07caSPaolo Bonzini *bs_new = *bs_old; 16654ddc07caSPaolo Bonzini *bs_old = tmp; 16664ddc07caSPaolo Bonzini 16674ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 16684ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 16694ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 16704ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 16714ddc07caSPaolo Bonzini 16724ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 16734ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 16744ddc07caSPaolo Bonzini 16754ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 16764ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 16774ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 16784ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 16794ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1680cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 16814ddc07caSPaolo Bonzini 16824ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 16834ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 16844ddc07caSPaolo Bonzini } 16854ddc07caSPaolo Bonzini 16868802d1fdSJeff Cody /* 16878802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 16888802d1fdSJeff Cody * live, while keeping required fields on the top layer. 16898802d1fdSJeff Cody * 16908802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 16918802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 16928802d1fdSJeff Cody * 1693f6801b83SJeff Cody * bs_new is required to be anonymous. 1694f6801b83SJeff Cody * 16958802d1fdSJeff Cody * This function does not create any image files. 16968802d1fdSJeff Cody */ 16978802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 16988802d1fdSJeff Cody { 16994ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 17008802d1fdSJeff Cody 17018802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 17028802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 17034ddc07caSPaolo Bonzini bs_top->backing_hd = bs_new; 17044ddc07caSPaolo Bonzini bs_top->open_flags &= ~BDRV_O_NO_BACKING; 17054ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 17064ddc07caSPaolo Bonzini bs_new->filename); 17074ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 17084ddc07caSPaolo Bonzini bs_new->drv ? bs_new->drv->format_name : ""); 17098802d1fdSJeff Cody } 17108802d1fdSJeff Cody 17114f6fd349SFam Zheng static void bdrv_delete(BlockDriverState *bs) 1712b338082bSbellard { 1713fa879d62SMarkus Armbruster assert(!bs->dev); 17143e914655SPaolo Bonzini assert(!bs->job); 17153e914655SPaolo Bonzini assert(!bs->in_use); 17164f6fd349SFam Zheng assert(!bs->refcnt); 1717e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 171818846deeSMarkus Armbruster 1719e1b5c52eSStefan Hajnoczi bdrv_close(bs); 1720e1b5c52eSStefan Hajnoczi 17211b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1722d22b2f41SRyan Harper bdrv_make_anon(bs); 172334c6f050Saurel32 17247267c094SAnthony Liguori g_free(bs); 1725fc01f7e7Sbellard } 1726fc01f7e7Sbellard 1727fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1728fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 172918846deeSMarkus Armbruster { 1730fa879d62SMarkus Armbruster if (bs->dev) { 173118846deeSMarkus Armbruster return -EBUSY; 173218846deeSMarkus Armbruster } 1733fa879d62SMarkus Armbruster bs->dev = dev; 173428a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 173518846deeSMarkus Armbruster return 0; 173618846deeSMarkus Armbruster } 173718846deeSMarkus Armbruster 1738fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1739fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 174018846deeSMarkus Armbruster { 1741fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1742fa879d62SMarkus Armbruster abort(); 1743fa879d62SMarkus Armbruster } 1744fa879d62SMarkus Armbruster } 1745fa879d62SMarkus Armbruster 1746fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1747fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1748fa879d62SMarkus Armbruster { 1749fa879d62SMarkus Armbruster assert(bs->dev == dev); 1750fa879d62SMarkus Armbruster bs->dev = NULL; 17510e49de52SMarkus Armbruster bs->dev_ops = NULL; 17520e49de52SMarkus Armbruster bs->dev_opaque = NULL; 175329e05f20SMarkus Armbruster bs->buffer_alignment = 512; 175418846deeSMarkus Armbruster } 175518846deeSMarkus Armbruster 1756fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1757fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 175818846deeSMarkus Armbruster { 1759fa879d62SMarkus Armbruster return bs->dev; 176018846deeSMarkus Armbruster } 176118846deeSMarkus Armbruster 17620e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 17630e49de52SMarkus Armbruster void *opaque) 17640e49de52SMarkus Armbruster { 17650e49de52SMarkus Armbruster bs->dev_ops = ops; 17660e49de52SMarkus Armbruster bs->dev_opaque = opaque; 17670e49de52SMarkus Armbruster } 17680e49de52SMarkus Armbruster 176932c81a4aSPaolo Bonzini void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 177032c81a4aSPaolo Bonzini enum MonitorEvent ev, 17711ceee0d5SPaolo Bonzini BlockErrorAction action, bool is_read) 1772329c0a48SLuiz Capitulino { 1773329c0a48SLuiz Capitulino QObject *data; 1774329c0a48SLuiz Capitulino const char *action_str; 1775329c0a48SLuiz Capitulino 1776329c0a48SLuiz Capitulino switch (action) { 1777329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1778329c0a48SLuiz Capitulino action_str = "report"; 1779329c0a48SLuiz Capitulino break; 1780329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1781329c0a48SLuiz Capitulino action_str = "ignore"; 1782329c0a48SLuiz Capitulino break; 1783329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1784329c0a48SLuiz Capitulino action_str = "stop"; 1785329c0a48SLuiz Capitulino break; 1786329c0a48SLuiz Capitulino default: 1787329c0a48SLuiz Capitulino abort(); 1788329c0a48SLuiz Capitulino } 1789329c0a48SLuiz Capitulino 1790329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1791329c0a48SLuiz Capitulino bdrv->device_name, 1792329c0a48SLuiz Capitulino action_str, 1793329c0a48SLuiz Capitulino is_read ? "read" : "write"); 179432c81a4aSPaolo Bonzini monitor_protocol_event(ev, data); 1795329c0a48SLuiz Capitulino 1796329c0a48SLuiz Capitulino qobject_decref(data); 1797329c0a48SLuiz Capitulino } 1798329c0a48SLuiz Capitulino 17996f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 18006f382ed2SLuiz Capitulino { 18016f382ed2SLuiz Capitulino QObject *data; 18026f382ed2SLuiz Capitulino 18036f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 18046f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 18056f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 18066f382ed2SLuiz Capitulino 18076f382ed2SLuiz Capitulino qobject_decref(data); 18086f382ed2SLuiz Capitulino } 18096f382ed2SLuiz Capitulino 18107d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 18110e49de52SMarkus Armbruster { 1812145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 18136f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 18147d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 18156f382ed2SLuiz Capitulino if (tray_was_closed) { 18166f382ed2SLuiz Capitulino /* tray open */ 18176f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 18186f382ed2SLuiz Capitulino } 18196f382ed2SLuiz Capitulino if (load) { 18206f382ed2SLuiz Capitulino /* tray close */ 18216f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 18226f382ed2SLuiz Capitulino } 1823145feb17SMarkus Armbruster } 1824145feb17SMarkus Armbruster } 1825145feb17SMarkus Armbruster 18262c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 18272c6942faSMarkus Armbruster { 18282c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 18292c6942faSMarkus Armbruster } 18302c6942faSMarkus Armbruster 1831025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1832025ccaa7SPaolo Bonzini { 1833025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1834025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1835025ccaa7SPaolo Bonzini } 1836025ccaa7SPaolo Bonzini } 1837025ccaa7SPaolo Bonzini 1838e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1839e4def80bSMarkus Armbruster { 1840e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1841e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1842e4def80bSMarkus Armbruster } 1843e4def80bSMarkus Armbruster return false; 1844e4def80bSMarkus Armbruster } 1845e4def80bSMarkus Armbruster 1846145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1847145feb17SMarkus Armbruster { 1848145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1849145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 18500e49de52SMarkus Armbruster } 18510e49de52SMarkus Armbruster } 18520e49de52SMarkus Armbruster 1853f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1854f107639aSMarkus Armbruster { 1855f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1856f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1857f107639aSMarkus Armbruster } 1858f107639aSMarkus Armbruster return false; 1859f107639aSMarkus Armbruster } 1860f107639aSMarkus Armbruster 1861e97fc193Saliguori /* 1862e97fc193Saliguori * Run consistency checks on an image 1863e97fc193Saliguori * 1864e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1865a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1866e076f338SKevin Wolf * check are stored in res. 1867e97fc193Saliguori */ 18684534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 1869e97fc193Saliguori { 1870e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1871e97fc193Saliguori return -ENOTSUP; 1872e97fc193Saliguori } 1873e97fc193Saliguori 1874e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 18754534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 1876e97fc193Saliguori } 1877e97fc193Saliguori 18788a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 18798a426614SKevin Wolf 188033e3963eSbellard /* commit COW file into the raw image */ 188133e3963eSbellard int bdrv_commit(BlockDriverState *bs) 188233e3963eSbellard { 188319cb3738Sbellard BlockDriver *drv = bs->drv; 18848a426614SKevin Wolf int64_t sector, total_sectors; 18858a426614SKevin Wolf int n, ro, open_flags; 18860bce597dSJeff Cody int ret = 0; 18878a426614SKevin Wolf uint8_t *buf; 1888c2cba3d9SJim Meyering char filename[PATH_MAX]; 188933e3963eSbellard 189019cb3738Sbellard if (!drv) 189119cb3738Sbellard return -ENOMEDIUM; 189233e3963eSbellard 18934dca4b63SNaphtali Sprei if (!bs->backing_hd) { 18944dca4b63SNaphtali Sprei return -ENOTSUP; 18954dca4b63SNaphtali Sprei } 18964dca4b63SNaphtali Sprei 18972d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 18982d3735d3SStefan Hajnoczi return -EBUSY; 18992d3735d3SStefan Hajnoczi } 19002d3735d3SStefan Hajnoczi 19014dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 1902c2cba3d9SJim Meyering /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ 1903c2cba3d9SJim Meyering pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); 19044dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 19054dca4b63SNaphtali Sprei 19064dca4b63SNaphtali Sprei if (ro) { 19070bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 19080bce597dSJeff Cody return -EACCES; 19094dca4b63SNaphtali Sprei } 1910ea2384d3Sbellard } 1911ea2384d3Sbellard 19126ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 19137267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 19148a426614SKevin Wolf 19158a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 1916d663640cSPaolo Bonzini ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); 1917d663640cSPaolo Bonzini if (ret < 0) { 1918d663640cSPaolo Bonzini goto ro_cleanup; 1919d663640cSPaolo Bonzini } 1920d663640cSPaolo Bonzini if (ret) { 19218a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 19224dca4b63SNaphtali Sprei ret = -EIO; 19234dca4b63SNaphtali Sprei goto ro_cleanup; 192433e3963eSbellard } 192533e3963eSbellard 19268a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 19274dca4b63SNaphtali Sprei ret = -EIO; 19284dca4b63SNaphtali Sprei goto ro_cleanup; 192933e3963eSbellard } 193033e3963eSbellard } 193133e3963eSbellard } 193295389c86Sbellard 19331d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 19341d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 19351d44952fSChristoph Hellwig bdrv_flush(bs); 19361d44952fSChristoph Hellwig } 193795389c86Sbellard 19383f5075aeSChristoph Hellwig /* 19393f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 19403f5075aeSChristoph Hellwig * stable on disk. 19413f5075aeSChristoph Hellwig */ 19423f5075aeSChristoph Hellwig if (bs->backing_hd) 19433f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 19444dca4b63SNaphtali Sprei 19454dca4b63SNaphtali Sprei ro_cleanup: 19467267c094SAnthony Liguori g_free(buf); 19474dca4b63SNaphtali Sprei 19484dca4b63SNaphtali Sprei if (ro) { 19490bce597dSJeff Cody /* ignoring error return here */ 19500bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 19514dca4b63SNaphtali Sprei } 19524dca4b63SNaphtali Sprei 19531d44952fSChristoph Hellwig return ret; 195433e3963eSbellard } 195533e3963eSbellard 1956e8877497SStefan Hajnoczi int bdrv_commit_all(void) 19576ab4b5abSMarkus Armbruster { 19586ab4b5abSMarkus Armbruster BlockDriverState *bs; 19596ab4b5abSMarkus Armbruster 19606ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1961272d2d8eSJeff Cody if (bs->drv && bs->backing_hd) { 1962e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1963e8877497SStefan Hajnoczi if (ret < 0) { 1964e8877497SStefan Hajnoczi return ret; 19656ab4b5abSMarkus Armbruster } 19666ab4b5abSMarkus Armbruster } 1967272d2d8eSJeff Cody } 1968e8877497SStefan Hajnoczi return 0; 1969e8877497SStefan Hajnoczi } 19706ab4b5abSMarkus Armbruster 1971dbffbdcfSStefan Hajnoczi /** 1972dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1973dbffbdcfSStefan Hajnoczi * 1974dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1975dbffbdcfSStefan Hajnoczi */ 1976dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1977dbffbdcfSStefan Hajnoczi { 1978dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1979f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1980dbffbdcfSStefan Hajnoczi } 1981dbffbdcfSStefan Hajnoczi 1982dbffbdcfSStefan Hajnoczi /** 1983dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1984dbffbdcfSStefan Hajnoczi */ 1985dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1986dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1987dbffbdcfSStefan Hajnoczi int64_t sector_num, 1988dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1989dbffbdcfSStefan Hajnoczi { 1990dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1991dbffbdcfSStefan Hajnoczi .bs = bs, 1992dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1993dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1994dbffbdcfSStefan Hajnoczi .is_write = is_write, 19955f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1996dbffbdcfSStefan Hajnoczi }; 1997dbffbdcfSStefan Hajnoczi 1998f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 1999f4658285SStefan Hajnoczi 2000dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 2001dbffbdcfSStefan Hajnoczi } 2002dbffbdcfSStefan Hajnoczi 2003d83947acSStefan Hajnoczi /** 2004d83947acSStefan Hajnoczi * Round a region to cluster boundaries 2005d83947acSStefan Hajnoczi */ 2006343bded4SPaolo Bonzini void bdrv_round_to_clusters(BlockDriverState *bs, 2007d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 2008d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 2009d83947acSStefan Hajnoczi int *cluster_nb_sectors) 2010d83947acSStefan Hajnoczi { 2011d83947acSStefan Hajnoczi BlockDriverInfo bdi; 2012d83947acSStefan Hajnoczi 2013d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 2014d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 2015d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 2016d83947acSStefan Hajnoczi } else { 2017d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 2018d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 2019d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 2020d83947acSStefan Hajnoczi nb_sectors, c); 2021d83947acSStefan Hajnoczi } 2022d83947acSStefan Hajnoczi } 2023d83947acSStefan Hajnoczi 2024f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 2025f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 2026d83947acSStefan Hajnoczi /* aaaa bbbb */ 2027d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 2028d83947acSStefan Hajnoczi return false; 2029d83947acSStefan Hajnoczi } 2030d83947acSStefan Hajnoczi /* bbbb aaaa */ 2031d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 2032d83947acSStefan Hajnoczi return false; 2033d83947acSStefan Hajnoczi } 2034d83947acSStefan Hajnoczi return true; 2035f4658285SStefan Hajnoczi } 2036f4658285SStefan Hajnoczi 2037f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 2038f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 2039f4658285SStefan Hajnoczi { 2040f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 2041d83947acSStefan Hajnoczi int64_t cluster_sector_num; 2042d83947acSStefan Hajnoczi int cluster_nb_sectors; 2043f4658285SStefan Hajnoczi bool retry; 2044f4658285SStefan Hajnoczi 2045d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 2046d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 2047d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 2048d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 2049d83947acSStefan Hajnoczi * interleave between them. 2050d83947acSStefan Hajnoczi */ 2051343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2052d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2053d83947acSStefan Hajnoczi 2054f4658285SStefan Hajnoczi do { 2055f4658285SStefan Hajnoczi retry = false; 2056f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 2057d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 2058d83947acSStefan Hajnoczi cluster_nb_sectors)) { 20595f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 20605f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 20615f8b6491SStefan Hajnoczi * never happen since it means deadlock. 20625f8b6491SStefan Hajnoczi */ 20635f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 20645f8b6491SStefan Hajnoczi 2065f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 2066f4658285SStefan Hajnoczi retry = true; 2067f4658285SStefan Hajnoczi break; 2068f4658285SStefan Hajnoczi } 2069f4658285SStefan Hajnoczi } 2070f4658285SStefan Hajnoczi } while (retry); 2071f4658285SStefan Hajnoczi } 2072f4658285SStefan Hajnoczi 2073756e6736SKevin Wolf /* 2074756e6736SKevin Wolf * Return values: 2075756e6736SKevin Wolf * 0 - success 2076756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 2077756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 2078756e6736SKevin Wolf * image file header 2079756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 2080756e6736SKevin Wolf */ 2081756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 2082756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 2083756e6736SKevin Wolf { 2084756e6736SKevin Wolf BlockDriver *drv = bs->drv; 2085469ef350SPaolo Bonzini int ret; 2086756e6736SKevin Wolf 20875f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 20885f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 20895f377794SPaolo Bonzini return -EINVAL; 20905f377794SPaolo Bonzini } 20915f377794SPaolo Bonzini 2092756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 2093469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 2094756e6736SKevin Wolf } else { 2095469ef350SPaolo Bonzini ret = -ENOTSUP; 2096756e6736SKevin Wolf } 2097469ef350SPaolo Bonzini 2098469ef350SPaolo Bonzini if (ret == 0) { 2099469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 2100469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 2101469ef350SPaolo Bonzini } 2102469ef350SPaolo Bonzini return ret; 2103756e6736SKevin Wolf } 2104756e6736SKevin Wolf 21056ebdcee2SJeff Cody /* 21066ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 21076ebdcee2SJeff Cody * 21086ebdcee2SJeff Cody * active is the current topmost image. 21096ebdcee2SJeff Cody * 21106ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 21116ebdcee2SJeff Cody * or if active == bs. 21126ebdcee2SJeff Cody */ 21136ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 21146ebdcee2SJeff Cody BlockDriverState *bs) 21156ebdcee2SJeff Cody { 21166ebdcee2SJeff Cody BlockDriverState *overlay = NULL; 21176ebdcee2SJeff Cody BlockDriverState *intermediate; 21186ebdcee2SJeff Cody 21196ebdcee2SJeff Cody assert(active != NULL); 21206ebdcee2SJeff Cody assert(bs != NULL); 21216ebdcee2SJeff Cody 21226ebdcee2SJeff Cody /* if bs is the same as active, then by definition it has no overlay 21236ebdcee2SJeff Cody */ 21246ebdcee2SJeff Cody if (active == bs) { 21256ebdcee2SJeff Cody return NULL; 21266ebdcee2SJeff Cody } 21276ebdcee2SJeff Cody 21286ebdcee2SJeff Cody intermediate = active; 21296ebdcee2SJeff Cody while (intermediate->backing_hd) { 21306ebdcee2SJeff Cody if (intermediate->backing_hd == bs) { 21316ebdcee2SJeff Cody overlay = intermediate; 21326ebdcee2SJeff Cody break; 21336ebdcee2SJeff Cody } 21346ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 21356ebdcee2SJeff Cody } 21366ebdcee2SJeff Cody 21376ebdcee2SJeff Cody return overlay; 21386ebdcee2SJeff Cody } 21396ebdcee2SJeff Cody 21406ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 21416ebdcee2SJeff Cody BlockDriverState *bs; 21426ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 21436ebdcee2SJeff Cody } BlkIntermediateStates; 21446ebdcee2SJeff Cody 21456ebdcee2SJeff Cody 21466ebdcee2SJeff Cody /* 21476ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 21486ebdcee2SJeff Cody * above 'top' to have base as its backing file. 21496ebdcee2SJeff Cody * 21506ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 21516ebdcee2SJeff Cody * information in 'bs' can be properly updated. 21526ebdcee2SJeff Cody * 21536ebdcee2SJeff Cody * E.g., this will convert the following chain: 21546ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 21556ebdcee2SJeff Cody * 21566ebdcee2SJeff Cody * to 21576ebdcee2SJeff Cody * 21586ebdcee2SJeff Cody * bottom <- base <- active 21596ebdcee2SJeff Cody * 21606ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 21616ebdcee2SJeff Cody * 21626ebdcee2SJeff Cody * base <- intermediate <- top <- active 21636ebdcee2SJeff Cody * 21646ebdcee2SJeff Cody * to 21656ebdcee2SJeff Cody * 21666ebdcee2SJeff Cody * base <- active 21676ebdcee2SJeff Cody * 21686ebdcee2SJeff Cody * Error conditions: 21696ebdcee2SJeff Cody * if active == top, that is considered an error 21706ebdcee2SJeff Cody * 21716ebdcee2SJeff Cody */ 21726ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 21736ebdcee2SJeff Cody BlockDriverState *base) 21746ebdcee2SJeff Cody { 21756ebdcee2SJeff Cody BlockDriverState *intermediate; 21766ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 21776ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 21786ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 21796ebdcee2SJeff Cody int ret = -EIO; 21806ebdcee2SJeff Cody 21816ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 21826ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 21836ebdcee2SJeff Cody 21846ebdcee2SJeff Cody if (!top->drv || !base->drv) { 21856ebdcee2SJeff Cody goto exit; 21866ebdcee2SJeff Cody } 21876ebdcee2SJeff Cody 21886ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 21896ebdcee2SJeff Cody 21906ebdcee2SJeff Cody if (new_top_bs == NULL) { 21916ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 21926ebdcee2SJeff Cody goto exit; 21936ebdcee2SJeff Cody } 21946ebdcee2SJeff Cody 21956ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 21966ebdcee2SJeff Cody * to do, no intermediate images */ 21976ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 21986ebdcee2SJeff Cody ret = 0; 21996ebdcee2SJeff Cody goto exit; 22006ebdcee2SJeff Cody } 22016ebdcee2SJeff Cody 22026ebdcee2SJeff Cody intermediate = top; 22036ebdcee2SJeff Cody 22046ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 22056ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 22066ebdcee2SJeff Cody */ 22076ebdcee2SJeff Cody while (intermediate) { 22086ebdcee2SJeff Cody intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); 22096ebdcee2SJeff Cody intermediate_state->bs = intermediate; 22106ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 22116ebdcee2SJeff Cody 22126ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 22136ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 22146ebdcee2SJeff Cody break; 22156ebdcee2SJeff Cody } 22166ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 22176ebdcee2SJeff Cody } 22186ebdcee2SJeff Cody if (base_bs == NULL) { 22196ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 22206ebdcee2SJeff Cody * unravel everything, and exit with error */ 22216ebdcee2SJeff Cody goto exit; 22226ebdcee2SJeff Cody } 22236ebdcee2SJeff Cody 22246ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 22256ebdcee2SJeff Cody ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, 22266ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 22276ebdcee2SJeff Cody if (ret) { 22286ebdcee2SJeff Cody goto exit; 22296ebdcee2SJeff Cody } 22306ebdcee2SJeff Cody new_top_bs->backing_hd = base_bs; 22316ebdcee2SJeff Cody 22326ebdcee2SJeff Cody 22336ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 22346ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 22356ebdcee2SJeff Cody intermediate_state->bs->backing_hd = NULL; 22364f6fd349SFam Zheng bdrv_unref(intermediate_state->bs); 22376ebdcee2SJeff Cody } 22386ebdcee2SJeff Cody ret = 0; 22396ebdcee2SJeff Cody 22406ebdcee2SJeff Cody exit: 22416ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 22426ebdcee2SJeff Cody g_free(intermediate_state); 22436ebdcee2SJeff Cody } 22446ebdcee2SJeff Cody return ret; 22456ebdcee2SJeff Cody } 22466ebdcee2SJeff Cody 22476ebdcee2SJeff Cody 224871d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 224971d0770cSaliguori size_t size) 225071d0770cSaliguori { 225171d0770cSaliguori int64_t len; 225271d0770cSaliguori 225371d0770cSaliguori if (!bdrv_is_inserted(bs)) 225471d0770cSaliguori return -ENOMEDIUM; 225571d0770cSaliguori 225671d0770cSaliguori if (bs->growable) 225771d0770cSaliguori return 0; 225871d0770cSaliguori 225971d0770cSaliguori len = bdrv_getlength(bs); 226071d0770cSaliguori 2261fbb7b4e0SKevin Wolf if (offset < 0) 2262fbb7b4e0SKevin Wolf return -EIO; 2263fbb7b4e0SKevin Wolf 2264fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 226571d0770cSaliguori return -EIO; 226671d0770cSaliguori 226771d0770cSaliguori return 0; 226871d0770cSaliguori } 226971d0770cSaliguori 227071d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 227171d0770cSaliguori int nb_sectors) 227271d0770cSaliguori { 2273eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 2274eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 227571d0770cSaliguori } 227671d0770cSaliguori 22771c9805a3SStefan Hajnoczi typedef struct RwCo { 22781c9805a3SStefan Hajnoczi BlockDriverState *bs; 22791c9805a3SStefan Hajnoczi int64_t sector_num; 22801c9805a3SStefan Hajnoczi int nb_sectors; 22811c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 22821c9805a3SStefan Hajnoczi bool is_write; 22831c9805a3SStefan Hajnoczi int ret; 22844105eaaaSPeter Lieven BdrvRequestFlags flags; 22851c9805a3SStefan Hajnoczi } RwCo; 22861c9805a3SStefan Hajnoczi 22871c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 2288fc01f7e7Sbellard { 22891c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 2290fc01f7e7Sbellard 22911c9805a3SStefan Hajnoczi if (!rwco->is_write) { 22921c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 22934105eaaaSPeter Lieven rwco->nb_sectors, rwco->qiov, 22944105eaaaSPeter Lieven rwco->flags); 22951c9805a3SStefan Hajnoczi } else { 22961c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 22974105eaaaSPeter Lieven rwco->nb_sectors, rwco->qiov, 22984105eaaaSPeter Lieven rwco->flags); 22991c9805a3SStefan Hajnoczi } 23001c9805a3SStefan Hajnoczi } 2301e7a8a783SKevin Wolf 23021c9805a3SStefan Hajnoczi /* 23038d3b1a2dSKevin Wolf * Process a vectored synchronous request using coroutines 23041c9805a3SStefan Hajnoczi */ 23058d3b1a2dSKevin Wolf static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, 23064105eaaaSPeter Lieven QEMUIOVector *qiov, bool is_write, 23074105eaaaSPeter Lieven BdrvRequestFlags flags) 23081c9805a3SStefan Hajnoczi { 23091c9805a3SStefan Hajnoczi Coroutine *co; 23101c9805a3SStefan Hajnoczi RwCo rwco = { 23111c9805a3SStefan Hajnoczi .bs = bs, 23121c9805a3SStefan Hajnoczi .sector_num = sector_num, 23138d3b1a2dSKevin Wolf .nb_sectors = qiov->size >> BDRV_SECTOR_BITS, 23148d3b1a2dSKevin Wolf .qiov = qiov, 23151c9805a3SStefan Hajnoczi .is_write = is_write, 23161c9805a3SStefan Hajnoczi .ret = NOT_DONE, 23174105eaaaSPeter Lieven .flags = flags, 23181c9805a3SStefan Hajnoczi }; 23198d3b1a2dSKevin Wolf assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0); 23201c9805a3SStefan Hajnoczi 2321498e386cSZhi Yong Wu /** 2322498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 2323498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 2324498e386cSZhi Yong Wu * if it has been enabled. 2325498e386cSZhi Yong Wu */ 2326498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 2327498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 2328498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 2329498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 2330498e386cSZhi Yong Wu } 2331498e386cSZhi Yong Wu 23321c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 23331c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 23341c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 23351c9805a3SStefan Hajnoczi } else { 23361c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 23371c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 23381c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 23391c9805a3SStefan Hajnoczi qemu_aio_wait(); 23401c9805a3SStefan Hajnoczi } 23411c9805a3SStefan Hajnoczi } 23421c9805a3SStefan Hajnoczi return rwco.ret; 2343e7a8a783SKevin Wolf } 2344e7a8a783SKevin Wolf 23458d3b1a2dSKevin Wolf /* 23468d3b1a2dSKevin Wolf * Process a synchronous request using coroutines 23478d3b1a2dSKevin Wolf */ 23488d3b1a2dSKevin Wolf static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 23494105eaaaSPeter Lieven int nb_sectors, bool is_write, BdrvRequestFlags flags) 23508d3b1a2dSKevin Wolf { 23518d3b1a2dSKevin Wolf QEMUIOVector qiov; 23528d3b1a2dSKevin Wolf struct iovec iov = { 23538d3b1a2dSKevin Wolf .iov_base = (void *)buf, 23548d3b1a2dSKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 23558d3b1a2dSKevin Wolf }; 23568d3b1a2dSKevin Wolf 23578d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 23584105eaaaSPeter Lieven return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags); 23598d3b1a2dSKevin Wolf } 23608d3b1a2dSKevin Wolf 23611c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 23621c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 23631c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 23641c9805a3SStefan Hajnoczi { 23654105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0); 236683f64091Sbellard } 2367fc01f7e7Sbellard 236807d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 236907d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 237007d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 237107d27a44SMarkus Armbruster { 237207d27a44SMarkus Armbruster bool enabled; 237307d27a44SMarkus Armbruster int ret; 237407d27a44SMarkus Armbruster 237507d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 237607d27a44SMarkus Armbruster bs->io_limits_enabled = false; 23774e7395e8SPeter Lieven ret = bdrv_read(bs, sector_num, buf, nb_sectors); 237807d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 237907d27a44SMarkus Armbruster return ret; 238007d27a44SMarkus Armbruster } 238107d27a44SMarkus Armbruster 238219cb3738Sbellard /* Return < 0 if error. Important errors are: 238319cb3738Sbellard -EIO generic I/O error (may happen for all errors) 238419cb3738Sbellard -ENOMEDIUM No media inserted. 238519cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 238619cb3738Sbellard -EACCES Trying to write a read-only device 238719cb3738Sbellard */ 2388fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 2389fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 2390fc01f7e7Sbellard { 23914105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0); 239283f64091Sbellard } 239383f64091Sbellard 23948d3b1a2dSKevin Wolf int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov) 23958d3b1a2dSKevin Wolf { 23964105eaaaSPeter Lieven return bdrv_rwv_co(bs, sector_num, qiov, true, 0); 23974105eaaaSPeter Lieven } 23984105eaaaSPeter Lieven 2399aa7bfbffSPeter Lieven int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, 2400aa7bfbffSPeter Lieven int nb_sectors, BdrvRequestFlags flags) 24014105eaaaSPeter Lieven { 24024105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true, 2403aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 24048d3b1a2dSKevin Wolf } 24058d3b1a2dSKevin Wolf 2406d75cbb5eSPeter Lieven /* 2407d75cbb5eSPeter Lieven * Completely zero out a block device with the help of bdrv_write_zeroes. 2408d75cbb5eSPeter Lieven * The operation is sped up by checking the block status and only writing 2409d75cbb5eSPeter Lieven * zeroes to the device if they currently do not return zeroes. Optional 2410d75cbb5eSPeter Lieven * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP). 2411d75cbb5eSPeter Lieven * 2412d75cbb5eSPeter Lieven * Returns < 0 on error, 0 on success. For error codes see bdrv_write(). 2413d75cbb5eSPeter Lieven */ 2414d75cbb5eSPeter Lieven int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) 2415d75cbb5eSPeter Lieven { 2416d75cbb5eSPeter Lieven int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE; 2417d75cbb5eSPeter Lieven int64_t ret, nb_sectors, sector_num = 0; 2418d75cbb5eSPeter Lieven int n; 2419d75cbb5eSPeter Lieven 2420d75cbb5eSPeter Lieven for (;;) { 2421d75cbb5eSPeter Lieven nb_sectors = target_size - sector_num; 2422d75cbb5eSPeter Lieven if (nb_sectors <= 0) { 2423d75cbb5eSPeter Lieven return 0; 2424d75cbb5eSPeter Lieven } 2425d75cbb5eSPeter Lieven if (nb_sectors > INT_MAX) { 2426d75cbb5eSPeter Lieven nb_sectors = INT_MAX; 2427d75cbb5eSPeter Lieven } 2428d75cbb5eSPeter Lieven ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n); 2429d75cbb5eSPeter Lieven if (ret & BDRV_BLOCK_ZERO) { 2430d75cbb5eSPeter Lieven sector_num += n; 2431d75cbb5eSPeter Lieven continue; 2432d75cbb5eSPeter Lieven } 2433d75cbb5eSPeter Lieven ret = bdrv_write_zeroes(bs, sector_num, n, flags); 2434d75cbb5eSPeter Lieven if (ret < 0) { 2435d75cbb5eSPeter Lieven error_report("error writing zeroes at sector %" PRId64 ": %s", 2436d75cbb5eSPeter Lieven sector_num, strerror(-ret)); 2437d75cbb5eSPeter Lieven return ret; 2438d75cbb5eSPeter Lieven } 2439d75cbb5eSPeter Lieven sector_num += n; 2440d75cbb5eSPeter Lieven } 2441d75cbb5eSPeter Lieven } 2442d75cbb5eSPeter Lieven 2443eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 2444eda578e5Saliguori void *buf, int count1) 244583f64091Sbellard { 24466ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 244783f64091Sbellard int len, nb_sectors, count; 244883f64091Sbellard int64_t sector_num; 24499a8c4cceSKevin Wolf int ret; 245083f64091Sbellard 245183f64091Sbellard count = count1; 245283f64091Sbellard /* first read to align to sector start */ 24536ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 245483f64091Sbellard if (len > count) 245583f64091Sbellard len = count; 24566ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 245783f64091Sbellard if (len > 0) { 24589a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 24599a8c4cceSKevin Wolf return ret; 24606ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 246183f64091Sbellard count -= len; 246283f64091Sbellard if (count == 0) 246383f64091Sbellard return count1; 246483f64091Sbellard sector_num++; 246583f64091Sbellard buf += len; 246683f64091Sbellard } 246783f64091Sbellard 246883f64091Sbellard /* read the sectors "in place" */ 24696ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 247083f64091Sbellard if (nb_sectors > 0) { 24719a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 24729a8c4cceSKevin Wolf return ret; 247383f64091Sbellard sector_num += nb_sectors; 24746ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 247583f64091Sbellard buf += len; 247683f64091Sbellard count -= len; 247783f64091Sbellard } 247883f64091Sbellard 247983f64091Sbellard /* add data from the last sector */ 248083f64091Sbellard if (count > 0) { 24819a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 24829a8c4cceSKevin Wolf return ret; 248383f64091Sbellard memcpy(buf, tmp_buf, count); 248483f64091Sbellard } 248583f64091Sbellard return count1; 248683f64091Sbellard } 248783f64091Sbellard 24888d3b1a2dSKevin Wolf int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov) 248983f64091Sbellard { 24906ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 249183f64091Sbellard int len, nb_sectors, count; 249283f64091Sbellard int64_t sector_num; 24939a8c4cceSKevin Wolf int ret; 249483f64091Sbellard 24958d3b1a2dSKevin Wolf count = qiov->size; 24968d3b1a2dSKevin Wolf 249783f64091Sbellard /* first write to align to sector start */ 24986ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 249983f64091Sbellard if (len > count) 250083f64091Sbellard len = count; 25016ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 250283f64091Sbellard if (len > 0) { 25039a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 25049a8c4cceSKevin Wolf return ret; 25058d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), 25068d3b1a2dSKevin Wolf len); 25079a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 25089a8c4cceSKevin Wolf return ret; 250983f64091Sbellard count -= len; 251083f64091Sbellard if (count == 0) 25118d3b1a2dSKevin Wolf return qiov->size; 251283f64091Sbellard sector_num++; 251383f64091Sbellard } 251483f64091Sbellard 251583f64091Sbellard /* write the sectors "in place" */ 25166ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 251783f64091Sbellard if (nb_sectors > 0) { 25188d3b1a2dSKevin Wolf QEMUIOVector qiov_inplace; 25198d3b1a2dSKevin Wolf 25208d3b1a2dSKevin Wolf qemu_iovec_init(&qiov_inplace, qiov->niov); 25218d3b1a2dSKevin Wolf qemu_iovec_concat(&qiov_inplace, qiov, len, 25228d3b1a2dSKevin Wolf nb_sectors << BDRV_SECTOR_BITS); 25238d3b1a2dSKevin Wolf ret = bdrv_writev(bs, sector_num, &qiov_inplace); 25248d3b1a2dSKevin Wolf qemu_iovec_destroy(&qiov_inplace); 25258d3b1a2dSKevin Wolf if (ret < 0) { 25269a8c4cceSKevin Wolf return ret; 25278d3b1a2dSKevin Wolf } 25288d3b1a2dSKevin Wolf 252983f64091Sbellard sector_num += nb_sectors; 25306ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 253183f64091Sbellard count -= len; 253283f64091Sbellard } 253383f64091Sbellard 253483f64091Sbellard /* add data from the last sector */ 253583f64091Sbellard if (count > 0) { 25369a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 25379a8c4cceSKevin Wolf return ret; 25388d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count); 25399a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 25409a8c4cceSKevin Wolf return ret; 254183f64091Sbellard } 25428d3b1a2dSKevin Wolf return qiov->size; 25438d3b1a2dSKevin Wolf } 25448d3b1a2dSKevin Wolf 25458d3b1a2dSKevin Wolf int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 25468d3b1a2dSKevin Wolf const void *buf, int count1) 25478d3b1a2dSKevin Wolf { 25488d3b1a2dSKevin Wolf QEMUIOVector qiov; 25498d3b1a2dSKevin Wolf struct iovec iov = { 25508d3b1a2dSKevin Wolf .iov_base = (void *) buf, 25518d3b1a2dSKevin Wolf .iov_len = count1, 25528d3b1a2dSKevin Wolf }; 25538d3b1a2dSKevin Wolf 25548d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 25558d3b1a2dSKevin Wolf return bdrv_pwritev(bs, offset, &qiov); 255683f64091Sbellard } 255783f64091Sbellard 2558f08145feSKevin Wolf /* 2559f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 2560f08145feSKevin Wolf * request (acts as a barrier) 2561f08145feSKevin Wolf * 2562f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 2563f08145feSKevin Wolf */ 2564f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 2565f08145feSKevin Wolf const void *buf, int count) 2566f08145feSKevin Wolf { 2567f08145feSKevin Wolf int ret; 2568f08145feSKevin Wolf 2569f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 2570f08145feSKevin Wolf if (ret < 0) { 2571f08145feSKevin Wolf return ret; 2572f08145feSKevin Wolf } 2573f08145feSKevin Wolf 2574f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 2575f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 2576f08145feSKevin Wolf bdrv_flush(bs); 2577f08145feSKevin Wolf } 2578f08145feSKevin Wolf 2579f08145feSKevin Wolf return 0; 2580f08145feSKevin Wolf } 2581f08145feSKevin Wolf 2582470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 2583ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2584ab185921SStefan Hajnoczi { 2585ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 2586ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 2587ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 2588ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 2589ab185921SStefan Hajnoczi */ 2590ab185921SStefan Hajnoczi void *bounce_buffer; 2591ab185921SStefan Hajnoczi 259279c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 2593ab185921SStefan Hajnoczi struct iovec iov; 2594ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 2595ab185921SStefan Hajnoczi int64_t cluster_sector_num; 2596ab185921SStefan Hajnoczi int cluster_nb_sectors; 2597ab185921SStefan Hajnoczi size_t skip_bytes; 2598ab185921SStefan Hajnoczi int ret; 2599ab185921SStefan Hajnoczi 2600ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 2601ab185921SStefan Hajnoczi * allocating cluster in the image file. 2602ab185921SStefan Hajnoczi */ 2603343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2604ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2605ab185921SStefan Hajnoczi 2606470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 2607ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 2608ab185921SStefan Hajnoczi 2609ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 2610ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 2611ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 2612ab185921SStefan Hajnoczi 261379c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 2614ab185921SStefan Hajnoczi &bounce_qiov); 2615ab185921SStefan Hajnoczi if (ret < 0) { 2616ab185921SStefan Hajnoczi goto err; 2617ab185921SStefan Hajnoczi } 2618ab185921SStefan Hajnoczi 261979c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 262079c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 2621621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 2622aa7bfbffSPeter Lieven cluster_nb_sectors, 0); 262379c053bdSStefan Hajnoczi } else { 2624f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 2625f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 2626f05fa4adSPaolo Bonzini */ 262779c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 2628ab185921SStefan Hajnoczi &bounce_qiov); 262979c053bdSStefan Hajnoczi } 263079c053bdSStefan Hajnoczi 2631ab185921SStefan Hajnoczi if (ret < 0) { 2632ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 2633ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 2634ab185921SStefan Hajnoczi * Simply report it in all cases. 2635ab185921SStefan Hajnoczi */ 2636ab185921SStefan Hajnoczi goto err; 2637ab185921SStefan Hajnoczi } 2638ab185921SStefan Hajnoczi 2639ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 264003396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 2641ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 2642ab185921SStefan Hajnoczi 2643ab185921SStefan Hajnoczi err: 2644ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 2645ab185921SStefan Hajnoczi return ret; 2646ab185921SStefan Hajnoczi } 2647ab185921SStefan Hajnoczi 2648c5fbe571SStefan Hajnoczi /* 2649c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 2650c5fbe571SStefan Hajnoczi */ 2651c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 2652470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2653470c0504SStefan Hajnoczi BdrvRequestFlags flags) 2654da1fa91dSKevin Wolf { 2655da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 2656dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 2657dbffbdcfSStefan Hajnoczi int ret; 2658da1fa91dSKevin Wolf 2659da1fa91dSKevin Wolf if (!drv) { 2660da1fa91dSKevin Wolf return -ENOMEDIUM; 2661da1fa91dSKevin Wolf } 2662da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2663da1fa91dSKevin Wolf return -EIO; 2664da1fa91dSKevin Wolf } 2665da1fa91dSKevin Wolf 2666f4658285SStefan Hajnoczi if (bs->copy_on_read) { 2667470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 2668470c0504SStefan Hajnoczi } 2669470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2670470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 2671470c0504SStefan Hajnoczi } 2672470c0504SStefan Hajnoczi 2673470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2674f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2675f4658285SStefan Hajnoczi } 2676f4658285SStefan Hajnoczi 2677cc0681c4SBenoît Canet /* throttling disk I/O */ 2678cc0681c4SBenoît Canet if (bs->io_limits_enabled) { 2679cc0681c4SBenoît Canet bdrv_io_limits_intercept(bs, nb_sectors, false); 2680cc0681c4SBenoît Canet } 2681cc0681c4SBenoît Canet 2682dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 2683ab185921SStefan Hajnoczi 2684470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2685ab185921SStefan Hajnoczi int pnum; 2686ab185921SStefan Hajnoczi 2687bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum); 2688ab185921SStefan Hajnoczi if (ret < 0) { 2689ab185921SStefan Hajnoczi goto out; 2690ab185921SStefan Hajnoczi } 2691ab185921SStefan Hajnoczi 2692ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 2693470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 2694ab185921SStefan Hajnoczi goto out; 2695ab185921SStefan Hajnoczi } 2696ab185921SStefan Hajnoczi } 2697ab185921SStefan Hajnoczi 2698893a8f62SMORITA Kazutaka if (!(bs->zero_beyond_eof && bs->growable)) { 2699dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 2700893a8f62SMORITA Kazutaka } else { 2701893a8f62SMORITA Kazutaka /* Read zeros after EOF of growable BDSes */ 2702893a8f62SMORITA Kazutaka int64_t len, total_sectors, max_nb_sectors; 2703893a8f62SMORITA Kazutaka 2704893a8f62SMORITA Kazutaka len = bdrv_getlength(bs); 2705893a8f62SMORITA Kazutaka if (len < 0) { 2706893a8f62SMORITA Kazutaka ret = len; 2707893a8f62SMORITA Kazutaka goto out; 2708893a8f62SMORITA Kazutaka } 2709893a8f62SMORITA Kazutaka 2710d055a1feSFam Zheng total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE); 2711893a8f62SMORITA Kazutaka max_nb_sectors = MAX(0, total_sectors - sector_num); 2712893a8f62SMORITA Kazutaka if (max_nb_sectors > 0) { 2713893a8f62SMORITA Kazutaka ret = drv->bdrv_co_readv(bs, sector_num, 2714893a8f62SMORITA Kazutaka MIN(nb_sectors, max_nb_sectors), qiov); 2715893a8f62SMORITA Kazutaka } else { 2716893a8f62SMORITA Kazutaka ret = 0; 2717893a8f62SMORITA Kazutaka } 2718893a8f62SMORITA Kazutaka 2719893a8f62SMORITA Kazutaka /* Reading beyond end of file is supposed to produce zeroes */ 2720893a8f62SMORITA Kazutaka if (ret == 0 && total_sectors < sector_num + nb_sectors) { 2721893a8f62SMORITA Kazutaka uint64_t offset = MAX(0, total_sectors - sector_num); 2722893a8f62SMORITA Kazutaka uint64_t bytes = (sector_num + nb_sectors - offset) * 2723893a8f62SMORITA Kazutaka BDRV_SECTOR_SIZE; 2724893a8f62SMORITA Kazutaka qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes); 2725893a8f62SMORITA Kazutaka } 2726893a8f62SMORITA Kazutaka } 2727ab185921SStefan Hajnoczi 2728ab185921SStefan Hajnoczi out: 2729dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2730470c0504SStefan Hajnoczi 2731470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2732470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 2733470c0504SStefan Hajnoczi } 2734470c0504SStefan Hajnoczi 2735dbffbdcfSStefan Hajnoczi return ret; 2736da1fa91dSKevin Wolf } 2737da1fa91dSKevin Wolf 2738c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 2739da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 2740da1fa91dSKevin Wolf { 2741c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 2742da1fa91dSKevin Wolf 2743470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 2744470c0504SStefan Hajnoczi } 2745470c0504SStefan Hajnoczi 2746470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 2747470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2748470c0504SStefan Hajnoczi { 2749470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 2750470c0504SStefan Hajnoczi 2751470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 2752470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 2753c5fbe571SStefan Hajnoczi } 2754c5fbe571SStefan Hajnoczi 2755c31cb707SPeter Lieven /* if no limit is specified in the BlockLimits use a default 2756c31cb707SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 2757c31cb707SPeter Lieven */ 2758c31cb707SPeter Lieven #define MAX_WRITE_ZEROES_DEFAULT 32768 2759c31cb707SPeter Lieven 2760f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 2761aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) 2762f08f2ddaSStefan Hajnoczi { 2763f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 2764f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 2765c31cb707SPeter Lieven struct iovec iov = {0}; 2766c31cb707SPeter Lieven int ret = 0; 2767f08f2ddaSStefan Hajnoczi 2768c31cb707SPeter Lieven int max_write_zeroes = bs->bl.max_write_zeroes ? 2769c31cb707SPeter Lieven bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT; 2770621f0589SKevin Wolf 2771c31cb707SPeter Lieven while (nb_sectors > 0 && !ret) { 2772c31cb707SPeter Lieven int num = nb_sectors; 2773c31cb707SPeter Lieven 2774c31cb707SPeter Lieven /* align request */ 2775c31cb707SPeter Lieven if (bs->bl.write_zeroes_alignment && 2776c31cb707SPeter Lieven num >= bs->bl.write_zeroes_alignment && 2777c31cb707SPeter Lieven sector_num % bs->bl.write_zeroes_alignment) { 2778c31cb707SPeter Lieven if (num > bs->bl.write_zeroes_alignment) { 2779c31cb707SPeter Lieven num = bs->bl.write_zeroes_alignment; 2780c31cb707SPeter Lieven } 2781c31cb707SPeter Lieven num -= sector_num % bs->bl.write_zeroes_alignment; 2782c31cb707SPeter Lieven } 2783c31cb707SPeter Lieven 2784c31cb707SPeter Lieven /* limit request size */ 2785c31cb707SPeter Lieven if (num > max_write_zeroes) { 2786c31cb707SPeter Lieven num = max_write_zeroes; 2787c31cb707SPeter Lieven } 2788c31cb707SPeter Lieven 2789c31cb707SPeter Lieven ret = -ENOTSUP; 2790f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 2791f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 2792c31cb707SPeter Lieven ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags); 2793f08f2ddaSStefan Hajnoczi } 2794f08f2ddaSStefan Hajnoczi 2795c31cb707SPeter Lieven if (ret == -ENOTSUP) { 2796f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 2797c31cb707SPeter Lieven iov.iov_len = num * BDRV_SECTOR_SIZE; 2798c31cb707SPeter Lieven if (iov.iov_base == NULL) { 2799c31cb707SPeter Lieven /* allocate bounce buffer only once and ensure that it 2800c31cb707SPeter Lieven * is big enough for this and all future requests. 2801c31cb707SPeter Lieven */ 2802c31cb707SPeter Lieven size_t bufsize = num <= nb_sectors ? num : max_write_zeroes; 2803c31cb707SPeter Lieven iov.iov_base = qemu_blockalign(bs, bufsize * BDRV_SECTOR_SIZE); 2804c31cb707SPeter Lieven memset(iov.iov_base, 0, bufsize * BDRV_SECTOR_SIZE); 2805c31cb707SPeter Lieven } 2806f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 2807f08f2ddaSStefan Hajnoczi 2808c31cb707SPeter Lieven ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov); 2809c31cb707SPeter Lieven } 2810c31cb707SPeter Lieven 2811c31cb707SPeter Lieven sector_num += num; 2812c31cb707SPeter Lieven nb_sectors -= num; 2813c31cb707SPeter Lieven } 2814f08f2ddaSStefan Hajnoczi 2815f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 2816f08f2ddaSStefan Hajnoczi return ret; 2817f08f2ddaSStefan Hajnoczi } 2818f08f2ddaSStefan Hajnoczi 2819c5fbe571SStefan Hajnoczi /* 2820c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 2821c5fbe571SStefan Hajnoczi */ 2822c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 2823f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2824f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 2825c5fbe571SStefan Hajnoczi { 2826c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 2827dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 28286b7cb247SStefan Hajnoczi int ret; 2829da1fa91dSKevin Wolf 2830da1fa91dSKevin Wolf if (!bs->drv) { 2831da1fa91dSKevin Wolf return -ENOMEDIUM; 2832da1fa91dSKevin Wolf } 2833da1fa91dSKevin Wolf if (bs->read_only) { 2834da1fa91dSKevin Wolf return -EACCES; 2835da1fa91dSKevin Wolf } 2836da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2837da1fa91dSKevin Wolf return -EIO; 2838da1fa91dSKevin Wolf } 2839da1fa91dSKevin Wolf 2840470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2841f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2842f4658285SStefan Hajnoczi } 2843f4658285SStefan Hajnoczi 2844cc0681c4SBenoît Canet /* throttling disk I/O */ 2845cc0681c4SBenoît Canet if (bs->io_limits_enabled) { 2846cc0681c4SBenoît Canet bdrv_io_limits_intercept(bs, nb_sectors, true); 2847cc0681c4SBenoît Canet } 2848cc0681c4SBenoît Canet 2849dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 2850dbffbdcfSStefan Hajnoczi 2851d616b224SStefan Hajnoczi ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); 2852d616b224SStefan Hajnoczi 2853d616b224SStefan Hajnoczi if (ret < 0) { 2854d616b224SStefan Hajnoczi /* Do nothing, write notifier decided to fail this request */ 2855d616b224SStefan Hajnoczi } else if (flags & BDRV_REQ_ZERO_WRITE) { 2856aa7bfbffSPeter Lieven ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags); 2857f08f2ddaSStefan Hajnoczi } else { 28586b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 2859f08f2ddaSStefan Hajnoczi } 28606b7cb247SStefan Hajnoczi 2861f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 2862f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 2863f05fa4adSPaolo Bonzini } 2864f05fa4adSPaolo Bonzini 28651755da16SPaolo Bonzini bdrv_set_dirty(bs, sector_num, nb_sectors); 2866da1fa91dSKevin Wolf 2867da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2868da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2869da1fa91dSKevin Wolf } 2870df2a6f29SPaolo Bonzini if (bs->growable && ret >= 0) { 2871df2a6f29SPaolo Bonzini bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors); 2872df2a6f29SPaolo Bonzini } 2873da1fa91dSKevin Wolf 2874dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2875dbffbdcfSStefan Hajnoczi 28766b7cb247SStefan Hajnoczi return ret; 2877da1fa91dSKevin Wolf } 2878da1fa91dSKevin Wolf 2879c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 2880c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 2881c5fbe571SStefan Hajnoczi { 2882c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 2883c5fbe571SStefan Hajnoczi 2884f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 2885f08f2ddaSStefan Hajnoczi } 2886f08f2ddaSStefan Hajnoczi 2887f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 2888aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, 2889aa7bfbffSPeter Lieven BdrvRequestFlags flags) 2890f08f2ddaSStefan Hajnoczi { 289194d6ff21SPaolo Bonzini trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags); 2892f08f2ddaSStefan Hajnoczi 2893d32f35cbSPeter Lieven if (!(bs->open_flags & BDRV_O_UNMAP)) { 2894d32f35cbSPeter Lieven flags &= ~BDRV_REQ_MAY_UNMAP; 2895d32f35cbSPeter Lieven } 2896d32f35cbSPeter Lieven 2897f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 2898aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 2899c5fbe571SStefan Hajnoczi } 2900c5fbe571SStefan Hajnoczi 290183f64091Sbellard /** 290283f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 290383f64091Sbellard */ 290483f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 290583f64091Sbellard { 290683f64091Sbellard BlockDriver *drv = bs->drv; 290751762288SStefan Hajnoczi int ret; 290883f64091Sbellard if (!drv) 290919cb3738Sbellard return -ENOMEDIUM; 291083f64091Sbellard if (!drv->bdrv_truncate) 291183f64091Sbellard return -ENOTSUP; 291259f2689dSNaphtali Sprei if (bs->read_only) 291359f2689dSNaphtali Sprei return -EACCES; 29148591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 29158591675fSMarcelo Tosatti return -EBUSY; 291651762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 291751762288SStefan Hajnoczi if (ret == 0) { 291851762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2919145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 292051762288SStefan Hajnoczi } 292151762288SStefan Hajnoczi return ret; 292283f64091Sbellard } 292383f64091Sbellard 292483f64091Sbellard /** 29254a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 29264a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 29274a1d5e1fSFam Zheng */ 29284a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 29294a1d5e1fSFam Zheng { 29304a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 29314a1d5e1fSFam Zheng if (!drv) { 29324a1d5e1fSFam Zheng return -ENOMEDIUM; 29334a1d5e1fSFam Zheng } 29344a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 29354a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 29364a1d5e1fSFam Zheng } 29374a1d5e1fSFam Zheng if (bs->file) { 29384a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 29394a1d5e1fSFam Zheng } 29404a1d5e1fSFam Zheng return -ENOTSUP; 29414a1d5e1fSFam Zheng } 29424a1d5e1fSFam Zheng 29434a1d5e1fSFam Zheng /** 294483f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 294583f64091Sbellard */ 294683f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 294783f64091Sbellard { 294883f64091Sbellard BlockDriver *drv = bs->drv; 294983f64091Sbellard if (!drv) 295019cb3738Sbellard return -ENOMEDIUM; 295151762288SStefan Hajnoczi 2952b94a2610SKevin Wolf if (drv->has_variable_length) { 2953b94a2610SKevin Wolf int ret = refresh_total_sectors(bs, bs->total_sectors); 2954b94a2610SKevin Wolf if (ret < 0) { 2955b94a2610SKevin Wolf return ret; 2956fc01f7e7Sbellard } 295746a4e4e6SStefan Hajnoczi } 295846a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 295946a4e4e6SStefan Hajnoczi } 2960fc01f7e7Sbellard 296119cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 296296b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2963fc01f7e7Sbellard { 296419cb3738Sbellard int64_t length; 296519cb3738Sbellard length = bdrv_getlength(bs); 296619cb3738Sbellard if (length < 0) 296719cb3738Sbellard length = 0; 296819cb3738Sbellard else 29696ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 297019cb3738Sbellard *nb_sectors_ptr = length; 2971fc01f7e7Sbellard } 2972cf98951bSbellard 2973ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 2974ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 2975abd7f68dSMarkus Armbruster { 2976abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2977abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2978abd7f68dSMarkus Armbruster } 2979abd7f68dSMarkus Armbruster 29801ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 2981abd7f68dSMarkus Armbruster { 2982abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2983abd7f68dSMarkus Armbruster } 2984abd7f68dSMarkus Armbruster 29853e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 29863e1caa5fSPaolo Bonzini { 29873e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 29883e1caa5fSPaolo Bonzini 29893e1caa5fSPaolo Bonzini switch (on_err) { 29903e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 29913e1caa5fSPaolo Bonzini return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; 29923e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 29933e1caa5fSPaolo Bonzini return BDRV_ACTION_STOP; 29943e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 29953e1caa5fSPaolo Bonzini return BDRV_ACTION_REPORT; 29963e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 29973e1caa5fSPaolo Bonzini return BDRV_ACTION_IGNORE; 29983e1caa5fSPaolo Bonzini default: 29993e1caa5fSPaolo Bonzini abort(); 30003e1caa5fSPaolo Bonzini } 30013e1caa5fSPaolo Bonzini } 30023e1caa5fSPaolo Bonzini 30033e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 30043e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 30053e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 30063e1caa5fSPaolo Bonzini */ 30073e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 30083e1caa5fSPaolo Bonzini bool is_read, int error) 30093e1caa5fSPaolo Bonzini { 30103e1caa5fSPaolo Bonzini assert(error >= 0); 301132c81a4aSPaolo Bonzini bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); 30123e1caa5fSPaolo Bonzini if (action == BDRV_ACTION_STOP) { 30133e1caa5fSPaolo Bonzini vm_stop(RUN_STATE_IO_ERROR); 30143e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 30153e1caa5fSPaolo Bonzini } 30163e1caa5fSPaolo Bonzini } 30173e1caa5fSPaolo Bonzini 3018b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 3019b338082bSbellard { 3020b338082bSbellard return bs->read_only; 3021b338082bSbellard } 3022b338082bSbellard 3023985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 3024985a03b0Sths { 3025985a03b0Sths return bs->sg; 3026985a03b0Sths } 3027985a03b0Sths 3028e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 3029e900a7b7SChristoph Hellwig { 3030e900a7b7SChristoph Hellwig return bs->enable_write_cache; 3031e900a7b7SChristoph Hellwig } 3032e900a7b7SChristoph Hellwig 3033425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 3034425b0148SPaolo Bonzini { 3035425b0148SPaolo Bonzini bs->enable_write_cache = wce; 303655b110f2SJeff Cody 303755b110f2SJeff Cody /* so a reopen() will preserve wce */ 303855b110f2SJeff Cody if (wce) { 303955b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 304055b110f2SJeff Cody } else { 304155b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 304255b110f2SJeff Cody } 3043425b0148SPaolo Bonzini } 3044425b0148SPaolo Bonzini 3045ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 3046ea2384d3Sbellard { 3047ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 3048ea2384d3Sbellard return 1; 3049ea2384d3Sbellard return bs->encrypted; 3050ea2384d3Sbellard } 3051ea2384d3Sbellard 3052c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 3053c0f4ce77Saliguori { 3054c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 3055c0f4ce77Saliguori 3056c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 3057c0f4ce77Saliguori return 1; 3058c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 3059c0f4ce77Saliguori } 3060c0f4ce77Saliguori 3061ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 3062ea2384d3Sbellard { 3063ea2384d3Sbellard int ret; 3064ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 3065ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 3066ea2384d3Sbellard if (ret < 0) 3067ea2384d3Sbellard return ret; 3068ea2384d3Sbellard if (!bs->encrypted) 3069ea2384d3Sbellard return 0; 3070ea2384d3Sbellard } 3071fd04a2aeSShahar Havivi if (!bs->encrypted) { 3072fd04a2aeSShahar Havivi return -EINVAL; 3073fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 3074fd04a2aeSShahar Havivi return -ENOMEDIUM; 3075fd04a2aeSShahar Havivi } 3076c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 3077bb5fc20fSaliguori if (ret < 0) { 3078bb5fc20fSaliguori bs->valid_key = 0; 3079bb5fc20fSaliguori } else if (!bs->valid_key) { 3080bb5fc20fSaliguori bs->valid_key = 1; 3081bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 30827d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 3083bb5fc20fSaliguori } 3084c0f4ce77Saliguori return ret; 3085ea2384d3Sbellard } 3086ea2384d3Sbellard 3087f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 3088ea2384d3Sbellard { 3089f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 3090ea2384d3Sbellard } 3091ea2384d3Sbellard 3092ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 3093ea2384d3Sbellard void *opaque) 3094ea2384d3Sbellard { 3095ea2384d3Sbellard BlockDriver *drv; 3096ea2384d3Sbellard 30978a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 3098ea2384d3Sbellard it(opaque, drv->format_name); 3099ea2384d3Sbellard } 3100ea2384d3Sbellard } 3101ea2384d3Sbellard 3102b338082bSbellard BlockDriverState *bdrv_find(const char *name) 3103b338082bSbellard { 3104b338082bSbellard BlockDriverState *bs; 3105b338082bSbellard 31061b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 31071b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 3108b338082bSbellard return bs; 3109b338082bSbellard } 31101b7bdbc1SStefan Hajnoczi } 3111b338082bSbellard return NULL; 3112b338082bSbellard } 3113b338082bSbellard 31142f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 31152f399b0aSMarkus Armbruster { 31162f399b0aSMarkus Armbruster if (!bs) { 31172f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 31182f399b0aSMarkus Armbruster } 31192f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 31202f399b0aSMarkus Armbruster } 31212f399b0aSMarkus Armbruster 312251de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 312381d0912dSbellard { 312481d0912dSbellard BlockDriverState *bs; 312581d0912dSbellard 31261b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 312751de9760Saliguori it(opaque, bs); 312881d0912dSbellard } 312981d0912dSbellard } 313081d0912dSbellard 3131ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 3132ea2384d3Sbellard { 3133ea2384d3Sbellard return bs->device_name; 3134ea2384d3Sbellard } 3135ea2384d3Sbellard 3136c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 3137c8433287SMarkus Armbruster { 3138c8433287SMarkus Armbruster return bs->open_flags; 3139c8433287SMarkus Armbruster } 3140c8433287SMarkus Armbruster 3141f0f0fdfeSKevin Wolf int bdrv_flush_all(void) 3142c6ca28d6Saliguori { 3143c6ca28d6Saliguori BlockDriverState *bs; 3144f0f0fdfeSKevin Wolf int result = 0; 3145c6ca28d6Saliguori 31461b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 3147f0f0fdfeSKevin Wolf int ret = bdrv_flush(bs); 3148f0f0fdfeSKevin Wolf if (ret < 0 && !result) { 3149f0f0fdfeSKevin Wolf result = ret; 3150c6ca28d6Saliguori } 31511b7bdbc1SStefan Hajnoczi } 3152c6ca28d6Saliguori 3153f0f0fdfeSKevin Wolf return result; 3154f0f0fdfeSKevin Wolf } 3155f0f0fdfeSKevin Wolf 31563ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 31573ac21627SPeter Lieven { 31583ac21627SPeter Lieven return 1; 31593ac21627SPeter Lieven } 31603ac21627SPeter Lieven 3161f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 3162f2feebbdSKevin Wolf { 3163f2feebbdSKevin Wolf assert(bs->drv); 3164f2feebbdSKevin Wolf 316511212d8fSPaolo Bonzini /* If BS is a copy on write image, it is initialized to 316611212d8fSPaolo Bonzini the contents of the base image, which may not be zeroes. */ 316711212d8fSPaolo Bonzini if (bs->backing_hd) { 316811212d8fSPaolo Bonzini return 0; 316911212d8fSPaolo Bonzini } 3170336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 3171336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 3172f2feebbdSKevin Wolf } 3173f2feebbdSKevin Wolf 31743ac21627SPeter Lieven /* safe default */ 31753ac21627SPeter Lieven return 0; 3176f2feebbdSKevin Wolf } 3177f2feebbdSKevin Wolf 31784ce78691SPeter Lieven bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) 31794ce78691SPeter Lieven { 31804ce78691SPeter Lieven BlockDriverInfo bdi; 31814ce78691SPeter Lieven 31824ce78691SPeter Lieven if (bs->backing_hd) { 31834ce78691SPeter Lieven return false; 31844ce78691SPeter Lieven } 31854ce78691SPeter Lieven 31864ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 31874ce78691SPeter Lieven return bdi.unallocated_blocks_are_zero; 31884ce78691SPeter Lieven } 31894ce78691SPeter Lieven 31904ce78691SPeter Lieven return false; 31914ce78691SPeter Lieven } 31924ce78691SPeter Lieven 31934ce78691SPeter Lieven bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) 31944ce78691SPeter Lieven { 31954ce78691SPeter Lieven BlockDriverInfo bdi; 31964ce78691SPeter Lieven 31974ce78691SPeter Lieven if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { 31984ce78691SPeter Lieven return false; 31994ce78691SPeter Lieven } 32004ce78691SPeter Lieven 32014ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 32024ce78691SPeter Lieven return bdi.can_write_zeroes_with_unmap; 32034ce78691SPeter Lieven } 32044ce78691SPeter Lieven 32054ce78691SPeter Lieven return false; 32064ce78691SPeter Lieven } 32074ce78691SPeter Lieven 3208b6b8a333SPaolo Bonzini typedef struct BdrvCoGetBlockStatusData { 3209376ae3f1SStefan Hajnoczi BlockDriverState *bs; 3210b35b2bbaSMiroslav Rezanina BlockDriverState *base; 3211376ae3f1SStefan Hajnoczi int64_t sector_num; 3212376ae3f1SStefan Hajnoczi int nb_sectors; 3213376ae3f1SStefan Hajnoczi int *pnum; 3214b6b8a333SPaolo Bonzini int64_t ret; 3215376ae3f1SStefan Hajnoczi bool done; 3216b6b8a333SPaolo Bonzini } BdrvCoGetBlockStatusData; 3217376ae3f1SStefan Hajnoczi 3218f58c7b35Sths /* 3219f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 3220f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 3221f58c7b35Sths * hence all their sectors are reported as allocated. 3222f58c7b35Sths * 3223bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 3224bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 3225bd9533e3SStefan Hajnoczi * 3226f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 3227f58c7b35Sths * the specified sector) that are known to be in the same 3228f58c7b35Sths * allocated/unallocated state. 3229f58c7b35Sths * 3230bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 3231bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 3232f58c7b35Sths */ 3233b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, 3234bdad13b9SPaolo Bonzini int64_t sector_num, 3235060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 3236f58c7b35Sths { 3237617ccb46SPaolo Bonzini int64_t length; 3238f58c7b35Sths int64_t n; 32395daa74a6SPaolo Bonzini int64_t ret, ret2; 3240bd9533e3SStefan Hajnoczi 3241617ccb46SPaolo Bonzini length = bdrv_getlength(bs); 3242617ccb46SPaolo Bonzini if (length < 0) { 3243617ccb46SPaolo Bonzini return length; 3244617ccb46SPaolo Bonzini } 3245617ccb46SPaolo Bonzini 3246617ccb46SPaolo Bonzini if (sector_num >= (length >> BDRV_SECTOR_BITS)) { 32476aebab14SStefan Hajnoczi *pnum = 0; 32486aebab14SStefan Hajnoczi return 0; 32496aebab14SStefan Hajnoczi } 3250bd9533e3SStefan Hajnoczi 32516aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 3252bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 3253bd9533e3SStefan Hajnoczi nb_sectors = n; 3254bd9533e3SStefan Hajnoczi } 3255bd9533e3SStefan Hajnoczi 3256b6b8a333SPaolo Bonzini if (!bs->drv->bdrv_co_get_block_status) { 3257bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 3258918e92d7SPaolo Bonzini ret = BDRV_BLOCK_DATA; 3259918e92d7SPaolo Bonzini if (bs->drv->protocol_name) { 3260918e92d7SPaolo Bonzini ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE); 3261918e92d7SPaolo Bonzini } 3262918e92d7SPaolo Bonzini return ret; 32636aebab14SStefan Hajnoczi } 32646aebab14SStefan Hajnoczi 3265415b5b01SPaolo Bonzini ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum); 3266415b5b01SPaolo Bonzini if (ret < 0) { 32673e0a233dSPeter Lieven *pnum = 0; 3268415b5b01SPaolo Bonzini return ret; 3269415b5b01SPaolo Bonzini } 3270415b5b01SPaolo Bonzini 327192bc50a5SPeter Lieven if (ret & BDRV_BLOCK_RAW) { 327292bc50a5SPeter Lieven assert(ret & BDRV_BLOCK_OFFSET_VALID); 327392bc50a5SPeter Lieven return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 327492bc50a5SPeter Lieven *pnum, pnum); 327592bc50a5SPeter Lieven } 327692bc50a5SPeter Lieven 3277c3d86884SPeter Lieven if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) { 3278c3d86884SPeter Lieven if (bdrv_unallocated_blocks_are_zero(bs)) { 3279415b5b01SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 32801f9db224SPeter Lieven } else if (bs->backing_hd) { 3281f0ad5712SPaolo Bonzini BlockDriverState *bs2 = bs->backing_hd; 3282f0ad5712SPaolo Bonzini int64_t length2 = bdrv_getlength(bs2); 3283f0ad5712SPaolo Bonzini if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) { 3284f0ad5712SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 3285f0ad5712SPaolo Bonzini } 3286f0ad5712SPaolo Bonzini } 3287415b5b01SPaolo Bonzini } 32885daa74a6SPaolo Bonzini 32895daa74a6SPaolo Bonzini if (bs->file && 32905daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && 32915daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_OFFSET_VALID)) { 32925daa74a6SPaolo Bonzini ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 32935daa74a6SPaolo Bonzini *pnum, pnum); 32945daa74a6SPaolo Bonzini if (ret2 >= 0) { 32955daa74a6SPaolo Bonzini /* Ignore errors. This is just providing extra information, it 32965daa74a6SPaolo Bonzini * is useful but not necessary. 32975daa74a6SPaolo Bonzini */ 32985daa74a6SPaolo Bonzini ret |= (ret2 & BDRV_BLOCK_ZERO); 32995daa74a6SPaolo Bonzini } 33005daa74a6SPaolo Bonzini } 33015daa74a6SPaolo Bonzini 3302415b5b01SPaolo Bonzini return ret; 3303060f51c9SStefan Hajnoczi } 3304060f51c9SStefan Hajnoczi 3305b6b8a333SPaolo Bonzini /* Coroutine wrapper for bdrv_get_block_status() */ 3306b6b8a333SPaolo Bonzini static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque) 3307060f51c9SStefan Hajnoczi { 3308b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData *data = opaque; 3309060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 3310060f51c9SStefan Hajnoczi 3311b6b8a333SPaolo Bonzini data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors, 3312060f51c9SStefan Hajnoczi data->pnum); 3313060f51c9SStefan Hajnoczi data->done = true; 3314060f51c9SStefan Hajnoczi } 3315060f51c9SStefan Hajnoczi 3316060f51c9SStefan Hajnoczi /* 3317b6b8a333SPaolo Bonzini * Synchronous wrapper around bdrv_co_get_block_status(). 3318060f51c9SStefan Hajnoczi * 3319b6b8a333SPaolo Bonzini * See bdrv_co_get_block_status() for details. 3320060f51c9SStefan Hajnoczi */ 3321b6b8a333SPaolo Bonzini int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num, 3322b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 3323060f51c9SStefan Hajnoczi { 3324376ae3f1SStefan Hajnoczi Coroutine *co; 3325b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData data = { 3326376ae3f1SStefan Hajnoczi .bs = bs, 3327376ae3f1SStefan Hajnoczi .sector_num = sector_num, 3328376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 3329376ae3f1SStefan Hajnoczi .pnum = pnum, 3330376ae3f1SStefan Hajnoczi .done = false, 3331376ae3f1SStefan Hajnoczi }; 3332376ae3f1SStefan Hajnoczi 3333bdad13b9SPaolo Bonzini if (qemu_in_coroutine()) { 3334bdad13b9SPaolo Bonzini /* Fast-path if already in coroutine context */ 3335b6b8a333SPaolo Bonzini bdrv_get_block_status_co_entry(&data); 3336bdad13b9SPaolo Bonzini } else { 3337b6b8a333SPaolo Bonzini co = qemu_coroutine_create(bdrv_get_block_status_co_entry); 3338376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 3339376ae3f1SStefan Hajnoczi while (!data.done) { 3340376ae3f1SStefan Hajnoczi qemu_aio_wait(); 3341376ae3f1SStefan Hajnoczi } 3342bdad13b9SPaolo Bonzini } 3343376ae3f1SStefan Hajnoczi return data.ret; 3344376ae3f1SStefan Hajnoczi } 3345f58c7b35Sths 3346b6b8a333SPaolo Bonzini int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, 3347b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 3348b6b8a333SPaolo Bonzini { 33494333bb71SPaolo Bonzini int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum); 33504333bb71SPaolo Bonzini if (ret < 0) { 33514333bb71SPaolo Bonzini return ret; 33524333bb71SPaolo Bonzini } 33534333bb71SPaolo Bonzini return 33544333bb71SPaolo Bonzini (ret & BDRV_BLOCK_DATA) || 33554333bb71SPaolo Bonzini ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs)); 3356b6b8a333SPaolo Bonzini } 3357b6b8a333SPaolo Bonzini 3358188a7bbfSPaolo Bonzini /* 3359188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 3360188a7bbfSPaolo Bonzini * 3361188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 3362188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 3363188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 3364188a7bbfSPaolo Bonzini * 3365188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 3366188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 3367188a7bbfSPaolo Bonzini * allocated/unallocated state. 3368188a7bbfSPaolo Bonzini * 3369188a7bbfSPaolo Bonzini */ 33704f578637SPaolo Bonzini int bdrv_is_allocated_above(BlockDriverState *top, 3371188a7bbfSPaolo Bonzini BlockDriverState *base, 3372188a7bbfSPaolo Bonzini int64_t sector_num, 3373188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 3374188a7bbfSPaolo Bonzini { 3375188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 3376188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 3377188a7bbfSPaolo Bonzini 3378188a7bbfSPaolo Bonzini intermediate = top; 3379188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 3380188a7bbfSPaolo Bonzini int pnum_inter; 3381bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors, 3382188a7bbfSPaolo Bonzini &pnum_inter); 3383188a7bbfSPaolo Bonzini if (ret < 0) { 3384188a7bbfSPaolo Bonzini return ret; 3385188a7bbfSPaolo Bonzini } else if (ret) { 3386188a7bbfSPaolo Bonzini *pnum = pnum_inter; 3387188a7bbfSPaolo Bonzini return 1; 3388188a7bbfSPaolo Bonzini } 3389188a7bbfSPaolo Bonzini 3390188a7bbfSPaolo Bonzini /* 3391188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 3392188a7bbfSPaolo Bonzini * might have 3393188a7bbfSPaolo Bonzini * 3394188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 3395188a7bbfSPaolo Bonzini */ 339663ba17d3SVishvananda Ishaya if (n > pnum_inter && 339763ba17d3SVishvananda Ishaya (intermediate == top || 339863ba17d3SVishvananda Ishaya sector_num + pnum_inter < intermediate->total_sectors)) { 3399188a7bbfSPaolo Bonzini n = pnum_inter; 3400188a7bbfSPaolo Bonzini } 3401188a7bbfSPaolo Bonzini 3402188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 3403188a7bbfSPaolo Bonzini } 3404188a7bbfSPaolo Bonzini 3405188a7bbfSPaolo Bonzini *pnum = n; 3406188a7bbfSPaolo Bonzini return 0; 3407188a7bbfSPaolo Bonzini } 3408188a7bbfSPaolo Bonzini 3409045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 3410045df330Saliguori { 3411045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 3412045df330Saliguori return bs->backing_file; 3413045df330Saliguori else if (bs->encrypted) 3414045df330Saliguori return bs->filename; 3415045df330Saliguori else 3416045df330Saliguori return NULL; 3417045df330Saliguori } 3418045df330Saliguori 341983f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 342083f64091Sbellard char *filename, int filename_size) 342183f64091Sbellard { 342283f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 342383f64091Sbellard } 342483f64091Sbellard 3425faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 3426faea38e7Sbellard const uint8_t *buf, int nb_sectors) 3427faea38e7Sbellard { 3428faea38e7Sbellard BlockDriver *drv = bs->drv; 3429faea38e7Sbellard if (!drv) 343019cb3738Sbellard return -ENOMEDIUM; 3431faea38e7Sbellard if (!drv->bdrv_write_compressed) 3432faea38e7Sbellard return -ENOTSUP; 3433fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 3434fbb7b4e0SKevin Wolf return -EIO; 34357cd1e32aSlirans@il.ibm.com 3436e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 34377cd1e32aSlirans@il.ibm.com 3438faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 3439faea38e7Sbellard } 3440faea38e7Sbellard 3441faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 3442faea38e7Sbellard { 3443faea38e7Sbellard BlockDriver *drv = bs->drv; 3444faea38e7Sbellard if (!drv) 344519cb3738Sbellard return -ENOMEDIUM; 3446faea38e7Sbellard if (!drv->bdrv_get_info) 3447faea38e7Sbellard return -ENOTSUP; 3448faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 3449faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 3450faea38e7Sbellard } 3451faea38e7Sbellard 3452eae041feSMax Reitz ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) 3453eae041feSMax Reitz { 3454eae041feSMax Reitz BlockDriver *drv = bs->drv; 3455eae041feSMax Reitz if (drv && drv->bdrv_get_specific_info) { 3456eae041feSMax Reitz return drv->bdrv_get_specific_info(bs); 3457eae041feSMax Reitz } 3458eae041feSMax Reitz return NULL; 3459eae041feSMax Reitz } 3460eae041feSMax Reitz 346145566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 346245566e9cSChristoph Hellwig int64_t pos, int size) 3463178e08a5Saliguori { 3464cf8074b3SKevin Wolf QEMUIOVector qiov; 3465cf8074b3SKevin Wolf struct iovec iov = { 3466cf8074b3SKevin Wolf .iov_base = (void *) buf, 3467cf8074b3SKevin Wolf .iov_len = size, 3468cf8074b3SKevin Wolf }; 3469cf8074b3SKevin Wolf 3470cf8074b3SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 3471cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs, &qiov, pos); 3472cf8074b3SKevin Wolf } 3473cf8074b3SKevin Wolf 3474cf8074b3SKevin Wolf int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) 3475cf8074b3SKevin Wolf { 3476178e08a5Saliguori BlockDriver *drv = bs->drv; 3477cf8074b3SKevin Wolf 3478cf8074b3SKevin Wolf if (!drv) { 3479178e08a5Saliguori return -ENOMEDIUM; 3480cf8074b3SKevin Wolf } else if (drv->bdrv_save_vmstate) { 3481cf8074b3SKevin Wolf return drv->bdrv_save_vmstate(bs, qiov, pos); 3482cf8074b3SKevin Wolf } else if (bs->file) { 3483cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs->file, qiov, pos); 3484cf8074b3SKevin Wolf } 3485cf8074b3SKevin Wolf 34867cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3487178e08a5Saliguori } 3488178e08a5Saliguori 348945566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 349045566e9cSChristoph Hellwig int64_t pos, int size) 3491178e08a5Saliguori { 3492178e08a5Saliguori BlockDriver *drv = bs->drv; 3493178e08a5Saliguori if (!drv) 3494178e08a5Saliguori return -ENOMEDIUM; 34957cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 349645566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 34977cdb1f6dSMORITA Kazutaka if (bs->file) 34987cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 34997cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3500178e08a5Saliguori } 3501178e08a5Saliguori 35028b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 35038b9b0cc2SKevin Wolf { 3504bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 35058b9b0cc2SKevin Wolf return; 35068b9b0cc2SKevin Wolf } 35078b9b0cc2SKevin Wolf 3508bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 350941c695c7SKevin Wolf } 35108b9b0cc2SKevin Wolf 351141c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 351241c695c7SKevin Wolf const char *tag) 351341c695c7SKevin Wolf { 351441c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 351541c695c7SKevin Wolf bs = bs->file; 351641c695c7SKevin Wolf } 351741c695c7SKevin Wolf 351841c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 351941c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 352041c695c7SKevin Wolf } 352141c695c7SKevin Wolf 352241c695c7SKevin Wolf return -ENOTSUP; 352341c695c7SKevin Wolf } 352441c695c7SKevin Wolf 35254cc70e93SFam Zheng int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) 35264cc70e93SFam Zheng { 35274cc70e93SFam Zheng while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { 35284cc70e93SFam Zheng bs = bs->file; 35294cc70e93SFam Zheng } 35304cc70e93SFam Zheng 35314cc70e93SFam Zheng if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { 35324cc70e93SFam Zheng return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); 35334cc70e93SFam Zheng } 35344cc70e93SFam Zheng 35354cc70e93SFam Zheng return -ENOTSUP; 35364cc70e93SFam Zheng } 35374cc70e93SFam Zheng 353841c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 353941c695c7SKevin Wolf { 354041c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_resume) { 354141c695c7SKevin Wolf bs = bs->file; 354241c695c7SKevin Wolf } 354341c695c7SKevin Wolf 354441c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 354541c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 354641c695c7SKevin Wolf } 354741c695c7SKevin Wolf 354841c695c7SKevin Wolf return -ENOTSUP; 354941c695c7SKevin Wolf } 355041c695c7SKevin Wolf 355141c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 355241c695c7SKevin Wolf { 355341c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 355441c695c7SKevin Wolf bs = bs->file; 355541c695c7SKevin Wolf } 355641c695c7SKevin Wolf 355741c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 355841c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 355941c695c7SKevin Wolf } 356041c695c7SKevin Wolf 356141c695c7SKevin Wolf return false; 35628b9b0cc2SKevin Wolf } 35638b9b0cc2SKevin Wolf 3564199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 3565199630b6SBlue Swirl { 3566199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 3567199630b6SBlue Swirl } 3568199630b6SBlue Swirl 3569b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 3570b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 3571b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 3572b1b1d783SJeff Cody * the CWD rather than the chain. */ 3573e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 3574e8a6bb9cSMarcelo Tosatti const char *backing_file) 3575e8a6bb9cSMarcelo Tosatti { 3576b1b1d783SJeff Cody char *filename_full = NULL; 3577b1b1d783SJeff Cody char *backing_file_full = NULL; 3578b1b1d783SJeff Cody char *filename_tmp = NULL; 3579b1b1d783SJeff Cody int is_protocol = 0; 3580b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 3581b1b1d783SJeff Cody BlockDriverState *retval = NULL; 3582b1b1d783SJeff Cody 3583b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 3584e8a6bb9cSMarcelo Tosatti return NULL; 3585e8a6bb9cSMarcelo Tosatti } 3586e8a6bb9cSMarcelo Tosatti 3587b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 3588b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 3589b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 3590b1b1d783SJeff Cody 3591b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 3592b1b1d783SJeff Cody 3593b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 3594b1b1d783SJeff Cody 3595b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 3596b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 3597b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 3598b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 3599b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3600b1b1d783SJeff Cody break; 3601b1b1d783SJeff Cody } 3602e8a6bb9cSMarcelo Tosatti } else { 3603b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 3604b1b1d783SJeff Cody * image's filename path */ 3605b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3606b1b1d783SJeff Cody backing_file); 3607b1b1d783SJeff Cody 3608b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 3609b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 3610b1b1d783SJeff Cody continue; 3611b1b1d783SJeff Cody } 3612b1b1d783SJeff Cody 3613b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 3614b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 3615b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3616b1b1d783SJeff Cody curr_bs->backing_file); 3617b1b1d783SJeff Cody 3618b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 3619b1b1d783SJeff Cody continue; 3620b1b1d783SJeff Cody } 3621b1b1d783SJeff Cody 3622b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 3623b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3624b1b1d783SJeff Cody break; 3625b1b1d783SJeff Cody } 3626e8a6bb9cSMarcelo Tosatti } 3627e8a6bb9cSMarcelo Tosatti } 3628e8a6bb9cSMarcelo Tosatti 3629b1b1d783SJeff Cody g_free(filename_full); 3630b1b1d783SJeff Cody g_free(backing_file_full); 3631b1b1d783SJeff Cody g_free(filename_tmp); 3632b1b1d783SJeff Cody return retval; 3633e8a6bb9cSMarcelo Tosatti } 3634e8a6bb9cSMarcelo Tosatti 3635f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 3636f198fd1cSBenoît Canet { 3637f198fd1cSBenoît Canet if (!bs->drv) { 3638f198fd1cSBenoît Canet return 0; 3639f198fd1cSBenoît Canet } 3640f198fd1cSBenoît Canet 3641f198fd1cSBenoît Canet if (!bs->backing_hd) { 3642f198fd1cSBenoît Canet return 0; 3643f198fd1cSBenoît Canet } 3644f198fd1cSBenoît Canet 3645f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 3646f198fd1cSBenoît Canet } 3647f198fd1cSBenoît Canet 364879fac568SJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 364979fac568SJeff Cody { 365079fac568SJeff Cody BlockDriverState *curr_bs = NULL; 365179fac568SJeff Cody 365279fac568SJeff Cody if (!bs) { 365379fac568SJeff Cody return NULL; 365479fac568SJeff Cody } 365579fac568SJeff Cody 365679fac568SJeff Cody curr_bs = bs; 365779fac568SJeff Cody 365879fac568SJeff Cody while (curr_bs->backing_hd) { 365979fac568SJeff Cody curr_bs = curr_bs->backing_hd; 366079fac568SJeff Cody } 366179fac568SJeff Cody return curr_bs; 366279fac568SJeff Cody } 366379fac568SJeff Cody 3664ea2384d3Sbellard /**************************************************************/ 366583f64091Sbellard /* async I/Os */ 3666ea2384d3Sbellard 36673b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 3668f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 366983f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 3670ea2384d3Sbellard { 3671bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 3672bbf0a440SStefan Hajnoczi 3673d20d9b7cSPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, 36748c5873d6SStefan Hajnoczi cb, opaque, false); 367583f64091Sbellard } 367683f64091Sbellard 3677f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 3678f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 367983f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 36807674e7bfSbellard { 3681bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 3682bbf0a440SStefan Hajnoczi 3683d20d9b7cSPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, 36848c5873d6SStefan Hajnoczi cb, opaque, true); 368583f64091Sbellard } 368683f64091Sbellard 3687*d5ef94d4SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs, 3688*d5ef94d4SPaolo Bonzini int64_t sector_num, int nb_sectors, BdrvRequestFlags flags, 3689*d5ef94d4SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 3690*d5ef94d4SPaolo Bonzini { 3691*d5ef94d4SPaolo Bonzini trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque); 3692*d5ef94d4SPaolo Bonzini 3693*d5ef94d4SPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors, 3694*d5ef94d4SPaolo Bonzini BDRV_REQ_ZERO_WRITE | flags, 3695*d5ef94d4SPaolo Bonzini cb, opaque, true); 3696*d5ef94d4SPaolo Bonzini } 3697*d5ef94d4SPaolo Bonzini 369840b4f539SKevin Wolf 369940b4f539SKevin Wolf typedef struct MultiwriteCB { 370040b4f539SKevin Wolf int error; 370140b4f539SKevin Wolf int num_requests; 370240b4f539SKevin Wolf int num_callbacks; 370340b4f539SKevin Wolf struct { 370440b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 370540b4f539SKevin Wolf void *opaque; 370640b4f539SKevin Wolf QEMUIOVector *free_qiov; 370740b4f539SKevin Wolf } callbacks[]; 370840b4f539SKevin Wolf } MultiwriteCB; 370940b4f539SKevin Wolf 371040b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 371140b4f539SKevin Wolf { 371240b4f539SKevin Wolf int i; 371340b4f539SKevin Wolf 371440b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 371540b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 37161e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 37171e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 37181e1ea48dSStefan Hajnoczi } 37197267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 372040b4f539SKevin Wolf } 372140b4f539SKevin Wolf } 372240b4f539SKevin Wolf 372340b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 372440b4f539SKevin Wolf { 372540b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 372640b4f539SKevin Wolf 37276d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 37286d519a5fSStefan Hajnoczi 3729cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 373040b4f539SKevin Wolf mcb->error = ret; 373140b4f539SKevin Wolf } 373240b4f539SKevin Wolf 373340b4f539SKevin Wolf mcb->num_requests--; 373440b4f539SKevin Wolf if (mcb->num_requests == 0) { 373540b4f539SKevin Wolf multiwrite_user_cb(mcb); 37367267c094SAnthony Liguori g_free(mcb); 373740b4f539SKevin Wolf } 373840b4f539SKevin Wolf } 373940b4f539SKevin Wolf 374040b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 374140b4f539SKevin Wolf { 374277be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 374377be4366SChristoph Hellwig 374477be4366SChristoph Hellwig /* 374577be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 374677be4366SChristoph Hellwig * here as that could overflow the return value. 374777be4366SChristoph Hellwig */ 374877be4366SChristoph Hellwig if (req1->sector > req2->sector) { 374977be4366SChristoph Hellwig return 1; 375077be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 375177be4366SChristoph Hellwig return -1; 375277be4366SChristoph Hellwig } else { 375377be4366SChristoph Hellwig return 0; 375477be4366SChristoph Hellwig } 375540b4f539SKevin Wolf } 375640b4f539SKevin Wolf 375740b4f539SKevin Wolf /* 375840b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 375940b4f539SKevin Wolf * requests that remain after merging. 376040b4f539SKevin Wolf */ 376140b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 376240b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 376340b4f539SKevin Wolf { 376440b4f539SKevin Wolf int i, outidx; 376540b4f539SKevin Wolf 376640b4f539SKevin Wolf // Sort requests by start sector 376740b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 376840b4f539SKevin Wolf 376940b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 377040b4f539SKevin Wolf // filling up gaps with zero sectors. 377140b4f539SKevin Wolf outidx = 0; 377240b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 377340b4f539SKevin Wolf int merge = 0; 377440b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 377540b4f539SKevin Wolf 3776b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 377740b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 377840b4f539SKevin Wolf merge = 1; 377940b4f539SKevin Wolf } 378040b4f539SKevin Wolf 3781e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3782e2a305fbSChristoph Hellwig merge = 0; 3783e2a305fbSChristoph Hellwig } 3784e2a305fbSChristoph Hellwig 378540b4f539SKevin Wolf if (merge) { 378640b4f539SKevin Wolf size_t size; 37877267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 378840b4f539SKevin Wolf qemu_iovec_init(qiov, 378940b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 379040b4f539SKevin Wolf 379140b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 379240b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 379340b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 37941b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 379540b4f539SKevin Wolf 3796b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3797b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 379840b4f539SKevin Wolf 379940b4f539SKevin Wolf // Add the second request 38001b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 380140b4f539SKevin Wolf 3802cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 380340b4f539SKevin Wolf reqs[outidx].qiov = qiov; 380440b4f539SKevin Wolf 380540b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 380640b4f539SKevin Wolf } else { 380740b4f539SKevin Wolf outidx++; 380840b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 380940b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 381040b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 381140b4f539SKevin Wolf } 381240b4f539SKevin Wolf } 381340b4f539SKevin Wolf 381440b4f539SKevin Wolf return outidx + 1; 381540b4f539SKevin Wolf } 381640b4f539SKevin Wolf 381740b4f539SKevin Wolf /* 381840b4f539SKevin Wolf * Submit multiple AIO write requests at once. 381940b4f539SKevin Wolf * 382040b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 382140b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 382240b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 382340b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 382440b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 382540b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 382640b4f539SKevin Wolf * 382740b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 382840b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 382940b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 383040b4f539SKevin Wolf */ 383140b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 383240b4f539SKevin Wolf { 383340b4f539SKevin Wolf MultiwriteCB *mcb; 383440b4f539SKevin Wolf int i; 383540b4f539SKevin Wolf 3836301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3837301db7c2SRyan Harper if (bs->drv == NULL) { 3838301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3839301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3840301db7c2SRyan Harper } 3841301db7c2SRyan Harper return -1; 3842301db7c2SRyan Harper } 3843301db7c2SRyan Harper 384440b4f539SKevin Wolf if (num_reqs == 0) { 384540b4f539SKevin Wolf return 0; 384640b4f539SKevin Wolf } 384740b4f539SKevin Wolf 384840b4f539SKevin Wolf // Create MultiwriteCB structure 38497267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 385040b4f539SKevin Wolf mcb->num_requests = 0; 385140b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 385240b4f539SKevin Wolf 385340b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 385440b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 385540b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 385640b4f539SKevin Wolf } 385740b4f539SKevin Wolf 385840b4f539SKevin Wolf // Check for mergable requests 385940b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 386040b4f539SKevin Wolf 38616d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 38626d519a5fSStefan Hajnoczi 3863df9309fbSPaolo Bonzini /* Run the aio requests. */ 3864df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 386540b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3866d20d9b7cSPaolo Bonzini bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov, 3867d20d9b7cSPaolo Bonzini reqs[i].nb_sectors, reqs[i].flags, 3868d20d9b7cSPaolo Bonzini multiwrite_cb, mcb, 3869d20d9b7cSPaolo Bonzini true); 387040b4f539SKevin Wolf } 387140b4f539SKevin Wolf 387240b4f539SKevin Wolf return 0; 387340b4f539SKevin Wolf } 387440b4f539SKevin Wolf 387583f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 387683f64091Sbellard { 3877d7331bedSStefan Hajnoczi acb->aiocb_info->cancel(acb); 387883f64091Sbellard } 387983f64091Sbellard 388083f64091Sbellard /**************************************************************/ 388183f64091Sbellard /* async block device emulation */ 388283f64091Sbellard 3883c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3884c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3885c16b5a2cSChristoph Hellwig QEMUBH *bh; 3886c16b5a2cSChristoph Hellwig int ret; 3887c16b5a2cSChristoph Hellwig /* vector translation state */ 3888c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3889c16b5a2cSChristoph Hellwig uint8_t *bounce; 3890c16b5a2cSChristoph Hellwig int is_write; 3891c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3892c16b5a2cSChristoph Hellwig 3893c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3894c16b5a2cSChristoph Hellwig { 3895b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3896b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 38976a7ad299SDor Laor qemu_bh_delete(acb->bh); 389836afc451SAvi Kivity acb->bh = NULL; 3899c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3900c16b5a2cSChristoph Hellwig } 3901c16b5a2cSChristoph Hellwig 3902d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_aiocb_info = { 3903c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3904c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3905c16b5a2cSChristoph Hellwig }; 3906c16b5a2cSChristoph Hellwig 390783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3908beac80cdSbellard { 3909ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3910f141eafeSaliguori 3911f141eafeSaliguori if (!acb->is_write) 391203396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 3913ceb42de8Saliguori qemu_vfree(acb->bounce); 3914ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 39156a7ad299SDor Laor qemu_bh_delete(acb->bh); 391636afc451SAvi Kivity acb->bh = NULL; 3917ce1a14dcSpbrook qemu_aio_release(acb); 3918beac80cdSbellard } 3919beac80cdSbellard 3920f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3921f141eafeSaliguori int64_t sector_num, 3922f141eafeSaliguori QEMUIOVector *qiov, 3923f141eafeSaliguori int nb_sectors, 3924f141eafeSaliguori BlockDriverCompletionFunc *cb, 3925f141eafeSaliguori void *opaque, 3926f141eafeSaliguori int is_write) 3927f141eafeSaliguori 3928ea2384d3Sbellard { 3929ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 393083f64091Sbellard 3931d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); 3932f141eafeSaliguori acb->is_write = is_write; 3933f141eafeSaliguori acb->qiov = qiov; 3934e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3935ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3936f141eafeSaliguori 3937f141eafeSaliguori if (is_write) { 3938d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 39391ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3940f141eafeSaliguori } else { 39411ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3942f141eafeSaliguori } 3943f141eafeSaliguori 3944ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3945f141eafeSaliguori 3946ce1a14dcSpbrook return &acb->common; 39477a6cba61Spbrook } 39487a6cba61Spbrook 3949f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3950f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3951ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 395283f64091Sbellard { 3953f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 395483f64091Sbellard } 395583f64091Sbellard 3956f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3957f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3958f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3959f141eafeSaliguori { 3960f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3961f141eafeSaliguori } 3962f141eafeSaliguori 396368485420SKevin Wolf 396468485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 396568485420SKevin Wolf BlockDriverAIOCB common; 396668485420SKevin Wolf BlockRequest req; 396768485420SKevin Wolf bool is_write; 3968d318aea9SKevin Wolf bool *done; 396968485420SKevin Wolf QEMUBH* bh; 397068485420SKevin Wolf } BlockDriverAIOCBCoroutine; 397168485420SKevin Wolf 397268485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 397368485420SKevin Wolf { 3974d318aea9SKevin Wolf BlockDriverAIOCBCoroutine *acb = 3975d318aea9SKevin Wolf container_of(blockacb, BlockDriverAIOCBCoroutine, common); 3976d318aea9SKevin Wolf bool done = false; 3977d318aea9SKevin Wolf 3978d318aea9SKevin Wolf acb->done = &done; 3979d318aea9SKevin Wolf while (!done) { 3980d318aea9SKevin Wolf qemu_aio_wait(); 3981d318aea9SKevin Wolf } 398268485420SKevin Wolf } 398368485420SKevin Wolf 3984d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_co_aiocb_info = { 398568485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 398668485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 398768485420SKevin Wolf }; 398868485420SKevin Wolf 398935246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 399068485420SKevin Wolf { 399168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 399268485420SKevin Wolf 399368485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 3994d318aea9SKevin Wolf 3995d318aea9SKevin Wolf if (acb->done) { 3996d318aea9SKevin Wolf *acb->done = true; 3997d318aea9SKevin Wolf } 3998d318aea9SKevin Wolf 399968485420SKevin Wolf qemu_bh_delete(acb->bh); 400068485420SKevin Wolf qemu_aio_release(acb); 400168485420SKevin Wolf } 400268485420SKevin Wolf 4003b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 4004b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 4005b2a61371SStefan Hajnoczi { 4006b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 4007b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 4008b2a61371SStefan Hajnoczi 4009b2a61371SStefan Hajnoczi if (!acb->is_write) { 4010b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 4011d20d9b7cSPaolo Bonzini acb->req.nb_sectors, acb->req.qiov, acb->req.flags); 4012b2a61371SStefan Hajnoczi } else { 4013b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 4014d20d9b7cSPaolo Bonzini acb->req.nb_sectors, acb->req.qiov, acb->req.flags); 4015b2a61371SStefan Hajnoczi } 4016b2a61371SStefan Hajnoczi 401735246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 4018b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 4019b2a61371SStefan Hajnoczi } 4020b2a61371SStefan Hajnoczi 402168485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 402268485420SKevin Wolf int64_t sector_num, 402368485420SKevin Wolf QEMUIOVector *qiov, 402468485420SKevin Wolf int nb_sectors, 4025d20d9b7cSPaolo Bonzini BdrvRequestFlags flags, 402668485420SKevin Wolf BlockDriverCompletionFunc *cb, 402768485420SKevin Wolf void *opaque, 40288c5873d6SStefan Hajnoczi bool is_write) 402968485420SKevin Wolf { 403068485420SKevin Wolf Coroutine *co; 403168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 403268485420SKevin Wolf 4033d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 403468485420SKevin Wolf acb->req.sector = sector_num; 403568485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 403668485420SKevin Wolf acb->req.qiov = qiov; 4037d20d9b7cSPaolo Bonzini acb->req.flags = flags; 403868485420SKevin Wolf acb->is_write = is_write; 4039d318aea9SKevin Wolf acb->done = NULL; 404068485420SKevin Wolf 40418c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 404268485420SKevin Wolf qemu_coroutine_enter(co, acb); 404368485420SKevin Wolf 404468485420SKevin Wolf return &acb->common; 404568485420SKevin Wolf } 404668485420SKevin Wolf 404707f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 4048b2e12bc6SChristoph Hellwig { 404907f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 405007f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 4051b2e12bc6SChristoph Hellwig 405207f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 405307f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 4054b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 4055b2e12bc6SChristoph Hellwig } 4056b2e12bc6SChristoph Hellwig 405707f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 4058016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 4059016f5cf6SAlexander Graf { 406007f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 4061016f5cf6SAlexander Graf 406207f07615SPaolo Bonzini Coroutine *co; 406307f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 4064016f5cf6SAlexander Graf 4065d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 4066d318aea9SKevin Wolf acb->done = NULL; 4067d318aea9SKevin Wolf 406807f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 406907f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 4070016f5cf6SAlexander Graf 4071016f5cf6SAlexander Graf return &acb->common; 4072016f5cf6SAlexander Graf } 4073016f5cf6SAlexander Graf 40744265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 40754265d620SPaolo Bonzini { 40764265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 40774265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 40784265d620SPaolo Bonzini 40794265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 40804265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 40814265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 40824265d620SPaolo Bonzini } 40834265d620SPaolo Bonzini 40844265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 40854265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 40864265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 40874265d620SPaolo Bonzini { 40884265d620SPaolo Bonzini Coroutine *co; 40894265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 40904265d620SPaolo Bonzini 40914265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 40924265d620SPaolo Bonzini 4093d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 40944265d620SPaolo Bonzini acb->req.sector = sector_num; 40954265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 4096d318aea9SKevin Wolf acb->done = NULL; 40974265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 40984265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 40994265d620SPaolo Bonzini 41004265d620SPaolo Bonzini return &acb->common; 41014265d620SPaolo Bonzini } 41024265d620SPaolo Bonzini 4103ea2384d3Sbellard void bdrv_init(void) 4104ea2384d3Sbellard { 41055efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 4106ea2384d3Sbellard } 4107ce1a14dcSpbrook 4108eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 4109eb852011SMarkus Armbruster { 4110eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 4111eb852011SMarkus Armbruster bdrv_init(); 4112eb852011SMarkus Armbruster } 4113eb852011SMarkus Armbruster 4114d7331bedSStefan Hajnoczi void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 41156bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 41166bbff9a0Saliguori { 4117ce1a14dcSpbrook BlockDriverAIOCB *acb; 4118ce1a14dcSpbrook 4119d7331bedSStefan Hajnoczi acb = g_slice_alloc(aiocb_info->aiocb_size); 4120d7331bedSStefan Hajnoczi acb->aiocb_info = aiocb_info; 4121ce1a14dcSpbrook acb->bs = bs; 4122ce1a14dcSpbrook acb->cb = cb; 4123ce1a14dcSpbrook acb->opaque = opaque; 4124ce1a14dcSpbrook return acb; 4125ce1a14dcSpbrook } 4126ce1a14dcSpbrook 4127ce1a14dcSpbrook void qemu_aio_release(void *p) 4128ce1a14dcSpbrook { 4129d37c975fSStefan Hajnoczi BlockDriverAIOCB *acb = p; 4130d7331bedSStefan Hajnoczi g_slice_free1(acb->aiocb_info->aiocb_size, acb); 4131ce1a14dcSpbrook } 413219cb3738Sbellard 413319cb3738Sbellard /**************************************************************/ 4134f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 4135f9f05dc5SKevin Wolf 4136f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 4137f9f05dc5SKevin Wolf Coroutine *coroutine; 4138f9f05dc5SKevin Wolf int ret; 4139f9f05dc5SKevin Wolf } CoroutineIOCompletion; 4140f9f05dc5SKevin Wolf 4141f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 4142f9f05dc5SKevin Wolf { 4143f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 4144f9f05dc5SKevin Wolf 4145f9f05dc5SKevin Wolf co->ret = ret; 4146f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 4147f9f05dc5SKevin Wolf } 4148f9f05dc5SKevin Wolf 4149f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 4150f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 4151f9f05dc5SKevin Wolf bool is_write) 4152f9f05dc5SKevin Wolf { 4153f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 4154f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 4155f9f05dc5SKevin Wolf }; 4156f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 4157f9f05dc5SKevin Wolf 4158f9f05dc5SKevin Wolf if (is_write) { 4159a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 4160f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4161f9f05dc5SKevin Wolf } else { 4162a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 4163f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4164f9f05dc5SKevin Wolf } 4165f9f05dc5SKevin Wolf 416659370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 4167f9f05dc5SKevin Wolf if (!acb) { 4168f9f05dc5SKevin Wolf return -EIO; 4169f9f05dc5SKevin Wolf } 4170f9f05dc5SKevin Wolf qemu_coroutine_yield(); 4171f9f05dc5SKevin Wolf 4172f9f05dc5SKevin Wolf return co.ret; 4173f9f05dc5SKevin Wolf } 4174f9f05dc5SKevin Wolf 4175f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 4176f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4177f9f05dc5SKevin Wolf QEMUIOVector *iov) 4178f9f05dc5SKevin Wolf { 4179f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 4180f9f05dc5SKevin Wolf } 4181f9f05dc5SKevin Wolf 4182f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 4183f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4184f9f05dc5SKevin Wolf QEMUIOVector *iov) 4185f9f05dc5SKevin Wolf { 4186f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 4187f9f05dc5SKevin Wolf } 4188f9f05dc5SKevin Wolf 418907f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 4190e7a8a783SKevin Wolf { 419107f07615SPaolo Bonzini RwCo *rwco = opaque; 419207f07615SPaolo Bonzini 419307f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 419407f07615SPaolo Bonzini } 419507f07615SPaolo Bonzini 419607f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 419707f07615SPaolo Bonzini { 4198eb489bb1SKevin Wolf int ret; 4199eb489bb1SKevin Wolf 420029cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 420107f07615SPaolo Bonzini return 0; 4202eb489bb1SKevin Wolf } 4203eb489bb1SKevin Wolf 4204ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 4205bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); 4206eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 4207eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 4208eb489bb1SKevin Wolf if (ret < 0) { 4209eb489bb1SKevin Wolf return ret; 4210eb489bb1SKevin Wolf } 4211eb489bb1SKevin Wolf } 4212eb489bb1SKevin Wolf 4213ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 4214ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 4215d4c82329SKevin Wolf goto flush_parent; 4216ca716364SKevin Wolf } 4217ca716364SKevin Wolf 4218bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); 4219eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 422029cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 422107f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 422207f07615SPaolo Bonzini BlockDriverAIOCB *acb; 4223e7a8a783SKevin Wolf CoroutineIOCompletion co = { 4224e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 4225e7a8a783SKevin Wolf }; 4226e7a8a783SKevin Wolf 422707f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 422807f07615SPaolo Bonzini if (acb == NULL) { 422929cdb251SPaolo Bonzini ret = -EIO; 423007f07615SPaolo Bonzini } else { 4231e7a8a783SKevin Wolf qemu_coroutine_yield(); 423229cdb251SPaolo Bonzini ret = co.ret; 4233e7a8a783SKevin Wolf } 423407f07615SPaolo Bonzini } else { 423507f07615SPaolo Bonzini /* 423607f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 423707f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 423807f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 423907f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 424007f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 424107f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 424207f07615SPaolo Bonzini * mode. 424307f07615SPaolo Bonzini * 424407f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 424507f07615SPaolo Bonzini */ 424629cdb251SPaolo Bonzini ret = 0; 424707f07615SPaolo Bonzini } 424829cdb251SPaolo Bonzini if (ret < 0) { 424929cdb251SPaolo Bonzini return ret; 425029cdb251SPaolo Bonzini } 425129cdb251SPaolo Bonzini 425229cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 425329cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 425429cdb251SPaolo Bonzini */ 4255d4c82329SKevin Wolf flush_parent: 425629cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 425707f07615SPaolo Bonzini } 425807f07615SPaolo Bonzini 42590f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 42600f15423cSAnthony Liguori { 42610f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 42620f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 42630f15423cSAnthony Liguori } 42640f15423cSAnthony Liguori } 42650f15423cSAnthony Liguori 42660f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 42670f15423cSAnthony Liguori { 42680f15423cSAnthony Liguori BlockDriverState *bs; 42690f15423cSAnthony Liguori 42700f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 42710f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 42720f15423cSAnthony Liguori } 42730f15423cSAnthony Liguori } 42740f15423cSAnthony Liguori 427507789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 427607789269SBenoît Canet { 427707789269SBenoît Canet BlockDriverState *bs; 427807789269SBenoît Canet 427907789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 428007789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 428107789269SBenoît Canet } 428207789269SBenoît Canet } 428307789269SBenoît Canet 428407f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 428507f07615SPaolo Bonzini { 428607f07615SPaolo Bonzini Coroutine *co; 428707f07615SPaolo Bonzini RwCo rwco = { 428807f07615SPaolo Bonzini .bs = bs, 428907f07615SPaolo Bonzini .ret = NOT_DONE, 429007f07615SPaolo Bonzini }; 429107f07615SPaolo Bonzini 429207f07615SPaolo Bonzini if (qemu_in_coroutine()) { 429307f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 429407f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 429507f07615SPaolo Bonzini } else { 429607f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 429707f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 429807f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 429907f07615SPaolo Bonzini qemu_aio_wait(); 430007f07615SPaolo Bonzini } 430107f07615SPaolo Bonzini } 430207f07615SPaolo Bonzini 430307f07615SPaolo Bonzini return rwco.ret; 430407f07615SPaolo Bonzini } 4305e7a8a783SKevin Wolf 43064265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 43074265d620SPaolo Bonzini { 43084265d620SPaolo Bonzini RwCo *rwco = opaque; 43094265d620SPaolo Bonzini 43104265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 43114265d620SPaolo Bonzini } 43124265d620SPaolo Bonzini 43136f14da52SPeter Lieven /* if no limit is specified in the BlockLimits use a default 43146f14da52SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 43156f14da52SPeter Lieven */ 43166f14da52SPeter Lieven #define MAX_DISCARD_DEFAULT 32768 43176f14da52SPeter Lieven 43184265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 43194265d620SPaolo Bonzini int nb_sectors) 43204265d620SPaolo Bonzini { 4321d51e9fe5SPaolo Bonzini int max_discard; 4322d51e9fe5SPaolo Bonzini 43234265d620SPaolo Bonzini if (!bs->drv) { 43244265d620SPaolo Bonzini return -ENOMEDIUM; 43254265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 43264265d620SPaolo Bonzini return -EIO; 43274265d620SPaolo Bonzini } else if (bs->read_only) { 43284265d620SPaolo Bonzini return -EROFS; 4329df702c9bSPaolo Bonzini } 4330df702c9bSPaolo Bonzini 43318f0720ecSPaolo Bonzini bdrv_reset_dirty(bs, sector_num, nb_sectors); 4332df702c9bSPaolo Bonzini 43339e8f1835SPaolo Bonzini /* Do nothing if disabled. */ 43349e8f1835SPaolo Bonzini if (!(bs->open_flags & BDRV_O_UNMAP)) { 43359e8f1835SPaolo Bonzini return 0; 43369e8f1835SPaolo Bonzini } 43379e8f1835SPaolo Bonzini 4338d51e9fe5SPaolo Bonzini if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) { 4339d51e9fe5SPaolo Bonzini return 0; 4340d51e9fe5SPaolo Bonzini } 43416f14da52SPeter Lieven 4342d51e9fe5SPaolo Bonzini max_discard = bs->bl.max_discard ? bs->bl.max_discard : MAX_DISCARD_DEFAULT; 43436f14da52SPeter Lieven while (nb_sectors > 0) { 43446f14da52SPeter Lieven int ret; 43456f14da52SPeter Lieven int num = nb_sectors; 43466f14da52SPeter Lieven 43476f14da52SPeter Lieven /* align request */ 43486f14da52SPeter Lieven if (bs->bl.discard_alignment && 43496f14da52SPeter Lieven num >= bs->bl.discard_alignment && 43506f14da52SPeter Lieven sector_num % bs->bl.discard_alignment) { 43516f14da52SPeter Lieven if (num > bs->bl.discard_alignment) { 43526f14da52SPeter Lieven num = bs->bl.discard_alignment; 43536f14da52SPeter Lieven } 43546f14da52SPeter Lieven num -= sector_num % bs->bl.discard_alignment; 43556f14da52SPeter Lieven } 43566f14da52SPeter Lieven 43576f14da52SPeter Lieven /* limit request size */ 43586f14da52SPeter Lieven if (num > max_discard) { 43596f14da52SPeter Lieven num = max_discard; 43606f14da52SPeter Lieven } 43616f14da52SPeter Lieven 4362d51e9fe5SPaolo Bonzini if (bs->drv->bdrv_co_discard) { 43636f14da52SPeter Lieven ret = bs->drv->bdrv_co_discard(bs, sector_num, num); 4364d51e9fe5SPaolo Bonzini } else { 43654265d620SPaolo Bonzini BlockDriverAIOCB *acb; 43664265d620SPaolo Bonzini CoroutineIOCompletion co = { 43674265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 43684265d620SPaolo Bonzini }; 43694265d620SPaolo Bonzini 43704265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 43714265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 43724265d620SPaolo Bonzini if (acb == NULL) { 43734265d620SPaolo Bonzini return -EIO; 43744265d620SPaolo Bonzini } else { 43754265d620SPaolo Bonzini qemu_coroutine_yield(); 4376d51e9fe5SPaolo Bonzini ret = co.ret; 43774265d620SPaolo Bonzini } 4378d51e9fe5SPaolo Bonzini } 4379d51e9fe5SPaolo Bonzini if (ret) { 4380d51e9fe5SPaolo Bonzini return ret; 4381d51e9fe5SPaolo Bonzini } 4382d51e9fe5SPaolo Bonzini 4383d51e9fe5SPaolo Bonzini sector_num += num; 4384d51e9fe5SPaolo Bonzini nb_sectors -= num; 4385d51e9fe5SPaolo Bonzini } 43864265d620SPaolo Bonzini return 0; 43874265d620SPaolo Bonzini } 43884265d620SPaolo Bonzini 43894265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 43904265d620SPaolo Bonzini { 43914265d620SPaolo Bonzini Coroutine *co; 43924265d620SPaolo Bonzini RwCo rwco = { 43934265d620SPaolo Bonzini .bs = bs, 43944265d620SPaolo Bonzini .sector_num = sector_num, 43954265d620SPaolo Bonzini .nb_sectors = nb_sectors, 43964265d620SPaolo Bonzini .ret = NOT_DONE, 43974265d620SPaolo Bonzini }; 43984265d620SPaolo Bonzini 43994265d620SPaolo Bonzini if (qemu_in_coroutine()) { 44004265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 44014265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 44024265d620SPaolo Bonzini } else { 44034265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 44044265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 44054265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 44064265d620SPaolo Bonzini qemu_aio_wait(); 44074265d620SPaolo Bonzini } 44084265d620SPaolo Bonzini } 44094265d620SPaolo Bonzini 44104265d620SPaolo Bonzini return rwco.ret; 44114265d620SPaolo Bonzini } 44124265d620SPaolo Bonzini 4413f9f05dc5SKevin Wolf /**************************************************************/ 441419cb3738Sbellard /* removable device support */ 441519cb3738Sbellard 441619cb3738Sbellard /** 441719cb3738Sbellard * Return TRUE if the media is present 441819cb3738Sbellard */ 441919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 442019cb3738Sbellard { 442119cb3738Sbellard BlockDriver *drv = bs->drv; 4422a1aff5bfSMarkus Armbruster 442319cb3738Sbellard if (!drv) 442419cb3738Sbellard return 0; 442519cb3738Sbellard if (!drv->bdrv_is_inserted) 4426a1aff5bfSMarkus Armbruster return 1; 4427a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 442819cb3738Sbellard } 442919cb3738Sbellard 443019cb3738Sbellard /** 44318e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 44328e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 443319cb3738Sbellard */ 443419cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 443519cb3738Sbellard { 443619cb3738Sbellard BlockDriver *drv = bs->drv; 443719cb3738Sbellard 44388e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 44398e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 44408e49ca46SMarkus Armbruster } 44418e49ca46SMarkus Armbruster return -ENOTSUP; 444219cb3738Sbellard } 444319cb3738Sbellard 444419cb3738Sbellard /** 444519cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 444619cb3738Sbellard */ 4447f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 444819cb3738Sbellard { 444919cb3738Sbellard BlockDriver *drv = bs->drv; 445019cb3738Sbellard 4451822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 4452822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 445319cb3738Sbellard } 44546f382ed2SLuiz Capitulino 44556f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 44566f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 44576f382ed2SLuiz Capitulino } 445819cb3738Sbellard } 445919cb3738Sbellard 446019cb3738Sbellard /** 446119cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 446219cb3738Sbellard * to eject it manually). 446319cb3738Sbellard */ 4464025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 446519cb3738Sbellard { 446619cb3738Sbellard BlockDriver *drv = bs->drv; 446719cb3738Sbellard 4468025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 4469b8c6d095SStefan Hajnoczi 4470025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 4471025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 447219cb3738Sbellard } 447319cb3738Sbellard } 4474985a03b0Sths 4475985a03b0Sths /* needed for generic scsi interface */ 4476985a03b0Sths 4477985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 4478985a03b0Sths { 4479985a03b0Sths BlockDriver *drv = bs->drv; 4480985a03b0Sths 4481985a03b0Sths if (drv && drv->bdrv_ioctl) 4482985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 4483985a03b0Sths return -ENOTSUP; 4484985a03b0Sths } 44857d780669Saliguori 4486221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 4487221f715dSaliguori unsigned long int req, void *buf, 44887d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 44897d780669Saliguori { 4490221f715dSaliguori BlockDriver *drv = bs->drv; 44917d780669Saliguori 4492221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 4493221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 4494221f715dSaliguori return NULL; 44957d780669Saliguori } 4496e268ca52Saliguori 44977b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 44987b6f9300SMarkus Armbruster { 44997b6f9300SMarkus Armbruster bs->buffer_alignment = align; 45007b6f9300SMarkus Armbruster } 45017cd1e32aSlirans@il.ibm.com 4502e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 4503e268ca52Saliguori { 4504e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 4505e268ca52Saliguori } 45067cd1e32aSlirans@il.ibm.com 4507c53b1c51SStefan Hajnoczi /* 4508c53b1c51SStefan Hajnoczi * Check if all memory in this vector is sector aligned. 4509c53b1c51SStefan Hajnoczi */ 4510c53b1c51SStefan Hajnoczi bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) 4511c53b1c51SStefan Hajnoczi { 4512c53b1c51SStefan Hajnoczi int i; 4513c53b1c51SStefan Hajnoczi 4514c53b1c51SStefan Hajnoczi for (i = 0; i < qiov->niov; i++) { 4515c53b1c51SStefan Hajnoczi if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { 4516c53b1c51SStefan Hajnoczi return false; 4517c53b1c51SStefan Hajnoczi } 4518c53b1c51SStefan Hajnoczi } 4519c53b1c51SStefan Hajnoczi 4520c53b1c51SStefan Hajnoczi return true; 4521c53b1c51SStefan Hajnoczi } 4522c53b1c51SStefan Hajnoczi 4523e4654d2dSFam Zheng BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity) 45247cd1e32aSlirans@il.ibm.com { 45257cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 4526e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4527a55eb92cSJan Kiszka 452850717e94SPaolo Bonzini assert((granularity & (granularity - 1)) == 0); 452950717e94SPaolo Bonzini 453050717e94SPaolo Bonzini granularity >>= BDRV_SECTOR_BITS; 4531e4654d2dSFam Zheng assert(granularity); 45328f0720ecSPaolo Bonzini bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); 4533e4654d2dSFam Zheng bitmap = g_malloc0(sizeof(BdrvDirtyBitmap)); 4534e4654d2dSFam Zheng bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); 4535e4654d2dSFam Zheng QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); 4536e4654d2dSFam Zheng return bitmap; 4537e4654d2dSFam Zheng } 4538e4654d2dSFam Zheng 4539e4654d2dSFam Zheng void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 4540e4654d2dSFam Zheng { 4541e4654d2dSFam Zheng BdrvDirtyBitmap *bm, *next; 4542e4654d2dSFam Zheng QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { 4543e4654d2dSFam Zheng if (bm == bitmap) { 4544e4654d2dSFam Zheng QLIST_REMOVE(bitmap, list); 4545e4654d2dSFam Zheng hbitmap_free(bitmap->bitmap); 4546e4654d2dSFam Zheng g_free(bitmap); 4547e4654d2dSFam Zheng return; 45487cd1e32aSlirans@il.ibm.com } 45497cd1e32aSlirans@il.ibm.com } 45507cd1e32aSlirans@il.ibm.com } 45517cd1e32aSlirans@il.ibm.com 455221b56835SFam Zheng BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 455321b56835SFam Zheng { 455421b56835SFam Zheng BdrvDirtyBitmap *bm; 455521b56835SFam Zheng BlockDirtyInfoList *list = NULL; 455621b56835SFam Zheng BlockDirtyInfoList **plist = &list; 455721b56835SFam Zheng 455821b56835SFam Zheng QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 455921b56835SFam Zheng BlockDirtyInfo *info = g_malloc0(sizeof(BlockDirtyInfo)); 456021b56835SFam Zheng BlockDirtyInfoList *entry = g_malloc0(sizeof(BlockDirtyInfoList)); 456121b56835SFam Zheng info->count = bdrv_get_dirty_count(bs, bm); 456221b56835SFam Zheng info->granularity = 456321b56835SFam Zheng ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bm->bitmap)); 456421b56835SFam Zheng entry->value = info; 456521b56835SFam Zheng *plist = entry; 456621b56835SFam Zheng plist = &entry->next; 456721b56835SFam Zheng } 456821b56835SFam Zheng 456921b56835SFam Zheng return list; 457021b56835SFam Zheng } 457121b56835SFam Zheng 4572e4654d2dSFam Zheng int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector) 45737cd1e32aSlirans@il.ibm.com { 4574e4654d2dSFam Zheng if (bitmap) { 4575e4654d2dSFam Zheng return hbitmap_get(bitmap->bitmap, sector); 45767cd1e32aSlirans@il.ibm.com } else { 45777cd1e32aSlirans@il.ibm.com return 0; 45787cd1e32aSlirans@il.ibm.com } 45797cd1e32aSlirans@il.ibm.com } 45807cd1e32aSlirans@il.ibm.com 4581e4654d2dSFam Zheng void bdrv_dirty_iter_init(BlockDriverState *bs, 4582e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap, HBitmapIter *hbi) 45831755da16SPaolo Bonzini { 4584e4654d2dSFam Zheng hbitmap_iter_init(hbi, bitmap->bitmap, 0); 45851755da16SPaolo Bonzini } 45861755da16SPaolo Bonzini 45871755da16SPaolo Bonzini void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 45881755da16SPaolo Bonzini int nr_sectors) 45891755da16SPaolo Bonzini { 4590e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4591e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 4592e4654d2dSFam Zheng hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 4593e4654d2dSFam Zheng } 45941755da16SPaolo Bonzini } 45951755da16SPaolo Bonzini 4596e4654d2dSFam Zheng void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors) 45977cd1e32aSlirans@il.ibm.com { 4598e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4599e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 4600e4654d2dSFam Zheng hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); 4601e4654d2dSFam Zheng } 46027cd1e32aSlirans@il.ibm.com } 4603aaa0eb75SLiran Schour 4604e4654d2dSFam Zheng int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 4605aaa0eb75SLiran Schour { 4606e4654d2dSFam Zheng return hbitmap_count(bitmap->bitmap); 4607aaa0eb75SLiran Schour } 4608f88e1a42SJes Sorensen 46099fcb0251SFam Zheng /* Get a reference to bs */ 46109fcb0251SFam Zheng void bdrv_ref(BlockDriverState *bs) 46119fcb0251SFam Zheng { 46129fcb0251SFam Zheng bs->refcnt++; 46139fcb0251SFam Zheng } 46149fcb0251SFam Zheng 46159fcb0251SFam Zheng /* Release a previously grabbed reference to bs. 46169fcb0251SFam Zheng * If after releasing, reference count is zero, the BlockDriverState is 46179fcb0251SFam Zheng * deleted. */ 46189fcb0251SFam Zheng void bdrv_unref(BlockDriverState *bs) 46199fcb0251SFam Zheng { 46209fcb0251SFam Zheng assert(bs->refcnt > 0); 46219fcb0251SFam Zheng if (--bs->refcnt == 0) { 46229fcb0251SFam Zheng bdrv_delete(bs); 46239fcb0251SFam Zheng } 46249fcb0251SFam Zheng } 46259fcb0251SFam Zheng 4626db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 4627db593f25SMarcelo Tosatti { 4628db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 4629db593f25SMarcelo Tosatti bs->in_use = in_use; 4630db593f25SMarcelo Tosatti } 4631db593f25SMarcelo Tosatti 4632db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 4633db593f25SMarcelo Tosatti { 4634db593f25SMarcelo Tosatti return bs->in_use; 4635db593f25SMarcelo Tosatti } 4636db593f25SMarcelo Tosatti 463728a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 463828a7282aSLuiz Capitulino { 4639d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 464058e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 464128a7282aSLuiz Capitulino } 464228a7282aSLuiz Capitulino 464328a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 464428a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 464528a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 464628a7282aSLuiz Capitulino { 4647d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 464892aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 464992aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 465092aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 465128a7282aSLuiz Capitulino } 465228a7282aSLuiz Capitulino 465328a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 465428a7282aSLuiz Capitulino { 4655d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 465628a7282aSLuiz Capitulino } 465728a7282aSLuiz Capitulino 465828a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 465928a7282aSLuiz Capitulino { 466028a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 466158e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 46623bd293c3SPaolo Bonzini if (bs->job) { 46633bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 46643bd293c3SPaolo Bonzini } 466528a7282aSLuiz Capitulino } 466628a7282aSLuiz Capitulino } 466728a7282aSLuiz Capitulino 466828a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 466928a7282aSLuiz Capitulino { 46703e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 46713e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 467258e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 467358e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 467428a7282aSLuiz Capitulino } 467528a7282aSLuiz Capitulino } 467628a7282aSLuiz Capitulino 4677a597e79cSChristoph Hellwig void 4678a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 4679a597e79cSChristoph Hellwig enum BlockAcctType type) 4680a597e79cSChristoph Hellwig { 4681a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 4682a597e79cSChristoph Hellwig 4683a597e79cSChristoph Hellwig cookie->bytes = bytes; 4684c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4685a597e79cSChristoph Hellwig cookie->type = type; 4686a597e79cSChristoph Hellwig } 4687a597e79cSChristoph Hellwig 4688a597e79cSChristoph Hellwig void 4689a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4690a597e79cSChristoph Hellwig { 4691a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4692a597e79cSChristoph Hellwig 4693a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4694a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4695c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4696a597e79cSChristoph Hellwig } 4697a597e79cSChristoph Hellwig 4698d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 4699f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 4700f382d43aSMiroslav Rezanina char *options, uint64_t img_size, int flags, 4701f382d43aSMiroslav Rezanina Error **errp, bool quiet) 4702f88e1a42SJes Sorensen { 4703f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4704d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4705f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 4706f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 470796df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4708cc84d90fSMax Reitz Error *local_err = NULL; 4709f88e1a42SJes Sorensen int ret = 0; 4710f88e1a42SJes Sorensen 4711f88e1a42SJes Sorensen /* Find driver and parse its options */ 4712f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4713f88e1a42SJes Sorensen if (!drv) { 471471c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 4715d92ada22SLuiz Capitulino return; 4716f88e1a42SJes Sorensen } 4717f88e1a42SJes Sorensen 471898289620SKevin Wolf proto_drv = bdrv_find_protocol(filename, true); 4719f88e1a42SJes Sorensen if (!proto_drv) { 472071c79813SLuiz Capitulino error_setg(errp, "Unknown protocol '%s'", filename); 4721d92ada22SLuiz Capitulino return; 4722f88e1a42SJes Sorensen } 4723f88e1a42SJes Sorensen 4724f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4725f88e1a42SJes Sorensen drv->create_options); 4726f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4727f88e1a42SJes Sorensen proto_drv->create_options); 4728f88e1a42SJes Sorensen 4729f88e1a42SJes Sorensen /* Create parameter list with default values */ 4730f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4731f88e1a42SJes Sorensen 4732f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4733f88e1a42SJes Sorensen 4734f88e1a42SJes Sorensen /* Parse -o options */ 4735f88e1a42SJes Sorensen if (options) { 4736f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4737f88e1a42SJes Sorensen if (param == NULL) { 473871c79813SLuiz Capitulino error_setg(errp, "Invalid options for file format '%s'.", fmt); 4739f88e1a42SJes Sorensen goto out; 4740f88e1a42SJes Sorensen } 4741f88e1a42SJes Sorensen } 4742f88e1a42SJes Sorensen 4743f88e1a42SJes Sorensen if (base_filename) { 4744f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4745f88e1a42SJes Sorensen base_filename)) { 474671c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 474771c79813SLuiz Capitulino fmt); 4748f88e1a42SJes Sorensen goto out; 4749f88e1a42SJes Sorensen } 4750f88e1a42SJes Sorensen } 4751f88e1a42SJes Sorensen 4752f88e1a42SJes Sorensen if (base_fmt) { 4753f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 475471c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 475571c79813SLuiz Capitulino "format '%s'", fmt); 4756f88e1a42SJes Sorensen goto out; 4757f88e1a42SJes Sorensen } 4758f88e1a42SJes Sorensen } 4759f88e1a42SJes Sorensen 4760792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4761792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4762792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 476371c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 476471c79813SLuiz Capitulino "same filename as the backing file"); 4765792da93aSJes Sorensen goto out; 4766792da93aSJes Sorensen } 4767792da93aSJes Sorensen } 4768792da93aSJes Sorensen 4769f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4770f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 477196df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 477296df67d1SStefan Hajnoczi if (!backing_drv) { 477371c79813SLuiz Capitulino error_setg(errp, "Unknown backing file format '%s'", 477471c79813SLuiz Capitulino backing_fmt->value.s); 4775f88e1a42SJes Sorensen goto out; 4776f88e1a42SJes Sorensen } 4777f88e1a42SJes Sorensen } 4778f88e1a42SJes Sorensen 4779f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4780f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4781d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4782d220894eSKevin Wolf if (size && size->value.n == -1) { 4783f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4784f88e1a42SJes Sorensen uint64_t size; 4785f88e1a42SJes Sorensen char buf[32]; 478663090dacSPaolo Bonzini int back_flags; 478763090dacSPaolo Bonzini 478863090dacSPaolo Bonzini /* backing files always opened read-only */ 478963090dacSPaolo Bonzini back_flags = 479063090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 4791f88e1a42SJes Sorensen 4792f88e1a42SJes Sorensen bs = bdrv_new(""); 4793f88e1a42SJes Sorensen 4794de9c0cecSKevin Wolf ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags, 4795cc84d90fSMax Reitz backing_drv, &local_err); 4796f88e1a42SJes Sorensen if (ret < 0) { 4797cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not open '%s': %s", 4798cc84d90fSMax Reitz backing_file->value.s, 4799cc84d90fSMax Reitz error_get_pretty(local_err)); 4800cc84d90fSMax Reitz error_free(local_err); 4801cc84d90fSMax Reitz local_err = NULL; 4802f88e1a42SJes Sorensen goto out; 4803f88e1a42SJes Sorensen } 4804f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4805f88e1a42SJes Sorensen size *= 512; 4806f88e1a42SJes Sorensen 4807f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4808f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4809f88e1a42SJes Sorensen } else { 481071c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 4811f88e1a42SJes Sorensen goto out; 4812f88e1a42SJes Sorensen } 4813f88e1a42SJes Sorensen } 4814f88e1a42SJes Sorensen 4815f382d43aSMiroslav Rezanina if (!quiet) { 4816f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4817f88e1a42SJes Sorensen print_option_parameters(param); 4818f88e1a42SJes Sorensen puts(""); 4819f382d43aSMiroslav Rezanina } 4820cc84d90fSMax Reitz ret = bdrv_create(drv, filename, param, &local_err); 4821cc84d90fSMax Reitz if (ret == -EFBIG) { 4822cc84d90fSMax Reitz /* This is generally a better message than whatever the driver would 4823cc84d90fSMax Reitz * deliver (especially because of the cluster_size_hint), since that 4824cc84d90fSMax Reitz * is most probably not much different from "image too large". */ 4825f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 4826f3f4d2c0SKevin Wolf if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) { 4827f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 4828f3f4d2c0SKevin Wolf } 4829cc84d90fSMax Reitz error_setg(errp, "The image size is too large for file format '%s'" 4830cc84d90fSMax Reitz "%s", fmt, cluster_size_hint); 4831cc84d90fSMax Reitz error_free(local_err); 4832cc84d90fSMax Reitz local_err = NULL; 4833f88e1a42SJes Sorensen } 4834f88e1a42SJes Sorensen 4835f88e1a42SJes Sorensen out: 4836f88e1a42SJes Sorensen free_option_parameters(create_options); 4837f88e1a42SJes Sorensen free_option_parameters(param); 4838f88e1a42SJes Sorensen 4839f88e1a42SJes Sorensen if (bs) { 48404f6fd349SFam Zheng bdrv_unref(bs); 4841f88e1a42SJes Sorensen } 4842cc84d90fSMax Reitz if (error_is_set(&local_err)) { 4843cc84d90fSMax Reitz error_propagate(errp, local_err); 4844cc84d90fSMax Reitz } 4845f88e1a42SJes Sorensen } 484685d126f3SStefan Hajnoczi 484785d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 484885d126f3SStefan Hajnoczi { 484985d126f3SStefan Hajnoczi /* Currently BlockDriverState always uses the main loop AioContext */ 485085d126f3SStefan Hajnoczi return qemu_get_aio_context(); 485185d126f3SStefan Hajnoczi } 4852d616b224SStefan Hajnoczi 4853d616b224SStefan Hajnoczi void bdrv_add_before_write_notifier(BlockDriverState *bs, 4854d616b224SStefan Hajnoczi NotifierWithReturn *notifier) 4855d616b224SStefan Hajnoczi { 4856d616b224SStefan Hajnoczi notifier_with_return_list_add(&bs->before_write_notifiers, notifier); 4857d616b224SStefan Hajnoczi } 48586f176b48SMax Reitz 48596f176b48SMax Reitz int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options) 48606f176b48SMax Reitz { 48616f176b48SMax Reitz if (bs->drv->bdrv_amend_options == NULL) { 48626f176b48SMax Reitz return -ENOTSUP; 48636f176b48SMax Reitz } 48646f176b48SMax Reitz return bs->drv->bdrv_amend_options(bs, options); 48656f176b48SMax Reitz } 4866f6186f49SBenoît Canet 4867f6186f49SBenoît Canet ExtSnapshotPerm bdrv_check_ext_snapshot(BlockDriverState *bs) 4868f6186f49SBenoît Canet { 4869f6186f49SBenoît Canet if (bs->drv->bdrv_check_ext_snapshot) { 4870f6186f49SBenoît Canet return bs->drv->bdrv_check_ext_snapshot(bs); 4871f6186f49SBenoît Canet } 4872f6186f49SBenoît Canet 4873f6186f49SBenoît Canet if (bs->file && bs->file->drv && bs->file->drv->bdrv_check_ext_snapshot) { 4874f6186f49SBenoît Canet return bs->file->drv->bdrv_check_ext_snapshot(bs); 4875f6186f49SBenoît Canet } 4876f6186f49SBenoît Canet 4877f6186f49SBenoît Canet /* external snapshots are allowed by default */ 4878f6186f49SBenoît Canet return EXT_SNAPSHOT_ALLOWED; 4879f6186f49SBenoît Canet } 4880f6186f49SBenoît Canet 4881f6186f49SBenoît Canet ExtSnapshotPerm bdrv_check_ext_snapshot_forbidden(BlockDriverState *bs) 4882f6186f49SBenoît Canet { 4883f6186f49SBenoît Canet return EXT_SNAPSHOT_FORBIDDEN; 4884f6186f49SBenoît Canet } 4885