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" 27376253ecSaliguori #include "monitor.h" 28ea2384d3Sbellard #include "block_int.h" 295efa9d5aSAnthony Liguori #include "module.h" 30d15e5465SLuiz Capitulino #include "qemu-objects.h" 3168485420SKevin Wolf #include "qemu-coroutine.h" 32fc01f7e7Sbellard 3371e72a19SJuan Quintela #ifdef CONFIG_BSD 347674e7bfSbellard #include <sys/types.h> 357674e7bfSbellard #include <sys/stat.h> 367674e7bfSbellard #include <sys/ioctl.h> 3772cf2d4fSBlue Swirl #include <sys/queue.h> 38c5e97233Sblueswir1 #ifndef __DragonFly__ 397674e7bfSbellard #include <sys/disk.h> 407674e7bfSbellard #endif 41c5e97233Sblueswir1 #endif 427674e7bfSbellard 4349dc768dSaliguori #ifdef _WIN32 4449dc768dSaliguori #include <windows.h> 4549dc768dSaliguori #endif 4649dc768dSaliguori 47f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 48f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 49c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 50f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 51f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 52ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 53b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 54b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque); 55016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 56016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque); 5783f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 5883f64091Sbellard uint8_t *buf, int nb_sectors); 5983f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 6083f64091Sbellard const uint8_t *buf, int nb_sectors); 6168485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs, 6268485420SKevin Wolf int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 6368485420SKevin Wolf BlockDriverCompletionFunc *cb, void *opaque); 6468485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs, 6568485420SKevin Wolf int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 6668485420SKevin Wolf 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); 73*e7a8a783SKevin Wolf static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs); 74ec530c81Sbellard 751b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 761b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 777ee930d0Sblueswir1 788a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 798a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 80ea2384d3Sbellard 81f9092b10SMarkus Armbruster /* The device to use for VM snapshots */ 82f9092b10SMarkus Armbruster static BlockDriverState *bs_snapshots; 83f9092b10SMarkus Armbruster 84eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 85eb852011SMarkus Armbruster static int use_bdrv_whitelist; 86eb852011SMarkus Armbruster 879e0b22f4SStefan Hajnoczi #ifdef _WIN32 889e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 899e0b22f4SStefan Hajnoczi { 909e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 919e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 929e0b22f4SStefan Hajnoczi filename[1] == ':'); 939e0b22f4SStefan Hajnoczi } 949e0b22f4SStefan Hajnoczi 959e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 969e0b22f4SStefan Hajnoczi { 979e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 989e0b22f4SStefan Hajnoczi filename[2] == '\0') 999e0b22f4SStefan Hajnoczi return 1; 1009e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 1019e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 1029e0b22f4SStefan Hajnoczi return 1; 1039e0b22f4SStefan Hajnoczi return 0; 1049e0b22f4SStefan Hajnoczi } 1059e0b22f4SStefan Hajnoczi #endif 1069e0b22f4SStefan Hajnoczi 1079e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 1089e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 1099e0b22f4SStefan Hajnoczi { 1109e0b22f4SStefan Hajnoczi #ifdef _WIN32 1119e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 1129e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 1139e0b22f4SStefan Hajnoczi return 0; 1149e0b22f4SStefan Hajnoczi } 1159e0b22f4SStefan Hajnoczi #endif 1169e0b22f4SStefan Hajnoczi 1179e0b22f4SStefan Hajnoczi return strchr(path, ':') != NULL; 1189e0b22f4SStefan Hajnoczi } 1199e0b22f4SStefan Hajnoczi 12083f64091Sbellard int path_is_absolute(const char *path) 12183f64091Sbellard { 12283f64091Sbellard const char *p; 12321664424Sbellard #ifdef _WIN32 12421664424Sbellard /* specific case for names like: "\\.\d:" */ 12521664424Sbellard if (*path == '/' || *path == '\\') 12621664424Sbellard return 1; 12721664424Sbellard #endif 12883f64091Sbellard p = strchr(path, ':'); 12983f64091Sbellard if (p) 13083f64091Sbellard p++; 13183f64091Sbellard else 13283f64091Sbellard p = path; 1333b9f94e1Sbellard #ifdef _WIN32 1343b9f94e1Sbellard return (*p == '/' || *p == '\\'); 1353b9f94e1Sbellard #else 1363b9f94e1Sbellard return (*p == '/'); 1373b9f94e1Sbellard #endif 13883f64091Sbellard } 13983f64091Sbellard 14083f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 14183f64091Sbellard path to it by considering it is relative to base_path. URL are 14283f64091Sbellard supported. */ 14383f64091Sbellard void path_combine(char *dest, int dest_size, 14483f64091Sbellard const char *base_path, 14583f64091Sbellard const char *filename) 14683f64091Sbellard { 14783f64091Sbellard const char *p, *p1; 14883f64091Sbellard int len; 14983f64091Sbellard 15083f64091Sbellard if (dest_size <= 0) 15183f64091Sbellard return; 15283f64091Sbellard if (path_is_absolute(filename)) { 15383f64091Sbellard pstrcpy(dest, dest_size, filename); 15483f64091Sbellard } else { 15583f64091Sbellard p = strchr(base_path, ':'); 15683f64091Sbellard if (p) 15783f64091Sbellard p++; 15883f64091Sbellard else 15983f64091Sbellard p = base_path; 1603b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1613b9f94e1Sbellard #ifdef _WIN32 1623b9f94e1Sbellard { 1633b9f94e1Sbellard const char *p2; 1643b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1653b9f94e1Sbellard if (!p1 || p2 > p1) 1663b9f94e1Sbellard p1 = p2; 1673b9f94e1Sbellard } 1683b9f94e1Sbellard #endif 16983f64091Sbellard if (p1) 17083f64091Sbellard p1++; 17183f64091Sbellard else 17283f64091Sbellard p1 = base_path; 17383f64091Sbellard if (p1 > p) 17483f64091Sbellard p = p1; 17583f64091Sbellard len = p - base_path; 17683f64091Sbellard if (len > dest_size - 1) 17783f64091Sbellard len = dest_size - 1; 17883f64091Sbellard memcpy(dest, base_path, len); 17983f64091Sbellard dest[len] = '\0'; 18083f64091Sbellard pstrcat(dest, dest_size, filename); 18183f64091Sbellard } 18283f64091Sbellard } 18383f64091Sbellard 1845efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 185ea2384d3Sbellard { 18668485420SKevin Wolf if (bdrv->bdrv_co_readv) { 18768485420SKevin Wolf /* Emulate AIO by coroutines, and sync by AIO */ 18868485420SKevin Wolf bdrv->bdrv_aio_readv = bdrv_co_aio_readv_em; 18968485420SKevin Wolf bdrv->bdrv_aio_writev = bdrv_co_aio_writev_em; 19068485420SKevin Wolf bdrv->bdrv_read = bdrv_read_em; 19168485420SKevin Wolf bdrv->bdrv_write = bdrv_write_em; 192f9f05dc5SKevin Wolf } else { 193f9f05dc5SKevin Wolf bdrv->bdrv_co_readv = bdrv_co_readv_em; 194f9f05dc5SKevin Wolf bdrv->bdrv_co_writev = bdrv_co_writev_em; 195f9f05dc5SKevin Wolf 196f9f05dc5SKevin Wolf if (!bdrv->bdrv_aio_readv) { 19783f64091Sbellard /* add AIO emulation layer */ 198f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 199f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 200eda578e5Saliguori } else if (!bdrv->bdrv_read) { 20183f64091Sbellard /* add synchronous IO emulation layer */ 20283f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 20383f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 20483f64091Sbellard } 205f9f05dc5SKevin Wolf } 206b2e12bc6SChristoph Hellwig 207b2e12bc6SChristoph Hellwig if (!bdrv->bdrv_aio_flush) 208b2e12bc6SChristoph Hellwig bdrv->bdrv_aio_flush = bdrv_aio_flush_em; 209b2e12bc6SChristoph Hellwig 2108a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 211ea2384d3Sbellard } 212b338082bSbellard 213b338082bSbellard /* create a new block device (by default it is empty) */ 214b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 215fc01f7e7Sbellard { 2161b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 217b338082bSbellard 218b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 219b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 220ea2384d3Sbellard if (device_name[0] != '\0') { 2211b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 222ea2384d3Sbellard } 223b338082bSbellard return bs; 224b338082bSbellard } 225b338082bSbellard 226ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 227ea2384d3Sbellard { 228ea2384d3Sbellard BlockDriver *drv1; 2298a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 2308a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 231ea2384d3Sbellard return drv1; 232ea2384d3Sbellard } 2338a22f02aSStefan Hajnoczi } 234ea2384d3Sbellard return NULL; 235ea2384d3Sbellard } 236ea2384d3Sbellard 237eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 238eb852011SMarkus Armbruster { 239eb852011SMarkus Armbruster static const char *whitelist[] = { 240eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 241eb852011SMarkus Armbruster }; 242eb852011SMarkus Armbruster const char **p; 243eb852011SMarkus Armbruster 244eb852011SMarkus Armbruster if (!whitelist[0]) 245eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 246eb852011SMarkus Armbruster 247eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 248eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 249eb852011SMarkus Armbruster return 1; 250eb852011SMarkus Armbruster } 251eb852011SMarkus Armbruster } 252eb852011SMarkus Armbruster return 0; 253eb852011SMarkus Armbruster } 254eb852011SMarkus Armbruster 255eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 256eb852011SMarkus Armbruster { 257eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 258eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 259eb852011SMarkus Armbruster } 260eb852011SMarkus Armbruster 2610e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 2620e7e1989SKevin Wolf QEMUOptionParameter *options) 263ea2384d3Sbellard { 264ea2384d3Sbellard if (!drv->bdrv_create) 265ea2384d3Sbellard return -ENOTSUP; 2660e7e1989SKevin Wolf 2670e7e1989SKevin Wolf return drv->bdrv_create(filename, options); 268ea2384d3Sbellard } 269ea2384d3Sbellard 27084a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 27184a12e66SChristoph Hellwig { 27284a12e66SChristoph Hellwig BlockDriver *drv; 27384a12e66SChristoph Hellwig 274b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 27584a12e66SChristoph Hellwig if (drv == NULL) { 27616905d71SStefan Hajnoczi return -ENOENT; 27784a12e66SChristoph Hellwig } 27884a12e66SChristoph Hellwig 27984a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 28084a12e66SChristoph Hellwig } 28184a12e66SChristoph Hellwig 282d5249393Sbellard #ifdef _WIN32 28395389c86Sbellard void get_tmp_filename(char *filename, int size) 284d5249393Sbellard { 2853b9f94e1Sbellard char temp_dir[MAX_PATH]; 2863b9f94e1Sbellard 2873b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 2883b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 289d5249393Sbellard } 290d5249393Sbellard #else 29195389c86Sbellard void get_tmp_filename(char *filename, int size) 292ea2384d3Sbellard { 293ea2384d3Sbellard int fd; 2947ccfb2ebSblueswir1 const char *tmpdir; 295d5249393Sbellard /* XXX: race condition possible */ 2960badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 2970badc1eeSaurel32 if (!tmpdir) 2980badc1eeSaurel32 tmpdir = "/tmp"; 2990badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 300ea2384d3Sbellard fd = mkstemp(filename); 301ea2384d3Sbellard close(fd); 302ea2384d3Sbellard } 303d5249393Sbellard #endif 304ea2384d3Sbellard 305f3a5d3f8SChristoph Hellwig /* 306f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 307f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 308f3a5d3f8SChristoph Hellwig */ 309f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 310f3a5d3f8SChristoph Hellwig { 311508c7cb3SChristoph Hellwig int score_max = 0, score; 312508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 313f3a5d3f8SChristoph Hellwig 3148a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 315508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 316508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 317508c7cb3SChristoph Hellwig if (score > score_max) { 318508c7cb3SChristoph Hellwig score_max = score; 319508c7cb3SChristoph Hellwig drv = d; 320f3a5d3f8SChristoph Hellwig } 321508c7cb3SChristoph Hellwig } 322f3a5d3f8SChristoph Hellwig } 323f3a5d3f8SChristoph Hellwig 324508c7cb3SChristoph Hellwig return drv; 325f3a5d3f8SChristoph Hellwig } 326f3a5d3f8SChristoph Hellwig 327b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 32884a12e66SChristoph Hellwig { 32984a12e66SChristoph Hellwig BlockDriver *drv1; 33084a12e66SChristoph Hellwig char protocol[128]; 33184a12e66SChristoph Hellwig int len; 33284a12e66SChristoph Hellwig const char *p; 33384a12e66SChristoph Hellwig 33466f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 33566f82ceeSKevin Wolf 33639508e7aSChristoph Hellwig /* 33739508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 33839508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 33939508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 34039508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 34139508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 34239508e7aSChristoph Hellwig */ 34384a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 34439508e7aSChristoph Hellwig if (drv1) { 34584a12e66SChristoph Hellwig return drv1; 34684a12e66SChristoph Hellwig } 34739508e7aSChristoph Hellwig 3489e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 34939508e7aSChristoph Hellwig return bdrv_find_format("file"); 35039508e7aSChristoph Hellwig } 3519e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 3529e0b22f4SStefan Hajnoczi assert(p != NULL); 35384a12e66SChristoph Hellwig len = p - filename; 35484a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 35584a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 35684a12e66SChristoph Hellwig memcpy(protocol, filename, len); 35784a12e66SChristoph Hellwig protocol[len] = '\0'; 35884a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 35984a12e66SChristoph Hellwig if (drv1->protocol_name && 36084a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 36184a12e66SChristoph Hellwig return drv1; 36284a12e66SChristoph Hellwig } 36384a12e66SChristoph Hellwig } 36484a12e66SChristoph Hellwig return NULL; 36584a12e66SChristoph Hellwig } 36684a12e66SChristoph Hellwig 367c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv) 368ea2384d3Sbellard { 36983f64091Sbellard int ret, score, score_max; 370ea2384d3Sbellard BlockDriver *drv1, *drv; 37183f64091Sbellard uint8_t buf[2048]; 37283f64091Sbellard BlockDriverState *bs; 373ea2384d3Sbellard 374f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 375c98ac35dSStefan Weil if (ret < 0) { 376c98ac35dSStefan Weil *pdrv = NULL; 377c98ac35dSStefan Weil return ret; 378c98ac35dSStefan Weil } 379f8ea0b00SNicholas Bellinger 38008a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 38108a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 3821a396859SNicholas A. Bellinger bdrv_delete(bs); 383c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 384c98ac35dSStefan Weil if (!drv) { 385c98ac35dSStefan Weil ret = -ENOENT; 386c98ac35dSStefan Weil } 387c98ac35dSStefan Weil *pdrv = drv; 388c98ac35dSStefan Weil return ret; 3891a396859SNicholas A. Bellinger } 390f8ea0b00SNicholas Bellinger 39183f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 39283f64091Sbellard bdrv_delete(bs); 393ea2384d3Sbellard if (ret < 0) { 394c98ac35dSStefan Weil *pdrv = NULL; 395c98ac35dSStefan Weil return ret; 396ea2384d3Sbellard } 397ea2384d3Sbellard 398ea2384d3Sbellard score_max = 0; 39984a12e66SChristoph Hellwig drv = NULL; 4008a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 40183f64091Sbellard if (drv1->bdrv_probe) { 402ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 403ea2384d3Sbellard if (score > score_max) { 404ea2384d3Sbellard score_max = score; 405ea2384d3Sbellard drv = drv1; 406ea2384d3Sbellard } 407ea2384d3Sbellard } 40883f64091Sbellard } 409c98ac35dSStefan Weil if (!drv) { 410c98ac35dSStefan Weil ret = -ENOENT; 411c98ac35dSStefan Weil } 412c98ac35dSStefan Weil *pdrv = drv; 413c98ac35dSStefan Weil return ret; 414ea2384d3Sbellard } 415ea2384d3Sbellard 41651762288SStefan Hajnoczi /** 41751762288SStefan Hajnoczi * Set the current 'total_sectors' value 41851762288SStefan Hajnoczi */ 41951762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 42051762288SStefan Hajnoczi { 42151762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 42251762288SStefan Hajnoczi 423396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 424396759adSNicholas Bellinger if (bs->sg) 425396759adSNicholas Bellinger return 0; 426396759adSNicholas Bellinger 42751762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 42851762288SStefan Hajnoczi if (drv->bdrv_getlength) { 42951762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 43051762288SStefan Hajnoczi if (length < 0) { 43151762288SStefan Hajnoczi return length; 43251762288SStefan Hajnoczi } 43351762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 43451762288SStefan Hajnoczi } 43551762288SStefan Hajnoczi 43651762288SStefan Hajnoczi bs->total_sectors = hint; 43751762288SStefan Hajnoczi return 0; 43851762288SStefan Hajnoczi } 43951762288SStefan Hajnoczi 440b6ce07aaSKevin Wolf /* 44157915332SKevin Wolf * Common part for opening disk images and files 44257915332SKevin Wolf */ 44357915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 44457915332SKevin Wolf int flags, BlockDriver *drv) 44557915332SKevin Wolf { 44657915332SKevin Wolf int ret, open_flags; 44757915332SKevin Wolf 44857915332SKevin Wolf assert(drv != NULL); 44957915332SKevin Wolf 45066f82ceeSKevin Wolf bs->file = NULL; 45151762288SStefan Hajnoczi bs->total_sectors = 0; 45257915332SKevin Wolf bs->encrypted = 0; 45357915332SKevin Wolf bs->valid_key = 0; 45457915332SKevin Wolf bs->open_flags = flags; 45557915332SKevin Wolf /* buffer_alignment defaulted to 512, drivers can change this value */ 45657915332SKevin Wolf bs->buffer_alignment = 512; 45757915332SKevin Wolf 45857915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 45957915332SKevin Wolf 46057915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 46157915332SKevin Wolf return -ENOTSUP; 46257915332SKevin Wolf } 46357915332SKevin Wolf 46457915332SKevin Wolf bs->drv = drv; 46557915332SKevin Wolf bs->opaque = qemu_mallocz(drv->instance_size); 46657915332SKevin Wolf 467a6599793SChristoph Hellwig if (flags & BDRV_O_CACHE_WB) 46857915332SKevin Wolf bs->enable_write_cache = 1; 46957915332SKevin Wolf 47057915332SKevin Wolf /* 47157915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 47257915332SKevin Wolf * image. 47357915332SKevin Wolf */ 47457915332SKevin Wolf open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 47557915332SKevin Wolf 47657915332SKevin Wolf /* 477ebabb67aSStefan Weil * Snapshots should be writable. 47857915332SKevin Wolf */ 47957915332SKevin Wolf if (bs->is_temporary) { 48057915332SKevin Wolf open_flags |= BDRV_O_RDWR; 48157915332SKevin Wolf } 48257915332SKevin Wolf 48366f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 48466f82ceeSKevin Wolf if (drv->bdrv_file_open) { 48566f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 48666f82ceeSKevin Wolf } else { 48766f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 48866f82ceeSKevin Wolf if (ret >= 0) { 48966f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 49066f82ceeSKevin Wolf } 49166f82ceeSKevin Wolf } 49266f82ceeSKevin Wolf 49357915332SKevin Wolf if (ret < 0) { 49457915332SKevin Wolf goto free_and_fail; 49557915332SKevin Wolf } 49657915332SKevin Wolf 49757915332SKevin Wolf bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 49851762288SStefan Hajnoczi 49951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 50051762288SStefan Hajnoczi if (ret < 0) { 50151762288SStefan Hajnoczi goto free_and_fail; 50257915332SKevin Wolf } 50351762288SStefan Hajnoczi 50457915332SKevin Wolf #ifndef _WIN32 50557915332SKevin Wolf if (bs->is_temporary) { 50657915332SKevin Wolf unlink(filename); 50757915332SKevin Wolf } 50857915332SKevin Wolf #endif 50957915332SKevin Wolf return 0; 51057915332SKevin Wolf 51157915332SKevin Wolf free_and_fail: 51266f82ceeSKevin Wolf if (bs->file) { 51366f82ceeSKevin Wolf bdrv_delete(bs->file); 51466f82ceeSKevin Wolf bs->file = NULL; 51566f82ceeSKevin Wolf } 51657915332SKevin Wolf qemu_free(bs->opaque); 51757915332SKevin Wolf bs->opaque = NULL; 51857915332SKevin Wolf bs->drv = NULL; 51957915332SKevin Wolf return ret; 52057915332SKevin Wolf } 52157915332SKevin Wolf 52257915332SKevin Wolf /* 523b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 524b6ce07aaSKevin Wolf */ 52583f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 526b338082bSbellard { 52783f64091Sbellard BlockDriverState *bs; 5286db95603SChristoph Hellwig BlockDriver *drv; 52983f64091Sbellard int ret; 5303b0d4f61Sbellard 531b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 5326db95603SChristoph Hellwig if (!drv) { 5336db95603SChristoph Hellwig return -ENOENT; 5346db95603SChristoph Hellwig } 5356db95603SChristoph Hellwig 53683f64091Sbellard bs = bdrv_new(""); 537b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 53883f64091Sbellard if (ret < 0) { 53983f64091Sbellard bdrv_delete(bs); 54083f64091Sbellard return ret; 5413b0d4f61Sbellard } 54271d0770cSaliguori bs->growable = 1; 54383f64091Sbellard *pbs = bs; 54483f64091Sbellard return 0; 5453b0d4f61Sbellard } 5463b0d4f61Sbellard 547b6ce07aaSKevin Wolf /* 548b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 549b6ce07aaSKevin Wolf */ 550d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 551ea2384d3Sbellard BlockDriver *drv) 552ea2384d3Sbellard { 553b6ce07aaSKevin Wolf int ret; 55433e3963eSbellard 55583f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 556ea2384d3Sbellard BlockDriverState *bs1; 557ea2384d3Sbellard int64_t total_size; 5587c96d46eSaliguori int is_protocol = 0; 55991a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 56091a073a9SKevin Wolf QEMUOptionParameter *options; 561b6ce07aaSKevin Wolf char tmp_filename[PATH_MAX]; 562b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 56333e3963eSbellard 564ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 565ea2384d3Sbellard instead of opening 'filename' directly */ 566ea2384d3Sbellard 567ea2384d3Sbellard /* if there is a backing file, use it */ 568ea2384d3Sbellard bs1 = bdrv_new(""); 569d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 57051d7c00cSaliguori if (ret < 0) { 571ea2384d3Sbellard bdrv_delete(bs1); 57251d7c00cSaliguori return ret; 573ea2384d3Sbellard } 5743e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 5757c96d46eSaliguori 5767c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 5777c96d46eSaliguori is_protocol = 1; 5787c96d46eSaliguori 579ea2384d3Sbellard bdrv_delete(bs1); 580ea2384d3Sbellard 581ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 5827c96d46eSaliguori 5837c96d46eSaliguori /* Real path is meaningless for protocols */ 5847c96d46eSaliguori if (is_protocol) 5857c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 5867c96d46eSaliguori "%s", filename); 587114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 588114cdfa9SKirill A. Shutemov return -errno; 5897c96d46eSaliguori 59091a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 59191a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 59291a073a9SKevin Wolf 5933e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 59491a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 59591a073a9SKevin Wolf if (drv) { 59691a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 59791a073a9SKevin Wolf drv->format_name); 59891a073a9SKevin Wolf } 59991a073a9SKevin Wolf 60091a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 601d748768cSJan Kiszka free_option_parameters(options); 60251d7c00cSaliguori if (ret < 0) { 60351d7c00cSaliguori return ret; 604ea2384d3Sbellard } 60591a073a9SKevin Wolf 606ea2384d3Sbellard filename = tmp_filename; 60791a073a9SKevin Wolf drv = bdrv_qcow2; 608ea2384d3Sbellard bs->is_temporary = 1; 609ea2384d3Sbellard } 610ea2384d3Sbellard 611b6ce07aaSKevin Wolf /* Find the right image format driver */ 6126db95603SChristoph Hellwig if (!drv) { 613c98ac35dSStefan Weil ret = find_image_format(filename, &drv); 614ea2384d3Sbellard } 6156987307cSChristoph Hellwig 61651d7c00cSaliguori if (!drv) { 61751d7c00cSaliguori goto unlink_and_fail; 61883f64091Sbellard } 619b6ce07aaSKevin Wolf 620b6ce07aaSKevin Wolf /* Open the image */ 621b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 622b6ce07aaSKevin Wolf if (ret < 0) { 6236987307cSChristoph Hellwig goto unlink_and_fail; 6246987307cSChristoph Hellwig } 6256987307cSChristoph Hellwig 626b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 627b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 628b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 629b6ce07aaSKevin Wolf int back_flags; 630b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 631b6ce07aaSKevin Wolf 632b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 633df2dbb4aSStefan Hajnoczi 634df2dbb4aSStefan Hajnoczi if (path_has_protocol(bs->backing_file)) { 635df2dbb4aSStefan Hajnoczi pstrcpy(backing_filename, sizeof(backing_filename), 636df2dbb4aSStefan Hajnoczi bs->backing_file); 637df2dbb4aSStefan Hajnoczi } else { 638b6ce07aaSKevin Wolf path_combine(backing_filename, sizeof(backing_filename), 639b6ce07aaSKevin Wolf filename, bs->backing_file); 640df2dbb4aSStefan Hajnoczi } 641df2dbb4aSStefan Hajnoczi 642df2dbb4aSStefan Hajnoczi if (bs->backing_format[0] != '\0') { 643b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 644df2dbb4aSStefan Hajnoczi } 645b6ce07aaSKevin Wolf 646b6ce07aaSKevin Wolf /* backing files always opened read-only */ 647b6ce07aaSKevin Wolf back_flags = 648b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 649b6ce07aaSKevin Wolf 650b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 651b6ce07aaSKevin Wolf if (ret < 0) { 652b6ce07aaSKevin Wolf bdrv_close(bs); 653b6ce07aaSKevin Wolf return ret; 654b6ce07aaSKevin Wolf } 655b6ce07aaSKevin Wolf if (bs->is_temporary) { 656b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 657b6ce07aaSKevin Wolf } else { 658b6ce07aaSKevin Wolf /* base image inherits from "parent" */ 659b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = bs->keep_read_only; 660b6ce07aaSKevin Wolf } 661b6ce07aaSKevin Wolf } 662b6ce07aaSKevin Wolf 663b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 664b6ce07aaSKevin Wolf /* call the change callback */ 665b6ce07aaSKevin Wolf bs->media_changed = 1; 666b6ce07aaSKevin Wolf if (bs->change_cb) 667db97ee6aSChristoph Hellwig bs->change_cb(bs->change_opaque, CHANGE_MEDIA); 668b6ce07aaSKevin Wolf } 669b6ce07aaSKevin Wolf 670b6ce07aaSKevin Wolf return 0; 671b6ce07aaSKevin Wolf 672b6ce07aaSKevin Wolf unlink_and_fail: 673b6ce07aaSKevin Wolf if (bs->is_temporary) { 674b6ce07aaSKevin Wolf unlink(filename); 675b6ce07aaSKevin Wolf } 676b6ce07aaSKevin Wolf return ret; 677b6ce07aaSKevin Wolf } 678b6ce07aaSKevin Wolf 679fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 680fc01f7e7Sbellard { 68119cb3738Sbellard if (bs->drv) { 682f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 683f9092b10SMarkus Armbruster bs_snapshots = NULL; 684f9092b10SMarkus Armbruster } 685557df6acSStefan Hajnoczi if (bs->backing_hd) { 686ea2384d3Sbellard bdrv_delete(bs->backing_hd); 687557df6acSStefan Hajnoczi bs->backing_hd = NULL; 688557df6acSStefan Hajnoczi } 689ea2384d3Sbellard bs->drv->bdrv_close(bs); 690ea2384d3Sbellard qemu_free(bs->opaque); 691ea2384d3Sbellard #ifdef _WIN32 692ea2384d3Sbellard if (bs->is_temporary) { 693ea2384d3Sbellard unlink(bs->filename); 694ea2384d3Sbellard } 69567b915a5Sbellard #endif 696ea2384d3Sbellard bs->opaque = NULL; 697ea2384d3Sbellard bs->drv = NULL; 698b338082bSbellard 69966f82ceeSKevin Wolf if (bs->file != NULL) { 70066f82ceeSKevin Wolf bdrv_close(bs->file); 70166f82ceeSKevin Wolf } 70266f82ceeSKevin Wolf 703b338082bSbellard /* call the change callback */ 70419cb3738Sbellard bs->media_changed = 1; 705b338082bSbellard if (bs->change_cb) 706db97ee6aSChristoph Hellwig bs->change_cb(bs->change_opaque, CHANGE_MEDIA); 707b338082bSbellard } 708b338082bSbellard } 709b338082bSbellard 7102bc93fedSMORITA Kazutaka void bdrv_close_all(void) 7112bc93fedSMORITA Kazutaka { 7122bc93fedSMORITA Kazutaka BlockDriverState *bs; 7132bc93fedSMORITA Kazutaka 7142bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 7152bc93fedSMORITA Kazutaka bdrv_close(bs); 7162bc93fedSMORITA Kazutaka } 7172bc93fedSMORITA Kazutaka } 7182bc93fedSMORITA Kazutaka 719d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 720d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 721d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 722d22b2f41SRyan Harper { 723d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 724d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 725d22b2f41SRyan Harper } 726d22b2f41SRyan Harper bs->device_name[0] = '\0'; 727d22b2f41SRyan Harper } 728d22b2f41SRyan Harper 729b338082bSbellard void bdrv_delete(BlockDriverState *bs) 730b338082bSbellard { 73118846deeSMarkus Armbruster assert(!bs->peer); 73218846deeSMarkus Armbruster 7331b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 734d22b2f41SRyan Harper bdrv_make_anon(bs); 73534c6f050Saurel32 736b338082bSbellard bdrv_close(bs); 73766f82ceeSKevin Wolf if (bs->file != NULL) { 73866f82ceeSKevin Wolf bdrv_delete(bs->file); 73966f82ceeSKevin Wolf } 74066f82ceeSKevin Wolf 741f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 742b338082bSbellard qemu_free(bs); 743fc01f7e7Sbellard } 744fc01f7e7Sbellard 74518846deeSMarkus Armbruster int bdrv_attach(BlockDriverState *bs, DeviceState *qdev) 74618846deeSMarkus Armbruster { 74718846deeSMarkus Armbruster if (bs->peer) { 74818846deeSMarkus Armbruster return -EBUSY; 74918846deeSMarkus Armbruster } 75018846deeSMarkus Armbruster bs->peer = qdev; 75118846deeSMarkus Armbruster return 0; 75218846deeSMarkus Armbruster } 75318846deeSMarkus Armbruster 75418846deeSMarkus Armbruster void bdrv_detach(BlockDriverState *bs, DeviceState *qdev) 75518846deeSMarkus Armbruster { 75618846deeSMarkus Armbruster assert(bs->peer == qdev); 75718846deeSMarkus Armbruster bs->peer = NULL; 758a19712b0SMarkus Armbruster bs->change_cb = NULL; 759a19712b0SMarkus Armbruster bs->change_opaque = NULL; 76018846deeSMarkus Armbruster } 76118846deeSMarkus Armbruster 76218846deeSMarkus Armbruster DeviceState *bdrv_get_attached(BlockDriverState *bs) 76318846deeSMarkus Armbruster { 76418846deeSMarkus Armbruster return bs->peer; 76518846deeSMarkus Armbruster } 76618846deeSMarkus Armbruster 767e97fc193Saliguori /* 768e97fc193Saliguori * Run consistency checks on an image 769e97fc193Saliguori * 770e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 771a1c7273bSStefan Weil * free of errors) or -errno when an internal error occurred. The results of the 772e076f338SKevin Wolf * check are stored in res. 773e97fc193Saliguori */ 774e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) 775e97fc193Saliguori { 776e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 777e97fc193Saliguori return -ENOTSUP; 778e97fc193Saliguori } 779e97fc193Saliguori 780e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 7819ac228e0SKevin Wolf return bs->drv->bdrv_check(bs, res); 782e97fc193Saliguori } 783e97fc193Saliguori 7848a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 7858a426614SKevin Wolf 78633e3963eSbellard /* commit COW file into the raw image */ 78733e3963eSbellard int bdrv_commit(BlockDriverState *bs) 78833e3963eSbellard { 78919cb3738Sbellard BlockDriver *drv = bs->drv; 790ee181196SKevin Wolf BlockDriver *backing_drv; 7918a426614SKevin Wolf int64_t sector, total_sectors; 7928a426614SKevin Wolf int n, ro, open_flags; 7934dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 7948a426614SKevin Wolf uint8_t *buf; 7954dca4b63SNaphtali Sprei char filename[1024]; 7964dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 79733e3963eSbellard 79819cb3738Sbellard if (!drv) 79919cb3738Sbellard return -ENOMEDIUM; 80033e3963eSbellard 8014dca4b63SNaphtali Sprei if (!bs->backing_hd) { 8024dca4b63SNaphtali Sprei return -ENOTSUP; 8034dca4b63SNaphtali Sprei } 8044dca4b63SNaphtali Sprei 8054dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 806ea2384d3Sbellard return -EACCES; 80733e3963eSbellard } 80833e3963eSbellard 809ee181196SKevin Wolf backing_drv = bs->backing_hd->drv; 8104dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 8114dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 8124dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 8134dca4b63SNaphtali Sprei 8144dca4b63SNaphtali Sprei if (ro) { 8154dca4b63SNaphtali Sprei /* re-open as RW */ 8164dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 8174dca4b63SNaphtali Sprei bs->backing_hd = NULL; 8184dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 819ee181196SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, 820ee181196SKevin Wolf backing_drv); 8214dca4b63SNaphtali Sprei if (rw_ret < 0) { 8224dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 8234dca4b63SNaphtali Sprei /* try to re-open read-only */ 8244dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 825ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 826ee181196SKevin Wolf backing_drv); 8274dca4b63SNaphtali Sprei if (ret < 0) { 8284dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 8294dca4b63SNaphtali Sprei /* drive not functional anymore */ 8304dca4b63SNaphtali Sprei bs->drv = NULL; 8314dca4b63SNaphtali Sprei return ret; 8324dca4b63SNaphtali Sprei } 8334dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 8344dca4b63SNaphtali Sprei return rw_ret; 8354dca4b63SNaphtali Sprei } 8364dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 837ea2384d3Sbellard } 838ea2384d3Sbellard 8396ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 8408a426614SKevin Wolf buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 8418a426614SKevin Wolf 8428a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 8438a426614SKevin Wolf if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 8448a426614SKevin Wolf 8458a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 8464dca4b63SNaphtali Sprei ret = -EIO; 8474dca4b63SNaphtali Sprei goto ro_cleanup; 84833e3963eSbellard } 84933e3963eSbellard 8508a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 8514dca4b63SNaphtali Sprei ret = -EIO; 8524dca4b63SNaphtali Sprei goto ro_cleanup; 85333e3963eSbellard } 85433e3963eSbellard } 85533e3963eSbellard } 85695389c86Sbellard 8571d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 8581d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 8591d44952fSChristoph Hellwig bdrv_flush(bs); 8601d44952fSChristoph Hellwig } 86195389c86Sbellard 8623f5075aeSChristoph Hellwig /* 8633f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 8643f5075aeSChristoph Hellwig * stable on disk. 8653f5075aeSChristoph Hellwig */ 8663f5075aeSChristoph Hellwig if (bs->backing_hd) 8673f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 8684dca4b63SNaphtali Sprei 8694dca4b63SNaphtali Sprei ro_cleanup: 8708a426614SKevin Wolf qemu_free(buf); 8714dca4b63SNaphtali Sprei 8724dca4b63SNaphtali Sprei if (ro) { 8734dca4b63SNaphtali Sprei /* re-open as RO */ 8744dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 8754dca4b63SNaphtali Sprei bs->backing_hd = NULL; 8764dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 877ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 878ee181196SKevin Wolf backing_drv); 8794dca4b63SNaphtali Sprei if (ret < 0) { 8804dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 8814dca4b63SNaphtali Sprei /* drive not functional anymore */ 8824dca4b63SNaphtali Sprei bs->drv = NULL; 8834dca4b63SNaphtali Sprei return ret; 8844dca4b63SNaphtali Sprei } 8854dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 8864dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 8874dca4b63SNaphtali Sprei } 8884dca4b63SNaphtali Sprei 8891d44952fSChristoph Hellwig return ret; 89033e3963eSbellard } 89133e3963eSbellard 8926ab4b5abSMarkus Armbruster void bdrv_commit_all(void) 8936ab4b5abSMarkus Armbruster { 8946ab4b5abSMarkus Armbruster BlockDriverState *bs; 8956ab4b5abSMarkus Armbruster 8966ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 8976ab4b5abSMarkus Armbruster bdrv_commit(bs); 8986ab4b5abSMarkus Armbruster } 8996ab4b5abSMarkus Armbruster } 9006ab4b5abSMarkus Armbruster 901756e6736SKevin Wolf /* 902756e6736SKevin Wolf * Return values: 903756e6736SKevin Wolf * 0 - success 904756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 905756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 906756e6736SKevin Wolf * image file header 907756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 908756e6736SKevin Wolf */ 909756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 910756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 911756e6736SKevin Wolf { 912756e6736SKevin Wolf BlockDriver *drv = bs->drv; 913756e6736SKevin Wolf 914756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 915756e6736SKevin Wolf return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 916756e6736SKevin Wolf } else { 917756e6736SKevin Wolf return -ENOTSUP; 918756e6736SKevin Wolf } 919756e6736SKevin Wolf } 920756e6736SKevin Wolf 92171d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 92271d0770cSaliguori size_t size) 92371d0770cSaliguori { 92471d0770cSaliguori int64_t len; 92571d0770cSaliguori 92671d0770cSaliguori if (!bdrv_is_inserted(bs)) 92771d0770cSaliguori return -ENOMEDIUM; 92871d0770cSaliguori 92971d0770cSaliguori if (bs->growable) 93071d0770cSaliguori return 0; 93171d0770cSaliguori 93271d0770cSaliguori len = bdrv_getlength(bs); 93371d0770cSaliguori 934fbb7b4e0SKevin Wolf if (offset < 0) 935fbb7b4e0SKevin Wolf return -EIO; 936fbb7b4e0SKevin Wolf 937fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 93871d0770cSaliguori return -EIO; 93971d0770cSaliguori 94071d0770cSaliguori return 0; 94171d0770cSaliguori } 94271d0770cSaliguori 94371d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 94471d0770cSaliguori int nb_sectors) 94571d0770cSaliguori { 946eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 947eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 94871d0770cSaliguori } 94971d0770cSaliguori 950*e7a8a783SKevin Wolf static inline bool bdrv_has_async_rw(BlockDriver *drv) 951*e7a8a783SKevin Wolf { 952*e7a8a783SKevin Wolf return drv->bdrv_co_readv != bdrv_co_readv_em 953*e7a8a783SKevin Wolf || drv->bdrv_aio_readv != bdrv_aio_readv_em; 954*e7a8a783SKevin Wolf } 955*e7a8a783SKevin Wolf 956*e7a8a783SKevin Wolf static inline bool bdrv_has_async_flush(BlockDriver *drv) 957*e7a8a783SKevin Wolf { 958*e7a8a783SKevin Wolf return drv->bdrv_aio_flush != bdrv_aio_flush_em; 959*e7a8a783SKevin Wolf } 960*e7a8a783SKevin Wolf 96119cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 962fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 963fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 964fc01f7e7Sbellard { 965ea2384d3Sbellard BlockDriver *drv = bs->drv; 966fc01f7e7Sbellard 96719cb3738Sbellard if (!drv) 96819cb3738Sbellard return -ENOMEDIUM; 969*e7a8a783SKevin Wolf 970*e7a8a783SKevin Wolf if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) { 971*e7a8a783SKevin Wolf QEMUIOVector qiov; 972*e7a8a783SKevin Wolf struct iovec iov = { 973*e7a8a783SKevin Wolf .iov_base = (void *)buf, 974*e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 975*e7a8a783SKevin Wolf }; 976*e7a8a783SKevin Wolf 977*e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 978*e7a8a783SKevin Wolf return bdrv_co_readv(bs, sector_num, nb_sectors, &qiov); 979*e7a8a783SKevin Wolf } 980*e7a8a783SKevin Wolf 98171d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 98271d0770cSaliguori return -EIO; 983b338082bSbellard 98483f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 98583f64091Sbellard } 986fc01f7e7Sbellard 9877cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 9887cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 9897cd1e32aSlirans@il.ibm.com { 9907cd1e32aSlirans@il.ibm.com int64_t start, end; 991c6d22830SJan Kiszka unsigned long val, idx, bit; 992a55eb92cSJan Kiszka 9936ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 994c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 9957cd1e32aSlirans@il.ibm.com 9967cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 997c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 998c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 999c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 1000c6d22830SJan Kiszka if (dirty) { 10016d59fec1SMarcelo Tosatti if (!(val & (1UL << bit))) { 1002aaa0eb75SLiran Schour bs->dirty_count++; 10036d59fec1SMarcelo Tosatti val |= 1UL << bit; 1004aaa0eb75SLiran Schour } 1005c6d22830SJan Kiszka } else { 10066d59fec1SMarcelo Tosatti if (val & (1UL << bit)) { 1007aaa0eb75SLiran Schour bs->dirty_count--; 10086d59fec1SMarcelo Tosatti val &= ~(1UL << bit); 1009c6d22830SJan Kiszka } 1010aaa0eb75SLiran Schour } 1011c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 10127cd1e32aSlirans@il.ibm.com } 10137cd1e32aSlirans@il.ibm.com } 10147cd1e32aSlirans@il.ibm.com 101519cb3738Sbellard /* Return < 0 if error. Important errors are: 101619cb3738Sbellard -EIO generic I/O error (may happen for all errors) 101719cb3738Sbellard -ENOMEDIUM No media inserted. 101819cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 101919cb3738Sbellard -EACCES Trying to write a read-only device 102019cb3738Sbellard */ 1021fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 1022fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 1023fc01f7e7Sbellard { 102483f64091Sbellard BlockDriver *drv = bs->drv; 1025*e7a8a783SKevin Wolf 102619cb3738Sbellard if (!bs->drv) 102719cb3738Sbellard return -ENOMEDIUM; 1028*e7a8a783SKevin Wolf 1029*e7a8a783SKevin Wolf if (bdrv_has_async_rw(drv) && qemu_in_coroutine()) { 1030*e7a8a783SKevin Wolf QEMUIOVector qiov; 1031*e7a8a783SKevin Wolf struct iovec iov = { 1032*e7a8a783SKevin Wolf .iov_base = (void *)buf, 1033*e7a8a783SKevin Wolf .iov_len = nb_sectors * BDRV_SECTOR_SIZE, 1034*e7a8a783SKevin Wolf }; 1035*e7a8a783SKevin Wolf 1036*e7a8a783SKevin Wolf qemu_iovec_init_external(&qiov, &iov, 1); 1037*e7a8a783SKevin Wolf return bdrv_co_writev(bs, sector_num, nb_sectors, &qiov); 1038*e7a8a783SKevin Wolf } 1039*e7a8a783SKevin Wolf 10400849bf08Sbellard if (bs->read_only) 104119cb3738Sbellard return -EACCES; 104271d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 104371d0770cSaliguori return -EIO; 104471d0770cSaliguori 1045c6d22830SJan Kiszka if (bs->dirty_bitmap) { 10467cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 10477cd1e32aSlirans@il.ibm.com } 10487cd1e32aSlirans@il.ibm.com 1049294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 1050294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 1051294cc35fSKevin Wolf } 1052294cc35fSKevin Wolf 105383f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 105483f64091Sbellard } 105583f64091Sbellard 1056eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 1057eda578e5Saliguori void *buf, int count1) 105883f64091Sbellard { 10596ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 106083f64091Sbellard int len, nb_sectors, count; 106183f64091Sbellard int64_t sector_num; 10629a8c4cceSKevin Wolf int ret; 106383f64091Sbellard 106483f64091Sbellard count = count1; 106583f64091Sbellard /* first read to align to sector start */ 10666ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 106783f64091Sbellard if (len > count) 106883f64091Sbellard len = count; 10696ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 107083f64091Sbellard if (len > 0) { 10719a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10729a8c4cceSKevin Wolf return ret; 10736ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 107483f64091Sbellard count -= len; 107583f64091Sbellard if (count == 0) 107683f64091Sbellard return count1; 107783f64091Sbellard sector_num++; 107883f64091Sbellard buf += len; 107983f64091Sbellard } 108083f64091Sbellard 108183f64091Sbellard /* read the sectors "in place" */ 10826ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 108383f64091Sbellard if (nb_sectors > 0) { 10849a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 10859a8c4cceSKevin Wolf return ret; 108683f64091Sbellard sector_num += nb_sectors; 10876ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 108883f64091Sbellard buf += len; 108983f64091Sbellard count -= len; 109083f64091Sbellard } 109183f64091Sbellard 109283f64091Sbellard /* add data from the last sector */ 109383f64091Sbellard if (count > 0) { 10949a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10959a8c4cceSKevin Wolf return ret; 109683f64091Sbellard memcpy(buf, tmp_buf, count); 109783f64091Sbellard } 109883f64091Sbellard return count1; 109983f64091Sbellard } 110083f64091Sbellard 1101eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 1102eda578e5Saliguori const void *buf, int count1) 110383f64091Sbellard { 11046ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 110583f64091Sbellard int len, nb_sectors, count; 110683f64091Sbellard int64_t sector_num; 11079a8c4cceSKevin Wolf int ret; 110883f64091Sbellard 110983f64091Sbellard count = count1; 111083f64091Sbellard /* first write to align to sector start */ 11116ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 111283f64091Sbellard if (len > count) 111383f64091Sbellard len = count; 11146ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 111583f64091Sbellard if (len > 0) { 11169a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 11179a8c4cceSKevin Wolf return ret; 11186ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 11199a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 11209a8c4cceSKevin Wolf return ret; 112183f64091Sbellard count -= len; 112283f64091Sbellard if (count == 0) 112383f64091Sbellard return count1; 112483f64091Sbellard sector_num++; 112583f64091Sbellard buf += len; 112683f64091Sbellard } 112783f64091Sbellard 112883f64091Sbellard /* write the sectors "in place" */ 11296ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 113083f64091Sbellard if (nb_sectors > 0) { 11319a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 11329a8c4cceSKevin Wolf return ret; 113383f64091Sbellard sector_num += nb_sectors; 11346ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 113583f64091Sbellard buf += len; 113683f64091Sbellard count -= len; 113783f64091Sbellard } 113883f64091Sbellard 113983f64091Sbellard /* add data from the last sector */ 114083f64091Sbellard if (count > 0) { 11419a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 11429a8c4cceSKevin Wolf return ret; 114383f64091Sbellard memcpy(tmp_buf, buf, count); 11449a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 11459a8c4cceSKevin Wolf return ret; 114683f64091Sbellard } 114783f64091Sbellard return count1; 114883f64091Sbellard } 114983f64091Sbellard 1150f08145feSKevin Wolf /* 1151f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1152f08145feSKevin Wolf * request (acts as a barrier) 1153f08145feSKevin Wolf * 1154f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1155f08145feSKevin Wolf */ 1156f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1157f08145feSKevin Wolf const void *buf, int count) 1158f08145feSKevin Wolf { 1159f08145feSKevin Wolf int ret; 1160f08145feSKevin Wolf 1161f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1162f08145feSKevin Wolf if (ret < 0) { 1163f08145feSKevin Wolf return ret; 1164f08145feSKevin Wolf } 1165f08145feSKevin Wolf 1166f08145feSKevin Wolf /* No flush needed for cache=writethrough, it uses O_DSYNC */ 1167f08145feSKevin Wolf if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) { 1168f08145feSKevin Wolf bdrv_flush(bs); 1169f08145feSKevin Wolf } 1170f08145feSKevin Wolf 1171f08145feSKevin Wolf return 0; 1172f08145feSKevin Wolf } 1173f08145feSKevin Wolf 1174da1fa91dSKevin Wolf int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num, 1175da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 1176da1fa91dSKevin Wolf { 1177da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 1178da1fa91dSKevin Wolf 1179da1fa91dSKevin Wolf trace_bdrv_co_readv(bs, sector_num, nb_sectors); 1180da1fa91dSKevin Wolf 1181da1fa91dSKevin Wolf if (!drv) { 1182da1fa91dSKevin Wolf return -ENOMEDIUM; 1183da1fa91dSKevin Wolf } 1184da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1185da1fa91dSKevin Wolf return -EIO; 1186da1fa91dSKevin Wolf } 1187da1fa91dSKevin Wolf 1188da1fa91dSKevin Wolf return drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov); 1189da1fa91dSKevin Wolf } 1190da1fa91dSKevin Wolf 1191da1fa91dSKevin Wolf int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num, 1192da1fa91dSKevin Wolf int nb_sectors, QEMUIOVector *qiov) 1193da1fa91dSKevin Wolf { 1194da1fa91dSKevin Wolf BlockDriver *drv = bs->drv; 1195da1fa91dSKevin Wolf 1196da1fa91dSKevin Wolf trace_bdrv_co_writev(bs, sector_num, nb_sectors); 1197da1fa91dSKevin Wolf 1198da1fa91dSKevin Wolf if (!bs->drv) { 1199da1fa91dSKevin Wolf return -ENOMEDIUM; 1200da1fa91dSKevin Wolf } 1201da1fa91dSKevin Wolf if (bs->read_only) { 1202da1fa91dSKevin Wolf return -EACCES; 1203da1fa91dSKevin Wolf } 1204da1fa91dSKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) { 1205da1fa91dSKevin Wolf return -EIO; 1206da1fa91dSKevin Wolf } 1207da1fa91dSKevin Wolf 1208da1fa91dSKevin Wolf if (bs->dirty_bitmap) { 1209da1fa91dSKevin Wolf set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 1210da1fa91dSKevin Wolf } 1211da1fa91dSKevin Wolf 1212da1fa91dSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 1213da1fa91dSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 1214da1fa91dSKevin Wolf } 1215da1fa91dSKevin Wolf 1216da1fa91dSKevin Wolf return drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov); 1217da1fa91dSKevin Wolf } 1218da1fa91dSKevin Wolf 121983f64091Sbellard /** 122083f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 122183f64091Sbellard */ 122283f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 122383f64091Sbellard { 122483f64091Sbellard BlockDriver *drv = bs->drv; 122551762288SStefan Hajnoczi int ret; 122683f64091Sbellard if (!drv) 122719cb3738Sbellard return -ENOMEDIUM; 122883f64091Sbellard if (!drv->bdrv_truncate) 122983f64091Sbellard return -ENOTSUP; 123059f2689dSNaphtali Sprei if (bs->read_only) 123159f2689dSNaphtali Sprei return -EACCES; 12328591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 12338591675fSMarcelo Tosatti return -EBUSY; 123451762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 123551762288SStefan Hajnoczi if (ret == 0) { 123651762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 1237db97ee6aSChristoph Hellwig if (bs->change_cb) { 1238db97ee6aSChristoph Hellwig bs->change_cb(bs->change_opaque, CHANGE_SIZE); 1239db97ee6aSChristoph Hellwig } 124051762288SStefan Hajnoczi } 124151762288SStefan Hajnoczi return ret; 124283f64091Sbellard } 124383f64091Sbellard 124483f64091Sbellard /** 12454a1d5e1fSFam Zheng * Length of a allocated file in bytes. Sparse files are counted by actual 12464a1d5e1fSFam Zheng * allocated space. Return < 0 if error or unknown. 12474a1d5e1fSFam Zheng */ 12484a1d5e1fSFam Zheng int64_t bdrv_get_allocated_file_size(BlockDriverState *bs) 12494a1d5e1fSFam Zheng { 12504a1d5e1fSFam Zheng BlockDriver *drv = bs->drv; 12514a1d5e1fSFam Zheng if (!drv) { 12524a1d5e1fSFam Zheng return -ENOMEDIUM; 12534a1d5e1fSFam Zheng } 12544a1d5e1fSFam Zheng if (drv->bdrv_get_allocated_file_size) { 12554a1d5e1fSFam Zheng return drv->bdrv_get_allocated_file_size(bs); 12564a1d5e1fSFam Zheng } 12574a1d5e1fSFam Zheng if (bs->file) { 12584a1d5e1fSFam Zheng return bdrv_get_allocated_file_size(bs->file); 12594a1d5e1fSFam Zheng } 12604a1d5e1fSFam Zheng return -ENOTSUP; 12614a1d5e1fSFam Zheng } 12624a1d5e1fSFam Zheng 12634a1d5e1fSFam Zheng /** 126483f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 126583f64091Sbellard */ 126683f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 126783f64091Sbellard { 126883f64091Sbellard BlockDriver *drv = bs->drv; 126983f64091Sbellard if (!drv) 127019cb3738Sbellard return -ENOMEDIUM; 127151762288SStefan Hajnoczi 127246a4e4e6SStefan Hajnoczi if (bs->growable || bs->removable) { 127346a4e4e6SStefan Hajnoczi if (drv->bdrv_getlength) { 127483f64091Sbellard return drv->bdrv_getlength(bs); 1275fc01f7e7Sbellard } 127646a4e4e6SStefan Hajnoczi } 127746a4e4e6SStefan Hajnoczi return bs->total_sectors * BDRV_SECTOR_SIZE; 127846a4e4e6SStefan Hajnoczi } 1279fc01f7e7Sbellard 128019cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 128196b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 1282fc01f7e7Sbellard { 128319cb3738Sbellard int64_t length; 128419cb3738Sbellard length = bdrv_getlength(bs); 128519cb3738Sbellard if (length < 0) 128619cb3738Sbellard length = 0; 128719cb3738Sbellard else 12886ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 128919cb3738Sbellard *nb_sectors_ptr = length; 1290fc01f7e7Sbellard } 1291cf98951bSbellard 1292f3d54fc4Saliguori struct partition { 1293f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 1294f3d54fc4Saliguori uint8_t head; /* starting head */ 1295f3d54fc4Saliguori uint8_t sector; /* starting sector */ 1296f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 1297f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 1298f3d54fc4Saliguori uint8_t end_head; /* end head */ 1299f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 1300f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 1301f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 1302f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 1303f3d54fc4Saliguori } __attribute__((packed)); 1304f3d54fc4Saliguori 1305f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 1306f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 1307f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 1308f3d54fc4Saliguori { 1309eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 1310f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 1311f3d54fc4Saliguori struct partition *p; 1312f3d54fc4Saliguori uint32_t nr_sects; 1313a38131b6Sblueswir1 uint64_t nb_sectors; 1314f3d54fc4Saliguori 1315f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1316f3d54fc4Saliguori 1317f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 1318f3d54fc4Saliguori if (ret < 0) 1319f3d54fc4Saliguori return -1; 1320f3d54fc4Saliguori /* test msdos magic */ 1321f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 1322f3d54fc4Saliguori return -1; 1323f3d54fc4Saliguori for(i = 0; i < 4; i++) { 1324f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 1325f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 1326f3d54fc4Saliguori if (nr_sects && p->end_head) { 1327f3d54fc4Saliguori /* We make the assumption that the partition terminates on 1328f3d54fc4Saliguori a cylinder boundary */ 1329f3d54fc4Saliguori heads = p->end_head + 1; 1330f3d54fc4Saliguori sectors = p->end_sector & 63; 1331f3d54fc4Saliguori if (sectors == 0) 1332f3d54fc4Saliguori continue; 1333f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 1334f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 1335f3d54fc4Saliguori continue; 1336f3d54fc4Saliguori *pheads = heads; 1337f3d54fc4Saliguori *psectors = sectors; 1338f3d54fc4Saliguori *pcylinders = cylinders; 1339f3d54fc4Saliguori #if 0 1340f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 1341f3d54fc4Saliguori cylinders, heads, sectors); 1342f3d54fc4Saliguori #endif 1343f3d54fc4Saliguori return 0; 1344f3d54fc4Saliguori } 1345f3d54fc4Saliguori } 1346f3d54fc4Saliguori return -1; 1347f3d54fc4Saliguori } 1348f3d54fc4Saliguori 1349f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 1350f3d54fc4Saliguori { 1351f3d54fc4Saliguori int translation, lba_detected = 0; 1352f3d54fc4Saliguori int cylinders, heads, secs; 1353a38131b6Sblueswir1 uint64_t nb_sectors; 1354f3d54fc4Saliguori 1355f3d54fc4Saliguori /* if a geometry hint is available, use it */ 1356f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1357f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 1358f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 1359f3d54fc4Saliguori if (cylinders != 0) { 1360f3d54fc4Saliguori *pcyls = cylinders; 1361f3d54fc4Saliguori *pheads = heads; 1362f3d54fc4Saliguori *psecs = secs; 1363f3d54fc4Saliguori } else { 1364f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 1365f3d54fc4Saliguori if (heads > 16) { 1366f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 1367f3d54fc4Saliguori translation was active, so the default 1368f3d54fc4Saliguori hardware geometry is OK */ 1369f3d54fc4Saliguori lba_detected = 1; 1370f3d54fc4Saliguori goto default_geometry; 1371f3d54fc4Saliguori } else { 1372f3d54fc4Saliguori *pcyls = cylinders; 1373f3d54fc4Saliguori *pheads = heads; 1374f3d54fc4Saliguori *psecs = secs; 1375f3d54fc4Saliguori /* disable any translation to be in sync with 1376f3d54fc4Saliguori the logical geometry */ 1377f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 1378f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1379f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 1380f3d54fc4Saliguori } 1381f3d54fc4Saliguori } 1382f3d54fc4Saliguori } else { 1383f3d54fc4Saliguori default_geometry: 1384f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 1385f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 1386f3d54fc4Saliguori 1387f3d54fc4Saliguori if (cylinders > 16383) 1388f3d54fc4Saliguori cylinders = 16383; 1389f3d54fc4Saliguori else if (cylinders < 2) 1390f3d54fc4Saliguori cylinders = 2; 1391f3d54fc4Saliguori *pcyls = cylinders; 1392f3d54fc4Saliguori *pheads = 16; 1393f3d54fc4Saliguori *psecs = 63; 1394f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 1395f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 1396f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1397f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 1398f3d54fc4Saliguori } else { 1399f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1400f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 1401f3d54fc4Saliguori } 1402f3d54fc4Saliguori } 1403f3d54fc4Saliguori } 1404f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 1405f3d54fc4Saliguori } 1406f3d54fc4Saliguori } 1407f3d54fc4Saliguori 1408b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 1409b338082bSbellard int cyls, int heads, int secs) 1410b338082bSbellard { 1411b338082bSbellard bs->cyls = cyls; 1412b338082bSbellard bs->heads = heads; 1413b338082bSbellard bs->secs = secs; 1414b338082bSbellard } 1415b338082bSbellard 141646d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 141746d4767dSbellard { 141846d4767dSbellard bs->translation = translation; 141946d4767dSbellard } 142046d4767dSbellard 1421b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 1422b338082bSbellard int *pcyls, int *pheads, int *psecs) 1423b338082bSbellard { 1424b338082bSbellard *pcyls = bs->cyls; 1425b338082bSbellard *pheads = bs->heads; 1426b338082bSbellard *psecs = bs->secs; 1427b338082bSbellard } 1428b338082bSbellard 14295bbdbb46SBlue Swirl /* Recognize floppy formats */ 14305bbdbb46SBlue Swirl typedef struct FDFormat { 14315bbdbb46SBlue Swirl FDriveType drive; 14325bbdbb46SBlue Swirl uint8_t last_sect; 14335bbdbb46SBlue Swirl uint8_t max_track; 14345bbdbb46SBlue Swirl uint8_t max_head; 14355bbdbb46SBlue Swirl } FDFormat; 14365bbdbb46SBlue Swirl 14375bbdbb46SBlue Swirl static const FDFormat fd_formats[] = { 14385bbdbb46SBlue Swirl /* First entry is default format */ 14395bbdbb46SBlue Swirl /* 1.44 MB 3"1/2 floppy disks */ 14405bbdbb46SBlue Swirl { FDRIVE_DRV_144, 18, 80, 1, }, 14415bbdbb46SBlue Swirl { FDRIVE_DRV_144, 20, 80, 1, }, 14425bbdbb46SBlue Swirl { FDRIVE_DRV_144, 21, 80, 1, }, 14435bbdbb46SBlue Swirl { FDRIVE_DRV_144, 21, 82, 1, }, 14445bbdbb46SBlue Swirl { FDRIVE_DRV_144, 21, 83, 1, }, 14455bbdbb46SBlue Swirl { FDRIVE_DRV_144, 22, 80, 1, }, 14465bbdbb46SBlue Swirl { FDRIVE_DRV_144, 23, 80, 1, }, 14475bbdbb46SBlue Swirl { FDRIVE_DRV_144, 24, 80, 1, }, 14485bbdbb46SBlue Swirl /* 2.88 MB 3"1/2 floppy disks */ 14495bbdbb46SBlue Swirl { FDRIVE_DRV_288, 36, 80, 1, }, 14505bbdbb46SBlue Swirl { FDRIVE_DRV_288, 39, 80, 1, }, 14515bbdbb46SBlue Swirl { FDRIVE_DRV_288, 40, 80, 1, }, 14525bbdbb46SBlue Swirl { FDRIVE_DRV_288, 44, 80, 1, }, 14535bbdbb46SBlue Swirl { FDRIVE_DRV_288, 48, 80, 1, }, 14545bbdbb46SBlue Swirl /* 720 kB 3"1/2 floppy disks */ 14555bbdbb46SBlue Swirl { FDRIVE_DRV_144, 9, 80, 1, }, 14565bbdbb46SBlue Swirl { FDRIVE_DRV_144, 10, 80, 1, }, 14575bbdbb46SBlue Swirl { FDRIVE_DRV_144, 10, 82, 1, }, 14585bbdbb46SBlue Swirl { FDRIVE_DRV_144, 10, 83, 1, }, 14595bbdbb46SBlue Swirl { FDRIVE_DRV_144, 13, 80, 1, }, 14605bbdbb46SBlue Swirl { FDRIVE_DRV_144, 14, 80, 1, }, 14615bbdbb46SBlue Swirl /* 1.2 MB 5"1/4 floppy disks */ 14625bbdbb46SBlue Swirl { FDRIVE_DRV_120, 15, 80, 1, }, 14635bbdbb46SBlue Swirl { FDRIVE_DRV_120, 18, 80, 1, }, 14645bbdbb46SBlue Swirl { FDRIVE_DRV_120, 18, 82, 1, }, 14655bbdbb46SBlue Swirl { FDRIVE_DRV_120, 18, 83, 1, }, 14665bbdbb46SBlue Swirl { FDRIVE_DRV_120, 20, 80, 1, }, 14675bbdbb46SBlue Swirl /* 720 kB 5"1/4 floppy disks */ 14685bbdbb46SBlue Swirl { FDRIVE_DRV_120, 9, 80, 1, }, 14695bbdbb46SBlue Swirl { FDRIVE_DRV_120, 11, 80, 1, }, 14705bbdbb46SBlue Swirl /* 360 kB 5"1/4 floppy disks */ 14715bbdbb46SBlue Swirl { FDRIVE_DRV_120, 9, 40, 1, }, 14725bbdbb46SBlue Swirl { FDRIVE_DRV_120, 9, 40, 0, }, 14735bbdbb46SBlue Swirl { FDRIVE_DRV_120, 10, 41, 1, }, 14745bbdbb46SBlue Swirl { FDRIVE_DRV_120, 10, 42, 1, }, 14755bbdbb46SBlue Swirl /* 320 kB 5"1/4 floppy disks */ 14765bbdbb46SBlue Swirl { FDRIVE_DRV_120, 8, 40, 1, }, 14775bbdbb46SBlue Swirl { FDRIVE_DRV_120, 8, 40, 0, }, 14785bbdbb46SBlue Swirl /* 360 kB must match 5"1/4 better than 3"1/2... */ 14795bbdbb46SBlue Swirl { FDRIVE_DRV_144, 9, 80, 0, }, 14805bbdbb46SBlue Swirl /* end */ 14815bbdbb46SBlue Swirl { FDRIVE_DRV_NONE, -1, -1, 0, }, 14825bbdbb46SBlue Swirl }; 14835bbdbb46SBlue Swirl 14845bbdbb46SBlue Swirl void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, 14855bbdbb46SBlue Swirl int *max_track, int *last_sect, 14865bbdbb46SBlue Swirl FDriveType drive_in, FDriveType *drive) 14875bbdbb46SBlue Swirl { 14885bbdbb46SBlue Swirl const FDFormat *parse; 14895bbdbb46SBlue Swirl uint64_t nb_sectors, size; 14905bbdbb46SBlue Swirl int i, first_match, match; 14915bbdbb46SBlue Swirl 14925bbdbb46SBlue Swirl bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); 14935bbdbb46SBlue Swirl if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { 14945bbdbb46SBlue Swirl /* User defined disk */ 14955bbdbb46SBlue Swirl } else { 14965bbdbb46SBlue Swirl bdrv_get_geometry(bs, &nb_sectors); 14975bbdbb46SBlue Swirl match = -1; 14985bbdbb46SBlue Swirl first_match = -1; 14995bbdbb46SBlue Swirl for (i = 0; ; i++) { 15005bbdbb46SBlue Swirl parse = &fd_formats[i]; 15015bbdbb46SBlue Swirl if (parse->drive == FDRIVE_DRV_NONE) { 15025bbdbb46SBlue Swirl break; 15035bbdbb46SBlue Swirl } 15045bbdbb46SBlue Swirl if (drive_in == parse->drive || 15055bbdbb46SBlue Swirl drive_in == FDRIVE_DRV_NONE) { 15065bbdbb46SBlue Swirl size = (parse->max_head + 1) * parse->max_track * 15075bbdbb46SBlue Swirl parse->last_sect; 15085bbdbb46SBlue Swirl if (nb_sectors == size) { 15095bbdbb46SBlue Swirl match = i; 15105bbdbb46SBlue Swirl break; 15115bbdbb46SBlue Swirl } 15125bbdbb46SBlue Swirl if (first_match == -1) { 15135bbdbb46SBlue Swirl first_match = i; 15145bbdbb46SBlue Swirl } 15155bbdbb46SBlue Swirl } 15165bbdbb46SBlue Swirl } 15175bbdbb46SBlue Swirl if (match == -1) { 15185bbdbb46SBlue Swirl if (first_match == -1) { 15195bbdbb46SBlue Swirl match = 1; 15205bbdbb46SBlue Swirl } else { 15215bbdbb46SBlue Swirl match = first_match; 15225bbdbb46SBlue Swirl } 15235bbdbb46SBlue Swirl parse = &fd_formats[match]; 15245bbdbb46SBlue Swirl } 15255bbdbb46SBlue Swirl *nb_heads = parse->max_head + 1; 15265bbdbb46SBlue Swirl *max_track = parse->max_track; 15275bbdbb46SBlue Swirl *last_sect = parse->last_sect; 15285bbdbb46SBlue Swirl *drive = parse->drive; 15295bbdbb46SBlue Swirl } 15305bbdbb46SBlue Swirl } 15315bbdbb46SBlue Swirl 153246d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 153346d4767dSbellard { 153446d4767dSbellard return bs->translation; 153546d4767dSbellard } 153646d4767dSbellard 1537abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 1538abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 1539abd7f68dSMarkus Armbruster { 1540abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 1541abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 1542abd7f68dSMarkus Armbruster } 1543abd7f68dSMarkus Armbruster 1544abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 1545abd7f68dSMarkus Armbruster { 1546abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 1547abd7f68dSMarkus Armbruster } 1548abd7f68dSMarkus Armbruster 15497d0d6950SMarkus Armbruster void bdrv_set_removable(BlockDriverState *bs, int removable) 15507d0d6950SMarkus Armbruster { 15517d0d6950SMarkus Armbruster bs->removable = removable; 15527d0d6950SMarkus Armbruster if (removable && bs == bs_snapshots) { 15537d0d6950SMarkus Armbruster bs_snapshots = NULL; 15547d0d6950SMarkus Armbruster } 15557d0d6950SMarkus Armbruster } 15567d0d6950SMarkus Armbruster 1557b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 1558b338082bSbellard { 1559b338082bSbellard return bs->removable; 1560b338082bSbellard } 1561b338082bSbellard 1562b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 1563b338082bSbellard { 1564b338082bSbellard return bs->read_only; 1565b338082bSbellard } 1566b338082bSbellard 1567985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 1568985a03b0Sths { 1569985a03b0Sths return bs->sg; 1570985a03b0Sths } 1571985a03b0Sths 1572e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 1573e900a7b7SChristoph Hellwig { 1574e900a7b7SChristoph Hellwig return bs->enable_write_cache; 1575e900a7b7SChristoph Hellwig } 1576e900a7b7SChristoph Hellwig 157719cb3738Sbellard /* XXX: no longer used */ 1578b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 1579db97ee6aSChristoph Hellwig void (*change_cb)(void *opaque, int reason), 1580db97ee6aSChristoph Hellwig void *opaque) 1581b338082bSbellard { 1582b338082bSbellard bs->change_cb = change_cb; 1583b338082bSbellard bs->change_opaque = opaque; 1584b338082bSbellard } 1585b338082bSbellard 1586ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 1587ea2384d3Sbellard { 1588ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 1589ea2384d3Sbellard return 1; 1590ea2384d3Sbellard return bs->encrypted; 1591ea2384d3Sbellard } 1592ea2384d3Sbellard 1593c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 1594c0f4ce77Saliguori { 1595c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 1596c0f4ce77Saliguori 1597c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 1598c0f4ce77Saliguori return 1; 1599c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 1600c0f4ce77Saliguori } 1601c0f4ce77Saliguori 1602ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 1603ea2384d3Sbellard { 1604ea2384d3Sbellard int ret; 1605ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 1606ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 1607ea2384d3Sbellard if (ret < 0) 1608ea2384d3Sbellard return ret; 1609ea2384d3Sbellard if (!bs->encrypted) 1610ea2384d3Sbellard return 0; 1611ea2384d3Sbellard } 1612fd04a2aeSShahar Havivi if (!bs->encrypted) { 1613fd04a2aeSShahar Havivi return -EINVAL; 1614fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 1615fd04a2aeSShahar Havivi return -ENOMEDIUM; 1616fd04a2aeSShahar Havivi } 1617c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 1618bb5fc20fSaliguori if (ret < 0) { 1619bb5fc20fSaliguori bs->valid_key = 0; 1620bb5fc20fSaliguori } else if (!bs->valid_key) { 1621bb5fc20fSaliguori bs->valid_key = 1; 1622bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 1623bb5fc20fSaliguori bs->media_changed = 1; 1624bb5fc20fSaliguori if (bs->change_cb) 1625db97ee6aSChristoph Hellwig bs->change_cb(bs->change_opaque, CHANGE_MEDIA); 1626bb5fc20fSaliguori } 1627c0f4ce77Saliguori return ret; 1628ea2384d3Sbellard } 1629ea2384d3Sbellard 1630ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 1631ea2384d3Sbellard { 163219cb3738Sbellard if (!bs->drv) { 1633ea2384d3Sbellard buf[0] = '\0'; 1634ea2384d3Sbellard } else { 1635ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 1636ea2384d3Sbellard } 1637ea2384d3Sbellard } 1638ea2384d3Sbellard 1639ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 1640ea2384d3Sbellard void *opaque) 1641ea2384d3Sbellard { 1642ea2384d3Sbellard BlockDriver *drv; 1643ea2384d3Sbellard 16448a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 1645ea2384d3Sbellard it(opaque, drv->format_name); 1646ea2384d3Sbellard } 1647ea2384d3Sbellard } 1648ea2384d3Sbellard 1649b338082bSbellard BlockDriverState *bdrv_find(const char *name) 1650b338082bSbellard { 1651b338082bSbellard BlockDriverState *bs; 1652b338082bSbellard 16531b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 16541b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 1655b338082bSbellard return bs; 1656b338082bSbellard } 16571b7bdbc1SStefan Hajnoczi } 1658b338082bSbellard return NULL; 1659b338082bSbellard } 1660b338082bSbellard 16612f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 16622f399b0aSMarkus Armbruster { 16632f399b0aSMarkus Armbruster if (!bs) { 16642f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 16652f399b0aSMarkus Armbruster } 16662f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 16672f399b0aSMarkus Armbruster } 16682f399b0aSMarkus Armbruster 166951de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 167081d0912dSbellard { 167181d0912dSbellard BlockDriverState *bs; 167281d0912dSbellard 16731b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 167451de9760Saliguori it(opaque, bs); 167581d0912dSbellard } 167681d0912dSbellard } 167781d0912dSbellard 1678ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1679ea2384d3Sbellard { 1680ea2384d3Sbellard return bs->device_name; 1681ea2384d3Sbellard } 1682ea2384d3Sbellard 1683205ef796SKevin Wolf int bdrv_flush(BlockDriverState *bs) 16847a6cba61Spbrook { 1685016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 1686205ef796SKevin Wolf return 0; 1687016f5cf6SAlexander Graf } 1688016f5cf6SAlexander Graf 1689*e7a8a783SKevin Wolf if (bs->drv && bdrv_has_async_flush(bs->drv) && qemu_in_coroutine()) { 1690*e7a8a783SKevin Wolf return bdrv_co_flush_em(bs); 1691*e7a8a783SKevin Wolf } 1692*e7a8a783SKevin Wolf 1693205ef796SKevin Wolf if (bs->drv && bs->drv->bdrv_flush) { 1694205ef796SKevin Wolf return bs->drv->bdrv_flush(bs); 1695205ef796SKevin Wolf } 1696205ef796SKevin Wolf 1697205ef796SKevin Wolf /* 1698205ef796SKevin Wolf * Some block drivers always operate in either writethrough or unsafe mode 1699205ef796SKevin Wolf * and don't support bdrv_flush therefore. Usually qemu doesn't know how 1700205ef796SKevin Wolf * the server works (because the behaviour is hardcoded or depends on 1701205ef796SKevin Wolf * server-side configuration), so we can't ensure that everything is safe 1702205ef796SKevin Wolf * on disk. Returning an error doesn't work because that would break guests 1703205ef796SKevin Wolf * even if the server operates in writethrough mode. 1704205ef796SKevin Wolf * 1705205ef796SKevin Wolf * Let's hope the user knows what he's doing. 1706205ef796SKevin Wolf */ 1707205ef796SKevin Wolf return 0; 17087a6cba61Spbrook } 17097a6cba61Spbrook 1710c6ca28d6Saliguori void bdrv_flush_all(void) 1711c6ca28d6Saliguori { 1712c6ca28d6Saliguori BlockDriverState *bs; 1713c6ca28d6Saliguori 17141b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1715c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 17161b7bdbc1SStefan Hajnoczi (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { 1717c6ca28d6Saliguori bdrv_flush(bs); 1718c6ca28d6Saliguori } 17191b7bdbc1SStefan Hajnoczi } 17201b7bdbc1SStefan Hajnoczi } 1721c6ca28d6Saliguori 1722f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 1723f2feebbdSKevin Wolf { 1724f2feebbdSKevin Wolf assert(bs->drv); 1725f2feebbdSKevin Wolf 1726336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 1727336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 1728f2feebbdSKevin Wolf } 1729f2feebbdSKevin Wolf 1730f2feebbdSKevin Wolf return 1; 1731f2feebbdSKevin Wolf } 1732f2feebbdSKevin Wolf 1733bb8bf76fSChristoph Hellwig int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 1734bb8bf76fSChristoph Hellwig { 1735bb8bf76fSChristoph Hellwig if (!bs->drv) { 1736bb8bf76fSChristoph Hellwig return -ENOMEDIUM; 1737bb8bf76fSChristoph Hellwig } 1738bb8bf76fSChristoph Hellwig if (!bs->drv->bdrv_discard) { 1739bb8bf76fSChristoph Hellwig return 0; 1740bb8bf76fSChristoph Hellwig } 1741bb8bf76fSChristoph Hellwig return bs->drv->bdrv_discard(bs, sector_num, nb_sectors); 1742bb8bf76fSChristoph Hellwig } 1743bb8bf76fSChristoph Hellwig 1744f58c7b35Sths /* 1745f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1746f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1747f58c7b35Sths * hence all their sectors are reported as allocated. 1748f58c7b35Sths * 1749f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1750f58c7b35Sths * the specified sector) that are known to be in the same 1751f58c7b35Sths * allocated/unallocated state. 1752f58c7b35Sths * 1753f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1754f58c7b35Sths */ 1755f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1756f58c7b35Sths int *pnum) 1757f58c7b35Sths { 1758f58c7b35Sths int64_t n; 1759f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1760f58c7b35Sths if (sector_num >= bs->total_sectors) { 1761f58c7b35Sths *pnum = 0; 1762f58c7b35Sths return 0; 1763f58c7b35Sths } 1764f58c7b35Sths n = bs->total_sectors - sector_num; 1765f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1766f58c7b35Sths return 1; 1767f58c7b35Sths } 1768f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1769f58c7b35Sths } 1770f58c7b35Sths 17712582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv, 17722582bfedSLuiz Capitulino BlockMonEventAction action, int is_read) 17732582bfedSLuiz Capitulino { 17742582bfedSLuiz Capitulino QObject *data; 17752582bfedSLuiz Capitulino const char *action_str; 17762582bfedSLuiz Capitulino 17772582bfedSLuiz Capitulino switch (action) { 17782582bfedSLuiz Capitulino case BDRV_ACTION_REPORT: 17792582bfedSLuiz Capitulino action_str = "report"; 17802582bfedSLuiz Capitulino break; 17812582bfedSLuiz Capitulino case BDRV_ACTION_IGNORE: 17822582bfedSLuiz Capitulino action_str = "ignore"; 17832582bfedSLuiz Capitulino break; 17842582bfedSLuiz Capitulino case BDRV_ACTION_STOP: 17852582bfedSLuiz Capitulino action_str = "stop"; 17862582bfedSLuiz Capitulino break; 17872582bfedSLuiz Capitulino default: 17882582bfedSLuiz Capitulino abort(); 17892582bfedSLuiz Capitulino } 17902582bfedSLuiz Capitulino 17912582bfedSLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 17922582bfedSLuiz Capitulino bdrv->device_name, 17932582bfedSLuiz Capitulino action_str, 17942582bfedSLuiz Capitulino is_read ? "read" : "write"); 17952582bfedSLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 17962582bfedSLuiz Capitulino 17972582bfedSLuiz Capitulino qobject_decref(data); 17982582bfedSLuiz Capitulino } 17992582bfedSLuiz Capitulino 1800d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque) 1801b338082bSbellard { 1802d15e5465SLuiz Capitulino QDict *bs_dict; 1803d15e5465SLuiz Capitulino Monitor *mon = opaque; 1804b338082bSbellard 1805d15e5465SLuiz Capitulino bs_dict = qobject_to_qdict(obj); 1806d15e5465SLuiz Capitulino 1807d8aeeb31SMarkus Armbruster monitor_printf(mon, "%s: removable=%d", 1808d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "device"), 1809d15e5465SLuiz Capitulino qdict_get_bool(bs_dict, "removable")); 1810d15e5465SLuiz Capitulino 1811d15e5465SLuiz Capitulino if (qdict_get_bool(bs_dict, "removable")) { 1812d15e5465SLuiz Capitulino monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); 1813b338082bSbellard } 1814d15e5465SLuiz Capitulino 1815d15e5465SLuiz Capitulino if (qdict_haskey(bs_dict, "inserted")) { 1816d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); 1817d15e5465SLuiz Capitulino 1818376253ecSaliguori monitor_printf(mon, " file="); 1819d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "file")); 1820d15e5465SLuiz Capitulino if (qdict_haskey(qdict, "backing_file")) { 1821376253ecSaliguori monitor_printf(mon, " backing_file="); 1822d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); 1823fef30743Sths } 1824d15e5465SLuiz Capitulino monitor_printf(mon, " ro=%d drv=%s encrypted=%d", 1825d15e5465SLuiz Capitulino qdict_get_bool(qdict, "ro"), 1826d15e5465SLuiz Capitulino qdict_get_str(qdict, "drv"), 1827d15e5465SLuiz Capitulino qdict_get_bool(qdict, "encrypted")); 1828b338082bSbellard } else { 1829376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1830b338082bSbellard } 1831d15e5465SLuiz Capitulino 1832376253ecSaliguori monitor_printf(mon, "\n"); 1833b338082bSbellard } 1834d15e5465SLuiz Capitulino 1835d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data) 1836d15e5465SLuiz Capitulino { 1837d15e5465SLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); 1838d15e5465SLuiz Capitulino } 1839d15e5465SLuiz Capitulino 1840d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data) 1841d15e5465SLuiz Capitulino { 1842d15e5465SLuiz Capitulino QList *bs_list; 1843d15e5465SLuiz Capitulino BlockDriverState *bs; 1844d15e5465SLuiz Capitulino 1845d15e5465SLuiz Capitulino bs_list = qlist_new(); 1846d15e5465SLuiz Capitulino 18471b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1848d15e5465SLuiz Capitulino QObject *bs_obj; 1849d15e5465SLuiz Capitulino 1850d8aeeb31SMarkus Armbruster bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': 'unknown', " 1851d15e5465SLuiz Capitulino "'removable': %i, 'locked': %i }", 1852d8aeeb31SMarkus Armbruster bs->device_name, bs->removable, 1853d15e5465SLuiz Capitulino bs->locked); 1854d15e5465SLuiz Capitulino 1855d15e5465SLuiz Capitulino if (bs->drv) { 1856d15e5465SLuiz Capitulino QObject *obj; 1857d15e5465SLuiz Capitulino QDict *bs_dict = qobject_to_qdict(bs_obj); 1858d15e5465SLuiz Capitulino 1859d15e5465SLuiz Capitulino obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " 1860d15e5465SLuiz Capitulino "'encrypted': %i }", 1861d15e5465SLuiz Capitulino bs->filename, bs->read_only, 1862d15e5465SLuiz Capitulino bs->drv->format_name, 1863d15e5465SLuiz Capitulino bdrv_is_encrypted(bs)); 1864d15e5465SLuiz Capitulino if (bs->backing_file[0] != '\0') { 1865d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(obj); 1866d15e5465SLuiz Capitulino qdict_put(qdict, "backing_file", 1867d15e5465SLuiz Capitulino qstring_from_str(bs->backing_file)); 1868d15e5465SLuiz Capitulino } 1869d15e5465SLuiz Capitulino 1870d15e5465SLuiz Capitulino qdict_put_obj(bs_dict, "inserted", obj); 1871d15e5465SLuiz Capitulino } 1872d15e5465SLuiz Capitulino qlist_append_obj(bs_list, bs_obj); 1873d15e5465SLuiz Capitulino } 1874d15e5465SLuiz Capitulino 1875d15e5465SLuiz Capitulino *ret_data = QOBJECT(bs_list); 1876b338082bSbellard } 1877a36e69ddSths 1878218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque) 1879a36e69ddSths { 1880218a536aSLuiz Capitulino QDict *qdict; 1881218a536aSLuiz Capitulino Monitor *mon = opaque; 1882218a536aSLuiz Capitulino 1883218a536aSLuiz Capitulino qdict = qobject_to_qdict(data); 1884218a536aSLuiz Capitulino monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); 1885218a536aSLuiz Capitulino 1886218a536aSLuiz Capitulino qdict = qobject_to_qdict(qdict_get(qdict, "stats")); 1887218a536aSLuiz Capitulino monitor_printf(mon, " rd_bytes=%" PRId64 1888218a536aSLuiz Capitulino " wr_bytes=%" PRId64 1889218a536aSLuiz Capitulino " rd_operations=%" PRId64 1890218a536aSLuiz Capitulino " wr_operations=%" PRId64 1891218a536aSLuiz Capitulino "\n", 1892218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_bytes"), 1893218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_bytes"), 1894218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_operations"), 1895218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_operations")); 1896218a536aSLuiz Capitulino } 1897218a536aSLuiz Capitulino 1898218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data) 1899218a536aSLuiz Capitulino { 1900218a536aSLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); 1901218a536aSLuiz Capitulino } 1902218a536aSLuiz Capitulino 1903294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs) 1904294cc35fSKevin Wolf { 1905294cc35fSKevin Wolf QObject *res; 1906294cc35fSKevin Wolf QDict *dict; 1907294cc35fSKevin Wolf 1908294cc35fSKevin Wolf res = qobject_from_jsonf("{ 'stats': {" 1909294cc35fSKevin Wolf "'rd_bytes': %" PRId64 "," 1910294cc35fSKevin Wolf "'wr_bytes': %" PRId64 "," 1911294cc35fSKevin Wolf "'rd_operations': %" PRId64 "," 1912294cc35fSKevin Wolf "'wr_operations': %" PRId64 "," 1913294cc35fSKevin Wolf "'wr_highest_offset': %" PRId64 1914294cc35fSKevin Wolf "} }", 1915294cc35fSKevin Wolf bs->rd_bytes, bs->wr_bytes, 1916294cc35fSKevin Wolf bs->rd_ops, bs->wr_ops, 19175ffbbc67SBlue Swirl bs->wr_highest_sector * 19185ffbbc67SBlue Swirl (uint64_t)BDRV_SECTOR_SIZE); 1919294cc35fSKevin Wolf dict = qobject_to_qdict(res); 1920294cc35fSKevin Wolf 1921294cc35fSKevin Wolf if (*bs->device_name) { 1922294cc35fSKevin Wolf qdict_put(dict, "device", qstring_from_str(bs->device_name)); 1923294cc35fSKevin Wolf } 1924294cc35fSKevin Wolf 1925294cc35fSKevin Wolf if (bs->file) { 1926294cc35fSKevin Wolf QObject *parent = bdrv_info_stats_bs(bs->file); 1927294cc35fSKevin Wolf qdict_put_obj(dict, "parent", parent); 1928294cc35fSKevin Wolf } 1929294cc35fSKevin Wolf 1930294cc35fSKevin Wolf return res; 1931294cc35fSKevin Wolf } 1932294cc35fSKevin Wolf 1933218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data) 1934218a536aSLuiz Capitulino { 1935218a536aSLuiz Capitulino QObject *obj; 1936218a536aSLuiz Capitulino QList *devices; 1937a36e69ddSths BlockDriverState *bs; 1938a36e69ddSths 1939218a536aSLuiz Capitulino devices = qlist_new(); 1940218a536aSLuiz Capitulino 19411b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1942294cc35fSKevin Wolf obj = bdrv_info_stats_bs(bs); 1943218a536aSLuiz Capitulino qlist_append_obj(devices, obj); 1944a36e69ddSths } 1945218a536aSLuiz Capitulino 1946218a536aSLuiz Capitulino *ret_data = QOBJECT(devices); 1947a36e69ddSths } 1948ea2384d3Sbellard 1949045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1950045df330Saliguori { 1951045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1952045df330Saliguori return bs->backing_file; 1953045df330Saliguori else if (bs->encrypted) 1954045df330Saliguori return bs->filename; 1955045df330Saliguori else 1956045df330Saliguori return NULL; 1957045df330Saliguori } 1958045df330Saliguori 195983f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 196083f64091Sbellard char *filename, int filename_size) 196183f64091Sbellard { 1962b783e409SKevin Wolf if (!bs->backing_file) { 196383f64091Sbellard pstrcpy(filename, filename_size, ""); 196483f64091Sbellard } else { 196583f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 196683f64091Sbellard } 196783f64091Sbellard } 196883f64091Sbellard 1969faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1970faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1971faea38e7Sbellard { 1972faea38e7Sbellard BlockDriver *drv = bs->drv; 1973faea38e7Sbellard if (!drv) 197419cb3738Sbellard return -ENOMEDIUM; 1975faea38e7Sbellard if (!drv->bdrv_write_compressed) 1976faea38e7Sbellard return -ENOTSUP; 1977fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 1978fbb7b4e0SKevin Wolf return -EIO; 19797cd1e32aSlirans@il.ibm.com 1980c6d22830SJan Kiszka if (bs->dirty_bitmap) { 19817cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 19827cd1e32aSlirans@il.ibm.com } 19837cd1e32aSlirans@il.ibm.com 1984faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1985faea38e7Sbellard } 1986faea38e7Sbellard 1987faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1988faea38e7Sbellard { 1989faea38e7Sbellard BlockDriver *drv = bs->drv; 1990faea38e7Sbellard if (!drv) 199119cb3738Sbellard return -ENOMEDIUM; 1992faea38e7Sbellard if (!drv->bdrv_get_info) 1993faea38e7Sbellard return -ENOTSUP; 1994faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1995faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1996faea38e7Sbellard } 1997faea38e7Sbellard 199845566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 199945566e9cSChristoph Hellwig int64_t pos, int size) 2000178e08a5Saliguori { 2001178e08a5Saliguori BlockDriver *drv = bs->drv; 2002178e08a5Saliguori if (!drv) 2003178e08a5Saliguori return -ENOMEDIUM; 20047cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 200545566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 20067cdb1f6dSMORITA Kazutaka if (bs->file) 20077cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 20087cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2009178e08a5Saliguori } 2010178e08a5Saliguori 201145566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 201245566e9cSChristoph Hellwig int64_t pos, int size) 2013178e08a5Saliguori { 2014178e08a5Saliguori BlockDriver *drv = bs->drv; 2015178e08a5Saliguori if (!drv) 2016178e08a5Saliguori return -ENOMEDIUM; 20177cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 201845566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 20197cdb1f6dSMORITA Kazutaka if (bs->file) 20207cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 20217cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2022178e08a5Saliguori } 2023178e08a5Saliguori 20248b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 20258b9b0cc2SKevin Wolf { 20268b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 20278b9b0cc2SKevin Wolf 20288b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 20298b9b0cc2SKevin Wolf return; 20308b9b0cc2SKevin Wolf } 20318b9b0cc2SKevin Wolf 20328b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 20338b9b0cc2SKevin Wolf 20348b9b0cc2SKevin Wolf } 20358b9b0cc2SKevin Wolf 2036faea38e7Sbellard /**************************************************************/ 2037faea38e7Sbellard /* handling of snapshots */ 2038faea38e7Sbellard 2039feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 2040feeee5acSMiguel Di Ciurcio Filho { 2041feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 2042feeee5acSMiguel Di Ciurcio Filho if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) { 2043feeee5acSMiguel Di Ciurcio Filho return 0; 2044feeee5acSMiguel Di Ciurcio Filho } 2045feeee5acSMiguel Di Ciurcio Filho 2046feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 2047feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 2048feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 2049feeee5acSMiguel Di Ciurcio Filho } 2050feeee5acSMiguel Di Ciurcio Filho return 0; 2051feeee5acSMiguel Di Ciurcio Filho } 2052feeee5acSMiguel Di Ciurcio Filho 2053feeee5acSMiguel Di Ciurcio Filho return 1; 2054feeee5acSMiguel Di Ciurcio Filho } 2055feeee5acSMiguel Di Ciurcio Filho 2056199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 2057199630b6SBlue Swirl { 2058199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 2059199630b6SBlue Swirl } 2060199630b6SBlue Swirl 2061f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 2062f9092b10SMarkus Armbruster { 2063f9092b10SMarkus Armbruster BlockDriverState *bs; 2064f9092b10SMarkus Armbruster 20653ac906f7SMarkus Armbruster if (bs_snapshots) { 2066f9092b10SMarkus Armbruster return bs_snapshots; 20673ac906f7SMarkus Armbruster } 2068f9092b10SMarkus Armbruster 2069f9092b10SMarkus Armbruster bs = NULL; 2070f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 2071f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 20723ac906f7SMarkus Armbruster bs_snapshots = bs; 20733ac906f7SMarkus Armbruster return bs; 2074f9092b10SMarkus Armbruster } 2075f9092b10SMarkus Armbruster } 2076f9092b10SMarkus Armbruster return NULL; 2077f9092b10SMarkus Armbruster } 2078f9092b10SMarkus Armbruster 2079faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 2080faea38e7Sbellard QEMUSnapshotInfo *sn_info) 2081faea38e7Sbellard { 2082faea38e7Sbellard BlockDriver *drv = bs->drv; 2083faea38e7Sbellard if (!drv) 208419cb3738Sbellard return -ENOMEDIUM; 20857cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 2086faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 20877cdb1f6dSMORITA Kazutaka if (bs->file) 20887cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 20897cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2090faea38e7Sbellard } 2091faea38e7Sbellard 2092faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 2093faea38e7Sbellard const char *snapshot_id) 2094faea38e7Sbellard { 2095faea38e7Sbellard BlockDriver *drv = bs->drv; 20967cdb1f6dSMORITA Kazutaka int ret, open_ret; 20977cdb1f6dSMORITA Kazutaka 2098faea38e7Sbellard if (!drv) 209919cb3738Sbellard return -ENOMEDIUM; 21007cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 2101faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 21027cdb1f6dSMORITA Kazutaka 21037cdb1f6dSMORITA Kazutaka if (bs->file) { 21047cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 21057cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 21067cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 21077cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 21087cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 21097cdb1f6dSMORITA Kazutaka bs->drv = NULL; 21107cdb1f6dSMORITA Kazutaka return open_ret; 21117cdb1f6dSMORITA Kazutaka } 21127cdb1f6dSMORITA Kazutaka return ret; 21137cdb1f6dSMORITA Kazutaka } 21147cdb1f6dSMORITA Kazutaka 21157cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2116faea38e7Sbellard } 2117faea38e7Sbellard 2118faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 2119faea38e7Sbellard { 2120faea38e7Sbellard BlockDriver *drv = bs->drv; 2121faea38e7Sbellard if (!drv) 212219cb3738Sbellard return -ENOMEDIUM; 21237cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 2124faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 21257cdb1f6dSMORITA Kazutaka if (bs->file) 21267cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 21277cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2128faea38e7Sbellard } 2129faea38e7Sbellard 2130faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 2131faea38e7Sbellard QEMUSnapshotInfo **psn_info) 2132faea38e7Sbellard { 2133faea38e7Sbellard BlockDriver *drv = bs->drv; 2134faea38e7Sbellard if (!drv) 213519cb3738Sbellard return -ENOMEDIUM; 21367cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 2137faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 21387cdb1f6dSMORITA Kazutaka if (bs->file) 21397cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 21407cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2141faea38e7Sbellard } 2142faea38e7Sbellard 214351ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 214451ef6727Sedison const char *snapshot_name) 214551ef6727Sedison { 214651ef6727Sedison BlockDriver *drv = bs->drv; 214751ef6727Sedison if (!drv) { 214851ef6727Sedison return -ENOMEDIUM; 214951ef6727Sedison } 215051ef6727Sedison if (!bs->read_only) { 215151ef6727Sedison return -EINVAL; 215251ef6727Sedison } 215351ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 215451ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 215551ef6727Sedison } 215651ef6727Sedison return -ENOTSUP; 215751ef6727Sedison } 215851ef6727Sedison 2159faea38e7Sbellard #define NB_SUFFIXES 4 2160faea38e7Sbellard 2161faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 2162faea38e7Sbellard { 2163faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 2164faea38e7Sbellard int64_t base; 2165faea38e7Sbellard int i; 2166faea38e7Sbellard 2167faea38e7Sbellard if (size <= 999) { 2168faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 2169faea38e7Sbellard } else { 2170faea38e7Sbellard base = 1024; 2171faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 2172faea38e7Sbellard if (size < (10 * base)) { 2173faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 2174faea38e7Sbellard (double)size / base, 2175faea38e7Sbellard suffixes[i]); 2176faea38e7Sbellard break; 2177faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 2178faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 2179faea38e7Sbellard ((size + (base >> 1)) / base), 2180faea38e7Sbellard suffixes[i]); 2181faea38e7Sbellard break; 2182faea38e7Sbellard } 2183faea38e7Sbellard base = base * 1024; 2184faea38e7Sbellard } 2185faea38e7Sbellard } 2186faea38e7Sbellard return buf; 2187faea38e7Sbellard } 2188faea38e7Sbellard 2189faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 2190faea38e7Sbellard { 2191faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 21923b9f94e1Sbellard #ifdef _WIN32 21933b9f94e1Sbellard struct tm *ptm; 21943b9f94e1Sbellard #else 2195faea38e7Sbellard struct tm tm; 21963b9f94e1Sbellard #endif 2197faea38e7Sbellard time_t ti; 2198faea38e7Sbellard int64_t secs; 2199faea38e7Sbellard 2200faea38e7Sbellard if (!sn) { 2201faea38e7Sbellard snprintf(buf, buf_size, 2202faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2203faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 2204faea38e7Sbellard } else { 2205faea38e7Sbellard ti = sn->date_sec; 22063b9f94e1Sbellard #ifdef _WIN32 22073b9f94e1Sbellard ptm = localtime(&ti); 22083b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 22093b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 22103b9f94e1Sbellard #else 2211faea38e7Sbellard localtime_r(&ti, &tm); 2212faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 2213faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 22143b9f94e1Sbellard #endif 2215faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 2216faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 2217faea38e7Sbellard "%02d:%02d:%02d.%03d", 2218faea38e7Sbellard (int)(secs / 3600), 2219faea38e7Sbellard (int)((secs / 60) % 60), 2220faea38e7Sbellard (int)(secs % 60), 2221faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 2222faea38e7Sbellard snprintf(buf, buf_size, 2223faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2224faea38e7Sbellard sn->id_str, sn->name, 2225faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 2226faea38e7Sbellard date_buf, 2227faea38e7Sbellard clock_buf); 2228faea38e7Sbellard } 2229faea38e7Sbellard return buf; 2230faea38e7Sbellard } 2231faea38e7Sbellard 223283f64091Sbellard 2233ea2384d3Sbellard /**************************************************************/ 223483f64091Sbellard /* async I/Os */ 2235ea2384d3Sbellard 22363b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 2237f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 223883f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 2239ea2384d3Sbellard { 224083f64091Sbellard BlockDriver *drv = bs->drv; 2241a36e69ddSths BlockDriverAIOCB *ret; 2242ea2384d3Sbellard 2243bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 2244bbf0a440SStefan Hajnoczi 224519cb3738Sbellard if (!drv) 2246ce1a14dcSpbrook return NULL; 224771d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 224871d0770cSaliguori return NULL; 224983f64091Sbellard 2250f141eafeSaliguori ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, 2251f141eafeSaliguori cb, opaque); 2252a36e69ddSths 2253a36e69ddSths if (ret) { 2254a36e69ddSths /* Update stats even though technically transfer has not happened. */ 22556ea44308SJan Kiszka bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 2256a36e69ddSths bs->rd_ops ++; 2257a36e69ddSths } 2258a36e69ddSths 2259a36e69ddSths return ret; 226083f64091Sbellard } 226183f64091Sbellard 22624dcafbb1SMarcelo Tosatti typedef struct BlockCompleteData { 22634dcafbb1SMarcelo Tosatti BlockDriverCompletionFunc *cb; 22644dcafbb1SMarcelo Tosatti void *opaque; 22654dcafbb1SMarcelo Tosatti BlockDriverState *bs; 22664dcafbb1SMarcelo Tosatti int64_t sector_num; 22674dcafbb1SMarcelo Tosatti int nb_sectors; 22684dcafbb1SMarcelo Tosatti } BlockCompleteData; 22694dcafbb1SMarcelo Tosatti 22704dcafbb1SMarcelo Tosatti static void block_complete_cb(void *opaque, int ret) 22714dcafbb1SMarcelo Tosatti { 22724dcafbb1SMarcelo Tosatti BlockCompleteData *b = opaque; 22734dcafbb1SMarcelo Tosatti 22744dcafbb1SMarcelo Tosatti if (b->bs->dirty_bitmap) { 22754dcafbb1SMarcelo Tosatti set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1); 22764dcafbb1SMarcelo Tosatti } 22774dcafbb1SMarcelo Tosatti b->cb(b->opaque, ret); 22784dcafbb1SMarcelo Tosatti qemu_free(b); 22794dcafbb1SMarcelo Tosatti } 22804dcafbb1SMarcelo Tosatti 22814dcafbb1SMarcelo Tosatti static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs, 22824dcafbb1SMarcelo Tosatti int64_t sector_num, 22834dcafbb1SMarcelo Tosatti int nb_sectors, 22844dcafbb1SMarcelo Tosatti BlockDriverCompletionFunc *cb, 22854dcafbb1SMarcelo Tosatti void *opaque) 22864dcafbb1SMarcelo Tosatti { 22874dcafbb1SMarcelo Tosatti BlockCompleteData *blkdata = qemu_mallocz(sizeof(BlockCompleteData)); 22884dcafbb1SMarcelo Tosatti 22894dcafbb1SMarcelo Tosatti blkdata->bs = bs; 22904dcafbb1SMarcelo Tosatti blkdata->cb = cb; 22914dcafbb1SMarcelo Tosatti blkdata->opaque = opaque; 22924dcafbb1SMarcelo Tosatti blkdata->sector_num = sector_num; 22934dcafbb1SMarcelo Tosatti blkdata->nb_sectors = nb_sectors; 22944dcafbb1SMarcelo Tosatti 22954dcafbb1SMarcelo Tosatti return blkdata; 22964dcafbb1SMarcelo Tosatti } 22974dcafbb1SMarcelo Tosatti 2298f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 2299f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 230083f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 23017674e7bfSbellard { 230283f64091Sbellard BlockDriver *drv = bs->drv; 2303a36e69ddSths BlockDriverAIOCB *ret; 23044dcafbb1SMarcelo Tosatti BlockCompleteData *blk_cb_data; 230583f64091Sbellard 2306bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 2307bbf0a440SStefan Hajnoczi 230819cb3738Sbellard if (!drv) 2309ce1a14dcSpbrook return NULL; 231083f64091Sbellard if (bs->read_only) 2311ce1a14dcSpbrook return NULL; 231271d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 231371d0770cSaliguori return NULL; 231483f64091Sbellard 2315c6d22830SJan Kiszka if (bs->dirty_bitmap) { 23164dcafbb1SMarcelo Tosatti blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb, 23174dcafbb1SMarcelo Tosatti opaque); 23184dcafbb1SMarcelo Tosatti cb = &block_complete_cb; 23194dcafbb1SMarcelo Tosatti opaque = blk_cb_data; 23207cd1e32aSlirans@il.ibm.com } 23217cd1e32aSlirans@il.ibm.com 2322f141eafeSaliguori ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, 2323f141eafeSaliguori cb, opaque); 2324a36e69ddSths 2325a36e69ddSths if (ret) { 2326a36e69ddSths /* Update stats even though technically transfer has not happened. */ 23276ea44308SJan Kiszka bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 2328a36e69ddSths bs->wr_ops ++; 2329294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2330294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2331294cc35fSKevin Wolf } 2332a36e69ddSths } 2333a36e69ddSths 2334a36e69ddSths return ret; 233583f64091Sbellard } 233683f64091Sbellard 233740b4f539SKevin Wolf 233840b4f539SKevin Wolf typedef struct MultiwriteCB { 233940b4f539SKevin Wolf int error; 234040b4f539SKevin Wolf int num_requests; 234140b4f539SKevin Wolf int num_callbacks; 234240b4f539SKevin Wolf struct { 234340b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 234440b4f539SKevin Wolf void *opaque; 234540b4f539SKevin Wolf QEMUIOVector *free_qiov; 234640b4f539SKevin Wolf void *free_buf; 234740b4f539SKevin Wolf } callbacks[]; 234840b4f539SKevin Wolf } MultiwriteCB; 234940b4f539SKevin Wolf 235040b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 235140b4f539SKevin Wolf { 235240b4f539SKevin Wolf int i; 235340b4f539SKevin Wolf 235440b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 235540b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 23561e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 23571e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 23581e1ea48dSStefan Hajnoczi } 235940b4f539SKevin Wolf qemu_free(mcb->callbacks[i].free_qiov); 2360f8a83245SHerve Poussineau qemu_vfree(mcb->callbacks[i].free_buf); 236140b4f539SKevin Wolf } 236240b4f539SKevin Wolf } 236340b4f539SKevin Wolf 236440b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 236540b4f539SKevin Wolf { 236640b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 236740b4f539SKevin Wolf 23686d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 23696d519a5fSStefan Hajnoczi 2370cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 237140b4f539SKevin Wolf mcb->error = ret; 237240b4f539SKevin Wolf } 237340b4f539SKevin Wolf 237440b4f539SKevin Wolf mcb->num_requests--; 237540b4f539SKevin Wolf if (mcb->num_requests == 0) { 237640b4f539SKevin Wolf multiwrite_user_cb(mcb); 237740b4f539SKevin Wolf qemu_free(mcb); 237840b4f539SKevin Wolf } 237940b4f539SKevin Wolf } 238040b4f539SKevin Wolf 238140b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 238240b4f539SKevin Wolf { 238377be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 238477be4366SChristoph Hellwig 238577be4366SChristoph Hellwig /* 238677be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 238777be4366SChristoph Hellwig * here as that could overflow the return value. 238877be4366SChristoph Hellwig */ 238977be4366SChristoph Hellwig if (req1->sector > req2->sector) { 239077be4366SChristoph Hellwig return 1; 239177be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 239277be4366SChristoph Hellwig return -1; 239377be4366SChristoph Hellwig } else { 239477be4366SChristoph Hellwig return 0; 239577be4366SChristoph Hellwig } 239640b4f539SKevin Wolf } 239740b4f539SKevin Wolf 239840b4f539SKevin Wolf /* 239940b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 240040b4f539SKevin Wolf * requests that remain after merging. 240140b4f539SKevin Wolf */ 240240b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 240340b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 240440b4f539SKevin Wolf { 240540b4f539SKevin Wolf int i, outidx; 240640b4f539SKevin Wolf 240740b4f539SKevin Wolf // Sort requests by start sector 240840b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 240940b4f539SKevin Wolf 241040b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 241140b4f539SKevin Wolf // filling up gaps with zero sectors. 241240b4f539SKevin Wolf outidx = 0; 241340b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 241440b4f539SKevin Wolf int merge = 0; 241540b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 241640b4f539SKevin Wolf 241740b4f539SKevin Wolf // This handles the cases that are valid for all block drivers, namely 241840b4f539SKevin Wolf // exactly sequential writes and overlapping writes. 241940b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 242040b4f539SKevin Wolf merge = 1; 242140b4f539SKevin Wolf } 242240b4f539SKevin Wolf 242340b4f539SKevin Wolf // The block driver may decide that it makes sense to combine requests 242440b4f539SKevin Wolf // even if there is a gap of some sectors between them. In this case, 242540b4f539SKevin Wolf // the gap is filled with zeros (therefore only applicable for yet 242640b4f539SKevin Wolf // unused space in format like qcow2). 242740b4f539SKevin Wolf if (!merge && bs->drv->bdrv_merge_requests) { 242840b4f539SKevin Wolf merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); 242940b4f539SKevin Wolf } 243040b4f539SKevin Wolf 2431e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 2432e2a305fbSChristoph Hellwig merge = 0; 2433e2a305fbSChristoph Hellwig } 2434e2a305fbSChristoph Hellwig 243540b4f539SKevin Wolf if (merge) { 243640b4f539SKevin Wolf size_t size; 243740b4f539SKevin Wolf QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); 243840b4f539SKevin Wolf qemu_iovec_init(qiov, 243940b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 244040b4f539SKevin Wolf 244140b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 244240b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 244340b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 244440b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 244540b4f539SKevin Wolf 244640b4f539SKevin Wolf // We might need to add some zeros between the two requests 244740b4f539SKevin Wolf if (reqs[i].sector > oldreq_last) { 244840b4f539SKevin Wolf size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; 244940b4f539SKevin Wolf uint8_t *buf = qemu_blockalign(bs, zero_bytes); 245040b4f539SKevin Wolf memset(buf, 0, zero_bytes); 245140b4f539SKevin Wolf qemu_iovec_add(qiov, buf, zero_bytes); 245240b4f539SKevin Wolf mcb->callbacks[i].free_buf = buf; 245340b4f539SKevin Wolf } 245440b4f539SKevin Wolf 245540b4f539SKevin Wolf // Add the second request 245640b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 245740b4f539SKevin Wolf 2458cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 245940b4f539SKevin Wolf reqs[outidx].qiov = qiov; 246040b4f539SKevin Wolf 246140b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 246240b4f539SKevin Wolf } else { 246340b4f539SKevin Wolf outidx++; 246440b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 246540b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 246640b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 246740b4f539SKevin Wolf } 246840b4f539SKevin Wolf } 246940b4f539SKevin Wolf 247040b4f539SKevin Wolf return outidx + 1; 247140b4f539SKevin Wolf } 247240b4f539SKevin Wolf 247340b4f539SKevin Wolf /* 247440b4f539SKevin Wolf * Submit multiple AIO write requests at once. 247540b4f539SKevin Wolf * 247640b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 247740b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 247840b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 247940b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 248040b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 248140b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 248240b4f539SKevin Wolf * 248340b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 248440b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 248540b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 248640b4f539SKevin Wolf */ 248740b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 248840b4f539SKevin Wolf { 248940b4f539SKevin Wolf BlockDriverAIOCB *acb; 249040b4f539SKevin Wolf MultiwriteCB *mcb; 249140b4f539SKevin Wolf int i; 249240b4f539SKevin Wolf 2493301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 2494301db7c2SRyan Harper if (bs->drv == NULL) { 2495301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 2496301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 2497301db7c2SRyan Harper } 2498301db7c2SRyan Harper return -1; 2499301db7c2SRyan Harper } 2500301db7c2SRyan Harper 250140b4f539SKevin Wolf if (num_reqs == 0) { 250240b4f539SKevin Wolf return 0; 250340b4f539SKevin Wolf } 250440b4f539SKevin Wolf 250540b4f539SKevin Wolf // Create MultiwriteCB structure 250640b4f539SKevin Wolf mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 250740b4f539SKevin Wolf mcb->num_requests = 0; 250840b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 250940b4f539SKevin Wolf 251040b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 251140b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 251240b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 251340b4f539SKevin Wolf } 251440b4f539SKevin Wolf 251540b4f539SKevin Wolf // Check for mergable requests 251640b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 251740b4f539SKevin Wolf 25186d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 25196d519a5fSStefan Hajnoczi 2520453f9a16SKevin Wolf /* 2521453f9a16SKevin Wolf * Run the aio requests. As soon as one request can't be submitted 2522453f9a16SKevin Wolf * successfully, fail all requests that are not yet submitted (we must 2523453f9a16SKevin Wolf * return failure for all requests anyway) 2524453f9a16SKevin Wolf * 2525453f9a16SKevin Wolf * num_requests cannot be set to the right value immediately: If 2526453f9a16SKevin Wolf * bdrv_aio_writev fails for some request, num_requests would be too high 2527453f9a16SKevin Wolf * and therefore multiwrite_cb() would never recognize the multiwrite 2528453f9a16SKevin Wolf * request as completed. We also cannot use the loop variable i to set it 2529453f9a16SKevin Wolf * when the first request fails because the callback may already have been 2530453f9a16SKevin Wolf * called for previously submitted requests. Thus, num_requests must be 2531453f9a16SKevin Wolf * incremented for each request that is submitted. 2532453f9a16SKevin Wolf * 2533453f9a16SKevin Wolf * The problem that callbacks may be called early also means that we need 2534453f9a16SKevin Wolf * to take care that num_requests doesn't become 0 before all requests are 2535453f9a16SKevin Wolf * submitted - multiwrite_cb() would consider the multiwrite request 2536453f9a16SKevin Wolf * completed. A dummy request that is "completed" by a manual call to 2537453f9a16SKevin Wolf * multiwrite_cb() takes care of this. 2538453f9a16SKevin Wolf */ 2539453f9a16SKevin Wolf mcb->num_requests = 1; 2540453f9a16SKevin Wolf 25416d519a5fSStefan Hajnoczi // Run the aio requests 254240b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 2543453f9a16SKevin Wolf mcb->num_requests++; 254440b4f539SKevin Wolf acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 254540b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 254640b4f539SKevin Wolf 254740b4f539SKevin Wolf if (acb == NULL) { 254840b4f539SKevin Wolf // We can only fail the whole thing if no request has been 254940b4f539SKevin Wolf // submitted yet. Otherwise we'll wait for the submitted AIOs to 255040b4f539SKevin Wolf // complete and report the error in the callback. 2551453f9a16SKevin Wolf if (i == 0) { 25526d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite_earlyfail(mcb); 255340b4f539SKevin Wolf goto fail; 255440b4f539SKevin Wolf } else { 25556d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite_latefail(mcb, i); 25567eb58a6cSKevin Wolf multiwrite_cb(mcb, -EIO); 255740b4f539SKevin Wolf break; 255840b4f539SKevin Wolf } 255940b4f539SKevin Wolf } 256040b4f539SKevin Wolf } 256140b4f539SKevin Wolf 2562453f9a16SKevin Wolf /* Complete the dummy request */ 2563453f9a16SKevin Wolf multiwrite_cb(mcb, 0); 2564453f9a16SKevin Wolf 256540b4f539SKevin Wolf return 0; 256640b4f539SKevin Wolf 256740b4f539SKevin Wolf fail: 2568453f9a16SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 2569453f9a16SKevin Wolf reqs[i].error = -EIO; 2570453f9a16SKevin Wolf } 2571af474591SBruce Rogers qemu_free(mcb); 257240b4f539SKevin Wolf return -1; 257340b4f539SKevin Wolf } 257440b4f539SKevin Wolf 2575b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 2576b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2577b2e12bc6SChristoph Hellwig { 2578b2e12bc6SChristoph Hellwig BlockDriver *drv = bs->drv; 2579b2e12bc6SChristoph Hellwig 2580a13aac04SStefan Hajnoczi trace_bdrv_aio_flush(bs, opaque); 2581a13aac04SStefan Hajnoczi 2582016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 2583016f5cf6SAlexander Graf return bdrv_aio_noop_em(bs, cb, opaque); 2584016f5cf6SAlexander Graf } 2585016f5cf6SAlexander Graf 2586b2e12bc6SChristoph Hellwig if (!drv) 2587b2e12bc6SChristoph Hellwig return NULL; 2588b2e12bc6SChristoph Hellwig return drv->bdrv_aio_flush(bs, cb, opaque); 2589b2e12bc6SChristoph Hellwig } 2590b2e12bc6SChristoph Hellwig 259183f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 259283f64091Sbellard { 25936bbff9a0Saliguori acb->pool->cancel(acb); 259483f64091Sbellard } 259583f64091Sbellard 259683f64091Sbellard 259783f64091Sbellard /**************************************************************/ 259883f64091Sbellard /* async block device emulation */ 259983f64091Sbellard 2600c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 2601c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 2602c16b5a2cSChristoph Hellwig QEMUBH *bh; 2603c16b5a2cSChristoph Hellwig int ret; 2604c16b5a2cSChristoph Hellwig /* vector translation state */ 2605c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 2606c16b5a2cSChristoph Hellwig uint8_t *bounce; 2607c16b5a2cSChristoph Hellwig int is_write; 2608c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 2609c16b5a2cSChristoph Hellwig 2610c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 2611c16b5a2cSChristoph Hellwig { 2612b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 2613b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 26146a7ad299SDor Laor qemu_bh_delete(acb->bh); 261536afc451SAvi Kivity acb->bh = NULL; 2616c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 2617c16b5a2cSChristoph Hellwig } 2618c16b5a2cSChristoph Hellwig 2619c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 2620c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 2621c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 2622c16b5a2cSChristoph Hellwig }; 2623c16b5a2cSChristoph Hellwig 262483f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 2625beac80cdSbellard { 2626ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 2627f141eafeSaliguori 2628f141eafeSaliguori if (!acb->is_write) 2629f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 2630ceb42de8Saliguori qemu_vfree(acb->bounce); 2631ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 26326a7ad299SDor Laor qemu_bh_delete(acb->bh); 263336afc451SAvi Kivity acb->bh = NULL; 2634ce1a14dcSpbrook qemu_aio_release(acb); 2635beac80cdSbellard } 2636beac80cdSbellard 2637f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 2638f141eafeSaliguori int64_t sector_num, 2639f141eafeSaliguori QEMUIOVector *qiov, 2640f141eafeSaliguori int nb_sectors, 2641f141eafeSaliguori BlockDriverCompletionFunc *cb, 2642f141eafeSaliguori void *opaque, 2643f141eafeSaliguori int is_write) 2644f141eafeSaliguori 2645ea2384d3Sbellard { 2646ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 264783f64091Sbellard 2648c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2649f141eafeSaliguori acb->is_write = is_write; 2650f141eafeSaliguori acb->qiov = qiov; 2651e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 2652f141eafeSaliguori 2653ce1a14dcSpbrook if (!acb->bh) 2654ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2655f141eafeSaliguori 2656f141eafeSaliguori if (is_write) { 2657f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 2658f141eafeSaliguori acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 2659f141eafeSaliguori } else { 2660f141eafeSaliguori acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 2661f141eafeSaliguori } 2662f141eafeSaliguori 2663ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 2664f141eafeSaliguori 2665ce1a14dcSpbrook return &acb->common; 26667a6cba61Spbrook } 26677a6cba61Spbrook 2668f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 2669f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2670ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 267183f64091Sbellard { 2672f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 267383f64091Sbellard } 267483f64091Sbellard 2675f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 2676f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2677f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 2678f141eafeSaliguori { 2679f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 2680f141eafeSaliguori } 2681f141eafeSaliguori 268268485420SKevin Wolf 268368485420SKevin Wolf typedef struct BlockDriverAIOCBCoroutine { 268468485420SKevin Wolf BlockDriverAIOCB common; 268568485420SKevin Wolf BlockRequest req; 268668485420SKevin Wolf bool is_write; 268768485420SKevin Wolf QEMUBH* bh; 268868485420SKevin Wolf } BlockDriverAIOCBCoroutine; 268968485420SKevin Wolf 269068485420SKevin Wolf static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb) 269168485420SKevin Wolf { 269268485420SKevin Wolf qemu_aio_flush(); 269368485420SKevin Wolf } 269468485420SKevin Wolf 269568485420SKevin Wolf static AIOPool bdrv_em_co_aio_pool = { 269668485420SKevin Wolf .aiocb_size = sizeof(BlockDriverAIOCBCoroutine), 269768485420SKevin Wolf .cancel = bdrv_aio_co_cancel_em, 269868485420SKevin Wolf }; 269968485420SKevin Wolf 270068485420SKevin Wolf static void bdrv_co_rw_bh(void *opaque) 270168485420SKevin Wolf { 270268485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 270368485420SKevin Wolf 270468485420SKevin Wolf acb->common.cb(acb->common.opaque, acb->req.error); 270568485420SKevin Wolf qemu_bh_delete(acb->bh); 270668485420SKevin Wolf qemu_aio_release(acb); 270768485420SKevin Wolf } 270868485420SKevin Wolf 270968485420SKevin Wolf static void coroutine_fn bdrv_co_rw(void *opaque) 271068485420SKevin Wolf { 271168485420SKevin Wolf BlockDriverAIOCBCoroutine *acb = opaque; 271268485420SKevin Wolf BlockDriverState *bs = acb->common.bs; 271368485420SKevin Wolf 271468485420SKevin Wolf if (!acb->is_write) { 271568485420SKevin Wolf acb->req.error = bs->drv->bdrv_co_readv(bs, acb->req.sector, 271668485420SKevin Wolf acb->req.nb_sectors, acb->req.qiov); 271768485420SKevin Wolf } else { 271868485420SKevin Wolf acb->req.error = bs->drv->bdrv_co_writev(bs, acb->req.sector, 271968485420SKevin Wolf acb->req.nb_sectors, acb->req.qiov); 272068485420SKevin Wolf } 272168485420SKevin Wolf 272268485420SKevin Wolf acb->bh = qemu_bh_new(bdrv_co_rw_bh, acb); 272368485420SKevin Wolf qemu_bh_schedule(acb->bh); 272468485420SKevin Wolf } 272568485420SKevin Wolf 272668485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs, 272768485420SKevin Wolf int64_t sector_num, 272868485420SKevin Wolf QEMUIOVector *qiov, 272968485420SKevin Wolf int nb_sectors, 273068485420SKevin Wolf BlockDriverCompletionFunc *cb, 273168485420SKevin Wolf void *opaque, 273268485420SKevin Wolf bool is_write) 273368485420SKevin Wolf { 273468485420SKevin Wolf Coroutine *co; 273568485420SKevin Wolf BlockDriverAIOCBCoroutine *acb; 273668485420SKevin Wolf 273768485420SKevin Wolf acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque); 273868485420SKevin Wolf acb->req.sector = sector_num; 273968485420SKevin Wolf acb->req.nb_sectors = nb_sectors; 274068485420SKevin Wolf acb->req.qiov = qiov; 274168485420SKevin Wolf acb->is_write = is_write; 274268485420SKevin Wolf 274368485420SKevin Wolf co = qemu_coroutine_create(bdrv_co_rw); 274468485420SKevin Wolf qemu_coroutine_enter(co, acb); 274568485420SKevin Wolf 274668485420SKevin Wolf return &acb->common; 274768485420SKevin Wolf } 274868485420SKevin Wolf 274968485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_readv_em(BlockDriverState *bs, 275068485420SKevin Wolf int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 275168485420SKevin Wolf BlockDriverCompletionFunc *cb, void *opaque) 275268485420SKevin Wolf { 275368485420SKevin Wolf return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 275468485420SKevin Wolf false); 275568485420SKevin Wolf } 275668485420SKevin Wolf 275768485420SKevin Wolf static BlockDriverAIOCB *bdrv_co_aio_writev_em(BlockDriverState *bs, 275868485420SKevin Wolf int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 275968485420SKevin Wolf BlockDriverCompletionFunc *cb, void *opaque) 276068485420SKevin Wolf { 276168485420SKevin Wolf return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 276268485420SKevin Wolf true); 276368485420SKevin Wolf } 276468485420SKevin Wolf 2765b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 2766b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2767b2e12bc6SChristoph Hellwig { 2768b2e12bc6SChristoph Hellwig BlockDriverAIOCBSync *acb; 2769b2e12bc6SChristoph Hellwig 2770b2e12bc6SChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2771b2e12bc6SChristoph Hellwig acb->is_write = 1; /* don't bounce in the completion hadler */ 2772b2e12bc6SChristoph Hellwig acb->qiov = NULL; 2773b2e12bc6SChristoph Hellwig acb->bounce = NULL; 2774b2e12bc6SChristoph Hellwig acb->ret = 0; 2775b2e12bc6SChristoph Hellwig 2776b2e12bc6SChristoph Hellwig if (!acb->bh) 2777b2e12bc6SChristoph Hellwig acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2778b2e12bc6SChristoph Hellwig 2779b2e12bc6SChristoph Hellwig bdrv_flush(bs); 2780b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 2781b2e12bc6SChristoph Hellwig return &acb->common; 2782b2e12bc6SChristoph Hellwig } 2783b2e12bc6SChristoph Hellwig 2784016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 2785016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 2786016f5cf6SAlexander Graf { 2787016f5cf6SAlexander Graf BlockDriverAIOCBSync *acb; 2788016f5cf6SAlexander Graf 2789016f5cf6SAlexander Graf acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2790016f5cf6SAlexander Graf acb->is_write = 1; /* don't bounce in the completion handler */ 2791016f5cf6SAlexander Graf acb->qiov = NULL; 2792016f5cf6SAlexander Graf acb->bounce = NULL; 2793016f5cf6SAlexander Graf acb->ret = 0; 2794016f5cf6SAlexander Graf 2795016f5cf6SAlexander Graf if (!acb->bh) { 2796016f5cf6SAlexander Graf acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2797016f5cf6SAlexander Graf } 2798016f5cf6SAlexander Graf 2799016f5cf6SAlexander Graf qemu_bh_schedule(acb->bh); 2800016f5cf6SAlexander Graf return &acb->common; 2801016f5cf6SAlexander Graf } 2802016f5cf6SAlexander Graf 280383f64091Sbellard /**************************************************************/ 280483f64091Sbellard /* sync block device emulation */ 280583f64091Sbellard 280683f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 280783f64091Sbellard { 280883f64091Sbellard *(int *)opaque = ret; 280983f64091Sbellard } 281083f64091Sbellard 281183f64091Sbellard #define NOT_DONE 0x7fffffff 281283f64091Sbellard 281383f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 281483f64091Sbellard uint8_t *buf, int nb_sectors) 281583f64091Sbellard { 2816ce1a14dcSpbrook int async_ret; 2817ce1a14dcSpbrook BlockDriverAIOCB *acb; 2818f141eafeSaliguori struct iovec iov; 2819f141eafeSaliguori QEMUIOVector qiov; 282083f64091Sbellard 282183f64091Sbellard async_ret = NOT_DONE; 28223f4cb3d3Sblueswir1 iov.iov_base = (void *)buf; 2823eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2824f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2825f141eafeSaliguori acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, 282683f64091Sbellard bdrv_rw_em_cb, &async_ret); 282765d6b3d8SKevin Wolf if (acb == NULL) { 282865d6b3d8SKevin Wolf async_ret = -1; 282965d6b3d8SKevin Wolf goto fail; 283065d6b3d8SKevin Wolf } 2831baf35cb9Saliguori 283283f64091Sbellard while (async_ret == NOT_DONE) { 283383f64091Sbellard qemu_aio_wait(); 283483f64091Sbellard } 2835baf35cb9Saliguori 283665d6b3d8SKevin Wolf 283765d6b3d8SKevin Wolf fail: 283883f64091Sbellard return async_ret; 283983f64091Sbellard } 284083f64091Sbellard 284183f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 284283f64091Sbellard const uint8_t *buf, int nb_sectors) 284383f64091Sbellard { 2844ce1a14dcSpbrook int async_ret; 2845ce1a14dcSpbrook BlockDriverAIOCB *acb; 2846f141eafeSaliguori struct iovec iov; 2847f141eafeSaliguori QEMUIOVector qiov; 284883f64091Sbellard 284983f64091Sbellard async_ret = NOT_DONE; 2850f141eafeSaliguori iov.iov_base = (void *)buf; 2851eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2852f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2853f141eafeSaliguori acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, 285483f64091Sbellard bdrv_rw_em_cb, &async_ret); 285565d6b3d8SKevin Wolf if (acb == NULL) { 285665d6b3d8SKevin Wolf async_ret = -1; 285765d6b3d8SKevin Wolf goto fail; 285865d6b3d8SKevin Wolf } 285983f64091Sbellard while (async_ret == NOT_DONE) { 286083f64091Sbellard qemu_aio_wait(); 286183f64091Sbellard } 286265d6b3d8SKevin Wolf 286365d6b3d8SKevin Wolf fail: 286483f64091Sbellard return async_ret; 286583f64091Sbellard } 2866ea2384d3Sbellard 2867ea2384d3Sbellard void bdrv_init(void) 2868ea2384d3Sbellard { 28695efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 2870ea2384d3Sbellard } 2871ce1a14dcSpbrook 2872eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 2873eb852011SMarkus Armbruster { 2874eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 2875eb852011SMarkus Armbruster bdrv_init(); 2876eb852011SMarkus Armbruster } 2877eb852011SMarkus Armbruster 2878c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 28796bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 28806bbff9a0Saliguori { 2881ce1a14dcSpbrook BlockDriverAIOCB *acb; 2882ce1a14dcSpbrook 28836bbff9a0Saliguori if (pool->free_aiocb) { 28846bbff9a0Saliguori acb = pool->free_aiocb; 28856bbff9a0Saliguori pool->free_aiocb = acb->next; 2886ce1a14dcSpbrook } else { 28876bbff9a0Saliguori acb = qemu_mallocz(pool->aiocb_size); 28886bbff9a0Saliguori acb->pool = pool; 2889ce1a14dcSpbrook } 2890ce1a14dcSpbrook acb->bs = bs; 2891ce1a14dcSpbrook acb->cb = cb; 2892ce1a14dcSpbrook acb->opaque = opaque; 2893ce1a14dcSpbrook return acb; 2894ce1a14dcSpbrook } 2895ce1a14dcSpbrook 2896ce1a14dcSpbrook void qemu_aio_release(void *p) 2897ce1a14dcSpbrook { 28986bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 28996bbff9a0Saliguori AIOPool *pool = acb->pool; 29006bbff9a0Saliguori acb->next = pool->free_aiocb; 29016bbff9a0Saliguori pool->free_aiocb = acb; 2902ce1a14dcSpbrook } 290319cb3738Sbellard 290419cb3738Sbellard /**************************************************************/ 2905f9f05dc5SKevin Wolf /* Coroutine block device emulation */ 2906f9f05dc5SKevin Wolf 2907f9f05dc5SKevin Wolf typedef struct CoroutineIOCompletion { 2908f9f05dc5SKevin Wolf Coroutine *coroutine; 2909f9f05dc5SKevin Wolf int ret; 2910f9f05dc5SKevin Wolf } CoroutineIOCompletion; 2911f9f05dc5SKevin Wolf 2912f9f05dc5SKevin Wolf static void bdrv_co_io_em_complete(void *opaque, int ret) 2913f9f05dc5SKevin Wolf { 2914f9f05dc5SKevin Wolf CoroutineIOCompletion *co = opaque; 2915f9f05dc5SKevin Wolf 2916f9f05dc5SKevin Wolf co->ret = ret; 2917f9f05dc5SKevin Wolf qemu_coroutine_enter(co->coroutine, NULL); 2918f9f05dc5SKevin Wolf } 2919f9f05dc5SKevin Wolf 2920f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num, 2921f9f05dc5SKevin Wolf int nb_sectors, QEMUIOVector *iov, 2922f9f05dc5SKevin Wolf bool is_write) 2923f9f05dc5SKevin Wolf { 2924f9f05dc5SKevin Wolf CoroutineIOCompletion co = { 2925f9f05dc5SKevin Wolf .coroutine = qemu_coroutine_self(), 2926f9f05dc5SKevin Wolf }; 2927f9f05dc5SKevin Wolf BlockDriverAIOCB *acb; 2928f9f05dc5SKevin Wolf 2929f9f05dc5SKevin Wolf if (is_write) { 2930f9f05dc5SKevin Wolf acb = bdrv_aio_writev(bs, sector_num, iov, nb_sectors, 2931f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 2932f9f05dc5SKevin Wolf } else { 2933f9f05dc5SKevin Wolf acb = bdrv_aio_readv(bs, sector_num, iov, nb_sectors, 2934f9f05dc5SKevin Wolf bdrv_co_io_em_complete, &co); 2935f9f05dc5SKevin Wolf } 2936f9f05dc5SKevin Wolf 2937f9f05dc5SKevin Wolf trace_bdrv_co_io(is_write, acb); 2938f9f05dc5SKevin Wolf if (!acb) { 2939f9f05dc5SKevin Wolf return -EIO; 2940f9f05dc5SKevin Wolf } 2941f9f05dc5SKevin Wolf qemu_coroutine_yield(); 2942f9f05dc5SKevin Wolf 2943f9f05dc5SKevin Wolf return co.ret; 2944f9f05dc5SKevin Wolf } 2945f9f05dc5SKevin Wolf 2946f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs, 2947f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 2948f9f05dc5SKevin Wolf QEMUIOVector *iov) 2949f9f05dc5SKevin Wolf { 2950f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false); 2951f9f05dc5SKevin Wolf } 2952f9f05dc5SKevin Wolf 2953f9f05dc5SKevin Wolf static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs, 2954f9f05dc5SKevin Wolf int64_t sector_num, int nb_sectors, 2955f9f05dc5SKevin Wolf QEMUIOVector *iov) 2956f9f05dc5SKevin Wolf { 2957f9f05dc5SKevin Wolf return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true); 2958f9f05dc5SKevin Wolf } 2959f9f05dc5SKevin Wolf 2960*e7a8a783SKevin Wolf static int coroutine_fn bdrv_co_flush_em(BlockDriverState *bs) 2961*e7a8a783SKevin Wolf { 2962*e7a8a783SKevin Wolf CoroutineIOCompletion co = { 2963*e7a8a783SKevin Wolf .coroutine = qemu_coroutine_self(), 2964*e7a8a783SKevin Wolf }; 2965*e7a8a783SKevin Wolf BlockDriverAIOCB *acb; 2966*e7a8a783SKevin Wolf 2967*e7a8a783SKevin Wolf acb = bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co); 2968*e7a8a783SKevin Wolf if (!acb) { 2969*e7a8a783SKevin Wolf return -EIO; 2970*e7a8a783SKevin Wolf } 2971*e7a8a783SKevin Wolf qemu_coroutine_yield(); 2972*e7a8a783SKevin Wolf return co.ret; 2973*e7a8a783SKevin Wolf } 2974*e7a8a783SKevin Wolf 2975f9f05dc5SKevin Wolf /**************************************************************/ 297619cb3738Sbellard /* removable device support */ 297719cb3738Sbellard 297819cb3738Sbellard /** 297919cb3738Sbellard * Return TRUE if the media is present 298019cb3738Sbellard */ 298119cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 298219cb3738Sbellard { 298319cb3738Sbellard BlockDriver *drv = bs->drv; 298419cb3738Sbellard int ret; 298519cb3738Sbellard if (!drv) 298619cb3738Sbellard return 0; 298719cb3738Sbellard if (!drv->bdrv_is_inserted) 29884be9762aSMarkus Armbruster return !bs->tray_open; 298919cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 299019cb3738Sbellard return ret; 299119cb3738Sbellard } 299219cb3738Sbellard 299319cb3738Sbellard /** 299419cb3738Sbellard * Return TRUE if the media changed since the last call to this 299519cb3738Sbellard * function. It is currently only used for floppy disks 299619cb3738Sbellard */ 299719cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 299819cb3738Sbellard { 299919cb3738Sbellard BlockDriver *drv = bs->drv; 300019cb3738Sbellard int ret; 300119cb3738Sbellard 300219cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 300319cb3738Sbellard ret = -ENOTSUP; 300419cb3738Sbellard else 300519cb3738Sbellard ret = drv->bdrv_media_changed(bs); 300619cb3738Sbellard if (ret == -ENOTSUP) 300719cb3738Sbellard ret = bs->media_changed; 300819cb3738Sbellard bs->media_changed = 0; 300919cb3738Sbellard return ret; 301019cb3738Sbellard } 301119cb3738Sbellard 301219cb3738Sbellard /** 301319cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 301419cb3738Sbellard */ 3015aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag) 301619cb3738Sbellard { 301719cb3738Sbellard BlockDriver *drv = bs->drv; 301819cb3738Sbellard 301949aa46bbSMarkus Armbruster if (eject_flag && bs->locked) { 3020aea2a33cSMark McLoughlin return -EBUSY; 3021aea2a33cSMark McLoughlin } 3022aea2a33cSMark McLoughlin 3023822e1cd1SMarkus Armbruster if (drv && drv->bdrv_eject) { 3024822e1cd1SMarkus Armbruster drv->bdrv_eject(bs, eject_flag); 302519cb3738Sbellard } 30264be9762aSMarkus Armbruster bs->tray_open = eject_flag; 3027822e1cd1SMarkus Armbruster return 0; 302819cb3738Sbellard } 302919cb3738Sbellard 303019cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 303119cb3738Sbellard { 303219cb3738Sbellard return bs->locked; 303319cb3738Sbellard } 303419cb3738Sbellard 303519cb3738Sbellard /** 303619cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 303719cb3738Sbellard * to eject it manually). 303819cb3738Sbellard */ 303919cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 304019cb3738Sbellard { 304119cb3738Sbellard BlockDriver *drv = bs->drv; 304219cb3738Sbellard 3043b8c6d095SStefan Hajnoczi trace_bdrv_set_locked(bs, locked); 3044b8c6d095SStefan Hajnoczi 304519cb3738Sbellard bs->locked = locked; 304619cb3738Sbellard if (drv && drv->bdrv_set_locked) { 304719cb3738Sbellard drv->bdrv_set_locked(bs, locked); 304819cb3738Sbellard } 304919cb3738Sbellard } 3050985a03b0Sths 3051985a03b0Sths /* needed for generic scsi interface */ 3052985a03b0Sths 3053985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 3054985a03b0Sths { 3055985a03b0Sths BlockDriver *drv = bs->drv; 3056985a03b0Sths 3057985a03b0Sths if (drv && drv->bdrv_ioctl) 3058985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 3059985a03b0Sths return -ENOTSUP; 3060985a03b0Sths } 30617d780669Saliguori 3062221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 3063221f715dSaliguori unsigned long int req, void *buf, 30647d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 30657d780669Saliguori { 3066221f715dSaliguori BlockDriver *drv = bs->drv; 30677d780669Saliguori 3068221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 3069221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 3070221f715dSaliguori return NULL; 30717d780669Saliguori } 3072e268ca52Saliguori 30737cd1e32aSlirans@il.ibm.com 30747cd1e32aSlirans@il.ibm.com 3075e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 3076e268ca52Saliguori { 3077e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 3078e268ca52Saliguori } 30797cd1e32aSlirans@il.ibm.com 30807cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 30817cd1e32aSlirans@il.ibm.com { 30827cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 3083a55eb92cSJan Kiszka 3084aaa0eb75SLiran Schour bs->dirty_count = 0; 30857cd1e32aSlirans@il.ibm.com if (enable) { 3086c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 3087c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 3088c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 3089c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 30907cd1e32aSlirans@il.ibm.com 30917cd1e32aSlirans@il.ibm.com bs->dirty_bitmap = qemu_mallocz(bitmap_size); 30927cd1e32aSlirans@il.ibm.com } 30937cd1e32aSlirans@il.ibm.com } else { 3094c6d22830SJan Kiszka if (bs->dirty_bitmap) { 30957cd1e32aSlirans@il.ibm.com qemu_free(bs->dirty_bitmap); 3096c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 30977cd1e32aSlirans@il.ibm.com } 30987cd1e32aSlirans@il.ibm.com } 30997cd1e32aSlirans@il.ibm.com } 31007cd1e32aSlirans@il.ibm.com 31017cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 31027cd1e32aSlirans@il.ibm.com { 31036ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 31047cd1e32aSlirans@il.ibm.com 3105c6d22830SJan Kiszka if (bs->dirty_bitmap && 3106c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 31076d59fec1SMarcelo Tosatti return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 31086d59fec1SMarcelo Tosatti (1UL << (chunk % (sizeof(unsigned long) * 8)))); 31097cd1e32aSlirans@il.ibm.com } else { 31107cd1e32aSlirans@il.ibm.com return 0; 31117cd1e32aSlirans@il.ibm.com } 31127cd1e32aSlirans@il.ibm.com } 31137cd1e32aSlirans@il.ibm.com 31147cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 31157cd1e32aSlirans@il.ibm.com int nr_sectors) 31167cd1e32aSlirans@il.ibm.com { 31177cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 31187cd1e32aSlirans@il.ibm.com } 3119aaa0eb75SLiran Schour 3120aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 3121aaa0eb75SLiran Schour { 3122aaa0eb75SLiran Schour return bs->dirty_count; 3123aaa0eb75SLiran Schour } 3124f88e1a42SJes Sorensen 3125db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 3126db593f25SMarcelo Tosatti { 3127db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 3128db593f25SMarcelo Tosatti bs->in_use = in_use; 3129db593f25SMarcelo Tosatti } 3130db593f25SMarcelo Tosatti 3131db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 3132db593f25SMarcelo Tosatti { 3133db593f25SMarcelo Tosatti return bs->in_use; 3134db593f25SMarcelo Tosatti } 3135db593f25SMarcelo Tosatti 3136f88e1a42SJes Sorensen int bdrv_img_create(const char *filename, const char *fmt, 3137f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 3138f88e1a42SJes Sorensen char *options, uint64_t img_size, int flags) 3139f88e1a42SJes Sorensen { 3140f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 3141d220894eSKevin Wolf QEMUOptionParameter *backing_fmt, *backing_file, *size; 3142f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 3143f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 314496df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 3145f88e1a42SJes Sorensen int ret = 0; 3146f88e1a42SJes Sorensen 3147f88e1a42SJes Sorensen /* Find driver and parse its options */ 3148f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 3149f88e1a42SJes Sorensen if (!drv) { 3150f88e1a42SJes Sorensen error_report("Unknown file format '%s'", fmt); 31514f70f249SJes Sorensen ret = -EINVAL; 3152f88e1a42SJes Sorensen goto out; 3153f88e1a42SJes Sorensen } 3154f88e1a42SJes Sorensen 3155f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 3156f88e1a42SJes Sorensen if (!proto_drv) { 3157f88e1a42SJes Sorensen error_report("Unknown protocol '%s'", filename); 31584f70f249SJes Sorensen ret = -EINVAL; 3159f88e1a42SJes Sorensen goto out; 3160f88e1a42SJes Sorensen } 3161f88e1a42SJes Sorensen 3162f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 3163f88e1a42SJes Sorensen drv->create_options); 3164f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 3165f88e1a42SJes Sorensen proto_drv->create_options); 3166f88e1a42SJes Sorensen 3167f88e1a42SJes Sorensen /* Create parameter list with default values */ 3168f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 3169f88e1a42SJes Sorensen 3170f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 3171f88e1a42SJes Sorensen 3172f88e1a42SJes Sorensen /* Parse -o options */ 3173f88e1a42SJes Sorensen if (options) { 3174f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 3175f88e1a42SJes Sorensen if (param == NULL) { 3176f88e1a42SJes Sorensen error_report("Invalid options for file format '%s'.", fmt); 31774f70f249SJes Sorensen ret = -EINVAL; 3178f88e1a42SJes Sorensen goto out; 3179f88e1a42SJes Sorensen } 3180f88e1a42SJes Sorensen } 3181f88e1a42SJes Sorensen 3182f88e1a42SJes Sorensen if (base_filename) { 3183f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 3184f88e1a42SJes Sorensen base_filename)) { 3185f88e1a42SJes Sorensen error_report("Backing file not supported for file format '%s'", 3186f88e1a42SJes Sorensen fmt); 31874f70f249SJes Sorensen ret = -EINVAL; 3188f88e1a42SJes Sorensen goto out; 3189f88e1a42SJes Sorensen } 3190f88e1a42SJes Sorensen } 3191f88e1a42SJes Sorensen 3192f88e1a42SJes Sorensen if (base_fmt) { 3193f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 3194f88e1a42SJes Sorensen error_report("Backing file format not supported for file " 3195f88e1a42SJes Sorensen "format '%s'", fmt); 31964f70f249SJes Sorensen ret = -EINVAL; 3197f88e1a42SJes Sorensen goto out; 3198f88e1a42SJes Sorensen } 3199f88e1a42SJes Sorensen } 3200f88e1a42SJes Sorensen 3201792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 3202792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 3203792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 3204792da93aSJes Sorensen error_report("Error: Trying to create an image with the " 3205792da93aSJes Sorensen "same filename as the backing file"); 32064f70f249SJes Sorensen ret = -EINVAL; 3207792da93aSJes Sorensen goto out; 3208792da93aSJes Sorensen } 3209792da93aSJes Sorensen } 3210792da93aSJes Sorensen 3211f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 3212f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 321396df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 321496df67d1SStefan Hajnoczi if (!backing_drv) { 3215f88e1a42SJes Sorensen error_report("Unknown backing file format '%s'", 3216f88e1a42SJes Sorensen backing_fmt->value.s); 32174f70f249SJes Sorensen ret = -EINVAL; 3218f88e1a42SJes Sorensen goto out; 3219f88e1a42SJes Sorensen } 3220f88e1a42SJes Sorensen } 3221f88e1a42SJes Sorensen 3222f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 3223f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 3224d220894eSKevin Wolf size = get_option_parameter(param, BLOCK_OPT_SIZE); 3225d220894eSKevin Wolf if (size && size->value.n == -1) { 3226f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 3227f88e1a42SJes Sorensen uint64_t size; 3228f88e1a42SJes Sorensen char buf[32]; 3229f88e1a42SJes Sorensen 3230f88e1a42SJes Sorensen bs = bdrv_new(""); 3231f88e1a42SJes Sorensen 323296df67d1SStefan Hajnoczi ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); 3233f88e1a42SJes Sorensen if (ret < 0) { 323496df67d1SStefan Hajnoczi error_report("Could not open '%s'", backing_file->value.s); 3235f88e1a42SJes Sorensen goto out; 3236f88e1a42SJes Sorensen } 3237f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 3238f88e1a42SJes Sorensen size *= 512; 3239f88e1a42SJes Sorensen 3240f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 3241f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 3242f88e1a42SJes Sorensen } else { 3243f88e1a42SJes Sorensen error_report("Image creation needs a size parameter"); 32444f70f249SJes Sorensen ret = -EINVAL; 3245f88e1a42SJes Sorensen goto out; 3246f88e1a42SJes Sorensen } 3247f88e1a42SJes Sorensen } 3248f88e1a42SJes Sorensen 3249f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 3250f88e1a42SJes Sorensen print_option_parameters(param); 3251f88e1a42SJes Sorensen puts(""); 3252f88e1a42SJes Sorensen 3253f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 3254f88e1a42SJes Sorensen 3255f88e1a42SJes Sorensen if (ret < 0) { 3256f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 3257f88e1a42SJes Sorensen error_report("Formatting or formatting option not supported for " 3258f88e1a42SJes Sorensen "file format '%s'", fmt); 3259f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 3260f88e1a42SJes Sorensen error_report("The image size is too large for file format '%s'", 3261f88e1a42SJes Sorensen fmt); 3262f88e1a42SJes Sorensen } else { 3263f88e1a42SJes Sorensen error_report("%s: error while creating %s: %s", filename, fmt, 3264f88e1a42SJes Sorensen strerror(-ret)); 3265f88e1a42SJes Sorensen } 3266f88e1a42SJes Sorensen } 3267f88e1a42SJes Sorensen 3268f88e1a42SJes Sorensen out: 3269f88e1a42SJes Sorensen free_option_parameters(create_options); 3270f88e1a42SJes Sorensen free_option_parameters(param); 3271f88e1a42SJes Sorensen 3272f88e1a42SJes Sorensen if (bs) { 3273f88e1a42SJes Sorensen bdrv_delete(bs); 3274f88e1a42SJes Sorensen } 32754f70f249SJes Sorensen 32764f70f249SJes Sorensen return ret; 3277f88e1a42SJes Sorensen } 3278