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" 35c13163fbSBenoît Canet #include "block/qapi.h" 36b2023818SLuiz Capitulino #include "qmp-commands.h" 371de7afc9SPaolo Bonzini #include "qemu/timer.h" 38fc01f7e7Sbellard 3971e72a19SJuan Quintela #ifdef CONFIG_BSD 407674e7bfSbellard #include <sys/types.h> 417674e7bfSbellard #include <sys/stat.h> 427674e7bfSbellard #include <sys/ioctl.h> 4372cf2d4fSBlue Swirl #include <sys/queue.h> 44c5e97233Sblueswir1 #ifndef __DragonFly__ 457674e7bfSbellard #include <sys/disk.h> 467674e7bfSbellard #endif 47c5e97233Sblueswir1 #endif 487674e7bfSbellard 4949dc768dSaliguori #ifdef _WIN32 5049dc768dSaliguori #include <windows.h> 5149dc768dSaliguori #endif 5249dc768dSaliguori 53e4654d2dSFam Zheng struct BdrvDirtyBitmap { 54e4654d2dSFam Zheng HBitmap *bitmap; 55e4654d2dSFam Zheng QLIST_ENTRY(BdrvDirtyBitmap) list; 56e4654d2dSFam Zheng }; 57e4654d2dSFam Zheng 581c9805a3SStefan Hajnoczi #define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */ 591c9805a3SStefan Hajnoczi 607d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load); 61f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 62f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 63c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 64f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 65f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 66ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 67f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 68f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 69f9f05dc5SKevin Wolf QEMUIOVector *iov); 70f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 71f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 72f9f05dc5SKevin Wolf QEMUIOVector *iov); 73c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 74470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 75470c0504SStefan Hajnoczi BdrvRequestFlags flags); 761c9805a3SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 77f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 78f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags); 79b2a61371SStefan Hajnoczi static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 80b2a61371SStefan Hajnoczi int64_t sector_num, 81b2a61371SStefan Hajnoczi QEMUIOVector *qiov, 82b2a61371SStefan Hajnoczi int nb_sectors, 83d20d9b7cSPaolo Bonzini BdrvRequestFlags flags, 84b2a61371SStefan Hajnoczi BlockDriverCompletionFunc *cb, 85b2a61371SStefan Hajnoczi void *opaque, 868c5873d6SStefan Hajnoczi bool is_write); 87b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque); 88621f0589SKevin Wolf static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 89aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags); 90ec530c81Sbellard 911b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 921b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 937ee930d0Sblueswir1 94dc364f4cSBenoît Canet static QTAILQ_HEAD(, BlockDriverState) graph_bdrv_states = 95dc364f4cSBenoît Canet QTAILQ_HEAD_INITIALIZER(graph_bdrv_states); 96dc364f4cSBenoît Canet 978a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 988a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 99ea2384d3Sbellard 100eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 101eb852011SMarkus Armbruster static int use_bdrv_whitelist; 102eb852011SMarkus Armbruster 1039e0b22f4SStefan Hajnoczi #ifdef _WIN32 1049e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 1059e0b22f4SStefan Hajnoczi { 1069e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 1079e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 1089e0b22f4SStefan Hajnoczi filename[1] == ':'); 1099e0b22f4SStefan Hajnoczi } 1109e0b22f4SStefan Hajnoczi 1119e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 1129e0b22f4SStefan Hajnoczi { 1139e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 1149e0b22f4SStefan Hajnoczi filename[2] == '\0') 1159e0b22f4SStefan Hajnoczi return 1; 1169e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1179e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1189e0b22f4SStefan Hajnoczi return 1; 1199e0b22f4SStefan Hajnoczi return 0; 1209e0b22f4SStefan Hajnoczi } 1219e0b22f4SStefan Hajnoczi #endif 1229e0b22f4SStefan Hajnoczi 1230563e191SZhi Yong Wu /* throttling disk I/O limits */ 124cc0681c4SBenoît Canet void bdrv_set_io_limits(BlockDriverState *bs, 125cc0681c4SBenoît Canet ThrottleConfig *cfg) 126cc0681c4SBenoît Canet { 127cc0681c4SBenoît Canet int i; 128cc0681c4SBenoît Canet 129cc0681c4SBenoît Canet throttle_config(&bs->throttle_state, cfg); 130cc0681c4SBenoît Canet 131cc0681c4SBenoît Canet for (i = 0; i < 2; i++) { 132cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[i]); 133cc0681c4SBenoît Canet } 134cc0681c4SBenoît Canet } 135cc0681c4SBenoît Canet 136cc0681c4SBenoît Canet /* this function drain all the throttled IOs */ 137cc0681c4SBenoît Canet static bool bdrv_start_throttled_reqs(BlockDriverState *bs) 138cc0681c4SBenoît Canet { 139cc0681c4SBenoît Canet bool drained = false; 140cc0681c4SBenoît Canet bool enabled = bs->io_limits_enabled; 141cc0681c4SBenoît Canet int i; 142cc0681c4SBenoît Canet 143cc0681c4SBenoît Canet bs->io_limits_enabled = false; 144cc0681c4SBenoît Canet 145cc0681c4SBenoît Canet for (i = 0; i < 2; i++) { 146cc0681c4SBenoît Canet while (qemu_co_enter_next(&bs->throttled_reqs[i])) { 147cc0681c4SBenoît Canet drained = true; 148cc0681c4SBenoît Canet } 149cc0681c4SBenoît Canet } 150cc0681c4SBenoît Canet 151cc0681c4SBenoît Canet bs->io_limits_enabled = enabled; 152cc0681c4SBenoît Canet 153cc0681c4SBenoît Canet return drained; 154cc0681c4SBenoît Canet } 155cc0681c4SBenoît Canet 15698f90dbaSZhi Yong Wu void bdrv_io_limits_disable(BlockDriverState *bs) 15798f90dbaSZhi Yong Wu { 15898f90dbaSZhi Yong Wu bs->io_limits_enabled = false; 15998f90dbaSZhi Yong Wu 160cc0681c4SBenoît Canet bdrv_start_throttled_reqs(bs); 16198f90dbaSZhi Yong Wu 162cc0681c4SBenoît Canet throttle_destroy(&bs->throttle_state); 16398f90dbaSZhi Yong Wu } 16498f90dbaSZhi Yong Wu 165cc0681c4SBenoît Canet static void bdrv_throttle_read_timer_cb(void *opaque) 1660563e191SZhi Yong Wu { 1670563e191SZhi Yong Wu BlockDriverState *bs = opaque; 168cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[0]); 1690563e191SZhi Yong Wu } 1700563e191SZhi Yong Wu 171cc0681c4SBenoît Canet static void bdrv_throttle_write_timer_cb(void *opaque) 172cc0681c4SBenoît Canet { 173cc0681c4SBenoît Canet BlockDriverState *bs = opaque; 174cc0681c4SBenoît Canet qemu_co_enter_next(&bs->throttled_reqs[1]); 175cc0681c4SBenoît Canet } 176cc0681c4SBenoît Canet 177cc0681c4SBenoît Canet /* should be called before bdrv_set_io_limits if a limit is set */ 1780563e191SZhi Yong Wu void bdrv_io_limits_enable(BlockDriverState *bs) 1790563e191SZhi Yong Wu { 180cc0681c4SBenoît Canet assert(!bs->io_limits_enabled); 181cc0681c4SBenoît Canet throttle_init(&bs->throttle_state, 182cc0681c4SBenoît Canet QEMU_CLOCK_VIRTUAL, 183cc0681c4SBenoît Canet bdrv_throttle_read_timer_cb, 184cc0681c4SBenoît Canet bdrv_throttle_write_timer_cb, 185cc0681c4SBenoît Canet bs); 1860563e191SZhi Yong Wu bs->io_limits_enabled = true; 1870563e191SZhi Yong Wu } 1880563e191SZhi Yong Wu 189cc0681c4SBenoît Canet /* This function makes an IO wait if needed 190cc0681c4SBenoît Canet * 191cc0681c4SBenoît Canet * @nb_sectors: the number of sectors of the IO 192cc0681c4SBenoît Canet * @is_write: is the IO a write 19398f90dbaSZhi Yong Wu */ 194cc0681c4SBenoît Canet static void bdrv_io_limits_intercept(BlockDriverState *bs, 195cc0681c4SBenoît Canet int nb_sectors, 196cc0681c4SBenoît Canet bool is_write) 197cc0681c4SBenoît Canet { 198cc0681c4SBenoît Canet /* does this io must wait */ 199cc0681c4SBenoît Canet bool must_wait = throttle_schedule_timer(&bs->throttle_state, is_write); 20098f90dbaSZhi Yong Wu 201cc0681c4SBenoît Canet /* if must wait or any request of this type throttled queue the IO */ 202cc0681c4SBenoît Canet if (must_wait || 203cc0681c4SBenoît Canet !qemu_co_queue_empty(&bs->throttled_reqs[is_write])) { 204cc0681c4SBenoît Canet qemu_co_queue_wait(&bs->throttled_reqs[is_write]); 20598f90dbaSZhi Yong Wu } 20698f90dbaSZhi Yong Wu 207cc0681c4SBenoît Canet /* the IO will be executed, do the accounting */ 208cc0681c4SBenoît Canet throttle_account(&bs->throttle_state, 209cc0681c4SBenoît Canet is_write, 210cc0681c4SBenoît Canet nb_sectors * BDRV_SECTOR_SIZE); 211cc0681c4SBenoît Canet 212cc0681c4SBenoît Canet /* if the next request must wait -> do nothing */ 213cc0681c4SBenoît Canet if (throttle_schedule_timer(&bs->throttle_state, is_write)) { 214cc0681c4SBenoît Canet return; 215cc0681c4SBenoît Canet } 216cc0681c4SBenoît Canet 217cc0681c4SBenoît Canet /* else queue next request for execution */ 218cc0681c4SBenoît Canet qemu_co_queue_next(&bs->throttled_reqs[is_write]); 21998f90dbaSZhi Yong Wu } 22098f90dbaSZhi Yong Wu 2219e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 2229e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 2239e0b22f4SStefan Hajnoczi { 224947995c0SPaolo Bonzini const char *p; 225947995c0SPaolo Bonzini 2269e0b22f4SStefan Hajnoczi #ifdef _WIN32 2279e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 2289e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 2299e0b22f4SStefan Hajnoczi return 0; 2309e0b22f4SStefan Hajnoczi } 231947995c0SPaolo Bonzini p = path + strcspn(path, ":/\\"); 232947995c0SPaolo Bonzini #else 233947995c0SPaolo Bonzini p = path + strcspn(path, ":/"); 2349e0b22f4SStefan Hajnoczi #endif 2359e0b22f4SStefan Hajnoczi 236947995c0SPaolo Bonzini return *p == ':'; 2379e0b22f4SStefan Hajnoczi } 2389e0b22f4SStefan Hajnoczi 23983f64091Sbellard int path_is_absolute(const char *path) 24083f64091Sbellard { 24121664424Sbellard #ifdef _WIN32 24221664424Sbellard /* specific case for names like: "\\.\d:" */ 243f53f4da9SPaolo Bonzini if (is_windows_drive(path) || is_windows_drive_prefix(path)) { 24421664424Sbellard return 1; 245f53f4da9SPaolo Bonzini } 246f53f4da9SPaolo Bonzini return (*path == '/' || *path == '\\'); 2473b9f94e1Sbellard #else 248f53f4da9SPaolo Bonzini return (*path == '/'); 2493b9f94e1Sbellard #endif 25083f64091Sbellard } 25183f64091Sbellard 25283f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 25383f64091Sbellard path to it by considering it is relative to base_path. URL are 25483f64091Sbellard supported. */ 25583f64091Sbellard void path_combine(char *dest, int dest_size, 25683f64091Sbellard const char *base_path, 25783f64091Sbellard const char *filename) 25883f64091Sbellard { 25983f64091Sbellard const char *p, *p1; 26083f64091Sbellard int len; 26183f64091Sbellard 26283f64091Sbellard if (dest_size <= 0) 26383f64091Sbellard return; 26483f64091Sbellard if (path_is_absolute(filename)) { 26583f64091Sbellard pstrcpy(dest, dest_size, filename); 26683f64091Sbellard } else { 26783f64091Sbellard p = strchr(base_path, ':'); 26883f64091Sbellard if (p) 26983f64091Sbellard p++; 27083f64091Sbellard else 27183f64091Sbellard p = base_path; 2723b9f94e1Sbellard p1 = strrchr(base_path, '/'); 2733b9f94e1Sbellard #ifdef _WIN32 2743b9f94e1Sbellard { 2753b9f94e1Sbellard const char *p2; 2763b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 2773b9f94e1Sbellard if (!p1 || p2 > p1) 2783b9f94e1Sbellard p1 = p2; 2793b9f94e1Sbellard } 2803b9f94e1Sbellard #endif 28183f64091Sbellard if (p1) 28283f64091Sbellard p1++; 28383f64091Sbellard else 28483f64091Sbellard p1 = base_path; 28583f64091Sbellard if (p1 > p) 28683f64091Sbellard p = p1; 28783f64091Sbellard len = p - base_path; 28883f64091Sbellard if (len > dest_size - 1) 28983f64091Sbellard len = dest_size - 1; 29083f64091Sbellard memcpy(dest, base_path, len); 29183f64091Sbellard dest[len] = '\0'; 29283f64091Sbellard pstrcat(dest, dest_size, filename); 29383f64091Sbellard } 29483f64091Sbellard } 29583f64091Sbellard 296dc5a1371SPaolo Bonzini void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz) 297dc5a1371SPaolo Bonzini { 298dc5a1371SPaolo Bonzini if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) { 299dc5a1371SPaolo Bonzini pstrcpy(dest, sz, bs->backing_file); 300dc5a1371SPaolo Bonzini } else { 301dc5a1371SPaolo Bonzini path_combine(dest, sz, bs->filename, bs->backing_file); 302dc5a1371SPaolo Bonzini } 303dc5a1371SPaolo Bonzini } 304dc5a1371SPaolo Bonzini 3055efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 306ea2384d3Sbellard { 3078c5873d6SStefan Hajnoczi /* Block drivers without coroutine functions need emulation */ 3088c5873d6SStefan Hajnoczi if (!bdrv->bdrv_co_readv) { 309f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 310f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 311f9f05dc5SKevin Wolf 312f8c35c1dSStefan Hajnoczi /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if 313f8c35c1dSStefan Hajnoczi * the block driver lacks aio we need to emulate that too. 314f8c35c1dSStefan Hajnoczi */ 315f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 31683f64091Sbellard /* add AIO emulation layer */ 317f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 318f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 31983f64091Sbellard } 320f9f05dc5SKevin Wolf } 321b2e12bc6SChristoph Hellwig 3228a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 323ea2384d3Sbellard } 324b338082bSbellard 325b338082bSbellard /* create a new block device (by default it is empty) */ 326b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 327fc01f7e7Sbellard { 3281b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 329b338082bSbellard 3307267c094SAnthony Liguori bs = g_malloc0(sizeof(BlockDriverState)); 331e4654d2dSFam Zheng QLIST_INIT(&bs->dirty_bitmaps); 332b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 333ea2384d3Sbellard if (device_name[0] != '\0') { 334dc364f4cSBenoît Canet QTAILQ_INSERT_TAIL(&bdrv_states, bs, device_list); 335ea2384d3Sbellard } 33628a7282aSLuiz Capitulino bdrv_iostatus_disable(bs); 337d7d512f6SPaolo Bonzini notifier_list_init(&bs->close_notifiers); 338d616b224SStefan Hajnoczi notifier_with_return_list_init(&bs->before_write_notifiers); 339cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[0]); 340cc0681c4SBenoît Canet qemu_co_queue_init(&bs->throttled_reqs[1]); 3419fcb0251SFam Zheng bs->refcnt = 1; 342d7d512f6SPaolo Bonzini 343b338082bSbellard return bs; 344b338082bSbellard } 345b338082bSbellard 346d7d512f6SPaolo Bonzini void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify) 347d7d512f6SPaolo Bonzini { 348d7d512f6SPaolo Bonzini notifier_list_add(&bs->close_notifiers, notify); 349d7d512f6SPaolo Bonzini } 350d7d512f6SPaolo Bonzini 351ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 352ea2384d3Sbellard { 353ea2384d3Sbellard BlockDriver *drv1; 3548a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 3558a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 356ea2384d3Sbellard return drv1; 357ea2384d3Sbellard } 3588a22f02aSStefan Hajnoczi } 359ea2384d3Sbellard return NULL; 360ea2384d3Sbellard } 361ea2384d3Sbellard 362b64ec4e4SFam Zheng static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only) 363eb852011SMarkus Armbruster { 364b64ec4e4SFam Zheng static const char *whitelist_rw[] = { 365b64ec4e4SFam Zheng CONFIG_BDRV_RW_WHITELIST 366b64ec4e4SFam Zheng }; 367b64ec4e4SFam Zheng static const char *whitelist_ro[] = { 368b64ec4e4SFam Zheng CONFIG_BDRV_RO_WHITELIST 369eb852011SMarkus Armbruster }; 370eb852011SMarkus Armbruster const char **p; 371eb852011SMarkus Armbruster 372b64ec4e4SFam Zheng if (!whitelist_rw[0] && !whitelist_ro[0]) { 373eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 374b64ec4e4SFam Zheng } 375eb852011SMarkus Armbruster 376b64ec4e4SFam Zheng for (p = whitelist_rw; *p; p++) { 377eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 378eb852011SMarkus Armbruster return 1; 379eb852011SMarkus Armbruster } 380eb852011SMarkus Armbruster } 381b64ec4e4SFam Zheng if (read_only) { 382b64ec4e4SFam Zheng for (p = whitelist_ro; *p; p++) { 383b64ec4e4SFam Zheng if (!strcmp(drv->format_name, *p)) { 384b64ec4e4SFam Zheng return 1; 385b64ec4e4SFam Zheng } 386b64ec4e4SFam Zheng } 387b64ec4e4SFam Zheng } 388eb852011SMarkus Armbruster return 0; 389eb852011SMarkus Armbruster } 390eb852011SMarkus Armbruster 391b64ec4e4SFam Zheng BlockDriver *bdrv_find_whitelisted_format(const char *format_name, 392b64ec4e4SFam Zheng bool read_only) 393eb852011SMarkus Armbruster { 394eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 395b64ec4e4SFam Zheng return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL; 396eb852011SMarkus Armbruster } 397eb852011SMarkus Armbruster 3985b7e1542SZhi Yong Wu typedef struct CreateCo { 3995b7e1542SZhi Yong Wu BlockDriver *drv; 4005b7e1542SZhi Yong Wu char *filename; 4015b7e1542SZhi Yong Wu QEMUOptionParameter *options; 4025b7e1542SZhi Yong Wu int ret; 403cc84d90fSMax Reitz Error *err; 4045b7e1542SZhi Yong Wu } CreateCo; 4055b7e1542SZhi Yong Wu 4065b7e1542SZhi Yong Wu static void coroutine_fn bdrv_create_co_entry(void *opaque) 4075b7e1542SZhi Yong Wu { 408cc84d90fSMax Reitz Error *local_err = NULL; 409cc84d90fSMax Reitz int ret; 410cc84d90fSMax Reitz 4115b7e1542SZhi Yong Wu CreateCo *cco = opaque; 4125b7e1542SZhi Yong Wu assert(cco->drv); 4135b7e1542SZhi Yong Wu 414cc84d90fSMax Reitz ret = cco->drv->bdrv_create(cco->filename, cco->options, &local_err); 415cc84d90fSMax Reitz if (error_is_set(&local_err)) { 416cc84d90fSMax Reitz error_propagate(&cco->err, local_err); 417cc84d90fSMax Reitz } 418cc84d90fSMax Reitz cco->ret = ret; 4195b7e1542SZhi Yong Wu } 4205b7e1542SZhi Yong Wu 4210e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 422cc84d90fSMax Reitz QEMUOptionParameter *options, Error **errp) 423ea2384d3Sbellard { 4245b7e1542SZhi Yong Wu int ret; 4250e7e1989SKevin Wolf 4265b7e1542SZhi Yong Wu Coroutine *co; 4275b7e1542SZhi Yong Wu CreateCo cco = { 4285b7e1542SZhi Yong Wu .drv = drv, 4295b7e1542SZhi Yong Wu .filename = g_strdup(filename), 4305b7e1542SZhi Yong Wu .options = options, 4315b7e1542SZhi Yong Wu .ret = NOT_DONE, 432cc84d90fSMax Reitz .err = NULL, 4335b7e1542SZhi Yong Wu }; 4345b7e1542SZhi Yong Wu 4355b7e1542SZhi Yong Wu if (!drv->bdrv_create) { 436cc84d90fSMax Reitz error_setg(errp, "Driver '%s' does not support image creation", drv->format_name); 43780168bffSLuiz Capitulino ret = -ENOTSUP; 43880168bffSLuiz Capitulino goto out; 4395b7e1542SZhi Yong Wu } 4405b7e1542SZhi Yong Wu 4415b7e1542SZhi Yong Wu if (qemu_in_coroutine()) { 4425b7e1542SZhi Yong Wu /* Fast-path if already in coroutine context */ 4435b7e1542SZhi Yong Wu bdrv_create_co_entry(&cco); 4445b7e1542SZhi Yong Wu } else { 4455b7e1542SZhi Yong Wu co = qemu_coroutine_create(bdrv_create_co_entry); 4465b7e1542SZhi Yong Wu qemu_coroutine_enter(co, &cco); 4475b7e1542SZhi Yong Wu while (cco.ret == NOT_DONE) { 4485b7e1542SZhi Yong Wu qemu_aio_wait(); 4495b7e1542SZhi Yong Wu } 4505b7e1542SZhi Yong Wu } 4515b7e1542SZhi Yong Wu 4525b7e1542SZhi Yong Wu ret = cco.ret; 453cc84d90fSMax Reitz if (ret < 0) { 454cc84d90fSMax Reitz if (error_is_set(&cco.err)) { 455cc84d90fSMax Reitz error_propagate(errp, cco.err); 456cc84d90fSMax Reitz } else { 457cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not create image"); 458cc84d90fSMax Reitz } 459cc84d90fSMax Reitz } 4605b7e1542SZhi Yong Wu 46180168bffSLuiz Capitulino out: 46280168bffSLuiz Capitulino g_free(cco.filename); 4635b7e1542SZhi Yong Wu return ret; 464ea2384d3Sbellard } 465ea2384d3Sbellard 466cc84d90fSMax Reitz int bdrv_create_file(const char* filename, QEMUOptionParameter *options, 467cc84d90fSMax Reitz Error **errp) 46884a12e66SChristoph Hellwig { 46984a12e66SChristoph Hellwig BlockDriver *drv; 470cc84d90fSMax Reitz Error *local_err = NULL; 471cc84d90fSMax Reitz int ret; 47284a12e66SChristoph Hellwig 47398289620SKevin Wolf drv = bdrv_find_protocol(filename, true); 47484a12e66SChristoph Hellwig if (drv == NULL) { 475cc84d90fSMax Reitz error_setg(errp, "Could not find protocol for file '%s'", filename); 47616905d71SStefan Hajnoczi return -ENOENT; 47784a12e66SChristoph Hellwig } 47884a12e66SChristoph Hellwig 479cc84d90fSMax Reitz ret = bdrv_create(drv, filename, options, &local_err); 480cc84d90fSMax Reitz if (error_is_set(&local_err)) { 481cc84d90fSMax Reitz error_propagate(errp, local_err); 482cc84d90fSMax Reitz } 483cc84d90fSMax Reitz return ret; 48484a12e66SChristoph Hellwig } 48584a12e66SChristoph Hellwig 486eba25057SJim Meyering /* 487eba25057SJim Meyering * Create a uniquely-named empty temporary file. 488eba25057SJim Meyering * Return 0 upon success, otherwise a negative errno value. 489eba25057SJim Meyering */ 490eba25057SJim Meyering int get_tmp_filename(char *filename, int size) 491eba25057SJim Meyering { 492d5249393Sbellard #ifdef _WIN32 4933b9f94e1Sbellard char temp_dir[MAX_PATH]; 494eba25057SJim Meyering /* GetTempFileName requires that its output buffer (4th param) 495eba25057SJim Meyering have length MAX_PATH or greater. */ 496eba25057SJim Meyering assert(size >= MAX_PATH); 497eba25057SJim Meyering return (GetTempPath(MAX_PATH, temp_dir) 498eba25057SJim Meyering && GetTempFileName(temp_dir, "qem", 0, filename) 499eba25057SJim Meyering ? 0 : -GetLastError()); 500d5249393Sbellard #else 501ea2384d3Sbellard int fd; 5027ccfb2ebSblueswir1 const char *tmpdir; 5030badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 5040badc1eeSaurel32 if (!tmpdir) 5050badc1eeSaurel32 tmpdir = "/tmp"; 506eba25057SJim Meyering if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) { 507eba25057SJim Meyering return -EOVERFLOW; 508ea2384d3Sbellard } 509eba25057SJim Meyering fd = mkstemp(filename); 510fe235a06SDunrong Huang if (fd < 0) { 511fe235a06SDunrong Huang return -errno; 512fe235a06SDunrong Huang } 513fe235a06SDunrong Huang if (close(fd) != 0) { 514fe235a06SDunrong Huang unlink(filename); 515eba25057SJim Meyering return -errno; 516eba25057SJim Meyering } 517eba25057SJim Meyering return 0; 518d5249393Sbellard #endif 519eba25057SJim Meyering } 520ea2384d3Sbellard 521f3a5d3f8SChristoph Hellwig /* 522f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 523f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 524f3a5d3f8SChristoph Hellwig */ 525f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 526f3a5d3f8SChristoph Hellwig { 527508c7cb3SChristoph Hellwig int score_max = 0, score; 528508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 529f3a5d3f8SChristoph Hellwig 5308a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 531508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 532508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 533508c7cb3SChristoph Hellwig if (score > score_max) { 534508c7cb3SChristoph Hellwig score_max = score; 535508c7cb3SChristoph Hellwig drv = d; 536f3a5d3f8SChristoph Hellwig } 537508c7cb3SChristoph Hellwig } 538f3a5d3f8SChristoph Hellwig } 539f3a5d3f8SChristoph Hellwig 540508c7cb3SChristoph Hellwig return drv; 541f3a5d3f8SChristoph Hellwig } 542f3a5d3f8SChristoph Hellwig 54398289620SKevin Wolf BlockDriver *bdrv_find_protocol(const char *filename, 54498289620SKevin Wolf bool allow_protocol_prefix) 54584a12e66SChristoph Hellwig { 54684a12e66SChristoph Hellwig BlockDriver *drv1; 54784a12e66SChristoph Hellwig char protocol[128]; 54884a12e66SChristoph Hellwig int len; 54984a12e66SChristoph Hellwig const char *p; 55084a12e66SChristoph Hellwig 55166f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 55266f82ceeSKevin Wolf 55339508e7aSChristoph Hellwig /* 55439508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 55539508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 55639508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 55739508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 55839508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 55939508e7aSChristoph Hellwig */ 56084a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 56139508e7aSChristoph Hellwig if (drv1) { 56284a12e66SChristoph Hellwig return drv1; 56384a12e66SChristoph Hellwig } 56439508e7aSChristoph Hellwig 56598289620SKevin Wolf if (!path_has_protocol(filename) || !allow_protocol_prefix) { 56639508e7aSChristoph Hellwig return bdrv_find_format("file"); 56739508e7aSChristoph Hellwig } 56898289620SKevin Wolf 5699e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 5709e0b22f4SStefan Hajnoczi assert(p != NULL); 57184a12e66SChristoph Hellwig len = p - filename; 57284a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 57384a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 57484a12e66SChristoph Hellwig memcpy(protocol, filename, len); 57584a12e66SChristoph Hellwig protocol[len] = '\0'; 57684a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 57784a12e66SChristoph Hellwig if (drv1->protocol_name && 57884a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 57984a12e66SChristoph Hellwig return drv1; 58084a12e66SChristoph Hellwig } 58184a12e66SChristoph Hellwig } 58284a12e66SChristoph Hellwig return NULL; 58384a12e66SChristoph Hellwig } 58484a12e66SChristoph Hellwig 585f500a6d3SKevin Wolf static int find_image_format(BlockDriverState *bs, const char *filename, 58634b5d2c6SMax Reitz BlockDriver **pdrv, Error **errp) 587ea2384d3Sbellard { 588f500a6d3SKevin Wolf int score, score_max; 589ea2384d3Sbellard BlockDriver *drv1, *drv; 59083f64091Sbellard uint8_t buf[2048]; 591f500a6d3SKevin Wolf int ret = 0; 592f8ea0b00SNicholas Bellinger 59308a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 5948e895599SPaolo Bonzini if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) { 595c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 596c98ac35dSStefan Weil if (!drv) { 59734b5d2c6SMax Reitz error_setg(errp, "Could not find raw image format"); 598c98ac35dSStefan Weil ret = -ENOENT; 599c98ac35dSStefan Weil } 600c98ac35dSStefan Weil *pdrv = drv; 601c98ac35dSStefan Weil return ret; 6021a396859SNicholas A. Bellinger } 603f8ea0b00SNicholas Bellinger 60483f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 605ea2384d3Sbellard if (ret < 0) { 60634b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not read image for determining its " 60734b5d2c6SMax Reitz "format"); 608c98ac35dSStefan Weil *pdrv = NULL; 609c98ac35dSStefan Weil return ret; 610ea2384d3Sbellard } 611ea2384d3Sbellard 612ea2384d3Sbellard score_max = 0; 61384a12e66SChristoph Hellwig drv = NULL; 6148a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 61583f64091Sbellard if (drv1->bdrv_probe) { 616ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 617ea2384d3Sbellard if (score > score_max) { 618ea2384d3Sbellard score_max = score; 619ea2384d3Sbellard drv = drv1; 620ea2384d3Sbellard } 621ea2384d3Sbellard } 62283f64091Sbellard } 623c98ac35dSStefan Weil if (!drv) { 62434b5d2c6SMax Reitz error_setg(errp, "Could not determine image format: No compatible " 62534b5d2c6SMax Reitz "driver found"); 626c98ac35dSStefan Weil ret = -ENOENT; 627c98ac35dSStefan Weil } 628c98ac35dSStefan Weil *pdrv = drv; 629c98ac35dSStefan Weil return ret; 630ea2384d3Sbellard } 631ea2384d3Sbellard 63251762288SStefan Hajnoczi /** 63351762288SStefan Hajnoczi * Set the current 'total_sectors' value 63451762288SStefan Hajnoczi */ 63551762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 63651762288SStefan Hajnoczi { 63751762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 63851762288SStefan Hajnoczi 639396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 640396759adSNicholas Bellinger if (bs->sg) 641396759adSNicholas Bellinger return 0; 642396759adSNicholas Bellinger 64351762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 64451762288SStefan Hajnoczi if (drv->bdrv_getlength) { 64551762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 64651762288SStefan Hajnoczi if (length < 0) { 64751762288SStefan Hajnoczi return length; 64851762288SStefan Hajnoczi } 6497e382003SFam Zheng hint = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE); 65051762288SStefan Hajnoczi } 65151762288SStefan Hajnoczi 65251762288SStefan Hajnoczi bs->total_sectors = hint; 65351762288SStefan Hajnoczi return 0; 65451762288SStefan Hajnoczi } 65551762288SStefan Hajnoczi 656c3993cdcSStefan Hajnoczi /** 6579e8f1835SPaolo Bonzini * Set open flags for a given discard mode 6589e8f1835SPaolo Bonzini * 6599e8f1835SPaolo Bonzini * Return 0 on success, -1 if the discard mode was invalid. 6609e8f1835SPaolo Bonzini */ 6619e8f1835SPaolo Bonzini int bdrv_parse_discard_flags(const char *mode, int *flags) 6629e8f1835SPaolo Bonzini { 6639e8f1835SPaolo Bonzini *flags &= ~BDRV_O_UNMAP; 6649e8f1835SPaolo Bonzini 6659e8f1835SPaolo Bonzini if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) { 6669e8f1835SPaolo Bonzini /* do nothing */ 6679e8f1835SPaolo Bonzini } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) { 6689e8f1835SPaolo Bonzini *flags |= BDRV_O_UNMAP; 6699e8f1835SPaolo Bonzini } else { 6709e8f1835SPaolo Bonzini return -1; 6719e8f1835SPaolo Bonzini } 6729e8f1835SPaolo Bonzini 6739e8f1835SPaolo Bonzini return 0; 6749e8f1835SPaolo Bonzini } 6759e8f1835SPaolo Bonzini 6769e8f1835SPaolo Bonzini /** 677c3993cdcSStefan Hajnoczi * Set open flags for a given cache mode 678c3993cdcSStefan Hajnoczi * 679c3993cdcSStefan Hajnoczi * Return 0 on success, -1 if the cache mode was invalid. 680c3993cdcSStefan Hajnoczi */ 681c3993cdcSStefan Hajnoczi int bdrv_parse_cache_flags(const char *mode, int *flags) 682c3993cdcSStefan Hajnoczi { 683c3993cdcSStefan Hajnoczi *flags &= ~BDRV_O_CACHE_MASK; 684c3993cdcSStefan Hajnoczi 685c3993cdcSStefan Hajnoczi if (!strcmp(mode, "off") || !strcmp(mode, "none")) { 686c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; 68792196b2fSStefan Hajnoczi } else if (!strcmp(mode, "directsync")) { 68892196b2fSStefan Hajnoczi *flags |= BDRV_O_NOCACHE; 689c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writeback")) { 690c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 691c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "unsafe")) { 692c3993cdcSStefan Hajnoczi *flags |= BDRV_O_CACHE_WB; 693c3993cdcSStefan Hajnoczi *flags |= BDRV_O_NO_FLUSH; 694c3993cdcSStefan Hajnoczi } else if (!strcmp(mode, "writethrough")) { 695c3993cdcSStefan Hajnoczi /* this is the default */ 696c3993cdcSStefan Hajnoczi } else { 697c3993cdcSStefan Hajnoczi return -1; 698c3993cdcSStefan Hajnoczi } 699c3993cdcSStefan Hajnoczi 700c3993cdcSStefan Hajnoczi return 0; 701c3993cdcSStefan Hajnoczi } 702c3993cdcSStefan Hajnoczi 70353fec9d3SStefan Hajnoczi /** 70453fec9d3SStefan Hajnoczi * The copy-on-read flag is actually a reference count so multiple users may 70553fec9d3SStefan Hajnoczi * use the feature without worrying about clobbering its previous state. 70653fec9d3SStefan Hajnoczi * Copy-on-read stays enabled until all users have called to disable it. 70753fec9d3SStefan Hajnoczi */ 70853fec9d3SStefan Hajnoczi void bdrv_enable_copy_on_read(BlockDriverState *bs) 70953fec9d3SStefan Hajnoczi { 71053fec9d3SStefan Hajnoczi bs->copy_on_read++; 71153fec9d3SStefan Hajnoczi } 71253fec9d3SStefan Hajnoczi 71353fec9d3SStefan Hajnoczi void bdrv_disable_copy_on_read(BlockDriverState *bs) 71453fec9d3SStefan Hajnoczi { 71553fec9d3SStefan Hajnoczi assert(bs->copy_on_read > 0); 71653fec9d3SStefan Hajnoczi bs->copy_on_read--; 71753fec9d3SStefan Hajnoczi } 71853fec9d3SStefan Hajnoczi 7197b272452SKevin Wolf static int bdrv_open_flags(BlockDriverState *bs, int flags) 7207b272452SKevin Wolf { 7217b272452SKevin Wolf int open_flags = flags | BDRV_O_CACHE_WB; 7227b272452SKevin Wolf 7237b272452SKevin Wolf /* 7247b272452SKevin Wolf * Clear flags that are internal to the block layer before opening the 7257b272452SKevin Wolf * image. 7267b272452SKevin Wolf */ 7277b272452SKevin Wolf open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 7287b272452SKevin Wolf 7297b272452SKevin Wolf /* 7307b272452SKevin Wolf * Snapshots should be writable. 7317b272452SKevin Wolf */ 7327b272452SKevin Wolf if (bs->is_temporary) { 7337b272452SKevin Wolf open_flags |= BDRV_O_RDWR; 7347b272452SKevin Wolf } 7357b272452SKevin Wolf 7367b272452SKevin Wolf return open_flags; 7377b272452SKevin Wolf } 7387b272452SKevin Wolf 7396913c0c2SBenoît Canet static int bdrv_assign_node_name(BlockDriverState *bs, 7406913c0c2SBenoît Canet const char *node_name, 7416913c0c2SBenoît Canet Error **errp) 7426913c0c2SBenoît Canet { 7436913c0c2SBenoît Canet if (!node_name) { 7446913c0c2SBenoît Canet return 0; 7456913c0c2SBenoît Canet } 7466913c0c2SBenoît Canet 7476913c0c2SBenoît Canet /* empty string node name is invalid */ 7486913c0c2SBenoît Canet if (node_name[0] == '\0') { 7496913c0c2SBenoît Canet error_setg(errp, "Empty node name"); 7506913c0c2SBenoît Canet return -EINVAL; 7516913c0c2SBenoît Canet } 7526913c0c2SBenoît Canet 7536913c0c2SBenoît Canet /* takes care of avoiding duplicates node names */ 7546913c0c2SBenoît Canet if (bdrv_find_node(node_name)) { 7556913c0c2SBenoît Canet error_setg(errp, "Duplicate node name"); 7566913c0c2SBenoît Canet return -EINVAL; 7576913c0c2SBenoît Canet } 7586913c0c2SBenoît Canet 7596913c0c2SBenoît Canet /* copy node name into the bs and insert it into the graph list */ 7606913c0c2SBenoît Canet pstrcpy(bs->node_name, sizeof(bs->node_name), node_name); 7616913c0c2SBenoît Canet QTAILQ_INSERT_TAIL(&graph_bdrv_states, bs, node_list); 7626913c0c2SBenoît Canet 7636913c0c2SBenoît Canet return 0; 7646913c0c2SBenoît Canet } 7656913c0c2SBenoît Canet 766b6ce07aaSKevin Wolf /* 76757915332SKevin Wolf * Common part for opening disk images and files 768b6ad491aSKevin Wolf * 769b6ad491aSKevin Wolf * Removes all processed options from *options. 77057915332SKevin Wolf */ 771f500a6d3SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file, 77234b5d2c6SMax Reitz QDict *options, int flags, BlockDriver *drv, Error **errp) 77357915332SKevin Wolf { 77457915332SKevin Wolf int ret, open_flags; 775035fccdfSKevin Wolf const char *filename; 7766913c0c2SBenoît Canet const char *node_name = NULL; 77734b5d2c6SMax Reitz Error *local_err = NULL; 77857915332SKevin Wolf 77957915332SKevin Wolf assert(drv != NULL); 7806405875cSPaolo Bonzini assert(bs->file == NULL); 781707ff828SKevin Wolf assert(options != NULL && bs->options != options); 78257915332SKevin Wolf 78345673671SKevin Wolf if (file != NULL) { 78445673671SKevin Wolf filename = file->filename; 78545673671SKevin Wolf } else { 78645673671SKevin Wolf filename = qdict_get_try_str(options, "filename"); 78745673671SKevin Wolf } 78845673671SKevin Wolf 78945673671SKevin Wolf trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name); 79028dcee10SStefan Hajnoczi 7916913c0c2SBenoît Canet node_name = qdict_get_try_str(options, "node-name"); 7926913c0c2SBenoît Canet ret = bdrv_assign_node_name(bs, node_name, errp); 7936913c0c2SBenoît Canet if (ret < 0) { 7946913c0c2SBenoît Canet return ret; 7956913c0c2SBenoît Canet } 7966913c0c2SBenoît Canet qdict_del(options, "node-name"); 7976913c0c2SBenoît Canet 7985d186eb0SKevin Wolf /* bdrv_open() with directly using a protocol as drv. This layer is already 7995d186eb0SKevin Wolf * opened, so assign it to bs (while file becomes a closed BlockDriverState) 8005d186eb0SKevin Wolf * and return immediately. */ 8015d186eb0SKevin Wolf if (file != NULL && drv->bdrv_file_open) { 8025d186eb0SKevin Wolf bdrv_swap(file, bs); 8035d186eb0SKevin Wolf return 0; 8045d186eb0SKevin Wolf } 8055d186eb0SKevin Wolf 80657915332SKevin Wolf bs->open_flags = flags; 80757915332SKevin Wolf bs->buffer_alignment = 512; 8080d51b4deSAsias He bs->zero_beyond_eof = true; 809b64ec4e4SFam Zheng open_flags = bdrv_open_flags(bs, flags); 810b64ec4e4SFam Zheng bs->read_only = !(open_flags & BDRV_O_RDWR); 811b64ec4e4SFam Zheng 812b64ec4e4SFam Zheng if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) { 8138f94a6e4SKevin Wolf error_setg(errp, 8148f94a6e4SKevin Wolf !bs->read_only && bdrv_is_whitelisted(drv, true) 8158f94a6e4SKevin Wolf ? "Driver '%s' can only be used for read-only devices" 8168f94a6e4SKevin Wolf : "Driver '%s' is not whitelisted", 8178f94a6e4SKevin Wolf drv->format_name); 818b64ec4e4SFam Zheng return -ENOTSUP; 819b64ec4e4SFam Zheng } 82057915332SKevin Wolf 82153fec9d3SStefan Hajnoczi assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */ 8220ebd24e0SKevin Wolf if (flags & BDRV_O_COPY_ON_READ) { 8230ebd24e0SKevin Wolf if (!bs->read_only) { 82453fec9d3SStefan Hajnoczi bdrv_enable_copy_on_read(bs); 8250ebd24e0SKevin Wolf } else { 8260ebd24e0SKevin Wolf error_setg(errp, "Can't use copy-on-read on read-only device"); 8270ebd24e0SKevin Wolf return -EINVAL; 8280ebd24e0SKevin Wolf } 82953fec9d3SStefan Hajnoczi } 83053fec9d3SStefan Hajnoczi 831c2ad1b0cSKevin Wolf if (filename != NULL) { 83257915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 833c2ad1b0cSKevin Wolf } else { 834c2ad1b0cSKevin Wolf bs->filename[0] = '\0'; 835c2ad1b0cSKevin Wolf } 83657915332SKevin Wolf 83757915332SKevin Wolf bs->drv = drv; 8387267c094SAnthony Liguori bs->opaque = g_malloc0(drv->instance_size); 83957915332SKevin Wolf 84003f541bdSStefan Hajnoczi bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB); 841e7c63796SStefan Hajnoczi 84266f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 84366f82ceeSKevin Wolf if (drv->bdrv_file_open) { 8445d186eb0SKevin Wolf assert(file == NULL); 845030be321SBenoît Canet assert(!drv->bdrv_needs_filename || filename != NULL); 84634b5d2c6SMax Reitz ret = drv->bdrv_file_open(bs, options, open_flags, &local_err); 847f500a6d3SKevin Wolf } else { 8482af5ef70SKevin Wolf if (file == NULL) { 84934b5d2c6SMax Reitz error_setg(errp, "Can't use '%s' as a block driver for the " 85034b5d2c6SMax Reitz "protocol level", drv->format_name); 8512af5ef70SKevin Wolf ret = -EINVAL; 8522af5ef70SKevin Wolf goto free_and_fail; 8532af5ef70SKevin Wolf } 854f500a6d3SKevin Wolf bs->file = file; 85534b5d2c6SMax Reitz ret = drv->bdrv_open(bs, options, open_flags, &local_err); 85666f82ceeSKevin Wolf } 85766f82ceeSKevin Wolf 85857915332SKevin Wolf if (ret < 0) { 85934b5d2c6SMax Reitz if (error_is_set(&local_err)) { 86034b5d2c6SMax Reitz error_propagate(errp, local_err); 8612fa9aa59SDunrong Huang } else if (bs->filename[0]) { 8622fa9aa59SDunrong Huang error_setg_errno(errp, -ret, "Could not open '%s'", bs->filename); 86334b5d2c6SMax Reitz } else { 86434b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not open image"); 86534b5d2c6SMax Reitz } 86657915332SKevin Wolf goto free_and_fail; 86757915332SKevin Wolf } 86857915332SKevin Wolf 86951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 87051762288SStefan Hajnoczi if (ret < 0) { 87134b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not refresh total sector count"); 87251762288SStefan Hajnoczi goto free_and_fail; 87357915332SKevin Wolf } 87451762288SStefan Hajnoczi 87557915332SKevin Wolf #ifndef _WIN32 87657915332SKevin Wolf if (bs->is_temporary) { 877d4cea8dfSDunrong Huang assert(bs->filename[0] != '\0'); 878d4cea8dfSDunrong Huang unlink(bs->filename); 87957915332SKevin Wolf } 88057915332SKevin Wolf #endif 88157915332SKevin Wolf return 0; 88257915332SKevin Wolf 88357915332SKevin Wolf free_and_fail: 88466f82ceeSKevin Wolf bs->file = NULL; 8857267c094SAnthony Liguori g_free(bs->opaque); 88657915332SKevin Wolf bs->opaque = NULL; 88757915332SKevin Wolf bs->drv = NULL; 88857915332SKevin Wolf return ret; 88957915332SKevin Wolf } 89057915332SKevin Wolf 89157915332SKevin Wolf /* 892b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 893787e4a85SKevin Wolf * 894787e4a85SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 895787e4a85SKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 896787e4a85SKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 897787e4a85SKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_file_open. 898b6ce07aaSKevin Wolf */ 899787e4a85SKevin Wolf int bdrv_file_open(BlockDriverState **pbs, const char *filename, 90072daa72eSMax Reitz const char *reference, QDict *options, int flags, 90172daa72eSMax Reitz Error **errp) 902b338082bSbellard { 90372daa72eSMax Reitz BlockDriverState *bs = NULL; 9046db95603SChristoph Hellwig BlockDriver *drv; 905c2ad1b0cSKevin Wolf const char *drvname; 90698289620SKevin Wolf bool allow_protocol_prefix = false; 90734b5d2c6SMax Reitz Error *local_err = NULL; 90883f64091Sbellard int ret; 9093b0d4f61Sbellard 910707ff828SKevin Wolf /* NULL means an empty set of options */ 911707ff828SKevin Wolf if (options == NULL) { 912707ff828SKevin Wolf options = qdict_new(); 9133b0d4f61Sbellard } 914707ff828SKevin Wolf 91572daa72eSMax Reitz if (reference) { 91672daa72eSMax Reitz if (filename || qdict_size(options)) { 91772daa72eSMax Reitz error_setg(errp, "Cannot reference an existing block device with " 91872daa72eSMax Reitz "additional options or a new filename"); 91972daa72eSMax Reitz return -EINVAL; 92072daa72eSMax Reitz } 92172daa72eSMax Reitz QDECREF(options); 92272daa72eSMax Reitz 92372daa72eSMax Reitz bs = bdrv_find(reference); 92472daa72eSMax Reitz if (!bs) { 92572daa72eSMax Reitz error_setg(errp, "Cannot find block device '%s'", reference); 92672daa72eSMax Reitz return -ENODEV; 92772daa72eSMax Reitz } 92872daa72eSMax Reitz bdrv_ref(bs); 92972daa72eSMax Reitz *pbs = bs; 93072daa72eSMax Reitz return 0; 93172daa72eSMax Reitz } 93272daa72eSMax Reitz 933707ff828SKevin Wolf bs = bdrv_new(""); 934707ff828SKevin Wolf bs->options = options; 935707ff828SKevin Wolf options = qdict_clone_shallow(options); 936707ff828SKevin Wolf 937035fccdfSKevin Wolf /* Fetch the file name from the options QDict if necessary */ 938035fccdfSKevin Wolf if (!filename) { 939035fccdfSKevin Wolf filename = qdict_get_try_str(options, "filename"); 940035fccdfSKevin Wolf } else if (filename && !qdict_haskey(options, "filename")) { 941035fccdfSKevin Wolf qdict_put(options, "filename", qstring_from_str(filename)); 94298289620SKevin Wolf allow_protocol_prefix = true; 943035fccdfSKevin Wolf } else { 94434b5d2c6SMax Reitz error_setg(errp, "Can't specify 'file' and 'filename' options at the " 94534b5d2c6SMax Reitz "same time"); 946035fccdfSKevin Wolf ret = -EINVAL; 947035fccdfSKevin Wolf goto fail; 948035fccdfSKevin Wolf } 949035fccdfSKevin Wolf 950c2ad1b0cSKevin Wolf /* Find the right block driver */ 951c2ad1b0cSKevin Wolf drvname = qdict_get_try_str(options, "driver"); 952c2ad1b0cSKevin Wolf if (drvname) { 9538f94a6e4SKevin Wolf drv = bdrv_find_format(drvname); 95434b5d2c6SMax Reitz if (!drv) { 95534b5d2c6SMax Reitz error_setg(errp, "Unknown driver '%s'", drvname); 95634b5d2c6SMax Reitz } 957c2ad1b0cSKevin Wolf qdict_del(options, "driver"); 958c2ad1b0cSKevin Wolf } else if (filename) { 95998289620SKevin Wolf drv = bdrv_find_protocol(filename, allow_protocol_prefix); 96098289620SKevin Wolf if (!drv) { 96134b5d2c6SMax Reitz error_setg(errp, "Unknown protocol"); 96298289620SKevin Wolf } 963c2ad1b0cSKevin Wolf } else { 96434b5d2c6SMax Reitz error_setg(errp, "Must specify either driver or file"); 965c2ad1b0cSKevin Wolf drv = NULL; 966c2ad1b0cSKevin Wolf } 967c2ad1b0cSKevin Wolf 968c2ad1b0cSKevin Wolf if (!drv) { 96934b5d2c6SMax Reitz /* errp has been set already */ 970c2ad1b0cSKevin Wolf ret = -ENOENT; 971c2ad1b0cSKevin Wolf goto fail; 972c2ad1b0cSKevin Wolf } 973c2ad1b0cSKevin Wolf 974c2ad1b0cSKevin Wolf /* Parse the filename and open it */ 975c2ad1b0cSKevin Wolf if (drv->bdrv_parse_filename && filename) { 9766963a30dSKevin Wolf drv->bdrv_parse_filename(filename, options, &local_err); 9776963a30dSKevin Wolf if (error_is_set(&local_err)) { 97834b5d2c6SMax Reitz error_propagate(errp, local_err); 9796963a30dSKevin Wolf ret = -EINVAL; 9806963a30dSKevin Wolf goto fail; 9816963a30dSKevin Wolf } 98256d1b4d2SKevin Wolf qdict_del(options, "filename"); 983030be321SBenoît Canet } else if (drv->bdrv_needs_filename && !filename) { 98434b5d2c6SMax Reitz error_setg(errp, "The '%s' block driver requires a file name", 985c2ad1b0cSKevin Wolf drv->format_name); 986c2ad1b0cSKevin Wolf ret = -EINVAL; 987c2ad1b0cSKevin Wolf goto fail; 9886963a30dSKevin Wolf } 9896963a30dSKevin Wolf 990505d7583SMax Reitz if (!drv->bdrv_file_open) { 991505d7583SMax Reitz ret = bdrv_open(bs, filename, options, flags, drv, &local_err); 992505d7583SMax Reitz options = NULL; 993505d7583SMax Reitz } else { 99434b5d2c6SMax Reitz ret = bdrv_open_common(bs, NULL, options, flags, drv, &local_err); 995505d7583SMax Reitz } 996707ff828SKevin Wolf if (ret < 0) { 99734b5d2c6SMax Reitz error_propagate(errp, local_err); 998707ff828SKevin Wolf goto fail; 999707ff828SKevin Wolf } 1000707ff828SKevin Wolf 1001707ff828SKevin Wolf /* Check if any unknown options were used */ 1002505d7583SMax Reitz if (options && (qdict_size(options) != 0)) { 1003707ff828SKevin Wolf const QDictEntry *entry = qdict_first(options); 100434b5d2c6SMax Reitz error_setg(errp, "Block protocol '%s' doesn't support the option '%s'", 1005707ff828SKevin Wolf drv->format_name, entry->key); 1006707ff828SKevin Wolf ret = -EINVAL; 1007707ff828SKevin Wolf goto fail; 1008707ff828SKevin Wolf } 1009707ff828SKevin Wolf QDECREF(options); 1010707ff828SKevin Wolf 101171d0770cSaliguori bs->growable = 1; 101283f64091Sbellard *pbs = bs; 101383f64091Sbellard return 0; 1014707ff828SKevin Wolf 1015707ff828SKevin Wolf fail: 1016707ff828SKevin Wolf QDECREF(options); 1017707ff828SKevin Wolf if (!bs->drv) { 1018707ff828SKevin Wolf QDECREF(bs->options); 1019707ff828SKevin Wolf } 10204f6fd349SFam Zheng bdrv_unref(bs); 1021707ff828SKevin Wolf return ret; 10223b0d4f61Sbellard } 10233b0d4f61Sbellard 102431ca6d07SKevin Wolf /* 102531ca6d07SKevin Wolf * Opens the backing file for a BlockDriverState if not yet open 102631ca6d07SKevin Wolf * 102731ca6d07SKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 102831ca6d07SKevin Wolf * empty set of options. The reference to the QDict is transferred to this 102931ca6d07SKevin Wolf * function (even on failure), so if the caller intends to reuse the dictionary, 103031ca6d07SKevin Wolf * it needs to use QINCREF() before calling bdrv_file_open. 103131ca6d07SKevin Wolf */ 103234b5d2c6SMax Reitz int bdrv_open_backing_file(BlockDriverState *bs, QDict *options, Error **errp) 10339156df12SPaolo Bonzini { 10349156df12SPaolo Bonzini char backing_filename[PATH_MAX]; 10359156df12SPaolo Bonzini int back_flags, ret; 10369156df12SPaolo Bonzini BlockDriver *back_drv = NULL; 103734b5d2c6SMax Reitz Error *local_err = NULL; 10389156df12SPaolo Bonzini 10399156df12SPaolo Bonzini if (bs->backing_hd != NULL) { 104031ca6d07SKevin Wolf QDECREF(options); 10419156df12SPaolo Bonzini return 0; 10429156df12SPaolo Bonzini } 10439156df12SPaolo Bonzini 104431ca6d07SKevin Wolf /* NULL means an empty set of options */ 104531ca6d07SKevin Wolf if (options == NULL) { 104631ca6d07SKevin Wolf options = qdict_new(); 104731ca6d07SKevin Wolf } 104831ca6d07SKevin Wolf 10499156df12SPaolo Bonzini bs->open_flags &= ~BDRV_O_NO_BACKING; 10501cb6f506SKevin Wolf if (qdict_haskey(options, "file.filename")) { 10511cb6f506SKevin Wolf backing_filename[0] = '\0'; 10521cb6f506SKevin Wolf } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) { 105331ca6d07SKevin Wolf QDECREF(options); 10549156df12SPaolo Bonzini return 0; 1055dbecebddSFam Zheng } else { 1056dbecebddSFam Zheng bdrv_get_full_backing_filename(bs, backing_filename, 1057dbecebddSFam Zheng sizeof(backing_filename)); 10589156df12SPaolo Bonzini } 10599156df12SPaolo Bonzini 10609156df12SPaolo Bonzini bs->backing_hd = bdrv_new(""); 10619156df12SPaolo Bonzini 10629156df12SPaolo Bonzini if (bs->backing_format[0] != '\0') { 10639156df12SPaolo Bonzini back_drv = bdrv_find_format(bs->backing_format); 10649156df12SPaolo Bonzini } 10659156df12SPaolo Bonzini 10669156df12SPaolo Bonzini /* backing files always opened read-only */ 106787a5debdSThibaut LAURENT back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | 106887a5debdSThibaut LAURENT BDRV_O_COPY_ON_READ); 10699156df12SPaolo Bonzini 107031ca6d07SKevin Wolf ret = bdrv_open(bs->backing_hd, 107131ca6d07SKevin Wolf *backing_filename ? backing_filename : NULL, options, 107234b5d2c6SMax Reitz back_flags, back_drv, &local_err); 10739156df12SPaolo Bonzini if (ret < 0) { 10744f6fd349SFam Zheng bdrv_unref(bs->backing_hd); 10759156df12SPaolo Bonzini bs->backing_hd = NULL; 10769156df12SPaolo Bonzini bs->open_flags |= BDRV_O_NO_BACKING; 1077b04b6b6eSFam Zheng error_setg(errp, "Could not open backing file: %s", 1078b04b6b6eSFam Zheng error_get_pretty(local_err)); 1079b04b6b6eSFam Zheng error_free(local_err); 10809156df12SPaolo Bonzini return ret; 10819156df12SPaolo Bonzini } 1082d80ac658SPeter Feiner 1083d80ac658SPeter Feiner if (bs->backing_hd->file) { 108461ed2684SMax Reitz pstrcpy(bs->backing_file, sizeof(bs->backing_file), 108561ed2684SMax Reitz bs->backing_hd->file->filename); 1086d80ac658SPeter Feiner } 1087d80ac658SPeter Feiner 10889156df12SPaolo Bonzini return 0; 10899156df12SPaolo Bonzini } 10909156df12SPaolo Bonzini 1091b6ce07aaSKevin Wolf /* 1092da557aacSMax Reitz * Opens a disk image whose options are given as BlockdevRef in another block 1093da557aacSMax Reitz * device's options. 1094da557aacSMax Reitz * 1095da557aacSMax Reitz * If force_raw is true, bdrv_file_open() will be used, thereby preventing any 1096da557aacSMax Reitz * image format auto-detection. If it is false and a filename is given, 1097da557aacSMax Reitz * bdrv_open() will be used for auto-detection. 1098da557aacSMax Reitz * 1099da557aacSMax Reitz * If allow_none is true, no image will be opened if filename is false and no 1100da557aacSMax Reitz * BlockdevRef is given. *pbs will remain unchanged and 0 will be returned. 1101da557aacSMax Reitz * 1102da557aacSMax Reitz * bdrev_key specifies the key for the image's BlockdevRef in the options QDict. 1103da557aacSMax Reitz * That QDict has to be flattened; therefore, if the BlockdevRef is a QDict 1104da557aacSMax Reitz * itself, all options starting with "${bdref_key}." are considered part of the 1105da557aacSMax Reitz * BlockdevRef. 1106da557aacSMax Reitz * 1107da557aacSMax Reitz * The BlockdevRef will be removed from the options QDict. 1108da557aacSMax Reitz */ 1109da557aacSMax Reitz int bdrv_open_image(BlockDriverState **pbs, const char *filename, 1110da557aacSMax Reitz QDict *options, const char *bdref_key, int flags, 1111da557aacSMax Reitz bool force_raw, bool allow_none, Error **errp) 1112da557aacSMax Reitz { 1113da557aacSMax Reitz QDict *image_options; 1114da557aacSMax Reitz int ret; 1115da557aacSMax Reitz char *bdref_key_dot; 1116da557aacSMax Reitz const char *reference; 1117da557aacSMax Reitz 1118da557aacSMax Reitz bdref_key_dot = g_strdup_printf("%s.", bdref_key); 1119da557aacSMax Reitz qdict_extract_subqdict(options, &image_options, bdref_key_dot); 1120da557aacSMax Reitz g_free(bdref_key_dot); 1121da557aacSMax Reitz 1122da557aacSMax Reitz reference = qdict_get_try_str(options, bdref_key); 1123da557aacSMax Reitz if (!filename && !reference && !qdict_size(image_options)) { 1124da557aacSMax Reitz if (allow_none) { 1125da557aacSMax Reitz ret = 0; 1126da557aacSMax Reitz } else { 1127da557aacSMax Reitz error_setg(errp, "A block device must be specified for \"%s\"", 1128da557aacSMax Reitz bdref_key); 1129da557aacSMax Reitz ret = -EINVAL; 1130da557aacSMax Reitz } 1131da557aacSMax Reitz goto done; 1132da557aacSMax Reitz } 1133da557aacSMax Reitz 1134da557aacSMax Reitz if (filename && !force_raw) { 1135da557aacSMax Reitz /* If a filename is given and the block driver should be detected 1136da557aacSMax Reitz automatically (instead of using none), use bdrv_open() in order to do 1137da557aacSMax Reitz that auto-detection. */ 1138da557aacSMax Reitz BlockDriverState *bs; 1139da557aacSMax Reitz 1140da557aacSMax Reitz if (reference) { 1141da557aacSMax Reitz error_setg(errp, "Cannot reference an existing block device while " 1142da557aacSMax Reitz "giving a filename"); 1143da557aacSMax Reitz ret = -EINVAL; 1144da557aacSMax Reitz goto done; 1145da557aacSMax Reitz } 1146da557aacSMax Reitz 1147da557aacSMax Reitz bs = bdrv_new(""); 1148da557aacSMax Reitz ret = bdrv_open(bs, filename, image_options, flags, NULL, errp); 1149da557aacSMax Reitz if (ret < 0) { 1150da557aacSMax Reitz bdrv_unref(bs); 1151da557aacSMax Reitz } else { 1152da557aacSMax Reitz *pbs = bs; 1153da557aacSMax Reitz } 1154da557aacSMax Reitz } else { 1155da557aacSMax Reitz ret = bdrv_file_open(pbs, filename, reference, image_options, flags, 1156da557aacSMax Reitz errp); 1157da557aacSMax Reitz } 1158da557aacSMax Reitz 1159da557aacSMax Reitz done: 1160da557aacSMax Reitz qdict_del(options, bdref_key); 1161da557aacSMax Reitz return ret; 1162da557aacSMax Reitz } 1163da557aacSMax Reitz 1164da557aacSMax Reitz /* 1165b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 1166de9c0cecSKevin Wolf * 1167de9c0cecSKevin Wolf * options is a QDict of options to pass to the block drivers, or NULL for an 1168de9c0cecSKevin Wolf * empty set of options. The reference to the QDict belongs to the block layer 1169de9c0cecSKevin Wolf * after the call (even on failure), so if the caller intends to reuse the 1170de9c0cecSKevin Wolf * dictionary, it needs to use QINCREF() before calling bdrv_open. 1171b6ce07aaSKevin Wolf */ 1172de9c0cecSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options, 117334b5d2c6SMax Reitz int flags, BlockDriver *drv, Error **errp) 1174ea2384d3Sbellard { 1175b6ce07aaSKevin Wolf int ret; 117689c9bc3dSStefan Weil /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */ 117789c9bc3dSStefan Weil char tmp_filename[PATH_MAX + 1]; 1178f500a6d3SKevin Wolf BlockDriverState *file = NULL; 117974fe54f2SKevin Wolf const char *drvname; 118034b5d2c6SMax Reitz Error *local_err = NULL; 118133e3963eSbellard 1182de9c0cecSKevin Wolf /* NULL means an empty set of options */ 1183de9c0cecSKevin Wolf if (options == NULL) { 1184de9c0cecSKevin Wolf options = qdict_new(); 1185de9c0cecSKevin Wolf } 1186de9c0cecSKevin Wolf 1187de9c0cecSKevin Wolf bs->options = options; 1188b6ad491aSKevin Wolf options = qdict_clone_shallow(options); 1189de9c0cecSKevin Wolf 1190de9c0cecSKevin Wolf /* For snapshot=on, create a temporary qcow2 overlay */ 119183f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 1192ea2384d3Sbellard BlockDriverState *bs1; 1193ea2384d3Sbellard int64_t total_size; 119491a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 119508b392e1SKevin Wolf QEMUOptionParameter *create_options; 11969fd3171aSKevin Wolf QDict *snapshot_options; 1197c2ad1b0cSKevin Wolf 1198ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 1199ea2384d3Sbellard instead of opening 'filename' directly */ 1200ea2384d3Sbellard 12019fd3171aSKevin Wolf /* Get the required size from the image */ 1202ea2384d3Sbellard bs1 = bdrv_new(""); 12039fd3171aSKevin Wolf QINCREF(options); 1204c9fbb99dSKevin Wolf ret = bdrv_open(bs1, filename, options, BDRV_O_NO_BACKING, 1205c9fbb99dSKevin Wolf drv, &local_err); 120651d7c00cSaliguori if (ret < 0) { 12074f6fd349SFam Zheng bdrv_unref(bs1); 1208de9c0cecSKevin Wolf goto fail; 1209ea2384d3Sbellard } 12103e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 12117c96d46eSaliguori 12124f6fd349SFam Zheng bdrv_unref(bs1); 1213ea2384d3Sbellard 12149fd3171aSKevin Wolf /* Create the temporary image */ 1215eba25057SJim Meyering ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 1216eba25057SJim Meyering if (ret < 0) { 121734b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not get temporary filename"); 1218de9c0cecSKevin Wolf goto fail; 1219eba25057SJim Meyering } 12207c96d46eSaliguori 122191a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 122208b392e1SKevin Wolf create_options = parse_option_parameters("", bdrv_qcow2->create_options, 122308b392e1SKevin Wolf NULL); 122491a073a9SKevin Wolf 122508b392e1SKevin Wolf set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size); 122691a073a9SKevin Wolf 1227cc84d90fSMax Reitz ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options, &local_err); 122808b392e1SKevin Wolf free_option_parameters(create_options); 122951d7c00cSaliguori if (ret < 0) { 123034b5d2c6SMax Reitz error_setg_errno(errp, -ret, "Could not create temporary overlay " 1231cc84d90fSMax Reitz "'%s': %s", tmp_filename, 1232cc84d90fSMax Reitz error_get_pretty(local_err)); 1233cc84d90fSMax Reitz error_free(local_err); 1234cc84d90fSMax Reitz local_err = NULL; 1235de9c0cecSKevin Wolf goto fail; 1236ea2384d3Sbellard } 123791a073a9SKevin Wolf 12389fd3171aSKevin Wolf /* Prepare a new options QDict for the temporary file, where user 12399fd3171aSKevin Wolf * options refer to the backing file */ 12409fd3171aSKevin Wolf if (filename) { 12419fd3171aSKevin Wolf qdict_put(options, "file.filename", qstring_from_str(filename)); 12429fd3171aSKevin Wolf } 12439fd3171aSKevin Wolf if (drv) { 12449fd3171aSKevin Wolf qdict_put(options, "driver", qstring_from_str(drv->format_name)); 12459fd3171aSKevin Wolf } 12469fd3171aSKevin Wolf 12479fd3171aSKevin Wolf snapshot_options = qdict_new(); 12489fd3171aSKevin Wolf qdict_put(snapshot_options, "backing", options); 12499fd3171aSKevin Wolf qdict_flatten(snapshot_options); 12509fd3171aSKevin Wolf 12519fd3171aSKevin Wolf bs->options = snapshot_options; 12529fd3171aSKevin Wolf options = qdict_clone_shallow(bs->options); 12539fd3171aSKevin Wolf 1254ea2384d3Sbellard filename = tmp_filename; 125591a073a9SKevin Wolf drv = bdrv_qcow2; 1256ea2384d3Sbellard bs->is_temporary = 1; 1257ea2384d3Sbellard } 1258ea2384d3Sbellard 1259f500a6d3SKevin Wolf /* Open image file without format layer */ 1260be028adcSJeff Cody if (flags & BDRV_O_RDWR) { 1261be028adcSJeff Cody flags |= BDRV_O_ALLOW_RDWR; 1262be028adcSJeff Cody } 1263be028adcSJeff Cody 1264054963f8SMax Reitz ret = bdrv_open_image(&file, filename, options, "file", 1265054963f8SMax Reitz bdrv_open_flags(bs, flags | BDRV_O_UNMAP), true, true, 12662a05cbe4SMax Reitz &local_err); 1267f500a6d3SKevin Wolf if (ret < 0) { 1268de9c0cecSKevin Wolf goto fail; 1269f500a6d3SKevin Wolf } 1270f500a6d3SKevin Wolf 1271f500a6d3SKevin Wolf /* Find the right image format driver */ 127274fe54f2SKevin Wolf drvname = qdict_get_try_str(options, "driver"); 127374fe54f2SKevin Wolf if (drvname) { 12748f94a6e4SKevin Wolf drv = bdrv_find_format(drvname); 127574fe54f2SKevin Wolf qdict_del(options, "driver"); 127606d22aa3SKevin Wolf if (!drv) { 127706d22aa3SKevin Wolf error_setg(errp, "Invalid driver: '%s'", drvname); 127806d22aa3SKevin Wolf ret = -EINVAL; 127906d22aa3SKevin Wolf goto unlink_and_fail; 128006d22aa3SKevin Wolf } 128174fe54f2SKevin Wolf } 128274fe54f2SKevin Wolf 1283f500a6d3SKevin Wolf if (!drv) { 12842a05cbe4SMax Reitz if (file) { 128534b5d2c6SMax Reitz ret = find_image_format(file, filename, &drv, &local_err); 12862a05cbe4SMax Reitz } else { 12872a05cbe4SMax Reitz error_setg(errp, "Must specify either driver or file"); 12882a05cbe4SMax Reitz ret = -EINVAL; 12892a05cbe4SMax Reitz goto unlink_and_fail; 12902a05cbe4SMax Reitz } 1291f500a6d3SKevin Wolf } 1292f500a6d3SKevin Wolf 1293f500a6d3SKevin Wolf if (!drv) { 1294f500a6d3SKevin Wolf goto unlink_and_fail; 1295f500a6d3SKevin Wolf } 1296f500a6d3SKevin Wolf 1297b6ce07aaSKevin Wolf /* Open the image */ 129834b5d2c6SMax Reitz ret = bdrv_open_common(bs, file, options, flags, drv, &local_err); 1299b6ce07aaSKevin Wolf if (ret < 0) { 13006987307cSChristoph Hellwig goto unlink_and_fail; 13016987307cSChristoph Hellwig } 13026987307cSChristoph Hellwig 13032a05cbe4SMax Reitz if (file && (bs->file != file)) { 13044f6fd349SFam Zheng bdrv_unref(file); 1305f500a6d3SKevin Wolf file = NULL; 1306f500a6d3SKevin Wolf } 1307f500a6d3SKevin Wolf 1308b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 13099156df12SPaolo Bonzini if ((flags & BDRV_O_NO_BACKING) == 0) { 131031ca6d07SKevin Wolf QDict *backing_options; 131131ca6d07SKevin Wolf 13125726d872SBenoît Canet qdict_extract_subqdict(options, &backing_options, "backing."); 131334b5d2c6SMax Reitz ret = bdrv_open_backing_file(bs, backing_options, &local_err); 1314b6ce07aaSKevin Wolf if (ret < 0) { 1315b6ad491aSKevin Wolf goto close_and_fail; 1316b6ce07aaSKevin Wolf } 1317b6ce07aaSKevin Wolf } 1318b6ce07aaSKevin Wolf 1319b6ad491aSKevin Wolf /* Check if any unknown options were used */ 1320b6ad491aSKevin Wolf if (qdict_size(options) != 0) { 1321b6ad491aSKevin Wolf const QDictEntry *entry = qdict_first(options); 132234b5d2c6SMax Reitz error_setg(errp, "Block format '%s' used by device '%s' doesn't " 132334b5d2c6SMax Reitz "support the option '%s'", drv->format_name, bs->device_name, 132434b5d2c6SMax Reitz entry->key); 1325b6ad491aSKevin Wolf 1326b6ad491aSKevin Wolf ret = -EINVAL; 1327b6ad491aSKevin Wolf goto close_and_fail; 1328b6ad491aSKevin Wolf } 1329b6ad491aSKevin Wolf QDECREF(options); 1330b6ad491aSKevin Wolf 1331b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 13327d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 1333b6ce07aaSKevin Wolf } 1334b6ce07aaSKevin Wolf 1335b6ce07aaSKevin Wolf return 0; 1336b6ce07aaSKevin Wolf 1337b6ce07aaSKevin Wolf unlink_and_fail: 1338f500a6d3SKevin Wolf if (file != NULL) { 13394f6fd349SFam Zheng bdrv_unref(file); 1340f500a6d3SKevin Wolf } 1341b6ce07aaSKevin Wolf if (bs->is_temporary) { 1342b6ce07aaSKevin Wolf unlink(filename); 1343b6ce07aaSKevin Wolf } 1344de9c0cecSKevin Wolf fail: 1345de9c0cecSKevin Wolf QDECREF(bs->options); 1346b6ad491aSKevin Wolf QDECREF(options); 1347de9c0cecSKevin Wolf bs->options = NULL; 134834b5d2c6SMax Reitz if (error_is_set(&local_err)) { 134934b5d2c6SMax Reitz error_propagate(errp, local_err); 135034b5d2c6SMax Reitz } 1351b6ad491aSKevin Wolf return ret; 1352de9c0cecSKevin Wolf 1353b6ad491aSKevin Wolf close_and_fail: 1354b6ad491aSKevin Wolf bdrv_close(bs); 1355b6ad491aSKevin Wolf QDECREF(options); 135634b5d2c6SMax Reitz if (error_is_set(&local_err)) { 135734b5d2c6SMax Reitz error_propagate(errp, local_err); 135834b5d2c6SMax Reitz } 1359b6ce07aaSKevin Wolf return ret; 1360b6ce07aaSKevin Wolf } 1361b6ce07aaSKevin Wolf 1362e971aa12SJeff Cody typedef struct BlockReopenQueueEntry { 1363e971aa12SJeff Cody bool prepared; 1364e971aa12SJeff Cody BDRVReopenState state; 1365e971aa12SJeff Cody QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry; 1366e971aa12SJeff Cody } BlockReopenQueueEntry; 1367e971aa12SJeff Cody 1368e971aa12SJeff Cody /* 1369e971aa12SJeff Cody * Adds a BlockDriverState to a simple queue for an atomic, transactional 1370e971aa12SJeff Cody * reopen of multiple devices. 1371e971aa12SJeff Cody * 1372e971aa12SJeff Cody * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT 1373e971aa12SJeff Cody * already performed, or alternatively may be NULL a new BlockReopenQueue will 1374e971aa12SJeff Cody * be created and initialized. This newly created BlockReopenQueue should be 1375e971aa12SJeff Cody * passed back in for subsequent calls that are intended to be of the same 1376e971aa12SJeff Cody * atomic 'set'. 1377e971aa12SJeff Cody * 1378e971aa12SJeff Cody * bs is the BlockDriverState to add to the reopen queue. 1379e971aa12SJeff Cody * 1380e971aa12SJeff Cody * flags contains the open flags for the associated bs 1381e971aa12SJeff Cody * 1382e971aa12SJeff Cody * returns a pointer to bs_queue, which is either the newly allocated 1383e971aa12SJeff Cody * bs_queue, or the existing bs_queue being used. 1384e971aa12SJeff Cody * 1385e971aa12SJeff Cody */ 1386e971aa12SJeff Cody BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue, 1387e971aa12SJeff Cody BlockDriverState *bs, int flags) 1388e971aa12SJeff Cody { 1389e971aa12SJeff Cody assert(bs != NULL); 1390e971aa12SJeff Cody 1391e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry; 1392e971aa12SJeff Cody if (bs_queue == NULL) { 1393e971aa12SJeff Cody bs_queue = g_new0(BlockReopenQueue, 1); 1394e971aa12SJeff Cody QSIMPLEQ_INIT(bs_queue); 1395e971aa12SJeff Cody } 1396e971aa12SJeff Cody 1397e971aa12SJeff Cody if (bs->file) { 1398e971aa12SJeff Cody bdrv_reopen_queue(bs_queue, bs->file, flags); 1399e971aa12SJeff Cody } 1400e971aa12SJeff Cody 1401e971aa12SJeff Cody bs_entry = g_new0(BlockReopenQueueEntry, 1); 1402e971aa12SJeff Cody QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry); 1403e971aa12SJeff Cody 1404e971aa12SJeff Cody bs_entry->state.bs = bs; 1405e971aa12SJeff Cody bs_entry->state.flags = flags; 1406e971aa12SJeff Cody 1407e971aa12SJeff Cody return bs_queue; 1408e971aa12SJeff Cody } 1409e971aa12SJeff Cody 1410e971aa12SJeff Cody /* 1411e971aa12SJeff Cody * Reopen multiple BlockDriverStates atomically & transactionally. 1412e971aa12SJeff Cody * 1413e971aa12SJeff Cody * The queue passed in (bs_queue) must have been built up previous 1414e971aa12SJeff Cody * via bdrv_reopen_queue(). 1415e971aa12SJeff Cody * 1416e971aa12SJeff Cody * Reopens all BDS specified in the queue, with the appropriate 1417e971aa12SJeff Cody * flags. All devices are prepared for reopen, and failure of any 1418e971aa12SJeff Cody * device will cause all device changes to be abandonded, and intermediate 1419e971aa12SJeff Cody * data cleaned up. 1420e971aa12SJeff Cody * 1421e971aa12SJeff Cody * If all devices prepare successfully, then the changes are committed 1422e971aa12SJeff Cody * to all devices. 1423e971aa12SJeff Cody * 1424e971aa12SJeff Cody */ 1425e971aa12SJeff Cody int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp) 1426e971aa12SJeff Cody { 1427e971aa12SJeff Cody int ret = -1; 1428e971aa12SJeff Cody BlockReopenQueueEntry *bs_entry, *next; 1429e971aa12SJeff Cody Error *local_err = NULL; 1430e971aa12SJeff Cody 1431e971aa12SJeff Cody assert(bs_queue != NULL); 1432e971aa12SJeff Cody 1433e971aa12SJeff Cody bdrv_drain_all(); 1434e971aa12SJeff Cody 1435e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1436e971aa12SJeff Cody if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) { 1437e971aa12SJeff Cody error_propagate(errp, local_err); 1438e971aa12SJeff Cody goto cleanup; 1439e971aa12SJeff Cody } 1440e971aa12SJeff Cody bs_entry->prepared = true; 1441e971aa12SJeff Cody } 1442e971aa12SJeff Cody 1443e971aa12SJeff Cody /* If we reach this point, we have success and just need to apply the 1444e971aa12SJeff Cody * changes 1445e971aa12SJeff Cody */ 1446e971aa12SJeff Cody QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) { 1447e971aa12SJeff Cody bdrv_reopen_commit(&bs_entry->state); 1448e971aa12SJeff Cody } 1449e971aa12SJeff Cody 1450e971aa12SJeff Cody ret = 0; 1451e971aa12SJeff Cody 1452e971aa12SJeff Cody cleanup: 1453e971aa12SJeff Cody QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) { 1454e971aa12SJeff Cody if (ret && bs_entry->prepared) { 1455e971aa12SJeff Cody bdrv_reopen_abort(&bs_entry->state); 1456e971aa12SJeff Cody } 1457e971aa12SJeff Cody g_free(bs_entry); 1458e971aa12SJeff Cody } 1459e971aa12SJeff Cody g_free(bs_queue); 1460e971aa12SJeff Cody return ret; 1461e971aa12SJeff Cody } 1462e971aa12SJeff Cody 1463e971aa12SJeff Cody 1464e971aa12SJeff Cody /* Reopen a single BlockDriverState with the specified flags. */ 1465e971aa12SJeff Cody int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp) 1466e971aa12SJeff Cody { 1467e971aa12SJeff Cody int ret = -1; 1468e971aa12SJeff Cody Error *local_err = NULL; 1469e971aa12SJeff Cody BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags); 1470e971aa12SJeff Cody 1471e971aa12SJeff Cody ret = bdrv_reopen_multiple(queue, &local_err); 1472e971aa12SJeff Cody if (local_err != NULL) { 1473e971aa12SJeff Cody error_propagate(errp, local_err); 1474e971aa12SJeff Cody } 1475e971aa12SJeff Cody return ret; 1476e971aa12SJeff Cody } 1477e971aa12SJeff Cody 1478e971aa12SJeff Cody 1479e971aa12SJeff Cody /* 1480e971aa12SJeff Cody * Prepares a BlockDriverState for reopen. All changes are staged in the 1481e971aa12SJeff Cody * 'opaque' field of the BDRVReopenState, which is used and allocated by 1482e971aa12SJeff Cody * the block driver layer .bdrv_reopen_prepare() 1483e971aa12SJeff Cody * 1484e971aa12SJeff Cody * bs is the BlockDriverState to reopen 1485e971aa12SJeff Cody * flags are the new open flags 1486e971aa12SJeff Cody * queue is the reopen queue 1487e971aa12SJeff Cody * 1488e971aa12SJeff Cody * Returns 0 on success, non-zero on error. On error errp will be set 1489e971aa12SJeff Cody * as well. 1490e971aa12SJeff Cody * 1491e971aa12SJeff Cody * On failure, bdrv_reopen_abort() will be called to clean up any data. 1492e971aa12SJeff Cody * It is the responsibility of the caller to then call the abort() or 1493e971aa12SJeff Cody * commit() for any other BDS that have been left in a prepare() state 1494e971aa12SJeff Cody * 1495e971aa12SJeff Cody */ 1496e971aa12SJeff Cody int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue, 1497e971aa12SJeff Cody Error **errp) 1498e971aa12SJeff Cody { 1499e971aa12SJeff Cody int ret = -1; 1500e971aa12SJeff Cody Error *local_err = NULL; 1501e971aa12SJeff Cody BlockDriver *drv; 1502e971aa12SJeff Cody 1503e971aa12SJeff Cody assert(reopen_state != NULL); 1504e971aa12SJeff Cody assert(reopen_state->bs->drv != NULL); 1505e971aa12SJeff Cody drv = reopen_state->bs->drv; 1506e971aa12SJeff Cody 1507e971aa12SJeff Cody /* if we are to stay read-only, do not allow permission change 1508e971aa12SJeff Cody * to r/w */ 1509e971aa12SJeff Cody if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) && 1510e971aa12SJeff Cody reopen_state->flags & BDRV_O_RDWR) { 1511e971aa12SJeff Cody error_set(errp, QERR_DEVICE_IS_READ_ONLY, 1512e971aa12SJeff Cody reopen_state->bs->device_name); 1513e971aa12SJeff Cody goto error; 1514e971aa12SJeff Cody } 1515e971aa12SJeff Cody 1516e971aa12SJeff Cody 1517e971aa12SJeff Cody ret = bdrv_flush(reopen_state->bs); 1518e971aa12SJeff Cody if (ret) { 1519e971aa12SJeff Cody error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive", 1520e971aa12SJeff Cody strerror(-ret)); 1521e971aa12SJeff Cody goto error; 1522e971aa12SJeff Cody } 1523e971aa12SJeff Cody 1524e971aa12SJeff Cody if (drv->bdrv_reopen_prepare) { 1525e971aa12SJeff Cody ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err); 1526e971aa12SJeff Cody if (ret) { 1527e971aa12SJeff Cody if (local_err != NULL) { 1528e971aa12SJeff Cody error_propagate(errp, local_err); 1529e971aa12SJeff Cody } else { 1530d8b6895fSLuiz Capitulino error_setg(errp, "failed while preparing to reopen image '%s'", 1531e971aa12SJeff Cody reopen_state->bs->filename); 1532e971aa12SJeff Cody } 1533e971aa12SJeff Cody goto error; 1534e971aa12SJeff Cody } 1535e971aa12SJeff Cody } else { 1536e971aa12SJeff Cody /* It is currently mandatory to have a bdrv_reopen_prepare() 1537e971aa12SJeff Cody * handler for each supported drv. */ 1538e971aa12SJeff Cody error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED, 1539e971aa12SJeff Cody drv->format_name, reopen_state->bs->device_name, 1540e971aa12SJeff Cody "reopening of file"); 1541e971aa12SJeff Cody ret = -1; 1542e971aa12SJeff Cody goto error; 1543e971aa12SJeff Cody } 1544e971aa12SJeff Cody 1545e971aa12SJeff Cody ret = 0; 1546e971aa12SJeff Cody 1547e971aa12SJeff Cody error: 1548e971aa12SJeff Cody return ret; 1549e971aa12SJeff Cody } 1550e971aa12SJeff Cody 1551e971aa12SJeff Cody /* 1552e971aa12SJeff Cody * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and 1553e971aa12SJeff Cody * makes them final by swapping the staging BlockDriverState contents into 1554e971aa12SJeff Cody * the active BlockDriverState contents. 1555e971aa12SJeff Cody */ 1556e971aa12SJeff Cody void bdrv_reopen_commit(BDRVReopenState *reopen_state) 1557e971aa12SJeff Cody { 1558e971aa12SJeff Cody BlockDriver *drv; 1559e971aa12SJeff Cody 1560e971aa12SJeff Cody assert(reopen_state != NULL); 1561e971aa12SJeff Cody drv = reopen_state->bs->drv; 1562e971aa12SJeff Cody assert(drv != NULL); 1563e971aa12SJeff Cody 1564e971aa12SJeff Cody /* If there are any driver level actions to take */ 1565e971aa12SJeff Cody if (drv->bdrv_reopen_commit) { 1566e971aa12SJeff Cody drv->bdrv_reopen_commit(reopen_state); 1567e971aa12SJeff Cody } 1568e971aa12SJeff Cody 1569e971aa12SJeff Cody /* set BDS specific flags now */ 1570e971aa12SJeff Cody reopen_state->bs->open_flags = reopen_state->flags; 1571e971aa12SJeff Cody reopen_state->bs->enable_write_cache = !!(reopen_state->flags & 1572e971aa12SJeff Cody BDRV_O_CACHE_WB); 1573e971aa12SJeff Cody reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR); 1574e971aa12SJeff Cody } 1575e971aa12SJeff Cody 1576e971aa12SJeff Cody /* 1577e971aa12SJeff Cody * Abort the reopen, and delete and free the staged changes in 1578e971aa12SJeff Cody * reopen_state 1579e971aa12SJeff Cody */ 1580e971aa12SJeff Cody void bdrv_reopen_abort(BDRVReopenState *reopen_state) 1581e971aa12SJeff Cody { 1582e971aa12SJeff Cody BlockDriver *drv; 1583e971aa12SJeff Cody 1584e971aa12SJeff Cody assert(reopen_state != NULL); 1585e971aa12SJeff Cody drv = reopen_state->bs->drv; 1586e971aa12SJeff Cody assert(drv != NULL); 1587e971aa12SJeff Cody 1588e971aa12SJeff Cody if (drv->bdrv_reopen_abort) { 1589e971aa12SJeff Cody drv->bdrv_reopen_abort(reopen_state); 1590e971aa12SJeff Cody } 1591e971aa12SJeff Cody } 1592e971aa12SJeff Cody 1593e971aa12SJeff Cody 1594fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 1595fc01f7e7Sbellard { 15963e914655SPaolo Bonzini if (bs->job) { 15973e914655SPaolo Bonzini block_job_cancel_sync(bs->job); 15983e914655SPaolo Bonzini } 159958fda173SStefan Hajnoczi bdrv_drain_all(); /* complete I/O */ 160058fda173SStefan Hajnoczi bdrv_flush(bs); 160158fda173SStefan Hajnoczi bdrv_drain_all(); /* in case flush left pending I/O */ 1602d7d512f6SPaolo Bonzini notifier_list_notify(&bs->close_notifiers, bs); 16037094f12fSKevin Wolf 16043cbc002cSPaolo Bonzini if (bs->drv) { 1605557df6acSStefan Hajnoczi if (bs->backing_hd) { 16064f6fd349SFam Zheng bdrv_unref(bs->backing_hd); 1607557df6acSStefan Hajnoczi bs->backing_hd = NULL; 1608557df6acSStefan Hajnoczi } 1609ea2384d3Sbellard bs->drv->bdrv_close(bs); 16107267c094SAnthony Liguori g_free(bs->opaque); 1611ea2384d3Sbellard #ifdef _WIN32 1612ea2384d3Sbellard if (bs->is_temporary) { 1613ea2384d3Sbellard unlink(bs->filename); 1614ea2384d3Sbellard } 161567b915a5Sbellard #endif 1616ea2384d3Sbellard bs->opaque = NULL; 1617ea2384d3Sbellard bs->drv = NULL; 161853fec9d3SStefan Hajnoczi bs->copy_on_read = 0; 1619a275fa42SPaolo Bonzini bs->backing_file[0] = '\0'; 1620a275fa42SPaolo Bonzini bs->backing_format[0] = '\0'; 16216405875cSPaolo Bonzini bs->total_sectors = 0; 16226405875cSPaolo Bonzini bs->encrypted = 0; 16236405875cSPaolo Bonzini bs->valid_key = 0; 16246405875cSPaolo Bonzini bs->sg = 0; 16256405875cSPaolo Bonzini bs->growable = 0; 16260d51b4deSAsias He bs->zero_beyond_eof = false; 1627de9c0cecSKevin Wolf QDECREF(bs->options); 1628de9c0cecSKevin Wolf bs->options = NULL; 1629b338082bSbellard 163066f82ceeSKevin Wolf if (bs->file != NULL) { 16314f6fd349SFam Zheng bdrv_unref(bs->file); 16320ac9377dSPaolo Bonzini bs->file = NULL; 163366f82ceeSKevin Wolf } 16349ca11154SPavel Hrdina } 163566f82ceeSKevin Wolf 16367d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, false); 163798f90dbaSZhi Yong Wu 163898f90dbaSZhi Yong Wu /*throttling disk I/O limits*/ 163998f90dbaSZhi Yong Wu if (bs->io_limits_enabled) { 164098f90dbaSZhi Yong Wu bdrv_io_limits_disable(bs); 164198f90dbaSZhi Yong Wu } 1642b338082bSbellard } 1643b338082bSbellard 16442bc93fedSMORITA Kazutaka void bdrv_close_all(void) 16452bc93fedSMORITA Kazutaka { 16462bc93fedSMORITA Kazutaka BlockDriverState *bs; 16472bc93fedSMORITA Kazutaka 1648dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 16492bc93fedSMORITA Kazutaka bdrv_close(bs); 16502bc93fedSMORITA Kazutaka } 16512bc93fedSMORITA Kazutaka } 16522bc93fedSMORITA Kazutaka 165388266f5aSStefan Hajnoczi /* Check if any requests are in-flight (including throttled requests) */ 165488266f5aSStefan Hajnoczi static bool bdrv_requests_pending(BlockDriverState *bs) 165588266f5aSStefan Hajnoczi { 165688266f5aSStefan Hajnoczi if (!QLIST_EMPTY(&bs->tracked_requests)) { 165788266f5aSStefan Hajnoczi return true; 165888266f5aSStefan Hajnoczi } 1659cc0681c4SBenoît Canet if (!qemu_co_queue_empty(&bs->throttled_reqs[0])) { 1660cc0681c4SBenoît Canet return true; 1661cc0681c4SBenoît Canet } 1662cc0681c4SBenoît Canet if (!qemu_co_queue_empty(&bs->throttled_reqs[1])) { 166388266f5aSStefan Hajnoczi return true; 166488266f5aSStefan Hajnoczi } 166588266f5aSStefan Hajnoczi if (bs->file && bdrv_requests_pending(bs->file)) { 166688266f5aSStefan Hajnoczi return true; 166788266f5aSStefan Hajnoczi } 166888266f5aSStefan Hajnoczi if (bs->backing_hd && bdrv_requests_pending(bs->backing_hd)) { 166988266f5aSStefan Hajnoczi return true; 167088266f5aSStefan Hajnoczi } 167188266f5aSStefan Hajnoczi return false; 167288266f5aSStefan Hajnoczi } 167388266f5aSStefan Hajnoczi 167488266f5aSStefan Hajnoczi static bool bdrv_requests_pending_all(void) 167588266f5aSStefan Hajnoczi { 167688266f5aSStefan Hajnoczi BlockDriverState *bs; 1677dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 167888266f5aSStefan Hajnoczi if (bdrv_requests_pending(bs)) { 167988266f5aSStefan Hajnoczi return true; 168088266f5aSStefan Hajnoczi } 168188266f5aSStefan Hajnoczi } 168288266f5aSStefan Hajnoczi return false; 168388266f5aSStefan Hajnoczi } 168488266f5aSStefan Hajnoczi 1685922453bcSStefan Hajnoczi /* 1686922453bcSStefan Hajnoczi * Wait for pending requests to complete across all BlockDriverStates 1687922453bcSStefan Hajnoczi * 1688922453bcSStefan Hajnoczi * This function does not flush data to disk, use bdrv_flush_all() for that 1689922453bcSStefan Hajnoczi * after calling this function. 16904c355d53SZhi Yong Wu * 16914c355d53SZhi Yong Wu * Note that completion of an asynchronous I/O operation can trigger any 16924c355d53SZhi Yong Wu * number of other I/O operations on other devices---for example a coroutine 16934c355d53SZhi Yong Wu * can be arbitrarily complex and a constant flow of I/O can come until the 16944c355d53SZhi Yong Wu * coroutine is complete. Because of this, it is not possible to have a 16954c355d53SZhi Yong Wu * function to drain a single device's I/O queue. 1696922453bcSStefan Hajnoczi */ 1697922453bcSStefan Hajnoczi void bdrv_drain_all(void) 1698922453bcSStefan Hajnoczi { 169988266f5aSStefan Hajnoczi /* Always run first iteration so any pending completion BHs run */ 170088266f5aSStefan Hajnoczi bool busy = true; 1701922453bcSStefan Hajnoczi BlockDriverState *bs; 1702922453bcSStefan Hajnoczi 170388266f5aSStefan Hajnoczi while (busy) { 1704dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 17050b06ef3bSStefan Hajnoczi bdrv_start_throttled_reqs(bs); 17064c355d53SZhi Yong Wu } 1707922453bcSStefan Hajnoczi 170888266f5aSStefan Hajnoczi busy = bdrv_requests_pending_all(); 170988266f5aSStefan Hajnoczi busy |= aio_poll(qemu_get_aio_context(), busy); 1710922453bcSStefan Hajnoczi } 1711922453bcSStefan Hajnoczi } 1712922453bcSStefan Hajnoczi 1713dc364f4cSBenoît Canet /* make a BlockDriverState anonymous by removing from bdrv_state and 1714dc364f4cSBenoît Canet * graph_bdrv_state list. 1715d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 1716d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 1717d22b2f41SRyan Harper { 1718d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 1719dc364f4cSBenoît Canet QTAILQ_REMOVE(&bdrv_states, bs, device_list); 1720d22b2f41SRyan Harper } 1721d22b2f41SRyan Harper bs->device_name[0] = '\0'; 1722dc364f4cSBenoît Canet if (bs->node_name[0] != '\0') { 1723dc364f4cSBenoît Canet QTAILQ_REMOVE(&graph_bdrv_states, bs, node_list); 1724dc364f4cSBenoît Canet } 1725dc364f4cSBenoît Canet bs->node_name[0] = '\0'; 1726d22b2f41SRyan Harper } 1727d22b2f41SRyan Harper 1728e023b2e2SPaolo Bonzini static void bdrv_rebind(BlockDriverState *bs) 1729e023b2e2SPaolo Bonzini { 1730e023b2e2SPaolo Bonzini if (bs->drv && bs->drv->bdrv_rebind) { 1731e023b2e2SPaolo Bonzini bs->drv->bdrv_rebind(bs); 1732e023b2e2SPaolo Bonzini } 1733e023b2e2SPaolo Bonzini } 1734e023b2e2SPaolo Bonzini 17354ddc07caSPaolo Bonzini static void bdrv_move_feature_fields(BlockDriverState *bs_dest, 17364ddc07caSPaolo Bonzini BlockDriverState *bs_src) 17374ddc07caSPaolo Bonzini { 17384ddc07caSPaolo Bonzini /* move some fields that need to stay attached to the device */ 17394ddc07caSPaolo Bonzini bs_dest->open_flags = bs_src->open_flags; 17404ddc07caSPaolo Bonzini 17414ddc07caSPaolo Bonzini /* dev info */ 17424ddc07caSPaolo Bonzini bs_dest->dev_ops = bs_src->dev_ops; 17434ddc07caSPaolo Bonzini bs_dest->dev_opaque = bs_src->dev_opaque; 17444ddc07caSPaolo Bonzini bs_dest->dev = bs_src->dev; 17454ddc07caSPaolo Bonzini bs_dest->buffer_alignment = bs_src->buffer_alignment; 17464ddc07caSPaolo Bonzini bs_dest->copy_on_read = bs_src->copy_on_read; 17474ddc07caSPaolo Bonzini 17484ddc07caSPaolo Bonzini bs_dest->enable_write_cache = bs_src->enable_write_cache; 17494ddc07caSPaolo Bonzini 1750cc0681c4SBenoît Canet /* i/o throttled req */ 1751cc0681c4SBenoît Canet memcpy(&bs_dest->throttle_state, 1752cc0681c4SBenoît Canet &bs_src->throttle_state, 1753cc0681c4SBenoît Canet sizeof(ThrottleState)); 1754cc0681c4SBenoît Canet bs_dest->throttled_reqs[0] = bs_src->throttled_reqs[0]; 1755cc0681c4SBenoît Canet bs_dest->throttled_reqs[1] = bs_src->throttled_reqs[1]; 17564ddc07caSPaolo Bonzini bs_dest->io_limits_enabled = bs_src->io_limits_enabled; 17574ddc07caSPaolo Bonzini 17584ddc07caSPaolo Bonzini /* r/w error */ 17594ddc07caSPaolo Bonzini bs_dest->on_read_error = bs_src->on_read_error; 17604ddc07caSPaolo Bonzini bs_dest->on_write_error = bs_src->on_write_error; 17614ddc07caSPaolo Bonzini 17624ddc07caSPaolo Bonzini /* i/o status */ 17634ddc07caSPaolo Bonzini bs_dest->iostatus_enabled = bs_src->iostatus_enabled; 17644ddc07caSPaolo Bonzini bs_dest->iostatus = bs_src->iostatus; 17654ddc07caSPaolo Bonzini 17664ddc07caSPaolo Bonzini /* dirty bitmap */ 1767e4654d2dSFam Zheng bs_dest->dirty_bitmaps = bs_src->dirty_bitmaps; 17684ddc07caSPaolo Bonzini 17699fcb0251SFam Zheng /* reference count */ 17709fcb0251SFam Zheng bs_dest->refcnt = bs_src->refcnt; 17719fcb0251SFam Zheng 17724ddc07caSPaolo Bonzini /* job */ 17734ddc07caSPaolo Bonzini bs_dest->in_use = bs_src->in_use; 17744ddc07caSPaolo Bonzini bs_dest->job = bs_src->job; 17754ddc07caSPaolo Bonzini 17764ddc07caSPaolo Bonzini /* keep the same entry in bdrv_states */ 17774ddc07caSPaolo Bonzini pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name), 17784ddc07caSPaolo Bonzini bs_src->device_name); 1779dc364f4cSBenoît Canet bs_dest->device_list = bs_src->device_list; 1780dc364f4cSBenoît Canet 1781dc364f4cSBenoît Canet /* keep the same entry in graph_bdrv_states 1782dc364f4cSBenoît Canet * We do want to swap name but don't want to swap linked list entries 1783dc364f4cSBenoît Canet */ 1784dc364f4cSBenoît Canet bs_dest->node_list = bs_src->node_list; 17854ddc07caSPaolo Bonzini } 17864ddc07caSPaolo Bonzini 17874ddc07caSPaolo Bonzini /* 17884ddc07caSPaolo Bonzini * Swap bs contents for two image chains while they are live, 17894ddc07caSPaolo Bonzini * while keeping required fields on the BlockDriverState that is 17904ddc07caSPaolo Bonzini * actually attached to a device. 17914ddc07caSPaolo Bonzini * 17924ddc07caSPaolo Bonzini * This will modify the BlockDriverState fields, and swap contents 17934ddc07caSPaolo Bonzini * between bs_new and bs_old. Both bs_new and bs_old are modified. 17944ddc07caSPaolo Bonzini * 17954ddc07caSPaolo Bonzini * bs_new is required to be anonymous. 17964ddc07caSPaolo Bonzini * 17974ddc07caSPaolo Bonzini * This function does not create any image files. 17984ddc07caSPaolo Bonzini */ 17994ddc07caSPaolo Bonzini void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old) 18004ddc07caSPaolo Bonzini { 18014ddc07caSPaolo Bonzini BlockDriverState tmp; 18024ddc07caSPaolo Bonzini 18034ddc07caSPaolo Bonzini /* bs_new must be anonymous and shouldn't have anything fancy enabled */ 18044ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 1805e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs_new->dirty_bitmaps)); 18064ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 18074ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 18084ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 18094ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1810cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 18114ddc07caSPaolo Bonzini 18124ddc07caSPaolo Bonzini tmp = *bs_new; 18134ddc07caSPaolo Bonzini *bs_new = *bs_old; 18144ddc07caSPaolo Bonzini *bs_old = tmp; 18154ddc07caSPaolo Bonzini 18164ddc07caSPaolo Bonzini /* there are some fields that should not be swapped, move them back */ 18174ddc07caSPaolo Bonzini bdrv_move_feature_fields(&tmp, bs_old); 18184ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_old, bs_new); 18194ddc07caSPaolo Bonzini bdrv_move_feature_fields(bs_new, &tmp); 18204ddc07caSPaolo Bonzini 18214ddc07caSPaolo Bonzini /* bs_new shouldn't be in bdrv_states even after the swap! */ 18224ddc07caSPaolo Bonzini assert(bs_new->device_name[0] == '\0'); 18234ddc07caSPaolo Bonzini 18244ddc07caSPaolo Bonzini /* Check a few fields that should remain attached to the device */ 18254ddc07caSPaolo Bonzini assert(bs_new->dev == NULL); 18264ddc07caSPaolo Bonzini assert(bs_new->job == NULL); 18274ddc07caSPaolo Bonzini assert(bs_new->in_use == 0); 18284ddc07caSPaolo Bonzini assert(bs_new->io_limits_enabled == false); 1829cc0681c4SBenoît Canet assert(!throttle_have_timer(&bs_new->throttle_state)); 18304ddc07caSPaolo Bonzini 18314ddc07caSPaolo Bonzini bdrv_rebind(bs_new); 18324ddc07caSPaolo Bonzini bdrv_rebind(bs_old); 18334ddc07caSPaolo Bonzini } 18344ddc07caSPaolo Bonzini 18358802d1fdSJeff Cody /* 18368802d1fdSJeff Cody * Add new bs contents at the top of an image chain while the chain is 18378802d1fdSJeff Cody * live, while keeping required fields on the top layer. 18388802d1fdSJeff Cody * 18398802d1fdSJeff Cody * This will modify the BlockDriverState fields, and swap contents 18408802d1fdSJeff Cody * between bs_new and bs_top. Both bs_new and bs_top are modified. 18418802d1fdSJeff Cody * 1842f6801b83SJeff Cody * bs_new is required to be anonymous. 1843f6801b83SJeff Cody * 18448802d1fdSJeff Cody * This function does not create any image files. 18458802d1fdSJeff Cody */ 18468802d1fdSJeff Cody void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top) 18478802d1fdSJeff Cody { 18484ddc07caSPaolo Bonzini bdrv_swap(bs_new, bs_top); 18498802d1fdSJeff Cody 18508802d1fdSJeff Cody /* The contents of 'tmp' will become bs_top, as we are 18518802d1fdSJeff Cody * swapping bs_new and bs_top contents. */ 18524ddc07caSPaolo Bonzini bs_top->backing_hd = bs_new; 18534ddc07caSPaolo Bonzini bs_top->open_flags &= ~BDRV_O_NO_BACKING; 18544ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file), 18554ddc07caSPaolo Bonzini bs_new->filename); 18564ddc07caSPaolo Bonzini pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format), 18574ddc07caSPaolo Bonzini bs_new->drv ? bs_new->drv->format_name : ""); 18588802d1fdSJeff Cody } 18598802d1fdSJeff Cody 18604f6fd349SFam Zheng static void bdrv_delete(BlockDriverState *bs) 1861b338082bSbellard { 1862fa879d62SMarkus Armbruster assert(!bs->dev); 18633e914655SPaolo Bonzini assert(!bs->job); 18643e914655SPaolo Bonzini assert(!bs->in_use); 18654f6fd349SFam Zheng assert(!bs->refcnt); 1866e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 186718846deeSMarkus Armbruster 1868e1b5c52eSStefan Hajnoczi bdrv_close(bs); 1869e1b5c52eSStefan Hajnoczi 18701b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 1871d22b2f41SRyan Harper bdrv_make_anon(bs); 187234c6f050Saurel32 18737267c094SAnthony Liguori g_free(bs); 1874fc01f7e7Sbellard } 1875fc01f7e7Sbellard 1876fa879d62SMarkus Armbruster int bdrv_attach_dev(BlockDriverState *bs, void *dev) 1877fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 187818846deeSMarkus Armbruster { 1879fa879d62SMarkus Armbruster if (bs->dev) { 188018846deeSMarkus Armbruster return -EBUSY; 188118846deeSMarkus Armbruster } 1882fa879d62SMarkus Armbruster bs->dev = dev; 188328a7282aSLuiz Capitulino bdrv_iostatus_reset(bs); 188418846deeSMarkus Armbruster return 0; 188518846deeSMarkus Armbruster } 188618846deeSMarkus Armbruster 1887fa879d62SMarkus Armbruster /* TODO qdevified devices don't use this, remove when devices are qdevified */ 1888fa879d62SMarkus Armbruster void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev) 188918846deeSMarkus Armbruster { 1890fa879d62SMarkus Armbruster if (bdrv_attach_dev(bs, dev) < 0) { 1891fa879d62SMarkus Armbruster abort(); 1892fa879d62SMarkus Armbruster } 1893fa879d62SMarkus Armbruster } 1894fa879d62SMarkus Armbruster 1895fa879d62SMarkus Armbruster void bdrv_detach_dev(BlockDriverState *bs, void *dev) 1896fa879d62SMarkus Armbruster /* TODO change to DeviceState *dev when all users are qdevified */ 1897fa879d62SMarkus Armbruster { 1898fa879d62SMarkus Armbruster assert(bs->dev == dev); 1899fa879d62SMarkus Armbruster bs->dev = NULL; 19000e49de52SMarkus Armbruster bs->dev_ops = NULL; 19010e49de52SMarkus Armbruster bs->dev_opaque = NULL; 190229e05f20SMarkus Armbruster bs->buffer_alignment = 512; 190318846deeSMarkus Armbruster } 190418846deeSMarkus Armbruster 1905fa879d62SMarkus Armbruster /* TODO change to return DeviceState * when all users are qdevified */ 1906fa879d62SMarkus Armbruster void *bdrv_get_attached_dev(BlockDriverState *bs) 190718846deeSMarkus Armbruster { 1908fa879d62SMarkus Armbruster return bs->dev; 190918846deeSMarkus Armbruster } 191018846deeSMarkus Armbruster 19110e49de52SMarkus Armbruster void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops, 19120e49de52SMarkus Armbruster void *opaque) 19130e49de52SMarkus Armbruster { 19140e49de52SMarkus Armbruster bs->dev_ops = ops; 19150e49de52SMarkus Armbruster bs->dev_opaque = opaque; 19160e49de52SMarkus Armbruster } 19170e49de52SMarkus Armbruster 191832c81a4aSPaolo Bonzini void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv, 191932c81a4aSPaolo Bonzini enum MonitorEvent ev, 19201ceee0d5SPaolo Bonzini BlockErrorAction action, bool is_read) 1921329c0a48SLuiz Capitulino { 1922329c0a48SLuiz Capitulino QObject *data; 1923329c0a48SLuiz Capitulino const char *action_str; 1924329c0a48SLuiz Capitulino 1925329c0a48SLuiz Capitulino switch (action) { 1926329c0a48SLuiz Capitulino case BDRV_ACTION_REPORT: 1927329c0a48SLuiz Capitulino action_str = "report"; 1928329c0a48SLuiz Capitulino break; 1929329c0a48SLuiz Capitulino case BDRV_ACTION_IGNORE: 1930329c0a48SLuiz Capitulino action_str = "ignore"; 1931329c0a48SLuiz Capitulino break; 1932329c0a48SLuiz Capitulino case BDRV_ACTION_STOP: 1933329c0a48SLuiz Capitulino action_str = "stop"; 1934329c0a48SLuiz Capitulino break; 1935329c0a48SLuiz Capitulino default: 1936329c0a48SLuiz Capitulino abort(); 1937329c0a48SLuiz Capitulino } 1938329c0a48SLuiz Capitulino 1939329c0a48SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 1940329c0a48SLuiz Capitulino bdrv->device_name, 1941329c0a48SLuiz Capitulino action_str, 1942329c0a48SLuiz Capitulino is_read ? "read" : "write"); 194332c81a4aSPaolo Bonzini monitor_protocol_event(ev, data); 1944329c0a48SLuiz Capitulino 1945329c0a48SLuiz Capitulino qobject_decref(data); 1946329c0a48SLuiz Capitulino } 1947329c0a48SLuiz Capitulino 19486f382ed2SLuiz Capitulino static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected) 19496f382ed2SLuiz Capitulino { 19506f382ed2SLuiz Capitulino QObject *data; 19516f382ed2SLuiz Capitulino 19526f382ed2SLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }", 19536f382ed2SLuiz Capitulino bdrv_get_device_name(bs), ejected); 19546f382ed2SLuiz Capitulino monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data); 19556f382ed2SLuiz Capitulino 19566f382ed2SLuiz Capitulino qobject_decref(data); 19576f382ed2SLuiz Capitulino } 19586f382ed2SLuiz Capitulino 19597d4b4ba5SMarkus Armbruster static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load) 19600e49de52SMarkus Armbruster { 1961145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->change_media_cb) { 19626f382ed2SLuiz Capitulino bool tray_was_closed = !bdrv_dev_is_tray_open(bs); 19637d4b4ba5SMarkus Armbruster bs->dev_ops->change_media_cb(bs->dev_opaque, load); 19646f382ed2SLuiz Capitulino if (tray_was_closed) { 19656f382ed2SLuiz Capitulino /* tray open */ 19666f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, true); 19676f382ed2SLuiz Capitulino } 19686f382ed2SLuiz Capitulino if (load) { 19696f382ed2SLuiz Capitulino /* tray close */ 19706f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, false); 19716f382ed2SLuiz Capitulino } 1972145feb17SMarkus Armbruster } 1973145feb17SMarkus Armbruster } 1974145feb17SMarkus Armbruster 19752c6942faSMarkus Armbruster bool bdrv_dev_has_removable_media(BlockDriverState *bs) 19762c6942faSMarkus Armbruster { 19772c6942faSMarkus Armbruster return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb); 19782c6942faSMarkus Armbruster } 19792c6942faSMarkus Armbruster 1980025ccaa7SPaolo Bonzini void bdrv_dev_eject_request(BlockDriverState *bs, bool force) 1981025ccaa7SPaolo Bonzini { 1982025ccaa7SPaolo Bonzini if (bs->dev_ops && bs->dev_ops->eject_request_cb) { 1983025ccaa7SPaolo Bonzini bs->dev_ops->eject_request_cb(bs->dev_opaque, force); 1984025ccaa7SPaolo Bonzini } 1985025ccaa7SPaolo Bonzini } 1986025ccaa7SPaolo Bonzini 1987e4def80bSMarkus Armbruster bool bdrv_dev_is_tray_open(BlockDriverState *bs) 1988e4def80bSMarkus Armbruster { 1989e4def80bSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_tray_open) { 1990e4def80bSMarkus Armbruster return bs->dev_ops->is_tray_open(bs->dev_opaque); 1991e4def80bSMarkus Armbruster } 1992e4def80bSMarkus Armbruster return false; 1993e4def80bSMarkus Armbruster } 1994e4def80bSMarkus Armbruster 1995145feb17SMarkus Armbruster static void bdrv_dev_resize_cb(BlockDriverState *bs) 1996145feb17SMarkus Armbruster { 1997145feb17SMarkus Armbruster if (bs->dev_ops && bs->dev_ops->resize_cb) { 1998145feb17SMarkus Armbruster bs->dev_ops->resize_cb(bs->dev_opaque); 19990e49de52SMarkus Armbruster } 20000e49de52SMarkus Armbruster } 20010e49de52SMarkus Armbruster 2002f107639aSMarkus Armbruster bool bdrv_dev_is_medium_locked(BlockDriverState *bs) 2003f107639aSMarkus Armbruster { 2004f107639aSMarkus Armbruster if (bs->dev_ops && bs->dev_ops->is_medium_locked) { 2005f107639aSMarkus Armbruster return bs->dev_ops->is_medium_locked(bs->dev_opaque); 2006f107639aSMarkus Armbruster } 2007f107639aSMarkus Armbruster return false; 2008f107639aSMarkus Armbruster } 2009f107639aSMarkus Armbruster 2010e97fc193Saliguori /* 2011e97fc193Saliguori * Run consistency checks on an image 2012e97fc193Saliguori * 2013e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 2014a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 2015e076f338SKevin Wolf * check are stored in res. 2016e97fc193Saliguori */ 20174534ff54SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix) 2018e97fc193Saliguori { 2019e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 2020e97fc193Saliguori return -ENOTSUP; 2021e97fc193Saliguori } 2022e97fc193Saliguori 2023e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 20244534ff54SKevin Wolf return bs->drv->bdrv_check(bs, res, fix); 2025e97fc193Saliguori } 2026e97fc193Saliguori 20278a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 20288a426614SKevin Wolf 202933e3963eSbellard /* commit COW file into the raw image */ 203033e3963eSbellard int bdrv_commit(BlockDriverState *bs) 203133e3963eSbellard { 203219cb3738Sbellard BlockDriver *drv = bs->drv; 20338a426614SKevin Wolf int64_t sector, total_sectors; 20348a426614SKevin Wolf int n, ro, open_flags; 20350bce597dSJeff Cody int ret = 0; 20368a426614SKevin Wolf uint8_t *buf; 2037c2cba3d9SJim Meyering char filename[PATH_MAX]; 203833e3963eSbellard 203919cb3738Sbellard if (!drv) 204019cb3738Sbellard return -ENOMEDIUM; 204133e3963eSbellard 20424dca4b63SNaphtali Sprei if (!bs->backing_hd) { 20434dca4b63SNaphtali Sprei return -ENOTSUP; 20444dca4b63SNaphtali Sprei } 20454dca4b63SNaphtali Sprei 20462d3735d3SStefan Hajnoczi if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) { 20472d3735d3SStefan Hajnoczi return -EBUSY; 20482d3735d3SStefan Hajnoczi } 20492d3735d3SStefan Hajnoczi 20504dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 2051c2cba3d9SJim Meyering /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */ 2052c2cba3d9SJim Meyering pstrcpy(filename, sizeof(filename), bs->backing_hd->filename); 20534dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 20544dca4b63SNaphtali Sprei 20554dca4b63SNaphtali Sprei if (ro) { 20560bce597dSJeff Cody if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) { 20570bce597dSJeff Cody return -EACCES; 20584dca4b63SNaphtali Sprei } 2059ea2384d3Sbellard } 2060ea2384d3Sbellard 20616ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 20627267c094SAnthony Liguori buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 20638a426614SKevin Wolf 20648a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 2065d663640cSPaolo Bonzini ret = bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n); 2066d663640cSPaolo Bonzini if (ret < 0) { 2067d663640cSPaolo Bonzini goto ro_cleanup; 2068d663640cSPaolo Bonzini } 2069d663640cSPaolo Bonzini if (ret) { 20708a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 20714dca4b63SNaphtali Sprei ret = -EIO; 20724dca4b63SNaphtali Sprei goto ro_cleanup; 207333e3963eSbellard } 207433e3963eSbellard 20758a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 20764dca4b63SNaphtali Sprei ret = -EIO; 20774dca4b63SNaphtali Sprei goto ro_cleanup; 207833e3963eSbellard } 207933e3963eSbellard } 208033e3963eSbellard } 208195389c86Sbellard 20821d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 20831d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 20841d44952fSChristoph Hellwig bdrv_flush(bs); 20851d44952fSChristoph Hellwig } 208695389c86Sbellard 20873f5075aeSChristoph Hellwig /* 20883f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 20893f5075aeSChristoph Hellwig * stable on disk. 20903f5075aeSChristoph Hellwig */ 20913f5075aeSChristoph Hellwig if (bs->backing_hd) 20923f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 20934dca4b63SNaphtali Sprei 20944dca4b63SNaphtali Sprei ro_cleanup: 20957267c094SAnthony Liguori g_free(buf); 20964dca4b63SNaphtali Sprei 20974dca4b63SNaphtali Sprei if (ro) { 20980bce597dSJeff Cody /* ignoring error return here */ 20990bce597dSJeff Cody bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL); 21004dca4b63SNaphtali Sprei } 21014dca4b63SNaphtali Sprei 21021d44952fSChristoph Hellwig return ret; 210333e3963eSbellard } 210433e3963eSbellard 2105e8877497SStefan Hajnoczi int bdrv_commit_all(void) 21066ab4b5abSMarkus Armbruster { 21076ab4b5abSMarkus Armbruster BlockDriverState *bs; 21086ab4b5abSMarkus Armbruster 2109dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 2110272d2d8eSJeff Cody if (bs->drv && bs->backing_hd) { 2111e8877497SStefan Hajnoczi int ret = bdrv_commit(bs); 2112e8877497SStefan Hajnoczi if (ret < 0) { 2113e8877497SStefan Hajnoczi return ret; 21146ab4b5abSMarkus Armbruster } 21156ab4b5abSMarkus Armbruster } 2116272d2d8eSJeff Cody } 2117e8877497SStefan Hajnoczi return 0; 2118e8877497SStefan Hajnoczi } 21196ab4b5abSMarkus Armbruster 2120dbffbdcfSStefan Hajnoczi /** 2121dbffbdcfSStefan Hajnoczi * Remove an active request from the tracked requests list 2122dbffbdcfSStefan Hajnoczi * 2123dbffbdcfSStefan Hajnoczi * This function should be called when a tracked request is completing. 2124dbffbdcfSStefan Hajnoczi */ 2125dbffbdcfSStefan Hajnoczi static void tracked_request_end(BdrvTrackedRequest *req) 2126dbffbdcfSStefan Hajnoczi { 2127dbffbdcfSStefan Hajnoczi QLIST_REMOVE(req, list); 2128f4658285SStefan Hajnoczi qemu_co_queue_restart_all(&req->wait_queue); 2129dbffbdcfSStefan Hajnoczi } 2130dbffbdcfSStefan Hajnoczi 2131dbffbdcfSStefan Hajnoczi /** 2132dbffbdcfSStefan Hajnoczi * Add an active request to the tracked requests list 2133dbffbdcfSStefan Hajnoczi */ 2134dbffbdcfSStefan Hajnoczi static void tracked_request_begin(BdrvTrackedRequest *req, 2135dbffbdcfSStefan Hajnoczi BlockDriverState *bs, 2136dbffbdcfSStefan Hajnoczi int64_t sector_num, 2137dbffbdcfSStefan Hajnoczi int nb_sectors, bool is_write) 2138dbffbdcfSStefan Hajnoczi { 2139dbffbdcfSStefan Hajnoczi *req = (BdrvTrackedRequest){ 2140dbffbdcfSStefan Hajnoczi .bs = bs, 2141dbffbdcfSStefan Hajnoczi .sector_num = sector_num, 2142dbffbdcfSStefan Hajnoczi .nb_sectors = nb_sectors, 2143dbffbdcfSStefan Hajnoczi .is_write = is_write, 21445f8b6491SStefan Hajnoczi .co = qemu_coroutine_self(), 2145dbffbdcfSStefan Hajnoczi }; 2146dbffbdcfSStefan Hajnoczi 2147f4658285SStefan Hajnoczi qemu_co_queue_init(&req->wait_queue); 2148f4658285SStefan Hajnoczi 2149dbffbdcfSStefan Hajnoczi QLIST_INSERT_HEAD(&bs->tracked_requests, req, list); 2150dbffbdcfSStefan Hajnoczi } 2151dbffbdcfSStefan Hajnoczi 2152d83947acSStefan Hajnoczi /** 2153d83947acSStefan Hajnoczi * Round a region to cluster boundaries 2154d83947acSStefan Hajnoczi */ 2155343bded4SPaolo Bonzini void bdrv_round_to_clusters(BlockDriverState *bs, 2156d83947acSStefan Hajnoczi int64_t sector_num, int nb_sectors, 2157d83947acSStefan Hajnoczi int64_t *cluster_sector_num, 2158d83947acSStefan Hajnoczi int *cluster_nb_sectors) 2159d83947acSStefan Hajnoczi { 2160d83947acSStefan Hajnoczi BlockDriverInfo bdi; 2161d83947acSStefan Hajnoczi 2162d83947acSStefan Hajnoczi if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) { 2163d83947acSStefan Hajnoczi *cluster_sector_num = sector_num; 2164d83947acSStefan Hajnoczi *cluster_nb_sectors = nb_sectors; 2165d83947acSStefan Hajnoczi } else { 2166d83947acSStefan Hajnoczi int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE; 2167d83947acSStefan Hajnoczi *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c); 2168d83947acSStefan Hajnoczi *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num + 2169d83947acSStefan Hajnoczi nb_sectors, c); 2170d83947acSStefan Hajnoczi } 2171d83947acSStefan Hajnoczi } 2172d83947acSStefan Hajnoczi 2173f4658285SStefan Hajnoczi static bool tracked_request_overlaps(BdrvTrackedRequest *req, 2174f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) { 2175d83947acSStefan Hajnoczi /* aaaa bbbb */ 2176d83947acSStefan Hajnoczi if (sector_num >= req->sector_num + req->nb_sectors) { 2177d83947acSStefan Hajnoczi return false; 2178d83947acSStefan Hajnoczi } 2179d83947acSStefan Hajnoczi /* bbbb aaaa */ 2180d83947acSStefan Hajnoczi if (req->sector_num >= sector_num + nb_sectors) { 2181d83947acSStefan Hajnoczi return false; 2182d83947acSStefan Hajnoczi } 2183d83947acSStefan Hajnoczi return true; 2184f4658285SStefan Hajnoczi } 2185f4658285SStefan Hajnoczi 2186f4658285SStefan Hajnoczi static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs, 2187f4658285SStefan Hajnoczi int64_t sector_num, int nb_sectors) 2188f4658285SStefan Hajnoczi { 2189f4658285SStefan Hajnoczi BdrvTrackedRequest *req; 2190d83947acSStefan Hajnoczi int64_t cluster_sector_num; 2191d83947acSStefan Hajnoczi int cluster_nb_sectors; 2192f4658285SStefan Hajnoczi bool retry; 2193f4658285SStefan Hajnoczi 2194d83947acSStefan Hajnoczi /* If we touch the same cluster it counts as an overlap. This guarantees 2195d83947acSStefan Hajnoczi * that allocating writes will be serialized and not race with each other 2196d83947acSStefan Hajnoczi * for the same cluster. For example, in copy-on-read it ensures that the 2197d83947acSStefan Hajnoczi * CoR read and write operations are atomic and guest writes cannot 2198d83947acSStefan Hajnoczi * interleave between them. 2199d83947acSStefan Hajnoczi */ 2200343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2201d83947acSStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2202d83947acSStefan Hajnoczi 2203f4658285SStefan Hajnoczi do { 2204f4658285SStefan Hajnoczi retry = false; 2205f4658285SStefan Hajnoczi QLIST_FOREACH(req, &bs->tracked_requests, list) { 2206d83947acSStefan Hajnoczi if (tracked_request_overlaps(req, cluster_sector_num, 2207d83947acSStefan Hajnoczi cluster_nb_sectors)) { 22085f8b6491SStefan Hajnoczi /* Hitting this means there was a reentrant request, for 22095f8b6491SStefan Hajnoczi * example, a block driver issuing nested requests. This must 22105f8b6491SStefan Hajnoczi * never happen since it means deadlock. 22115f8b6491SStefan Hajnoczi */ 22125f8b6491SStefan Hajnoczi assert(qemu_coroutine_self() != req->co); 22135f8b6491SStefan Hajnoczi 2214f4658285SStefan Hajnoczi qemu_co_queue_wait(&req->wait_queue); 2215f4658285SStefan Hajnoczi retry = true; 2216f4658285SStefan Hajnoczi break; 2217f4658285SStefan Hajnoczi } 2218f4658285SStefan Hajnoczi } 2219f4658285SStefan Hajnoczi } while (retry); 2220f4658285SStefan Hajnoczi } 2221f4658285SStefan Hajnoczi 2222756e6736SKevin Wolf /* 2223756e6736SKevin Wolf * Return values: 2224756e6736SKevin Wolf * 0 - success 2225756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 2226756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 2227756e6736SKevin Wolf * image file header 2228756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 2229756e6736SKevin Wolf */ 2230756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 2231756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 2232756e6736SKevin Wolf { 2233756e6736SKevin Wolf BlockDriver *drv = bs->drv; 2234469ef350SPaolo Bonzini int ret; 2235756e6736SKevin Wolf 22365f377794SPaolo Bonzini /* Backing file format doesn't make sense without a backing file */ 22375f377794SPaolo Bonzini if (backing_fmt && !backing_file) { 22385f377794SPaolo Bonzini return -EINVAL; 22395f377794SPaolo Bonzini } 22405f377794SPaolo Bonzini 2241756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 2242469ef350SPaolo Bonzini ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 2243756e6736SKevin Wolf } else { 2244469ef350SPaolo Bonzini ret = -ENOTSUP; 2245756e6736SKevin Wolf } 2246469ef350SPaolo Bonzini 2247469ef350SPaolo Bonzini if (ret == 0) { 2248469ef350SPaolo Bonzini pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: ""); 2249469ef350SPaolo Bonzini pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: ""); 2250469ef350SPaolo Bonzini } 2251469ef350SPaolo Bonzini return ret; 2252756e6736SKevin Wolf } 2253756e6736SKevin Wolf 22546ebdcee2SJeff Cody /* 22556ebdcee2SJeff Cody * Finds the image layer in the chain that has 'bs' as its backing file. 22566ebdcee2SJeff Cody * 22576ebdcee2SJeff Cody * active is the current topmost image. 22586ebdcee2SJeff Cody * 22596ebdcee2SJeff Cody * Returns NULL if bs is not found in active's image chain, 22606ebdcee2SJeff Cody * or if active == bs. 22616ebdcee2SJeff Cody */ 22626ebdcee2SJeff Cody BlockDriverState *bdrv_find_overlay(BlockDriverState *active, 22636ebdcee2SJeff Cody BlockDriverState *bs) 22646ebdcee2SJeff Cody { 22656ebdcee2SJeff Cody BlockDriverState *overlay = NULL; 22666ebdcee2SJeff Cody BlockDriverState *intermediate; 22676ebdcee2SJeff Cody 22686ebdcee2SJeff Cody assert(active != NULL); 22696ebdcee2SJeff Cody assert(bs != NULL); 22706ebdcee2SJeff Cody 22716ebdcee2SJeff Cody /* if bs is the same as active, then by definition it has no overlay 22726ebdcee2SJeff Cody */ 22736ebdcee2SJeff Cody if (active == bs) { 22746ebdcee2SJeff Cody return NULL; 22756ebdcee2SJeff Cody } 22766ebdcee2SJeff Cody 22776ebdcee2SJeff Cody intermediate = active; 22786ebdcee2SJeff Cody while (intermediate->backing_hd) { 22796ebdcee2SJeff Cody if (intermediate->backing_hd == bs) { 22806ebdcee2SJeff Cody overlay = intermediate; 22816ebdcee2SJeff Cody break; 22826ebdcee2SJeff Cody } 22836ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 22846ebdcee2SJeff Cody } 22856ebdcee2SJeff Cody 22866ebdcee2SJeff Cody return overlay; 22876ebdcee2SJeff Cody } 22886ebdcee2SJeff Cody 22896ebdcee2SJeff Cody typedef struct BlkIntermediateStates { 22906ebdcee2SJeff Cody BlockDriverState *bs; 22916ebdcee2SJeff Cody QSIMPLEQ_ENTRY(BlkIntermediateStates) entry; 22926ebdcee2SJeff Cody } BlkIntermediateStates; 22936ebdcee2SJeff Cody 22946ebdcee2SJeff Cody 22956ebdcee2SJeff Cody /* 22966ebdcee2SJeff Cody * Drops images above 'base' up to and including 'top', and sets the image 22976ebdcee2SJeff Cody * above 'top' to have base as its backing file. 22986ebdcee2SJeff Cody * 22996ebdcee2SJeff Cody * Requires that the overlay to 'top' is opened r/w, so that the backing file 23006ebdcee2SJeff Cody * information in 'bs' can be properly updated. 23016ebdcee2SJeff Cody * 23026ebdcee2SJeff Cody * E.g., this will convert the following chain: 23036ebdcee2SJeff Cody * bottom <- base <- intermediate <- top <- active 23046ebdcee2SJeff Cody * 23056ebdcee2SJeff Cody * to 23066ebdcee2SJeff Cody * 23076ebdcee2SJeff Cody * bottom <- base <- active 23086ebdcee2SJeff Cody * 23096ebdcee2SJeff Cody * It is allowed for bottom==base, in which case it converts: 23106ebdcee2SJeff Cody * 23116ebdcee2SJeff Cody * base <- intermediate <- top <- active 23126ebdcee2SJeff Cody * 23136ebdcee2SJeff Cody * to 23146ebdcee2SJeff Cody * 23156ebdcee2SJeff Cody * base <- active 23166ebdcee2SJeff Cody * 23176ebdcee2SJeff Cody * Error conditions: 23186ebdcee2SJeff Cody * if active == top, that is considered an error 23196ebdcee2SJeff Cody * 23206ebdcee2SJeff Cody */ 23216ebdcee2SJeff Cody int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top, 23226ebdcee2SJeff Cody BlockDriverState *base) 23236ebdcee2SJeff Cody { 23246ebdcee2SJeff Cody BlockDriverState *intermediate; 23256ebdcee2SJeff Cody BlockDriverState *base_bs = NULL; 23266ebdcee2SJeff Cody BlockDriverState *new_top_bs = NULL; 23276ebdcee2SJeff Cody BlkIntermediateStates *intermediate_state, *next; 23286ebdcee2SJeff Cody int ret = -EIO; 23296ebdcee2SJeff Cody 23306ebdcee2SJeff Cody QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete; 23316ebdcee2SJeff Cody QSIMPLEQ_INIT(&states_to_delete); 23326ebdcee2SJeff Cody 23336ebdcee2SJeff Cody if (!top->drv || !base->drv) { 23346ebdcee2SJeff Cody goto exit; 23356ebdcee2SJeff Cody } 23366ebdcee2SJeff Cody 23376ebdcee2SJeff Cody new_top_bs = bdrv_find_overlay(active, top); 23386ebdcee2SJeff Cody 23396ebdcee2SJeff Cody if (new_top_bs == NULL) { 23406ebdcee2SJeff Cody /* we could not find the image above 'top', this is an error */ 23416ebdcee2SJeff Cody goto exit; 23426ebdcee2SJeff Cody } 23436ebdcee2SJeff Cody 23446ebdcee2SJeff Cody /* special case of new_top_bs->backing_hd already pointing to base - nothing 23456ebdcee2SJeff Cody * to do, no intermediate images */ 23466ebdcee2SJeff Cody if (new_top_bs->backing_hd == base) { 23476ebdcee2SJeff Cody ret = 0; 23486ebdcee2SJeff Cody goto exit; 23496ebdcee2SJeff Cody } 23506ebdcee2SJeff Cody 23516ebdcee2SJeff Cody intermediate = top; 23526ebdcee2SJeff Cody 23536ebdcee2SJeff Cody /* now we will go down through the list, and add each BDS we find 23546ebdcee2SJeff Cody * into our deletion queue, until we hit the 'base' 23556ebdcee2SJeff Cody */ 23566ebdcee2SJeff Cody while (intermediate) { 23576ebdcee2SJeff Cody intermediate_state = g_malloc0(sizeof(BlkIntermediateStates)); 23586ebdcee2SJeff Cody intermediate_state->bs = intermediate; 23596ebdcee2SJeff Cody QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry); 23606ebdcee2SJeff Cody 23616ebdcee2SJeff Cody if (intermediate->backing_hd == base) { 23626ebdcee2SJeff Cody base_bs = intermediate->backing_hd; 23636ebdcee2SJeff Cody break; 23646ebdcee2SJeff Cody } 23656ebdcee2SJeff Cody intermediate = intermediate->backing_hd; 23666ebdcee2SJeff Cody } 23676ebdcee2SJeff Cody if (base_bs == NULL) { 23686ebdcee2SJeff Cody /* something went wrong, we did not end at the base. safely 23696ebdcee2SJeff Cody * unravel everything, and exit with error */ 23706ebdcee2SJeff Cody goto exit; 23716ebdcee2SJeff Cody } 23726ebdcee2SJeff Cody 23736ebdcee2SJeff Cody /* success - we can delete the intermediate states, and link top->base */ 23746ebdcee2SJeff Cody ret = bdrv_change_backing_file(new_top_bs, base_bs->filename, 23756ebdcee2SJeff Cody base_bs->drv ? base_bs->drv->format_name : ""); 23766ebdcee2SJeff Cody if (ret) { 23776ebdcee2SJeff Cody goto exit; 23786ebdcee2SJeff Cody } 23796ebdcee2SJeff Cody new_top_bs->backing_hd = base_bs; 23806ebdcee2SJeff Cody 23816ebdcee2SJeff Cody 23826ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 23836ebdcee2SJeff Cody /* so that bdrv_close() does not recursively close the chain */ 23846ebdcee2SJeff Cody intermediate_state->bs->backing_hd = NULL; 23854f6fd349SFam Zheng bdrv_unref(intermediate_state->bs); 23866ebdcee2SJeff Cody } 23876ebdcee2SJeff Cody ret = 0; 23886ebdcee2SJeff Cody 23896ebdcee2SJeff Cody exit: 23906ebdcee2SJeff Cody QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) { 23916ebdcee2SJeff Cody g_free(intermediate_state); 23926ebdcee2SJeff Cody } 23936ebdcee2SJeff Cody return ret; 23946ebdcee2SJeff Cody } 23956ebdcee2SJeff Cody 23966ebdcee2SJeff Cody 239771d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 239871d0770cSaliguori size_t size) 239971d0770cSaliguori { 240071d0770cSaliguori int64_t len; 240171d0770cSaliguori 240271d0770cSaliguori if (!bdrv_is_inserted(bs)) 240371d0770cSaliguori return -ENOMEDIUM; 240471d0770cSaliguori 240571d0770cSaliguori if (bs->growable) 240671d0770cSaliguori return 0; 240771d0770cSaliguori 240871d0770cSaliguori len = bdrv_getlength(bs); 240971d0770cSaliguori 2410fbb7b4e0SKevin Wolf if (offset < 0) 2411fbb7b4e0SKevin Wolf return -EIO; 2412fbb7b4e0SKevin Wolf 2413fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 241471d0770cSaliguori return -EIO; 241571d0770cSaliguori 241671d0770cSaliguori return 0; 241771d0770cSaliguori } 241871d0770cSaliguori 241971d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 242071d0770cSaliguori int nb_sectors) 242171d0770cSaliguori { 2422eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 2423eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 242471d0770cSaliguori } 242571d0770cSaliguori 24261c9805a3SStefan Hajnoczi typedef struct RwCo { 24271c9805a3SStefan Hajnoczi BlockDriverState *bs; 24281c9805a3SStefan Hajnoczi int64_t sector_num; 24291c9805a3SStefan Hajnoczi int nb_sectors; 24301c9805a3SStefan Hajnoczi QEMUIOVector *qiov; 24311c9805a3SStefan Hajnoczi bool is_write; 24321c9805a3SStefan Hajnoczi int ret; 24334105eaaaSPeter Lieven BdrvRequestFlags flags; 24341c9805a3SStefan Hajnoczi } RwCo; 24351c9805a3SStefan Hajnoczi 24361c9805a3SStefan Hajnoczi static void coroutine_fn bdrv_rw_co_entry(void *opaque) 2437fc01f7e7Sbellard { 24381c9805a3SStefan Hajnoczi RwCo *rwco = opaque; 2439fc01f7e7Sbellard 24401c9805a3SStefan Hajnoczi if (!rwco->is_write) { 24411c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num, 24424105eaaaSPeter Lieven rwco->nb_sectors, rwco->qiov, 24434105eaaaSPeter Lieven rwco->flags); 24441c9805a3SStefan Hajnoczi } else { 24451c9805a3SStefan Hajnoczi rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num, 24464105eaaaSPeter Lieven rwco->nb_sectors, rwco->qiov, 24474105eaaaSPeter Lieven rwco->flags); 24481c9805a3SStefan Hajnoczi } 24491c9805a3SStefan Hajnoczi } 2450e7a8a783SKevin Wolf 24511c9805a3SStefan Hajnoczi /* 24528d3b1a2dSKevin Wolf * Process a vectored synchronous request using coroutines 24531c9805a3SStefan Hajnoczi */ 24548d3b1a2dSKevin Wolf static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num, 24554105eaaaSPeter Lieven QEMUIOVector *qiov, bool is_write, 24564105eaaaSPeter Lieven BdrvRequestFlags flags) 24571c9805a3SStefan Hajnoczi { 24581c9805a3SStefan Hajnoczi Coroutine *co; 24591c9805a3SStefan Hajnoczi RwCo rwco = { 24601c9805a3SStefan Hajnoczi .bs = bs, 24611c9805a3SStefan Hajnoczi .sector_num = sector_num, 24628d3b1a2dSKevin Wolf .nb_sectors = qiov->size >> BDRV_SECTOR_BITS, 24638d3b1a2dSKevin Wolf .qiov = qiov, 24641c9805a3SStefan Hajnoczi .is_write = is_write, 24651c9805a3SStefan Hajnoczi .ret = NOT_DONE, 24664105eaaaSPeter Lieven .flags = flags, 24671c9805a3SStefan Hajnoczi }; 24688d3b1a2dSKevin Wolf assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0); 24691c9805a3SStefan Hajnoczi 2470498e386cSZhi Yong Wu /** 2471498e386cSZhi Yong Wu * In sync call context, when the vcpu is blocked, this throttling timer 2472498e386cSZhi Yong Wu * will not fire; so the I/O throttling function has to be disabled here 2473498e386cSZhi Yong Wu * if it has been enabled. 2474498e386cSZhi Yong Wu */ 2475498e386cSZhi Yong Wu if (bs->io_limits_enabled) { 2476498e386cSZhi Yong Wu fprintf(stderr, "Disabling I/O throttling on '%s' due " 2477498e386cSZhi Yong Wu "to synchronous I/O.\n", bdrv_get_device_name(bs)); 2478498e386cSZhi Yong Wu bdrv_io_limits_disable(bs); 2479498e386cSZhi Yong Wu } 2480498e386cSZhi Yong Wu 24811c9805a3SStefan Hajnoczi if (qemu_in_coroutine()) { 24821c9805a3SStefan Hajnoczi /* Fast-path if already in coroutine context */ 24831c9805a3SStefan Hajnoczi bdrv_rw_co_entry(&rwco); 24841c9805a3SStefan Hajnoczi } else { 24851c9805a3SStefan Hajnoczi co = qemu_coroutine_create(bdrv_rw_co_entry); 24861c9805a3SStefan Hajnoczi qemu_coroutine_enter(co, &rwco); 24871c9805a3SStefan Hajnoczi while (rwco.ret == NOT_DONE) { 24881c9805a3SStefan Hajnoczi qemu_aio_wait(); 24891c9805a3SStefan Hajnoczi } 24901c9805a3SStefan Hajnoczi } 24911c9805a3SStefan Hajnoczi return rwco.ret; 2492e7a8a783SKevin Wolf } 2493e7a8a783SKevin Wolf 24948d3b1a2dSKevin Wolf /* 24958d3b1a2dSKevin Wolf * Process a synchronous request using coroutines 24968d3b1a2dSKevin Wolf */ 24978d3b1a2dSKevin Wolf static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, 24984105eaaaSPeter Lieven int nb_sectors, bool is_write, BdrvRequestFlags flags) 24998d3b1a2dSKevin Wolf { 25008d3b1a2dSKevin Wolf QEMUIOVector qiov; 25018d3b1a2dSKevin Wolf struct iovec iov = { 25028d3b1a2dSKevin Wolf .iov_base = (void *)buf, 25038d3b1a2dSKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 25048d3b1a2dSKevin Wolf }; 25058d3b1a2dSKevin Wolf 25068d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 25074105eaaaSPeter Lieven return bdrv_rwv_co(bs, sector_num, &qiov, is_write, flags); 25088d3b1a2dSKevin Wolf } 25098d3b1a2dSKevin Wolf 25101c9805a3SStefan Hajnoczi /* return < 0 if error. See bdrv_write() for the return codes */ 25111c9805a3SStefan Hajnoczi int bdrv_read(BlockDriverState *bs, int64_t sector_num, 25121c9805a3SStefan Hajnoczi uint8_t *buf, int nb_sectors) 25131c9805a3SStefan Hajnoczi { 25144105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false, 0); 251583f64091Sbellard } 2516fc01f7e7Sbellard 251707d27a44SMarkus Armbruster /* Just like bdrv_read(), but with I/O throttling temporarily disabled */ 251807d27a44SMarkus Armbruster int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num, 251907d27a44SMarkus Armbruster uint8_t *buf, int nb_sectors) 252007d27a44SMarkus Armbruster { 252107d27a44SMarkus Armbruster bool enabled; 252207d27a44SMarkus Armbruster int ret; 252307d27a44SMarkus Armbruster 252407d27a44SMarkus Armbruster enabled = bs->io_limits_enabled; 252507d27a44SMarkus Armbruster bs->io_limits_enabled = false; 25264e7395e8SPeter Lieven ret = bdrv_read(bs, sector_num, buf, nb_sectors); 252707d27a44SMarkus Armbruster bs->io_limits_enabled = enabled; 252807d27a44SMarkus Armbruster return ret; 252907d27a44SMarkus Armbruster } 253007d27a44SMarkus Armbruster 253119cb3738Sbellard /* Return < 0 if error. Important errors are: 253219cb3738Sbellard -EIO generic I/O error (may happen for all errors) 253319cb3738Sbellard -ENOMEDIUM No media inserted. 253419cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 253519cb3738Sbellard -EACCES Trying to write a read-only device 253619cb3738Sbellard */ 2537fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 2538fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 2539fc01f7e7Sbellard { 25404105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true, 0); 254183f64091Sbellard } 254283f64091Sbellard 25438d3b1a2dSKevin Wolf int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov) 25448d3b1a2dSKevin Wolf { 25454105eaaaSPeter Lieven return bdrv_rwv_co(bs, sector_num, qiov, true, 0); 25464105eaaaSPeter Lieven } 25474105eaaaSPeter Lieven 2548aa7bfbffSPeter Lieven int bdrv_write_zeroes(BlockDriverState *bs, int64_t sector_num, 2549aa7bfbffSPeter Lieven int nb_sectors, BdrvRequestFlags flags) 25504105eaaaSPeter Lieven { 25514105eaaaSPeter Lieven return bdrv_rw_co(bs, sector_num, NULL, nb_sectors, true, 2552aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 25538d3b1a2dSKevin Wolf } 25548d3b1a2dSKevin Wolf 2555d75cbb5eSPeter Lieven /* 2556d75cbb5eSPeter Lieven * Completely zero out a block device with the help of bdrv_write_zeroes. 2557d75cbb5eSPeter Lieven * The operation is sped up by checking the block status and only writing 2558d75cbb5eSPeter Lieven * zeroes to the device if they currently do not return zeroes. Optional 2559d75cbb5eSPeter Lieven * flags are passed through to bdrv_write_zeroes (e.g. BDRV_REQ_MAY_UNMAP). 2560d75cbb5eSPeter Lieven * 2561d75cbb5eSPeter Lieven * Returns < 0 on error, 0 on success. For error codes see bdrv_write(). 2562d75cbb5eSPeter Lieven */ 2563d75cbb5eSPeter Lieven int bdrv_make_zero(BlockDriverState *bs, BdrvRequestFlags flags) 2564d75cbb5eSPeter Lieven { 2565d75cbb5eSPeter Lieven int64_t target_size = bdrv_getlength(bs) / BDRV_SECTOR_SIZE; 2566d75cbb5eSPeter Lieven int64_t ret, nb_sectors, sector_num = 0; 2567d75cbb5eSPeter Lieven int n; 2568d75cbb5eSPeter Lieven 2569d75cbb5eSPeter Lieven for (;;) { 2570d75cbb5eSPeter Lieven nb_sectors = target_size - sector_num; 2571d75cbb5eSPeter Lieven if (nb_sectors <= 0) { 2572d75cbb5eSPeter Lieven return 0; 2573d75cbb5eSPeter Lieven } 2574d75cbb5eSPeter Lieven if (nb_sectors > INT_MAX) { 2575d75cbb5eSPeter Lieven nb_sectors = INT_MAX; 2576d75cbb5eSPeter Lieven } 2577d75cbb5eSPeter Lieven ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &n); 25783d94ce60SPeter Lieven if (ret < 0) { 25793d94ce60SPeter Lieven error_report("error getting block status at sector %" PRId64 ": %s", 25803d94ce60SPeter Lieven sector_num, strerror(-ret)); 25813d94ce60SPeter Lieven return ret; 25823d94ce60SPeter Lieven } 2583d75cbb5eSPeter Lieven if (ret & BDRV_BLOCK_ZERO) { 2584d75cbb5eSPeter Lieven sector_num += n; 2585d75cbb5eSPeter Lieven continue; 2586d75cbb5eSPeter Lieven } 2587d75cbb5eSPeter Lieven ret = bdrv_write_zeroes(bs, sector_num, n, flags); 2588d75cbb5eSPeter Lieven if (ret < 0) { 2589d75cbb5eSPeter Lieven error_report("error writing zeroes at sector %" PRId64 ": %s", 2590d75cbb5eSPeter Lieven sector_num, strerror(-ret)); 2591d75cbb5eSPeter Lieven return ret; 2592d75cbb5eSPeter Lieven } 2593d75cbb5eSPeter Lieven sector_num += n; 2594d75cbb5eSPeter Lieven } 2595d75cbb5eSPeter Lieven } 2596d75cbb5eSPeter Lieven 2597eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 2598eda578e5Saliguori void *buf, int count1) 259983f64091Sbellard { 26006ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 260183f64091Sbellard int len, nb_sectors, count; 260283f64091Sbellard int64_t sector_num; 26039a8c4cceSKevin Wolf int ret; 260483f64091Sbellard 260583f64091Sbellard count = count1; 260683f64091Sbellard /* first read to align to sector start */ 26076ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 260883f64091Sbellard if (len > count) 260983f64091Sbellard len = count; 26106ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 261183f64091Sbellard if (len > 0) { 26129a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 26139a8c4cceSKevin Wolf return ret; 26146ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 261583f64091Sbellard count -= len; 261683f64091Sbellard if (count == 0) 261783f64091Sbellard return count1; 261883f64091Sbellard sector_num++; 261983f64091Sbellard buf += len; 262083f64091Sbellard } 262183f64091Sbellard 262283f64091Sbellard /* read the sectors "in place" */ 26236ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 262483f64091Sbellard if (nb_sectors > 0) { 26259a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 26269a8c4cceSKevin Wolf return ret; 262783f64091Sbellard sector_num += nb_sectors; 26286ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 262983f64091Sbellard buf += len; 263083f64091Sbellard count -= len; 263183f64091Sbellard } 263283f64091Sbellard 263383f64091Sbellard /* add data from the last sector */ 263483f64091Sbellard if (count > 0) { 26359a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 26369a8c4cceSKevin Wolf return ret; 263783f64091Sbellard memcpy(buf, tmp_buf, count); 263883f64091Sbellard } 263983f64091Sbellard return count1; 264083f64091Sbellard } 264183f64091Sbellard 26428d3b1a2dSKevin Wolf int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov) 264383f64091Sbellard { 26446ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 264583f64091Sbellard int len, nb_sectors, count; 264683f64091Sbellard int64_t sector_num; 26479a8c4cceSKevin Wolf int ret; 264883f64091Sbellard 26498d3b1a2dSKevin Wolf count = qiov->size; 26508d3b1a2dSKevin Wolf 265183f64091Sbellard /* first write to align to sector start */ 26526ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 265383f64091Sbellard if (len > count) 265483f64091Sbellard len = count; 26556ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 265683f64091Sbellard if (len > 0) { 26579a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 26589a8c4cceSKevin Wolf return ret; 26598d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), 26608d3b1a2dSKevin Wolf len); 26619a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 26629a8c4cceSKevin Wolf return ret; 266383f64091Sbellard count -= len; 266483f64091Sbellard if (count == 0) 26658d3b1a2dSKevin Wolf return qiov->size; 266683f64091Sbellard sector_num++; 266783f64091Sbellard } 266883f64091Sbellard 266983f64091Sbellard /* write the sectors "in place" */ 26706ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 267183f64091Sbellard if (nb_sectors > 0) { 26728d3b1a2dSKevin Wolf QEMUIOVector qiov_inplace; 26738d3b1a2dSKevin Wolf 26748d3b1a2dSKevin Wolf qemu_iovec_init(&qiov_inplace, qiov->niov); 26758d3b1a2dSKevin Wolf qemu_iovec_concat(&qiov_inplace, qiov, len, 26768d3b1a2dSKevin Wolf nb_sectors << BDRV_SECTOR_BITS); 26778d3b1a2dSKevin Wolf ret = bdrv_writev(bs, sector_num, &qiov_inplace); 26788d3b1a2dSKevin Wolf qemu_iovec_destroy(&qiov_inplace); 26798d3b1a2dSKevin Wolf if (ret < 0) { 26809a8c4cceSKevin Wolf return ret; 26818d3b1a2dSKevin Wolf } 26828d3b1a2dSKevin Wolf 268383f64091Sbellard sector_num += nb_sectors; 26846ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 268583f64091Sbellard count -= len; 268683f64091Sbellard } 268783f64091Sbellard 268883f64091Sbellard /* add data from the last sector */ 268983f64091Sbellard if (count > 0) { 26909a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 26919a8c4cceSKevin Wolf return ret; 26928d3b1a2dSKevin Wolf qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count); 26939a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 26949a8c4cceSKevin Wolf return ret; 269583f64091Sbellard } 26968d3b1a2dSKevin Wolf return qiov->size; 26978d3b1a2dSKevin Wolf } 26988d3b1a2dSKevin Wolf 26998d3b1a2dSKevin Wolf int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 27008d3b1a2dSKevin Wolf const void *buf, int count1) 27018d3b1a2dSKevin Wolf { 27028d3b1a2dSKevin Wolf QEMUIOVector qiov; 27038d3b1a2dSKevin Wolf struct iovec iov = { 27048d3b1a2dSKevin Wolf .iov_base = (void *) buf, 27058d3b1a2dSKevin Wolf .iov_len = count1, 27068d3b1a2dSKevin Wolf }; 27078d3b1a2dSKevin Wolf 27088d3b1a2dSKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 27098d3b1a2dSKevin Wolf return bdrv_pwritev(bs, offset, &qiov); 271083f64091Sbellard } 271183f64091Sbellard 2712f08145feSKevin Wolf /* 2713f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 2714f08145feSKevin Wolf * request (acts as a barrier) 2715f08145feSKevin Wolf * 2716f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 2717f08145feSKevin Wolf */ 2718f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 2719f08145feSKevin Wolf const void *buf, int count) 2720f08145feSKevin Wolf { 2721f08145feSKevin Wolf int ret; 2722f08145feSKevin Wolf 2723f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 2724f08145feSKevin Wolf if (ret < 0) { 2725f08145feSKevin Wolf return ret; 2726f08145feSKevin Wolf } 2727f08145feSKevin Wolf 2728f05fa4adSPaolo Bonzini /* No flush needed for cache modes that already do it */ 2729f05fa4adSPaolo Bonzini if (bs->enable_write_cache) { 2730f08145feSKevin Wolf bdrv_flush(bs); 2731f08145feSKevin Wolf } 2732f08145feSKevin Wolf 2733f08145feSKevin Wolf return 0; 2734f08145feSKevin Wolf } 2735f08145feSKevin Wolf 2736470c0504SStefan Hajnoczi static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs, 2737ab185921SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2738ab185921SStefan Hajnoczi { 2739ab185921SStefan Hajnoczi /* Perform I/O through a temporary buffer so that users who scribble over 2740ab185921SStefan Hajnoczi * their read buffer while the operation is in progress do not end up 2741ab185921SStefan Hajnoczi * modifying the image file. This is critical for zero-copy guest I/O 2742ab185921SStefan Hajnoczi * where anything might happen inside guest memory. 2743ab185921SStefan Hajnoczi */ 2744ab185921SStefan Hajnoczi void *bounce_buffer; 2745ab185921SStefan Hajnoczi 274679c053bdSStefan Hajnoczi BlockDriver *drv = bs->drv; 2747ab185921SStefan Hajnoczi struct iovec iov; 2748ab185921SStefan Hajnoczi QEMUIOVector bounce_qiov; 2749ab185921SStefan Hajnoczi int64_t cluster_sector_num; 2750ab185921SStefan Hajnoczi int cluster_nb_sectors; 2751ab185921SStefan Hajnoczi size_t skip_bytes; 2752ab185921SStefan Hajnoczi int ret; 2753ab185921SStefan Hajnoczi 2754ab185921SStefan Hajnoczi /* Cover entire cluster so no additional backing file I/O is required when 2755ab185921SStefan Hajnoczi * allocating cluster in the image file. 2756ab185921SStefan Hajnoczi */ 2757343bded4SPaolo Bonzini bdrv_round_to_clusters(bs, sector_num, nb_sectors, 2758ab185921SStefan Hajnoczi &cluster_sector_num, &cluster_nb_sectors); 2759ab185921SStefan Hajnoczi 2760470c0504SStefan Hajnoczi trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, 2761ab185921SStefan Hajnoczi cluster_sector_num, cluster_nb_sectors); 2762ab185921SStefan Hajnoczi 2763ab185921SStefan Hajnoczi iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE; 2764ab185921SStefan Hajnoczi iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len); 2765ab185921SStefan Hajnoczi qemu_iovec_init_external(&bounce_qiov, &iov, 1); 2766ab185921SStefan Hajnoczi 276779c053bdSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors, 2768ab185921SStefan Hajnoczi &bounce_qiov); 2769ab185921SStefan Hajnoczi if (ret < 0) { 2770ab185921SStefan Hajnoczi goto err; 2771ab185921SStefan Hajnoczi } 2772ab185921SStefan Hajnoczi 277379c053bdSStefan Hajnoczi if (drv->bdrv_co_write_zeroes && 277479c053bdSStefan Hajnoczi buffer_is_zero(bounce_buffer, iov.iov_len)) { 2775621f0589SKevin Wolf ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num, 2776aa7bfbffSPeter Lieven cluster_nb_sectors, 0); 277779c053bdSStefan Hajnoczi } else { 2778f05fa4adSPaolo Bonzini /* This does not change the data on the disk, it is not necessary 2779f05fa4adSPaolo Bonzini * to flush even in cache=writethrough mode. 2780f05fa4adSPaolo Bonzini */ 278179c053bdSStefan Hajnoczi ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors, 2782ab185921SStefan Hajnoczi &bounce_qiov); 278379c053bdSStefan Hajnoczi } 278479c053bdSStefan Hajnoczi 2785ab185921SStefan Hajnoczi if (ret < 0) { 2786ab185921SStefan Hajnoczi /* It might be okay to ignore write errors for guest requests. If this 2787ab185921SStefan Hajnoczi * is a deliberate copy-on-read then we don't want to ignore the error. 2788ab185921SStefan Hajnoczi * Simply report it in all cases. 2789ab185921SStefan Hajnoczi */ 2790ab185921SStefan Hajnoczi goto err; 2791ab185921SStefan Hajnoczi } 2792ab185921SStefan Hajnoczi 2793ab185921SStefan Hajnoczi skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE; 279403396148SMichael Tokarev qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes, 2795ab185921SStefan Hajnoczi nb_sectors * BDRV_SECTOR_SIZE); 2796ab185921SStefan Hajnoczi 2797ab185921SStefan Hajnoczi err: 2798ab185921SStefan Hajnoczi qemu_vfree(bounce_buffer); 2799ab185921SStefan Hajnoczi return ret; 2800ab185921SStefan Hajnoczi } 2801ab185921SStefan Hajnoczi 2802c5fbe571SStefan Hajnoczi /* 2803c5fbe571SStefan Hajnoczi * Handle a read request in coroutine context 2804c5fbe571SStefan Hajnoczi */ 2805c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs, 2806470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2807470c0504SStefan Hajnoczi BdrvRequestFlags flags) 2808da1fa91dSKevin Wolf { 2809da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 2810dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 2811dbffbdcfSStefan Hajnoczi int ret; 2812da1fa91dSKevin Wolf 2813da1fa91dSKevin Wolf if (!drv) { 2814da1fa91dSKevin Wolf return -ENOMEDIUM; 2815da1fa91dSKevin Wolf } 2816da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 2817da1fa91dSKevin Wolf return -EIO; 2818da1fa91dSKevin Wolf } 2819da1fa91dSKevin Wolf 2820f4658285SStefan Hajnoczi if (bs->copy_on_read) { 2821470c0504SStefan Hajnoczi flags |= BDRV_REQ_COPY_ON_READ; 2822470c0504SStefan Hajnoczi } 2823470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2824470c0504SStefan Hajnoczi bs->copy_on_read_in_flight++; 2825470c0504SStefan Hajnoczi } 2826470c0504SStefan Hajnoczi 2827470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 2828f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 2829f4658285SStefan Hajnoczi } 2830f4658285SStefan Hajnoczi 2831cc0681c4SBenoît Canet /* throttling disk I/O */ 2832cc0681c4SBenoît Canet if (bs->io_limits_enabled) { 2833cc0681c4SBenoît Canet bdrv_io_limits_intercept(bs, nb_sectors, false); 2834cc0681c4SBenoît Canet } 2835cc0681c4SBenoît Canet 2836dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, false); 2837ab185921SStefan Hajnoczi 2838470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2839ab185921SStefan Hajnoczi int pnum; 2840ab185921SStefan Hajnoczi 2841bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(bs, sector_num, nb_sectors, &pnum); 2842ab185921SStefan Hajnoczi if (ret < 0) { 2843ab185921SStefan Hajnoczi goto out; 2844ab185921SStefan Hajnoczi } 2845ab185921SStefan Hajnoczi 2846ab185921SStefan Hajnoczi if (!ret || pnum != nb_sectors) { 2847470c0504SStefan Hajnoczi ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov); 2848ab185921SStefan Hajnoczi goto out; 2849ab185921SStefan Hajnoczi } 2850ab185921SStefan Hajnoczi } 2851ab185921SStefan Hajnoczi 2852893a8f62SMORITA Kazutaka if (!(bs->zero_beyond_eof && bs->growable)) { 2853dbffbdcfSStefan Hajnoczi ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 2854893a8f62SMORITA Kazutaka } else { 2855893a8f62SMORITA Kazutaka /* Read zeros after EOF of growable BDSes */ 2856893a8f62SMORITA Kazutaka int64_t len, total_sectors, max_nb_sectors; 2857893a8f62SMORITA Kazutaka 2858893a8f62SMORITA Kazutaka len = bdrv_getlength(bs); 2859893a8f62SMORITA Kazutaka if (len < 0) { 2860893a8f62SMORITA Kazutaka ret = len; 2861893a8f62SMORITA Kazutaka goto out; 2862893a8f62SMORITA Kazutaka } 2863893a8f62SMORITA Kazutaka 2864d055a1feSFam Zheng total_sectors = DIV_ROUND_UP(len, BDRV_SECTOR_SIZE); 2865893a8f62SMORITA Kazutaka max_nb_sectors = MAX(0, total_sectors - sector_num); 2866893a8f62SMORITA Kazutaka if (max_nb_sectors > 0) { 2867893a8f62SMORITA Kazutaka ret = drv->bdrv_co_readv(bs, sector_num, 2868893a8f62SMORITA Kazutaka MIN(nb_sectors, max_nb_sectors), qiov); 2869893a8f62SMORITA Kazutaka } else { 2870893a8f62SMORITA Kazutaka ret = 0; 2871893a8f62SMORITA Kazutaka } 2872893a8f62SMORITA Kazutaka 2873893a8f62SMORITA Kazutaka /* Reading beyond end of file is supposed to produce zeroes */ 2874893a8f62SMORITA Kazutaka if (ret == 0 && total_sectors < sector_num + nb_sectors) { 2875893a8f62SMORITA Kazutaka uint64_t offset = MAX(0, total_sectors - sector_num); 2876893a8f62SMORITA Kazutaka uint64_t bytes = (sector_num + nb_sectors - offset) * 2877893a8f62SMORITA Kazutaka BDRV_SECTOR_SIZE; 2878893a8f62SMORITA Kazutaka qemu_iovec_memset(qiov, offset * BDRV_SECTOR_SIZE, 0, bytes); 2879893a8f62SMORITA Kazutaka } 2880893a8f62SMORITA Kazutaka } 2881ab185921SStefan Hajnoczi 2882ab185921SStefan Hajnoczi out: 2883dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 2884470c0504SStefan Hajnoczi 2885470c0504SStefan Hajnoczi if (flags & BDRV_REQ_COPY_ON_READ) { 2886470c0504SStefan Hajnoczi bs->copy_on_read_in_flight--; 2887470c0504SStefan Hajnoczi } 2888470c0504SStefan Hajnoczi 2889dbffbdcfSStefan Hajnoczi return ret; 2890da1fa91dSKevin Wolf } 2891da1fa91dSKevin Wolf 2892c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 2893da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 2894da1fa91dSKevin Wolf { 2895c5fbe571SStefan Hajnoczi trace_bdrv_co_readv(bs, sector_num, nb_sectors); 2896da1fa91dSKevin Wolf 2897470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0); 2898470c0504SStefan Hajnoczi } 2899470c0504SStefan Hajnoczi 2900470c0504SStefan Hajnoczi int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs, 2901470c0504SStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov) 2902470c0504SStefan Hajnoczi { 2903470c0504SStefan Hajnoczi trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors); 2904470c0504SStefan Hajnoczi 2905470c0504SStefan Hajnoczi return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 2906470c0504SStefan Hajnoczi BDRV_REQ_COPY_ON_READ); 2907c5fbe571SStefan Hajnoczi } 2908c5fbe571SStefan Hajnoczi 2909c31cb707SPeter Lieven /* if no limit is specified in the BlockLimits use a default 2910c31cb707SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 2911c31cb707SPeter Lieven */ 2912c31cb707SPeter Lieven #define MAX_WRITE_ZEROES_DEFAULT 32768 2913c31cb707SPeter Lieven 2914f08f2ddaSStefan Hajnoczi static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs, 2915aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, BdrvRequestFlags flags) 2916f08f2ddaSStefan Hajnoczi { 2917f08f2ddaSStefan Hajnoczi BlockDriver *drv = bs->drv; 2918f08f2ddaSStefan Hajnoczi QEMUIOVector qiov; 2919c31cb707SPeter Lieven struct iovec iov = {0}; 2920c31cb707SPeter Lieven int ret = 0; 2921f08f2ddaSStefan Hajnoczi 2922c31cb707SPeter Lieven int max_write_zeroes = bs->bl.max_write_zeroes ? 2923c31cb707SPeter Lieven bs->bl.max_write_zeroes : MAX_WRITE_ZEROES_DEFAULT; 2924621f0589SKevin Wolf 2925c31cb707SPeter Lieven while (nb_sectors > 0 && !ret) { 2926c31cb707SPeter Lieven int num = nb_sectors; 2927c31cb707SPeter Lieven 2928b8d71c09SPaolo Bonzini /* Align request. Block drivers can expect the "bulk" of the request 2929b8d71c09SPaolo Bonzini * to be aligned. 2930b8d71c09SPaolo Bonzini */ 2931b8d71c09SPaolo Bonzini if (bs->bl.write_zeroes_alignment 2932b8d71c09SPaolo Bonzini && num > bs->bl.write_zeroes_alignment) { 2933b8d71c09SPaolo Bonzini if (sector_num % bs->bl.write_zeroes_alignment != 0) { 2934b8d71c09SPaolo Bonzini /* Make a small request up to the first aligned sector. */ 2935c31cb707SPeter Lieven num = bs->bl.write_zeroes_alignment; 2936c31cb707SPeter Lieven num -= sector_num % bs->bl.write_zeroes_alignment; 2937b8d71c09SPaolo Bonzini } else if ((sector_num + num) % bs->bl.write_zeroes_alignment != 0) { 2938b8d71c09SPaolo Bonzini /* Shorten the request to the last aligned sector. num cannot 2939b8d71c09SPaolo Bonzini * underflow because num > bs->bl.write_zeroes_alignment. 2940b8d71c09SPaolo Bonzini */ 2941b8d71c09SPaolo Bonzini num -= (sector_num + num) % bs->bl.write_zeroes_alignment; 2942b8d71c09SPaolo Bonzini } 2943c31cb707SPeter Lieven } 2944c31cb707SPeter Lieven 2945c31cb707SPeter Lieven /* limit request size */ 2946c31cb707SPeter Lieven if (num > max_write_zeroes) { 2947c31cb707SPeter Lieven num = max_write_zeroes; 2948c31cb707SPeter Lieven } 2949c31cb707SPeter Lieven 2950c31cb707SPeter Lieven ret = -ENOTSUP; 2951f08f2ddaSStefan Hajnoczi /* First try the efficient write zeroes operation */ 2952f08f2ddaSStefan Hajnoczi if (drv->bdrv_co_write_zeroes) { 2953c31cb707SPeter Lieven ret = drv->bdrv_co_write_zeroes(bs, sector_num, num, flags); 2954f08f2ddaSStefan Hajnoczi } 2955f08f2ddaSStefan Hajnoczi 2956c31cb707SPeter Lieven if (ret == -ENOTSUP) { 2957f08f2ddaSStefan Hajnoczi /* Fall back to bounce buffer if write zeroes is unsupported */ 2958c31cb707SPeter Lieven iov.iov_len = num * BDRV_SECTOR_SIZE; 2959c31cb707SPeter Lieven if (iov.iov_base == NULL) { 2960b8d71c09SPaolo Bonzini iov.iov_base = qemu_blockalign(bs, num * BDRV_SECTOR_SIZE); 2961b8d71c09SPaolo Bonzini memset(iov.iov_base, 0, num * BDRV_SECTOR_SIZE); 2962c31cb707SPeter Lieven } 2963f08f2ddaSStefan Hajnoczi qemu_iovec_init_external(&qiov, &iov, 1); 2964f08f2ddaSStefan Hajnoczi 2965c31cb707SPeter Lieven ret = drv->bdrv_co_writev(bs, sector_num, num, &qiov); 2966b8d71c09SPaolo Bonzini 2967b8d71c09SPaolo Bonzini /* Keep bounce buffer around if it is big enough for all 2968b8d71c09SPaolo Bonzini * all future requests. 2969b8d71c09SPaolo Bonzini */ 2970b8d71c09SPaolo Bonzini if (num < max_write_zeroes) { 2971b8d71c09SPaolo Bonzini qemu_vfree(iov.iov_base); 2972b8d71c09SPaolo Bonzini iov.iov_base = NULL; 2973b8d71c09SPaolo Bonzini } 2974c31cb707SPeter Lieven } 2975c31cb707SPeter Lieven 2976c31cb707SPeter Lieven sector_num += num; 2977c31cb707SPeter Lieven nb_sectors -= num; 2978c31cb707SPeter Lieven } 2979f08f2ddaSStefan Hajnoczi 2980f08f2ddaSStefan Hajnoczi qemu_vfree(iov.iov_base); 2981f08f2ddaSStefan Hajnoczi return ret; 2982f08f2ddaSStefan Hajnoczi } 2983f08f2ddaSStefan Hajnoczi 2984c5fbe571SStefan Hajnoczi /* 2985c5fbe571SStefan Hajnoczi * Handle a write request in coroutine context 2986c5fbe571SStefan Hajnoczi */ 2987c5fbe571SStefan Hajnoczi static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs, 2988f08f2ddaSStefan Hajnoczi int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, 2989f08f2ddaSStefan Hajnoczi BdrvRequestFlags flags) 2990c5fbe571SStefan Hajnoczi { 2991c5fbe571SStefan Hajnoczi BlockDriver *drv = bs->drv; 2992dbffbdcfSStefan Hajnoczi BdrvTrackedRequest req; 29936b7cb247SStefan Hajnoczi int ret; 2994da1fa91dSKevin Wolf 2995da1fa91dSKevin Wolf if (!bs->drv) { 2996da1fa91dSKevin Wolf return -ENOMEDIUM; 2997da1fa91dSKevin Wolf } 2998da1fa91dSKevin Wolf if (bs->read_only) { 2999da1fa91dSKevin Wolf return -EACCES; 3000da1fa91dSKevin Wolf } 3001da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 3002da1fa91dSKevin Wolf return -EIO; 3003da1fa91dSKevin Wolf } 3004da1fa91dSKevin Wolf 3005470c0504SStefan Hajnoczi if (bs->copy_on_read_in_flight) { 3006f4658285SStefan Hajnoczi wait_for_overlapping_requests(bs, sector_num, nb_sectors); 3007f4658285SStefan Hajnoczi } 3008f4658285SStefan Hajnoczi 3009cc0681c4SBenoît Canet /* throttling disk I/O */ 3010cc0681c4SBenoît Canet if (bs->io_limits_enabled) { 3011cc0681c4SBenoît Canet bdrv_io_limits_intercept(bs, nb_sectors, true); 3012cc0681c4SBenoît Canet } 3013cc0681c4SBenoît Canet 3014dbffbdcfSStefan Hajnoczi tracked_request_begin(&req, bs, sector_num, nb_sectors, true); 3015dbffbdcfSStefan Hajnoczi 3016d616b224SStefan Hajnoczi ret = notifier_with_return_list_notify(&bs->before_write_notifiers, &req); 3017d616b224SStefan Hajnoczi 3018d616b224SStefan Hajnoczi if (ret < 0) { 3019d616b224SStefan Hajnoczi /* Do nothing, write notifier decided to fail this request */ 3020d616b224SStefan Hajnoczi } else if (flags & BDRV_REQ_ZERO_WRITE) { 3021aa7bfbffSPeter Lieven ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors, flags); 3022f08f2ddaSStefan Hajnoczi } else { 30236b7cb247SStefan Hajnoczi ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 3024f08f2ddaSStefan Hajnoczi } 30256b7cb247SStefan Hajnoczi 3026f05fa4adSPaolo Bonzini if (ret == 0 && !bs->enable_write_cache) { 3027f05fa4adSPaolo Bonzini ret = bdrv_co_flush(bs); 3028f05fa4adSPaolo Bonzini } 3029f05fa4adSPaolo Bonzini 30301755da16SPaolo Bonzini bdrv_set_dirty(bs, sector_num, nb_sectors); 3031da1fa91dSKevin Wolf 3032da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 3033da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 3034da1fa91dSKevin Wolf } 3035df2a6f29SPaolo Bonzini if (bs->growable && ret >= 0) { 3036df2a6f29SPaolo Bonzini bs->total_sectors = MAX(bs->total_sectors, sector_num + nb_sectors); 3037df2a6f29SPaolo Bonzini } 3038da1fa91dSKevin Wolf 3039dbffbdcfSStefan Hajnoczi tracked_request_end(&req); 3040dbffbdcfSStefan Hajnoczi 30416b7cb247SStefan Hajnoczi return ret; 3042da1fa91dSKevin Wolf } 3043da1fa91dSKevin Wolf 3044c5fbe571SStefan Hajnoczi int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 3045c5fbe571SStefan Hajnoczi int nb_sectors, QEMUIOVector *qiov) 3046c5fbe571SStefan Hajnoczi { 3047c5fbe571SStefan Hajnoczi trace_bdrv_co_writev(bs, sector_num, nb_sectors); 3048c5fbe571SStefan Hajnoczi 3049f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0); 3050f08f2ddaSStefan Hajnoczi } 3051f08f2ddaSStefan Hajnoczi 3052f08f2ddaSStefan Hajnoczi int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs, 3053aa7bfbffSPeter Lieven int64_t sector_num, int nb_sectors, 3054aa7bfbffSPeter Lieven BdrvRequestFlags flags) 3055f08f2ddaSStefan Hajnoczi { 305694d6ff21SPaolo Bonzini trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors, flags); 3057f08f2ddaSStefan Hajnoczi 3058d32f35cbSPeter Lieven if (!(bs->open_flags & BDRV_O_UNMAP)) { 3059d32f35cbSPeter Lieven flags &= ~BDRV_REQ_MAY_UNMAP; 3060d32f35cbSPeter Lieven } 3061d32f35cbSPeter Lieven 3062f08f2ddaSStefan Hajnoczi return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL, 3063aa7bfbffSPeter Lieven BDRV_REQ_ZERO_WRITE | flags); 3064c5fbe571SStefan Hajnoczi } 3065c5fbe571SStefan Hajnoczi 306683f64091Sbellard /** 306783f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 306883f64091Sbellard */ 306983f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 307083f64091Sbellard { 307183f64091Sbellard BlockDriver *drv = bs->drv; 307251762288SStefan Hajnoczi int ret; 307383f64091Sbellard if (!drv) 307419cb3738Sbellard return -ENOMEDIUM; 307583f64091Sbellard if (!drv->bdrv_truncate) 307683f64091Sbellard return -ENOTSUP; 307759f2689dSNaphtali Sprei if (bs->read_only) 307859f2689dSNaphtali Sprei return -EACCES; 30798591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 30808591675fSMarcelo Tosatti return -EBUSY; 308151762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 308251762288SStefan Hajnoczi if (ret == 0) { 308351762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 3084145feb17SMarkus Armbruster bdrv_dev_resize_cb(bs); 308551762288SStefan Hajnoczi } 308651762288SStefan Hajnoczi return ret; 308783f64091Sbellard } 308883f64091Sbellard 308983f64091Sbellard /** 30904a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 30914a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 30924a1d5e1fSFam Zheng */ 30934a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 30944a1d5e1fSFam Zheng { 30954a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 30964a1d5e1fSFam Zheng if (!drv) { 30974a1d5e1fSFam Zheng return -ENOMEDIUM; 30984a1d5e1fSFam Zheng } 30994a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 31004a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 31014a1d5e1fSFam Zheng } 31024a1d5e1fSFam Zheng if (bs->file) { 31034a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 31044a1d5e1fSFam Zheng } 31054a1d5e1fSFam Zheng return -ENOTSUP; 31064a1d5e1fSFam Zheng } 31074a1d5e1fSFam Zheng 31084a1d5e1fSFam Zheng /** 310983f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 311083f64091Sbellard */ 311183f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 311283f64091Sbellard { 311383f64091Sbellard BlockDriver *drv = bs->drv; 311483f64091Sbellard if (!drv) 311519cb3738Sbellard return -ENOMEDIUM; 311651762288SStefan Hajnoczi 3117b94a2610SKevin Wolf if (drv->has_variable_length) { 3118b94a2610SKevin Wolf int ret = refresh_total_sectors(bs, bs->total_sectors); 3119b94a2610SKevin Wolf if (ret < 0) { 3120b94a2610SKevin Wolf return ret; 3121fc01f7e7Sbellard } 312246a4e4e6SStefan Hajnoczi } 312346a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 312446a4e4e6SStefan Hajnoczi } 3125fc01f7e7Sbellard 312619cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 312796b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 3128fc01f7e7Sbellard { 312919cb3738Sbellard int64_t length; 313019cb3738Sbellard length = bdrv_getlength(bs); 313119cb3738Sbellard if (length < 0) 313219cb3738Sbellard length = 0; 313319cb3738Sbellard else 31346ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 313519cb3738Sbellard *nb_sectors_ptr = length; 3136fc01f7e7Sbellard } 3137cf98951bSbellard 3138ff06f5f3SPaolo Bonzini void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error, 3139ff06f5f3SPaolo Bonzini BlockdevOnError on_write_error) 3140abd7f68dSMarkus Armbruster { 3141abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 3142abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 3143abd7f68dSMarkus Armbruster } 3144abd7f68dSMarkus Armbruster 31451ceee0d5SPaolo Bonzini BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read) 3146abd7f68dSMarkus Armbruster { 3147abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 3148abd7f68dSMarkus Armbruster } 3149abd7f68dSMarkus Armbruster 31503e1caa5fSPaolo Bonzini BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error) 31513e1caa5fSPaolo Bonzini { 31523e1caa5fSPaolo Bonzini BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error; 31533e1caa5fSPaolo Bonzini 31543e1caa5fSPaolo Bonzini switch (on_err) { 31553e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_ENOSPC: 31563e1caa5fSPaolo Bonzini return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT; 31573e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_STOP: 31583e1caa5fSPaolo Bonzini return BDRV_ACTION_STOP; 31593e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_REPORT: 31603e1caa5fSPaolo Bonzini return BDRV_ACTION_REPORT; 31613e1caa5fSPaolo Bonzini case BLOCKDEV_ON_ERROR_IGNORE: 31623e1caa5fSPaolo Bonzini return BDRV_ACTION_IGNORE; 31633e1caa5fSPaolo Bonzini default: 31643e1caa5fSPaolo Bonzini abort(); 31653e1caa5fSPaolo Bonzini } 31663e1caa5fSPaolo Bonzini } 31673e1caa5fSPaolo Bonzini 31683e1caa5fSPaolo Bonzini /* This is done by device models because, while the block layer knows 31693e1caa5fSPaolo Bonzini * about the error, it does not know whether an operation comes from 31703e1caa5fSPaolo Bonzini * the device or the block layer (from a job, for example). 31713e1caa5fSPaolo Bonzini */ 31723e1caa5fSPaolo Bonzini void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action, 31733e1caa5fSPaolo Bonzini bool is_read, int error) 31743e1caa5fSPaolo Bonzini { 31753e1caa5fSPaolo Bonzini assert(error >= 0); 317632c81a4aSPaolo Bonzini bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read); 31773e1caa5fSPaolo Bonzini if (action == BDRV_ACTION_STOP) { 31783e1caa5fSPaolo Bonzini vm_stop(RUN_STATE_IO_ERROR); 31793e1caa5fSPaolo Bonzini bdrv_iostatus_set_err(bs, error); 31803e1caa5fSPaolo Bonzini } 31813e1caa5fSPaolo Bonzini } 31823e1caa5fSPaolo Bonzini 3183b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 3184b338082bSbellard { 3185b338082bSbellard return bs->read_only; 3186b338082bSbellard } 3187b338082bSbellard 3188985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 3189985a03b0Sths { 3190985a03b0Sths return bs->sg; 3191985a03b0Sths } 3192985a03b0Sths 3193e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 3194e900a7b7SChristoph Hellwig { 3195e900a7b7SChristoph Hellwig return bs->enable_write_cache; 3196e900a7b7SChristoph Hellwig } 3197e900a7b7SChristoph Hellwig 3198425b0148SPaolo Bonzini void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce) 3199425b0148SPaolo Bonzini { 3200425b0148SPaolo Bonzini bs->enable_write_cache = wce; 320155b110f2SJeff Cody 320255b110f2SJeff Cody /* so a reopen() will preserve wce */ 320355b110f2SJeff Cody if (wce) { 320455b110f2SJeff Cody bs->open_flags |= BDRV_O_CACHE_WB; 320555b110f2SJeff Cody } else { 320655b110f2SJeff Cody bs->open_flags &= ~BDRV_O_CACHE_WB; 320755b110f2SJeff Cody } 3208425b0148SPaolo Bonzini } 3209425b0148SPaolo Bonzini 3210ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 3211ea2384d3Sbellard { 3212ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 3213ea2384d3Sbellard return 1; 3214ea2384d3Sbellard return bs->encrypted; 3215ea2384d3Sbellard } 3216ea2384d3Sbellard 3217c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 3218c0f4ce77Saliguori { 3219c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 3220c0f4ce77Saliguori 3221c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 3222c0f4ce77Saliguori return 1; 3223c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 3224c0f4ce77Saliguori } 3225c0f4ce77Saliguori 3226ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 3227ea2384d3Sbellard { 3228ea2384d3Sbellard int ret; 3229ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 3230ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 3231ea2384d3Sbellard if (ret < 0) 3232ea2384d3Sbellard return ret; 3233ea2384d3Sbellard if (!bs->encrypted) 3234ea2384d3Sbellard return 0; 3235ea2384d3Sbellard } 3236fd04a2aeSShahar Havivi if (!bs->encrypted) { 3237fd04a2aeSShahar Havivi return -EINVAL; 3238fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 3239fd04a2aeSShahar Havivi return -ENOMEDIUM; 3240fd04a2aeSShahar Havivi } 3241c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 3242bb5fc20fSaliguori if (ret < 0) { 3243bb5fc20fSaliguori bs->valid_key = 0; 3244bb5fc20fSaliguori } else if (!bs->valid_key) { 3245bb5fc20fSaliguori bs->valid_key = 1; 3246bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 32477d4b4ba5SMarkus Armbruster bdrv_dev_change_media_cb(bs, true); 3248bb5fc20fSaliguori } 3249c0f4ce77Saliguori return ret; 3250ea2384d3Sbellard } 3251ea2384d3Sbellard 3252f8d6bba1SMarkus Armbruster const char *bdrv_get_format_name(BlockDriverState *bs) 3253ea2384d3Sbellard { 3254f8d6bba1SMarkus Armbruster return bs->drv ? bs->drv->format_name : NULL; 3255ea2384d3Sbellard } 3256ea2384d3Sbellard 3257ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 3258ea2384d3Sbellard void *opaque) 3259ea2384d3Sbellard { 3260ea2384d3Sbellard BlockDriver *drv; 3261ea2384d3Sbellard 32628a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 3263ea2384d3Sbellard it(opaque, drv->format_name); 3264ea2384d3Sbellard } 3265ea2384d3Sbellard } 3266ea2384d3Sbellard 3267dc364f4cSBenoît Canet /* This function is to find block backend bs */ 3268b338082bSbellard BlockDriverState *bdrv_find(const char *name) 3269b338082bSbellard { 3270b338082bSbellard BlockDriverState *bs; 3271b338082bSbellard 3272dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 32731b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 3274b338082bSbellard return bs; 3275b338082bSbellard } 32761b7bdbc1SStefan Hajnoczi } 3277b338082bSbellard return NULL; 3278b338082bSbellard } 3279b338082bSbellard 3280dc364f4cSBenoît Canet /* This function is to find a node in the bs graph */ 3281dc364f4cSBenoît Canet BlockDriverState *bdrv_find_node(const char *node_name) 3282dc364f4cSBenoît Canet { 3283dc364f4cSBenoît Canet BlockDriverState *bs; 3284dc364f4cSBenoît Canet 3285dc364f4cSBenoît Canet assert(node_name); 3286dc364f4cSBenoît Canet 3287dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 3288dc364f4cSBenoît Canet if (!strcmp(node_name, bs->node_name)) { 3289dc364f4cSBenoît Canet return bs; 3290dc364f4cSBenoît Canet } 3291dc364f4cSBenoît Canet } 3292dc364f4cSBenoît Canet return NULL; 3293dc364f4cSBenoît Canet } 3294dc364f4cSBenoît Canet 3295c13163fbSBenoît Canet /* Put this QMP function here so it can access the static graph_bdrv_states. */ 3296c13163fbSBenoît Canet BlockDeviceInfoList *bdrv_named_nodes_list(void) 3297c13163fbSBenoît Canet { 3298c13163fbSBenoît Canet BlockDeviceInfoList *list, *entry; 3299c13163fbSBenoît Canet BlockDriverState *bs; 3300c13163fbSBenoît Canet 3301c13163fbSBenoît Canet list = NULL; 3302c13163fbSBenoît Canet QTAILQ_FOREACH(bs, &graph_bdrv_states, node_list) { 3303c13163fbSBenoît Canet entry = g_malloc0(sizeof(*entry)); 3304c13163fbSBenoît Canet entry->value = bdrv_block_device_info(bs); 3305c13163fbSBenoît Canet entry->next = list; 3306c13163fbSBenoît Canet list = entry; 3307c13163fbSBenoît Canet } 3308c13163fbSBenoît Canet 3309c13163fbSBenoît Canet return list; 3310c13163fbSBenoît Canet } 3311c13163fbSBenoît Canet 3312*12d3ba82SBenoît Canet BlockDriverState *bdrv_lookup_bs(const char *device, 3313*12d3ba82SBenoît Canet const char *node_name, 3314*12d3ba82SBenoît Canet Error **errp) 3315*12d3ba82SBenoît Canet { 3316*12d3ba82SBenoît Canet BlockDriverState *bs = NULL; 3317*12d3ba82SBenoît Canet 3318*12d3ba82SBenoît Canet if ((!device && !node_name) || (device && node_name)) { 3319*12d3ba82SBenoît Canet error_setg(errp, "Use either device or node-name but not both"); 3320*12d3ba82SBenoît Canet return NULL; 3321*12d3ba82SBenoît Canet } 3322*12d3ba82SBenoît Canet 3323*12d3ba82SBenoît Canet if (device) { 3324*12d3ba82SBenoît Canet bs = bdrv_find(device); 3325*12d3ba82SBenoît Canet 3326*12d3ba82SBenoît Canet if (!bs) { 3327*12d3ba82SBenoît Canet error_set(errp, QERR_DEVICE_NOT_FOUND, device); 3328*12d3ba82SBenoît Canet return NULL; 3329*12d3ba82SBenoît Canet } 3330*12d3ba82SBenoît Canet 3331*12d3ba82SBenoît Canet return bs; 3332*12d3ba82SBenoît Canet } 3333*12d3ba82SBenoît Canet 3334*12d3ba82SBenoît Canet bs = bdrv_find_node(node_name); 3335*12d3ba82SBenoît Canet 3336*12d3ba82SBenoît Canet if (!bs) { 3337*12d3ba82SBenoît Canet error_set(errp, QERR_DEVICE_NOT_FOUND, node_name); 3338*12d3ba82SBenoît Canet return NULL; 3339*12d3ba82SBenoît Canet } 3340*12d3ba82SBenoît Canet 3341*12d3ba82SBenoît Canet return bs; 3342*12d3ba82SBenoît Canet } 3343*12d3ba82SBenoît Canet 33442f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 33452f399b0aSMarkus Armbruster { 33462f399b0aSMarkus Armbruster if (!bs) { 33472f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 33482f399b0aSMarkus Armbruster } 3349dc364f4cSBenoît Canet return QTAILQ_NEXT(bs, device_list); 33502f399b0aSMarkus Armbruster } 33512f399b0aSMarkus Armbruster 335251de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 335381d0912dSbellard { 335481d0912dSbellard BlockDriverState *bs; 335581d0912dSbellard 3356dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 335751de9760Saliguori it(opaque, bs); 335881d0912dSbellard } 335981d0912dSbellard } 336081d0912dSbellard 3361ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 3362ea2384d3Sbellard { 3363ea2384d3Sbellard return bs->device_name; 3364ea2384d3Sbellard } 3365ea2384d3Sbellard 3366c8433287SMarkus Armbruster int bdrv_get_flags(BlockDriverState *bs) 3367c8433287SMarkus Armbruster { 3368c8433287SMarkus Armbruster return bs->open_flags; 3369c8433287SMarkus Armbruster } 3370c8433287SMarkus Armbruster 3371f0f0fdfeSKevin Wolf int bdrv_flush_all(void) 3372c6ca28d6Saliguori { 3373c6ca28d6Saliguori BlockDriverState *bs; 3374f0f0fdfeSKevin Wolf int result = 0; 3375c6ca28d6Saliguori 3376dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 3377f0f0fdfeSKevin Wolf int ret = bdrv_flush(bs); 3378f0f0fdfeSKevin Wolf if (ret < 0 && !result) { 3379f0f0fdfeSKevin Wolf result = ret; 3380c6ca28d6Saliguori } 33811b7bdbc1SStefan Hajnoczi } 3382c6ca28d6Saliguori 3383f0f0fdfeSKevin Wolf return result; 3384f0f0fdfeSKevin Wolf } 3385f0f0fdfeSKevin Wolf 33863ac21627SPeter Lieven int bdrv_has_zero_init_1(BlockDriverState *bs) 33873ac21627SPeter Lieven { 33883ac21627SPeter Lieven return 1; 33893ac21627SPeter Lieven } 33903ac21627SPeter Lieven 3391f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 3392f2feebbdSKevin Wolf { 3393f2feebbdSKevin Wolf assert(bs->drv); 3394f2feebbdSKevin Wolf 339511212d8fSPaolo Bonzini /* If BS is a copy on write image, it is initialized to 339611212d8fSPaolo Bonzini the contents of the base image, which may not be zeroes. */ 339711212d8fSPaolo Bonzini if (bs->backing_hd) { 339811212d8fSPaolo Bonzini return 0; 339911212d8fSPaolo Bonzini } 3400336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 3401336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 3402f2feebbdSKevin Wolf } 3403f2feebbdSKevin Wolf 34043ac21627SPeter Lieven /* safe default */ 34053ac21627SPeter Lieven return 0; 3406f2feebbdSKevin Wolf } 3407f2feebbdSKevin Wolf 34084ce78691SPeter Lieven bool bdrv_unallocated_blocks_are_zero(BlockDriverState *bs) 34094ce78691SPeter Lieven { 34104ce78691SPeter Lieven BlockDriverInfo bdi; 34114ce78691SPeter Lieven 34124ce78691SPeter Lieven if (bs->backing_hd) { 34134ce78691SPeter Lieven return false; 34144ce78691SPeter Lieven } 34154ce78691SPeter Lieven 34164ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 34174ce78691SPeter Lieven return bdi.unallocated_blocks_are_zero; 34184ce78691SPeter Lieven } 34194ce78691SPeter Lieven 34204ce78691SPeter Lieven return false; 34214ce78691SPeter Lieven } 34224ce78691SPeter Lieven 34234ce78691SPeter Lieven bool bdrv_can_write_zeroes_with_unmap(BlockDriverState *bs) 34244ce78691SPeter Lieven { 34254ce78691SPeter Lieven BlockDriverInfo bdi; 34264ce78691SPeter Lieven 34274ce78691SPeter Lieven if (bs->backing_hd || !(bs->open_flags & BDRV_O_UNMAP)) { 34284ce78691SPeter Lieven return false; 34294ce78691SPeter Lieven } 34304ce78691SPeter Lieven 34314ce78691SPeter Lieven if (bdrv_get_info(bs, &bdi) == 0) { 34324ce78691SPeter Lieven return bdi.can_write_zeroes_with_unmap; 34334ce78691SPeter Lieven } 34344ce78691SPeter Lieven 34354ce78691SPeter Lieven return false; 34364ce78691SPeter Lieven } 34374ce78691SPeter Lieven 3438b6b8a333SPaolo Bonzini typedef struct BdrvCoGetBlockStatusData { 3439376ae3f1SStefan Hajnoczi BlockDriverState *bs; 3440b35b2bbaSMiroslav Rezanina BlockDriverState *base; 3441376ae3f1SStefan Hajnoczi int64_t sector_num; 3442376ae3f1SStefan Hajnoczi int nb_sectors; 3443376ae3f1SStefan Hajnoczi int *pnum; 3444b6b8a333SPaolo Bonzini int64_t ret; 3445376ae3f1SStefan Hajnoczi bool done; 3446b6b8a333SPaolo Bonzini } BdrvCoGetBlockStatusData; 3447376ae3f1SStefan Hajnoczi 3448f58c7b35Sths /* 3449f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 3450f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 3451f58c7b35Sths * hence all their sectors are reported as allocated. 3452f58c7b35Sths * 3453bd9533e3SStefan Hajnoczi * If 'sector_num' is beyond the end of the disk image the return value is 0 3454bd9533e3SStefan Hajnoczi * and 'pnum' is set to 0. 3455bd9533e3SStefan Hajnoczi * 3456f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 3457f58c7b35Sths * the specified sector) that are known to be in the same 3458f58c7b35Sths * allocated/unallocated state. 3459f58c7b35Sths * 3460bd9533e3SStefan Hajnoczi * 'nb_sectors' is the max value 'pnum' should be set to. If nb_sectors goes 3461bd9533e3SStefan Hajnoczi * beyond the end of the disk image it will be clamped. 3462f58c7b35Sths */ 3463b6b8a333SPaolo Bonzini static int64_t coroutine_fn bdrv_co_get_block_status(BlockDriverState *bs, 3464bdad13b9SPaolo Bonzini int64_t sector_num, 3465060f51c9SStefan Hajnoczi int nb_sectors, int *pnum) 3466f58c7b35Sths { 3467617ccb46SPaolo Bonzini int64_t length; 3468f58c7b35Sths int64_t n; 34695daa74a6SPaolo Bonzini int64_t ret, ret2; 3470bd9533e3SStefan Hajnoczi 3471617ccb46SPaolo Bonzini length = bdrv_getlength(bs); 3472617ccb46SPaolo Bonzini if (length < 0) { 3473617ccb46SPaolo Bonzini return length; 3474617ccb46SPaolo Bonzini } 3475617ccb46SPaolo Bonzini 3476617ccb46SPaolo Bonzini if (sector_num >= (length >> BDRV_SECTOR_BITS)) { 34776aebab14SStefan Hajnoczi *pnum = 0; 34786aebab14SStefan Hajnoczi return 0; 34796aebab14SStefan Hajnoczi } 3480bd9533e3SStefan Hajnoczi 34816aebab14SStefan Hajnoczi n = bs->total_sectors - sector_num; 3482bd9533e3SStefan Hajnoczi if (n < nb_sectors) { 3483bd9533e3SStefan Hajnoczi nb_sectors = n; 3484bd9533e3SStefan Hajnoczi } 3485bd9533e3SStefan Hajnoczi 3486b6b8a333SPaolo Bonzini if (!bs->drv->bdrv_co_get_block_status) { 3487bd9533e3SStefan Hajnoczi *pnum = nb_sectors; 3488918e92d7SPaolo Bonzini ret = BDRV_BLOCK_DATA; 3489918e92d7SPaolo Bonzini if (bs->drv->protocol_name) { 3490918e92d7SPaolo Bonzini ret |= BDRV_BLOCK_OFFSET_VALID | (sector_num * BDRV_SECTOR_SIZE); 3491918e92d7SPaolo Bonzini } 3492918e92d7SPaolo Bonzini return ret; 34936aebab14SStefan Hajnoczi } 34946aebab14SStefan Hajnoczi 3495415b5b01SPaolo Bonzini ret = bs->drv->bdrv_co_get_block_status(bs, sector_num, nb_sectors, pnum); 3496415b5b01SPaolo Bonzini if (ret < 0) { 34973e0a233dSPeter Lieven *pnum = 0; 3498415b5b01SPaolo Bonzini return ret; 3499415b5b01SPaolo Bonzini } 3500415b5b01SPaolo Bonzini 350192bc50a5SPeter Lieven if (ret & BDRV_BLOCK_RAW) { 350292bc50a5SPeter Lieven assert(ret & BDRV_BLOCK_OFFSET_VALID); 350392bc50a5SPeter Lieven return bdrv_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 350492bc50a5SPeter Lieven *pnum, pnum); 350592bc50a5SPeter Lieven } 350692bc50a5SPeter Lieven 3507c3d86884SPeter Lieven if (!(ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO)) { 3508c3d86884SPeter Lieven if (bdrv_unallocated_blocks_are_zero(bs)) { 3509415b5b01SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 35101f9db224SPeter Lieven } else if (bs->backing_hd) { 3511f0ad5712SPaolo Bonzini BlockDriverState *bs2 = bs->backing_hd; 3512f0ad5712SPaolo Bonzini int64_t length2 = bdrv_getlength(bs2); 3513f0ad5712SPaolo Bonzini if (length2 >= 0 && sector_num >= (length2 >> BDRV_SECTOR_BITS)) { 3514f0ad5712SPaolo Bonzini ret |= BDRV_BLOCK_ZERO; 3515f0ad5712SPaolo Bonzini } 3516f0ad5712SPaolo Bonzini } 3517415b5b01SPaolo Bonzini } 35185daa74a6SPaolo Bonzini 35195daa74a6SPaolo Bonzini if (bs->file && 35205daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_DATA) && !(ret & BDRV_BLOCK_ZERO) && 35215daa74a6SPaolo Bonzini (ret & BDRV_BLOCK_OFFSET_VALID)) { 35225daa74a6SPaolo Bonzini ret2 = bdrv_co_get_block_status(bs->file, ret >> BDRV_SECTOR_BITS, 35235daa74a6SPaolo Bonzini *pnum, pnum); 35245daa74a6SPaolo Bonzini if (ret2 >= 0) { 35255daa74a6SPaolo Bonzini /* Ignore errors. This is just providing extra information, it 35265daa74a6SPaolo Bonzini * is useful but not necessary. 35275daa74a6SPaolo Bonzini */ 35285daa74a6SPaolo Bonzini ret |= (ret2 & BDRV_BLOCK_ZERO); 35295daa74a6SPaolo Bonzini } 35305daa74a6SPaolo Bonzini } 35315daa74a6SPaolo Bonzini 3532415b5b01SPaolo Bonzini return ret; 3533060f51c9SStefan Hajnoczi } 3534060f51c9SStefan Hajnoczi 3535b6b8a333SPaolo Bonzini /* Coroutine wrapper for bdrv_get_block_status() */ 3536b6b8a333SPaolo Bonzini static void coroutine_fn bdrv_get_block_status_co_entry(void *opaque) 3537060f51c9SStefan Hajnoczi { 3538b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData *data = opaque; 3539060f51c9SStefan Hajnoczi BlockDriverState *bs = data->bs; 3540060f51c9SStefan Hajnoczi 3541b6b8a333SPaolo Bonzini data->ret = bdrv_co_get_block_status(bs, data->sector_num, data->nb_sectors, 3542060f51c9SStefan Hajnoczi data->pnum); 3543060f51c9SStefan Hajnoczi data->done = true; 3544060f51c9SStefan Hajnoczi } 3545060f51c9SStefan Hajnoczi 3546060f51c9SStefan Hajnoczi /* 3547b6b8a333SPaolo Bonzini * Synchronous wrapper around bdrv_co_get_block_status(). 3548060f51c9SStefan Hajnoczi * 3549b6b8a333SPaolo Bonzini * See bdrv_co_get_block_status() for details. 3550060f51c9SStefan Hajnoczi */ 3551b6b8a333SPaolo Bonzini int64_t bdrv_get_block_status(BlockDriverState *bs, int64_t sector_num, 3552b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 3553060f51c9SStefan Hajnoczi { 3554376ae3f1SStefan Hajnoczi Coroutine *co; 3555b6b8a333SPaolo Bonzini BdrvCoGetBlockStatusData data = { 3556376ae3f1SStefan Hajnoczi .bs = bs, 3557376ae3f1SStefan Hajnoczi .sector_num = sector_num, 3558376ae3f1SStefan Hajnoczi .nb_sectors = nb_sectors, 3559376ae3f1SStefan Hajnoczi .pnum = pnum, 3560376ae3f1SStefan Hajnoczi .done = false, 3561376ae3f1SStefan Hajnoczi }; 3562376ae3f1SStefan Hajnoczi 3563bdad13b9SPaolo Bonzini if (qemu_in_coroutine()) { 3564bdad13b9SPaolo Bonzini /* Fast-path if already in coroutine context */ 3565b6b8a333SPaolo Bonzini bdrv_get_block_status_co_entry(&data); 3566bdad13b9SPaolo Bonzini } else { 3567b6b8a333SPaolo Bonzini co = qemu_coroutine_create(bdrv_get_block_status_co_entry); 3568376ae3f1SStefan Hajnoczi qemu_coroutine_enter(co, &data); 3569376ae3f1SStefan Hajnoczi while (!data.done) { 3570376ae3f1SStefan Hajnoczi qemu_aio_wait(); 3571376ae3f1SStefan Hajnoczi } 3572bdad13b9SPaolo Bonzini } 3573376ae3f1SStefan Hajnoczi return data.ret; 3574376ae3f1SStefan Hajnoczi } 3575f58c7b35Sths 3576b6b8a333SPaolo Bonzini int coroutine_fn bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, 3577b6b8a333SPaolo Bonzini int nb_sectors, int *pnum) 3578b6b8a333SPaolo Bonzini { 35794333bb71SPaolo Bonzini int64_t ret = bdrv_get_block_status(bs, sector_num, nb_sectors, pnum); 35804333bb71SPaolo Bonzini if (ret < 0) { 35814333bb71SPaolo Bonzini return ret; 35824333bb71SPaolo Bonzini } 35834333bb71SPaolo Bonzini return 35844333bb71SPaolo Bonzini (ret & BDRV_BLOCK_DATA) || 35854333bb71SPaolo Bonzini ((ret & BDRV_BLOCK_ZERO) && !bdrv_has_zero_init(bs)); 3586b6b8a333SPaolo Bonzini } 3587b6b8a333SPaolo Bonzini 3588188a7bbfSPaolo Bonzini /* 3589188a7bbfSPaolo Bonzini * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP] 3590188a7bbfSPaolo Bonzini * 3591188a7bbfSPaolo Bonzini * Return true if the given sector is allocated in any image between 3592188a7bbfSPaolo Bonzini * BASE and TOP (inclusive). BASE can be NULL to check if the given 3593188a7bbfSPaolo Bonzini * sector is allocated in any image of the chain. Return false otherwise. 3594188a7bbfSPaolo Bonzini * 3595188a7bbfSPaolo Bonzini * 'pnum' is set to the number of sectors (including and immediately following 3596188a7bbfSPaolo Bonzini * the specified sector) that are known to be in the same 3597188a7bbfSPaolo Bonzini * allocated/unallocated state. 3598188a7bbfSPaolo Bonzini * 3599188a7bbfSPaolo Bonzini */ 36004f578637SPaolo Bonzini int bdrv_is_allocated_above(BlockDriverState *top, 3601188a7bbfSPaolo Bonzini BlockDriverState *base, 3602188a7bbfSPaolo Bonzini int64_t sector_num, 3603188a7bbfSPaolo Bonzini int nb_sectors, int *pnum) 3604188a7bbfSPaolo Bonzini { 3605188a7bbfSPaolo Bonzini BlockDriverState *intermediate; 3606188a7bbfSPaolo Bonzini int ret, n = nb_sectors; 3607188a7bbfSPaolo Bonzini 3608188a7bbfSPaolo Bonzini intermediate = top; 3609188a7bbfSPaolo Bonzini while (intermediate && intermediate != base) { 3610188a7bbfSPaolo Bonzini int pnum_inter; 3611bdad13b9SPaolo Bonzini ret = bdrv_is_allocated(intermediate, sector_num, nb_sectors, 3612188a7bbfSPaolo Bonzini &pnum_inter); 3613188a7bbfSPaolo Bonzini if (ret < 0) { 3614188a7bbfSPaolo Bonzini return ret; 3615188a7bbfSPaolo Bonzini } else if (ret) { 3616188a7bbfSPaolo Bonzini *pnum = pnum_inter; 3617188a7bbfSPaolo Bonzini return 1; 3618188a7bbfSPaolo Bonzini } 3619188a7bbfSPaolo Bonzini 3620188a7bbfSPaolo Bonzini /* 3621188a7bbfSPaolo Bonzini * [sector_num, nb_sectors] is unallocated on top but intermediate 3622188a7bbfSPaolo Bonzini * might have 3623188a7bbfSPaolo Bonzini * 3624188a7bbfSPaolo Bonzini * [sector_num+x, nr_sectors] allocated. 3625188a7bbfSPaolo Bonzini */ 362663ba17d3SVishvananda Ishaya if (n > pnum_inter && 362763ba17d3SVishvananda Ishaya (intermediate == top || 362863ba17d3SVishvananda Ishaya sector_num + pnum_inter < intermediate->total_sectors)) { 3629188a7bbfSPaolo Bonzini n = pnum_inter; 3630188a7bbfSPaolo Bonzini } 3631188a7bbfSPaolo Bonzini 3632188a7bbfSPaolo Bonzini intermediate = intermediate->backing_hd; 3633188a7bbfSPaolo Bonzini } 3634188a7bbfSPaolo Bonzini 3635188a7bbfSPaolo Bonzini *pnum = n; 3636188a7bbfSPaolo Bonzini return 0; 3637188a7bbfSPaolo Bonzini } 3638188a7bbfSPaolo Bonzini 3639045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 3640045df330Saliguori { 3641045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 3642045df330Saliguori return bs->backing_file; 3643045df330Saliguori else if (bs->encrypted) 3644045df330Saliguori return bs->filename; 3645045df330Saliguori else 3646045df330Saliguori return NULL; 3647045df330Saliguori } 3648045df330Saliguori 364983f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 365083f64091Sbellard char *filename, int filename_size) 365183f64091Sbellard { 365283f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 365383f64091Sbellard } 365483f64091Sbellard 3655faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 3656faea38e7Sbellard const uint8_t *buf, int nb_sectors) 3657faea38e7Sbellard { 3658faea38e7Sbellard BlockDriver *drv = bs->drv; 3659faea38e7Sbellard if (!drv) 366019cb3738Sbellard return -ENOMEDIUM; 3661faea38e7Sbellard if (!drv->bdrv_write_compressed) 3662faea38e7Sbellard return -ENOTSUP; 3663fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 3664fbb7b4e0SKevin Wolf return -EIO; 36657cd1e32aSlirans@il.ibm.com 3666e4654d2dSFam Zheng assert(QLIST_EMPTY(&bs->dirty_bitmaps)); 36677cd1e32aSlirans@il.ibm.com 3668faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 3669faea38e7Sbellard } 3670faea38e7Sbellard 3671faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 3672faea38e7Sbellard { 3673faea38e7Sbellard BlockDriver *drv = bs->drv; 3674faea38e7Sbellard if (!drv) 367519cb3738Sbellard return -ENOMEDIUM; 3676faea38e7Sbellard if (!drv->bdrv_get_info) 3677faea38e7Sbellard return -ENOTSUP; 3678faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 3679faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 3680faea38e7Sbellard } 3681faea38e7Sbellard 3682eae041feSMax Reitz ImageInfoSpecific *bdrv_get_specific_info(BlockDriverState *bs) 3683eae041feSMax Reitz { 3684eae041feSMax Reitz BlockDriver *drv = bs->drv; 3685eae041feSMax Reitz if (drv && drv->bdrv_get_specific_info) { 3686eae041feSMax Reitz return drv->bdrv_get_specific_info(bs); 3687eae041feSMax Reitz } 3688eae041feSMax Reitz return NULL; 3689eae041feSMax Reitz } 3690eae041feSMax Reitz 369145566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 369245566e9cSChristoph Hellwig int64_t pos, int size) 3693178e08a5Saliguori { 3694cf8074b3SKevin Wolf QEMUIOVector qiov; 3695cf8074b3SKevin Wolf struct iovec iov = { 3696cf8074b3SKevin Wolf .iov_base = (void *) buf, 3697cf8074b3SKevin Wolf .iov_len = size, 3698cf8074b3SKevin Wolf }; 3699cf8074b3SKevin Wolf 3700cf8074b3SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 3701cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs, &qiov, pos); 3702cf8074b3SKevin Wolf } 3703cf8074b3SKevin Wolf 3704cf8074b3SKevin Wolf int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos) 3705cf8074b3SKevin Wolf { 3706178e08a5Saliguori BlockDriver *drv = bs->drv; 3707cf8074b3SKevin Wolf 3708cf8074b3SKevin Wolf if (!drv) { 3709178e08a5Saliguori return -ENOMEDIUM; 3710cf8074b3SKevin Wolf } else if (drv->bdrv_save_vmstate) { 3711cf8074b3SKevin Wolf return drv->bdrv_save_vmstate(bs, qiov, pos); 3712cf8074b3SKevin Wolf } else if (bs->file) { 3713cf8074b3SKevin Wolf return bdrv_writev_vmstate(bs->file, qiov, pos); 3714cf8074b3SKevin Wolf } 3715cf8074b3SKevin Wolf 37167cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3717178e08a5Saliguori } 3718178e08a5Saliguori 371945566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 372045566e9cSChristoph Hellwig int64_t pos, int size) 3721178e08a5Saliguori { 3722178e08a5Saliguori BlockDriver *drv = bs->drv; 3723178e08a5Saliguori if (!drv) 3724178e08a5Saliguori return -ENOMEDIUM; 37257cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 372645566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 37277cdb1f6dSMORITA Kazutaka if (bs->file) 37287cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 37297cdb1f6dSMORITA Kazutaka return -ENOTSUP; 3730178e08a5Saliguori } 3731178e08a5Saliguori 37328b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 37338b9b0cc2SKevin Wolf { 3734bf736fe3SKevin Wolf if (!bs || !bs->drv || !bs->drv->bdrv_debug_event) { 37358b9b0cc2SKevin Wolf return; 37368b9b0cc2SKevin Wolf } 37378b9b0cc2SKevin Wolf 3738bf736fe3SKevin Wolf bs->drv->bdrv_debug_event(bs, event); 373941c695c7SKevin Wolf } 37408b9b0cc2SKevin Wolf 374141c695c7SKevin Wolf int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event, 374241c695c7SKevin Wolf const char *tag) 374341c695c7SKevin Wolf { 374441c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) { 374541c695c7SKevin Wolf bs = bs->file; 374641c695c7SKevin Wolf } 374741c695c7SKevin Wolf 374841c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) { 374941c695c7SKevin Wolf return bs->drv->bdrv_debug_breakpoint(bs, event, tag); 375041c695c7SKevin Wolf } 375141c695c7SKevin Wolf 375241c695c7SKevin Wolf return -ENOTSUP; 375341c695c7SKevin Wolf } 375441c695c7SKevin Wolf 37554cc70e93SFam Zheng int bdrv_debug_remove_breakpoint(BlockDriverState *bs, const char *tag) 37564cc70e93SFam Zheng { 37574cc70e93SFam Zheng while (bs && bs->drv && !bs->drv->bdrv_debug_remove_breakpoint) { 37584cc70e93SFam Zheng bs = bs->file; 37594cc70e93SFam Zheng } 37604cc70e93SFam Zheng 37614cc70e93SFam Zheng if (bs && bs->drv && bs->drv->bdrv_debug_remove_breakpoint) { 37624cc70e93SFam Zheng return bs->drv->bdrv_debug_remove_breakpoint(bs, tag); 37634cc70e93SFam Zheng } 37644cc70e93SFam Zheng 37654cc70e93SFam Zheng return -ENOTSUP; 37664cc70e93SFam Zheng } 37674cc70e93SFam Zheng 376841c695c7SKevin Wolf int bdrv_debug_resume(BlockDriverState *bs, const char *tag) 376941c695c7SKevin Wolf { 377041c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_resume) { 377141c695c7SKevin Wolf bs = bs->file; 377241c695c7SKevin Wolf } 377341c695c7SKevin Wolf 377441c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_resume) { 377541c695c7SKevin Wolf return bs->drv->bdrv_debug_resume(bs, tag); 377641c695c7SKevin Wolf } 377741c695c7SKevin Wolf 377841c695c7SKevin Wolf return -ENOTSUP; 377941c695c7SKevin Wolf } 378041c695c7SKevin Wolf 378141c695c7SKevin Wolf bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag) 378241c695c7SKevin Wolf { 378341c695c7SKevin Wolf while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) { 378441c695c7SKevin Wolf bs = bs->file; 378541c695c7SKevin Wolf } 378641c695c7SKevin Wolf 378741c695c7SKevin Wolf if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) { 378841c695c7SKevin Wolf return bs->drv->bdrv_debug_is_suspended(bs, tag); 378941c695c7SKevin Wolf } 379041c695c7SKevin Wolf 379141c695c7SKevin Wolf return false; 37928b9b0cc2SKevin Wolf } 37938b9b0cc2SKevin Wolf 3794199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 3795199630b6SBlue Swirl { 3796199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 3797199630b6SBlue Swirl } 3798199630b6SBlue Swirl 3799b1b1d783SJeff Cody /* backing_file can either be relative, or absolute, or a protocol. If it is 3800b1b1d783SJeff Cody * relative, it must be relative to the chain. So, passing in bs->filename 3801b1b1d783SJeff Cody * from a BDS as backing_file should not be done, as that may be relative to 3802b1b1d783SJeff Cody * the CWD rather than the chain. */ 3803e8a6bb9cSMarcelo Tosatti BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs, 3804e8a6bb9cSMarcelo Tosatti const char *backing_file) 3805e8a6bb9cSMarcelo Tosatti { 3806b1b1d783SJeff Cody char *filename_full = NULL; 3807b1b1d783SJeff Cody char *backing_file_full = NULL; 3808b1b1d783SJeff Cody char *filename_tmp = NULL; 3809b1b1d783SJeff Cody int is_protocol = 0; 3810b1b1d783SJeff Cody BlockDriverState *curr_bs = NULL; 3811b1b1d783SJeff Cody BlockDriverState *retval = NULL; 3812b1b1d783SJeff Cody 3813b1b1d783SJeff Cody if (!bs || !bs->drv || !backing_file) { 3814e8a6bb9cSMarcelo Tosatti return NULL; 3815e8a6bb9cSMarcelo Tosatti } 3816e8a6bb9cSMarcelo Tosatti 3817b1b1d783SJeff Cody filename_full = g_malloc(PATH_MAX); 3818b1b1d783SJeff Cody backing_file_full = g_malloc(PATH_MAX); 3819b1b1d783SJeff Cody filename_tmp = g_malloc(PATH_MAX); 3820b1b1d783SJeff Cody 3821b1b1d783SJeff Cody is_protocol = path_has_protocol(backing_file); 3822b1b1d783SJeff Cody 3823b1b1d783SJeff Cody for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) { 3824b1b1d783SJeff Cody 3825b1b1d783SJeff Cody /* If either of the filename paths is actually a protocol, then 3826b1b1d783SJeff Cody * compare unmodified paths; otherwise make paths relative */ 3827b1b1d783SJeff Cody if (is_protocol || path_has_protocol(curr_bs->backing_file)) { 3828b1b1d783SJeff Cody if (strcmp(backing_file, curr_bs->backing_file) == 0) { 3829b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3830b1b1d783SJeff Cody break; 3831b1b1d783SJeff Cody } 3832e8a6bb9cSMarcelo Tosatti } else { 3833b1b1d783SJeff Cody /* If not an absolute filename path, make it relative to the current 3834b1b1d783SJeff Cody * image's filename path */ 3835b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3836b1b1d783SJeff Cody backing_file); 3837b1b1d783SJeff Cody 3838b1b1d783SJeff Cody /* We are going to compare absolute pathnames */ 3839b1b1d783SJeff Cody if (!realpath(filename_tmp, filename_full)) { 3840b1b1d783SJeff Cody continue; 3841b1b1d783SJeff Cody } 3842b1b1d783SJeff Cody 3843b1b1d783SJeff Cody /* We need to make sure the backing filename we are comparing against 3844b1b1d783SJeff Cody * is relative to the current image filename (or absolute) */ 3845b1b1d783SJeff Cody path_combine(filename_tmp, PATH_MAX, curr_bs->filename, 3846b1b1d783SJeff Cody curr_bs->backing_file); 3847b1b1d783SJeff Cody 3848b1b1d783SJeff Cody if (!realpath(filename_tmp, backing_file_full)) { 3849b1b1d783SJeff Cody continue; 3850b1b1d783SJeff Cody } 3851b1b1d783SJeff Cody 3852b1b1d783SJeff Cody if (strcmp(backing_file_full, filename_full) == 0) { 3853b1b1d783SJeff Cody retval = curr_bs->backing_hd; 3854b1b1d783SJeff Cody break; 3855b1b1d783SJeff Cody } 3856e8a6bb9cSMarcelo Tosatti } 3857e8a6bb9cSMarcelo Tosatti } 3858e8a6bb9cSMarcelo Tosatti 3859b1b1d783SJeff Cody g_free(filename_full); 3860b1b1d783SJeff Cody g_free(backing_file_full); 3861b1b1d783SJeff Cody g_free(filename_tmp); 3862b1b1d783SJeff Cody return retval; 3863e8a6bb9cSMarcelo Tosatti } 3864e8a6bb9cSMarcelo Tosatti 3865f198fd1cSBenoît Canet int bdrv_get_backing_file_depth(BlockDriverState *bs) 3866f198fd1cSBenoît Canet { 3867f198fd1cSBenoît Canet if (!bs->drv) { 3868f198fd1cSBenoît Canet return 0; 3869f198fd1cSBenoît Canet } 3870f198fd1cSBenoît Canet 3871f198fd1cSBenoît Canet if (!bs->backing_hd) { 3872f198fd1cSBenoît Canet return 0; 3873f198fd1cSBenoît Canet } 3874f198fd1cSBenoît Canet 3875f198fd1cSBenoît Canet return 1 + bdrv_get_backing_file_depth(bs->backing_hd); 3876f198fd1cSBenoît Canet } 3877f198fd1cSBenoît Canet 387879fac568SJeff Cody BlockDriverState *bdrv_find_base(BlockDriverState *bs) 387979fac568SJeff Cody { 388079fac568SJeff Cody BlockDriverState *curr_bs = NULL; 388179fac568SJeff Cody 388279fac568SJeff Cody if (!bs) { 388379fac568SJeff Cody return NULL; 388479fac568SJeff Cody } 388579fac568SJeff Cody 388679fac568SJeff Cody curr_bs = bs; 388779fac568SJeff Cody 388879fac568SJeff Cody while (curr_bs->backing_hd) { 388979fac568SJeff Cody curr_bs = curr_bs->backing_hd; 389079fac568SJeff Cody } 389179fac568SJeff Cody return curr_bs; 389279fac568SJeff Cody } 389379fac568SJeff Cody 3894ea2384d3Sbellard /**************************************************************/ 389583f64091Sbellard /* async I/Os */ 3896ea2384d3Sbellard 38973b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 3898f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 389983f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 3900ea2384d3Sbellard { 3901bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 3902bbf0a440SStefan Hajnoczi 3903d20d9b7cSPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, 39048c5873d6SStefan Hajnoczi cb, opaque, false); 390583f64091Sbellard } 390683f64091Sbellard 3907f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 3908f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 390983f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 39107674e7bfSbellard { 3911bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 3912bbf0a440SStefan Hajnoczi 3913d20d9b7cSPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, 0, 39148c5873d6SStefan Hajnoczi cb, opaque, true); 391583f64091Sbellard } 391683f64091Sbellard 3917d5ef94d4SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_write_zeroes(BlockDriverState *bs, 3918d5ef94d4SPaolo Bonzini int64_t sector_num, int nb_sectors, BdrvRequestFlags flags, 3919d5ef94d4SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 3920d5ef94d4SPaolo Bonzini { 3921d5ef94d4SPaolo Bonzini trace_bdrv_aio_write_zeroes(bs, sector_num, nb_sectors, flags, opaque); 3922d5ef94d4SPaolo Bonzini 3923d5ef94d4SPaolo Bonzini return bdrv_co_aio_rw_vector(bs, sector_num, NULL, nb_sectors, 3924d5ef94d4SPaolo Bonzini BDRV_REQ_ZERO_WRITE | flags, 3925d5ef94d4SPaolo Bonzini cb, opaque, true); 3926d5ef94d4SPaolo Bonzini } 3927d5ef94d4SPaolo Bonzini 392840b4f539SKevin Wolf 392940b4f539SKevin Wolf typedef struct MultiwriteCB { 393040b4f539SKevin Wolf int error; 393140b4f539SKevin Wolf int num_requests; 393240b4f539SKevin Wolf int num_callbacks; 393340b4f539SKevin Wolf struct { 393440b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 393540b4f539SKevin Wolf void *opaque; 393640b4f539SKevin Wolf QEMUIOVector *free_qiov; 393740b4f539SKevin Wolf } callbacks[]; 393840b4f539SKevin Wolf } MultiwriteCB; 393940b4f539SKevin Wolf 394040b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 394140b4f539SKevin Wolf { 394240b4f539SKevin Wolf int i; 394340b4f539SKevin Wolf 394440b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 394540b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 39461e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 39471e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 39481e1ea48dSStefan Hajnoczi } 39497267c094SAnthony Liguori g_free(mcb->callbacks[i].free_qiov); 395040b4f539SKevin Wolf } 395140b4f539SKevin Wolf } 395240b4f539SKevin Wolf 395340b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 395440b4f539SKevin Wolf { 395540b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 395640b4f539SKevin Wolf 39576d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 39586d519a5fSStefan Hajnoczi 3959cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 396040b4f539SKevin Wolf mcb->error = ret; 396140b4f539SKevin Wolf } 396240b4f539SKevin Wolf 396340b4f539SKevin Wolf mcb->num_requests--; 396440b4f539SKevin Wolf if (mcb->num_requests == 0) { 396540b4f539SKevin Wolf multiwrite_user_cb(mcb); 39667267c094SAnthony Liguori g_free(mcb); 396740b4f539SKevin Wolf } 396840b4f539SKevin Wolf } 396940b4f539SKevin Wolf 397040b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 397140b4f539SKevin Wolf { 397277be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 397377be4366SChristoph Hellwig 397477be4366SChristoph Hellwig /* 397577be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 397677be4366SChristoph Hellwig * here as that could overflow the return value. 397777be4366SChristoph Hellwig */ 397877be4366SChristoph Hellwig if (req1->sector > req2->sector) { 397977be4366SChristoph Hellwig return 1; 398077be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 398177be4366SChristoph Hellwig return -1; 398277be4366SChristoph Hellwig } else { 398377be4366SChristoph Hellwig return 0; 398477be4366SChristoph Hellwig } 398540b4f539SKevin Wolf } 398640b4f539SKevin Wolf 398740b4f539SKevin Wolf /* 398840b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 398940b4f539SKevin Wolf * requests that remain after merging. 399040b4f539SKevin Wolf */ 399140b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 399240b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 399340b4f539SKevin Wolf { 399440b4f539SKevin Wolf int i, outidx; 399540b4f539SKevin Wolf 399640b4f539SKevin Wolf // Sort requests by start sector 399740b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 399840b4f539SKevin Wolf 399940b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 400040b4f539SKevin Wolf // filling up gaps with zero sectors. 400140b4f539SKevin Wolf outidx = 0; 400240b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 400340b4f539SKevin Wolf int merge = 0; 400440b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 400540b4f539SKevin Wolf 4006b6a127a1SPaolo Bonzini // Handle exactly sequential writes and overlapping writes. 400740b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 400840b4f539SKevin Wolf merge = 1; 400940b4f539SKevin Wolf } 401040b4f539SKevin Wolf 4011e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 4012e2a305fbSChristoph Hellwig merge = 0; 4013e2a305fbSChristoph Hellwig } 4014e2a305fbSChristoph Hellwig 401540b4f539SKevin Wolf if (merge) { 401640b4f539SKevin Wolf size_t size; 40177267c094SAnthony Liguori QEMUIOVector *qiov = g_malloc0(sizeof(*qiov)); 401840b4f539SKevin Wolf qemu_iovec_init(qiov, 401940b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 402040b4f539SKevin Wolf 402140b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 402240b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 402340b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 40241b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size); 402540b4f539SKevin Wolf 4026b6a127a1SPaolo Bonzini // We should need to add any zeros between the two requests 4027b6a127a1SPaolo Bonzini assert (reqs[i].sector <= oldreq_last); 402840b4f539SKevin Wolf 402940b4f539SKevin Wolf // Add the second request 40301b093c48SMichael Tokarev qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size); 403140b4f539SKevin Wolf 4032cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 403340b4f539SKevin Wolf reqs[outidx].qiov = qiov; 403440b4f539SKevin Wolf 403540b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 403640b4f539SKevin Wolf } else { 403740b4f539SKevin Wolf outidx++; 403840b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 403940b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 404040b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 404140b4f539SKevin Wolf } 404240b4f539SKevin Wolf } 404340b4f539SKevin Wolf 404440b4f539SKevin Wolf return outidx + 1; 404540b4f539SKevin Wolf } 404640b4f539SKevin Wolf 404740b4f539SKevin Wolf /* 404840b4f539SKevin Wolf * Submit multiple AIO write requests at once. 404940b4f539SKevin Wolf * 405040b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 405140b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 405240b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 405340b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 405440b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 405540b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 405640b4f539SKevin Wolf * 405740b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 405840b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 405940b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 406040b4f539SKevin Wolf */ 406140b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 406240b4f539SKevin Wolf { 406340b4f539SKevin Wolf MultiwriteCB *mcb; 406440b4f539SKevin Wolf int i; 406540b4f539SKevin Wolf 4066301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 4067301db7c2SRyan Harper if (bs->drv == NULL) { 4068301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 4069301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 4070301db7c2SRyan Harper } 4071301db7c2SRyan Harper return -1; 4072301db7c2SRyan Harper } 4073301db7c2SRyan Harper 407440b4f539SKevin Wolf if (num_reqs == 0) { 407540b4f539SKevin Wolf return 0; 407640b4f539SKevin Wolf } 407740b4f539SKevin Wolf 407840b4f539SKevin Wolf // Create MultiwriteCB structure 40797267c094SAnthony Liguori mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 408040b4f539SKevin Wolf mcb->num_requests = 0; 408140b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 408240b4f539SKevin Wolf 408340b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 408440b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 408540b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 408640b4f539SKevin Wolf } 408740b4f539SKevin Wolf 408840b4f539SKevin Wolf // Check for mergable requests 408940b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 409040b4f539SKevin Wolf 40916d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 40926d519a5fSStefan Hajnoczi 4093df9309fbSPaolo Bonzini /* Run the aio requests. */ 4094df9309fbSPaolo Bonzini mcb->num_requests = num_reqs; 409540b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 4096d20d9b7cSPaolo Bonzini bdrv_co_aio_rw_vector(bs, reqs[i].sector, reqs[i].qiov, 4097d20d9b7cSPaolo Bonzini reqs[i].nb_sectors, reqs[i].flags, 4098d20d9b7cSPaolo Bonzini multiwrite_cb, mcb, 4099d20d9b7cSPaolo Bonzini true); 410040b4f539SKevin Wolf } 410140b4f539SKevin Wolf 410240b4f539SKevin Wolf return 0; 410340b4f539SKevin Wolf } 410440b4f539SKevin Wolf 410583f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 410683f64091Sbellard { 4107d7331bedSStefan Hajnoczi acb->aiocb_info->cancel(acb); 410883f64091Sbellard } 410983f64091Sbellard 411083f64091Sbellard /**************************************************************/ 411183f64091Sbellard /* async block device emulation */ 411283f64091Sbellard 4113c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 4114c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 4115c16b5a2cSChristoph Hellwig QEMUBH *bh; 4116c16b5a2cSChristoph Hellwig int ret; 4117c16b5a2cSChristoph Hellwig /* vector translation state */ 4118c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 4119c16b5a2cSChristoph Hellwig uint8_t *bounce; 4120c16b5a2cSChristoph Hellwig int is_write; 4121c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 4122c16b5a2cSChristoph Hellwig 4123c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 4124c16b5a2cSChristoph Hellwig { 4125b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 4126b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 41276a7ad299SDor Laor qemu_bh_delete(acb->bh); 412836afc451SAvi Kivity acb->bh = NULL; 4129c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 4130c16b5a2cSChristoph Hellwig } 4131c16b5a2cSChristoph Hellwig 4132d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_aiocb_info = { 4133c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 4134c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 4135c16b5a2cSChristoph Hellwig }; 4136c16b5a2cSChristoph Hellwig 413783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 4138beac80cdSbellard { 4139ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 4140f141eafeSaliguori 4141f141eafeSaliguori if (!acb->is_write) 414203396148SMichael Tokarev qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size); 4143ceb42de8Saliguori qemu_vfree(acb->bounce); 4144ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 41456a7ad299SDor Laor qemu_bh_delete(acb->bh); 414636afc451SAvi Kivity acb->bh = NULL; 4147ce1a14dcSpbrook qemu_aio_release(acb); 4148beac80cdSbellard } 4149beac80cdSbellard 4150f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 4151f141eafeSaliguori int64_t sector_num, 4152f141eafeSaliguori QEMUIOVector *qiov, 4153f141eafeSaliguori int nb_sectors, 4154f141eafeSaliguori BlockDriverCompletionFunc *cb, 4155f141eafeSaliguori void *opaque, 4156f141eafeSaliguori int is_write) 4157f141eafeSaliguori 4158ea2384d3Sbellard { 4159ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 416083f64091Sbellard 4161d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque); 4162f141eafeSaliguori acb->is_write = is_write; 4163f141eafeSaliguori acb->qiov = qiov; 4164e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 4165ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 4166f141eafeSaliguori 4167f141eafeSaliguori if (is_write) { 4168d5e6b161SMichael Tokarev qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size); 41691ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 4170f141eafeSaliguori } else { 41711ed20acfSStefan Hajnoczi acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 4172f141eafeSaliguori } 4173f141eafeSaliguori 4174ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 4175f141eafeSaliguori 4176ce1a14dcSpbrook return &acb->common; 41777a6cba61Spbrook } 41787a6cba61Spbrook 4179f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 4180f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 4181ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 418283f64091Sbellard { 4183f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 418483f64091Sbellard } 418583f64091Sbellard 4186f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 4187f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 4188f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 4189f141eafeSaliguori { 4190f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 4191f141eafeSaliguori } 4192f141eafeSaliguori 419368485420SKevin Wolf 419468485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 419568485420SKevin Wolf BlockDriverAIOCB common; 419668485420SKevin Wolf BlockRequest req; 419768485420SKevin Wolf bool is_write; 4198d318aea9SKevin Wolf bool *done; 419968485420SKevin Wolf QEMUBH* bh; 420068485420SKevin Wolf } BlockDriverAIOCBCoroutine; 420168485420SKevin Wolf 420268485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 420368485420SKevin Wolf { 4204d318aea9SKevin Wolf BlockDriverAIOCBCoroutine *acb = 4205d318aea9SKevin Wolf container_of(blockacb, BlockDriverAIOCBCoroutine, common); 4206d318aea9SKevin Wolf bool done = false; 4207d318aea9SKevin Wolf 4208d318aea9SKevin Wolf acb->done = &done; 4209d318aea9SKevin Wolf while (!done) { 4210d318aea9SKevin Wolf qemu_aio_wait(); 4211d318aea9SKevin Wolf } 421268485420SKevin Wolf } 421368485420SKevin Wolf 4214d7331bedSStefan Hajnoczi static const AIOCBInfo bdrv_em_co_aiocb_info = { 421568485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 421668485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 421768485420SKevin Wolf }; 421868485420SKevin Wolf 421935246a68SPaolo Bonzini static void bdrv_co_em_bh(void *opaque) 422068485420SKevin Wolf { 422168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 422268485420SKevin Wolf 422368485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 4224d318aea9SKevin Wolf 4225d318aea9SKevin Wolf if (acb->done) { 4226d318aea9SKevin Wolf *acb->done = true; 4227d318aea9SKevin Wolf } 4228d318aea9SKevin Wolf 422968485420SKevin Wolf qemu_bh_delete(acb->bh); 423068485420SKevin Wolf qemu_aio_release(acb); 423168485420SKevin Wolf } 423268485420SKevin Wolf 4233b2a61371SStefan Hajnoczi /* Invoke bdrv_co_do_readv/bdrv_co_do_writev */ 4234b2a61371SStefan Hajnoczi static void coroutine_fn bdrv_co_do_rw(void *opaque) 4235b2a61371SStefan Hajnoczi { 4236b2a61371SStefan Hajnoczi BlockDriverAIOCBCoroutine *acb = opaque; 4237b2a61371SStefan Hajnoczi BlockDriverState *bs = acb->common.bs; 4238b2a61371SStefan Hajnoczi 4239b2a61371SStefan Hajnoczi if (!acb->is_write) { 4240b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_readv(bs, acb->req.sector, 4241d20d9b7cSPaolo Bonzini acb->req.nb_sectors, acb->req.qiov, acb->req.flags); 4242b2a61371SStefan Hajnoczi } else { 4243b2a61371SStefan Hajnoczi acb->req.error = bdrv_co_do_writev(bs, acb->req.sector, 4244d20d9b7cSPaolo Bonzini acb->req.nb_sectors, acb->req.qiov, acb->req.flags); 4245b2a61371SStefan Hajnoczi } 4246b2a61371SStefan Hajnoczi 424735246a68SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 4248b2a61371SStefan Hajnoczi qemu_bh_schedule(acb->bh); 4249b2a61371SStefan Hajnoczi } 4250b2a61371SStefan Hajnoczi 425168485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 425268485420SKevin Wolf int64_t sector_num, 425368485420SKevin Wolf QEMUIOVector *qiov, 425468485420SKevin Wolf int nb_sectors, 4255d20d9b7cSPaolo Bonzini BdrvRequestFlags flags, 425668485420SKevin Wolf BlockDriverCompletionFunc *cb, 425768485420SKevin Wolf void *opaque, 42588c5873d6SStefan Hajnoczi bool is_write) 425968485420SKevin Wolf { 426068485420SKevin Wolf Coroutine *co; 426168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 426268485420SKevin Wolf 4263d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 426468485420SKevin Wolf acb->req.sector = sector_num; 426568485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 426668485420SKevin Wolf acb->req.qiov = qiov; 4267d20d9b7cSPaolo Bonzini acb->req.flags = flags; 426868485420SKevin Wolf acb->is_write = is_write; 4269d318aea9SKevin Wolf acb->done = NULL; 427068485420SKevin Wolf 42718c5873d6SStefan Hajnoczi co = qemu_coroutine_create(bdrv_co_do_rw); 427268485420SKevin Wolf qemu_coroutine_enter(co, acb); 427368485420SKevin Wolf 427468485420SKevin Wolf return &acb->common; 427568485420SKevin Wolf } 427668485420SKevin Wolf 427707f07615SPaolo Bonzini static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque) 4278b2e12bc6SChristoph Hellwig { 427907f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 428007f07615SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 4281b2e12bc6SChristoph Hellwig 428207f07615SPaolo Bonzini acb->req.error = bdrv_co_flush(bs); 428307f07615SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 4284b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 4285b2e12bc6SChristoph Hellwig } 4286b2e12bc6SChristoph Hellwig 428707f07615SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 4288016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 4289016f5cf6SAlexander Graf { 429007f07615SPaolo Bonzini trace_bdrv_aio_flush(bs, opaque); 4291016f5cf6SAlexander Graf 429207f07615SPaolo Bonzini Coroutine *co; 429307f07615SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 4294016f5cf6SAlexander Graf 4295d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 4296d318aea9SKevin Wolf acb->done = NULL; 4297d318aea9SKevin Wolf 429807f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_flush_co_entry); 429907f07615SPaolo Bonzini qemu_coroutine_enter(co, acb); 4300016f5cf6SAlexander Graf 4301016f5cf6SAlexander Graf return &acb->common; 4302016f5cf6SAlexander Graf } 4303016f5cf6SAlexander Graf 43044265d620SPaolo Bonzini static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque) 43054265d620SPaolo Bonzini { 43064265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb = opaque; 43074265d620SPaolo Bonzini BlockDriverState *bs = acb->common.bs; 43084265d620SPaolo Bonzini 43094265d620SPaolo Bonzini acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors); 43104265d620SPaolo Bonzini acb->bh = qemu_bh_new(bdrv_co_em_bh, acb); 43114265d620SPaolo Bonzini qemu_bh_schedule(acb->bh); 43124265d620SPaolo Bonzini } 43134265d620SPaolo Bonzini 43144265d620SPaolo Bonzini BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs, 43154265d620SPaolo Bonzini int64_t sector_num, int nb_sectors, 43164265d620SPaolo Bonzini BlockDriverCompletionFunc *cb, void *opaque) 43174265d620SPaolo Bonzini { 43184265d620SPaolo Bonzini Coroutine *co; 43194265d620SPaolo Bonzini BlockDriverAIOCBCoroutine *acb; 43204265d620SPaolo Bonzini 43214265d620SPaolo Bonzini trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque); 43224265d620SPaolo Bonzini 4323d7331bedSStefan Hajnoczi acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque); 43244265d620SPaolo Bonzini acb->req.sector = sector_num; 43254265d620SPaolo Bonzini acb->req.nb_sectors = nb_sectors; 4326d318aea9SKevin Wolf acb->done = NULL; 43274265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_aio_discard_co_entry); 43284265d620SPaolo Bonzini qemu_coroutine_enter(co, acb); 43294265d620SPaolo Bonzini 43304265d620SPaolo Bonzini return &acb->common; 43314265d620SPaolo Bonzini } 43324265d620SPaolo Bonzini 4333ea2384d3Sbellard void bdrv_init(void) 4334ea2384d3Sbellard { 43355efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 4336ea2384d3Sbellard } 4337ce1a14dcSpbrook 4338eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 4339eb852011SMarkus Armbruster { 4340eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 4341eb852011SMarkus Armbruster bdrv_init(); 4342eb852011SMarkus Armbruster } 4343eb852011SMarkus Armbruster 4344d7331bedSStefan Hajnoczi void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs, 43456bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 43466bbff9a0Saliguori { 4347ce1a14dcSpbrook BlockDriverAIOCB *acb; 4348ce1a14dcSpbrook 4349d7331bedSStefan Hajnoczi acb = g_slice_alloc(aiocb_info->aiocb_size); 4350d7331bedSStefan Hajnoczi acb->aiocb_info = aiocb_info; 4351ce1a14dcSpbrook acb->bs = bs; 4352ce1a14dcSpbrook acb->cb = cb; 4353ce1a14dcSpbrook acb->opaque = opaque; 4354ce1a14dcSpbrook return acb; 4355ce1a14dcSpbrook } 4356ce1a14dcSpbrook 4357ce1a14dcSpbrook void qemu_aio_release(void *p) 4358ce1a14dcSpbrook { 4359d37c975fSStefan Hajnoczi BlockDriverAIOCB *acb = p; 4360d7331bedSStefan Hajnoczi g_slice_free1(acb->aiocb_info->aiocb_size, acb); 4361ce1a14dcSpbrook } 436219cb3738Sbellard 436319cb3738Sbellard /**************************************************************/ 4364f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 4365f9f05dc5SKevin Wolf 4366f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 4367f9f05dc5SKevin Wolf Coroutine *coroutine; 4368f9f05dc5SKevin Wolf int ret; 4369f9f05dc5SKevin Wolf } CoroutineIOCompletion; 4370f9f05dc5SKevin Wolf 4371f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 4372f9f05dc5SKevin Wolf { 4373f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 4374f9f05dc5SKevin Wolf 4375f9f05dc5SKevin Wolf co->ret = ret; 4376f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 4377f9f05dc5SKevin Wolf } 4378f9f05dc5SKevin Wolf 4379f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 4380f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 4381f9f05dc5SKevin Wolf bool is_write) 4382f9f05dc5SKevin Wolf { 4383f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 4384f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 4385f9f05dc5SKevin Wolf }; 4386f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 4387f9f05dc5SKevin Wolf 4388f9f05dc5SKevin Wolf if (is_write) { 4389a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 4390f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4391f9f05dc5SKevin Wolf } else { 4392a652d160SStefan Hajnoczi acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 4393f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 4394f9f05dc5SKevin Wolf } 4395f9f05dc5SKevin Wolf 439659370aaaSStefan Hajnoczi trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb); 4397f9f05dc5SKevin Wolf if (!acb) { 4398f9f05dc5SKevin Wolf return -EIO; 4399f9f05dc5SKevin Wolf } 4400f9f05dc5SKevin Wolf qemu_coroutine_yield(); 4401f9f05dc5SKevin Wolf 4402f9f05dc5SKevin Wolf return co.ret; 4403f9f05dc5SKevin Wolf } 4404f9f05dc5SKevin Wolf 4405f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 4406f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4407f9f05dc5SKevin Wolf QEMUIOVector *iov) 4408f9f05dc5SKevin Wolf { 4409f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 4410f9f05dc5SKevin Wolf } 4411f9f05dc5SKevin Wolf 4412f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 4413f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 4414f9f05dc5SKevin Wolf QEMUIOVector *iov) 4415f9f05dc5SKevin Wolf { 4416f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 4417f9f05dc5SKevin Wolf } 4418f9f05dc5SKevin Wolf 441907f07615SPaolo Bonzini static void coroutine_fn bdrv_flush_co_entry(void *opaque) 4420e7a8a783SKevin Wolf { 442107f07615SPaolo Bonzini RwCo *rwco = opaque; 442207f07615SPaolo Bonzini 442307f07615SPaolo Bonzini rwco->ret = bdrv_co_flush(rwco->bs); 442407f07615SPaolo Bonzini } 442507f07615SPaolo Bonzini 442607f07615SPaolo Bonzini int coroutine_fn bdrv_co_flush(BlockDriverState *bs) 442707f07615SPaolo Bonzini { 4428eb489bb1SKevin Wolf int ret; 4429eb489bb1SKevin Wolf 443029cdb251SPaolo Bonzini if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) { 443107f07615SPaolo Bonzini return 0; 4432eb489bb1SKevin Wolf } 4433eb489bb1SKevin Wolf 4434ca716364SKevin Wolf /* Write back cached data to the OS even with cache=unsafe */ 4435bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_OS); 4436eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_os) { 4437eb489bb1SKevin Wolf ret = bs->drv->bdrv_co_flush_to_os(bs); 4438eb489bb1SKevin Wolf if (ret < 0) { 4439eb489bb1SKevin Wolf return ret; 4440eb489bb1SKevin Wolf } 4441eb489bb1SKevin Wolf } 4442eb489bb1SKevin Wolf 4443ca716364SKevin Wolf /* But don't actually force it to the disk with cache=unsafe */ 4444ca716364SKevin Wolf if (bs->open_flags & BDRV_O_NO_FLUSH) { 4445d4c82329SKevin Wolf goto flush_parent; 4446ca716364SKevin Wolf } 4447ca716364SKevin Wolf 4448bf736fe3SKevin Wolf BLKDBG_EVENT(bs->file, BLKDBG_FLUSH_TO_DISK); 4449eb489bb1SKevin Wolf if (bs->drv->bdrv_co_flush_to_disk) { 445029cdb251SPaolo Bonzini ret = bs->drv->bdrv_co_flush_to_disk(bs); 445107f07615SPaolo Bonzini } else if (bs->drv->bdrv_aio_flush) { 445207f07615SPaolo Bonzini BlockDriverAIOCB *acb; 4453e7a8a783SKevin Wolf CoroutineIOCompletion co = { 4454e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 4455e7a8a783SKevin Wolf }; 4456e7a8a783SKevin Wolf 445707f07615SPaolo Bonzini acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 445807f07615SPaolo Bonzini if (acb == NULL) { 445929cdb251SPaolo Bonzini ret = -EIO; 446007f07615SPaolo Bonzini } else { 4461e7a8a783SKevin Wolf qemu_coroutine_yield(); 446229cdb251SPaolo Bonzini ret = co.ret; 4463e7a8a783SKevin Wolf } 446407f07615SPaolo Bonzini } else { 446507f07615SPaolo Bonzini /* 446607f07615SPaolo Bonzini * Some block drivers always operate in either writethrough or unsafe 446707f07615SPaolo Bonzini * mode and don't support bdrv_flush therefore. Usually qemu doesn't 446807f07615SPaolo Bonzini * know how the server works (because the behaviour is hardcoded or 446907f07615SPaolo Bonzini * depends on server-side configuration), so we can't ensure that 447007f07615SPaolo Bonzini * everything is safe on disk. Returning an error doesn't work because 447107f07615SPaolo Bonzini * that would break guests even if the server operates in writethrough 447207f07615SPaolo Bonzini * mode. 447307f07615SPaolo Bonzini * 447407f07615SPaolo Bonzini * Let's hope the user knows what he's doing. 447507f07615SPaolo Bonzini */ 447629cdb251SPaolo Bonzini ret = 0; 447707f07615SPaolo Bonzini } 447829cdb251SPaolo Bonzini if (ret < 0) { 447929cdb251SPaolo Bonzini return ret; 448029cdb251SPaolo Bonzini } 448129cdb251SPaolo Bonzini 448229cdb251SPaolo Bonzini /* Now flush the underlying protocol. It will also have BDRV_O_NO_FLUSH 448329cdb251SPaolo Bonzini * in the case of cache=unsafe, so there are no useless flushes. 448429cdb251SPaolo Bonzini */ 4485d4c82329SKevin Wolf flush_parent: 448629cdb251SPaolo Bonzini return bdrv_co_flush(bs->file); 448707f07615SPaolo Bonzini } 448807f07615SPaolo Bonzini 44890f15423cSAnthony Liguori void bdrv_invalidate_cache(BlockDriverState *bs) 44900f15423cSAnthony Liguori { 44910f15423cSAnthony Liguori if (bs->drv && bs->drv->bdrv_invalidate_cache) { 44920f15423cSAnthony Liguori bs->drv->bdrv_invalidate_cache(bs); 44930f15423cSAnthony Liguori } 44940f15423cSAnthony Liguori } 44950f15423cSAnthony Liguori 44960f15423cSAnthony Liguori void bdrv_invalidate_cache_all(void) 44970f15423cSAnthony Liguori { 44980f15423cSAnthony Liguori BlockDriverState *bs; 44990f15423cSAnthony Liguori 4500dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 45010f15423cSAnthony Liguori bdrv_invalidate_cache(bs); 45020f15423cSAnthony Liguori } 45030f15423cSAnthony Liguori } 45040f15423cSAnthony Liguori 450507789269SBenoît Canet void bdrv_clear_incoming_migration_all(void) 450607789269SBenoît Canet { 450707789269SBenoît Canet BlockDriverState *bs; 450807789269SBenoît Canet 4509dc364f4cSBenoît Canet QTAILQ_FOREACH(bs, &bdrv_states, device_list) { 451007789269SBenoît Canet bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING); 451107789269SBenoît Canet } 451207789269SBenoît Canet } 451307789269SBenoît Canet 451407f07615SPaolo Bonzini int bdrv_flush(BlockDriverState *bs) 451507f07615SPaolo Bonzini { 451607f07615SPaolo Bonzini Coroutine *co; 451707f07615SPaolo Bonzini RwCo rwco = { 451807f07615SPaolo Bonzini .bs = bs, 451907f07615SPaolo Bonzini .ret = NOT_DONE, 452007f07615SPaolo Bonzini }; 452107f07615SPaolo Bonzini 452207f07615SPaolo Bonzini if (qemu_in_coroutine()) { 452307f07615SPaolo Bonzini /* Fast-path if already in coroutine context */ 452407f07615SPaolo Bonzini bdrv_flush_co_entry(&rwco); 452507f07615SPaolo Bonzini } else { 452607f07615SPaolo Bonzini co = qemu_coroutine_create(bdrv_flush_co_entry); 452707f07615SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 452807f07615SPaolo Bonzini while (rwco.ret == NOT_DONE) { 452907f07615SPaolo Bonzini qemu_aio_wait(); 453007f07615SPaolo Bonzini } 453107f07615SPaolo Bonzini } 453207f07615SPaolo Bonzini 453307f07615SPaolo Bonzini return rwco.ret; 453407f07615SPaolo Bonzini } 4535e7a8a783SKevin Wolf 45364265d620SPaolo Bonzini static void coroutine_fn bdrv_discard_co_entry(void *opaque) 45374265d620SPaolo Bonzini { 45384265d620SPaolo Bonzini RwCo *rwco = opaque; 45394265d620SPaolo Bonzini 45404265d620SPaolo Bonzini rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors); 45414265d620SPaolo Bonzini } 45424265d620SPaolo Bonzini 45436f14da52SPeter Lieven /* if no limit is specified in the BlockLimits use a default 45446f14da52SPeter Lieven * of 32768 512-byte sectors (16 MiB) per request. 45456f14da52SPeter Lieven */ 45466f14da52SPeter Lieven #define MAX_DISCARD_DEFAULT 32768 45476f14da52SPeter Lieven 45484265d620SPaolo Bonzini int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num, 45494265d620SPaolo Bonzini int nb_sectors) 45504265d620SPaolo Bonzini { 4551d51e9fe5SPaolo Bonzini int max_discard; 4552d51e9fe5SPaolo Bonzini 45534265d620SPaolo Bonzini if (!bs->drv) { 45544265d620SPaolo Bonzini return -ENOMEDIUM; 45554265d620SPaolo Bonzini } else if (bdrv_check_request(bs, sector_num, nb_sectors)) { 45564265d620SPaolo Bonzini return -EIO; 45574265d620SPaolo Bonzini } else if (bs->read_only) { 45584265d620SPaolo Bonzini return -EROFS; 4559df702c9bSPaolo Bonzini } 4560df702c9bSPaolo Bonzini 45618f0720ecSPaolo Bonzini bdrv_reset_dirty(bs, sector_num, nb_sectors); 4562df702c9bSPaolo Bonzini 45639e8f1835SPaolo Bonzini /* Do nothing if disabled. */ 45649e8f1835SPaolo Bonzini if (!(bs->open_flags & BDRV_O_UNMAP)) { 45659e8f1835SPaolo Bonzini return 0; 45669e8f1835SPaolo Bonzini } 45679e8f1835SPaolo Bonzini 4568d51e9fe5SPaolo Bonzini if (!bs->drv->bdrv_co_discard && !bs->drv->bdrv_aio_discard) { 4569d51e9fe5SPaolo Bonzini return 0; 4570d51e9fe5SPaolo Bonzini } 45716f14da52SPeter Lieven 4572d51e9fe5SPaolo Bonzini max_discard = bs->bl.max_discard ? bs->bl.max_discard : MAX_DISCARD_DEFAULT; 45736f14da52SPeter Lieven while (nb_sectors > 0) { 45746f14da52SPeter Lieven int ret; 45756f14da52SPeter Lieven int num = nb_sectors; 45766f14da52SPeter Lieven 45776f14da52SPeter Lieven /* align request */ 45786f14da52SPeter Lieven if (bs->bl.discard_alignment && 45796f14da52SPeter Lieven num >= bs->bl.discard_alignment && 45806f14da52SPeter Lieven sector_num % bs->bl.discard_alignment) { 45816f14da52SPeter Lieven if (num > bs->bl.discard_alignment) { 45826f14da52SPeter Lieven num = bs->bl.discard_alignment; 45836f14da52SPeter Lieven } 45846f14da52SPeter Lieven num -= sector_num % bs->bl.discard_alignment; 45856f14da52SPeter Lieven } 45866f14da52SPeter Lieven 45876f14da52SPeter Lieven /* limit request size */ 45886f14da52SPeter Lieven if (num > max_discard) { 45896f14da52SPeter Lieven num = max_discard; 45906f14da52SPeter Lieven } 45916f14da52SPeter Lieven 4592d51e9fe5SPaolo Bonzini if (bs->drv->bdrv_co_discard) { 45936f14da52SPeter Lieven ret = bs->drv->bdrv_co_discard(bs, sector_num, num); 4594d51e9fe5SPaolo Bonzini } else { 45954265d620SPaolo Bonzini BlockDriverAIOCB *acb; 45964265d620SPaolo Bonzini CoroutineIOCompletion co = { 45974265d620SPaolo Bonzini .coroutine = qemu_coroutine_self(), 45984265d620SPaolo Bonzini }; 45994265d620SPaolo Bonzini 46004265d620SPaolo Bonzini acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors, 46014265d620SPaolo Bonzini bdrv_co_io_em_complete, &co); 46024265d620SPaolo Bonzini if (acb == NULL) { 46034265d620SPaolo Bonzini return -EIO; 46044265d620SPaolo Bonzini } else { 46054265d620SPaolo Bonzini qemu_coroutine_yield(); 4606d51e9fe5SPaolo Bonzini ret = co.ret; 46074265d620SPaolo Bonzini } 4608d51e9fe5SPaolo Bonzini } 46097ce21016SPaolo Bonzini if (ret && ret != -ENOTSUP) { 4610d51e9fe5SPaolo Bonzini return ret; 4611d51e9fe5SPaolo Bonzini } 4612d51e9fe5SPaolo Bonzini 4613d51e9fe5SPaolo Bonzini sector_num += num; 4614d51e9fe5SPaolo Bonzini nb_sectors -= num; 4615d51e9fe5SPaolo Bonzini } 46164265d620SPaolo Bonzini return 0; 46174265d620SPaolo Bonzini } 46184265d620SPaolo Bonzini 46194265d620SPaolo Bonzini int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 46204265d620SPaolo Bonzini { 46214265d620SPaolo Bonzini Coroutine *co; 46224265d620SPaolo Bonzini RwCo rwco = { 46234265d620SPaolo Bonzini .bs = bs, 46244265d620SPaolo Bonzini .sector_num = sector_num, 46254265d620SPaolo Bonzini .nb_sectors = nb_sectors, 46264265d620SPaolo Bonzini .ret = NOT_DONE, 46274265d620SPaolo Bonzini }; 46284265d620SPaolo Bonzini 46294265d620SPaolo Bonzini if (qemu_in_coroutine()) { 46304265d620SPaolo Bonzini /* Fast-path if already in coroutine context */ 46314265d620SPaolo Bonzini bdrv_discard_co_entry(&rwco); 46324265d620SPaolo Bonzini } else { 46334265d620SPaolo Bonzini co = qemu_coroutine_create(bdrv_discard_co_entry); 46344265d620SPaolo Bonzini qemu_coroutine_enter(co, &rwco); 46354265d620SPaolo Bonzini while (rwco.ret == NOT_DONE) { 46364265d620SPaolo Bonzini qemu_aio_wait(); 46374265d620SPaolo Bonzini } 46384265d620SPaolo Bonzini } 46394265d620SPaolo Bonzini 46404265d620SPaolo Bonzini return rwco.ret; 46414265d620SPaolo Bonzini } 46424265d620SPaolo Bonzini 4643f9f05dc5SKevin Wolf /**************************************************************/ 464419cb3738Sbellard /* removable device support */ 464519cb3738Sbellard 464619cb3738Sbellard /** 464719cb3738Sbellard * Return TRUE if the media is present 464819cb3738Sbellard */ 464919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 465019cb3738Sbellard { 465119cb3738Sbellard BlockDriver *drv = bs->drv; 4652a1aff5bfSMarkus Armbruster 465319cb3738Sbellard if (!drv) 465419cb3738Sbellard return 0; 465519cb3738Sbellard if (!drv->bdrv_is_inserted) 4656a1aff5bfSMarkus Armbruster return 1; 4657a1aff5bfSMarkus Armbruster return drv->bdrv_is_inserted(bs); 465819cb3738Sbellard } 465919cb3738Sbellard 466019cb3738Sbellard /** 46618e49ca46SMarkus Armbruster * Return whether the media changed since the last call to this 46628e49ca46SMarkus Armbruster * function, or -ENOTSUP if we don't know. Most drivers don't know. 466319cb3738Sbellard */ 466419cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 466519cb3738Sbellard { 466619cb3738Sbellard BlockDriver *drv = bs->drv; 466719cb3738Sbellard 46688e49ca46SMarkus Armbruster if (drv && drv->bdrv_media_changed) { 46698e49ca46SMarkus Armbruster return drv->bdrv_media_changed(bs); 46708e49ca46SMarkus Armbruster } 46718e49ca46SMarkus Armbruster return -ENOTSUP; 467219cb3738Sbellard } 467319cb3738Sbellard 467419cb3738Sbellard /** 467519cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 467619cb3738Sbellard */ 4677f36f3949SLuiz Capitulino void bdrv_eject(BlockDriverState *bs, bool eject_flag) 467819cb3738Sbellard { 467919cb3738Sbellard BlockDriver *drv = bs->drv; 468019cb3738Sbellard 4681822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 4682822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 468319cb3738Sbellard } 46846f382ed2SLuiz Capitulino 46856f382ed2SLuiz Capitulino if (bs->device_name[0] != '\0') { 46866f382ed2SLuiz Capitulino bdrv_emit_qmp_eject_event(bs, eject_flag); 46876f382ed2SLuiz Capitulino } 468819cb3738Sbellard } 468919cb3738Sbellard 469019cb3738Sbellard /** 469119cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 469219cb3738Sbellard * to eject it manually). 469319cb3738Sbellard */ 4694025e849aSMarkus Armbruster void bdrv_lock_medium(BlockDriverState *bs, bool locked) 469519cb3738Sbellard { 469619cb3738Sbellard BlockDriver *drv = bs->drv; 469719cb3738Sbellard 4698025e849aSMarkus Armbruster trace_bdrv_lock_medium(bs, locked); 4699b8c6d095SStefan Hajnoczi 4700025e849aSMarkus Armbruster if (drv && drv->bdrv_lock_medium) { 4701025e849aSMarkus Armbruster drv->bdrv_lock_medium(bs, locked); 470219cb3738Sbellard } 470319cb3738Sbellard } 4704985a03b0Sths 4705985a03b0Sths /* needed for generic scsi interface */ 4706985a03b0Sths 4707985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 4708985a03b0Sths { 4709985a03b0Sths BlockDriver *drv = bs->drv; 4710985a03b0Sths 4711985a03b0Sths if (drv && drv->bdrv_ioctl) 4712985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 4713985a03b0Sths return -ENOTSUP; 4714985a03b0Sths } 47157d780669Saliguori 4716221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 4717221f715dSaliguori unsigned long int req, void *buf, 47187d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 47197d780669Saliguori { 4720221f715dSaliguori BlockDriver *drv = bs->drv; 47217d780669Saliguori 4722221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 4723221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 4724221f715dSaliguori return NULL; 47257d780669Saliguori } 4726e268ca52Saliguori 47277b6f9300SMarkus Armbruster void bdrv_set_buffer_alignment(BlockDriverState *bs, int align) 47287b6f9300SMarkus Armbruster { 47297b6f9300SMarkus Armbruster bs->buffer_alignment = align; 47307b6f9300SMarkus Armbruster } 47317cd1e32aSlirans@il.ibm.com 4732e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 4733e268ca52Saliguori { 4734e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 4735e268ca52Saliguori } 47367cd1e32aSlirans@il.ibm.com 4737c53b1c51SStefan Hajnoczi /* 4738c53b1c51SStefan Hajnoczi * Check if all memory in this vector is sector aligned. 4739c53b1c51SStefan Hajnoczi */ 4740c53b1c51SStefan Hajnoczi bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov) 4741c53b1c51SStefan Hajnoczi { 4742c53b1c51SStefan Hajnoczi int i; 4743c53b1c51SStefan Hajnoczi 4744c53b1c51SStefan Hajnoczi for (i = 0; i < qiov->niov; i++) { 4745c53b1c51SStefan Hajnoczi if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) { 4746c53b1c51SStefan Hajnoczi return false; 4747c53b1c51SStefan Hajnoczi } 4748c53b1c51SStefan Hajnoczi } 4749c53b1c51SStefan Hajnoczi 4750c53b1c51SStefan Hajnoczi return true; 4751c53b1c51SStefan Hajnoczi } 4752c53b1c51SStefan Hajnoczi 4753e4654d2dSFam Zheng BdrvDirtyBitmap *bdrv_create_dirty_bitmap(BlockDriverState *bs, int granularity) 47547cd1e32aSlirans@il.ibm.com { 47557cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 4756e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4757a55eb92cSJan Kiszka 475850717e94SPaolo Bonzini assert((granularity & (granularity - 1)) == 0); 475950717e94SPaolo Bonzini 476050717e94SPaolo Bonzini granularity >>= BDRV_SECTOR_BITS; 4761e4654d2dSFam Zheng assert(granularity); 47628f0720ecSPaolo Bonzini bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS); 4763e4654d2dSFam Zheng bitmap = g_malloc0(sizeof(BdrvDirtyBitmap)); 4764e4654d2dSFam Zheng bitmap->bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1); 4765e4654d2dSFam Zheng QLIST_INSERT_HEAD(&bs->dirty_bitmaps, bitmap, list); 4766e4654d2dSFam Zheng return bitmap; 4767e4654d2dSFam Zheng } 4768e4654d2dSFam Zheng 4769e4654d2dSFam Zheng void bdrv_release_dirty_bitmap(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 4770e4654d2dSFam Zheng { 4771e4654d2dSFam Zheng BdrvDirtyBitmap *bm, *next; 4772e4654d2dSFam Zheng QLIST_FOREACH_SAFE(bm, &bs->dirty_bitmaps, list, next) { 4773e4654d2dSFam Zheng if (bm == bitmap) { 4774e4654d2dSFam Zheng QLIST_REMOVE(bitmap, list); 4775e4654d2dSFam Zheng hbitmap_free(bitmap->bitmap); 4776e4654d2dSFam Zheng g_free(bitmap); 4777e4654d2dSFam Zheng return; 47787cd1e32aSlirans@il.ibm.com } 47797cd1e32aSlirans@il.ibm.com } 47807cd1e32aSlirans@il.ibm.com } 47817cd1e32aSlirans@il.ibm.com 478221b56835SFam Zheng BlockDirtyInfoList *bdrv_query_dirty_bitmaps(BlockDriverState *bs) 478321b56835SFam Zheng { 478421b56835SFam Zheng BdrvDirtyBitmap *bm; 478521b56835SFam Zheng BlockDirtyInfoList *list = NULL; 478621b56835SFam Zheng BlockDirtyInfoList **plist = &list; 478721b56835SFam Zheng 478821b56835SFam Zheng QLIST_FOREACH(bm, &bs->dirty_bitmaps, list) { 478921b56835SFam Zheng BlockDirtyInfo *info = g_malloc0(sizeof(BlockDirtyInfo)); 479021b56835SFam Zheng BlockDirtyInfoList *entry = g_malloc0(sizeof(BlockDirtyInfoList)); 479121b56835SFam Zheng info->count = bdrv_get_dirty_count(bs, bm); 479221b56835SFam Zheng info->granularity = 479321b56835SFam Zheng ((int64_t) BDRV_SECTOR_SIZE << hbitmap_granularity(bm->bitmap)); 479421b56835SFam Zheng entry->value = info; 479521b56835SFam Zheng *plist = entry; 479621b56835SFam Zheng plist = &entry->next; 479721b56835SFam Zheng } 479821b56835SFam Zheng 479921b56835SFam Zheng return list; 480021b56835SFam Zheng } 480121b56835SFam Zheng 4802e4654d2dSFam Zheng int bdrv_get_dirty(BlockDriverState *bs, BdrvDirtyBitmap *bitmap, int64_t sector) 48037cd1e32aSlirans@il.ibm.com { 4804e4654d2dSFam Zheng if (bitmap) { 4805e4654d2dSFam Zheng return hbitmap_get(bitmap->bitmap, sector); 48067cd1e32aSlirans@il.ibm.com } else { 48077cd1e32aSlirans@il.ibm.com return 0; 48087cd1e32aSlirans@il.ibm.com } 48097cd1e32aSlirans@il.ibm.com } 48107cd1e32aSlirans@il.ibm.com 4811e4654d2dSFam Zheng void bdrv_dirty_iter_init(BlockDriverState *bs, 4812e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap, HBitmapIter *hbi) 48131755da16SPaolo Bonzini { 4814e4654d2dSFam Zheng hbitmap_iter_init(hbi, bitmap->bitmap, 0); 48151755da16SPaolo Bonzini } 48161755da16SPaolo Bonzini 48171755da16SPaolo Bonzini void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector, 48181755da16SPaolo Bonzini int nr_sectors) 48191755da16SPaolo Bonzini { 4820e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4821e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 4822e4654d2dSFam Zheng hbitmap_set(bitmap->bitmap, cur_sector, nr_sectors); 4823e4654d2dSFam Zheng } 48241755da16SPaolo Bonzini } 48251755da16SPaolo Bonzini 4826e4654d2dSFam Zheng void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, int nr_sectors) 48277cd1e32aSlirans@il.ibm.com { 4828e4654d2dSFam Zheng BdrvDirtyBitmap *bitmap; 4829e4654d2dSFam Zheng QLIST_FOREACH(bitmap, &bs->dirty_bitmaps, list) { 4830e4654d2dSFam Zheng hbitmap_reset(bitmap->bitmap, cur_sector, nr_sectors); 4831e4654d2dSFam Zheng } 48327cd1e32aSlirans@il.ibm.com } 4833aaa0eb75SLiran Schour 4834e4654d2dSFam Zheng int64_t bdrv_get_dirty_count(BlockDriverState *bs, BdrvDirtyBitmap *bitmap) 4835aaa0eb75SLiran Schour { 4836e4654d2dSFam Zheng return hbitmap_count(bitmap->bitmap); 4837aaa0eb75SLiran Schour } 4838f88e1a42SJes Sorensen 48399fcb0251SFam Zheng /* Get a reference to bs */ 48409fcb0251SFam Zheng void bdrv_ref(BlockDriverState *bs) 48419fcb0251SFam Zheng { 48429fcb0251SFam Zheng bs->refcnt++; 48439fcb0251SFam Zheng } 48449fcb0251SFam Zheng 48459fcb0251SFam Zheng /* Release a previously grabbed reference to bs. 48469fcb0251SFam Zheng * If after releasing, reference count is zero, the BlockDriverState is 48479fcb0251SFam Zheng * deleted. */ 48489fcb0251SFam Zheng void bdrv_unref(BlockDriverState *bs) 48499fcb0251SFam Zheng { 48509fcb0251SFam Zheng assert(bs->refcnt > 0); 48519fcb0251SFam Zheng if (--bs->refcnt == 0) { 48529fcb0251SFam Zheng bdrv_delete(bs); 48539fcb0251SFam Zheng } 48549fcb0251SFam Zheng } 48559fcb0251SFam Zheng 4856db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 4857db593f25SMarcelo Tosatti { 4858db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 4859db593f25SMarcelo Tosatti bs->in_use = in_use; 4860db593f25SMarcelo Tosatti } 4861db593f25SMarcelo Tosatti 4862db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 4863db593f25SMarcelo Tosatti { 4864db593f25SMarcelo Tosatti return bs->in_use; 4865db593f25SMarcelo Tosatti } 4866db593f25SMarcelo Tosatti 486728a7282aSLuiz Capitulino void bdrv_iostatus_enable(BlockDriverState *bs) 486828a7282aSLuiz Capitulino { 4869d6bf279eSLuiz Capitulino bs->iostatus_enabled = true; 487058e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 487128a7282aSLuiz Capitulino } 487228a7282aSLuiz Capitulino 487328a7282aSLuiz Capitulino /* The I/O status is only enabled if the drive explicitly 487428a7282aSLuiz Capitulino * enables it _and_ the VM is configured to stop on errors */ 487528a7282aSLuiz Capitulino bool bdrv_iostatus_is_enabled(const BlockDriverState *bs) 487628a7282aSLuiz Capitulino { 4877d6bf279eSLuiz Capitulino return (bs->iostatus_enabled && 487892aa5c6dSPaolo Bonzini (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC || 487992aa5c6dSPaolo Bonzini bs->on_write_error == BLOCKDEV_ON_ERROR_STOP || 488092aa5c6dSPaolo Bonzini bs->on_read_error == BLOCKDEV_ON_ERROR_STOP)); 488128a7282aSLuiz Capitulino } 488228a7282aSLuiz Capitulino 488328a7282aSLuiz Capitulino void bdrv_iostatus_disable(BlockDriverState *bs) 488428a7282aSLuiz Capitulino { 4885d6bf279eSLuiz Capitulino bs->iostatus_enabled = false; 488628a7282aSLuiz Capitulino } 488728a7282aSLuiz Capitulino 488828a7282aSLuiz Capitulino void bdrv_iostatus_reset(BlockDriverState *bs) 488928a7282aSLuiz Capitulino { 489028a7282aSLuiz Capitulino if (bdrv_iostatus_is_enabled(bs)) { 489158e21ef5SLuiz Capitulino bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK; 48923bd293c3SPaolo Bonzini if (bs->job) { 48933bd293c3SPaolo Bonzini block_job_iostatus_reset(bs->job); 48943bd293c3SPaolo Bonzini } 489528a7282aSLuiz Capitulino } 489628a7282aSLuiz Capitulino } 489728a7282aSLuiz Capitulino 489828a7282aSLuiz Capitulino void bdrv_iostatus_set_err(BlockDriverState *bs, int error) 489928a7282aSLuiz Capitulino { 49003e1caa5fSPaolo Bonzini assert(bdrv_iostatus_is_enabled(bs)); 49013e1caa5fSPaolo Bonzini if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) { 490258e21ef5SLuiz Capitulino bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE : 490358e21ef5SLuiz Capitulino BLOCK_DEVICE_IO_STATUS_FAILED; 490428a7282aSLuiz Capitulino } 490528a7282aSLuiz Capitulino } 490628a7282aSLuiz Capitulino 4907a597e79cSChristoph Hellwig void 4908a597e79cSChristoph Hellwig bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes, 4909a597e79cSChristoph Hellwig enum BlockAcctType type) 4910a597e79cSChristoph Hellwig { 4911a597e79cSChristoph Hellwig assert(type < BDRV_MAX_IOTYPE); 4912a597e79cSChristoph Hellwig 4913a597e79cSChristoph Hellwig cookie->bytes = bytes; 4914c488c7f6SChristoph Hellwig cookie->start_time_ns = get_clock(); 4915a597e79cSChristoph Hellwig cookie->type = type; 4916a597e79cSChristoph Hellwig } 4917a597e79cSChristoph Hellwig 4918a597e79cSChristoph Hellwig void 4919a597e79cSChristoph Hellwig bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie) 4920a597e79cSChristoph Hellwig { 4921a597e79cSChristoph Hellwig assert(cookie->type < BDRV_MAX_IOTYPE); 4922a597e79cSChristoph Hellwig 4923a597e79cSChristoph Hellwig bs->nr_bytes[cookie->type] += cookie->bytes; 4924a597e79cSChristoph Hellwig bs->nr_ops[cookie->type]++; 4925c488c7f6SChristoph Hellwig bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns; 4926a597e79cSChristoph Hellwig } 4927a597e79cSChristoph Hellwig 4928d92ada22SLuiz Capitulino void bdrv_img_create(const char *filename, const char *fmt, 4929f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 4930f382d43aSMiroslav Rezanina char *options, uint64_t img_size, int flags, 4931f382d43aSMiroslav Rezanina Error **errp, bool quiet) 4932f88e1a42SJes Sorensen { 4933f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 4934d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 4935f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 493696df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 4937cc84d90fSMax Reitz Error *local_err = NULL; 4938f88e1a42SJes Sorensen int ret = 0; 4939f88e1a42SJes Sorensen 4940f88e1a42SJes Sorensen /* Find driver and parse its options */ 4941f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 4942f88e1a42SJes Sorensen if (!drv) { 494371c79813SLuiz Capitulino error_setg(errp, "Unknown file format '%s'", fmt); 4944d92ada22SLuiz Capitulino return; 4945f88e1a42SJes Sorensen } 4946f88e1a42SJes Sorensen 494798289620SKevin Wolf proto_drv = bdrv_find_protocol(filename, true); 4948f88e1a42SJes Sorensen if (!proto_drv) { 494971c79813SLuiz Capitulino error_setg(errp, "Unknown protocol '%s'", filename); 4950d92ada22SLuiz Capitulino return; 4951f88e1a42SJes Sorensen } 4952f88e1a42SJes Sorensen 4953f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4954f88e1a42SJes Sorensen drv->create_options); 4955f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 4956f88e1a42SJes Sorensen proto_drv->create_options); 4957f88e1a42SJes Sorensen 4958f88e1a42SJes Sorensen /* Create parameter list with default values */ 4959f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 4960f88e1a42SJes Sorensen 4961f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 4962f88e1a42SJes Sorensen 4963f88e1a42SJes Sorensen /* Parse -o options */ 4964f88e1a42SJes Sorensen if (options) { 4965f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 4966f88e1a42SJes Sorensen if (param == NULL) { 496771c79813SLuiz Capitulino error_setg(errp, "Invalid options for file format '%s'.", fmt); 4968f88e1a42SJes Sorensen goto out; 4969f88e1a42SJes Sorensen } 4970f88e1a42SJes Sorensen } 4971f88e1a42SJes Sorensen 4972f88e1a42SJes Sorensen if (base_filename) { 4973f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 4974f88e1a42SJes Sorensen base_filename)) { 497571c79813SLuiz Capitulino error_setg(errp, "Backing file not supported for file format '%s'", 497671c79813SLuiz Capitulino fmt); 4977f88e1a42SJes Sorensen goto out; 4978f88e1a42SJes Sorensen } 4979f88e1a42SJes Sorensen } 4980f88e1a42SJes Sorensen 4981f88e1a42SJes Sorensen if (base_fmt) { 4982f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 498371c79813SLuiz Capitulino error_setg(errp, "Backing file format not supported for file " 498471c79813SLuiz Capitulino "format '%s'", fmt); 4985f88e1a42SJes Sorensen goto out; 4986f88e1a42SJes Sorensen } 4987f88e1a42SJes Sorensen } 4988f88e1a42SJes Sorensen 4989792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 4990792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 4991792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 499271c79813SLuiz Capitulino error_setg(errp, "Error: Trying to create an image with the " 499371c79813SLuiz Capitulino "same filename as the backing file"); 4994792da93aSJes Sorensen goto out; 4995792da93aSJes Sorensen } 4996792da93aSJes Sorensen } 4997792da93aSJes Sorensen 4998f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 4999f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 500096df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 500196df67d1SStefan Hajnoczi if (!backing_drv) { 500271c79813SLuiz Capitulino error_setg(errp, "Unknown backing file format '%s'", 500371c79813SLuiz Capitulino backing_fmt->value.s); 5004f88e1a42SJes Sorensen goto out; 5005f88e1a42SJes Sorensen } 5006f88e1a42SJes Sorensen } 5007f88e1a42SJes Sorensen 5008f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 5009f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 5010d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 5011d220894eSKevin Wolf if (size && size->value.n == -1) { 5012f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 501366f6b814SMax Reitz BlockDriverState *bs; 5014f88e1a42SJes Sorensen uint64_t size; 5015f88e1a42SJes Sorensen char buf[32]; 501663090dacSPaolo Bonzini int back_flags; 501763090dacSPaolo Bonzini 501863090dacSPaolo Bonzini /* backing files always opened read-only */ 501963090dacSPaolo Bonzini back_flags = 502063090dacSPaolo Bonzini flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 5021f88e1a42SJes Sorensen 5022f88e1a42SJes Sorensen bs = bdrv_new(""); 5023f88e1a42SJes Sorensen 5024de9c0cecSKevin Wolf ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags, 5025cc84d90fSMax Reitz backing_drv, &local_err); 5026f88e1a42SJes Sorensen if (ret < 0) { 5027cc84d90fSMax Reitz error_setg_errno(errp, -ret, "Could not open '%s': %s", 5028cc84d90fSMax Reitz backing_file->value.s, 5029cc84d90fSMax Reitz error_get_pretty(local_err)); 5030cc84d90fSMax Reitz error_free(local_err); 5031cc84d90fSMax Reitz local_err = NULL; 503266f6b814SMax Reitz bdrv_unref(bs); 5033f88e1a42SJes Sorensen goto out; 5034f88e1a42SJes Sorensen } 5035f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 5036f88e1a42SJes Sorensen size *= 512; 5037f88e1a42SJes Sorensen 5038f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 5039f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 504066f6b814SMax Reitz 504166f6b814SMax Reitz bdrv_unref(bs); 5042f88e1a42SJes Sorensen } else { 504371c79813SLuiz Capitulino error_setg(errp, "Image creation needs a size parameter"); 5044f88e1a42SJes Sorensen goto out; 5045f88e1a42SJes Sorensen } 5046f88e1a42SJes Sorensen } 5047f88e1a42SJes Sorensen 5048f382d43aSMiroslav Rezanina if (!quiet) { 5049f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 5050f88e1a42SJes Sorensen print_option_parameters(param); 5051f88e1a42SJes Sorensen puts(""); 5052f382d43aSMiroslav Rezanina } 5053cc84d90fSMax Reitz ret = bdrv_create(drv, filename, param, &local_err); 5054cc84d90fSMax Reitz if (ret == -EFBIG) { 5055cc84d90fSMax Reitz /* This is generally a better message than whatever the driver would 5056cc84d90fSMax Reitz * deliver (especially because of the cluster_size_hint), since that 5057cc84d90fSMax Reitz * is most probably not much different from "image too large". */ 5058f3f4d2c0SKevin Wolf const char *cluster_size_hint = ""; 5059f3f4d2c0SKevin Wolf if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) { 5060f3f4d2c0SKevin Wolf cluster_size_hint = " (try using a larger cluster size)"; 5061f3f4d2c0SKevin Wolf } 5062cc84d90fSMax Reitz error_setg(errp, "The image size is too large for file format '%s'" 5063cc84d90fSMax Reitz "%s", fmt, cluster_size_hint); 5064cc84d90fSMax Reitz error_free(local_err); 5065cc84d90fSMax Reitz local_err = NULL; 5066f88e1a42SJes Sorensen } 5067f88e1a42SJes Sorensen 5068f88e1a42SJes Sorensen out: 5069f88e1a42SJes Sorensen free_option_parameters(create_options); 5070f88e1a42SJes Sorensen free_option_parameters(param); 5071f88e1a42SJes Sorensen 5072cc84d90fSMax Reitz if (error_is_set(&local_err)) { 5073cc84d90fSMax Reitz error_propagate(errp, local_err); 5074cc84d90fSMax Reitz } 5075f88e1a42SJes Sorensen } 507685d126f3SStefan Hajnoczi 507785d126f3SStefan Hajnoczi AioContext *bdrv_get_aio_context(BlockDriverState *bs) 507885d126f3SStefan Hajnoczi { 507985d126f3SStefan Hajnoczi /* Currently BlockDriverState always uses the main loop AioContext */ 508085d126f3SStefan Hajnoczi return qemu_get_aio_context(); 508185d126f3SStefan Hajnoczi } 5082d616b224SStefan Hajnoczi 5083d616b224SStefan Hajnoczi void bdrv_add_before_write_notifier(BlockDriverState *bs, 5084d616b224SStefan Hajnoczi NotifierWithReturn *notifier) 5085d616b224SStefan Hajnoczi { 5086d616b224SStefan Hajnoczi notifier_with_return_list_add(&bs->before_write_notifiers, notifier); 5087d616b224SStefan Hajnoczi } 50886f176b48SMax Reitz 50896f176b48SMax Reitz int bdrv_amend_options(BlockDriverState *bs, QEMUOptionParameter *options) 50906f176b48SMax Reitz { 50916f176b48SMax Reitz if (bs->drv->bdrv_amend_options == NULL) { 50926f176b48SMax Reitz return -ENOTSUP; 50936f176b48SMax Reitz } 50946f176b48SMax Reitz return bs->drv->bdrv_amend_options(bs, options); 50956f176b48SMax Reitz } 5096f6186f49SBenoît Canet 5097f6186f49SBenoît Canet ExtSnapshotPerm bdrv_check_ext_snapshot(BlockDriverState *bs) 5098f6186f49SBenoît Canet { 5099f6186f49SBenoît Canet if (bs->drv->bdrv_check_ext_snapshot) { 5100f6186f49SBenoît Canet return bs->drv->bdrv_check_ext_snapshot(bs); 5101f6186f49SBenoît Canet } 5102f6186f49SBenoît Canet 5103f6186f49SBenoît Canet if (bs->file && bs->file->drv && bs->file->drv->bdrv_check_ext_snapshot) { 5104f6186f49SBenoît Canet return bs->file->drv->bdrv_check_ext_snapshot(bs); 5105f6186f49SBenoît Canet } 5106f6186f49SBenoît Canet 5107f6186f49SBenoît Canet /* external snapshots are allowed by default */ 5108f6186f49SBenoît Canet return EXT_SNAPSHOT_ALLOWED; 5109f6186f49SBenoît Canet } 5110f6186f49SBenoît Canet 5111f6186f49SBenoît Canet ExtSnapshotPerm bdrv_check_ext_snapshot_forbidden(BlockDriverState *bs) 5112f6186f49SBenoît Canet { 5113f6186f49SBenoît Canet return EXT_SNAPSHOT_FORBIDDEN; 5114f6186f49SBenoît Canet } 5115