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, 82b2a61371SStefan Hajnoczi BlockDriverCompletionFunc *cb, 83b2a61371SStefan Hajnoczi void *opaque, 848c5873d6SStefan Hajnoczi bool is_write); 85b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque); 86621f0589SKevin Wolf static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 87aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags); 88ec530c81Sbellard 891b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 901b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 917ee930d0Sblueswir1 928a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 938a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 94ea2384d3Sbellard 95eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 96eb852011SMarkus Armbruster static int use_bdrv_whitelist; 97eb852011SMarkus Armbruster 989e0b22f4SStefan Hajnoczi #ifdef _WIN32 999e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 1009e0b22f4SStefan Hajnoczi { 1019e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 1029e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 1039e0b22f4SStefan Hajnoczi filename[1] == ':'); 1049e0b22f4SStefan Hajnoczi } 1059e0b22f4SStefan Hajnoczi 1069e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 1079e0b22f4SStefan Hajnoczi { 1089e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 1099e0b22f4SStefan Hajnoczi filename[2] == '\0') 1109e0b22f4SStefan Hajnoczi return 1; 1119e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1129e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1139e0b22f4SStefan Hajnoczi return 1; 1149e0b22f4SStefan Hajnoczi return 0; 1159e0b22f4SStefan Hajnoczi } 1169e0b22f4SStefan Hajnoczi #endif 1179e0b22f4SStefan Hajnoczi 1180563e191SZhi Yong Wu /* throttling disk I/O limits */ 119cc0681c4SBenoît Canet void bdrv_set_io_limits(BlockDriverState *bs, 120cc0681c4SBenoît Canet ThrottleConfig *cfg) 121cc0681c4SBenoît Canet { 122cc0681c4SBenoît Canet int i; 123cc0681c4SBenoît Canet 124cc0681c4SBenoît Canet throttle_config(&bs->throttle_state, cfg); 125cc0681c4SBenoît Canet 126cc0681c4SBenoît Canet for (i = 0; i < 2; i++) { 127cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[i]); 128cc0681c4SBenoît Canet } 129cc0681c4SBenoît Canet } 130cc0681c4SBenoît Canet 131cc0681c4SBenoît Canet /* this function drain all the throttled IOs */ 132cc0681c4SBenoît Canet static bool bdrv_start_throttled_reqs(BlockDriverState *bs) 133cc0681c4SBenoît Canet { 134cc0681c4SBenoît Canet bool drained = false; 135cc0681c4SBenoît Canet bool enabled = bs->io_limits_enabled; 136cc0681c4SBenoît Canet int i; 137cc0681c4SBenoît Canet 138cc0681c4SBenoît Canet bs->io_limits_enabled = false; 139cc0681c4SBenoît Canet 140cc0681c4SBenoît Canet for (i = 0; i < 2; i++) { 141cc0681c4SBenoît Canet while (qemu_co_enter_next(&bs->throttled_reqs[i])) { 142cc0681c4SBenoît Canet drained = true; 143cc0681c4SBenoît Canet } 144cc0681c4SBenoît Canet } 145cc0681c4SBenoît Canet 146cc0681c4SBenoît Canet bs->io_limits_enabled = enabled; 147cc0681c4SBenoît Canet 148cc0681c4SBenoît Canet return drained; 149cc0681c4SBenoît Canet } 150cc0681c4SBenoît Canet 15198f90dbaSZhi Yong Wu void bdrv_io_limits_disable(BlockDriverState *bs) 15298f90dbaSZhi Yong Wu { 15398f90dbaSZhi Yong Wu bs->io_limits_enabled = false; 15498f90dbaSZhi Yong Wu 155cc0681c4SBenoît Canet bdrv_start_throttled_reqs(bs); 15698f90dbaSZhi Yong Wu 157cc0681c4SBenoît Canet throttle_destroy(&bs->throttle_state); 15898f90dbaSZhi Yong Wu } 15998f90dbaSZhi Yong Wu 160cc0681c4SBenoît Canet static void bdrv_throttle_read_timer_cb(void *opaque) 1610563e191SZhi Yong Wu { 1620563e191SZhi Yong Wu BlockDriverState *bs = opaque; 163cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[0]); 1640563e191SZhi Yong Wu } 1650563e191SZhi Yong Wu 166cc0681c4SBenoît Canet static void bdrv_throttle_write_timer_cb(void *opaque) 167cc0681c4SBenoît Canet { 168cc0681c4SBenoît Canet BlockDriverState *bs = opaque; 169cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[1]); 170cc0681c4SBenoît Canet } 171cc0681c4SBenoît Canet 172cc0681c4SBenoît Canet /* should be called before bdrv_set_io_limits if a limit is set */ 1730563e191SZhi Yong Wu void bdrv_io_limits_enable(BlockDriverState *bs) 1740563e191SZhi Yong Wu { 175cc0681c4SBenoît Canet assert(!bs->io_limits_enabled); 176cc0681c4SBenoît Canet throttle_init(&bs->throttle_state, 177cc0681c4SBenoît Canet QEMU_CLOCK_VIRTUAL, 178cc0681c4SBenoît Canet bdrv_throttle_read_timer_cb, 179cc0681c4SBenoît Canet bdrv_throttle_write_timer_cb, 180cc0681c4SBenoît Canet bs); 1810563e191SZhi Yong Wu bs->io_limits_enabled = true; 1820563e191SZhi Yong Wu } 1830563e191SZhi Yong Wu 184cc0681c4SBenoît Canet /* This function makes an IO wait if needed 185cc0681c4SBenoît Canet * 186cc0681c4SBenoît Canet * @nb_sectors: the number of sectors of the IO 187cc0681c4SBenoît Canet * @is_write: is the IO a write 18898f90dbaSZhi Yong Wu */ 189cc0681c4SBenoît Canet static void bdrv_io_limits_intercept(BlockDriverState *bs, 190cc0681c4SBenoît Canet int nb_sectors, 191cc0681c4SBenoît Canet bool is_write) 192cc0681c4SBenoît Canet { 193cc0681c4SBenoît Canet /* does this io must wait */ 194cc0681c4SBenoît Canet bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write); 19598f90dbaSZhi Yong Wu 196cc0681c4SBenoît Canet /* if must wait or any request of this type throttled queue the IO */ 197cc0681c4SBenoît Canet if (must_wait || 198cc0681c4SBenoît Canet !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) { 199cc0681c4SBenoît Canet qemu_co_queue_wait(&bs->throttled_reqs[is_write]); 20098f90dbaSZhi Yong Wu } 20198f90dbaSZhi Yong Wu 202cc0681c4SBenoît Canet /* the IO will be executed, do the accounting */ 203cc0681c4SBenoît Canet throttle_account(&bs->throttle_state, 204cc0681c4SBenoît Canet is_write, 205cc0681c4SBenoît Canet nb_sectors * BDRV_SECTOR_SIZE); 206cc0681c4SBenoît Canet 207cc0681c4SBenoît Canet /* if the next request must wait -> do nothing */ 208cc0681c4SBenoît Canet if (throttle_schedule_timer(&bs->throttle_state, is_write)) { 209cc0681c4SBenoît Canet return; 210cc0681c4SBenoît Canet } 211cc0681c4SBenoît Canet 212cc0681c4SBenoît Canet /* else queue next request for execution */ 213cc0681c4SBenoît Canet qemu_co_queue_next(&bs->throttled_reqs[is_write]); 21498f90dbaSZhi Yong Wu } 21598f90dbaSZhi Yong Wu 2169e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 2179e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 2189e0b22f4SStefan Hajnoczi { 219947995c0SPaolo Bonzini const char *p; 220947995c0SPaolo Bonzini 2219e0b22f4SStefan Hajnoczi #ifdef _WIN32 2229e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2239e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2249e0b22f4SStefan Hajnoczi return 0; 2259e0b22f4SStefan Hajnoczi } 226947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 227947995c0SPaolo Bonzini #else 228947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2299e0b22f4SStefan Hajnoczi #endif 2309e0b22f4SStefan Hajnoczi 231947995c0SPaolo Bonzini return *p == ':'; 2329e0b22f4SStefan Hajnoczi } 2339e0b22f4SStefan Hajnoczi 23483f64091Sbellard int path_is_absolute(const char *path) 23583f64091Sbellard { 23621664424Sbellard #ifdef _WIN32 23721664424Sbellard /* specific case for names like: "\\.\d:" */ 238f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 23921664424Sbellard return 1; 240f53f4da9SPaolo Bonzini } 241f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 2423b9f94e1Sbellard #else 243f53f4da9SPaolo Bonzini return (*path == '/'); 2443b9f94e1Sbellard #endif 24583f64091Sbellard } 24683f64091Sbellard 24783f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 24883f64091Sbellard path to it by considering it is relative to base_path. URL are 24983f64091Sbellard supported. */ 25083f64091Sbellard void path_combine(char *dest, int dest_size, 25183f64091Sbellard const char *base_path, 25283f64091Sbellard const char *filename) 25383f64091Sbellard { 25483f64091Sbellard const char *p, *p1; 25583f64091Sbellard int len; 25683f64091Sbellard 25783f64091Sbellard if (dest_size <= 0) 25883f64091Sbellard return; 25983f64091Sbellard if (path_is_absolute(filename)) { 26083f64091Sbellard pstrcpy(dest, dest_size, filename); 26183f64091Sbellard } else { 26283f64091Sbellard p = strchr(base_path, ':'); 26383f64091Sbellard if (p) 26483f64091Sbellard p++; 26583f64091Sbellard else 26683f64091Sbellard p = base_path; 2673b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2683b9f94e1Sbellard #ifdef _WIN32 2693b9f94e1Sbellard { 2703b9f94e1Sbellard const char *p2; 2713b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2723b9f94e1Sbellard if (!p1 || p2 > p1) 2733b9f94e1Sbellard p1 = p2; 2743b9f94e1Sbellard } 2753b9f94e1Sbellard #endif 27683f64091Sbellard if (p1) 27783f64091Sbellard p1++; 27883f64091Sbellard else 27983f64091Sbellard p1 = base_path; 28083f64091Sbellard if (p1 > p) 28183f64091Sbellard p = p1; 28283f64091Sbellard len = p - base_path; 28383f64091Sbellard if (len > dest_size - 1) 28483f64091Sbellard len = dest_size - 1; 28583f64091Sbellard memcpy(dest, base_path, len); 28683f64091Sbellard dest[len] = '\0'; 28783f64091Sbellard pstrcat(dest, dest_size, filename); 28883f64091Sbellard } 28983f64091Sbellard } 29083f64091Sbellard 291dc5a1371SPaolo Bonzini void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) 292dc5a1371SPaolo Bonzini { 293dc5a1371SPaolo Bonzini if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { 294dc5a1371SPaolo Bonzini pstrcpy(dest, sz, bs->backing_file); 295dc5a1371SPaolo Bonzini } else { 296dc5a1371SPaolo Bonzini path_combine(dest, sz, bs->filename, bs->backing_file); 297dc5a1371SPaolo Bonzini } 298dc5a1371SPaolo Bonzini } 299dc5a1371SPaolo Bonzini 3005efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 301ea2384d3Sbellard { 3028c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 3038c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 304f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 305f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 306f9f05dc5SKevin Wolf 307f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 308f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 309f8c35c1dSStefan Hajnoczi */ 310f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 31183f64091Sbellard /* add AIO emulation layer */ 312f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 313f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 31483f64091Sbellard } 315f9f05dc5SKevin Wolf } 316b2e12bc6SChristoph Hellwig 3178a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 318ea2384d3Sbellard } 319b338082bSbellard 320b338082bSbellard /* create a new block device (by default it is empty) */ 321b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 322fc01f7e7Sbellard { 3231b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 324b338082bSbellard 3257267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 326e4654d2dSFam Zheng QLIST_INIT(&bs->dirty_bitmaps); 327b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 328ea2384d3Sbellard if (device_name[0] != '\0') { 3291b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 330ea2384d3Sbellard } 33128a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 332d7d512f6SPaolo Bonzini notifier_list_init(&bs->close_notifiers); 333d616b224SStefan Hajnoczi notifier_with_return_list_init(&bs->before_write_notifiers); 334cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[0]); 335cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[1]); 3369fcb0251SFam Zheng bs->refcnt = 1; 337d7d512f6SPaolo Bonzini 338b338082bSbellard return bs; 339b338082bSbellard } 340b338082bSbellard 341d7d512f6SPaolo Bonzini void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) 342d7d512f6SPaolo Bonzini { 343d7d512f6SPaolo Bonzini notifier_list_add(&bs->close_notifiers, notify); 344d7d512f6SPaolo Bonzini } 345d7d512f6SPaolo Bonzini 346ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 347ea2384d3Sbellard { 348ea2384d3Sbellard BlockDriver *drv1; 3498a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3508a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 351ea2384d3Sbellard return drv1; 352ea2384d3Sbellard } 3538a22f02aSStefan Hajnoczi } 354ea2384d3Sbellard return NULL; 355ea2384d3Sbellard } 356ea2384d3Sbellard 357b64ec4e4SFam Zheng static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) 358eb852011SMarkus Armbruster { 359b64ec4e4SFam Zheng static const char *whitelist_rw[] = { 360b64ec4e4SFam Zheng CONFIG_BDRV_RW_WHITELIST 361b64ec4e4SFam Zheng }; 362b64ec4e4SFam Zheng static const char *whitelist_ro[] = { 363b64ec4e4SFam Zheng CONFIG_BDRV_RO_WHITELIST 364eb852011SMarkus Armbruster }; 365eb852011SMarkus Armbruster const char **p; 366eb852011SMarkus Armbruster 367b64ec4e4SFam Zheng if (!whitelist_rw[0] && !whitelist_ro[0]) { 368eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 369b64ec4e4SFam Zheng } 370eb852011SMarkus Armbruster 371b64ec4e4SFam Zheng for (p = whitelist_rw; *p; p++) { 372eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 373eb852011SMarkus Armbruster return 1; 374eb852011SMarkus Armbruster } 375eb852011SMarkus Armbruster } 376b64ec4e4SFam Zheng if (read_only) { 377b64ec4e4SFam Zheng for (p = whitelist_ro; *p; p++) { 378b64ec4e4SFam Zheng if (!strcmp(drv->format_name, *p)) { 379b64ec4e4SFam Zheng return 1; 380b64ec4e4SFam Zheng } 381b64ec4e4SFam Zheng } 382b64ec4e4SFam Zheng } 383eb852011SMarkus Armbruster return 0; 384eb852011SMarkus Armbruster } 385eb852011SMarkus Armbruster 386b64ec4e4SFam Zheng BlockDriver *bdrv_find_whitelisted_format(const char *format_name, 387b64ec4e4SFam Zheng bool read_only) 388eb852011SMarkus Armbruster { 389eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 390b64ec4e4SFam Zheng return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL; 391eb852011SMarkus Armbruster } 392eb852011SMarkus Armbruster 3935b7e1542SZhi Yong Wu typedef struct CreateCo { 3945b7e1542SZhi Yong Wu BlockDriver *drv; 3955b7e1542SZhi Yong Wu char *filename; 3965b7e1542SZhi Yong Wu QEMUOptionParameter *options; 3975b7e1542SZhi Yong Wu int ret; 398cc84d90fSMax Reitz Error *err; 3995b7e1542SZhi Yong Wu } CreateCo; 4005b7e1542SZhi Yong Wu 4015b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 4025b7e1542SZhi Yong Wu { 403cc84d90fSMax Reitz Error *local_err = NULL; 404cc84d90fSMax Reitz int ret; 405cc84d90fSMax Reitz 4065b7e1542SZhi Yong Wu CreateCo *cco = opaque; 4075b7e1542SZhi Yong Wu assert(cco->drv); 4085b7e1542SZhi Yong Wu 409cc84d90fSMax Reitz ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err); 410cc84d90fSMax Reitz if (error_is_set(&local_err)) { 411cc84d90fSMax Reitz error_propagate(&cco->err, local_err); 412cc84d90fSMax Reitz } 413cc84d90fSMax Reitz cco->ret = ret; 4145b7e1542SZhi Yong Wu } 4155b7e1542SZhi Yong Wu 4160e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 417cc84d90fSMax Reitz QEMUOptionParameter *options, Error **errp) 418ea2384d3Sbellard { 4195b7e1542SZhi Yong Wu int ret; 4200e7e1989SKevin Wolf 4215b7e1542SZhi Yong Wu Coroutine *co; 4225b7e1542SZhi Yong Wu CreateCo cco = { 4235b7e1542SZhi Yong Wu .drv = drv, 4245b7e1542SZhi Yong Wu .filename = g_strdup(filename), 4255b7e1542SZhi Yong Wu .options = options, 4265b7e1542SZhi Yong Wu .ret = NOT_DONE, 427cc84d90fSMax Reitz .err = NULL, 4285b7e1542SZhi Yong Wu }; 4295b7e1542SZhi Yong Wu 4305b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 431cc84d90fSMax Reitz error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); 43280168bffSLuiz Capitulino ret = -ENOTSUP; 43380168bffSLuiz Capitulino goto out; 4345b7e1542SZhi Yong Wu } 4355b7e1542SZhi Yong Wu 4365b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 4375b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 4385b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 4395b7e1542SZhi Yong Wu } else { 4405b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 4415b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 4425b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 4435b7e1542SZhi Yong Wu qemu_aio_wait(); 4445b7e1542SZhi Yong Wu } 4455b7e1542SZhi Yong Wu } 4465b7e1542SZhi Yong Wu 4475b7e1542SZhi Yong Wu ret = cco.ret; 448cc84d90fSMax Reitz if (ret < 0) { 449cc84d90fSMax Reitz if (error_is_set(&cco.err)) { 450cc84d90fSMax Reitz error_propagate(errp, cco.err); 451cc84d90fSMax Reitz } else { 452cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not create image"); 453cc84d90fSMax Reitz } 454cc84d90fSMax Reitz } 4555b7e1542SZhi Yong Wu 45680168bffSLuiz Capitulino out: 45780168bffSLuiz Capitulino g_free(cco.filename); 4585b7e1542SZhi Yong Wu return ret; 459ea2384d3Sbellard } 460ea2384d3Sbellard 461cc84d90fSMax Reitz int bdrv_create_file(const char* filename, QEMUOptionParameter *options, 462cc84d90fSMax Reitz Error **errp) 46384a12e66SChristoph Hellwig { 46484a12e66SChristoph Hellwig BlockDriver *drv; 465cc84d90fSMax Reitz Error *local_err = NULL; 466cc84d90fSMax Reitz int ret; 46784a12e66SChristoph Hellwig 46898289620SKevin Wolf drv = bdrv_find_protocol(filename, true); 46984a12e66SChristoph Hellwig if (drv == NULL) { 470cc84d90fSMax Reitz error_setg(errp, "Could not find protocol for file '%s'", filename); 47116905d71SStefan Hajnoczi return -ENOENT; 47284a12e66SChristoph Hellwig } 47384a12e66SChristoph Hellwig 474cc84d90fSMax Reitz ret = bdrv_create(drv, filename, options, &local_err); 475cc84d90fSMax Reitz if (error_is_set(&local_err)) { 476cc84d90fSMax Reitz error_propagate(errp, local_err); 477cc84d90fSMax Reitz } 478cc84d90fSMax Reitz return ret; 47984a12e66SChristoph Hellwig } 48084a12e66SChristoph Hellwig 481eba25057SJim Meyering /* 482eba25057SJim Meyering * Create a uniquely-named empty temporary file. 483eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 484eba25057SJim Meyering */ 485eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 486eba25057SJim Meyering { 487d5249393Sbellard #ifdef _WIN32 4883b9f94e1Sbellard char temp_dir[MAX_PATH]; 489eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 490eba25057SJim Meyering have length MAX_PATH or greater. */ 491eba25057SJim Meyering assert(size >= MAX_PATH); 492eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 493eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 494eba25057SJim Meyering ? 0 : -GetLastError()); 495d5249393Sbellard #else 496ea2384d3Sbellard int fd; 4977ccfb2ebSblueswir1 const char *tmpdir; 4980badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 4990badc1eeSaurel32 if (!tmpdir) 5000badc1eeSaurel32 tmpdir = "/tmp"; 501eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 502eba25057SJim Meyering return -EOVERFLOW; 503ea2384d3Sbellard } 504eba25057SJim Meyering fd = mkstemp(filename); 505fe235a06SDunrong Huang if (fd < 0) { 506fe235a06SDunrong Huang return -errno; 507fe235a06SDunrong Huang } 508fe235a06SDunrong Huang if (close(fd) != 0) { 509fe235a06SDunrong Huang unlink(filename); 510eba25057SJim Meyering return -errno; 511eba25057SJim Meyering } 512eba25057SJim Meyering return 0; 513d5249393Sbellard #endif 514eba25057SJim Meyering } 515ea2384d3Sbellard 516f3a5d3f8SChristoph Hellwig /* 517f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 518f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 519f3a5d3f8SChristoph Hellwig */ 520f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 521f3a5d3f8SChristoph Hellwig { 522508c7cb3SChristoph Hellwig int score_max = 0, score; 523508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 524f3a5d3f8SChristoph Hellwig 5258a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 526508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 527508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 528508c7cb3SChristoph Hellwig if (score > score_max) { 529508c7cb3SChristoph Hellwig score_max = score; 530508c7cb3SChristoph Hellwig drv = d; 531f3a5d3f8SChristoph Hellwig } 532508c7cb3SChristoph Hellwig } 533f3a5d3f8SChristoph Hellwig } 534f3a5d3f8SChristoph Hellwig 535508c7cb3SChristoph Hellwig return drv; 536f3a5d3f8SChristoph Hellwig } 537f3a5d3f8SChristoph Hellwig 53898289620SKevin Wolf BlockDriver *bdrv_find_protocol(const char *filename, 53998289620SKevin Wolf bool allow_protocol_prefix) 54084a12e66SChristoph Hellwig { 54184a12e66SChristoph Hellwig BlockDriver *drv1; 54284a12e66SChristoph Hellwig char protocol[128]; 54384a12e66SChristoph Hellwig int len; 54484a12e66SChristoph Hellwig const char *p; 54584a12e66SChristoph Hellwig 54666f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 54766f82ceeSKevin Wolf 54839508e7aSChristoph Hellwig /* 54939508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 55039508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 55139508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 55239508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 55339508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 55439508e7aSChristoph Hellwig */ 55584a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 55639508e7aSChristoph Hellwig if (drv1) { 55784a12e66SChristoph Hellwig return drv1; 55884a12e66SChristoph Hellwig } 55939508e7aSChristoph Hellwig 56098289620SKevin Wolf if (!path_has_protocol(filename) || !allow_protocol_prefix) { 56139508e7aSChristoph Hellwig return bdrv_find_format("file"); 56239508e7aSChristoph Hellwig } 56398289620SKevin Wolf 5649e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 5659e0b22f4SStefan Hajnoczi assert(p != NULL); 56684a12e66SChristoph Hellwig len = p - filename; 56784a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 56884a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 56984a12e66SChristoph Hellwig memcpy(protocol, filename, len); 57084a12e66SChristoph Hellwig protocol[len] = '\0'; 57184a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 57284a12e66SChristoph Hellwig if (drv1->protocol_name && 57384a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 57484a12e66SChristoph Hellwig return drv1; 57584a12e66SChristoph Hellwig } 57684a12e66SChristoph Hellwig } 57784a12e66SChristoph Hellwig return NULL; 57884a12e66SChristoph Hellwig } 57984a12e66SChristoph Hellwig 580f500a6d3SKevin Wolf static int find_image_format(BlockDriverState *bs, const char *filename, 58134b5d2c6SMax Reitz BlockDriver **pdrv, Error **errp) 582ea2384d3Sbellard { 583f500a6d3SKevin Wolf int score, score_max; 584ea2384d3Sbellard BlockDriver *drv1, *drv; 58583f64091Sbellard uint8_t buf[2048]; 586f500a6d3SKevin Wolf int ret = 0; 587f8ea0b00SNicholas Bellinger 58808a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 5898e895599SPaolo Bonzini if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { 590c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 591c98ac35dSStefan Weil if (!drv) { 59234b5d2c6SMax Reitz error_setg(errp, "Could not find raw image format"); 593c98ac35dSStefan Weil ret = -ENOENT; 594c98ac35dSStefan Weil } 595c98ac35dSStefan Weil *pdrv = drv; 596c98ac35dSStefan Weil return ret; 5971a396859SNicholas A. Bellinger } 598f8ea0b00SNicholas Bellinger 59983f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 600ea2384d3Sbellard if (ret < 0) { 60134b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not read image for determining its " 60234b5d2c6SMax Reitz "format"); 603c98ac35dSStefan Weil *pdrv = NULL; 604c98ac35dSStefan Weil return ret; 605ea2384d3Sbellard } 606ea2384d3Sbellard 607ea2384d3Sbellard score_max = 0; 60884a12e66SChristoph Hellwig drv = NULL; 6098a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 61083f64091Sbellard if (drv1->bdrv_probe) { 611ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 612ea2384d3Sbellard if (score > score_max) { 613ea2384d3Sbellard score_max = score; 614ea2384d3Sbellard drv = drv1; 615ea2384d3Sbellard } 616ea2384d3Sbellard } 61783f64091Sbellard } 618c98ac35dSStefan Weil if (!drv) { 61934b5d2c6SMax Reitz error_setg(errp, "Could not determine image format: No compatible " 62034b5d2c6SMax Reitz "driver found"); 621c98ac35dSStefan Weil ret = -ENOENT; 622c98ac35dSStefan Weil } 623c98ac35dSStefan Weil *pdrv = drv; 624c98ac35dSStefan Weil return ret; 625ea2384d3Sbellard } 626ea2384d3Sbellard 62751762288SStefan Hajnoczi /** 62851762288SStefan Hajnoczi * Set the current 'total_sectors' value 62951762288SStefan Hajnoczi */ 63051762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 63151762288SStefan Hajnoczi { 63251762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 63351762288SStefan Hajnoczi 634396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 635396759adSNicholas Bellinger if (bs->sg) 636396759adSNicholas Bellinger return 0; 637396759adSNicholas Bellinger 63851762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 63951762288SStefan Hajnoczi if (drv->bdrv_getlength) { 64051762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 64151762288SStefan Hajnoczi if (length < 0) { 64251762288SStefan Hajnoczi return length; 64351762288SStefan Hajnoczi } 6447e382003SFam Zheng hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); 64551762288SStefan Hajnoczi } 64651762288SStefan Hajnoczi 64751762288SStefan Hajnoczi bs->total_sectors = hint; 64851762288SStefan Hajnoczi return 0; 64951762288SStefan Hajnoczi } 65051762288SStefan Hajnoczi 651c3993cdcSStefan Hajnoczi /** 6529e8f1835SPaolo Bonzini * Set open flags for a given discard mode 6539e8f1835SPaolo Bonzini * 6549e8f1835SPaolo Bonzini * Return 0 on success, -1 if the discard mode was invalid. 6559e8f1835SPaolo Bonzini */ 6569e8f1835SPaolo Bonzini int bdrv_parse_discard_flags(const char *mode, int *flags) 6579e8f1835SPaolo Bonzini { 6589e8f1835SPaolo Bonzini *flags &= ~BDRV_O_UNMAP; 6599e8f1835SPaolo Bonzini 6609e8f1835SPaolo Bonzini if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { 6619e8f1835SPaolo Bonzini /* do nothing */ 6629e8f1835SPaolo Bonzini } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { 6639e8f1835SPaolo Bonzini *flags |= BDRV_O_UNMAP; 6649e8f1835SPaolo Bonzini } else { 6659e8f1835SPaolo Bonzini return -1; 6669e8f1835SPaolo Bonzini } 6679e8f1835SPaolo Bonzini 6689e8f1835SPaolo Bonzini return 0; 6699e8f1835SPaolo Bonzini } 6709e8f1835SPaolo Bonzini 6719e8f1835SPaolo Bonzini /** 672c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 673c3993cdcSStefan Hajnoczi * 674c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 675c3993cdcSStefan Hajnoczi */ 676c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 677c3993cdcSStefan Hajnoczi { 678c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 679c3993cdcSStefan Hajnoczi 680c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 681c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 68292196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 68392196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 684c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 685c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 686c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 687c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 688c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 689c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 690c3993cdcSStefan Hajnoczi /* this is the default */ 691c3993cdcSStefan Hajnoczi } else { 692c3993cdcSStefan Hajnoczi return -1; 693c3993cdcSStefan Hajnoczi } 694c3993cdcSStefan Hajnoczi 695c3993cdcSStefan Hajnoczi return 0; 696c3993cdcSStefan Hajnoczi } 697c3993cdcSStefan Hajnoczi 69853fec9d3SStefan Hajnoczi /** 69953fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 70053fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 70153fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 70253fec9d3SStefan Hajnoczi */ 70353fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 70453fec9d3SStefan Hajnoczi { 70553fec9d3SStefan Hajnoczi bs->copy_on_read++; 70653fec9d3SStefan Hajnoczi } 70753fec9d3SStefan Hajnoczi 70853fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 70953fec9d3SStefan Hajnoczi { 71053fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 71153fec9d3SStefan Hajnoczi bs->copy_on_read--; 71253fec9d3SStefan Hajnoczi } 71353fec9d3SStefan Hajnoczi 7147b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 7157b272452SKevin Wolf { 7167b272452SKevin Wolf int open_flags = flags | BDRV_O_CACHE_WB; 7177b272452SKevin Wolf 7187b272452SKevin Wolf /* 7197b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 7207b272452SKevin Wolf * image. 7217b272452SKevin Wolf */ 7227b272452SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 7237b272452SKevin Wolf 7247b272452SKevin Wolf /* 7257b272452SKevin Wolf * Snapshots should be writable. 7267b272452SKevin Wolf */ 7277b272452SKevin Wolf if (bs->is_temporary) { 7287b272452SKevin Wolf open_flags |= BDRV_O_RDWR; 7297b272452SKevin Wolf } 7307b272452SKevin Wolf 7317b272452SKevin Wolf return open_flags; 7327b272452SKevin Wolf } 7337b272452SKevin Wolf 734b6ce07aaSKevin Wolf /* 73557915332SKevin Wolf * Common part for opening disk images and files 736b6ad491aSKevin Wolf * 737b6ad491aSKevin Wolf * Removes all processed options from *options. 73857915332SKevin Wolf */ 739f500a6d3SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, 74034b5d2c6SMax Reitz QDict *options, int flags, BlockDriver *drv, Error **errp) 74157915332SKevin Wolf { 74257915332SKevin Wolf int ret, open_flags; 743035fccdfSKevin Wolf const char *filename; 74434b5d2c6SMax Reitz Error *local_err = NULL; 74557915332SKevin Wolf 74657915332SKevin Wolf assert(drv != NULL); 7476405875cSPaolo Bonzini assert(bs->file == NULL); 748707ff828SKevin Wolf assert(options != NULL && bs->options != options); 74957915332SKevin Wolf 75045673671SKevin Wolf if (file != NULL) { 75145673671SKevin Wolf filename = file->filename; 75245673671SKevin Wolf } else { 75345673671SKevin Wolf filename = qdict_get_try_str(options, "filename"); 75445673671SKevin Wolf } 75545673671SKevin Wolf 75645673671SKevin Wolf trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name); 75728dcee10SStefan Hajnoczi 7585d186eb0SKevin Wolf /* bdrv_open() with directly using a protocol as drv. This layer is already 7595d186eb0SKevin Wolf * opened, so assign it to bs (while file becomes a closed BlockDriverState) 7605d186eb0SKevin Wolf * and return immediately. */ 7615d186eb0SKevin Wolf if (file != NULL && drv->bdrv_file_open) { 7625d186eb0SKevin Wolf bdrv_swap(file, bs); 7635d186eb0SKevin Wolf return 0; 7645d186eb0SKevin Wolf } 7655d186eb0SKevin Wolf 76657915332SKevin Wolf bs->open_flags = flags; 76757915332SKevin Wolf bs->buffer_alignment = 512; 7680d51b4deSAsias He bs->zero_beyond_eof = true; 769b64ec4e4SFam Zheng open_flags = bdrv_open_flags(bs, flags); 770b64ec4e4SFam Zheng bs->read_only = !(open_flags & BDRV_O_RDWR); 771b64ec4e4SFam Zheng 772b64ec4e4SFam Zheng if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { 7738f94a6e4SKevin Wolf error_setg(errp, 7748f94a6e4SKevin Wolf !bs->read_only && bdrv_is_whitelisted(drv, true) 7758f94a6e4SKevin Wolf ? "Driver '%s' can only be used for read-only devices" 7768f94a6e4SKevin Wolf : "Driver '%s' is not whitelisted", 7778f94a6e4SKevin Wolf drv->format_name); 778b64ec4e4SFam Zheng return -ENOTSUP; 779b64ec4e4SFam Zheng } 78057915332SKevin Wolf 78153fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 7820ebd24e0SKevin Wolf if (flags & BDRV_O_COPY_ON_READ) { 7830ebd24e0SKevin Wolf if (!bs->read_only) { 78453fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 7850ebd24e0SKevin Wolf } else { 7860ebd24e0SKevin Wolf error_setg(errp, "Can't use copy-on-read on read-only device"); 7870ebd24e0SKevin Wolf return -EINVAL; 7880ebd24e0SKevin Wolf } 78953fec9d3SStefan Hajnoczi } 79053fec9d3SStefan Hajnoczi 791c2ad1b0cSKevin Wolf if (filename != NULL) { 79257915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 793c2ad1b0cSKevin Wolf } else { 794c2ad1b0cSKevin Wolf bs->filename[0] = '\0'; 795c2ad1b0cSKevin Wolf } 79657915332SKevin Wolf 79757915332SKevin Wolf bs->drv = drv; 7987267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 79957915332SKevin Wolf 80003f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 801e7c63796SStefan Hajnoczi 80266f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 80366f82ceeSKevin Wolf if (drv->bdrv_file_open) { 8045d186eb0SKevin Wolf assert(file == NULL); 805030be321SBenoît Canet assert(!drv->bdrv_needs_filename || filename != NULL); 80634b5d2c6SMax Reitz ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); 807f500a6d3SKevin Wolf } else { 8082af5ef70SKevin Wolf if (file == NULL) { 80934b5d2c6SMax Reitz error_setg(errp, "Can't use '%s' as a block driver for the " 81034b5d2c6SMax Reitz "protocol level", drv->format_name); 8112af5ef70SKevin Wolf ret = -EINVAL; 8122af5ef70SKevin Wolf goto free_and_fail; 8132af5ef70SKevin Wolf } 814f500a6d3SKevin Wolf bs->file = file; 81534b5d2c6SMax Reitz ret = drv->bdrv_open(bs, options, open_flags, &local_err); 81666f82ceeSKevin Wolf } 81766f82ceeSKevin Wolf 81857915332SKevin Wolf if (ret < 0) { 81934b5d2c6SMax Reitz if (error_is_set(&local_err)) { 82034b5d2c6SMax Reitz error_propagate(errp, local_err); 8212fa9aa59SDunrong Huang } else if (bs->filename[0]) { 8222fa9aa59SDunrong Huang error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); 82334b5d2c6SMax Reitz } else { 82434b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not open image"); 82534b5d2c6SMax Reitz } 82657915332SKevin Wolf goto free_and_fail; 82757915332SKevin Wolf } 82857915332SKevin Wolf 82951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 83051762288SStefan Hajnoczi if (ret < 0) { 83134b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not refresh total sector count"); 83251762288SStefan Hajnoczi goto free_and_fail; 83357915332SKevin Wolf } 83451762288SStefan Hajnoczi 83557915332SKevin Wolf #ifndef _WIN32 83657915332SKevin Wolf if (bs->is_temporary) { 837d4cea8dfSDunrong Huang assert(bs->filename[0] != '\0'); 838d4cea8dfSDunrong Huang unlink(bs->filename); 83957915332SKevin Wolf } 84057915332SKevin Wolf #endif 84157915332SKevin Wolf return 0; 84257915332SKevin Wolf 84357915332SKevin Wolf free_and_fail: 84466f82ceeSKevin Wolf bs->file = NULL; 8457267c094SAnthony Liguori g_free(bs->opaque); 84657915332SKevin Wolf bs->opaque = NULL; 84757915332SKevin Wolf bs->drv = NULL; 84857915332SKevin Wolf return ret; 84957915332SKevin Wolf } 85057915332SKevin Wolf 85157915332SKevin Wolf /* 852b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 853787e4a85SKevin Wolf * 854787e4a85SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 855787e4a85SKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 856787e4a85SKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 857787e4a85SKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_file_open. 858b6ce07aaSKevin Wolf */ 859787e4a85SKevin Wolf int bdrv_file_open(BlockDriverState **pbs, const char *filename, 86034b5d2c6SMax Reitz QDict *options, int flags, Error **errp) 861b338082bSbellard { 86283f64091Sbellard BlockDriverState *bs; 8636db95603SChristoph Hellwig BlockDriver *drv; 864c2ad1b0cSKevin Wolf const char *drvname; 86598289620SKevin Wolf bool allow_protocol_prefix = false; 86634b5d2c6SMax Reitz Error *local_err = NULL; 86783f64091Sbellard int ret; 8683b0d4f61Sbellard 869707ff828SKevin Wolf /* NULL means an empty set of options */ 870707ff828SKevin Wolf if (options == NULL) { 871707ff828SKevin Wolf options = qdict_new(); 8723b0d4f61Sbellard } 873707ff828SKevin Wolf 874707ff828SKevin Wolf bs = bdrv_new(""); 875707ff828SKevin Wolf bs->options = options; 876707ff828SKevin Wolf options = qdict_clone_shallow(options); 877707ff828SKevin Wolf 878035fccdfSKevin Wolf /* Fetch the file name from the options QDict if necessary */ 879035fccdfSKevin Wolf if (!filename) { 880035fccdfSKevin Wolf filename = qdict_get_try_str(options, "filename"); 881035fccdfSKevin Wolf } else if (filename && !qdict_haskey(options, "filename")) { 882035fccdfSKevin Wolf qdict_put(options, "filename", qstring_from_str(filename)); 88398289620SKevin Wolf allow_protocol_prefix = true; 884035fccdfSKevin Wolf } else { 88534b5d2c6SMax Reitz error_setg(errp, "Can't specify 'file' and 'filename' options at the " 88634b5d2c6SMax Reitz "same time"); 887035fccdfSKevin Wolf ret = -EINVAL; 888035fccdfSKevin Wolf goto fail; 889035fccdfSKevin Wolf } 890035fccdfSKevin Wolf 891c2ad1b0cSKevin Wolf /* Find the right block driver */ 892c2ad1b0cSKevin Wolf drvname = qdict_get_try_str(options, "driver"); 893c2ad1b0cSKevin Wolf if (drvname) { 8948f94a6e4SKevin Wolf drv = bdrv_find_format(drvname); 89534b5d2c6SMax Reitz if (!drv) { 89634b5d2c6SMax Reitz error_setg(errp, "Unknown driver '%s'", drvname); 89734b5d2c6SMax Reitz } 898c2ad1b0cSKevin Wolf qdict_del(options, "driver"); 899c2ad1b0cSKevin Wolf } else if (filename) { 90098289620SKevin Wolf drv = bdrv_find_protocol(filename, allow_protocol_prefix); 90198289620SKevin Wolf if (!drv) { 90234b5d2c6SMax Reitz error_setg(errp, "Unknown protocol"); 90398289620SKevin Wolf } 904c2ad1b0cSKevin Wolf } else { 90534b5d2c6SMax Reitz error_setg(errp, "Must specify either driver or file"); 906c2ad1b0cSKevin Wolf drv = NULL; 907c2ad1b0cSKevin Wolf } 908c2ad1b0cSKevin Wolf 909c2ad1b0cSKevin Wolf if (!drv) { 91034b5d2c6SMax Reitz /* errp has been set already */ 911c2ad1b0cSKevin Wolf ret = -ENOENT; 912c2ad1b0cSKevin Wolf goto fail; 913c2ad1b0cSKevin Wolf } 914c2ad1b0cSKevin Wolf 915c2ad1b0cSKevin Wolf /* Parse the filename and open it */ 916c2ad1b0cSKevin Wolf if (drv->bdrv_parse_filename && filename) { 9176963a30dSKevin Wolf drv->bdrv_parse_filename(filename, options, &local_err); 9186963a30dSKevin Wolf if (error_is_set(&local_err)) { 91934b5d2c6SMax Reitz error_propagate(errp, local_err); 9206963a30dSKevin Wolf ret = -EINVAL; 9216963a30dSKevin Wolf goto fail; 9226963a30dSKevin Wolf } 92356d1b4d2SKevin Wolf qdict_del(options, "filename"); 924030be321SBenoît Canet } else if (drv->bdrv_needs_filename && !filename) { 92534b5d2c6SMax Reitz error_setg(errp, "The '%s' block driver requires a file name", 926c2ad1b0cSKevin Wolf drv->format_name); 927c2ad1b0cSKevin Wolf ret = -EINVAL; 928c2ad1b0cSKevin Wolf goto fail; 9296963a30dSKevin Wolf } 9306963a30dSKevin Wolf 93134b5d2c6SMax Reitz ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err); 932707ff828SKevin Wolf if (ret < 0) { 93334b5d2c6SMax Reitz error_propagate(errp, local_err); 934707ff828SKevin Wolf goto fail; 935707ff828SKevin Wolf } 936707ff828SKevin Wolf 937707ff828SKevin Wolf /* Check if any unknown options were used */ 938707ff828SKevin Wolf if (qdict_size(options) != 0) { 939707ff828SKevin Wolf const QDictEntry *entry = qdict_first(options); 94034b5d2c6SMax Reitz error_setg(errp, "Block protocol '%s' doesn't support the option '%s'", 941707ff828SKevin Wolf drv->format_name, entry->key); 942707ff828SKevin Wolf ret = -EINVAL; 943707ff828SKevin Wolf goto fail; 944707ff828SKevin Wolf } 945707ff828SKevin Wolf QDECREF(options); 946707ff828SKevin Wolf 94771d0770cSaliguori bs->growable = 1; 94883f64091Sbellard *pbs = bs; 94983f64091Sbellard return 0; 950707ff828SKevin Wolf 951707ff828SKevin Wolf fail: 952707ff828SKevin Wolf QDECREF(options); 953707ff828SKevin Wolf if (!bs->drv) { 954707ff828SKevin Wolf QDECREF(bs->options); 955707ff828SKevin Wolf } 9564f6fd349SFam Zheng bdrv_unref(bs); 957707ff828SKevin Wolf return ret; 9583b0d4f61Sbellard } 9593b0d4f61Sbellard 96031ca6d07SKevin Wolf /* 96131ca6d07SKevin Wolf * Opens the backing file for a BlockDriverState if not yet open 96231ca6d07SKevin Wolf * 96331ca6d07SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 96431ca6d07SKevin Wolf * empty set of options. The reference to the QDict is transferred to this 96531ca6d07SKevin Wolf * function (even on failure), so if the caller intends to reuse the dictionary, 96631ca6d07SKevin Wolf * it needs to use QINCREF() before calling bdrv_file_open. 96731ca6d07SKevin Wolf */ 96834b5d2c6SMax Reitz int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) 9699156df12SPaolo Bonzini { 9709156df12SPaolo Bonzini char backing_filename[PATH_MAX]; 9719156df12SPaolo Bonzini int back_flags, ret; 9729156df12SPaolo Bonzini BlockDriver *back_drv = NULL; 97334b5d2c6SMax Reitz Error *local_err = NULL; 9749156df12SPaolo Bonzini 9759156df12SPaolo Bonzini if (bs->backing_hd != NULL) { 97631ca6d07SKevin Wolf QDECREF(options); 9779156df12SPaolo Bonzini return 0; 9789156df12SPaolo Bonzini } 9799156df12SPaolo Bonzini 98031ca6d07SKevin Wolf /* NULL means an empty set of options */ 98131ca6d07SKevin Wolf if (options == NULL) { 98231ca6d07SKevin Wolf options = qdict_new(); 98331ca6d07SKevin Wolf } 98431ca6d07SKevin Wolf 9859156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 9861cb6f506SKevin Wolf if (qdict_haskey(options, "file.filename")) { 9871cb6f506SKevin Wolf backing_filename[0] = '\0'; 9881cb6f506SKevin Wolf } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { 98931ca6d07SKevin Wolf QDECREF(options); 9909156df12SPaolo Bonzini return 0; 991dbecebddSFam Zheng } else { 992dbecebddSFam Zheng bdrv_get_full_backing_filename(bs, backing_filename, 993dbecebddSFam Zheng sizeof(backing_filename)); 9949156df12SPaolo Bonzini } 9959156df12SPaolo Bonzini 9969156df12SPaolo Bonzini bs->backing_hd = bdrv_new(""); 9979156df12SPaolo Bonzini 9989156df12SPaolo Bonzini if (bs->backing_format[0] != '\0') { 9999156df12SPaolo Bonzini back_drv = bdrv_find_format(bs->backing_format); 10009156df12SPaolo Bonzini } 10019156df12SPaolo Bonzini 10029156df12SPaolo Bonzini /* backing files always opened read-only */ 100387a5debdSThibaut LAURENT back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | 100487a5debdSThibaut LAURENT BDRV_O_COPY_ON_READ); 10059156df12SPaolo Bonzini 100631ca6d07SKevin Wolf ret = bdrv_open(bs->backing_hd, 100731ca6d07SKevin Wolf *backing_filename ? backing_filename : NULL, options, 100834b5d2c6SMax Reitz back_flags, back_drv, &local_err); 10099156df12SPaolo Bonzini if (ret < 0) { 10104f6fd349SFam Zheng bdrv_unref(bs->backing_hd); 10119156df12SPaolo Bonzini bs->backing_hd = NULL; 10129156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 1013b04b6b6eSFam Zheng error_setg(errp, "Could not open backing file: %s", 1014b04b6b6eSFam Zheng error_get_pretty(local_err)); 1015b04b6b6eSFam Zheng error_free(local_err); 10169156df12SPaolo Bonzini return ret; 10179156df12SPaolo Bonzini } 101861ed2684SMax Reitz pstrcpy(bs->backing_file, sizeof(bs->backing_file), 101961ed2684SMax Reitz bs->backing_hd->file->filename); 10209156df12SPaolo Bonzini return 0; 10219156df12SPaolo Bonzini } 10229156df12SPaolo Bonzini 1023b6ce07aaSKevin Wolf /* 1024b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 1025de9c0cecSKevin Wolf * 1026de9c0cecSKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 1027de9c0cecSKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 1028de9c0cecSKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 1029de9c0cecSKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_open. 1030b6ce07aaSKevin Wolf */ 1031de9c0cecSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, 103234b5d2c6SMax Reitz int flags, BlockDriver *drv, Error **errp) 1033ea2384d3Sbellard { 1034b6ce07aaSKevin Wolf int ret; 103589c9bc3dSStefan Weil /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 103689c9bc3dSStefan Weil char tmp_filename[PATH_MAX + 1]; 1037f500a6d3SKevin Wolf BlockDriverState *file = NULL; 1038707ff828SKevin Wolf QDict *file_options = NULL; 103974fe54f2SKevin Wolf const char *drvname; 104034b5d2c6SMax Reitz Error *local_err = NULL; 104133e3963eSbellard 1042de9c0cecSKevin Wolf /* NULL means an empty set of options */ 1043de9c0cecSKevin Wolf if (options == NULL) { 1044de9c0cecSKevin Wolf options = qdict_new(); 1045de9c0cecSKevin Wolf } 1046de9c0cecSKevin Wolf 1047de9c0cecSKevin Wolf bs->options = options; 1048b6ad491aSKevin Wolf options = qdict_clone_shallow(options); 1049de9c0cecSKevin Wolf 1050de9c0cecSKevin Wolf /* For snapshot=on, create a temporary qcow2 overlay */ 105183f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 1052ea2384d3Sbellard BlockDriverState *bs1; 1053ea2384d3Sbellard int64_t total_size; 105491a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 105508b392e1SKevin Wolf QEMUOptionParameter *create_options; 1056b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 105733e3963eSbellard 1058c2ad1b0cSKevin Wolf if (qdict_size(options) != 0) { 105934b5d2c6SMax Reitz error_setg(errp, "Can't use snapshot=on with driver-specific options"); 1060c2ad1b0cSKevin Wolf ret = -EINVAL; 1061c2ad1b0cSKevin Wolf goto fail; 1062c2ad1b0cSKevin Wolf } 1063c2ad1b0cSKevin Wolf assert(filename != NULL); 1064c2ad1b0cSKevin Wolf 1065ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 1066ea2384d3Sbellard instead of opening 'filename' directly */ 1067ea2384d3Sbellard 1068ea2384d3Sbellard /* if there is a backing file, use it */ 1069ea2384d3Sbellard bs1 = bdrv_new(""); 107034b5d2c6SMax Reitz ret = bdrv_open(bs1, filename, NULL, 0, drv, &local_err); 107151d7c00cSaliguori if (ret < 0) { 10724f6fd349SFam Zheng bdrv_unref(bs1); 1073de9c0cecSKevin Wolf goto fail; 1074ea2384d3Sbellard } 10753e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 10767c96d46eSaliguori 10774f6fd349SFam Zheng bdrv_unref(bs1); 1078ea2384d3Sbellard 1079eba25057SJim Meyering ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 1080eba25057SJim Meyering if (ret < 0) { 108134b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not get temporary filename"); 1082de9c0cecSKevin Wolf goto fail; 1083eba25057SJim Meyering } 10847c96d46eSaliguori 10857c96d46eSaliguori /* Real path is meaningless for protocols */ 10864d70655bSStefan Hajnoczi if (path_has_protocol(filename)) { 10877c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 10887c96d46eSaliguori "%s", filename); 1089de9c0cecSKevin Wolf } else if (!realpath(filename, backing_filename)) { 1090de9c0cecSKevin Wolf ret = -errno; 109117826bc1SMax Reitz error_setg_errno(errp, errno, "Could not resolve path '%s'", filename); 1092de9c0cecSKevin Wolf goto fail; 1093de9c0cecSKevin Wolf } 10947c96d46eSaliguori 109591a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 109608b392e1SKevin Wolf create_options = parse_option_parameters("", bdrv_qcow2->create_options, 109708b392e1SKevin Wolf NULL); 109891a073a9SKevin Wolf 109908b392e1SKevin Wolf set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); 110008b392e1SKevin Wolf set_option_parameter(create_options, BLOCK_OPT_BACKING_FILE, 110108b392e1SKevin Wolf backing_filename); 110291a073a9SKevin Wolf if (drv) { 110308b392e1SKevin Wolf set_option_parameter(create_options, BLOCK_OPT_BACKING_FMT, 110491a073a9SKevin Wolf drv->format_name); 110591a073a9SKevin Wolf } 110691a073a9SKevin Wolf 1107cc84d90fSMax Reitz ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err); 110808b392e1SKevin Wolf free_option_parameters(create_options); 110951d7c00cSaliguori if (ret < 0) { 111034b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not create temporary overlay " 1111cc84d90fSMax Reitz "'%s': %s", tmp_filename, 1112cc84d90fSMax Reitz error_get_pretty(local_err)); 1113cc84d90fSMax Reitz error_free(local_err); 1114cc84d90fSMax Reitz local_err = NULL; 1115de9c0cecSKevin Wolf goto fail; 1116ea2384d3Sbellard } 111791a073a9SKevin Wolf 1118ea2384d3Sbellard filename = tmp_filename; 111991a073a9SKevin Wolf drv = bdrv_qcow2; 1120ea2384d3Sbellard bs->is_temporary = 1; 1121ea2384d3Sbellard } 1122ea2384d3Sbellard 1123f500a6d3SKevin Wolf /* Open image file without format layer */ 1124be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 1125be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 1126be028adcSJeff Cody } 1127be028adcSJeff Cody 11285726d872SBenoît Canet qdict_extract_subqdict(options, &file_options, "file."); 1129707ff828SKevin Wolf 1130707ff828SKevin Wolf ret = bdrv_file_open(&file, filename, file_options, 113134b5d2c6SMax Reitz bdrv_open_flags(bs, flags | BDRV_O_UNMAP), &local_err); 1132f500a6d3SKevin Wolf if (ret < 0) { 1133de9c0cecSKevin Wolf goto fail; 1134f500a6d3SKevin Wolf } 1135f500a6d3SKevin Wolf 1136f500a6d3SKevin Wolf /* Find the right image format driver */ 113774fe54f2SKevin Wolf drvname = qdict_get_try_str(options, "driver"); 113874fe54f2SKevin Wolf if (drvname) { 11398f94a6e4SKevin Wolf drv = bdrv_find_format(drvname); 114074fe54f2SKevin Wolf qdict_del(options, "driver"); 114106d22aa3SKevin Wolf if (!drv) { 114206d22aa3SKevin Wolf error_setg(errp, "Invalid driver: '%s'", drvname); 114306d22aa3SKevin Wolf ret = -EINVAL; 114406d22aa3SKevin Wolf goto unlink_and_fail; 114506d22aa3SKevin Wolf } 114674fe54f2SKevin Wolf } 114774fe54f2SKevin Wolf 1148f500a6d3SKevin Wolf if (!drv) { 114934b5d2c6SMax Reitz ret = find_image_format(file, filename, &drv, &local_err); 1150f500a6d3SKevin Wolf } 1151f500a6d3SKevin Wolf 1152f500a6d3SKevin Wolf if (!drv) { 1153f500a6d3SKevin Wolf goto unlink_and_fail; 1154f500a6d3SKevin Wolf } 1155f500a6d3SKevin Wolf 1156b6ce07aaSKevin Wolf /* Open the image */ 115734b5d2c6SMax Reitz ret = bdrv_open_common(bs, file, options, flags, drv, &local_err); 1158b6ce07aaSKevin Wolf if (ret < 0) { 11596987307cSChristoph Hellwig goto unlink_and_fail; 11606987307cSChristoph Hellwig } 11616987307cSChristoph Hellwig 1162f500a6d3SKevin Wolf if (bs->file != file) { 11634f6fd349SFam Zheng bdrv_unref(file); 1164f500a6d3SKevin Wolf file = NULL; 1165f500a6d3SKevin Wolf } 1166f500a6d3SKevin Wolf 1167b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 11689156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 116931ca6d07SKevin Wolf QDict *backing_options; 117031ca6d07SKevin Wolf 11715726d872SBenoît Canet qdict_extract_subqdict(options, &backing_options, "backing."); 117234b5d2c6SMax Reitz ret = bdrv_open_backing_file(bs, backing_options, &local_err); 1173b6ce07aaSKevin Wolf if (ret < 0) { 1174b6ad491aSKevin Wolf goto close_and_fail; 1175b6ce07aaSKevin Wolf } 1176b6ce07aaSKevin Wolf } 1177b6ce07aaSKevin Wolf 1178b6ad491aSKevin Wolf /* Check if any unknown options were used */ 1179b6ad491aSKevin Wolf if (qdict_size(options) != 0) { 1180b6ad491aSKevin Wolf const QDictEntry *entry = qdict_first(options); 118134b5d2c6SMax Reitz error_setg(errp, "Block format '%s' used by device '%s' doesn't " 118234b5d2c6SMax Reitz "support the option '%s'", drv->format_name, bs->device_name, 118334b5d2c6SMax Reitz entry->key); 1184b6ad491aSKevin Wolf 1185b6ad491aSKevin Wolf ret = -EINVAL; 1186b6ad491aSKevin Wolf goto close_and_fail; 1187b6ad491aSKevin Wolf } 1188b6ad491aSKevin Wolf QDECREF(options); 1189b6ad491aSKevin Wolf 1190b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 11917d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 1192b6ce07aaSKevin Wolf } 1193b6ce07aaSKevin Wolf 1194b6ce07aaSKevin Wolf return 0; 1195b6ce07aaSKevin Wolf 1196b6ce07aaSKevin Wolf unlink_and_fail: 1197f500a6d3SKevin Wolf if (file != NULL) { 11984f6fd349SFam Zheng bdrv_unref(file); 1199f500a6d3SKevin Wolf } 1200b6ce07aaSKevin Wolf if (bs->is_temporary) { 1201b6ce07aaSKevin Wolf unlink(filename); 1202b6ce07aaSKevin Wolf } 1203de9c0cecSKevin Wolf fail: 1204de9c0cecSKevin Wolf QDECREF(bs->options); 1205b6ad491aSKevin Wolf QDECREF(options); 1206de9c0cecSKevin Wolf bs->options = NULL; 120734b5d2c6SMax Reitz if (error_is_set(&local_err)) { 120834b5d2c6SMax Reitz error_propagate(errp, local_err); 120934b5d2c6SMax Reitz } 1210b6ad491aSKevin Wolf return ret; 1211de9c0cecSKevin Wolf 1212b6ad491aSKevin Wolf close_and_fail: 1213b6ad491aSKevin Wolf bdrv_close(bs); 1214b6ad491aSKevin Wolf QDECREF(options); 121534b5d2c6SMax Reitz if (error_is_set(&local_err)) { 121634b5d2c6SMax Reitz error_propagate(errp, local_err); 121734b5d2c6SMax Reitz } 1218b6ce07aaSKevin Wolf return ret; 1219b6ce07aaSKevin Wolf } 1220b6ce07aaSKevin Wolf 1221e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 1222e971aa12SJeff Cody bool prepared; 1223e971aa12SJeff Cody BDRVReopenState state; 1224e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 1225e971aa12SJeff Cody } BlockReopenQueueEntry; 1226e971aa12SJeff Cody 1227e971aa12SJeff Cody /* 1228e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 1229e971aa12SJeff Cody * reopen of multiple devices. 1230e971aa12SJeff Cody * 1231e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 1232e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 1233e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 1234e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 1235e971aa12SJeff Cody * atomic 'set'. 1236e971aa12SJeff Cody * 1237e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 1238e971aa12SJeff Cody * 1239e971aa12SJeff Cody * flags contains the open flags for the associated bs 1240e971aa12SJeff Cody * 1241e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 1242e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 1243e971aa12SJeff Cody * 1244e971aa12SJeff Cody */ 1245e971aa12SJeff Cody BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 1246e971aa12SJeff Cody BlockDriverState *bs, int flags) 1247e971aa12SJeff Cody { 1248e971aa12SJeff Cody assert(bs != NULL); 1249e971aa12SJeff Cody 1250e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 1251e971aa12SJeff Cody if (bs_queue == NULL) { 1252e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 1253e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 1254e971aa12SJeff Cody } 1255e971aa12SJeff Cody 1256e971aa12SJeff Cody if (bs->file) { 1257e971aa12SJeff Cody bdrv_reopen_queue(bs_queue, bs->file, flags); 1258e971aa12SJeff Cody } 1259e971aa12SJeff Cody 1260e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 1261e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 1262e971aa12SJeff Cody 1263e971aa12SJeff Cody bs_entry->state.bs = bs; 1264e971aa12SJeff Cody bs_entry->state.flags = flags; 1265e971aa12SJeff Cody 1266e971aa12SJeff Cody return bs_queue; 1267e971aa12SJeff Cody } 1268e971aa12SJeff Cody 1269e971aa12SJeff Cody /* 1270e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 1271e971aa12SJeff Cody * 1272e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 1273e971aa12SJeff Cody * via bdrv_reopen_queue(). 1274e971aa12SJeff Cody * 1275e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 1276e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 1277e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 1278e971aa12SJeff Cody * data cleaned up. 1279e971aa12SJeff Cody * 1280e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 1281e971aa12SJeff Cody * to all devices. 1282e971aa12SJeff Cody * 1283e971aa12SJeff Cody */ 1284e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 1285e971aa12SJeff Cody { 1286e971aa12SJeff Cody int ret = -1; 1287e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 1288e971aa12SJeff Cody Error *local_err = NULL; 1289e971aa12SJeff Cody 1290e971aa12SJeff Cody assert(bs_queue != NULL); 1291e971aa12SJeff Cody 1292e971aa12SJeff Cody bdrv_drain_all(); 1293e971aa12SJeff Cody 1294e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1295e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 1296e971aa12SJeff Cody error_propagate(errp, local_err); 1297e971aa12SJeff Cody goto cleanup; 1298e971aa12SJeff Cody } 1299e971aa12SJeff Cody bs_entry->prepared = true; 1300e971aa12SJeff Cody } 1301e971aa12SJeff Cody 1302e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 1303e971aa12SJeff Cody * changes 1304e971aa12SJeff Cody */ 1305e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1306e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 1307e971aa12SJeff Cody } 1308e971aa12SJeff Cody 1309e971aa12SJeff Cody ret = 0; 1310e971aa12SJeff Cody 1311e971aa12SJeff Cody cleanup: 1312e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 1313e971aa12SJeff Cody if (ret && bs_entry->prepared) { 1314e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 1315e971aa12SJeff Cody } 1316e971aa12SJeff Cody g_free(bs_entry); 1317e971aa12SJeff Cody } 1318e971aa12SJeff Cody g_free(bs_queue); 1319e971aa12SJeff Cody return ret; 1320e971aa12SJeff Cody } 1321e971aa12SJeff Cody 1322e971aa12SJeff Cody 1323e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 1324e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1325e971aa12SJeff Cody { 1326e971aa12SJeff Cody int ret = -1; 1327e971aa12SJeff Cody Error *local_err = NULL; 1328e971aa12SJeff Cody BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); 1329e971aa12SJeff Cody 1330e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1331e971aa12SJeff Cody if (local_err != NULL) { 1332e971aa12SJeff Cody error_propagate(errp, local_err); 1333e971aa12SJeff Cody } 1334e971aa12SJeff Cody return ret; 1335e971aa12SJeff Cody } 1336e971aa12SJeff Cody 1337e971aa12SJeff Cody 1338e971aa12SJeff Cody /* 1339e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1340e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1341e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1342e971aa12SJeff Cody * 1343e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1344e971aa12SJeff Cody * flags are the new open flags 1345e971aa12SJeff Cody * queue is the reopen queue 1346e971aa12SJeff Cody * 1347e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1348e971aa12SJeff Cody * as well. 1349e971aa12SJeff Cody * 1350e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1351e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1352e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1353e971aa12SJeff Cody * 1354e971aa12SJeff Cody */ 1355e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1356e971aa12SJeff Cody Error **errp) 1357e971aa12SJeff Cody { 1358e971aa12SJeff Cody int ret = -1; 1359e971aa12SJeff Cody Error *local_err = NULL; 1360e971aa12SJeff Cody BlockDriver *drv; 1361e971aa12SJeff Cody 1362e971aa12SJeff Cody assert(reopen_state != NULL); 1363e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1364e971aa12SJeff Cody drv = reopen_state->bs->drv; 1365e971aa12SJeff Cody 1366e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 1367e971aa12SJeff Cody * to r/w */ 1368e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 1369e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 1370e971aa12SJeff Cody error_set(errp, QERR_DEVICE_IS_READ_ONLY, 1371e971aa12SJeff Cody reopen_state->bs->device_name); 1372e971aa12SJeff Cody goto error; 1373e971aa12SJeff Cody } 1374e971aa12SJeff Cody 1375e971aa12SJeff Cody 1376e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 1377e971aa12SJeff Cody if (ret) { 1378e971aa12SJeff Cody error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", 1379e971aa12SJeff Cody strerror(-ret)); 1380e971aa12SJeff Cody goto error; 1381e971aa12SJeff Cody } 1382e971aa12SJeff Cody 1383e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 1384e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 1385e971aa12SJeff Cody if (ret) { 1386e971aa12SJeff Cody if (local_err != NULL) { 1387e971aa12SJeff Cody error_propagate(errp, local_err); 1388e971aa12SJeff Cody } else { 1389d8b6895fSLuiz Capitulino error_setg(errp, "failed while preparing to reopen image '%s'", 1390e971aa12SJeff Cody reopen_state->bs->filename); 1391e971aa12SJeff Cody } 1392e971aa12SJeff Cody goto error; 1393e971aa12SJeff Cody } 1394e971aa12SJeff Cody } else { 1395e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 1396e971aa12SJeff Cody * handler for each supported drv. */ 1397e971aa12SJeff Cody error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 1398e971aa12SJeff Cody drv->format_name, reopen_state->bs->device_name, 1399e971aa12SJeff Cody "reopening of file"); 1400e971aa12SJeff Cody ret = -1; 1401e971aa12SJeff Cody goto error; 1402e971aa12SJeff Cody } 1403e971aa12SJeff Cody 1404e971aa12SJeff Cody ret = 0; 1405e971aa12SJeff Cody 1406e971aa12SJeff Cody error: 1407e971aa12SJeff Cody return ret; 1408e971aa12SJeff Cody } 1409e971aa12SJeff Cody 1410e971aa12SJeff Cody /* 1411e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 1412e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 1413e971aa12SJeff Cody * the active BlockDriverState contents. 1414e971aa12SJeff Cody */ 1415e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 1416e971aa12SJeff Cody { 1417e971aa12SJeff Cody BlockDriver *drv; 1418e971aa12SJeff Cody 1419e971aa12SJeff Cody assert(reopen_state != NULL); 1420e971aa12SJeff Cody drv = reopen_state->bs->drv; 1421e971aa12SJeff Cody assert(drv != NULL); 1422e971aa12SJeff Cody 1423e971aa12SJeff Cody /* If there are any driver level actions to take */ 1424e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 1425e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 1426e971aa12SJeff Cody } 1427e971aa12SJeff Cody 1428e971aa12SJeff Cody /* set BDS specific flags now */ 1429e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 1430e971aa12SJeff Cody reopen_state->bs->enable_write_cache = !!(reopen_state->flags & 1431e971aa12SJeff Cody BDRV_O_CACHE_WB); 1432e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 1433e971aa12SJeff Cody } 1434e971aa12SJeff Cody 1435e971aa12SJeff Cody /* 1436e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 1437e971aa12SJeff Cody * reopen_state 1438e971aa12SJeff Cody */ 1439e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 1440e971aa12SJeff Cody { 1441e971aa12SJeff Cody BlockDriver *drv; 1442e971aa12SJeff Cody 1443e971aa12SJeff Cody assert(reopen_state != NULL); 1444e971aa12SJeff Cody drv = reopen_state->bs->drv; 1445e971aa12SJeff Cody assert(drv != NULL); 1446e971aa12SJeff Cody 1447e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 1448e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 1449e971aa12SJeff Cody } 1450e971aa12SJeff Cody } 1451e971aa12SJeff Cody 1452e971aa12SJeff Cody 1453fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 1454fc01f7e7Sbellard { 14553e914655SPaolo Bonzini if (bs->job) { 14563e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 14573e914655SPaolo Bonzini } 145858fda173SStefan Hajnoczi bdrv_drain_all(); /* complete I/O */ 145958fda173SStefan Hajnoczi bdrv_flush(bs); 146058fda173SStefan Hajnoczi bdrv_drain_all(); /* in case flush left pending I/O */ 1461d7d512f6SPaolo Bonzini notifier_list_notify(&bs->close_notifiers, bs); 14627094f12fSKevin Wolf 14633cbc002cSPaolo Bonzini if (bs->drv) { 1464557df6acSStefan Hajnoczi if (bs->backing_hd) { 14654f6fd349SFam Zheng bdrv_unref(bs->backing_hd); 1466557df6acSStefan Hajnoczi bs->backing_hd = NULL; 1467557df6acSStefan Hajnoczi } 1468ea2384d3Sbellard bs->drv->bdrv_close(bs); 14697267c094SAnthony Liguori g_free(bs->opaque); 1470ea2384d3Sbellard #ifdef _WIN32 1471ea2384d3Sbellard if (bs->is_temporary) { 1472ea2384d3Sbellard unlink(bs->filename); 1473ea2384d3Sbellard } 147467b915a5Sbellard #endif 1475ea2384d3Sbellard bs->opaque = NULL; 1476ea2384d3Sbellard bs->drv = NULL; 147753fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 1478a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 1479a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 14806405875cSPaolo Bonzini bs->total_sectors = 0; 14816405875cSPaolo Bonzini bs->encrypted = 0; 14826405875cSPaolo Bonzini bs->valid_key = 0; 14836405875cSPaolo Bonzini bs->sg = 0; 14846405875cSPaolo Bonzini bs->growable = 0; 14850d51b4deSAsias He bs->zero_beyond_eof = false; 1486de9c0cecSKevin Wolf QDECREF(bs->options); 1487de9c0cecSKevin Wolf bs->options = NULL; 1488b338082bSbellard 148966f82ceeSKevin Wolf if (bs->file != NULL) { 14904f6fd349SFam Zheng bdrv_unref(bs->file); 14910ac9377dSPaolo Bonzini bs->file = NULL; 149266f82ceeSKevin Wolf } 14939ca11154SPavel Hrdina } 149466f82ceeSKevin Wolf 14957d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 149698f90dbaSZhi Yong Wu 149798f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 149898f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 149998f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 150098f90dbaSZhi Yong Wu } 1501b338082bSbellard } 1502b338082bSbellard 15032bc93fedSMORITA Kazutaka void bdrv_close_all(void) 15042bc93fedSMORITA Kazutaka { 15052bc93fedSMORITA Kazutaka BlockDriverState *bs; 15062bc93fedSMORITA Kazutaka 15072bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 15082bc93fedSMORITA Kazutaka bdrv_close(bs); 15092bc93fedSMORITA Kazutaka } 15102bc93fedSMORITA Kazutaka } 15112bc93fedSMORITA Kazutaka 151288266f5aSStefan Hajnoczi /* Check if any requests are in-flight (including throttled requests) */ 151388266f5aSStefan Hajnoczi static bool bdrv_requests_pending(BlockDriverState *bs) 151488266f5aSStefan Hajnoczi { 151588266f5aSStefan Hajnoczi if (!QLIST_EMPTY(&bs->tracked_requests)) { 151688266f5aSStefan Hajnoczi return true; 151788266f5aSStefan Hajnoczi } 1518cc0681c4SBenoît Canet if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) { 1519cc0681c4SBenoît Canet return true; 1520cc0681c4SBenoît Canet } 1521cc0681c4SBenoît Canet if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) { 152288266f5aSStefan Hajnoczi return true; 152388266f5aSStefan Hajnoczi } 152488266f5aSStefan Hajnoczi if (bs->file && bdrv_requests_pending(bs->file)) { 152588266f5aSStefan Hajnoczi return true; 152688266f5aSStefan Hajnoczi } 152788266f5aSStefan Hajnoczi if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) { 152888266f5aSStefan Hajnoczi return true; 152988266f5aSStefan Hajnoczi } 153088266f5aSStefan Hajnoczi return false; 153188266f5aSStefan Hajnoczi } 153288266f5aSStefan Hajnoczi 153388266f5aSStefan Hajnoczi static bool bdrv_requests_pending_all(void) 153488266f5aSStefan Hajnoczi { 153588266f5aSStefan Hajnoczi BlockDriverState *bs; 153688266f5aSStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 153788266f5aSStefan Hajnoczi if (bdrv_requests_pending(bs)) { 153888266f5aSStefan Hajnoczi return true; 153988266f5aSStefan Hajnoczi } 154088266f5aSStefan Hajnoczi } 154188266f5aSStefan Hajnoczi return false; 154288266f5aSStefan Hajnoczi } 154388266f5aSStefan Hajnoczi 1544922453bcSStefan Hajnoczi /* 1545922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 1546922453bcSStefan Hajnoczi * 1547922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 1548922453bcSStefan Hajnoczi * after calling this function. 15494c355d53SZhi Yong Wu * 15504c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 15514c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 15524c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 15534c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 15544c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 1555922453bcSStefan Hajnoczi */ 1556922453bcSStefan Hajnoczi void bdrv_drain_all(void) 1557922453bcSStefan Hajnoczi { 155888266f5aSStefan Hajnoczi /* Always run first iteration so any pending completion BHs run */ 155988266f5aSStefan Hajnoczi bool busy = true; 1560922453bcSStefan Hajnoczi BlockDriverState *bs; 1561922453bcSStefan Hajnoczi 156288266f5aSStefan Hajnoczi while (busy) { 15634c355d53SZhi Yong Wu /* FIXME: We do not have timer support here, so this is effectively 15644c355d53SZhi Yong Wu * a busy wait. 15654c355d53SZhi Yong Wu */ 15664c355d53SZhi Yong Wu QTAILQ_FOREACH(bs, &bdrv_states, list) { 1567cc0681c4SBenoît Canet if (bdrv_start_throttled_reqs(bs)) { 15684c355d53SZhi Yong Wu busy = true; 15694c355d53SZhi Yong Wu } 15704c355d53SZhi Yong Wu } 1571922453bcSStefan Hajnoczi 157288266f5aSStefan Hajnoczi busy = bdrv_requests_pending_all(); 157388266f5aSStefan Hajnoczi busy |= aio_poll(qemu_get_aio_context(), busy); 1574922453bcSStefan Hajnoczi } 1575922453bcSStefan Hajnoczi } 1576922453bcSStefan Hajnoczi 1577d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 1578d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1579d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1580d22b2f41SRyan Harper { 1581d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 1582d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 1583d22b2f41SRyan Harper } 1584d22b2f41SRyan Harper bs->device_name[0] = '\0'; 1585d22b2f41SRyan Harper } 1586d22b2f41SRyan Harper 1587e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1588e023b2e2SPaolo Bonzini { 1589e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1590e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1591e023b2e2SPaolo Bonzini } 1592e023b2e2SPaolo Bonzini } 1593e023b2e2SPaolo Bonzini 15944ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 15954ddc07caSPaolo Bonzini BlockDriverState *bs_src) 15964ddc07caSPaolo Bonzini { 15974ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 15984ddc07caSPaolo Bonzini bs_dest->open_flags = bs_src->open_flags; 15994ddc07caSPaolo Bonzini 16004ddc07caSPaolo Bonzini /* dev info */ 16014ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 16024ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 16034ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 16044ddc07caSPaolo Bonzini bs_dest->buffer_alignment = bs_src->buffer_alignment; 16054ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 16064ddc07caSPaolo Bonzini 16074ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 16084ddc07caSPaolo Bonzini 1609cc0681c4SBenoît Canet /* i/o throttled req */ 1610cc0681c4SBenoît Canet memcpy(&bs_dest->throttle_state, 1611cc0681c4SBenoît Canet &bs_src->throttle_state, 1612cc0681c4SBenoît Canet sizeof(ThrottleState)); 1613cc0681c4SBenoît Canet bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0]; 1614cc0681c4SBenoît Canet bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1]; 16154ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 16164ddc07caSPaolo Bonzini 16174ddc07caSPaolo Bonzini /* r/w error */ 16184ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 16194ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 16204ddc07caSPaolo Bonzini 16214ddc07caSPaolo Bonzini /* i/o status */ 16224ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 16234ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 16244ddc07caSPaolo Bonzini 16254ddc07caSPaolo Bonzini /* dirty bitmap */ 1626e4654d2dSFam Zheng bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps; 16274ddc07caSPaolo Bonzini 16289fcb0251SFam Zheng /* reference count */ 16299fcb0251SFam Zheng bs_dest->refcnt = bs_src->refcnt; 16309fcb0251SFam Zheng 16314ddc07caSPaolo Bonzini /* job */ 16324ddc07caSPaolo Bonzini bs_dest->in_use = bs_src->in_use; 16334ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 16344ddc07caSPaolo Bonzini 16354ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 16364ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 16374ddc07caSPaolo Bonzini bs_src->device_name); 16384ddc07caSPaolo Bonzini bs_dest->list = bs_src->list; 16394ddc07caSPaolo Bonzini } 16404ddc07caSPaolo Bonzini 16414ddc07caSPaolo Bonzini /* 16424ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 16434ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 16444ddc07caSPaolo Bonzini * actually attached to a device. 16454ddc07caSPaolo Bonzini * 16464ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 16474ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 16484ddc07caSPaolo Bonzini * 16494ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 16504ddc07caSPaolo Bonzini * 16514ddc07caSPaolo Bonzini * This function does not create any image files. 16524ddc07caSPaolo Bonzini */ 16534ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 16544ddc07caSPaolo Bonzini { 16554ddc07caSPaolo Bonzini BlockDriverState tmp; 16564ddc07caSPaolo Bonzini 16574ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 16584ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 1659e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs_new->dirty_bitmaps)); 16604ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 16614ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 16624ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 16634ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1664cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 16654ddc07caSPaolo Bonzini 16664ddc07caSPaolo Bonzini tmp = *bs_new; 16674ddc07caSPaolo Bonzini *bs_new = *bs_old; 16684ddc07caSPaolo Bonzini *bs_old = tmp; 16694ddc07caSPaolo Bonzini 16704ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 16714ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 16724ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 16734ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 16744ddc07caSPaolo Bonzini 16754ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 16764ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 16774ddc07caSPaolo Bonzini 16784ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 16794ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 16804ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 16814ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 16824ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1683cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 16844ddc07caSPaolo Bonzini 16854ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 16864ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 16874ddc07caSPaolo Bonzini } 16884ddc07caSPaolo Bonzini 16898802d1fdSJeff Cody /* 16908802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 16918802d1fdSJeff Cody * live, while keeping required fields on the top layer. 16928802d1fdSJeff Cody * 16938802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 16948802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 16958802d1fdSJeff Cody * 1696f6801b83SJeff Cody * bs_new is required to be anonymous. 1697f6801b83SJeff Cody * 16988802d1fdSJeff Cody * This function does not create any image files. 16998802d1fdSJeff Cody */ 17008802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 17018802d1fdSJeff Cody { 17024ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 17038802d1fdSJeff Cody 17048802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 17058802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 17064ddc07caSPaolo Bonzini bs_top->backing_hd = bs_new; 17074ddc07caSPaolo Bonzini bs_top->open_flags &= ~BDRV_O_NO_BACKING; 17084ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 17094ddc07caSPaolo Bonzini bs_new->filename); 17104ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 17114ddc07caSPaolo Bonzini bs_new->drv ? bs_new->drv->format_name : ""); 17128802d1fdSJeff Cody } 17138802d1fdSJeff Cody 17144f6fd349SFam Zheng static void bdrv_delete(BlockDriverState *bs) 1715b338082bSbellard { 1716fa879d62SMarkus Armbruster assert(!bs->dev); 17173e914655SPaolo Bonzini assert(!bs->job); 17183e914655SPaolo Bonzini assert(!bs->in_use); 17194f6fd349SFam Zheng assert(!bs->refcnt); 1720e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 172118846deeSMarkus Armbruster 1722e1b5c52eSStefan Hajnoczi bdrv_close(bs); 1723e1b5c52eSStefan Hajnoczi 17241b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1725d22b2f41SRyan Harper bdrv_make_anon(bs); 172634c6f050Saurel32 17277267c094SAnthony Liguori g_free(bs); 1728fc01f7e7Sbellard } 1729fc01f7e7Sbellard 1730fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1731fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 173218846deeSMarkus Armbruster { 1733fa879d62SMarkus Armbruster if (bs->dev) { 173418846deeSMarkus Armbruster return -EBUSY; 173518846deeSMarkus Armbruster } 1736fa879d62SMarkus Armbruster bs->dev = dev; 173728a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 173818846deeSMarkus Armbruster return 0; 173918846deeSMarkus Armbruster } 174018846deeSMarkus Armbruster 1741fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1742fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 174318846deeSMarkus Armbruster { 1744fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1745fa879d62SMarkus Armbruster abort(); 1746fa879d62SMarkus Armbruster } 1747fa879d62SMarkus Armbruster } 1748fa879d62SMarkus Armbruster 1749fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1750fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1751fa879d62SMarkus Armbruster { 1752fa879d62SMarkus Armbruster assert(bs->dev == dev); 1753fa879d62SMarkus Armbruster bs->dev = NULL; 17540e49de52SMarkus Armbruster bs->dev_ops = NULL; 17550e49de52SMarkus Armbruster bs->dev_opaque = NULL; 175629e05f20SMarkus Armbruster bs->buffer_alignment = 512; 175718846deeSMarkus Armbruster } 175818846deeSMarkus Armbruster 1759fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1760fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 176118846deeSMarkus Armbruster { 1762fa879d62SMarkus Armbruster return bs->dev; 176318846deeSMarkus Armbruster } 176418846deeSMarkus Armbruster 17650e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 17660e49de52SMarkus Armbruster void *opaque) 17670e49de52SMarkus Armbruster { 17680e49de52SMarkus Armbruster bs->dev_ops = ops; 17690e49de52SMarkus Armbruster bs->dev_opaque = opaque; 17700e49de52SMarkus Armbruster } 17710e49de52SMarkus Armbruster 177232c81a4aSPaolo Bonzini void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 177332c81a4aSPaolo Bonzini enum MonitorEvent ev, 17741ceee0d5SPaolo Bonzini BlockErrorAction action, bool is_read) 1775329c0a48SLuiz Capitulino { 1776329c0a48SLuiz Capitulino QObject *data; 1777329c0a48SLuiz Capitulino const char *action_str; 1778329c0a48SLuiz Capitulino 1779329c0a48SLuiz Capitulino switch (action) { 1780329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1781329c0a48SLuiz Capitulino action_str = "report"; 1782329c0a48SLuiz Capitulino break; 1783329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1784329c0a48SLuiz Capitulino action_str = "ignore"; 1785329c0a48SLuiz Capitulino break; 1786329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1787329c0a48SLuiz Capitulino action_str = "stop"; 1788329c0a48SLuiz Capitulino break; 1789329c0a48SLuiz Capitulino default: 1790329c0a48SLuiz Capitulino abort(); 1791329c0a48SLuiz Capitulino } 1792329c0a48SLuiz Capitulino 1793329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1794329c0a48SLuiz Capitulino bdrv->device_name, 1795329c0a48SLuiz Capitulino action_str, 1796329c0a48SLuiz Capitulino is_read ? "read" : "write"); 179732c81a4aSPaolo Bonzini monitor_protocol_event(ev, data); 1798329c0a48SLuiz Capitulino 1799329c0a48SLuiz Capitulino qobject_decref(data); 1800329c0a48SLuiz Capitulino } 1801329c0a48SLuiz Capitulino 18026f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 18036f382ed2SLuiz Capitulino { 18046f382ed2SLuiz Capitulino QObject *data; 18056f382ed2SLuiz Capitulino 18066f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 18076f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 18086f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 18096f382ed2SLuiz Capitulino 18106f382ed2SLuiz Capitulino qobject_decref(data); 18116f382ed2SLuiz Capitulino } 18126f382ed2SLuiz Capitulino 18137d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 18140e49de52SMarkus Armbruster { 1815145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 18166f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 18177d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 18186f382ed2SLuiz Capitulino if (tray_was_closed) { 18196f382ed2SLuiz Capitulino /* tray open */ 18206f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 18216f382ed2SLuiz Capitulino } 18226f382ed2SLuiz Capitulino if (load) { 18236f382ed2SLuiz Capitulino /* tray close */ 18246f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 18256f382ed2SLuiz Capitulino } 1826145feb17SMarkus Armbruster } 1827145feb17SMarkus Armbruster } 1828145feb17SMarkus Armbruster 18292c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 18302c6942faSMarkus Armbruster { 18312c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 18322c6942faSMarkus Armbruster } 18332c6942faSMarkus Armbruster 1834025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1835025ccaa7SPaolo Bonzini { 1836025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1837025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1838025ccaa7SPaolo Bonzini } 1839025ccaa7SPaolo Bonzini } 1840025ccaa7SPaolo Bonzini 1841e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1842e4def80bSMarkus Armbruster { 1843e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1844e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1845e4def80bSMarkus Armbruster } 1846e4def80bSMarkus Armbruster return false; 1847e4def80bSMarkus Armbruster } 1848e4def80bSMarkus Armbruster 1849145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1850145feb17SMarkus Armbruster { 1851145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1852145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 18530e49de52SMarkus Armbruster } 18540e49de52SMarkus Armbruster } 18550e49de52SMarkus Armbruster 1856f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 1857f107639aSMarkus Armbruster { 1858f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 1859f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 1860f107639aSMarkus Armbruster } 1861f107639aSMarkus Armbruster return false; 1862f107639aSMarkus Armbruster } 1863f107639aSMarkus Armbruster 1864e97fc193Saliguori /* 1865e97fc193Saliguori * Run consistency checks on an image 1866e97fc193Saliguori * 1867e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 1868a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 1869e076f338SKevin Wolf * check are stored in res. 1870e97fc193Saliguori */ 18714534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 1872e97fc193Saliguori { 1873e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 1874e97fc193Saliguori return -ENOTSUP; 1875e97fc193Saliguori } 1876e97fc193Saliguori 1877e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 18784534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 1879e97fc193Saliguori } 1880e97fc193Saliguori 18818a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 18828a426614SKevin Wolf 188333e3963eSbellard /* commit COW file into the raw image */ 188433e3963eSbellard int bdrv_commit(BlockDriverState *bs) 188533e3963eSbellard { 188619cb3738Sbellard BlockDriver *drv = bs->drv; 18878a426614SKevin Wolf int64_t sector, total_sectors; 18888a426614SKevin Wolf int n, ro, open_flags; 18890bce597dSJeff Cody int ret = 0; 18908a426614SKevin Wolf uint8_t *buf; 1891c2cba3d9SJim Meyering char filename[PATH_MAX]; 189233e3963eSbellard 189319cb3738Sbellard if (!drv) 189419cb3738Sbellard return -ENOMEDIUM; 189533e3963eSbellard 18964dca4b63SNaphtali Sprei if (!bs->backing_hd) { 18974dca4b63SNaphtali Sprei return -ENOTSUP; 18984dca4b63SNaphtali Sprei } 18994dca4b63SNaphtali Sprei 19002d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 19012d3735d3SStefan Hajnoczi return -EBUSY; 19022d3735d3SStefan Hajnoczi } 19032d3735d3SStefan Hajnoczi 19044dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 1905c2cba3d9SJim Meyering /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ 1906c2cba3d9SJim Meyering pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); 19074dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 19084dca4b63SNaphtali Sprei 19094dca4b63SNaphtali Sprei if (ro) { 19100bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 19110bce597dSJeff Cody return -EACCES; 19124dca4b63SNaphtali Sprei } 1913ea2384d3Sbellard } 1914ea2384d3Sbellard 19156ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 19167267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 19178a426614SKevin Wolf 19188a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 1919d663640cSPaolo Bonzini ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); 1920d663640cSPaolo Bonzini if (ret < 0) { 1921d663640cSPaolo Bonzini goto ro_cleanup; 1922d663640cSPaolo Bonzini } 1923d663640cSPaolo Bonzini if (ret) { 19248a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 19254dca4b63SNaphtali Sprei ret = -EIO; 19264dca4b63SNaphtali Sprei goto ro_cleanup; 192733e3963eSbellard } 192833e3963eSbellard 19298a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 19304dca4b63SNaphtali Sprei ret = -EIO; 19314dca4b63SNaphtali Sprei goto ro_cleanup; 193233e3963eSbellard } 193333e3963eSbellard } 193433e3963eSbellard } 193595389c86Sbellard 19361d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 19371d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 19381d44952fSChristoph Hellwig bdrv_flush(bs); 19391d44952fSChristoph Hellwig } 194095389c86Sbellard 19413f5075aeSChristoph Hellwig /* 19423f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 19433f5075aeSChristoph Hellwig * stable on disk. 19443f5075aeSChristoph Hellwig */ 19453f5075aeSChristoph Hellwig if (bs->backing_hd) 19463f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 19474dca4b63SNaphtali Sprei 19484dca4b63SNaphtali Sprei ro_cleanup: 19497267c094SAnthony Liguori g_free(buf); 19504dca4b63SNaphtali Sprei 19514dca4b63SNaphtali Sprei if (ro) { 19520bce597dSJeff Cody /* ignoring error return here */ 19530bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 19544dca4b63SNaphtali Sprei } 19554dca4b63SNaphtali Sprei 19561d44952fSChristoph Hellwig return ret; 195733e3963eSbellard } 195833e3963eSbellard 1959e8877497SStefan Hajnoczi int bdrv_commit_all(void) 19606ab4b5abSMarkus Armbruster { 19616ab4b5abSMarkus Armbruster BlockDriverState *bs; 19626ab4b5abSMarkus Armbruster 19636ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 1964272d2d8eSJeff Cody if (bs->drv && bs->backing_hd) { 1965e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 1966e8877497SStefan Hajnoczi if (ret < 0) { 1967e8877497SStefan Hajnoczi return ret; 19686ab4b5abSMarkus Armbruster } 19696ab4b5abSMarkus Armbruster } 1970272d2d8eSJeff Cody } 1971e8877497SStefan Hajnoczi return 0; 1972e8877497SStefan Hajnoczi } 19736ab4b5abSMarkus Armbruster 1974dbffbdcfSStefan Hajnoczi /** 1975dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 1976dbffbdcfSStefan Hajnoczi * 1977dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 1978dbffbdcfSStefan Hajnoczi */ 1979dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 1980dbffbdcfSStefan Hajnoczi { 1981dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 1982f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 1983dbffbdcfSStefan Hajnoczi } 1984dbffbdcfSStefan Hajnoczi 1985dbffbdcfSStefan Hajnoczi /** 1986dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 1987dbffbdcfSStefan Hajnoczi */ 1988dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 1989dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 1990dbffbdcfSStefan Hajnoczi int64_t sector_num, 1991dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 1992dbffbdcfSStefan Hajnoczi { 1993dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 1994dbffbdcfSStefan Hajnoczi .bs = bs, 1995dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 1996dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 1997dbffbdcfSStefan Hajnoczi .is_write = is_write, 19985f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 1999dbffbdcfSStefan Hajnoczi }; 2000dbffbdcfSStefan Hajnoczi 2001f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 2002f4658285SStefan Hajnoczi 2003dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 2004dbffbdcfSStefan Hajnoczi } 2005dbffbdcfSStefan Hajnoczi 2006d83947acSStefan Hajnoczi /** 2007d83947acSStefan Hajnoczi * Round a region to cluster boundaries 2008d83947acSStefan Hajnoczi */ 2009343bded4SPaolo Bonzini void bdrv_round_to_clusters(BlockDriverState *bs, 2010d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 2011d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 2012d83947acSStefan Hajnoczi int *cluster_nb_sectors) 2013d83947acSStefan Hajnoczi { 2014d83947acSStefan Hajnoczi BlockDriverInfo bdi; 2015d83947acSStefan Hajnoczi 2016d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 2017d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 2018d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 2019d83947acSStefan Hajnoczi } else { 2020d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 2021d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 2022d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 2023d83947acSStefan Hajnoczi nb_sectors, c); 2024d83947acSStefan Hajnoczi } 2025d83947acSStefan Hajnoczi } 2026d83947acSStefan Hajnoczi 2027f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 2028f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 2029d83947acSStefan Hajnoczi /* aaaa bbbb */ 2030d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 2031d83947acSStefan Hajnoczi return false; 2032d83947acSStefan Hajnoczi } 2033d83947acSStefan Hajnoczi /* bbbb aaaa */ 2034d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 2035d83947acSStefan Hajnoczi return false; 2036d83947acSStefan Hajnoczi } 2037d83947acSStefan Hajnoczi return true; 2038f4658285SStefan Hajnoczi } 2039f4658285SStefan Hajnoczi 2040f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 2041f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 2042f4658285SStefan Hajnoczi { 2043f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 2044d83947acSStefan Hajnoczi int64_t cluster_sector_num; 2045d83947acSStefan Hajnoczi int cluster_nb_sectors; 2046f4658285SStefan Hajnoczi bool retry; 2047f4658285SStefan Hajnoczi 2048d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 2049d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 2050d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 2051d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 2052d83947acSStefan Hajnoczi * interleave between them. 2053d83947acSStefan Hajnoczi */ 2054343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2055d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2056d83947acSStefan Hajnoczi 2057f4658285SStefan Hajnoczi do { 2058f4658285SStefan Hajnoczi retry = false; 2059f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 2060d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 2061d83947acSStefan Hajnoczi cluster_nb_sectors)) { 20625f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 20635f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 20645f8b6491SStefan Hajnoczi * never happen since it means deadlock. 20655f8b6491SStefan Hajnoczi */ 20665f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 20675f8b6491SStefan Hajnoczi 2068f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 2069f4658285SStefan Hajnoczi retry = true; 2070f4658285SStefan Hajnoczi break; 2071f4658285SStefan Hajnoczi } 2072f4658285SStefan Hajnoczi } 2073f4658285SStefan Hajnoczi } while (retry); 2074f4658285SStefan Hajnoczi } 2075f4658285SStefan Hajnoczi 2076756e6736SKevin Wolf /* 2077756e6736SKevin Wolf * Return values: 2078756e6736SKevin Wolf * 0 - success 2079756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 2080756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 2081756e6736SKevin Wolf * image file header 2082756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 2083756e6736SKevin Wolf */ 2084756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 2085756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 2086756e6736SKevin Wolf { 2087756e6736SKevin Wolf BlockDriver *drv = bs->drv; 2088469ef350SPaolo Bonzini int ret; 2089756e6736SKevin Wolf 20905f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 20915f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 20925f377794SPaolo Bonzini return -EINVAL; 20935f377794SPaolo Bonzini } 20945f377794SPaolo Bonzini 2095756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 2096469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 2097756e6736SKevin Wolf } else { 2098469ef350SPaolo Bonzini ret = -ENOTSUP; 2099756e6736SKevin Wolf } 2100469ef350SPaolo Bonzini 2101469ef350SPaolo Bonzini if (ret == 0) { 2102469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 2103469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 2104469ef350SPaolo Bonzini } 2105469ef350SPaolo Bonzini return ret; 2106756e6736SKevin Wolf } 2107756e6736SKevin Wolf 21086ebdcee2SJeff Cody /* 21096ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 21106ebdcee2SJeff Cody * 21116ebdcee2SJeff Cody * active is the current topmost image. 21126ebdcee2SJeff Cody * 21136ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 21146ebdcee2SJeff Cody * or if active == bs. 21156ebdcee2SJeff Cody */ 21166ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 21176ebdcee2SJeff Cody BlockDriverState *bs) 21186ebdcee2SJeff Cody { 21196ebdcee2SJeff Cody BlockDriverState *overlay = NULL; 21206ebdcee2SJeff Cody BlockDriverState *intermediate; 21216ebdcee2SJeff Cody 21226ebdcee2SJeff Cody assert(active != NULL); 21236ebdcee2SJeff Cody assert(bs != NULL); 21246ebdcee2SJeff Cody 21256ebdcee2SJeff Cody /* if bs is the same as active, then by definition it has no overlay 21266ebdcee2SJeff Cody */ 21276ebdcee2SJeff Cody if (active == bs) { 21286ebdcee2SJeff Cody return NULL; 21296ebdcee2SJeff Cody } 21306ebdcee2SJeff Cody 21316ebdcee2SJeff Cody intermediate = active; 21326ebdcee2SJeff Cody while (intermediate->backing_hd) { 21336ebdcee2SJeff Cody if (intermediate->backing_hd == bs) { 21346ebdcee2SJeff Cody overlay = intermediate; 21356ebdcee2SJeff Cody break; 21366ebdcee2SJeff Cody } 21376ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 21386ebdcee2SJeff Cody } 21396ebdcee2SJeff Cody 21406ebdcee2SJeff Cody return overlay; 21416ebdcee2SJeff Cody } 21426ebdcee2SJeff Cody 21436ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 21446ebdcee2SJeff Cody BlockDriverState *bs; 21456ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 21466ebdcee2SJeff Cody } BlkIntermediateStates; 21476ebdcee2SJeff Cody 21486ebdcee2SJeff Cody 21496ebdcee2SJeff Cody /* 21506ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 21516ebdcee2SJeff Cody * above 'top' to have base as its backing file. 21526ebdcee2SJeff Cody * 21536ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 21546ebdcee2SJeff Cody * information in 'bs' can be properly updated. 21556ebdcee2SJeff Cody * 21566ebdcee2SJeff Cody * E.g., this will convert the following chain: 21576ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 21586ebdcee2SJeff Cody * 21596ebdcee2SJeff Cody * to 21606ebdcee2SJeff Cody * 21616ebdcee2SJeff Cody * bottom <- base <- active 21626ebdcee2SJeff Cody * 21636ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 21646ebdcee2SJeff Cody * 21656ebdcee2SJeff Cody * base <- intermediate <- top <- active 21666ebdcee2SJeff Cody * 21676ebdcee2SJeff Cody * to 21686ebdcee2SJeff Cody * 21696ebdcee2SJeff Cody * base <- active 21706ebdcee2SJeff Cody * 21716ebdcee2SJeff Cody * Error conditions: 21726ebdcee2SJeff Cody * if active == top, that is considered an error 21736ebdcee2SJeff Cody * 21746ebdcee2SJeff Cody */ 21756ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 21766ebdcee2SJeff Cody BlockDriverState *base) 21776ebdcee2SJeff Cody { 21786ebdcee2SJeff Cody BlockDriverState *intermediate; 21796ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 21806ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 21816ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 21826ebdcee2SJeff Cody int ret = -EIO; 21836ebdcee2SJeff Cody 21846ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 21856ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 21866ebdcee2SJeff Cody 21876ebdcee2SJeff Cody if (!top->drv || !base->drv) { 21886ebdcee2SJeff Cody goto exit; 21896ebdcee2SJeff Cody } 21906ebdcee2SJeff Cody 21916ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 21926ebdcee2SJeff Cody 21936ebdcee2SJeff Cody if (new_top_bs == NULL) { 21946ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 21956ebdcee2SJeff Cody goto exit; 21966ebdcee2SJeff Cody } 21976ebdcee2SJeff Cody 21986ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 21996ebdcee2SJeff Cody * to do, no intermediate images */ 22006ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 22016ebdcee2SJeff Cody ret = 0; 22026ebdcee2SJeff Cody goto exit; 22036ebdcee2SJeff Cody } 22046ebdcee2SJeff Cody 22056ebdcee2SJeff Cody intermediate = top; 22066ebdcee2SJeff Cody 22076ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 22086ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 22096ebdcee2SJeff Cody */ 22106ebdcee2SJeff Cody while (intermediate) { 22116ebdcee2SJeff Cody intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); 22126ebdcee2SJeff Cody intermediate_state->bs = intermediate; 22136ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 22146ebdcee2SJeff Cody 22156ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 22166ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 22176ebdcee2SJeff Cody break; 22186ebdcee2SJeff Cody } 22196ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 22206ebdcee2SJeff Cody } 22216ebdcee2SJeff Cody if (base_bs == NULL) { 22226ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 22236ebdcee2SJeff Cody * unravel everything, and exit with error */ 22246ebdcee2SJeff Cody goto exit; 22256ebdcee2SJeff Cody } 22266ebdcee2SJeff Cody 22276ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 22286ebdcee2SJeff Cody ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, 22296ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 22306ebdcee2SJeff Cody if (ret) { 22316ebdcee2SJeff Cody goto exit; 22326ebdcee2SJeff Cody } 22336ebdcee2SJeff Cody new_top_bs->backing_hd = base_bs; 22346ebdcee2SJeff Cody 22356ebdcee2SJeff Cody 22366ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 22376ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 22386ebdcee2SJeff Cody intermediate_state->bs->backing_hd = NULL; 22394f6fd349SFam Zheng bdrv_unref(intermediate_state->bs); 22406ebdcee2SJeff Cody } 22416ebdcee2SJeff Cody ret = 0; 22426ebdcee2SJeff Cody 22436ebdcee2SJeff Cody exit: 22446ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 22456ebdcee2SJeff Cody g_free(intermediate_state); 22466ebdcee2SJeff Cody } 22476ebdcee2SJeff Cody return ret; 22486ebdcee2SJeff Cody } 22496ebdcee2SJeff Cody 22506ebdcee2SJeff Cody 225171d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 225271d0770cSaliguori size_t size) 225371d0770cSaliguori { 225471d0770cSaliguori int64_t len; 225571d0770cSaliguori 225671d0770cSaliguori if (!bdrv_is_inserted(bs)) 225771d0770cSaliguori return -ENOMEDIUM; 225871d0770cSaliguori 225971d0770cSaliguori if (bs->growable) 226071d0770cSaliguori return 0; 226171d0770cSaliguori 226271d0770cSaliguori len = bdrv_getlength(bs); 226371d0770cSaliguori 2264fbb7b4e0SKevin Wolf if (offset < 0) 2265fbb7b4e0SKevin Wolf return -EIO; 2266fbb7b4e0SKevin Wolf 2267fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 226871d0770cSaliguori return -EIO; 226971d0770cSaliguori 227071d0770cSaliguori return 0; 227171d0770cSaliguori } 227271d0770cSaliguori 227371d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 227471d0770cSaliguori int nb_sectors) 227571d0770cSaliguori { 2276eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 2277eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 227871d0770cSaliguori } 227971d0770cSaliguori 22801c9805a3SStefan Hajnoczi typedef struct RwCo { 22811c9805a3SStefan Hajnoczi BlockDriverState *bs; 22821c9805a3SStefan Hajnoczi int64_t sector_num; 22831c9805a3SStefan Hajnoczi int nb_sectors; 22841c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 22851c9805a3SStefan Hajnoczi bool is_write; 22861c9805a3SStefan Hajnoczi int ret; 22874105eaaaSPeter Lieven BdrvRequestFlags flags; 22881c9805a3SStefan Hajnoczi } RwCo; 22891c9805a3SStefan Hajnoczi 22901c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 2291fc01f7e7Sbellard { 22921c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 2293fc01f7e7Sbellard 22941c9805a3SStefan Hajnoczi if (!rwco->is_write) { 22951c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 22964105eaaaSPeter Lieven rwco->nb_sectors, rwco->qiov, 22974105eaaaSPeter Lieven rwco->flags); 22981c9805a3SStefan Hajnoczi } else { 22991c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 23004105eaaaSPeter Lieven rwco->nb_sectors, rwco->qiov, 23014105eaaaSPeter Lieven rwco->flags); 23021c9805a3SStefan Hajnoczi } 23031c9805a3SStefan Hajnoczi } 2304e7a8a783SKevin Wolf 23051c9805a3SStefan Hajnoczi /* 23068d3b1a2dSKevin Wolf * Process a vectored synchronous request using coroutines 23071c9805a3SStefan Hajnoczi */ 23088d3b1a2dSKevin Wolf static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, 23094105eaaaSPeter Lieven QEMUIOVector *qiov, bool is_write, 23104105eaaaSPeter Lieven BdrvRequestFlags flags) 23111c9805a3SStefan Hajnoczi { 23121c9805a3SStefan Hajnoczi Coroutine *co; 23131c9805a3SStefan Hajnoczi RwCo rwco = { 23141c9805a3SStefan Hajnoczi .bs = bs, 23151c9805a3SStefan Hajnoczi .sector_num = sector_num, 23168d3b1a2dSKevin Wolf .nb_sectors = qiov->size >> BDRV_SECTOR_BITS, 23178d3b1a2dSKevin Wolf .qiov = qiov, 23181c9805a3SStefan Hajnoczi .is_write = is_write, 23191c9805a3SStefan Hajnoczi .ret = NOT_DONE, 23204105eaaaSPeter Lieven .flags = flags, 23211c9805a3SStefan Hajnoczi }; 23228d3b1a2dSKevin Wolf assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0); 23231c9805a3SStefan Hajnoczi 2324498e386cSZhi Yong Wu /** 2325498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 2326498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 2327498e386cSZhi Yong Wu * if it has been enabled. 2328498e386cSZhi Yong Wu */ 2329498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 2330498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 2331498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 2332498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 2333498e386cSZhi Yong Wu } 2334498e386cSZhi Yong Wu 23351c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 23361c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 23371c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 23381c9805a3SStefan Hajnoczi } else { 23391c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 23401c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 23411c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 23421c9805a3SStefan Hajnoczi qemu_aio_wait(); 23431c9805a3SStefan Hajnoczi } 23441c9805a3SStefan Hajnoczi } 23451c9805a3SStefan Hajnoczi return rwco.ret; 2346e7a8a783SKevin Wolf } 2347e7a8a783SKevin Wolf 23488d3b1a2dSKevin Wolf /* 23498d3b1a2dSKevin Wolf * Process a synchronous request using coroutines 23508d3b1a2dSKevin Wolf */ 23518d3b1a2dSKevin Wolf static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 23524105eaaaSPeter Lieven int nb_sectors, bool is_write, BdrvRequestFlags flags) 23538d3b1a2dSKevin Wolf { 23548d3b1a2dSKevin Wolf QEMUIOVector qiov; 23558d3b1a2dSKevin Wolf struct iovec iov = { 23568d3b1a2dSKevin Wolf .iov_base = (void *)buf, 23578d3b1a2dSKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 23588d3b1a2dSKevin Wolf }; 23598d3b1a2dSKevin Wolf 23608d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 23614105eaaaSPeter Lieven return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags); 23628d3b1a2dSKevin Wolf } 23638d3b1a2dSKevin Wolf 23641c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 23651c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 23661c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 23671c9805a3SStefan Hajnoczi { 23684105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0); 236983f64091Sbellard } 2370fc01f7e7Sbellard 237107d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 237207d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 237307d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 237407d27a44SMarkus Armbruster { 237507d27a44SMarkus Armbruster bool enabled; 237607d27a44SMarkus Armbruster int ret; 237707d27a44SMarkus Armbruster 237807d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 237907d27a44SMarkus Armbruster bs->io_limits_enabled = false; 23804e7395e8SPeter Lieven ret = bdrv_read(bs, sector_num, buf, nb_sectors); 238107d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 238207d27a44SMarkus Armbruster return ret; 238307d27a44SMarkus Armbruster } 238407d27a44SMarkus Armbruster 238519cb3738Sbellard /* Return < 0 if error. Important errors are: 238619cb3738Sbellard -EIO generic I/O error (may happen for all errors) 238719cb3738Sbellard -ENOMEDIUM No media inserted. 238819cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 238919cb3738Sbellard -EACCES Trying to write a read-only device 239019cb3738Sbellard */ 2391fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 2392fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 2393fc01f7e7Sbellard { 23944105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0); 239583f64091Sbellard } 239683f64091Sbellard 23978d3b1a2dSKevin Wolf int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov) 23988d3b1a2dSKevin Wolf { 23994105eaaaSPeter Lieven return bdrv_rwv_co(bs, sector_num, qiov, true, 0); 24004105eaaaSPeter Lieven } 24014105eaaaSPeter Lieven 2402aa7bfbffSPeter Lieven int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, 2403aa7bfbffSPeter Lieven int nb_sectors, BdrvRequestFlags flags) 24044105eaaaSPeter Lieven { 24054105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true, 2406aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 24078d3b1a2dSKevin Wolf } 24088d3b1a2dSKevin Wolf 2409d75cbb5eSPeter Lieven /* 2410d75cbb5eSPeter Lieven * Completely zero out a block device with the help of bdrv_write_zeroes. 2411d75cbb5eSPeter Lieven * The operation is sped up by checking the block status and only writing 2412d75cbb5eSPeter Lieven * zeroes to the device if they currently do not return zeroes. Optional 2413d75cbb5eSPeter Lieven * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP). 2414d75cbb5eSPeter Lieven * 2415d75cbb5eSPeter Lieven * Returns < 0 on error, 0 on success. For error codes see bdrv_write(). 2416d75cbb5eSPeter Lieven */ 2417d75cbb5eSPeter Lieven int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) 2418d75cbb5eSPeter Lieven { 2419d75cbb5eSPeter Lieven int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE; 2420d75cbb5eSPeter Lieven int64_t ret, nb_sectors, sector_num = 0; 2421d75cbb5eSPeter Lieven int n; 2422d75cbb5eSPeter Lieven 2423d75cbb5eSPeter Lieven for (;;) { 2424d75cbb5eSPeter Lieven nb_sectors = target_size - sector_num; 2425d75cbb5eSPeter Lieven if (nb_sectors <= 0) { 2426d75cbb5eSPeter Lieven return 0; 2427d75cbb5eSPeter Lieven } 2428d75cbb5eSPeter Lieven if (nb_sectors > INT_MAX) { 2429d75cbb5eSPeter Lieven nb_sectors = INT_MAX; 2430d75cbb5eSPeter Lieven } 2431d75cbb5eSPeter Lieven ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n); 2432d75cbb5eSPeter Lieven if (ret & BDRV_BLOCK_ZERO) { 2433d75cbb5eSPeter Lieven sector_num += n; 2434d75cbb5eSPeter Lieven continue; 2435d75cbb5eSPeter Lieven } 2436d75cbb5eSPeter Lieven ret = bdrv_write_zeroes(bs, sector_num, n, flags); 2437d75cbb5eSPeter Lieven if (ret < 0) { 2438d75cbb5eSPeter Lieven error_report("error writing zeroes at sector %" PRId64 ": %s", 2439d75cbb5eSPeter Lieven sector_num, strerror(-ret)); 2440d75cbb5eSPeter Lieven return ret; 2441d75cbb5eSPeter Lieven } 2442d75cbb5eSPeter Lieven sector_num += n; 2443d75cbb5eSPeter Lieven } 2444d75cbb5eSPeter Lieven } 2445d75cbb5eSPeter Lieven 2446eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 2447eda578e5Saliguori void *buf, int count1) 244883f64091Sbellard { 24496ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 245083f64091Sbellard int len, nb_sectors, count; 245183f64091Sbellard int64_t sector_num; 24529a8c4cceSKevin Wolf int ret; 245383f64091Sbellard 245483f64091Sbellard count = count1; 245583f64091Sbellard /* first read to align to sector start */ 24566ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 245783f64091Sbellard if (len > count) 245883f64091Sbellard len = count; 24596ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 246083f64091Sbellard if (len > 0) { 24619a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 24629a8c4cceSKevin Wolf return ret; 24636ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 246483f64091Sbellard count -= len; 246583f64091Sbellard if (count == 0) 246683f64091Sbellard return count1; 246783f64091Sbellard sector_num++; 246883f64091Sbellard buf += len; 246983f64091Sbellard } 247083f64091Sbellard 247183f64091Sbellard /* read the sectors "in place" */ 24726ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 247383f64091Sbellard if (nb_sectors > 0) { 24749a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 24759a8c4cceSKevin Wolf return ret; 247683f64091Sbellard sector_num += nb_sectors; 24776ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 247883f64091Sbellard buf += len; 247983f64091Sbellard count -= len; 248083f64091Sbellard } 248183f64091Sbellard 248283f64091Sbellard /* add data from the last sector */ 248383f64091Sbellard if (count > 0) { 24849a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 24859a8c4cceSKevin Wolf return ret; 248683f64091Sbellard memcpy(buf, tmp_buf, count); 248783f64091Sbellard } 248883f64091Sbellard return count1; 248983f64091Sbellard } 249083f64091Sbellard 24918d3b1a2dSKevin Wolf int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov) 249283f64091Sbellard { 24936ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 249483f64091Sbellard int len, nb_sectors, count; 249583f64091Sbellard int64_t sector_num; 24969a8c4cceSKevin Wolf int ret; 249783f64091Sbellard 24988d3b1a2dSKevin Wolf count = qiov->size; 24998d3b1a2dSKevin Wolf 250083f64091Sbellard /* first write to align to sector start */ 25016ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 250283f64091Sbellard if (len > count) 250383f64091Sbellard len = count; 25046ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 250583f64091Sbellard if (len > 0) { 25069a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 25079a8c4cceSKevin Wolf return ret; 25088d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), 25098d3b1a2dSKevin Wolf len); 25109a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 25119a8c4cceSKevin Wolf return ret; 251283f64091Sbellard count -= len; 251383f64091Sbellard if (count == 0) 25148d3b1a2dSKevin Wolf return qiov->size; 251583f64091Sbellard sector_num++; 251683f64091Sbellard } 251783f64091Sbellard 251883f64091Sbellard /* write the sectors "in place" */ 25196ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 252083f64091Sbellard if (nb_sectors > 0) { 25218d3b1a2dSKevin Wolf QEMUIOVector qiov_inplace; 25228d3b1a2dSKevin Wolf 25238d3b1a2dSKevin Wolf qemu_iovec_init(&qiov_inplace, qiov->niov); 25248d3b1a2dSKevin Wolf qemu_iovec_concat(&qiov_inplace, qiov, len, 25258d3b1a2dSKevin Wolf nb_sectors << BDRV_SECTOR_BITS); 25268d3b1a2dSKevin Wolf ret = bdrv_writev(bs, sector_num, &qiov_inplace); 25278d3b1a2dSKevin Wolf qemu_iovec_destroy(&qiov_inplace); 25288d3b1a2dSKevin Wolf if (ret < 0) { 25299a8c4cceSKevin Wolf return ret; 25308d3b1a2dSKevin Wolf } 25318d3b1a2dSKevin Wolf 253283f64091Sbellard sector_num += nb_sectors; 25336ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 253483f64091Sbellard count -= len; 253583f64091Sbellard } 253683f64091Sbellard 253783f64091Sbellard /* add data from the last sector */ 253883f64091Sbellard if (count > 0) { 25399a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 25409a8c4cceSKevin Wolf return ret; 25418d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count); 25429a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 25439a8c4cceSKevin Wolf return ret; 254483f64091Sbellard } 25458d3b1a2dSKevin Wolf return qiov->size; 25468d3b1a2dSKevin Wolf } 25478d3b1a2dSKevin Wolf 25488d3b1a2dSKevin Wolf int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 25498d3b1a2dSKevin Wolf const void *buf, int count1) 25508d3b1a2dSKevin Wolf { 25518d3b1a2dSKevin Wolf QEMUIOVector qiov; 25528d3b1a2dSKevin Wolf struct iovec iov = { 25538d3b1a2dSKevin Wolf .iov_base = (void *) buf, 25548d3b1a2dSKevin Wolf .iov_len = count1, 25558d3b1a2dSKevin Wolf }; 25568d3b1a2dSKevin Wolf 25578d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 25588d3b1a2dSKevin Wolf return bdrv_pwritev(bs, offset, &qiov); 255983f64091Sbellard } 256083f64091Sbellard 2561f08145feSKevin Wolf /* 2562f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 2563f08145feSKevin Wolf * request (acts as a barrier) 2564f08145feSKevin Wolf * 2565f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 2566f08145feSKevin Wolf */ 2567f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 2568f08145feSKevin Wolf const void *buf, int count) 2569f08145feSKevin Wolf { 2570f08145feSKevin Wolf int ret; 2571f08145feSKevin Wolf 2572f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 2573f08145feSKevin Wolf if (ret < 0) { 2574f08145feSKevin Wolf return ret; 2575f08145feSKevin Wolf } 2576f08145feSKevin Wolf 2577f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 2578f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 2579f08145feSKevin Wolf bdrv_flush(bs); 2580f08145feSKevin Wolf } 2581f08145feSKevin Wolf 2582f08145feSKevin Wolf return 0; 2583f08145feSKevin Wolf } 2584f08145feSKevin Wolf 2585470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 2586ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2587ab185921SStefan Hajnoczi { 2588ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 2589ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 2590ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 2591ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 2592ab185921SStefan Hajnoczi */ 2593ab185921SStefan Hajnoczi void *bounce_buffer; 2594ab185921SStefan Hajnoczi 259579c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 2596ab185921SStefan Hajnoczi struct iovec iov; 2597ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 2598ab185921SStefan Hajnoczi int64_t cluster_sector_num; 2599ab185921SStefan Hajnoczi int cluster_nb_sectors; 2600ab185921SStefan Hajnoczi size_t skip_bytes; 2601ab185921SStefan Hajnoczi int ret; 2602ab185921SStefan Hajnoczi 2603ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 2604ab185921SStefan Hajnoczi * allocating cluster in the image file. 2605ab185921SStefan Hajnoczi */ 2606343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2607ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2608ab185921SStefan Hajnoczi 2609470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 2610ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 2611ab185921SStefan Hajnoczi 2612ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 2613ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 2614ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 2615ab185921SStefan Hajnoczi 261679c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 2617ab185921SStefan Hajnoczi &bounce_qiov); 2618ab185921SStefan Hajnoczi if (ret < 0) { 2619ab185921SStefan Hajnoczi goto err; 2620ab185921SStefan Hajnoczi } 2621ab185921SStefan Hajnoczi 262279c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 262379c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 2624621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 2625aa7bfbffSPeter Lieven cluster_nb_sectors, 0); 262679c053bdSStefan Hajnoczi } else { 2627f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 2628f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 2629f05fa4adSPaolo Bonzini */ 263079c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 2631ab185921SStefan Hajnoczi &bounce_qiov); 263279c053bdSStefan Hajnoczi } 263379c053bdSStefan Hajnoczi 2634ab185921SStefan Hajnoczi if (ret < 0) { 2635ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 2636ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 2637ab185921SStefan Hajnoczi * Simply report it in all cases. 2638ab185921SStefan Hajnoczi */ 2639ab185921SStefan Hajnoczi goto err; 2640ab185921SStefan Hajnoczi } 2641ab185921SStefan Hajnoczi 2642ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 264303396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 2644ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 2645ab185921SStefan Hajnoczi 2646ab185921SStefan Hajnoczi err: 2647ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 2648ab185921SStefan Hajnoczi return ret; 2649ab185921SStefan Hajnoczi } 2650ab185921SStefan Hajnoczi 2651c5fbe571SStefan Hajnoczi /* 2652c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 2653c5fbe571SStefan Hajnoczi */ 2654c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 2655470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2656470c0504SStefan Hajnoczi BdrvRequestFlags flags) 2657da1fa91dSKevin Wolf { 2658da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 2659dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 2660dbffbdcfSStefan Hajnoczi int ret; 2661da1fa91dSKevin Wolf 2662da1fa91dSKevin Wolf if (!drv) { 2663da1fa91dSKevin Wolf return -ENOMEDIUM; 2664da1fa91dSKevin Wolf } 2665da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2666da1fa91dSKevin Wolf return -EIO; 2667da1fa91dSKevin Wolf } 2668da1fa91dSKevin Wolf 2669f4658285SStefan Hajnoczi if (bs->copy_on_read) { 2670470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 2671470c0504SStefan Hajnoczi } 2672470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2673470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 2674470c0504SStefan Hajnoczi } 2675470c0504SStefan Hajnoczi 2676470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2677f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2678f4658285SStefan Hajnoczi } 2679f4658285SStefan Hajnoczi 2680cc0681c4SBenoît Canet /* throttling disk I/O */ 2681cc0681c4SBenoît Canet if (bs->io_limits_enabled) { 2682cc0681c4SBenoît Canet bdrv_io_limits_intercept(bs, nb_sectors, false); 2683cc0681c4SBenoît Canet } 2684cc0681c4SBenoît Canet 2685dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 2686ab185921SStefan Hajnoczi 2687470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2688ab185921SStefan Hajnoczi int pnum; 2689ab185921SStefan Hajnoczi 2690bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum); 2691ab185921SStefan Hajnoczi if (ret < 0) { 2692ab185921SStefan Hajnoczi goto out; 2693ab185921SStefan Hajnoczi } 2694ab185921SStefan Hajnoczi 2695ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 2696470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 2697ab185921SStefan Hajnoczi goto out; 2698ab185921SStefan Hajnoczi } 2699ab185921SStefan Hajnoczi } 2700ab185921SStefan Hajnoczi 2701893a8f62SMORITA Kazutaka if (!(bs->zero_beyond_eof && bs->growable)) { 2702dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 2703893a8f62SMORITA Kazutaka } else { 2704893a8f62SMORITA Kazutaka /* Read zeros after EOF of growable BDSes */ 2705893a8f62SMORITA Kazutaka int64_t len, total_sectors, max_nb_sectors; 2706893a8f62SMORITA Kazutaka 2707893a8f62SMORITA Kazutaka len = bdrv_getlength(bs); 2708893a8f62SMORITA Kazutaka if (len < 0) { 2709893a8f62SMORITA Kazutaka ret = len; 2710893a8f62SMORITA Kazutaka goto out; 2711893a8f62SMORITA Kazutaka } 2712893a8f62SMORITA Kazutaka 2713d055a1feSFam Zheng total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE); 2714893a8f62SMORITA Kazutaka max_nb_sectors = MAX(0, total_sectors - sector_num); 2715893a8f62SMORITA Kazutaka if (max_nb_sectors > 0) { 2716893a8f62SMORITA Kazutaka ret = drv->bdrv_co_readv(bs, sector_num, 2717893a8f62SMORITA Kazutaka MIN(nb_sectors, max_nb_sectors), qiov); 2718893a8f62SMORITA Kazutaka } else { 2719893a8f62SMORITA Kazutaka ret = 0; 2720893a8f62SMORITA Kazutaka } 2721893a8f62SMORITA Kazutaka 2722893a8f62SMORITA Kazutaka /* Reading beyond end of file is supposed to produce zeroes */ 2723893a8f62SMORITA Kazutaka if (ret == 0 && total_sectors < sector_num + nb_sectors) { 2724893a8f62SMORITA Kazutaka uint64_t offset = MAX(0, total_sectors - sector_num); 2725893a8f62SMORITA Kazutaka uint64_t bytes = (sector_num + nb_sectors - offset) * 2726893a8f62SMORITA Kazutaka BDRV_SECTOR_SIZE; 2727893a8f62SMORITA Kazutaka qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes); 2728893a8f62SMORITA Kazutaka } 2729893a8f62SMORITA Kazutaka } 2730ab185921SStefan Hajnoczi 2731ab185921SStefan Hajnoczi out: 2732dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2733470c0504SStefan Hajnoczi 2734470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2735470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 2736470c0504SStefan Hajnoczi } 2737470c0504SStefan Hajnoczi 2738dbffbdcfSStefan Hajnoczi return ret; 2739da1fa91dSKevin Wolf } 2740da1fa91dSKevin Wolf 2741c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 2742da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 2743da1fa91dSKevin Wolf { 2744c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 2745da1fa91dSKevin Wolf 2746470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 2747470c0504SStefan Hajnoczi } 2748470c0504SStefan Hajnoczi 2749470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 2750470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2751470c0504SStefan Hajnoczi { 2752470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 2753470c0504SStefan Hajnoczi 2754470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 2755470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 2756c5fbe571SStefan Hajnoczi } 2757c5fbe571SStefan Hajnoczi 2758c31cb707SPeter Lieven /* if no limit is specified in the BlockLimits use a default 2759c31cb707SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 2760c31cb707SPeter Lieven */ 2761c31cb707SPeter Lieven #define MAX_WRITE_ZEROES_DEFAULT 32768 2762c31cb707SPeter Lieven 2763f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 2764aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) 2765f08f2ddaSStefan Hajnoczi { 2766f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 2767f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 2768c31cb707SPeter Lieven struct iovec iov = {0}; 2769c31cb707SPeter Lieven int ret = 0; 2770f08f2ddaSStefan Hajnoczi 2771c31cb707SPeter Lieven int max_write_zeroes = bs->bl.max_write_zeroes ? 2772c31cb707SPeter Lieven bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT; 2773621f0589SKevin Wolf 2774c31cb707SPeter Lieven while (nb_sectors > 0 && !ret) { 2775c31cb707SPeter Lieven int num = nb_sectors; 2776c31cb707SPeter Lieven 2777c31cb707SPeter Lieven /* align request */ 2778c31cb707SPeter Lieven if (bs->bl.write_zeroes_alignment && 2779c31cb707SPeter Lieven num >= bs->bl.write_zeroes_alignment && 2780c31cb707SPeter Lieven sector_num % bs->bl.write_zeroes_alignment) { 2781c31cb707SPeter Lieven if (num > bs->bl.write_zeroes_alignment) { 2782c31cb707SPeter Lieven num = bs->bl.write_zeroes_alignment; 2783c31cb707SPeter Lieven } 2784c31cb707SPeter Lieven num -= sector_num % bs->bl.write_zeroes_alignment; 2785c31cb707SPeter Lieven } 2786c31cb707SPeter Lieven 2787c31cb707SPeter Lieven /* limit request size */ 2788c31cb707SPeter Lieven if (num > max_write_zeroes) { 2789c31cb707SPeter Lieven num = max_write_zeroes; 2790c31cb707SPeter Lieven } 2791c31cb707SPeter Lieven 2792c31cb707SPeter Lieven ret = -ENOTSUP; 2793f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 2794f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 2795c31cb707SPeter Lieven ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags); 2796f08f2ddaSStefan Hajnoczi } 2797f08f2ddaSStefan Hajnoczi 2798c31cb707SPeter Lieven if (ret == -ENOTSUP) { 2799f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 2800c31cb707SPeter Lieven iov.iov_len = num * BDRV_SECTOR_SIZE; 2801c31cb707SPeter Lieven if (iov.iov_base == NULL) { 2802c31cb707SPeter Lieven /* allocate bounce buffer only once and ensure that it 2803c31cb707SPeter Lieven * is big enough for this and all future requests. 2804c31cb707SPeter Lieven */ 2805c31cb707SPeter Lieven size_t bufsize = num <= nb_sectors ? num : max_write_zeroes; 2806c31cb707SPeter Lieven iov.iov_base = qemu_blockalign(bs, bufsize * BDRV_SECTOR_SIZE); 2807c31cb707SPeter Lieven memset(iov.iov_base, 0, bufsize * BDRV_SECTOR_SIZE); 2808c31cb707SPeter Lieven } 2809f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 2810f08f2ddaSStefan Hajnoczi 2811c31cb707SPeter Lieven ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov); 2812c31cb707SPeter Lieven } 2813c31cb707SPeter Lieven 2814c31cb707SPeter Lieven sector_num += num; 2815c31cb707SPeter Lieven nb_sectors -= num; 2816c31cb707SPeter Lieven } 2817f08f2ddaSStefan Hajnoczi 2818f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 2819f08f2ddaSStefan Hajnoczi return ret; 2820f08f2ddaSStefan Hajnoczi } 2821f08f2ddaSStefan Hajnoczi 2822c5fbe571SStefan Hajnoczi /* 2823c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 2824c5fbe571SStefan Hajnoczi */ 2825c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 2826f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2827f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 2828c5fbe571SStefan Hajnoczi { 2829c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 2830dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 28316b7cb247SStefan Hajnoczi int ret; 2832da1fa91dSKevin Wolf 2833da1fa91dSKevin Wolf if (!bs->drv) { 2834da1fa91dSKevin Wolf return -ENOMEDIUM; 2835da1fa91dSKevin Wolf } 2836da1fa91dSKevin Wolf if (bs->read_only) { 2837da1fa91dSKevin Wolf return -EACCES; 2838da1fa91dSKevin Wolf } 2839da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2840da1fa91dSKevin Wolf return -EIO; 2841da1fa91dSKevin Wolf } 2842da1fa91dSKevin Wolf 2843470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2844f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2845f4658285SStefan Hajnoczi } 2846f4658285SStefan Hajnoczi 2847cc0681c4SBenoît Canet /* throttling disk I/O */ 2848cc0681c4SBenoît Canet if (bs->io_limits_enabled) { 2849cc0681c4SBenoît Canet bdrv_io_limits_intercept(bs, nb_sectors, true); 2850cc0681c4SBenoît Canet } 2851cc0681c4SBenoît Canet 2852dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 2853dbffbdcfSStefan Hajnoczi 2854d616b224SStefan Hajnoczi ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); 2855d616b224SStefan Hajnoczi 2856d616b224SStefan Hajnoczi if (ret < 0) { 2857d616b224SStefan Hajnoczi /* Do nothing, write notifier decided to fail this request */ 2858d616b224SStefan Hajnoczi } else if (flags & BDRV_REQ_ZERO_WRITE) { 2859aa7bfbffSPeter Lieven ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags); 2860f08f2ddaSStefan Hajnoczi } else { 28616b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 2862f08f2ddaSStefan Hajnoczi } 28636b7cb247SStefan Hajnoczi 2864f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 2865f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 2866f05fa4adSPaolo Bonzini } 2867f05fa4adSPaolo Bonzini 28681755da16SPaolo Bonzini bdrv_set_dirty(bs, sector_num, nb_sectors); 2869da1fa91dSKevin Wolf 2870da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2871da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2872da1fa91dSKevin Wolf } 2873df2a6f29SPaolo Bonzini if (bs->growable && ret >= 0) { 2874df2a6f29SPaolo Bonzini bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors); 2875df2a6f29SPaolo Bonzini } 2876da1fa91dSKevin Wolf 2877dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2878dbffbdcfSStefan Hajnoczi 28796b7cb247SStefan Hajnoczi return ret; 2880da1fa91dSKevin Wolf } 2881da1fa91dSKevin Wolf 2882c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 2883c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 2884c5fbe571SStefan Hajnoczi { 2885c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 2886c5fbe571SStefan Hajnoczi 2887f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 2888f08f2ddaSStefan Hajnoczi } 2889f08f2ddaSStefan Hajnoczi 2890f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 2891aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, 2892aa7bfbffSPeter Lieven BdrvRequestFlags flags) 2893f08f2ddaSStefan Hajnoczi { 2894f08f2ddaSStefan Hajnoczi trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors); 2895f08f2ddaSStefan Hajnoczi 2896d32f35cbSPeter Lieven if (!(bs->open_flags & BDRV_O_UNMAP)) { 2897d32f35cbSPeter Lieven flags &= ~BDRV_REQ_MAY_UNMAP; 2898d32f35cbSPeter Lieven } 2899d32f35cbSPeter Lieven 2900f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 2901aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 2902c5fbe571SStefan Hajnoczi } 2903c5fbe571SStefan Hajnoczi 290483f64091Sbellard /** 290583f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 290683f64091Sbellard */ 290783f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 290883f64091Sbellard { 290983f64091Sbellard BlockDriver *drv = bs->drv; 291051762288SStefan Hajnoczi int ret; 291183f64091Sbellard if (!drv) 291219cb3738Sbellard return -ENOMEDIUM; 291383f64091Sbellard if (!drv->bdrv_truncate) 291483f64091Sbellard return -ENOTSUP; 291559f2689dSNaphtali Sprei if (bs->read_only) 291659f2689dSNaphtali Sprei return -EACCES; 29178591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 29188591675fSMarcelo Tosatti return -EBUSY; 291951762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 292051762288SStefan Hajnoczi if (ret == 0) { 292151762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 2922145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 292351762288SStefan Hajnoczi } 292451762288SStefan Hajnoczi return ret; 292583f64091Sbellard } 292683f64091Sbellard 292783f64091Sbellard /** 29284a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 29294a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 29304a1d5e1fSFam Zheng */ 29314a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 29324a1d5e1fSFam Zheng { 29334a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 29344a1d5e1fSFam Zheng if (!drv) { 29354a1d5e1fSFam Zheng return -ENOMEDIUM; 29364a1d5e1fSFam Zheng } 29374a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 29384a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 29394a1d5e1fSFam Zheng } 29404a1d5e1fSFam Zheng if (bs->file) { 29414a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 29424a1d5e1fSFam Zheng } 29434a1d5e1fSFam Zheng return -ENOTSUP; 29444a1d5e1fSFam Zheng } 29454a1d5e1fSFam Zheng 29464a1d5e1fSFam Zheng /** 294783f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 294883f64091Sbellard */ 294983f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 295083f64091Sbellard { 295183f64091Sbellard BlockDriver *drv = bs->drv; 295283f64091Sbellard if (!drv) 295319cb3738Sbellard return -ENOMEDIUM; 295451762288SStefan Hajnoczi 2955b94a2610SKevin Wolf if (drv->has_variable_length) { 2956b94a2610SKevin Wolf int ret = refresh_total_sectors(bs, bs->total_sectors); 2957b94a2610SKevin Wolf if (ret < 0) { 2958b94a2610SKevin Wolf return ret; 2959fc01f7e7Sbellard } 296046a4e4e6SStefan Hajnoczi } 296146a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 296246a4e4e6SStefan Hajnoczi } 2963fc01f7e7Sbellard 296419cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 296596b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 2966fc01f7e7Sbellard { 296719cb3738Sbellard int64_t length; 296819cb3738Sbellard length = bdrv_getlength(bs); 296919cb3738Sbellard if (length < 0) 297019cb3738Sbellard length = 0; 297119cb3738Sbellard else 29726ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 297319cb3738Sbellard *nb_sectors_ptr = length; 2974fc01f7e7Sbellard } 2975cf98951bSbellard 2976ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 2977ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 2978abd7f68dSMarkus Armbruster { 2979abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 2980abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 2981abd7f68dSMarkus Armbruster } 2982abd7f68dSMarkus Armbruster 29831ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 2984abd7f68dSMarkus Armbruster { 2985abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 2986abd7f68dSMarkus Armbruster } 2987abd7f68dSMarkus Armbruster 29883e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 29893e1caa5fSPaolo Bonzini { 29903e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 29913e1caa5fSPaolo Bonzini 29923e1caa5fSPaolo Bonzini switch (on_err) { 29933e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 29943e1caa5fSPaolo Bonzini return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; 29953e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 29963e1caa5fSPaolo Bonzini return BDRV_ACTION_STOP; 29973e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 29983e1caa5fSPaolo Bonzini return BDRV_ACTION_REPORT; 29993e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 30003e1caa5fSPaolo Bonzini return BDRV_ACTION_IGNORE; 30013e1caa5fSPaolo Bonzini default: 30023e1caa5fSPaolo Bonzini abort(); 30033e1caa5fSPaolo Bonzini } 30043e1caa5fSPaolo Bonzini } 30053e1caa5fSPaolo Bonzini 30063e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 30073e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 30083e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 30093e1caa5fSPaolo Bonzini */ 30103e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 30113e1caa5fSPaolo Bonzini bool is_read, int error) 30123e1caa5fSPaolo Bonzini { 30133e1caa5fSPaolo Bonzini assert(error >= 0); 301432c81a4aSPaolo Bonzini bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); 30153e1caa5fSPaolo Bonzini if (action == BDRV_ACTION_STOP) { 30163e1caa5fSPaolo Bonzini vm_stop(RUN_STATE_IO_ERROR); 30173e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 30183e1caa5fSPaolo Bonzini } 30193e1caa5fSPaolo Bonzini } 30203e1caa5fSPaolo Bonzini 3021b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 3022b338082bSbellard { 3023b338082bSbellard return bs->read_only; 3024b338082bSbellard } 3025b338082bSbellard 3026985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 3027985a03b0Sths { 3028985a03b0Sths return bs->sg; 3029985a03b0Sths } 3030985a03b0Sths 3031e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 3032e900a7b7SChristoph Hellwig { 3033e900a7b7SChristoph Hellwig return bs->enable_write_cache; 3034e900a7b7SChristoph Hellwig } 3035e900a7b7SChristoph Hellwig 3036425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 3037425b0148SPaolo Bonzini { 3038425b0148SPaolo Bonzini bs->enable_write_cache = wce; 303955b110f2SJeff Cody 304055b110f2SJeff Cody /* so a reopen() will preserve wce */ 304155b110f2SJeff Cody if (wce) { 304255b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 304355b110f2SJeff Cody } else { 304455b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 304555b110f2SJeff Cody } 3046425b0148SPaolo Bonzini } 3047425b0148SPaolo Bonzini 3048ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 3049ea2384d3Sbellard { 3050ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 3051ea2384d3Sbellard return 1; 3052ea2384d3Sbellard return bs->encrypted; 3053ea2384d3Sbellard } 3054ea2384d3Sbellard 3055c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 3056c0f4ce77Saliguori { 3057c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 3058c0f4ce77Saliguori 3059c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 3060c0f4ce77Saliguori return 1; 3061c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 3062c0f4ce77Saliguori } 3063c0f4ce77Saliguori 3064ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 3065ea2384d3Sbellard { 3066ea2384d3Sbellard int ret; 3067ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 3068ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 3069ea2384d3Sbellard if (ret < 0) 3070ea2384d3Sbellard return ret; 3071ea2384d3Sbellard if (!bs->encrypted) 3072ea2384d3Sbellard return 0; 3073ea2384d3Sbellard } 3074fd04a2aeSShahar Havivi if (!bs->encrypted) { 3075fd04a2aeSShahar Havivi return -EINVAL; 3076fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 3077fd04a2aeSShahar Havivi return -ENOMEDIUM; 3078fd04a2aeSShahar Havivi } 3079c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 3080bb5fc20fSaliguori if (ret < 0) { 3081bb5fc20fSaliguori bs->valid_key = 0; 3082bb5fc20fSaliguori } else if (!bs->valid_key) { 3083bb5fc20fSaliguori bs->valid_key = 1; 3084bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 30857d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 3086bb5fc20fSaliguori } 3087c0f4ce77Saliguori return ret; 3088ea2384d3Sbellard } 3089ea2384d3Sbellard 3090f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 3091ea2384d3Sbellard { 3092f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 3093ea2384d3Sbellard } 3094ea2384d3Sbellard 3095ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 3096ea2384d3Sbellard void *opaque) 3097ea2384d3Sbellard { 3098ea2384d3Sbellard BlockDriver *drv; 3099ea2384d3Sbellard 31008a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 3101ea2384d3Sbellard it(opaque, drv->format_name); 3102ea2384d3Sbellard } 3103ea2384d3Sbellard } 3104ea2384d3Sbellard 3105b338082bSbellard BlockDriverState *bdrv_find(const char *name) 3106b338082bSbellard { 3107b338082bSbellard BlockDriverState *bs; 3108b338082bSbellard 31091b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 31101b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 3111b338082bSbellard return bs; 3112b338082bSbellard } 31131b7bdbc1SStefan Hajnoczi } 3114b338082bSbellard return NULL; 3115b338082bSbellard } 3116b338082bSbellard 31172f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 31182f399b0aSMarkus Armbruster { 31192f399b0aSMarkus Armbruster if (!bs) { 31202f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 31212f399b0aSMarkus Armbruster } 31222f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 31232f399b0aSMarkus Armbruster } 31242f399b0aSMarkus Armbruster 312551de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 312681d0912dSbellard { 312781d0912dSbellard BlockDriverState *bs; 312881d0912dSbellard 31291b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 313051de9760Saliguori it(opaque, bs); 313181d0912dSbellard } 313281d0912dSbellard } 313381d0912dSbellard 3134ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 3135ea2384d3Sbellard { 3136ea2384d3Sbellard return bs->device_name; 3137ea2384d3Sbellard } 3138ea2384d3Sbellard 3139c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 3140c8433287SMarkus Armbruster { 3141c8433287SMarkus Armbruster return bs->open_flags; 3142c8433287SMarkus Armbruster } 3143c8433287SMarkus Armbruster 3144f0f0fdfeSKevin Wolf int bdrv_flush_all(void) 3145c6ca28d6Saliguori { 3146c6ca28d6Saliguori BlockDriverState *bs; 3147f0f0fdfeSKevin Wolf int result = 0; 3148c6ca28d6Saliguori 31491b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 3150f0f0fdfeSKevin Wolf int ret = bdrv_flush(bs); 3151f0f0fdfeSKevin Wolf if (ret < 0 && !result) { 3152f0f0fdfeSKevin Wolf result = ret; 3153c6ca28d6Saliguori } 31541b7bdbc1SStefan Hajnoczi } 3155c6ca28d6Saliguori 3156f0f0fdfeSKevin Wolf return result; 3157f0f0fdfeSKevin Wolf } 3158f0f0fdfeSKevin Wolf 31593ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 31603ac21627SPeter Lieven { 31613ac21627SPeter Lieven return 1; 31623ac21627SPeter Lieven } 31633ac21627SPeter Lieven 3164f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 3165f2feebbdSKevin Wolf { 3166f2feebbdSKevin Wolf assert(bs->drv); 3167f2feebbdSKevin Wolf 316811212d8fSPaolo Bonzini /* If BS is a copy on write image, it is initialized to 316911212d8fSPaolo Bonzini the contents of the base image, which may not be zeroes. */ 317011212d8fSPaolo Bonzini if (bs->backing_hd) { 317111212d8fSPaolo Bonzini return 0; 317211212d8fSPaolo Bonzini } 3173336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 3174336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 3175f2feebbdSKevin Wolf } 3176f2feebbdSKevin Wolf 31773ac21627SPeter Lieven /* safe default */ 31783ac21627SPeter Lieven return 0; 3179f2feebbdSKevin Wolf } 3180f2feebbdSKevin Wolf 31814ce78691SPeter Lieven bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) 31824ce78691SPeter Lieven { 31834ce78691SPeter Lieven BlockDriverInfo bdi; 31844ce78691SPeter Lieven 31854ce78691SPeter Lieven if (bs->backing_hd) { 31864ce78691SPeter Lieven return false; 31874ce78691SPeter Lieven } 31884ce78691SPeter Lieven 31894ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 31904ce78691SPeter Lieven return bdi.unallocated_blocks_are_zero; 31914ce78691SPeter Lieven } 31924ce78691SPeter Lieven 31934ce78691SPeter Lieven return false; 31944ce78691SPeter Lieven } 31954ce78691SPeter Lieven 31964ce78691SPeter Lieven bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) 31974ce78691SPeter Lieven { 31984ce78691SPeter Lieven BlockDriverInfo bdi; 31994ce78691SPeter Lieven 32004ce78691SPeter Lieven if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { 32014ce78691SPeter Lieven return false; 32024ce78691SPeter Lieven } 32034ce78691SPeter Lieven 32044ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 32054ce78691SPeter Lieven return bdi.can_write_zeroes_with_unmap; 32064ce78691SPeter Lieven } 32074ce78691SPeter Lieven 32084ce78691SPeter Lieven return false; 32094ce78691SPeter Lieven } 32104ce78691SPeter Lieven 3211b6b8a333SPaolo Bonzini typedef struct BdrvCoGetBlockStatusData { 3212376ae3f1SStefan Hajnoczi BlockDriverState *bs; 3213b35b2bbaSMiroslav Rezanina BlockDriverState *base; 3214376ae3f1SStefan Hajnoczi int64_t sector_num; 3215376ae3f1SStefan Hajnoczi int nb_sectors; 3216376ae3f1SStefan Hajnoczi int *pnum; 3217b6b8a333SPaolo Bonzini int64_t ret; 3218376ae3f1SStefan Hajnoczi bool done; 3219b6b8a333SPaolo Bonzini } BdrvCoGetBlockStatusData; 3220376ae3f1SStefan Hajnoczi 3221f58c7b35Sths /* 3222f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 3223f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 3224f58c7b35Sths * hence all their sectors are reported as allocated. 3225f58c7b35Sths * 3226bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 3227bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 3228bd9533e3SStefan Hajnoczi * 3229f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 3230f58c7b35Sths * the specified sector) that are known to be in the same 3231f58c7b35Sths * allocated/unallocated state. 3232f58c7b35Sths * 3233bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 3234bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 3235f58c7b35Sths */ 3236b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, 3237bdad13b9SPaolo Bonzini int64_t sector_num, 3238060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 3239f58c7b35Sths { 3240617ccb46SPaolo Bonzini int64_t length; 3241f58c7b35Sths int64_t n; 32425daa74a6SPaolo Bonzini int64_t ret, ret2; 3243bd9533e3SStefan Hajnoczi 3244617ccb46SPaolo Bonzini length = bdrv_getlength(bs); 3245617ccb46SPaolo Bonzini if (length < 0) { 3246617ccb46SPaolo Bonzini return length; 3247617ccb46SPaolo Bonzini } 3248617ccb46SPaolo Bonzini 3249617ccb46SPaolo Bonzini if (sector_num >= (length >> BDRV_SECTOR_BITS)) { 32506aebab14SStefan Hajnoczi *pnum = 0; 32516aebab14SStefan Hajnoczi return 0; 32526aebab14SStefan Hajnoczi } 3253bd9533e3SStefan Hajnoczi 32546aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 3255bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 3256bd9533e3SStefan Hajnoczi nb_sectors = n; 3257bd9533e3SStefan Hajnoczi } 3258bd9533e3SStefan Hajnoczi 3259b6b8a333SPaolo Bonzini if (!bs->drv->bdrv_co_get_block_status) { 3260bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 3261918e92d7SPaolo Bonzini ret = BDRV_BLOCK_DATA; 3262918e92d7SPaolo Bonzini if (bs->drv->protocol_name) { 3263918e92d7SPaolo Bonzini ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE); 3264918e92d7SPaolo Bonzini } 3265918e92d7SPaolo Bonzini return ret; 32666aebab14SStefan Hajnoczi } 32676aebab14SStefan Hajnoczi 3268415b5b01SPaolo Bonzini ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum); 3269415b5b01SPaolo Bonzini if (ret < 0) { 32703e0a233dSPeter Lieven *pnum = 0; 3271415b5b01SPaolo Bonzini return ret; 3272415b5b01SPaolo Bonzini } 3273415b5b01SPaolo Bonzini 327492bc50a5SPeter Lieven if (ret & BDRV_BLOCK_RAW) { 327592bc50a5SPeter Lieven assert(ret & BDRV_BLOCK_OFFSET_VALID); 327692bc50a5SPeter Lieven return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 327792bc50a5SPeter Lieven *pnum, pnum); 327892bc50a5SPeter Lieven } 327992bc50a5SPeter Lieven 3280c3d86884SPeter Lieven if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) { 3281c3d86884SPeter Lieven if (bdrv_unallocated_blocks_are_zero(bs)) { 3282415b5b01SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 32831f9db224SPeter Lieven } else if (bs->backing_hd) { 3284f0ad5712SPaolo Bonzini BlockDriverState *bs2 = bs->backing_hd; 3285f0ad5712SPaolo Bonzini int64_t length2 = bdrv_getlength(bs2); 3286f0ad5712SPaolo Bonzini if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) { 3287f0ad5712SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 3288f0ad5712SPaolo Bonzini } 3289f0ad5712SPaolo Bonzini } 3290415b5b01SPaolo Bonzini } 32915daa74a6SPaolo Bonzini 32925daa74a6SPaolo Bonzini if (bs->file && 32935daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && 32945daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_OFFSET_VALID)) { 32955daa74a6SPaolo Bonzini ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 32965daa74a6SPaolo Bonzini *pnum, pnum); 32975daa74a6SPaolo Bonzini if (ret2 >= 0) { 32985daa74a6SPaolo Bonzini /* Ignore errors. This is just providing extra information, it 32995daa74a6SPaolo Bonzini * is useful but not necessary. 33005daa74a6SPaolo Bonzini */ 33015daa74a6SPaolo Bonzini ret |= (ret2 & BDRV_BLOCK_ZERO); 33025daa74a6SPaolo Bonzini } 33035daa74a6SPaolo Bonzini } 33045daa74a6SPaolo Bonzini 3305415b5b01SPaolo Bonzini return ret; 3306060f51c9SStefan Hajnoczi } 3307060f51c9SStefan Hajnoczi 3308b6b8a333SPaolo Bonzini /* Coroutine wrapper for bdrv_get_block_status() */ 3309b6b8a333SPaolo Bonzini static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque) 3310060f51c9SStefan Hajnoczi { 3311b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData *data = opaque; 3312060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 3313060f51c9SStefan Hajnoczi 3314b6b8a333SPaolo Bonzini data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors, 3315060f51c9SStefan Hajnoczi data->pnum); 3316060f51c9SStefan Hajnoczi data->done = true; 3317060f51c9SStefan Hajnoczi } 3318060f51c9SStefan Hajnoczi 3319060f51c9SStefan Hajnoczi /* 3320b6b8a333SPaolo Bonzini * Synchronous wrapper around bdrv_co_get_block_status(). 3321060f51c9SStefan Hajnoczi * 3322b6b8a333SPaolo Bonzini * See bdrv_co_get_block_status() for details. 3323060f51c9SStefan Hajnoczi */ 3324b6b8a333SPaolo Bonzini int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num, 3325b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 3326060f51c9SStefan Hajnoczi { 3327376ae3f1SStefan Hajnoczi Coroutine *co; 3328b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData data = { 3329376ae3f1SStefan Hajnoczi .bs = bs, 3330376ae3f1SStefan Hajnoczi .sector_num = sector_num, 3331376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 3332376ae3f1SStefan Hajnoczi .pnum = pnum, 3333376ae3f1SStefan Hajnoczi .done = false, 3334376ae3f1SStefan Hajnoczi }; 3335376ae3f1SStefan Hajnoczi 3336bdad13b9SPaolo Bonzini if (qemu_in_coroutine()) { 3337bdad13b9SPaolo Bonzini /* Fast-path if already in coroutine context */ 3338b6b8a333SPaolo Bonzini bdrv_get_block_status_co_entry(&data); 3339bdad13b9SPaolo Bonzini } else { 3340b6b8a333SPaolo Bonzini co = qemu_coroutine_create(bdrv_get_block_status_co_entry); 3341376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 3342376ae3f1SStefan Hajnoczi while (!data.done) { 3343376ae3f1SStefan Hajnoczi qemu_aio_wait(); 3344376ae3f1SStefan Hajnoczi } 3345bdad13b9SPaolo Bonzini } 3346376ae3f1SStefan Hajnoczi return data.ret; 3347376ae3f1SStefan Hajnoczi } 3348f58c7b35Sths 3349b6b8a333SPaolo Bonzini int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, 3350b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 3351b6b8a333SPaolo Bonzini { 33524333bb71SPaolo Bonzini int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum); 33534333bb71SPaolo Bonzini if (ret < 0) { 33544333bb71SPaolo Bonzini return ret; 33554333bb71SPaolo Bonzini } 33564333bb71SPaolo Bonzini return 33574333bb71SPaolo Bonzini (ret & BDRV_BLOCK_DATA) || 33584333bb71SPaolo Bonzini ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs)); 3359b6b8a333SPaolo Bonzini } 3360b6b8a333SPaolo Bonzini 3361188a7bbfSPaolo Bonzini /* 3362188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 3363188a7bbfSPaolo Bonzini * 3364188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 3365188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 3366188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 3367188a7bbfSPaolo Bonzini * 3368188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 3369188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 3370188a7bbfSPaolo Bonzini * allocated/unallocated state. 3371188a7bbfSPaolo Bonzini * 3372188a7bbfSPaolo Bonzini */ 33734f578637SPaolo Bonzini int bdrv_is_allocated_above(BlockDriverState *top, 3374188a7bbfSPaolo Bonzini BlockDriverState *base, 3375188a7bbfSPaolo Bonzini int64_t sector_num, 3376188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 3377188a7bbfSPaolo Bonzini { 3378188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 3379188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 3380188a7bbfSPaolo Bonzini 3381188a7bbfSPaolo Bonzini intermediate = top; 3382188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 3383188a7bbfSPaolo Bonzini int pnum_inter; 3384bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors, 3385188a7bbfSPaolo Bonzini &pnum_inter); 3386188a7bbfSPaolo Bonzini if (ret < 0) { 3387188a7bbfSPaolo Bonzini return ret; 3388188a7bbfSPaolo Bonzini } else if (ret) { 3389188a7bbfSPaolo Bonzini *pnum = pnum_inter; 3390188a7bbfSPaolo Bonzini return 1; 3391188a7bbfSPaolo Bonzini } 3392188a7bbfSPaolo Bonzini 3393188a7bbfSPaolo Bonzini /* 3394188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 3395188a7bbfSPaolo Bonzini * might have 3396188a7bbfSPaolo Bonzini * 3397188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 3398188a7bbfSPaolo Bonzini */ 339963ba17d3SVishvananda Ishaya if (n > pnum_inter && 340063ba17d3SVishvananda Ishaya (intermediate == top || 340163ba17d3SVishvananda Ishaya sector_num + pnum_inter < intermediate->total_sectors)) { 3402188a7bbfSPaolo Bonzini n = pnum_inter; 3403188a7bbfSPaolo Bonzini } 3404188a7bbfSPaolo Bonzini 3405188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 3406188a7bbfSPaolo Bonzini } 3407188a7bbfSPaolo Bonzini 3408188a7bbfSPaolo Bonzini *pnum = n; 3409188a7bbfSPaolo Bonzini return 0; 3410188a7bbfSPaolo Bonzini } 3411188a7bbfSPaolo Bonzini 3412045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 3413045df330Saliguori { 3414045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 3415045df330Saliguori return bs->backing_file; 3416045df330Saliguori else if (bs->encrypted) 3417045df330Saliguori return bs->filename; 3418045df330Saliguori else 3419045df330Saliguori return NULL; 3420045df330Saliguori } 3421045df330Saliguori 342283f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 342383f64091Sbellard char *filename, int filename_size) 342483f64091Sbellard { 342583f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 342683f64091Sbellard } 342783f64091Sbellard 3428faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 3429faea38e7Sbellard const uint8_t *buf, int nb_sectors) 3430faea38e7Sbellard { 3431faea38e7Sbellard BlockDriver *drv = bs->drv; 3432faea38e7Sbellard if (!drv) 343319cb3738Sbellard return -ENOMEDIUM; 3434faea38e7Sbellard if (!drv->bdrv_write_compressed) 3435faea38e7Sbellard return -ENOTSUP; 3436fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 3437fbb7b4e0SKevin Wolf return -EIO; 34387cd1e32aSlirans@il.ibm.com 3439e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 34407cd1e32aSlirans@il.ibm.com 3441faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 3442faea38e7Sbellard } 3443faea38e7Sbellard 3444faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 3445faea38e7Sbellard { 3446faea38e7Sbellard BlockDriver *drv = bs->drv; 3447faea38e7Sbellard if (!drv) 344819cb3738Sbellard return -ENOMEDIUM; 3449faea38e7Sbellard if (!drv->bdrv_get_info) 3450faea38e7Sbellard return -ENOTSUP; 3451faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 3452faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 3453faea38e7Sbellard } 3454faea38e7Sbellard 3455eae041feSMax Reitz ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) 3456eae041feSMax Reitz { 3457eae041feSMax Reitz BlockDriver *drv = bs->drv; 3458eae041feSMax Reitz if (drv && drv->bdrv_get_specific_info) { 3459eae041feSMax Reitz return drv->bdrv_get_specific_info(bs); 3460eae041feSMax Reitz } 3461eae041feSMax Reitz return NULL; 3462eae041feSMax Reitz } 3463eae041feSMax Reitz 346445566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 346545566e9cSChristoph Hellwig int64_t pos, int size) 3466178e08a5Saliguori { 3467cf8074b3SKevin Wolf QEMUIOVector qiov; 3468cf8074b3SKevin Wolf struct iovec iov = { 3469cf8074b3SKevin Wolf .iov_base = (void *) buf, 3470cf8074b3SKevin Wolf .iov_len = size, 3471cf8074b3SKevin Wolf }; 3472cf8074b3SKevin Wolf 3473cf8074b3SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 3474cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs, &qiov, pos); 3475cf8074b3SKevin Wolf } 3476cf8074b3SKevin Wolf 3477cf8074b3SKevin Wolf int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) 3478cf8074b3SKevin Wolf { 3479178e08a5Saliguori BlockDriver *drv = bs->drv; 3480cf8074b3SKevin Wolf 3481cf8074b3SKevin Wolf if (!drv) { 3482178e08a5Saliguori return -ENOMEDIUM; 3483cf8074b3SKevin Wolf } else if (drv->bdrv_save_vmstate) { 3484cf8074b3SKevin Wolf return drv->bdrv_save_vmstate(bs, qiov, pos); 3485cf8074b3SKevin Wolf } else if (bs->file) { 3486cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs->file, qiov, pos); 3487cf8074b3SKevin Wolf } 3488cf8074b3SKevin Wolf 34897cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3490178e08a5Saliguori } 3491178e08a5Saliguori 349245566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 349345566e9cSChristoph Hellwig int64_t pos, int size) 3494178e08a5Saliguori { 3495178e08a5Saliguori BlockDriver *drv = bs->drv; 3496178e08a5Saliguori if (!drv) 3497178e08a5Saliguori return -ENOMEDIUM; 34987cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 349945566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 35007cdb1f6dSMORITA Kazutaka if (bs->file) 35017cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 35027cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3503178e08a5Saliguori } 3504178e08a5Saliguori 35058b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 35068b9b0cc2SKevin Wolf { 3507bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 35088b9b0cc2SKevin Wolf return; 35098b9b0cc2SKevin Wolf } 35108b9b0cc2SKevin Wolf 3511bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 351241c695c7SKevin Wolf } 35138b9b0cc2SKevin Wolf 351441c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 351541c695c7SKevin Wolf const char *tag) 351641c695c7SKevin Wolf { 351741c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 351841c695c7SKevin Wolf bs = bs->file; 351941c695c7SKevin Wolf } 352041c695c7SKevin Wolf 352141c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 352241c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 352341c695c7SKevin Wolf } 352441c695c7SKevin Wolf 352541c695c7SKevin Wolf return -ENOTSUP; 352641c695c7SKevin Wolf } 352741c695c7SKevin Wolf 3528*4cc70e93SFam Zheng int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) 3529*4cc70e93SFam Zheng { 3530*4cc70e93SFam Zheng while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { 3531*4cc70e93SFam Zheng bs = bs->file; 3532*4cc70e93SFam Zheng } 3533*4cc70e93SFam Zheng 3534*4cc70e93SFam Zheng if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { 3535*4cc70e93SFam Zheng return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); 3536*4cc70e93SFam Zheng } 3537*4cc70e93SFam Zheng 3538*4cc70e93SFam Zheng return -ENOTSUP; 3539*4cc70e93SFam Zheng } 3540*4cc70e93SFam Zheng 354141c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 354241c695c7SKevin Wolf { 354341c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_resume) { 354441c695c7SKevin Wolf bs = bs->file; 354541c695c7SKevin Wolf } 354641c695c7SKevin Wolf 354741c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 354841c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 354941c695c7SKevin Wolf } 355041c695c7SKevin Wolf 355141c695c7SKevin Wolf return -ENOTSUP; 355241c695c7SKevin Wolf } 355341c695c7SKevin Wolf 355441c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 355541c695c7SKevin Wolf { 355641c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 355741c695c7SKevin Wolf bs = bs->file; 355841c695c7SKevin Wolf } 355941c695c7SKevin Wolf 356041c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 356141c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 356241c695c7SKevin Wolf } 356341c695c7SKevin Wolf 356441c695c7SKevin Wolf return false; 35658b9b0cc2SKevin Wolf } 35668b9b0cc2SKevin Wolf 3567199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 3568199630b6SBlue Swirl { 3569199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 3570199630b6SBlue Swirl } 3571199630b6SBlue Swirl 3572b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 3573b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 3574b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 3575b1b1d783SJeff Cody * the CWD rather than the chain. */ 3576e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 3577e8a6bb9cSMarcelo Tosatti const char *backing_file) 3578e8a6bb9cSMarcelo Tosatti { 3579b1b1d783SJeff Cody char *filename_full = NULL; 3580b1b1d783SJeff Cody char *backing_file_full = NULL; 3581b1b1d783SJeff Cody char *filename_tmp = NULL; 3582b1b1d783SJeff Cody int is_protocol = 0; 3583b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 3584b1b1d783SJeff Cody BlockDriverState *retval = NULL; 3585b1b1d783SJeff Cody 3586b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 3587e8a6bb9cSMarcelo Tosatti return NULL; 3588e8a6bb9cSMarcelo Tosatti } 3589e8a6bb9cSMarcelo Tosatti 3590b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 3591b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 3592b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 3593b1b1d783SJeff Cody 3594b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 3595b1b1d783SJeff Cody 3596b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 3597b1b1d783SJeff Cody 3598b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 3599b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 3600b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 3601b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 3602b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3603b1b1d783SJeff Cody break; 3604b1b1d783SJeff Cody } 3605e8a6bb9cSMarcelo Tosatti } else { 3606b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 3607b1b1d783SJeff Cody * image's filename path */ 3608b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3609b1b1d783SJeff Cody backing_file); 3610b1b1d783SJeff Cody 3611b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 3612b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 3613b1b1d783SJeff Cody continue; 3614b1b1d783SJeff Cody } 3615b1b1d783SJeff Cody 3616b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 3617b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 3618b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3619b1b1d783SJeff Cody curr_bs->backing_file); 3620b1b1d783SJeff Cody 3621b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 3622b1b1d783SJeff Cody continue; 3623b1b1d783SJeff Cody } 3624b1b1d783SJeff Cody 3625b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 3626b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3627b1b1d783SJeff Cody break; 3628b1b1d783SJeff Cody } 3629e8a6bb9cSMarcelo Tosatti } 3630e8a6bb9cSMarcelo Tosatti } 3631e8a6bb9cSMarcelo Tosatti 3632b1b1d783SJeff Cody g_free(filename_full); 3633b1b1d783SJeff Cody g_free(backing_file_full); 3634b1b1d783SJeff Cody g_free(filename_tmp); 3635b1b1d783SJeff Cody return retval; 3636e8a6bb9cSMarcelo Tosatti } 3637e8a6bb9cSMarcelo Tosatti 3638f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 3639f198fd1cSBenoît Canet { 3640f198fd1cSBenoît Canet if (!bs->drv) { 3641f198fd1cSBenoît Canet return 0; 3642f198fd1cSBenoît Canet } 3643f198fd1cSBenoît Canet 3644f198fd1cSBenoît Canet if (!bs->backing_hd) { 3645f198fd1cSBenoît Canet return 0; 3646f198fd1cSBenoît Canet } 3647f198fd1cSBenoît Canet 3648f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 3649f198fd1cSBenoît Canet } 3650f198fd1cSBenoît Canet 365179fac568SJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 365279fac568SJeff Cody { 365379fac568SJeff Cody BlockDriverState *curr_bs = NULL; 365479fac568SJeff Cody 365579fac568SJeff Cody if (!bs) { 365679fac568SJeff Cody return NULL; 365779fac568SJeff Cody } 365879fac568SJeff Cody 365979fac568SJeff Cody curr_bs = bs; 366079fac568SJeff Cody 366179fac568SJeff Cody while (curr_bs->backing_hd) { 366279fac568SJeff Cody curr_bs = curr_bs->backing_hd; 366379fac568SJeff Cody } 366479fac568SJeff Cody return curr_bs; 366579fac568SJeff Cody } 366679fac568SJeff Cody 3667ea2384d3Sbellard /**************************************************************/ 366883f64091Sbellard /* async I/Os */ 3669ea2384d3Sbellard 36703b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 3671f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 367283f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 3673ea2384d3Sbellard { 3674bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 3675bbf0a440SStefan Hajnoczi 3676b2a61371SStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 36778c5873d6SStefan Hajnoczi cb, opaque, false); 367883f64091Sbellard } 367983f64091Sbellard 3680f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 3681f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 368283f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 36837674e7bfSbellard { 3684bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 3685bbf0a440SStefan Hajnoczi 36861a6e115bSStefan Hajnoczi return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 36878c5873d6SStefan Hajnoczi cb, opaque, true); 368883f64091Sbellard } 368983f64091Sbellard 369040b4f539SKevin Wolf 369140b4f539SKevin Wolf typedef struct MultiwriteCB { 369240b4f539SKevin Wolf int error; 369340b4f539SKevin Wolf int num_requests; 369440b4f539SKevin Wolf int num_callbacks; 369540b4f539SKevin Wolf struct { 369640b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 369740b4f539SKevin Wolf void *opaque; 369840b4f539SKevin Wolf QEMUIOVector *free_qiov; 369940b4f539SKevin Wolf } callbacks[]; 370040b4f539SKevin Wolf } MultiwriteCB; 370140b4f539SKevin Wolf 370240b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 370340b4f539SKevin Wolf { 370440b4f539SKevin Wolf int i; 370540b4f539SKevin Wolf 370640b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 370740b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 37081e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 37091e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 37101e1ea48dSStefan Hajnoczi } 37117267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 371240b4f539SKevin Wolf } 371340b4f539SKevin Wolf } 371440b4f539SKevin Wolf 371540b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 371640b4f539SKevin Wolf { 371740b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 371840b4f539SKevin Wolf 37196d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 37206d519a5fSStefan Hajnoczi 3721cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 372240b4f539SKevin Wolf mcb->error = ret; 372340b4f539SKevin Wolf } 372440b4f539SKevin Wolf 372540b4f539SKevin Wolf mcb->num_requests--; 372640b4f539SKevin Wolf if (mcb->num_requests == 0) { 372740b4f539SKevin Wolf multiwrite_user_cb(mcb); 37287267c094SAnthony Liguori g_free(mcb); 372940b4f539SKevin Wolf } 373040b4f539SKevin Wolf } 373140b4f539SKevin Wolf 373240b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 373340b4f539SKevin Wolf { 373477be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 373577be4366SChristoph Hellwig 373677be4366SChristoph Hellwig /* 373777be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 373877be4366SChristoph Hellwig * here as that could overflow the return value. 373977be4366SChristoph Hellwig */ 374077be4366SChristoph Hellwig if (req1->sector > req2->sector) { 374177be4366SChristoph Hellwig return 1; 374277be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 374377be4366SChristoph Hellwig return -1; 374477be4366SChristoph Hellwig } else { 374577be4366SChristoph Hellwig return 0; 374677be4366SChristoph Hellwig } 374740b4f539SKevin Wolf } 374840b4f539SKevin Wolf 374940b4f539SKevin Wolf /* 375040b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 375140b4f539SKevin Wolf * requests that remain after merging. 375240b4f539SKevin Wolf */ 375340b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 375440b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 375540b4f539SKevin Wolf { 375640b4f539SKevin Wolf int i, outidx; 375740b4f539SKevin Wolf 375840b4f539SKevin Wolf // Sort requests by start sector 375940b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 376040b4f539SKevin Wolf 376140b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 376240b4f539SKevin Wolf // filling up gaps with zero sectors. 376340b4f539SKevin Wolf outidx = 0; 376440b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 376540b4f539SKevin Wolf int merge = 0; 376640b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 376740b4f539SKevin Wolf 3768b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 376940b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 377040b4f539SKevin Wolf merge = 1; 377140b4f539SKevin Wolf } 377240b4f539SKevin Wolf 3773e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 3774e2a305fbSChristoph Hellwig merge = 0; 3775e2a305fbSChristoph Hellwig } 3776e2a305fbSChristoph Hellwig 377740b4f539SKevin Wolf if (merge) { 377840b4f539SKevin Wolf size_t size; 37797267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 378040b4f539SKevin Wolf qemu_iovec_init(qiov, 378140b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 378240b4f539SKevin Wolf 378340b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 378440b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 378540b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 37861b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 378740b4f539SKevin Wolf 3788b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 3789b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 379040b4f539SKevin Wolf 379140b4f539SKevin Wolf // Add the second request 37921b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 379340b4f539SKevin Wolf 3794cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 379540b4f539SKevin Wolf reqs[outidx].qiov = qiov; 379640b4f539SKevin Wolf 379740b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 379840b4f539SKevin Wolf } else { 379940b4f539SKevin Wolf outidx++; 380040b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 380140b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 380240b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 380340b4f539SKevin Wolf } 380440b4f539SKevin Wolf } 380540b4f539SKevin Wolf 380640b4f539SKevin Wolf return outidx + 1; 380740b4f539SKevin Wolf } 380840b4f539SKevin Wolf 380940b4f539SKevin Wolf /* 381040b4f539SKevin Wolf * Submit multiple AIO write requests at once. 381140b4f539SKevin Wolf * 381240b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 381340b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 381440b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 381540b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 381640b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 381740b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 381840b4f539SKevin Wolf * 381940b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 382040b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 382140b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 382240b4f539SKevin Wolf */ 382340b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 382440b4f539SKevin Wolf { 382540b4f539SKevin Wolf MultiwriteCB *mcb; 382640b4f539SKevin Wolf int i; 382740b4f539SKevin Wolf 3828301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 3829301db7c2SRyan Harper if (bs->drv == NULL) { 3830301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 3831301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 3832301db7c2SRyan Harper } 3833301db7c2SRyan Harper return -1; 3834301db7c2SRyan Harper } 3835301db7c2SRyan Harper 383640b4f539SKevin Wolf if (num_reqs == 0) { 383740b4f539SKevin Wolf return 0; 383840b4f539SKevin Wolf } 383940b4f539SKevin Wolf 384040b4f539SKevin Wolf // Create MultiwriteCB structure 38417267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 384240b4f539SKevin Wolf mcb->num_requests = 0; 384340b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 384440b4f539SKevin Wolf 384540b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 384640b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 384740b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 384840b4f539SKevin Wolf } 384940b4f539SKevin Wolf 385040b4f539SKevin Wolf // Check for mergable requests 385140b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 385240b4f539SKevin Wolf 38536d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 38546d519a5fSStefan Hajnoczi 3855df9309fbSPaolo Bonzini /* Run the aio requests. */ 3856df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 385740b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 3858ad54ae80SPaolo Bonzini bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 385940b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 386040b4f539SKevin Wolf } 386140b4f539SKevin Wolf 386240b4f539SKevin Wolf return 0; 386340b4f539SKevin Wolf } 386440b4f539SKevin Wolf 386583f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 386683f64091Sbellard { 3867d7331bedSStefan Hajnoczi acb->aiocb_info->cancel(acb); 386883f64091Sbellard } 386983f64091Sbellard 387083f64091Sbellard /**************************************************************/ 387183f64091Sbellard /* async block device emulation */ 387283f64091Sbellard 3873c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 3874c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 3875c16b5a2cSChristoph Hellwig QEMUBH *bh; 3876c16b5a2cSChristoph Hellwig int ret; 3877c16b5a2cSChristoph Hellwig /* vector translation state */ 3878c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 3879c16b5a2cSChristoph Hellwig uint8_t *bounce; 3880c16b5a2cSChristoph Hellwig int is_write; 3881c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 3882c16b5a2cSChristoph Hellwig 3883c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 3884c16b5a2cSChristoph Hellwig { 3885b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 3886b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 38876a7ad299SDor Laor qemu_bh_delete(acb->bh); 388836afc451SAvi Kivity acb->bh = NULL; 3889c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 3890c16b5a2cSChristoph Hellwig } 3891c16b5a2cSChristoph Hellwig 3892d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_aiocb_info = { 3893c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 3894c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 3895c16b5a2cSChristoph Hellwig }; 3896c16b5a2cSChristoph Hellwig 389783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 3898beac80cdSbellard { 3899ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 3900f141eafeSaliguori 3901f141eafeSaliguori if (!acb->is_write) 390203396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 3903ceb42de8Saliguori qemu_vfree(acb->bounce); 3904ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 39056a7ad299SDor Laor qemu_bh_delete(acb->bh); 390636afc451SAvi Kivity acb->bh = NULL; 3907ce1a14dcSpbrook qemu_aio_release(acb); 3908beac80cdSbellard } 3909beac80cdSbellard 3910f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 3911f141eafeSaliguori int64_t sector_num, 3912f141eafeSaliguori QEMUIOVector *qiov, 3913f141eafeSaliguori int nb_sectors, 3914f141eafeSaliguori BlockDriverCompletionFunc *cb, 3915f141eafeSaliguori void *opaque, 3916f141eafeSaliguori int is_write) 3917f141eafeSaliguori 3918ea2384d3Sbellard { 3919ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 392083f64091Sbellard 3921d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); 3922f141eafeSaliguori acb->is_write = is_write; 3923f141eafeSaliguori acb->qiov = qiov; 3924e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 3925ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 3926f141eafeSaliguori 3927f141eafeSaliguori if (is_write) { 3928d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 39291ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 3930f141eafeSaliguori } else { 39311ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 3932f141eafeSaliguori } 3933f141eafeSaliguori 3934ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 3935f141eafeSaliguori 3936ce1a14dcSpbrook return &acb->common; 39377a6cba61Spbrook } 39387a6cba61Spbrook 3939f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 3940f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3941ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 394283f64091Sbellard { 3943f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 394483f64091Sbellard } 394583f64091Sbellard 3946f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 3947f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 3948f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 3949f141eafeSaliguori { 3950f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 3951f141eafeSaliguori } 3952f141eafeSaliguori 395368485420SKevin Wolf 395468485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 395568485420SKevin Wolf BlockDriverAIOCB common; 395668485420SKevin Wolf BlockRequest req; 395768485420SKevin Wolf bool is_write; 3958d318aea9SKevin Wolf bool *done; 395968485420SKevin Wolf QEMUBH* bh; 396068485420SKevin Wolf } BlockDriverAIOCBCoroutine; 396168485420SKevin Wolf 396268485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 396368485420SKevin Wolf { 3964d318aea9SKevin Wolf BlockDriverAIOCBCoroutine *acb = 3965d318aea9SKevin Wolf container_of(blockacb, BlockDriverAIOCBCoroutine, common); 3966d318aea9SKevin Wolf bool done = false; 3967d318aea9SKevin Wolf 3968d318aea9SKevin Wolf acb->done = &done; 3969d318aea9SKevin Wolf while (!done) { 3970d318aea9SKevin Wolf qemu_aio_wait(); 3971d318aea9SKevin Wolf } 397268485420SKevin Wolf } 397368485420SKevin Wolf 3974d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_co_aiocb_info = { 397568485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 397668485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 397768485420SKevin Wolf }; 397868485420SKevin Wolf 397935246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 398068485420SKevin Wolf { 398168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 398268485420SKevin Wolf 398368485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 3984d318aea9SKevin Wolf 3985d318aea9SKevin Wolf if (acb->done) { 3986d318aea9SKevin Wolf *acb->done = true; 3987d318aea9SKevin Wolf } 3988d318aea9SKevin Wolf 398968485420SKevin Wolf qemu_bh_delete(acb->bh); 399068485420SKevin Wolf qemu_aio_release(acb); 399168485420SKevin Wolf } 399268485420SKevin Wolf 3993b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 3994b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 3995b2a61371SStefan Hajnoczi { 3996b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 3997b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 3998b2a61371SStefan Hajnoczi 3999b2a61371SStefan Hajnoczi if (!acb->is_write) { 4000b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 4001470c0504SStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 4002b2a61371SStefan Hajnoczi } else { 4003b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 4004f08f2ddaSStefan Hajnoczi acb->req.nb_sectors, acb->req.qiov, 0); 4005b2a61371SStefan Hajnoczi } 4006b2a61371SStefan Hajnoczi 400735246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 4008b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 4009b2a61371SStefan Hajnoczi } 4010b2a61371SStefan Hajnoczi 401168485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 401268485420SKevin Wolf int64_t sector_num, 401368485420SKevin Wolf QEMUIOVector *qiov, 401468485420SKevin Wolf int nb_sectors, 401568485420SKevin Wolf BlockDriverCompletionFunc *cb, 401668485420SKevin Wolf void *opaque, 40178c5873d6SStefan Hajnoczi bool is_write) 401868485420SKevin Wolf { 401968485420SKevin Wolf Coroutine *co; 402068485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 402168485420SKevin Wolf 4022d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 402368485420SKevin Wolf acb->req.sector = sector_num; 402468485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 402568485420SKevin Wolf acb->req.qiov = qiov; 402668485420SKevin Wolf acb->is_write = is_write; 4027d318aea9SKevin Wolf acb->done = NULL; 402868485420SKevin Wolf 40298c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 403068485420SKevin Wolf qemu_coroutine_enter(co, acb); 403168485420SKevin Wolf 403268485420SKevin Wolf return &acb->common; 403368485420SKevin Wolf } 403468485420SKevin Wolf 403507f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 4036b2e12bc6SChristoph Hellwig { 403707f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 403807f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 4039b2e12bc6SChristoph Hellwig 404007f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 404107f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 4042b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 4043b2e12bc6SChristoph Hellwig } 4044b2e12bc6SChristoph Hellwig 404507f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 4046016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 4047016f5cf6SAlexander Graf { 404807f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 4049016f5cf6SAlexander Graf 405007f07615SPaolo Bonzini Coroutine *co; 405107f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 4052016f5cf6SAlexander Graf 4053d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 4054d318aea9SKevin Wolf acb->done = NULL; 4055d318aea9SKevin Wolf 405607f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 405707f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 4058016f5cf6SAlexander Graf 4059016f5cf6SAlexander Graf return &acb->common; 4060016f5cf6SAlexander Graf } 4061016f5cf6SAlexander Graf 40624265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 40634265d620SPaolo Bonzini { 40644265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 40654265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 40664265d620SPaolo Bonzini 40674265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 40684265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 40694265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 40704265d620SPaolo Bonzini } 40714265d620SPaolo Bonzini 40724265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 40734265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 40744265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 40754265d620SPaolo Bonzini { 40764265d620SPaolo Bonzini Coroutine *co; 40774265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 40784265d620SPaolo Bonzini 40794265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 40804265d620SPaolo Bonzini 4081d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 40824265d620SPaolo Bonzini acb->req.sector = sector_num; 40834265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 4084d318aea9SKevin Wolf acb->done = NULL; 40854265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 40864265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 40874265d620SPaolo Bonzini 40884265d620SPaolo Bonzini return &acb->common; 40894265d620SPaolo Bonzini } 40904265d620SPaolo Bonzini 4091ea2384d3Sbellard void bdrv_init(void) 4092ea2384d3Sbellard { 40935efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 4094ea2384d3Sbellard } 4095ce1a14dcSpbrook 4096eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 4097eb852011SMarkus Armbruster { 4098eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 4099eb852011SMarkus Armbruster bdrv_init(); 4100eb852011SMarkus Armbruster } 4101eb852011SMarkus Armbruster 4102d7331bedSStefan Hajnoczi void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 41036bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 41046bbff9a0Saliguori { 4105ce1a14dcSpbrook BlockDriverAIOCB *acb; 4106ce1a14dcSpbrook 4107d7331bedSStefan Hajnoczi acb = g_slice_alloc(aiocb_info->aiocb_size); 4108d7331bedSStefan Hajnoczi acb->aiocb_info = aiocb_info; 4109ce1a14dcSpbrook acb->bs = bs; 4110ce1a14dcSpbrook acb->cb = cb; 4111ce1a14dcSpbrook acb->opaque = opaque; 4112ce1a14dcSpbrook return acb; 4113ce1a14dcSpbrook } 4114ce1a14dcSpbrook 4115ce1a14dcSpbrook void qemu_aio_release(void *p) 4116ce1a14dcSpbrook { 4117d37c975fSStefan Hajnoczi BlockDriverAIOCB *acb = p; 4118d7331bedSStefan Hajnoczi g_slice_free1(acb->aiocb_info->aiocb_size, acb); 4119ce1a14dcSpbrook } 412019cb3738Sbellard 412119cb3738Sbellard /**************************************************************/ 4122f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 4123f9f05dc5SKevin Wolf 4124f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 4125f9f05dc5SKevin Wolf Coroutine *coroutine; 4126f9f05dc5SKevin Wolf int ret; 4127f9f05dc5SKevin Wolf } CoroutineIOCompletion; 4128f9f05dc5SKevin Wolf 4129f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 4130f9f05dc5SKevin Wolf { 4131f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 4132f9f05dc5SKevin Wolf 4133f9f05dc5SKevin Wolf co->ret = ret; 4134f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 4135f9f05dc5SKevin Wolf } 4136f9f05dc5SKevin Wolf 4137f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 4138f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 4139f9f05dc5SKevin Wolf bool is_write) 4140f9f05dc5SKevin Wolf { 4141f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 4142f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 4143f9f05dc5SKevin Wolf }; 4144f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 4145f9f05dc5SKevin Wolf 4146f9f05dc5SKevin Wolf if (is_write) { 4147a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 4148f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4149f9f05dc5SKevin Wolf } else { 4150a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 4151f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4152f9f05dc5SKevin Wolf } 4153f9f05dc5SKevin Wolf 415459370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 4155f9f05dc5SKevin Wolf if (!acb) { 4156f9f05dc5SKevin Wolf return -EIO; 4157f9f05dc5SKevin Wolf } 4158f9f05dc5SKevin Wolf qemu_coroutine_yield(); 4159f9f05dc5SKevin Wolf 4160f9f05dc5SKevin Wolf return co.ret; 4161f9f05dc5SKevin Wolf } 4162f9f05dc5SKevin Wolf 4163f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 4164f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4165f9f05dc5SKevin Wolf QEMUIOVector *iov) 4166f9f05dc5SKevin Wolf { 4167f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 4168f9f05dc5SKevin Wolf } 4169f9f05dc5SKevin Wolf 4170f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 4171f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4172f9f05dc5SKevin Wolf QEMUIOVector *iov) 4173f9f05dc5SKevin Wolf { 4174f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 4175f9f05dc5SKevin Wolf } 4176f9f05dc5SKevin Wolf 417707f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 4178e7a8a783SKevin Wolf { 417907f07615SPaolo Bonzini RwCo *rwco = opaque; 418007f07615SPaolo Bonzini 418107f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 418207f07615SPaolo Bonzini } 418307f07615SPaolo Bonzini 418407f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 418507f07615SPaolo Bonzini { 4186eb489bb1SKevin Wolf int ret; 4187eb489bb1SKevin Wolf 418829cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 418907f07615SPaolo Bonzini return 0; 4190eb489bb1SKevin Wolf } 4191eb489bb1SKevin Wolf 4192ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 4193bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); 4194eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 4195eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 4196eb489bb1SKevin Wolf if (ret < 0) { 4197eb489bb1SKevin Wolf return ret; 4198eb489bb1SKevin Wolf } 4199eb489bb1SKevin Wolf } 4200eb489bb1SKevin Wolf 4201ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 4202ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 4203d4c82329SKevin Wolf goto flush_parent; 4204ca716364SKevin Wolf } 4205ca716364SKevin Wolf 4206bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); 4207eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 420829cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 420907f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 421007f07615SPaolo Bonzini BlockDriverAIOCB *acb; 4211e7a8a783SKevin Wolf CoroutineIOCompletion co = { 4212e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 4213e7a8a783SKevin Wolf }; 4214e7a8a783SKevin Wolf 421507f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 421607f07615SPaolo Bonzini if (acb == NULL) { 421729cdb251SPaolo Bonzini ret = -EIO; 421807f07615SPaolo Bonzini } else { 4219e7a8a783SKevin Wolf qemu_coroutine_yield(); 422029cdb251SPaolo Bonzini ret = co.ret; 4221e7a8a783SKevin Wolf } 422207f07615SPaolo Bonzini } else { 422307f07615SPaolo Bonzini /* 422407f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 422507f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 422607f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 422707f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 422807f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 422907f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 423007f07615SPaolo Bonzini * mode. 423107f07615SPaolo Bonzini * 423207f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 423307f07615SPaolo Bonzini */ 423429cdb251SPaolo Bonzini ret = 0; 423507f07615SPaolo Bonzini } 423629cdb251SPaolo Bonzini if (ret < 0) { 423729cdb251SPaolo Bonzini return ret; 423829cdb251SPaolo Bonzini } 423929cdb251SPaolo Bonzini 424029cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 424129cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 424229cdb251SPaolo Bonzini */ 4243d4c82329SKevin Wolf flush_parent: 424429cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 424507f07615SPaolo Bonzini } 424607f07615SPaolo Bonzini 42470f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 42480f15423cSAnthony Liguori { 42490f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 42500f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 42510f15423cSAnthony Liguori } 42520f15423cSAnthony Liguori } 42530f15423cSAnthony Liguori 42540f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 42550f15423cSAnthony Liguori { 42560f15423cSAnthony Liguori BlockDriverState *bs; 42570f15423cSAnthony Liguori 42580f15423cSAnthony Liguori QTAILQ_FOREACH(bs, &bdrv_states, list) { 42590f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 42600f15423cSAnthony Liguori } 42610f15423cSAnthony Liguori } 42620f15423cSAnthony Liguori 426307789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 426407789269SBenoît Canet { 426507789269SBenoît Canet BlockDriverState *bs; 426607789269SBenoît Canet 426707789269SBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, list) { 426807789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 426907789269SBenoît Canet } 427007789269SBenoît Canet } 427107789269SBenoît Canet 427207f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 427307f07615SPaolo Bonzini { 427407f07615SPaolo Bonzini Coroutine *co; 427507f07615SPaolo Bonzini RwCo rwco = { 427607f07615SPaolo Bonzini .bs = bs, 427707f07615SPaolo Bonzini .ret = NOT_DONE, 427807f07615SPaolo Bonzini }; 427907f07615SPaolo Bonzini 428007f07615SPaolo Bonzini if (qemu_in_coroutine()) { 428107f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 428207f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 428307f07615SPaolo Bonzini } else { 428407f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 428507f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 428607f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 428707f07615SPaolo Bonzini qemu_aio_wait(); 428807f07615SPaolo Bonzini } 428907f07615SPaolo Bonzini } 429007f07615SPaolo Bonzini 429107f07615SPaolo Bonzini return rwco.ret; 429207f07615SPaolo Bonzini } 4293e7a8a783SKevin Wolf 42944265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 42954265d620SPaolo Bonzini { 42964265d620SPaolo Bonzini RwCo *rwco = opaque; 42974265d620SPaolo Bonzini 42984265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 42994265d620SPaolo Bonzini } 43004265d620SPaolo Bonzini 43016f14da52SPeter Lieven /* if no limit is specified in the BlockLimits use a default 43026f14da52SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 43036f14da52SPeter Lieven */ 43046f14da52SPeter Lieven #define MAX_DISCARD_DEFAULT 32768 43056f14da52SPeter Lieven 43064265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 43074265d620SPaolo Bonzini int nb_sectors) 43084265d620SPaolo Bonzini { 43094265d620SPaolo Bonzini if (!bs->drv) { 43104265d620SPaolo Bonzini return -ENOMEDIUM; 43114265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 43124265d620SPaolo Bonzini return -EIO; 43134265d620SPaolo Bonzini } else if (bs->read_only) { 43144265d620SPaolo Bonzini return -EROFS; 4315df702c9bSPaolo Bonzini } 4316df702c9bSPaolo Bonzini 43178f0720ecSPaolo Bonzini bdrv_reset_dirty(bs, sector_num, nb_sectors); 4318df702c9bSPaolo Bonzini 43199e8f1835SPaolo Bonzini /* Do nothing if disabled. */ 43209e8f1835SPaolo Bonzini if (!(bs->open_flags & BDRV_O_UNMAP)) { 43219e8f1835SPaolo Bonzini return 0; 43229e8f1835SPaolo Bonzini } 43239e8f1835SPaolo Bonzini 4324df702c9bSPaolo Bonzini if (bs->drv->bdrv_co_discard) { 43256f14da52SPeter Lieven int max_discard = bs->bl.max_discard ? 43266f14da52SPeter Lieven bs->bl.max_discard : MAX_DISCARD_DEFAULT; 43276f14da52SPeter Lieven 43286f14da52SPeter Lieven while (nb_sectors > 0) { 43296f14da52SPeter Lieven int ret; 43306f14da52SPeter Lieven int num = nb_sectors; 43316f14da52SPeter Lieven 43326f14da52SPeter Lieven /* align request */ 43336f14da52SPeter Lieven if (bs->bl.discard_alignment && 43346f14da52SPeter Lieven num >= bs->bl.discard_alignment && 43356f14da52SPeter Lieven sector_num % bs->bl.discard_alignment) { 43366f14da52SPeter Lieven if (num > bs->bl.discard_alignment) { 43376f14da52SPeter Lieven num = bs->bl.discard_alignment; 43386f14da52SPeter Lieven } 43396f14da52SPeter Lieven num -= sector_num % bs->bl.discard_alignment; 43406f14da52SPeter Lieven } 43416f14da52SPeter Lieven 43426f14da52SPeter Lieven /* limit request size */ 43436f14da52SPeter Lieven if (num > max_discard) { 43446f14da52SPeter Lieven num = max_discard; 43456f14da52SPeter Lieven } 43466f14da52SPeter Lieven 43476f14da52SPeter Lieven ret = bs->drv->bdrv_co_discard(bs, sector_num, num); 43486f14da52SPeter Lieven if (ret) { 43496f14da52SPeter Lieven return ret; 43506f14da52SPeter Lieven } 43516f14da52SPeter Lieven 43526f14da52SPeter Lieven sector_num += num; 43536f14da52SPeter Lieven nb_sectors -= num; 43546f14da52SPeter Lieven } 43556f14da52SPeter Lieven return 0; 43564265d620SPaolo Bonzini } else if (bs->drv->bdrv_aio_discard) { 43574265d620SPaolo Bonzini BlockDriverAIOCB *acb; 43584265d620SPaolo Bonzini CoroutineIOCompletion co = { 43594265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 43604265d620SPaolo Bonzini }; 43614265d620SPaolo Bonzini 43624265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 43634265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 43644265d620SPaolo Bonzini if (acb == NULL) { 43654265d620SPaolo Bonzini return -EIO; 43664265d620SPaolo Bonzini } else { 43674265d620SPaolo Bonzini qemu_coroutine_yield(); 43684265d620SPaolo Bonzini return co.ret; 43694265d620SPaolo Bonzini } 43704265d620SPaolo Bonzini } else { 43714265d620SPaolo Bonzini return 0; 43724265d620SPaolo Bonzini } 43734265d620SPaolo Bonzini } 43744265d620SPaolo Bonzini 43754265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 43764265d620SPaolo Bonzini { 43774265d620SPaolo Bonzini Coroutine *co; 43784265d620SPaolo Bonzini RwCo rwco = { 43794265d620SPaolo Bonzini .bs = bs, 43804265d620SPaolo Bonzini .sector_num = sector_num, 43814265d620SPaolo Bonzini .nb_sectors = nb_sectors, 43824265d620SPaolo Bonzini .ret = NOT_DONE, 43834265d620SPaolo Bonzini }; 43844265d620SPaolo Bonzini 43854265d620SPaolo Bonzini if (qemu_in_coroutine()) { 43864265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 43874265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 43884265d620SPaolo Bonzini } else { 43894265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 43904265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 43914265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 43924265d620SPaolo Bonzini qemu_aio_wait(); 43934265d620SPaolo Bonzini } 43944265d620SPaolo Bonzini } 43954265d620SPaolo Bonzini 43964265d620SPaolo Bonzini return rwco.ret; 43974265d620SPaolo Bonzini } 43984265d620SPaolo Bonzini 4399f9f05dc5SKevin Wolf /**************************************************************/ 440019cb3738Sbellard /* removable device support */ 440119cb3738Sbellard 440219cb3738Sbellard /** 440319cb3738Sbellard * Return TRUE if the media is present 440419cb3738Sbellard */ 440519cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 440619cb3738Sbellard { 440719cb3738Sbellard BlockDriver *drv = bs->drv; 4408a1aff5bfSMarkus Armbruster 440919cb3738Sbellard if (!drv) 441019cb3738Sbellard return 0; 441119cb3738Sbellard if (!drv->bdrv_is_inserted) 4412a1aff5bfSMarkus Armbruster return 1; 4413a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 441419cb3738Sbellard } 441519cb3738Sbellard 441619cb3738Sbellard /** 44178e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 44188e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 441919cb3738Sbellard */ 442019cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 442119cb3738Sbellard { 442219cb3738Sbellard BlockDriver *drv = bs->drv; 442319cb3738Sbellard 44248e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 44258e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 44268e49ca46SMarkus Armbruster } 44278e49ca46SMarkus Armbruster return -ENOTSUP; 442819cb3738Sbellard } 442919cb3738Sbellard 443019cb3738Sbellard /** 443119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 443219cb3738Sbellard */ 4433f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 443419cb3738Sbellard { 443519cb3738Sbellard BlockDriver *drv = bs->drv; 443619cb3738Sbellard 4437822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 4438822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 443919cb3738Sbellard } 44406f382ed2SLuiz Capitulino 44416f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 44426f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 44436f382ed2SLuiz Capitulino } 444419cb3738Sbellard } 444519cb3738Sbellard 444619cb3738Sbellard /** 444719cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 444819cb3738Sbellard * to eject it manually). 444919cb3738Sbellard */ 4450025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 445119cb3738Sbellard { 445219cb3738Sbellard BlockDriver *drv = bs->drv; 445319cb3738Sbellard 4454025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 4455b8c6d095SStefan Hajnoczi 4456025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 4457025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 445819cb3738Sbellard } 445919cb3738Sbellard } 4460985a03b0Sths 4461985a03b0Sths /* needed for generic scsi interface */ 4462985a03b0Sths 4463985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 4464985a03b0Sths { 4465985a03b0Sths BlockDriver *drv = bs->drv; 4466985a03b0Sths 4467985a03b0Sths if (drv && drv->bdrv_ioctl) 4468985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 4469985a03b0Sths return -ENOTSUP; 4470985a03b0Sths } 44717d780669Saliguori 4472221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 4473221f715dSaliguori unsigned long int req, void *buf, 44747d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 44757d780669Saliguori { 4476221f715dSaliguori BlockDriver *drv = bs->drv; 44777d780669Saliguori 4478221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 4479221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 4480221f715dSaliguori return NULL; 44817d780669Saliguori } 4482e268ca52Saliguori 44837b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 44847b6f9300SMarkus Armbruster { 44857b6f9300SMarkus Armbruster bs->buffer_alignment = align; 44867b6f9300SMarkus Armbruster } 44877cd1e32aSlirans@il.ibm.com 4488e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 4489e268ca52Saliguori { 4490e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 4491e268ca52Saliguori } 44927cd1e32aSlirans@il.ibm.com 4493c53b1c51SStefan Hajnoczi /* 4494c53b1c51SStefan Hajnoczi * Check if all memory in this vector is sector aligned. 4495c53b1c51SStefan Hajnoczi */ 4496c53b1c51SStefan Hajnoczi bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) 4497c53b1c51SStefan Hajnoczi { 4498c53b1c51SStefan Hajnoczi int i; 4499c53b1c51SStefan Hajnoczi 4500c53b1c51SStefan Hajnoczi for (i = 0; i < qiov->niov; i++) { 4501c53b1c51SStefan Hajnoczi if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { 4502c53b1c51SStefan Hajnoczi return false; 4503c53b1c51SStefan Hajnoczi } 4504c53b1c51SStefan Hajnoczi } 4505c53b1c51SStefan Hajnoczi 4506c53b1c51SStefan Hajnoczi return true; 4507c53b1c51SStefan Hajnoczi } 4508c53b1c51SStefan Hajnoczi 4509e4654d2dSFam Zheng BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity) 45107cd1e32aSlirans@il.ibm.com { 45117cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 4512e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4513a55eb92cSJan Kiszka 451450717e94SPaolo Bonzini assert((granularity & (granularity - 1)) == 0); 451550717e94SPaolo Bonzini 451650717e94SPaolo Bonzini granularity >>= BDRV_SECTOR_BITS; 4517e4654d2dSFam Zheng assert(granularity); 45188f0720ecSPaolo Bonzini bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); 4519e4654d2dSFam Zheng bitmap = g_malloc0(sizeof(BdrvDirtyBitmap)); 4520e4654d2dSFam Zheng bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); 4521e4654d2dSFam Zheng QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); 4522e4654d2dSFam Zheng return bitmap; 4523e4654d2dSFam Zheng } 4524e4654d2dSFam Zheng 4525e4654d2dSFam Zheng void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 4526e4654d2dSFam Zheng { 4527e4654d2dSFam Zheng BdrvDirtyBitmap *bm, *next; 4528e4654d2dSFam Zheng QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { 4529e4654d2dSFam Zheng if (bm == bitmap) { 4530e4654d2dSFam Zheng QLIST_REMOVE(bitmap, list); 4531e4654d2dSFam Zheng hbitmap_free(bitmap->bitmap); 4532e4654d2dSFam Zheng g_free(bitmap); 4533e4654d2dSFam Zheng return; 45347cd1e32aSlirans@il.ibm.com } 45357cd1e32aSlirans@il.ibm.com } 45367cd1e32aSlirans@il.ibm.com } 45377cd1e32aSlirans@il.ibm.com 453821b56835SFam Zheng BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 453921b56835SFam Zheng { 454021b56835SFam Zheng BdrvDirtyBitmap *bm; 454121b56835SFam Zheng BlockDirtyInfoList *list = NULL; 454221b56835SFam Zheng BlockDirtyInfoList **plist = &list; 454321b56835SFam Zheng 454421b56835SFam Zheng QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 454521b56835SFam Zheng BlockDirtyInfo *info = g_malloc0(sizeof(BlockDirtyInfo)); 454621b56835SFam Zheng BlockDirtyInfoList *entry = g_malloc0(sizeof(BlockDirtyInfoList)); 454721b56835SFam Zheng info->count = bdrv_get_dirty_count(bs, bm); 454821b56835SFam Zheng info->granularity = 454921b56835SFam Zheng ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bm->bitmap)); 455021b56835SFam Zheng entry->value = info; 455121b56835SFam Zheng *plist = entry; 455221b56835SFam Zheng plist = &entry->next; 455321b56835SFam Zheng } 455421b56835SFam Zheng 455521b56835SFam Zheng return list; 455621b56835SFam Zheng } 455721b56835SFam Zheng 4558e4654d2dSFam Zheng int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector) 45597cd1e32aSlirans@il.ibm.com { 4560e4654d2dSFam Zheng if (bitmap) { 4561e4654d2dSFam Zheng return hbitmap_get(bitmap->bitmap, sector); 45627cd1e32aSlirans@il.ibm.com } else { 45637cd1e32aSlirans@il.ibm.com return 0; 45647cd1e32aSlirans@il.ibm.com } 45657cd1e32aSlirans@il.ibm.com } 45667cd1e32aSlirans@il.ibm.com 4567e4654d2dSFam Zheng void bdrv_dirty_iter_init(BlockDriverState *bs, 4568e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap, HBitmapIter *hbi) 45691755da16SPaolo Bonzini { 4570e4654d2dSFam Zheng hbitmap_iter_init(hbi, bitmap->bitmap, 0); 45711755da16SPaolo Bonzini } 45721755da16SPaolo Bonzini 45731755da16SPaolo Bonzini void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 45741755da16SPaolo Bonzini int nr_sectors) 45751755da16SPaolo Bonzini { 4576e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4577e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 4578e4654d2dSFam Zheng hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 4579e4654d2dSFam Zheng } 45801755da16SPaolo Bonzini } 45811755da16SPaolo Bonzini 4582e4654d2dSFam Zheng void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors) 45837cd1e32aSlirans@il.ibm.com { 4584e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4585e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 4586e4654d2dSFam Zheng hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); 4587e4654d2dSFam Zheng } 45887cd1e32aSlirans@il.ibm.com } 4589aaa0eb75SLiran Schour 4590e4654d2dSFam Zheng int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 4591aaa0eb75SLiran Schour { 4592e4654d2dSFam Zheng return hbitmap_count(bitmap->bitmap); 4593aaa0eb75SLiran Schour } 4594f88e1a42SJes Sorensen 45959fcb0251SFam Zheng /* Get a reference to bs */ 45969fcb0251SFam Zheng void bdrv_ref(BlockDriverState *bs) 45979fcb0251SFam Zheng { 45989fcb0251SFam Zheng bs->refcnt++; 45999fcb0251SFam Zheng } 46009fcb0251SFam Zheng 46019fcb0251SFam Zheng /* Release a previously grabbed reference to bs. 46029fcb0251SFam Zheng * If after releasing, reference count is zero, the BlockDriverState is 46039fcb0251SFam Zheng * deleted. */ 46049fcb0251SFam Zheng void bdrv_unref(BlockDriverState *bs) 46059fcb0251SFam Zheng { 46069fcb0251SFam Zheng assert(bs->refcnt > 0); 46079fcb0251SFam Zheng if (--bs->refcnt == 0) { 46089fcb0251SFam Zheng bdrv_delete(bs); 46099fcb0251SFam Zheng } 46109fcb0251SFam Zheng } 46119fcb0251SFam Zheng 4612db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 4613db593f25SMarcelo Tosatti { 4614db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 4615db593f25SMarcelo Tosatti bs->in_use = in_use; 4616db593f25SMarcelo Tosatti } 4617db593f25SMarcelo Tosatti 4618db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 4619db593f25SMarcelo Tosatti { 4620db593f25SMarcelo Tosatti return bs->in_use; 4621db593f25SMarcelo Tosatti } 4622db593f25SMarcelo Tosatti 462328a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 462428a7282aSLuiz Capitulino { 4625d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 462658e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 462728a7282aSLuiz Capitulino } 462828a7282aSLuiz Capitulino 462928a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 463028a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 463128a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 463228a7282aSLuiz Capitulino { 4633d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 463492aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 463592aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 463692aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 463728a7282aSLuiz Capitulino } 463828a7282aSLuiz Capitulino 463928a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 464028a7282aSLuiz Capitulino { 4641d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 464228a7282aSLuiz Capitulino } 464328a7282aSLuiz Capitulino 464428a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 464528a7282aSLuiz Capitulino { 464628a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 464758e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 46483bd293c3SPaolo Bonzini if (bs->job) { 46493bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 46503bd293c3SPaolo Bonzini } 465128a7282aSLuiz Capitulino } 465228a7282aSLuiz Capitulino } 465328a7282aSLuiz Capitulino 465428a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 465528a7282aSLuiz Capitulino { 46563e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 46573e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 465858e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 465958e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 466028a7282aSLuiz Capitulino } 466128a7282aSLuiz Capitulino } 466228a7282aSLuiz Capitulino 4663a597e79cSChristoph Hellwig void 4664a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 4665a597e79cSChristoph Hellwig enum BlockAcctType type) 4666a597e79cSChristoph Hellwig { 4667a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 4668a597e79cSChristoph Hellwig 4669a597e79cSChristoph Hellwig cookie->bytes = bytes; 4670c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4671a597e79cSChristoph Hellwig cookie->type = type; 4672a597e79cSChristoph Hellwig } 4673a597e79cSChristoph Hellwig 4674a597e79cSChristoph Hellwig void 4675a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4676a597e79cSChristoph Hellwig { 4677a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4678a597e79cSChristoph Hellwig 4679a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4680a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4681c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4682a597e79cSChristoph Hellwig } 4683a597e79cSChristoph Hellwig 4684d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 4685f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 4686f382d43aSMiroslav Rezanina char *options, uint64_t img_size, int flags, 4687f382d43aSMiroslav Rezanina Error **errp, bool quiet) 4688f88e1a42SJes Sorensen { 4689f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4690d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4691f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 4692f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 469396df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4694cc84d90fSMax Reitz Error *local_err = NULL; 4695f88e1a42SJes Sorensen int ret = 0; 4696f88e1a42SJes Sorensen 4697f88e1a42SJes Sorensen /* Find driver and parse its options */ 4698f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4699f88e1a42SJes Sorensen if (!drv) { 470071c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 4701d92ada22SLuiz Capitulino return; 4702f88e1a42SJes Sorensen } 4703f88e1a42SJes Sorensen 470498289620SKevin Wolf proto_drv = bdrv_find_protocol(filename, true); 4705f88e1a42SJes Sorensen if (!proto_drv) { 470671c79813SLuiz Capitulino error_setg(errp, "Unknown protocol '%s'", filename); 4707d92ada22SLuiz Capitulino return; 4708f88e1a42SJes Sorensen } 4709f88e1a42SJes Sorensen 4710f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4711f88e1a42SJes Sorensen drv->create_options); 4712f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4713f88e1a42SJes Sorensen proto_drv->create_options); 4714f88e1a42SJes Sorensen 4715f88e1a42SJes Sorensen /* Create parameter list with default values */ 4716f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4717f88e1a42SJes Sorensen 4718f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4719f88e1a42SJes Sorensen 4720f88e1a42SJes Sorensen /* Parse -o options */ 4721f88e1a42SJes Sorensen if (options) { 4722f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4723f88e1a42SJes Sorensen if (param == NULL) { 472471c79813SLuiz Capitulino error_setg(errp, "Invalid options for file format '%s'.", fmt); 4725f88e1a42SJes Sorensen goto out; 4726f88e1a42SJes Sorensen } 4727f88e1a42SJes Sorensen } 4728f88e1a42SJes Sorensen 4729f88e1a42SJes Sorensen if (base_filename) { 4730f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4731f88e1a42SJes Sorensen base_filename)) { 473271c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 473371c79813SLuiz Capitulino fmt); 4734f88e1a42SJes Sorensen goto out; 4735f88e1a42SJes Sorensen } 4736f88e1a42SJes Sorensen } 4737f88e1a42SJes Sorensen 4738f88e1a42SJes Sorensen if (base_fmt) { 4739f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 474071c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 474171c79813SLuiz Capitulino "format '%s'", fmt); 4742f88e1a42SJes Sorensen goto out; 4743f88e1a42SJes Sorensen } 4744f88e1a42SJes Sorensen } 4745f88e1a42SJes Sorensen 4746792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4747792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4748792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 474971c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 475071c79813SLuiz Capitulino "same filename as the backing file"); 4751792da93aSJes Sorensen goto out; 4752792da93aSJes Sorensen } 4753792da93aSJes Sorensen } 4754792da93aSJes Sorensen 4755f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4756f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 475796df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 475896df67d1SStefan Hajnoczi if (!backing_drv) { 475971c79813SLuiz Capitulino error_setg(errp, "Unknown backing file format '%s'", 476071c79813SLuiz Capitulino backing_fmt->value.s); 4761f88e1a42SJes Sorensen goto out; 4762f88e1a42SJes Sorensen } 4763f88e1a42SJes Sorensen } 4764f88e1a42SJes Sorensen 4765f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 4766f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 4767d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 4768d220894eSKevin Wolf if (size && size->value.n == -1) { 4769f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 4770f88e1a42SJes Sorensen uint64_t size; 4771f88e1a42SJes Sorensen char buf[32]; 477263090dacSPaolo Bonzini int back_flags; 477363090dacSPaolo Bonzini 477463090dacSPaolo Bonzini /* backing files always opened read-only */ 477563090dacSPaolo Bonzini back_flags = 477663090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 4777f88e1a42SJes Sorensen 4778f88e1a42SJes Sorensen bs = bdrv_new(""); 4779f88e1a42SJes Sorensen 4780de9c0cecSKevin Wolf ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags, 4781cc84d90fSMax Reitz backing_drv, &local_err); 4782f88e1a42SJes Sorensen if (ret < 0) { 4783cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not open '%s': %s", 4784cc84d90fSMax Reitz backing_file->value.s, 4785cc84d90fSMax Reitz error_get_pretty(local_err)); 4786cc84d90fSMax Reitz error_free(local_err); 4787cc84d90fSMax Reitz local_err = NULL; 4788f88e1a42SJes Sorensen goto out; 4789f88e1a42SJes Sorensen } 4790f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 4791f88e1a42SJes Sorensen size *= 512; 4792f88e1a42SJes Sorensen 4793f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 4794f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 4795f88e1a42SJes Sorensen } else { 479671c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 4797f88e1a42SJes Sorensen goto out; 4798f88e1a42SJes Sorensen } 4799f88e1a42SJes Sorensen } 4800f88e1a42SJes Sorensen 4801f382d43aSMiroslav Rezanina if (!quiet) { 4802f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 4803f88e1a42SJes Sorensen print_option_parameters(param); 4804f88e1a42SJes Sorensen puts(""); 4805f382d43aSMiroslav Rezanina } 4806cc84d90fSMax Reitz ret = bdrv_create(drv, filename, param, &local_err); 4807cc84d90fSMax Reitz if (ret == -EFBIG) { 4808cc84d90fSMax Reitz /* This is generally a better message than whatever the driver would 4809cc84d90fSMax Reitz * deliver (especially because of the cluster_size_hint), since that 4810cc84d90fSMax Reitz * is most probably not much different from "image too large". */ 4811f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 4812f3f4d2c0SKevin Wolf if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) { 4813f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 4814f3f4d2c0SKevin Wolf } 4815cc84d90fSMax Reitz error_setg(errp, "The image size is too large for file format '%s'" 4816cc84d90fSMax Reitz "%s", fmt, cluster_size_hint); 4817cc84d90fSMax Reitz error_free(local_err); 4818cc84d90fSMax Reitz local_err = NULL; 4819f88e1a42SJes Sorensen } 4820f88e1a42SJes Sorensen 4821f88e1a42SJes Sorensen out: 4822f88e1a42SJes Sorensen free_option_parameters(create_options); 4823f88e1a42SJes Sorensen free_option_parameters(param); 4824f88e1a42SJes Sorensen 4825f88e1a42SJes Sorensen if (bs) { 48264f6fd349SFam Zheng bdrv_unref(bs); 4827f88e1a42SJes Sorensen } 4828cc84d90fSMax Reitz if (error_is_set(&local_err)) { 4829cc84d90fSMax Reitz error_propagate(errp, local_err); 4830cc84d90fSMax Reitz } 4831f88e1a42SJes Sorensen } 483285d126f3SStefan Hajnoczi 483385d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 483485d126f3SStefan Hajnoczi { 483585d126f3SStefan Hajnoczi /* Currently BlockDriverState always uses the main loop AioContext */ 483685d126f3SStefan Hajnoczi return qemu_get_aio_context(); 483785d126f3SStefan Hajnoczi } 4838d616b224SStefan Hajnoczi 4839d616b224SStefan Hajnoczi void bdrv_add_before_write_notifier(BlockDriverState *bs, 4840d616b224SStefan Hajnoczi NotifierWithReturn *notifier) 4841d616b224SStefan Hajnoczi { 4842d616b224SStefan Hajnoczi notifier_with_return_list_add(&bs->before_write_notifiers, notifier); 4843d616b224SStefan Hajnoczi } 48446f176b48SMax Reitz 48456f176b48SMax Reitz int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options) 48466f176b48SMax Reitz { 48476f176b48SMax Reitz if (bs->drv->bdrv_amend_options == NULL) { 48486f176b48SMax Reitz return -ENOTSUP; 48496f176b48SMax Reitz } 48506f176b48SMax Reitz return bs->drv->bdrv_amend_options(bs, options); 48516f176b48SMax Reitz } 4852f6186f49SBenoît Canet 4853f6186f49SBenoît Canet ExtSnapshotPerm bdrv_check_ext_snapshot(BlockDriverState *bs) 4854f6186f49SBenoît Canet { 4855f6186f49SBenoît Canet if (bs->drv->bdrv_check_ext_snapshot) { 4856f6186f49SBenoît Canet return bs->drv->bdrv_check_ext_snapshot(bs); 4857f6186f49SBenoît Canet } 4858f6186f49SBenoît Canet 4859f6186f49SBenoît Canet if (bs->file && bs->file->drv && bs->file->drv->bdrv_check_ext_snapshot) { 4860f6186f49SBenoît Canet return bs->file->drv->bdrv_check_ext_snapshot(bs); 4861f6186f49SBenoît Canet } 4862f6186f49SBenoît Canet 4863f6186f49SBenoît Canet /* external snapshots are allowed by default */ 4864f6186f49SBenoît Canet return EXT_SNAPSHOT_ALLOWED; 4865f6186f49SBenoît Canet } 4866f6186f49SBenoît Canet 4867f6186f49SBenoît Canet ExtSnapshotPerm bdrv_check_ext_snapshot_forbidden(BlockDriverState *bs) 4868f6186f49SBenoît Canet { 4869f6186f49SBenoît Canet return EXT_SNAPSHOT_FORBIDDEN; 4870f6186f49SBenoît Canet } 4871