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" 31fc01f7e7Sbellard 3271e72a19SJuan Quintela #ifdef CONFIG_BSD 337674e7bfSbellard #include <sys/types.h> 347674e7bfSbellard #include <sys/stat.h> 357674e7bfSbellard #include <sys/ioctl.h> 3672cf2d4fSBlue Swirl #include <sys/queue.h> 37c5e97233Sblueswir1 #ifndef __DragonFly__ 387674e7bfSbellard #include <sys/disk.h> 397674e7bfSbellard #endif 40c5e97233Sblueswir1 #endif 417674e7bfSbellard 4249dc768dSaliguori #ifdef _WIN32 4349dc768dSaliguori #include <windows.h> 4449dc768dSaliguori #endif 4549dc768dSaliguori 46f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 47f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 48c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 49f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 50f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 51ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 52b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 53b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque); 54016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 55016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque); 5683f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 5783f64091Sbellard uint8_t *buf, int nb_sectors); 5883f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 5983f64091Sbellard const uint8_t *buf, int nb_sectors); 60ec530c81Sbellard 611b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 621b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 637ee930d0Sblueswir1 648a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 658a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 66ea2384d3Sbellard 67f9092b10SMarkus Armbruster /* The device to use for VM snapshots */ 68f9092b10SMarkus Armbruster static BlockDriverState *bs_snapshots; 69f9092b10SMarkus Armbruster 70eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 71eb852011SMarkus Armbruster static int use_bdrv_whitelist; 72eb852011SMarkus Armbruster 739e0b22f4SStefan Hajnoczi #ifdef _WIN32 749e0b22f4SStefan Hajnoczi static int is_windows_drive_prefix(const char *filename) 759e0b22f4SStefan Hajnoczi { 769e0b22f4SStefan Hajnoczi return (((filename[0] >= 'a' && filename[0] <= 'z') || 779e0b22f4SStefan Hajnoczi (filename[0] >= 'A' && filename[0] <= 'Z')) && 789e0b22f4SStefan Hajnoczi filename[1] == ':'); 799e0b22f4SStefan Hajnoczi } 809e0b22f4SStefan Hajnoczi 819e0b22f4SStefan Hajnoczi int is_windows_drive(const char *filename) 829e0b22f4SStefan Hajnoczi { 839e0b22f4SStefan Hajnoczi if (is_windows_drive_prefix(filename) && 849e0b22f4SStefan Hajnoczi filename[2] == '\0') 859e0b22f4SStefan Hajnoczi return 1; 869e0b22f4SStefan Hajnoczi if (strstart(filename, "\\\\.\\", NULL) || 879e0b22f4SStefan Hajnoczi strstart(filename, "//./", NULL)) 889e0b22f4SStefan Hajnoczi return 1; 899e0b22f4SStefan Hajnoczi return 0; 909e0b22f4SStefan Hajnoczi } 919e0b22f4SStefan Hajnoczi #endif 929e0b22f4SStefan Hajnoczi 939e0b22f4SStefan Hajnoczi /* check if the path starts with "<protocol>:" */ 949e0b22f4SStefan Hajnoczi static int path_has_protocol(const char *path) 959e0b22f4SStefan Hajnoczi { 969e0b22f4SStefan Hajnoczi #ifdef _WIN32 979e0b22f4SStefan Hajnoczi if (is_windows_drive(path) || 989e0b22f4SStefan Hajnoczi is_windows_drive_prefix(path)) { 999e0b22f4SStefan Hajnoczi return 0; 1009e0b22f4SStefan Hajnoczi } 1019e0b22f4SStefan Hajnoczi #endif 1029e0b22f4SStefan Hajnoczi 1039e0b22f4SStefan Hajnoczi return strchr(path, ':') != NULL; 1049e0b22f4SStefan Hajnoczi } 1059e0b22f4SStefan Hajnoczi 10683f64091Sbellard int path_is_absolute(const char *path) 10783f64091Sbellard { 10883f64091Sbellard const char *p; 10921664424Sbellard #ifdef _WIN32 11021664424Sbellard /* specific case for names like: "\\.\d:" */ 11121664424Sbellard if (*path == '/' || *path == '\\') 11221664424Sbellard return 1; 11321664424Sbellard #endif 11483f64091Sbellard p = strchr(path, ':'); 11583f64091Sbellard if (p) 11683f64091Sbellard p++; 11783f64091Sbellard else 11883f64091Sbellard p = path; 1193b9f94e1Sbellard #ifdef _WIN32 1203b9f94e1Sbellard return (*p == '/' || *p == '\\'); 1213b9f94e1Sbellard #else 1223b9f94e1Sbellard return (*p == '/'); 1233b9f94e1Sbellard #endif 12483f64091Sbellard } 12583f64091Sbellard 12683f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 12783f64091Sbellard path to it by considering it is relative to base_path. URL are 12883f64091Sbellard supported. */ 12983f64091Sbellard void path_combine(char *dest, int dest_size, 13083f64091Sbellard const char *base_path, 13183f64091Sbellard const char *filename) 13283f64091Sbellard { 13383f64091Sbellard const char *p, *p1; 13483f64091Sbellard int len; 13583f64091Sbellard 13683f64091Sbellard if (dest_size <= 0) 13783f64091Sbellard return; 13883f64091Sbellard if (path_is_absolute(filename)) { 13983f64091Sbellard pstrcpy(dest, dest_size, filename); 14083f64091Sbellard } else { 14183f64091Sbellard p = strchr(base_path, ':'); 14283f64091Sbellard if (p) 14383f64091Sbellard p++; 14483f64091Sbellard else 14583f64091Sbellard p = base_path; 1463b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1473b9f94e1Sbellard #ifdef _WIN32 1483b9f94e1Sbellard { 1493b9f94e1Sbellard const char *p2; 1503b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1513b9f94e1Sbellard if (!p1 || p2 > p1) 1523b9f94e1Sbellard p1 = p2; 1533b9f94e1Sbellard } 1543b9f94e1Sbellard #endif 15583f64091Sbellard if (p1) 15683f64091Sbellard p1++; 15783f64091Sbellard else 15883f64091Sbellard p1 = base_path; 15983f64091Sbellard if (p1 > p) 16083f64091Sbellard p = p1; 16183f64091Sbellard len = p - base_path; 16283f64091Sbellard if (len > dest_size - 1) 16383f64091Sbellard len = dest_size - 1; 16483f64091Sbellard memcpy(dest, base_path, len); 16583f64091Sbellard dest[len] = '\0'; 16683f64091Sbellard pstrcat(dest, dest_size, filename); 16783f64091Sbellard } 16883f64091Sbellard } 16983f64091Sbellard 1705efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 171ea2384d3Sbellard { 172f141eafeSaliguori if (!bdrv->bdrv_aio_readv) { 17383f64091Sbellard /* add AIO emulation layer */ 174f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 175f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 176eda578e5Saliguori } else if (!bdrv->bdrv_read) { 17783f64091Sbellard /* add synchronous IO emulation layer */ 17883f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 17983f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 18083f64091Sbellard } 181b2e12bc6SChristoph Hellwig 182b2e12bc6SChristoph Hellwig if (!bdrv->bdrv_aio_flush) 183b2e12bc6SChristoph Hellwig bdrv->bdrv_aio_flush = bdrv_aio_flush_em; 184b2e12bc6SChristoph Hellwig 1858a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 186ea2384d3Sbellard } 187b338082bSbellard 188b338082bSbellard /* create a new block device (by default it is empty) */ 189b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 190fc01f7e7Sbellard { 1911b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 192b338082bSbellard 193b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 194b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 195ea2384d3Sbellard if (device_name[0] != '\0') { 1961b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 197ea2384d3Sbellard } 198b338082bSbellard return bs; 199b338082bSbellard } 200b338082bSbellard 201ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 202ea2384d3Sbellard { 203ea2384d3Sbellard BlockDriver *drv1; 2048a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 2058a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 206ea2384d3Sbellard return drv1; 207ea2384d3Sbellard } 2088a22f02aSStefan Hajnoczi } 209ea2384d3Sbellard return NULL; 210ea2384d3Sbellard } 211ea2384d3Sbellard 212eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 213eb852011SMarkus Armbruster { 214eb852011SMarkus Armbruster static const char *whitelist[] = { 215eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 216eb852011SMarkus Armbruster }; 217eb852011SMarkus Armbruster const char **p; 218eb852011SMarkus Armbruster 219eb852011SMarkus Armbruster if (!whitelist[0]) 220eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 221eb852011SMarkus Armbruster 222eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 223eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 224eb852011SMarkus Armbruster return 1; 225eb852011SMarkus Armbruster } 226eb852011SMarkus Armbruster } 227eb852011SMarkus Armbruster return 0; 228eb852011SMarkus Armbruster } 229eb852011SMarkus Armbruster 230eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 231eb852011SMarkus Armbruster { 232eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 233eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 234eb852011SMarkus Armbruster } 235eb852011SMarkus Armbruster 2360e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 2370e7e1989SKevin Wolf QEMUOptionParameter *options) 238ea2384d3Sbellard { 239ea2384d3Sbellard if (!drv->bdrv_create) 240ea2384d3Sbellard return -ENOTSUP; 2410e7e1989SKevin Wolf 2420e7e1989SKevin Wolf return drv->bdrv_create(filename, options); 243ea2384d3Sbellard } 244ea2384d3Sbellard 24584a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 24684a12e66SChristoph Hellwig { 24784a12e66SChristoph Hellwig BlockDriver *drv; 24884a12e66SChristoph Hellwig 249b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 25084a12e66SChristoph Hellwig if (drv == NULL) { 25116905d71SStefan Hajnoczi return -ENOENT; 25284a12e66SChristoph Hellwig } 25384a12e66SChristoph Hellwig 25484a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 25584a12e66SChristoph Hellwig } 25684a12e66SChristoph Hellwig 257d5249393Sbellard #ifdef _WIN32 25895389c86Sbellard void get_tmp_filename(char *filename, int size) 259d5249393Sbellard { 2603b9f94e1Sbellard char temp_dir[MAX_PATH]; 2613b9f94e1Sbellard 2623b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 2633b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 264d5249393Sbellard } 265d5249393Sbellard #else 26695389c86Sbellard void get_tmp_filename(char *filename, int size) 267ea2384d3Sbellard { 268ea2384d3Sbellard int fd; 2697ccfb2ebSblueswir1 const char *tmpdir; 270d5249393Sbellard /* XXX: race condition possible */ 2710badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 2720badc1eeSaurel32 if (!tmpdir) 2730badc1eeSaurel32 tmpdir = "/tmp"; 2740badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 275ea2384d3Sbellard fd = mkstemp(filename); 276ea2384d3Sbellard close(fd); 277ea2384d3Sbellard } 278d5249393Sbellard #endif 279ea2384d3Sbellard 280f3a5d3f8SChristoph Hellwig /* 281f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 282f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 283f3a5d3f8SChristoph Hellwig */ 284f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 285f3a5d3f8SChristoph Hellwig { 286508c7cb3SChristoph Hellwig int score_max = 0, score; 287508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 288f3a5d3f8SChristoph Hellwig 2898a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 290508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 291508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 292508c7cb3SChristoph Hellwig if (score > score_max) { 293508c7cb3SChristoph Hellwig score_max = score; 294508c7cb3SChristoph Hellwig drv = d; 295f3a5d3f8SChristoph Hellwig } 296508c7cb3SChristoph Hellwig } 297f3a5d3f8SChristoph Hellwig } 298f3a5d3f8SChristoph Hellwig 299508c7cb3SChristoph Hellwig return drv; 300f3a5d3f8SChristoph Hellwig } 301f3a5d3f8SChristoph Hellwig 302b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 30384a12e66SChristoph Hellwig { 30484a12e66SChristoph Hellwig BlockDriver *drv1; 30584a12e66SChristoph Hellwig char protocol[128]; 30684a12e66SChristoph Hellwig int len; 30784a12e66SChristoph Hellwig const char *p; 30884a12e66SChristoph Hellwig 30966f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 31066f82ceeSKevin Wolf 31139508e7aSChristoph Hellwig /* 31239508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 31339508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 31439508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 31539508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 31639508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 31739508e7aSChristoph Hellwig */ 31884a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 31939508e7aSChristoph Hellwig if (drv1) { 32084a12e66SChristoph Hellwig return drv1; 32184a12e66SChristoph Hellwig } 32239508e7aSChristoph Hellwig 3239e0b22f4SStefan Hajnoczi if (!path_has_protocol(filename)) { 32439508e7aSChristoph Hellwig return bdrv_find_format("file"); 32539508e7aSChristoph Hellwig } 3269e0b22f4SStefan Hajnoczi p = strchr(filename, ':'); 3279e0b22f4SStefan Hajnoczi assert(p != NULL); 32884a12e66SChristoph Hellwig len = p - filename; 32984a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 33084a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 33184a12e66SChristoph Hellwig memcpy(protocol, filename, len); 33284a12e66SChristoph Hellwig protocol[len] = '\0'; 33384a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 33484a12e66SChristoph Hellwig if (drv1->protocol_name && 33584a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 33684a12e66SChristoph Hellwig return drv1; 33784a12e66SChristoph Hellwig } 33884a12e66SChristoph Hellwig } 33984a12e66SChristoph Hellwig return NULL; 34084a12e66SChristoph Hellwig } 34184a12e66SChristoph Hellwig 342c98ac35dSStefan Weil static int find_image_format(const char *filename, BlockDriver **pdrv) 343ea2384d3Sbellard { 34483f64091Sbellard int ret, score, score_max; 345ea2384d3Sbellard BlockDriver *drv1, *drv; 34683f64091Sbellard uint8_t buf[2048]; 34783f64091Sbellard BlockDriverState *bs; 348ea2384d3Sbellard 349f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 350c98ac35dSStefan Weil if (ret < 0) { 351c98ac35dSStefan Weil *pdrv = NULL; 352c98ac35dSStefan Weil return ret; 353c98ac35dSStefan Weil } 354f8ea0b00SNicholas Bellinger 35508a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 35608a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 3571a396859SNicholas A. Bellinger bdrv_delete(bs); 358c98ac35dSStefan Weil drv = bdrv_find_format("raw"); 359c98ac35dSStefan Weil if (!drv) { 360c98ac35dSStefan Weil ret = -ENOENT; 361c98ac35dSStefan Weil } 362c98ac35dSStefan Weil *pdrv = drv; 363c98ac35dSStefan Weil return ret; 3641a396859SNicholas A. Bellinger } 365f8ea0b00SNicholas Bellinger 36683f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 36783f64091Sbellard bdrv_delete(bs); 368ea2384d3Sbellard if (ret < 0) { 369c98ac35dSStefan Weil *pdrv = NULL; 370c98ac35dSStefan Weil return ret; 371ea2384d3Sbellard } 372ea2384d3Sbellard 373ea2384d3Sbellard score_max = 0; 37484a12e66SChristoph Hellwig drv = NULL; 3758a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 37683f64091Sbellard if (drv1->bdrv_probe) { 377ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 378ea2384d3Sbellard if (score > score_max) { 379ea2384d3Sbellard score_max = score; 380ea2384d3Sbellard drv = drv1; 381ea2384d3Sbellard } 382ea2384d3Sbellard } 38383f64091Sbellard } 384c98ac35dSStefan Weil if (!drv) { 385c98ac35dSStefan Weil ret = -ENOENT; 386c98ac35dSStefan Weil } 387c98ac35dSStefan Weil *pdrv = drv; 388c98ac35dSStefan Weil return ret; 389ea2384d3Sbellard } 390ea2384d3Sbellard 39151762288SStefan Hajnoczi /** 39251762288SStefan Hajnoczi * Set the current 'total_sectors' value 39351762288SStefan Hajnoczi */ 39451762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 39551762288SStefan Hajnoczi { 39651762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 39751762288SStefan Hajnoczi 398396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 399396759adSNicholas Bellinger if (bs->sg) 400396759adSNicholas Bellinger return 0; 401396759adSNicholas Bellinger 40251762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 40351762288SStefan Hajnoczi if (drv->bdrv_getlength) { 40451762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 40551762288SStefan Hajnoczi if (length < 0) { 40651762288SStefan Hajnoczi return length; 40751762288SStefan Hajnoczi } 40851762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 40951762288SStefan Hajnoczi } 41051762288SStefan Hajnoczi 41151762288SStefan Hajnoczi bs->total_sectors = hint; 41251762288SStefan Hajnoczi return 0; 41351762288SStefan Hajnoczi } 41451762288SStefan Hajnoczi 415b6ce07aaSKevin Wolf /* 41657915332SKevin Wolf * Common part for opening disk images and files 41757915332SKevin Wolf */ 41857915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 41957915332SKevin Wolf int flags, BlockDriver *drv) 42057915332SKevin Wolf { 42157915332SKevin Wolf int ret, open_flags; 42257915332SKevin Wolf 42357915332SKevin Wolf assert(drv != NULL); 42457915332SKevin Wolf 42566f82ceeSKevin Wolf bs->file = NULL; 42651762288SStefan Hajnoczi bs->total_sectors = 0; 42757915332SKevin Wolf bs->encrypted = 0; 42857915332SKevin Wolf bs->valid_key = 0; 42957915332SKevin Wolf bs->open_flags = flags; 43057915332SKevin Wolf /* buffer_alignment defaulted to 512, drivers can change this value */ 43157915332SKevin Wolf bs->buffer_alignment = 512; 43257915332SKevin Wolf 43357915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 43457915332SKevin Wolf 43557915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 43657915332SKevin Wolf return -ENOTSUP; 43757915332SKevin Wolf } 43857915332SKevin Wolf 43957915332SKevin Wolf bs->drv = drv; 44057915332SKevin Wolf bs->opaque = qemu_mallocz(drv->instance_size); 44157915332SKevin Wolf 44257915332SKevin Wolf /* 44357915332SKevin Wolf * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a 44457915332SKevin Wolf * write cache to the guest. We do need the fdatasync to flush 44557915332SKevin Wolf * out transactions for block allocations, and we maybe have a 44657915332SKevin Wolf * volatile write cache in our backing device to deal with. 44757915332SKevin Wolf */ 44857915332SKevin Wolf if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) 44957915332SKevin Wolf bs->enable_write_cache = 1; 45057915332SKevin Wolf 45157915332SKevin Wolf /* 45257915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 45357915332SKevin Wolf * image. 45457915332SKevin Wolf */ 45557915332SKevin Wolf open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 45657915332SKevin Wolf 45757915332SKevin Wolf /* 45857915332SKevin Wolf * Snapshots should be writeable. 45957915332SKevin Wolf */ 46057915332SKevin Wolf if (bs->is_temporary) { 46157915332SKevin Wolf open_flags |= BDRV_O_RDWR; 46257915332SKevin Wolf } 46357915332SKevin Wolf 46466f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 46566f82ceeSKevin Wolf if (drv->bdrv_file_open) { 46666f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 46766f82ceeSKevin Wolf } else { 46866f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 46966f82ceeSKevin Wolf if (ret >= 0) { 47066f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 47166f82ceeSKevin Wolf } 47266f82ceeSKevin Wolf } 47366f82ceeSKevin Wolf 47457915332SKevin Wolf if (ret < 0) { 47557915332SKevin Wolf goto free_and_fail; 47657915332SKevin Wolf } 47757915332SKevin Wolf 47857915332SKevin Wolf bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 47951762288SStefan Hajnoczi 48051762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 48151762288SStefan Hajnoczi if (ret < 0) { 48251762288SStefan Hajnoczi goto free_and_fail; 48357915332SKevin Wolf } 48451762288SStefan Hajnoczi 48557915332SKevin Wolf #ifndef _WIN32 48657915332SKevin Wolf if (bs->is_temporary) { 48757915332SKevin Wolf unlink(filename); 48857915332SKevin Wolf } 48957915332SKevin Wolf #endif 49057915332SKevin Wolf return 0; 49157915332SKevin Wolf 49257915332SKevin Wolf free_and_fail: 49366f82ceeSKevin Wolf if (bs->file) { 49466f82ceeSKevin Wolf bdrv_delete(bs->file); 49566f82ceeSKevin Wolf bs->file = NULL; 49666f82ceeSKevin Wolf } 49757915332SKevin Wolf qemu_free(bs->opaque); 49857915332SKevin Wolf bs->opaque = NULL; 49957915332SKevin Wolf bs->drv = NULL; 50057915332SKevin Wolf return ret; 50157915332SKevin Wolf } 50257915332SKevin Wolf 50357915332SKevin Wolf /* 504b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 505b6ce07aaSKevin Wolf */ 50683f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 507b338082bSbellard { 50883f64091Sbellard BlockDriverState *bs; 5096db95603SChristoph Hellwig BlockDriver *drv; 51083f64091Sbellard int ret; 5113b0d4f61Sbellard 512b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 5136db95603SChristoph Hellwig if (!drv) { 5146db95603SChristoph Hellwig return -ENOENT; 5156db95603SChristoph Hellwig } 5166db95603SChristoph Hellwig 51783f64091Sbellard bs = bdrv_new(""); 518b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 51983f64091Sbellard if (ret < 0) { 52083f64091Sbellard bdrv_delete(bs); 52183f64091Sbellard return ret; 5223b0d4f61Sbellard } 52371d0770cSaliguori bs->growable = 1; 52483f64091Sbellard *pbs = bs; 52583f64091Sbellard return 0; 5263b0d4f61Sbellard } 5273b0d4f61Sbellard 528b6ce07aaSKevin Wolf /* 529b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 530b6ce07aaSKevin Wolf */ 531d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 532ea2384d3Sbellard BlockDriver *drv) 533ea2384d3Sbellard { 534b6ce07aaSKevin Wolf int ret; 53533e3963eSbellard 53683f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 537ea2384d3Sbellard BlockDriverState *bs1; 538ea2384d3Sbellard int64_t total_size; 5397c96d46eSaliguori int is_protocol = 0; 54091a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 54191a073a9SKevin Wolf QEMUOptionParameter *options; 542b6ce07aaSKevin Wolf char tmp_filename[PATH_MAX]; 543b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 54433e3963eSbellard 545ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 546ea2384d3Sbellard instead of opening 'filename' directly */ 547ea2384d3Sbellard 548ea2384d3Sbellard /* if there is a backing file, use it */ 549ea2384d3Sbellard bs1 = bdrv_new(""); 550d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 55151d7c00cSaliguori if (ret < 0) { 552ea2384d3Sbellard bdrv_delete(bs1); 55351d7c00cSaliguori return ret; 554ea2384d3Sbellard } 5553e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 5567c96d46eSaliguori 5577c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 5587c96d46eSaliguori is_protocol = 1; 5597c96d46eSaliguori 560ea2384d3Sbellard bdrv_delete(bs1); 561ea2384d3Sbellard 562ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 5637c96d46eSaliguori 5647c96d46eSaliguori /* Real path is meaningless for protocols */ 5657c96d46eSaliguori if (is_protocol) 5667c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 5677c96d46eSaliguori "%s", filename); 568114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 569114cdfa9SKirill A. Shutemov return -errno; 5707c96d46eSaliguori 57191a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 57291a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 57391a073a9SKevin Wolf 5743e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 57591a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 57691a073a9SKevin Wolf if (drv) { 57791a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 57891a073a9SKevin Wolf drv->format_name); 57991a073a9SKevin Wolf } 58091a073a9SKevin Wolf 58191a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 582d748768cSJan Kiszka free_option_parameters(options); 58351d7c00cSaliguori if (ret < 0) { 58451d7c00cSaliguori return ret; 585ea2384d3Sbellard } 58691a073a9SKevin Wolf 587ea2384d3Sbellard filename = tmp_filename; 58891a073a9SKevin Wolf drv = bdrv_qcow2; 589ea2384d3Sbellard bs->is_temporary = 1; 590ea2384d3Sbellard } 591ea2384d3Sbellard 592b6ce07aaSKevin Wolf /* Find the right image format driver */ 5936db95603SChristoph Hellwig if (!drv) { 594c98ac35dSStefan Weil ret = find_image_format(filename, &drv); 595ea2384d3Sbellard } 5966987307cSChristoph Hellwig 59751d7c00cSaliguori if (!drv) { 59851d7c00cSaliguori goto unlink_and_fail; 59983f64091Sbellard } 600b6ce07aaSKevin Wolf 601b6ce07aaSKevin Wolf /* Open the image */ 602b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 603b6ce07aaSKevin Wolf if (ret < 0) { 6046987307cSChristoph Hellwig goto unlink_and_fail; 6056987307cSChristoph Hellwig } 6066987307cSChristoph Hellwig 607b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 608b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 609b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 610b6ce07aaSKevin Wolf int back_flags; 611b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 612b6ce07aaSKevin Wolf 613b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 614df2dbb4aSStefan Hajnoczi 615df2dbb4aSStefan Hajnoczi if (path_has_protocol(bs->backing_file)) { 616df2dbb4aSStefan Hajnoczi pstrcpy(backing_filename, sizeof(backing_filename), 617df2dbb4aSStefan Hajnoczi bs->backing_file); 618df2dbb4aSStefan Hajnoczi } else { 619b6ce07aaSKevin Wolf path_combine(backing_filename, sizeof(backing_filename), 620b6ce07aaSKevin Wolf filename, bs->backing_file); 621df2dbb4aSStefan Hajnoczi } 622df2dbb4aSStefan Hajnoczi 623df2dbb4aSStefan Hajnoczi if (bs->backing_format[0] != '\0') { 624b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 625df2dbb4aSStefan Hajnoczi } 626b6ce07aaSKevin Wolf 627b6ce07aaSKevin Wolf /* backing files always opened read-only */ 628b6ce07aaSKevin Wolf back_flags = 629b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 630b6ce07aaSKevin Wolf 631b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 632b6ce07aaSKevin Wolf if (ret < 0) { 633b6ce07aaSKevin Wolf bdrv_close(bs); 634b6ce07aaSKevin Wolf return ret; 635b6ce07aaSKevin Wolf } 636b6ce07aaSKevin Wolf if (bs->is_temporary) { 637b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 638b6ce07aaSKevin Wolf } else { 639b6ce07aaSKevin Wolf /* base image inherits from "parent" */ 640b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = bs->keep_read_only; 641b6ce07aaSKevin Wolf } 642b6ce07aaSKevin Wolf } 643b6ce07aaSKevin Wolf 644b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 645b6ce07aaSKevin Wolf /* call the change callback */ 646b6ce07aaSKevin Wolf bs->media_changed = 1; 647b6ce07aaSKevin Wolf if (bs->change_cb) 648db97ee6aSChristoph Hellwig bs->change_cb(bs->change_opaque, CHANGE_MEDIA); 649b6ce07aaSKevin Wolf } 650b6ce07aaSKevin Wolf 651b6ce07aaSKevin Wolf return 0; 652b6ce07aaSKevin Wolf 653b6ce07aaSKevin Wolf unlink_and_fail: 654b6ce07aaSKevin Wolf if (bs->is_temporary) { 655b6ce07aaSKevin Wolf unlink(filename); 656b6ce07aaSKevin Wolf } 657b6ce07aaSKevin Wolf return ret; 658b6ce07aaSKevin Wolf } 659b6ce07aaSKevin Wolf 660fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 661fc01f7e7Sbellard { 66219cb3738Sbellard if (bs->drv) { 663f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 664f9092b10SMarkus Armbruster bs_snapshots = NULL; 665f9092b10SMarkus Armbruster } 666557df6acSStefan Hajnoczi if (bs->backing_hd) { 667ea2384d3Sbellard bdrv_delete(bs->backing_hd); 668557df6acSStefan Hajnoczi bs->backing_hd = NULL; 669557df6acSStefan Hajnoczi } 670ea2384d3Sbellard bs->drv->bdrv_close(bs); 671ea2384d3Sbellard qemu_free(bs->opaque); 672ea2384d3Sbellard #ifdef _WIN32 673ea2384d3Sbellard if (bs->is_temporary) { 674ea2384d3Sbellard unlink(bs->filename); 675ea2384d3Sbellard } 67667b915a5Sbellard #endif 677ea2384d3Sbellard bs->opaque = NULL; 678ea2384d3Sbellard bs->drv = NULL; 679b338082bSbellard 68066f82ceeSKevin Wolf if (bs->file != NULL) { 68166f82ceeSKevin Wolf bdrv_close(bs->file); 68266f82ceeSKevin Wolf } 68366f82ceeSKevin Wolf 684b338082bSbellard /* call the change callback */ 68519cb3738Sbellard bs->media_changed = 1; 686b338082bSbellard if (bs->change_cb) 687db97ee6aSChristoph Hellwig bs->change_cb(bs->change_opaque, CHANGE_MEDIA); 688b338082bSbellard } 689b338082bSbellard } 690b338082bSbellard 6912bc93fedSMORITA Kazutaka void bdrv_close_all(void) 6922bc93fedSMORITA Kazutaka { 6932bc93fedSMORITA Kazutaka BlockDriverState *bs; 6942bc93fedSMORITA Kazutaka 6952bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 6962bc93fedSMORITA Kazutaka bdrv_close(bs); 6972bc93fedSMORITA Kazutaka } 6982bc93fedSMORITA Kazutaka } 6992bc93fedSMORITA Kazutaka 700d22b2f41SRyan Harper /* make a BlockDriverState anonymous by removing from bdrv_state list. 701d22b2f41SRyan Harper Also, NULL terminate the device_name to prevent double remove */ 702d22b2f41SRyan Harper void bdrv_make_anon(BlockDriverState *bs) 703d22b2f41SRyan Harper { 704d22b2f41SRyan Harper if (bs->device_name[0] != '\0') { 705d22b2f41SRyan Harper QTAILQ_REMOVE(&bdrv_states, bs, list); 706d22b2f41SRyan Harper } 707d22b2f41SRyan Harper bs->device_name[0] = '\0'; 708d22b2f41SRyan Harper } 709d22b2f41SRyan Harper 710b338082bSbellard void bdrv_delete(BlockDriverState *bs) 711b338082bSbellard { 71218846deeSMarkus Armbruster assert(!bs->peer); 71318846deeSMarkus Armbruster 7141b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 715d22b2f41SRyan Harper bdrv_make_anon(bs); 71634c6f050Saurel32 717b338082bSbellard bdrv_close(bs); 71866f82ceeSKevin Wolf if (bs->file != NULL) { 71966f82ceeSKevin Wolf bdrv_delete(bs->file); 72066f82ceeSKevin Wolf } 72166f82ceeSKevin Wolf 722f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 723b338082bSbellard qemu_free(bs); 724fc01f7e7Sbellard } 725fc01f7e7Sbellard 72618846deeSMarkus Armbruster int bdrv_attach(BlockDriverState *bs, DeviceState *qdev) 72718846deeSMarkus Armbruster { 72818846deeSMarkus Armbruster if (bs->peer) { 72918846deeSMarkus Armbruster return -EBUSY; 73018846deeSMarkus Armbruster } 73118846deeSMarkus Armbruster bs->peer = qdev; 73218846deeSMarkus Armbruster return 0; 73318846deeSMarkus Armbruster } 73418846deeSMarkus Armbruster 73518846deeSMarkus Armbruster void bdrv_detach(BlockDriverState *bs, DeviceState *qdev) 73618846deeSMarkus Armbruster { 73718846deeSMarkus Armbruster assert(bs->peer == qdev); 73818846deeSMarkus Armbruster bs->peer = NULL; 73918846deeSMarkus Armbruster } 74018846deeSMarkus Armbruster 74118846deeSMarkus Armbruster DeviceState *bdrv_get_attached(BlockDriverState *bs) 74218846deeSMarkus Armbruster { 74318846deeSMarkus Armbruster return bs->peer; 74418846deeSMarkus Armbruster } 74518846deeSMarkus Armbruster 746e97fc193Saliguori /* 747e97fc193Saliguori * Run consistency checks on an image 748e97fc193Saliguori * 749e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 750e076f338SKevin Wolf * free of errors) or -errno when an internal error occured. The results of the 751e076f338SKevin Wolf * check are stored in res. 752e97fc193Saliguori */ 753e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) 754e97fc193Saliguori { 755e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 756e97fc193Saliguori return -ENOTSUP; 757e97fc193Saliguori } 758e97fc193Saliguori 759e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 7609ac228e0SKevin Wolf return bs->drv->bdrv_check(bs, res); 761e97fc193Saliguori } 762e97fc193Saliguori 7638a426614SKevin Wolf #define COMMIT_BUF_SECTORS 2048 7648a426614SKevin Wolf 76533e3963eSbellard /* commit COW file into the raw image */ 76633e3963eSbellard int bdrv_commit(BlockDriverState *bs) 76733e3963eSbellard { 76819cb3738Sbellard BlockDriver *drv = bs->drv; 769ee181196SKevin Wolf BlockDriver *backing_drv; 7708a426614SKevin Wolf int64_t sector, total_sectors; 7718a426614SKevin Wolf int n, ro, open_flags; 7724dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 7738a426614SKevin Wolf uint8_t *buf; 7744dca4b63SNaphtali Sprei char filename[1024]; 7754dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 77633e3963eSbellard 77719cb3738Sbellard if (!drv) 77819cb3738Sbellard return -ENOMEDIUM; 77933e3963eSbellard 7804dca4b63SNaphtali Sprei if (!bs->backing_hd) { 7814dca4b63SNaphtali Sprei return -ENOTSUP; 7824dca4b63SNaphtali Sprei } 7834dca4b63SNaphtali Sprei 7844dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 785ea2384d3Sbellard return -EACCES; 78633e3963eSbellard } 78733e3963eSbellard 788ee181196SKevin Wolf backing_drv = bs->backing_hd->drv; 7894dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 7904dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 7914dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 7924dca4b63SNaphtali Sprei 7934dca4b63SNaphtali Sprei if (ro) { 7944dca4b63SNaphtali Sprei /* re-open as RW */ 7954dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 7964dca4b63SNaphtali Sprei bs->backing_hd = NULL; 7974dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 798ee181196SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, 799ee181196SKevin Wolf backing_drv); 8004dca4b63SNaphtali Sprei if (rw_ret < 0) { 8014dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 8024dca4b63SNaphtali Sprei /* try to re-open read-only */ 8034dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 804ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 805ee181196SKevin Wolf backing_drv); 8064dca4b63SNaphtali Sprei if (ret < 0) { 8074dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 8084dca4b63SNaphtali Sprei /* drive not functional anymore */ 8094dca4b63SNaphtali Sprei bs->drv = NULL; 8104dca4b63SNaphtali Sprei return ret; 8114dca4b63SNaphtali Sprei } 8124dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 8134dca4b63SNaphtali Sprei return rw_ret; 8144dca4b63SNaphtali Sprei } 8154dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 816ea2384d3Sbellard } 817ea2384d3Sbellard 8186ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 8198a426614SKevin Wolf buf = qemu_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE); 8208a426614SKevin Wolf 8218a426614SKevin Wolf for (sector = 0; sector < total_sectors; sector += n) { 8228a426614SKevin Wolf if (drv->bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) { 8238a426614SKevin Wolf 8248a426614SKevin Wolf if (bdrv_read(bs, sector, buf, n) != 0) { 8254dca4b63SNaphtali Sprei ret = -EIO; 8264dca4b63SNaphtali Sprei goto ro_cleanup; 82733e3963eSbellard } 82833e3963eSbellard 8298a426614SKevin Wolf if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) { 8304dca4b63SNaphtali Sprei ret = -EIO; 8314dca4b63SNaphtali Sprei goto ro_cleanup; 83233e3963eSbellard } 83333e3963eSbellard } 83433e3963eSbellard } 83595389c86Sbellard 8361d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 8371d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 8381d44952fSChristoph Hellwig bdrv_flush(bs); 8391d44952fSChristoph Hellwig } 84095389c86Sbellard 8413f5075aeSChristoph Hellwig /* 8423f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 8433f5075aeSChristoph Hellwig * stable on disk. 8443f5075aeSChristoph Hellwig */ 8453f5075aeSChristoph Hellwig if (bs->backing_hd) 8463f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 8474dca4b63SNaphtali Sprei 8484dca4b63SNaphtali Sprei ro_cleanup: 8498a426614SKevin Wolf qemu_free(buf); 8504dca4b63SNaphtali Sprei 8514dca4b63SNaphtali Sprei if (ro) { 8524dca4b63SNaphtali Sprei /* re-open as RO */ 8534dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 8544dca4b63SNaphtali Sprei bs->backing_hd = NULL; 8554dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 856ee181196SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, 857ee181196SKevin Wolf backing_drv); 8584dca4b63SNaphtali Sprei if (ret < 0) { 8594dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 8604dca4b63SNaphtali Sprei /* drive not functional anymore */ 8614dca4b63SNaphtali Sprei bs->drv = NULL; 8624dca4b63SNaphtali Sprei return ret; 8634dca4b63SNaphtali Sprei } 8644dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 8654dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 8664dca4b63SNaphtali Sprei } 8674dca4b63SNaphtali Sprei 8681d44952fSChristoph Hellwig return ret; 86933e3963eSbellard } 87033e3963eSbellard 8716ab4b5abSMarkus Armbruster void bdrv_commit_all(void) 8726ab4b5abSMarkus Armbruster { 8736ab4b5abSMarkus Armbruster BlockDriverState *bs; 8746ab4b5abSMarkus Armbruster 8756ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 8766ab4b5abSMarkus Armbruster bdrv_commit(bs); 8776ab4b5abSMarkus Armbruster } 8786ab4b5abSMarkus Armbruster } 8796ab4b5abSMarkus Armbruster 880756e6736SKevin Wolf /* 881756e6736SKevin Wolf * Return values: 882756e6736SKevin Wolf * 0 - success 883756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 884756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 885756e6736SKevin Wolf * image file header 886756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 887756e6736SKevin Wolf */ 888756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 889756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 890756e6736SKevin Wolf { 891756e6736SKevin Wolf BlockDriver *drv = bs->drv; 892756e6736SKevin Wolf 893756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 894756e6736SKevin Wolf return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 895756e6736SKevin Wolf } else { 896756e6736SKevin Wolf return -ENOTSUP; 897756e6736SKevin Wolf } 898756e6736SKevin Wolf } 899756e6736SKevin Wolf 90071d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 90171d0770cSaliguori size_t size) 90271d0770cSaliguori { 90371d0770cSaliguori int64_t len; 90471d0770cSaliguori 90571d0770cSaliguori if (!bdrv_is_inserted(bs)) 90671d0770cSaliguori return -ENOMEDIUM; 90771d0770cSaliguori 90871d0770cSaliguori if (bs->growable) 90971d0770cSaliguori return 0; 91071d0770cSaliguori 91171d0770cSaliguori len = bdrv_getlength(bs); 91271d0770cSaliguori 913fbb7b4e0SKevin Wolf if (offset < 0) 914fbb7b4e0SKevin Wolf return -EIO; 915fbb7b4e0SKevin Wolf 916fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 91771d0770cSaliguori return -EIO; 91871d0770cSaliguori 91971d0770cSaliguori return 0; 92071d0770cSaliguori } 92171d0770cSaliguori 92271d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 92371d0770cSaliguori int nb_sectors) 92471d0770cSaliguori { 925eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 926eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 92771d0770cSaliguori } 92871d0770cSaliguori 92919cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 930fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 931fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 932fc01f7e7Sbellard { 933ea2384d3Sbellard BlockDriver *drv = bs->drv; 934fc01f7e7Sbellard 93519cb3738Sbellard if (!drv) 93619cb3738Sbellard return -ENOMEDIUM; 93771d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 93871d0770cSaliguori return -EIO; 939b338082bSbellard 94083f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 94183f64091Sbellard } 942fc01f7e7Sbellard 9437cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 9447cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 9457cd1e32aSlirans@il.ibm.com { 9467cd1e32aSlirans@il.ibm.com int64_t start, end; 947c6d22830SJan Kiszka unsigned long val, idx, bit; 948a55eb92cSJan Kiszka 9496ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 950c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 9517cd1e32aSlirans@il.ibm.com 9527cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 953c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 954c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 955c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 956c6d22830SJan Kiszka if (dirty) { 9576d59fec1SMarcelo Tosatti if (!(val & (1UL << bit))) { 958aaa0eb75SLiran Schour bs->dirty_count++; 9596d59fec1SMarcelo Tosatti val |= 1UL << bit; 960aaa0eb75SLiran Schour } 961c6d22830SJan Kiszka } else { 9626d59fec1SMarcelo Tosatti if (val & (1UL << bit)) { 963aaa0eb75SLiran Schour bs->dirty_count--; 9646d59fec1SMarcelo Tosatti val &= ~(1UL << bit); 965c6d22830SJan Kiszka } 966aaa0eb75SLiran Schour } 967c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 9687cd1e32aSlirans@il.ibm.com } 9697cd1e32aSlirans@il.ibm.com } 9707cd1e32aSlirans@il.ibm.com 97119cb3738Sbellard /* Return < 0 if error. Important errors are: 97219cb3738Sbellard -EIO generic I/O error (may happen for all errors) 97319cb3738Sbellard -ENOMEDIUM No media inserted. 97419cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 97519cb3738Sbellard -EACCES Trying to write a read-only device 97619cb3738Sbellard */ 977fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 978fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 979fc01f7e7Sbellard { 98083f64091Sbellard BlockDriver *drv = bs->drv; 98119cb3738Sbellard if (!bs->drv) 98219cb3738Sbellard return -ENOMEDIUM; 9830849bf08Sbellard if (bs->read_only) 98419cb3738Sbellard return -EACCES; 98571d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 98671d0770cSaliguori return -EIO; 98771d0770cSaliguori 988c6d22830SJan Kiszka if (bs->dirty_bitmap) { 9897cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 9907cd1e32aSlirans@il.ibm.com } 9917cd1e32aSlirans@il.ibm.com 992294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 993294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 994294cc35fSKevin Wolf } 995294cc35fSKevin Wolf 99683f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 99783f64091Sbellard } 99883f64091Sbellard 999eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 1000eda578e5Saliguori void *buf, int count1) 100183f64091Sbellard { 10026ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 100383f64091Sbellard int len, nb_sectors, count; 100483f64091Sbellard int64_t sector_num; 10059a8c4cceSKevin Wolf int ret; 100683f64091Sbellard 100783f64091Sbellard count = count1; 100883f64091Sbellard /* first read to align to sector start */ 10096ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 101083f64091Sbellard if (len > count) 101183f64091Sbellard len = count; 10126ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 101383f64091Sbellard if (len > 0) { 10149a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10159a8c4cceSKevin Wolf return ret; 10166ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 101783f64091Sbellard count -= len; 101883f64091Sbellard if (count == 0) 101983f64091Sbellard return count1; 102083f64091Sbellard sector_num++; 102183f64091Sbellard buf += len; 102283f64091Sbellard } 102383f64091Sbellard 102483f64091Sbellard /* read the sectors "in place" */ 10256ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 102683f64091Sbellard if (nb_sectors > 0) { 10279a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 10289a8c4cceSKevin Wolf return ret; 102983f64091Sbellard sector_num += nb_sectors; 10306ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 103183f64091Sbellard buf += len; 103283f64091Sbellard count -= len; 103383f64091Sbellard } 103483f64091Sbellard 103583f64091Sbellard /* add data from the last sector */ 103683f64091Sbellard if (count > 0) { 10379a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10389a8c4cceSKevin Wolf return ret; 103983f64091Sbellard memcpy(buf, tmp_buf, count); 104083f64091Sbellard } 104183f64091Sbellard return count1; 104283f64091Sbellard } 104383f64091Sbellard 1044eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 1045eda578e5Saliguori const void *buf, int count1) 104683f64091Sbellard { 10476ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 104883f64091Sbellard int len, nb_sectors, count; 104983f64091Sbellard int64_t sector_num; 10509a8c4cceSKevin Wolf int ret; 105183f64091Sbellard 105283f64091Sbellard count = count1; 105383f64091Sbellard /* first write to align to sector start */ 10546ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 105583f64091Sbellard if (len > count) 105683f64091Sbellard len = count; 10576ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 105883f64091Sbellard if (len > 0) { 10599a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10609a8c4cceSKevin Wolf return ret; 10616ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 10629a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 10639a8c4cceSKevin Wolf return ret; 106483f64091Sbellard count -= len; 106583f64091Sbellard if (count == 0) 106683f64091Sbellard return count1; 106783f64091Sbellard sector_num++; 106883f64091Sbellard buf += len; 106983f64091Sbellard } 107083f64091Sbellard 107183f64091Sbellard /* write the sectors "in place" */ 10726ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 107383f64091Sbellard if (nb_sectors > 0) { 10749a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 10759a8c4cceSKevin Wolf return ret; 107683f64091Sbellard sector_num += nb_sectors; 10776ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 107883f64091Sbellard buf += len; 107983f64091Sbellard count -= len; 108083f64091Sbellard } 108183f64091Sbellard 108283f64091Sbellard /* add data from the last sector */ 108383f64091Sbellard if (count > 0) { 10849a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10859a8c4cceSKevin Wolf return ret; 108683f64091Sbellard memcpy(tmp_buf, buf, count); 10879a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 10889a8c4cceSKevin Wolf return ret; 108983f64091Sbellard } 109083f64091Sbellard return count1; 109183f64091Sbellard } 109283f64091Sbellard 1093f08145feSKevin Wolf /* 1094f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1095f08145feSKevin Wolf * request (acts as a barrier) 1096f08145feSKevin Wolf * 1097f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1098f08145feSKevin Wolf */ 1099f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1100f08145feSKevin Wolf const void *buf, int count) 1101f08145feSKevin Wolf { 1102f08145feSKevin Wolf int ret; 1103f08145feSKevin Wolf 1104f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1105f08145feSKevin Wolf if (ret < 0) { 1106f08145feSKevin Wolf return ret; 1107f08145feSKevin Wolf } 1108f08145feSKevin Wolf 1109f08145feSKevin Wolf /* No flush needed for cache=writethrough, it uses O_DSYNC */ 1110f08145feSKevin Wolf if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) { 1111f08145feSKevin Wolf bdrv_flush(bs); 1112f08145feSKevin Wolf } 1113f08145feSKevin Wolf 1114f08145feSKevin Wolf return 0; 1115f08145feSKevin Wolf } 1116f08145feSKevin Wolf 1117f08145feSKevin Wolf /* 1118f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1119f08145feSKevin Wolf * request (acts as a barrier) 1120f08145feSKevin Wolf * 1121f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1122f08145feSKevin Wolf */ 1123f08145feSKevin Wolf int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num, 1124f08145feSKevin Wolf const uint8_t *buf, int nb_sectors) 1125f08145feSKevin Wolf { 1126f08145feSKevin Wolf return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num, 1127f08145feSKevin Wolf buf, BDRV_SECTOR_SIZE * nb_sectors); 1128f08145feSKevin Wolf } 1129f08145feSKevin Wolf 113083f64091Sbellard /** 113183f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 113283f64091Sbellard */ 113383f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 113483f64091Sbellard { 113583f64091Sbellard BlockDriver *drv = bs->drv; 113651762288SStefan Hajnoczi int ret; 113783f64091Sbellard if (!drv) 113819cb3738Sbellard return -ENOMEDIUM; 113983f64091Sbellard if (!drv->bdrv_truncate) 114083f64091Sbellard return -ENOTSUP; 114159f2689dSNaphtali Sprei if (bs->read_only) 114259f2689dSNaphtali Sprei return -EACCES; 11438591675fSMarcelo Tosatti if (bdrv_in_use(bs)) 11448591675fSMarcelo Tosatti return -EBUSY; 114551762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 114651762288SStefan Hajnoczi if (ret == 0) { 114751762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 1148db97ee6aSChristoph Hellwig if (bs->change_cb) { 1149db97ee6aSChristoph Hellwig bs->change_cb(bs->change_opaque, CHANGE_SIZE); 1150db97ee6aSChristoph Hellwig } 115151762288SStefan Hajnoczi } 115251762288SStefan Hajnoczi return ret; 115383f64091Sbellard } 115483f64091Sbellard 115583f64091Sbellard /** 115683f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 115783f64091Sbellard */ 115883f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 115983f64091Sbellard { 116083f64091Sbellard BlockDriver *drv = bs->drv; 116183f64091Sbellard if (!drv) 116219cb3738Sbellard return -ENOMEDIUM; 116351762288SStefan Hajnoczi 116451762288SStefan Hajnoczi /* Fixed size devices use the total_sectors value for speed instead of 116551762288SStefan Hajnoczi issuing a length query (like lseek) on each call. Also, legacy block 116651762288SStefan Hajnoczi drivers don't provide a bdrv_getlength function and must use 116751762288SStefan Hajnoczi total_sectors. */ 116851762288SStefan Hajnoczi if (!bs->growable || !drv->bdrv_getlength) { 11696ea44308SJan Kiszka return bs->total_sectors * BDRV_SECTOR_SIZE; 117083f64091Sbellard } 117183f64091Sbellard return drv->bdrv_getlength(bs); 1172fc01f7e7Sbellard } 1173fc01f7e7Sbellard 117419cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 117596b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 1176fc01f7e7Sbellard { 117719cb3738Sbellard int64_t length; 117819cb3738Sbellard length = bdrv_getlength(bs); 117919cb3738Sbellard if (length < 0) 118019cb3738Sbellard length = 0; 118119cb3738Sbellard else 11826ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 118319cb3738Sbellard *nb_sectors_ptr = length; 1184fc01f7e7Sbellard } 1185cf98951bSbellard 1186f3d54fc4Saliguori struct partition { 1187f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 1188f3d54fc4Saliguori uint8_t head; /* starting head */ 1189f3d54fc4Saliguori uint8_t sector; /* starting sector */ 1190f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 1191f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 1192f3d54fc4Saliguori uint8_t end_head; /* end head */ 1193f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 1194f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 1195f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 1196f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 1197f3d54fc4Saliguori } __attribute__((packed)); 1198f3d54fc4Saliguori 1199f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 1200f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 1201f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 1202f3d54fc4Saliguori { 1203eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 1204f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 1205f3d54fc4Saliguori struct partition *p; 1206f3d54fc4Saliguori uint32_t nr_sects; 1207a38131b6Sblueswir1 uint64_t nb_sectors; 1208f3d54fc4Saliguori 1209f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1210f3d54fc4Saliguori 1211f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 1212f3d54fc4Saliguori if (ret < 0) 1213f3d54fc4Saliguori return -1; 1214f3d54fc4Saliguori /* test msdos magic */ 1215f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 1216f3d54fc4Saliguori return -1; 1217f3d54fc4Saliguori for(i = 0; i < 4; i++) { 1218f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 1219f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 1220f3d54fc4Saliguori if (nr_sects && p->end_head) { 1221f3d54fc4Saliguori /* We make the assumption that the partition terminates on 1222f3d54fc4Saliguori a cylinder boundary */ 1223f3d54fc4Saliguori heads = p->end_head + 1; 1224f3d54fc4Saliguori sectors = p->end_sector & 63; 1225f3d54fc4Saliguori if (sectors == 0) 1226f3d54fc4Saliguori continue; 1227f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 1228f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 1229f3d54fc4Saliguori continue; 1230f3d54fc4Saliguori *pheads = heads; 1231f3d54fc4Saliguori *psectors = sectors; 1232f3d54fc4Saliguori *pcylinders = cylinders; 1233f3d54fc4Saliguori #if 0 1234f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 1235f3d54fc4Saliguori cylinders, heads, sectors); 1236f3d54fc4Saliguori #endif 1237f3d54fc4Saliguori return 0; 1238f3d54fc4Saliguori } 1239f3d54fc4Saliguori } 1240f3d54fc4Saliguori return -1; 1241f3d54fc4Saliguori } 1242f3d54fc4Saliguori 1243f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 1244f3d54fc4Saliguori { 1245f3d54fc4Saliguori int translation, lba_detected = 0; 1246f3d54fc4Saliguori int cylinders, heads, secs; 1247a38131b6Sblueswir1 uint64_t nb_sectors; 1248f3d54fc4Saliguori 1249f3d54fc4Saliguori /* if a geometry hint is available, use it */ 1250f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1251f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 1252f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 1253f3d54fc4Saliguori if (cylinders != 0) { 1254f3d54fc4Saliguori *pcyls = cylinders; 1255f3d54fc4Saliguori *pheads = heads; 1256f3d54fc4Saliguori *psecs = secs; 1257f3d54fc4Saliguori } else { 1258f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 1259f3d54fc4Saliguori if (heads > 16) { 1260f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 1261f3d54fc4Saliguori translation was active, so the default 1262f3d54fc4Saliguori hardware geometry is OK */ 1263f3d54fc4Saliguori lba_detected = 1; 1264f3d54fc4Saliguori goto default_geometry; 1265f3d54fc4Saliguori } else { 1266f3d54fc4Saliguori *pcyls = cylinders; 1267f3d54fc4Saliguori *pheads = heads; 1268f3d54fc4Saliguori *psecs = secs; 1269f3d54fc4Saliguori /* disable any translation to be in sync with 1270f3d54fc4Saliguori the logical geometry */ 1271f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 1272f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1273f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 1274f3d54fc4Saliguori } 1275f3d54fc4Saliguori } 1276f3d54fc4Saliguori } else { 1277f3d54fc4Saliguori default_geometry: 1278f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 1279f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 1280f3d54fc4Saliguori 1281f3d54fc4Saliguori if (cylinders > 16383) 1282f3d54fc4Saliguori cylinders = 16383; 1283f3d54fc4Saliguori else if (cylinders < 2) 1284f3d54fc4Saliguori cylinders = 2; 1285f3d54fc4Saliguori *pcyls = cylinders; 1286f3d54fc4Saliguori *pheads = 16; 1287f3d54fc4Saliguori *psecs = 63; 1288f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 1289f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 1290f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1291f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 1292f3d54fc4Saliguori } else { 1293f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1294f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 1295f3d54fc4Saliguori } 1296f3d54fc4Saliguori } 1297f3d54fc4Saliguori } 1298f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 1299f3d54fc4Saliguori } 1300f3d54fc4Saliguori } 1301f3d54fc4Saliguori 1302b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 1303b338082bSbellard int cyls, int heads, int secs) 1304b338082bSbellard { 1305b338082bSbellard bs->cyls = cyls; 1306b338082bSbellard bs->heads = heads; 1307b338082bSbellard bs->secs = secs; 1308b338082bSbellard } 1309b338082bSbellard 1310b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 1311b338082bSbellard { 1312b338082bSbellard bs->type = type; 1313b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 1314b338082bSbellard type == BDRV_TYPE_FLOPPY)); 1315b338082bSbellard } 1316b338082bSbellard 131746d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 131846d4767dSbellard { 131946d4767dSbellard bs->translation = translation; 132046d4767dSbellard } 132146d4767dSbellard 1322b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 1323b338082bSbellard int *pcyls, int *pheads, int *psecs) 1324b338082bSbellard { 1325b338082bSbellard *pcyls = bs->cyls; 1326b338082bSbellard *pheads = bs->heads; 1327b338082bSbellard *psecs = bs->secs; 1328b338082bSbellard } 1329b338082bSbellard 13305bbdbb46SBlue Swirl /* Recognize floppy formats */ 13315bbdbb46SBlue Swirl typedef struct FDFormat { 13325bbdbb46SBlue Swirl FDriveType drive; 13335bbdbb46SBlue Swirl uint8_t last_sect; 13345bbdbb46SBlue Swirl uint8_t max_track; 13355bbdbb46SBlue Swirl uint8_t max_head; 13365bbdbb46SBlue Swirl } FDFormat; 13375bbdbb46SBlue Swirl 13385bbdbb46SBlue Swirl static const FDFormat fd_formats[] = { 13395bbdbb46SBlue Swirl /* First entry is default format */ 13405bbdbb46SBlue Swirl /* 1.44 MB 3"1/2 floppy disks */ 13415bbdbb46SBlue Swirl { FDRIVE_DRV_144, 18, 80, 1, }, 13425bbdbb46SBlue Swirl { FDRIVE_DRV_144, 20, 80, 1, }, 13435bbdbb46SBlue Swirl { FDRIVE_DRV_144, 21, 80, 1, }, 13445bbdbb46SBlue Swirl { FDRIVE_DRV_144, 21, 82, 1, }, 13455bbdbb46SBlue Swirl { FDRIVE_DRV_144, 21, 83, 1, }, 13465bbdbb46SBlue Swirl { FDRIVE_DRV_144, 22, 80, 1, }, 13475bbdbb46SBlue Swirl { FDRIVE_DRV_144, 23, 80, 1, }, 13485bbdbb46SBlue Swirl { FDRIVE_DRV_144, 24, 80, 1, }, 13495bbdbb46SBlue Swirl /* 2.88 MB 3"1/2 floppy disks */ 13505bbdbb46SBlue Swirl { FDRIVE_DRV_288, 36, 80, 1, }, 13515bbdbb46SBlue Swirl { FDRIVE_DRV_288, 39, 80, 1, }, 13525bbdbb46SBlue Swirl { FDRIVE_DRV_288, 40, 80, 1, }, 13535bbdbb46SBlue Swirl { FDRIVE_DRV_288, 44, 80, 1, }, 13545bbdbb46SBlue Swirl { FDRIVE_DRV_288, 48, 80, 1, }, 13555bbdbb46SBlue Swirl /* 720 kB 3"1/2 floppy disks */ 13565bbdbb46SBlue Swirl { FDRIVE_DRV_144, 9, 80, 1, }, 13575bbdbb46SBlue Swirl { FDRIVE_DRV_144, 10, 80, 1, }, 13585bbdbb46SBlue Swirl { FDRIVE_DRV_144, 10, 82, 1, }, 13595bbdbb46SBlue Swirl { FDRIVE_DRV_144, 10, 83, 1, }, 13605bbdbb46SBlue Swirl { FDRIVE_DRV_144, 13, 80, 1, }, 13615bbdbb46SBlue Swirl { FDRIVE_DRV_144, 14, 80, 1, }, 13625bbdbb46SBlue Swirl /* 1.2 MB 5"1/4 floppy disks */ 13635bbdbb46SBlue Swirl { FDRIVE_DRV_120, 15, 80, 1, }, 13645bbdbb46SBlue Swirl { FDRIVE_DRV_120, 18, 80, 1, }, 13655bbdbb46SBlue Swirl { FDRIVE_DRV_120, 18, 82, 1, }, 13665bbdbb46SBlue Swirl { FDRIVE_DRV_120, 18, 83, 1, }, 13675bbdbb46SBlue Swirl { FDRIVE_DRV_120, 20, 80, 1, }, 13685bbdbb46SBlue Swirl /* 720 kB 5"1/4 floppy disks */ 13695bbdbb46SBlue Swirl { FDRIVE_DRV_120, 9, 80, 1, }, 13705bbdbb46SBlue Swirl { FDRIVE_DRV_120, 11, 80, 1, }, 13715bbdbb46SBlue Swirl /* 360 kB 5"1/4 floppy disks */ 13725bbdbb46SBlue Swirl { FDRIVE_DRV_120, 9, 40, 1, }, 13735bbdbb46SBlue Swirl { FDRIVE_DRV_120, 9, 40, 0, }, 13745bbdbb46SBlue Swirl { FDRIVE_DRV_120, 10, 41, 1, }, 13755bbdbb46SBlue Swirl { FDRIVE_DRV_120, 10, 42, 1, }, 13765bbdbb46SBlue Swirl /* 320 kB 5"1/4 floppy disks */ 13775bbdbb46SBlue Swirl { FDRIVE_DRV_120, 8, 40, 1, }, 13785bbdbb46SBlue Swirl { FDRIVE_DRV_120, 8, 40, 0, }, 13795bbdbb46SBlue Swirl /* 360 kB must match 5"1/4 better than 3"1/2... */ 13805bbdbb46SBlue Swirl { FDRIVE_DRV_144, 9, 80, 0, }, 13815bbdbb46SBlue Swirl /* end */ 13825bbdbb46SBlue Swirl { FDRIVE_DRV_NONE, -1, -1, 0, }, 13835bbdbb46SBlue Swirl }; 13845bbdbb46SBlue Swirl 13855bbdbb46SBlue Swirl void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads, 13865bbdbb46SBlue Swirl int *max_track, int *last_sect, 13875bbdbb46SBlue Swirl FDriveType drive_in, FDriveType *drive) 13885bbdbb46SBlue Swirl { 13895bbdbb46SBlue Swirl const FDFormat *parse; 13905bbdbb46SBlue Swirl uint64_t nb_sectors, size; 13915bbdbb46SBlue Swirl int i, first_match, match; 13925bbdbb46SBlue Swirl 13935bbdbb46SBlue Swirl bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect); 13945bbdbb46SBlue Swirl if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) { 13955bbdbb46SBlue Swirl /* User defined disk */ 13965bbdbb46SBlue Swirl } else { 13975bbdbb46SBlue Swirl bdrv_get_geometry(bs, &nb_sectors); 13985bbdbb46SBlue Swirl match = -1; 13995bbdbb46SBlue Swirl first_match = -1; 14005bbdbb46SBlue Swirl for (i = 0; ; i++) { 14015bbdbb46SBlue Swirl parse = &fd_formats[i]; 14025bbdbb46SBlue Swirl if (parse->drive == FDRIVE_DRV_NONE) { 14035bbdbb46SBlue Swirl break; 14045bbdbb46SBlue Swirl } 14055bbdbb46SBlue Swirl if (drive_in == parse->drive || 14065bbdbb46SBlue Swirl drive_in == FDRIVE_DRV_NONE) { 14075bbdbb46SBlue Swirl size = (parse->max_head + 1) * parse->max_track * 14085bbdbb46SBlue Swirl parse->last_sect; 14095bbdbb46SBlue Swirl if (nb_sectors == size) { 14105bbdbb46SBlue Swirl match = i; 14115bbdbb46SBlue Swirl break; 14125bbdbb46SBlue Swirl } 14135bbdbb46SBlue Swirl if (first_match == -1) { 14145bbdbb46SBlue Swirl first_match = i; 14155bbdbb46SBlue Swirl } 14165bbdbb46SBlue Swirl } 14175bbdbb46SBlue Swirl } 14185bbdbb46SBlue Swirl if (match == -1) { 14195bbdbb46SBlue Swirl if (first_match == -1) { 14205bbdbb46SBlue Swirl match = 1; 14215bbdbb46SBlue Swirl } else { 14225bbdbb46SBlue Swirl match = first_match; 14235bbdbb46SBlue Swirl } 14245bbdbb46SBlue Swirl parse = &fd_formats[match]; 14255bbdbb46SBlue Swirl } 14265bbdbb46SBlue Swirl *nb_heads = parse->max_head + 1; 14275bbdbb46SBlue Swirl *max_track = parse->max_track; 14285bbdbb46SBlue Swirl *last_sect = parse->last_sect; 14295bbdbb46SBlue Swirl *drive = parse->drive; 14305bbdbb46SBlue Swirl } 14315bbdbb46SBlue Swirl } 14325bbdbb46SBlue Swirl 1433b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 1434b338082bSbellard { 1435b338082bSbellard return bs->type; 1436b338082bSbellard } 1437b338082bSbellard 143846d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 143946d4767dSbellard { 144046d4767dSbellard return bs->translation; 144146d4767dSbellard } 144246d4767dSbellard 1443abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 1444abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 1445abd7f68dSMarkus Armbruster { 1446abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 1447abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 1448abd7f68dSMarkus Armbruster } 1449abd7f68dSMarkus Armbruster 1450abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 1451abd7f68dSMarkus Armbruster { 1452abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 1453abd7f68dSMarkus Armbruster } 1454abd7f68dSMarkus Armbruster 14557d0d6950SMarkus Armbruster void bdrv_set_removable(BlockDriverState *bs, int removable) 14567d0d6950SMarkus Armbruster { 14577d0d6950SMarkus Armbruster bs->removable = removable; 14587d0d6950SMarkus Armbruster if (removable && bs == bs_snapshots) { 14597d0d6950SMarkus Armbruster bs_snapshots = NULL; 14607d0d6950SMarkus Armbruster } 14617d0d6950SMarkus Armbruster } 14627d0d6950SMarkus Armbruster 1463b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 1464b338082bSbellard { 1465b338082bSbellard return bs->removable; 1466b338082bSbellard } 1467b338082bSbellard 1468b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 1469b338082bSbellard { 1470b338082bSbellard return bs->read_only; 1471b338082bSbellard } 1472b338082bSbellard 1473985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 1474985a03b0Sths { 1475985a03b0Sths return bs->sg; 1476985a03b0Sths } 1477985a03b0Sths 1478e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 1479e900a7b7SChristoph Hellwig { 1480e900a7b7SChristoph Hellwig return bs->enable_write_cache; 1481e900a7b7SChristoph Hellwig } 1482e900a7b7SChristoph Hellwig 148319cb3738Sbellard /* XXX: no longer used */ 1484b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 1485db97ee6aSChristoph Hellwig void (*change_cb)(void *opaque, int reason), 1486db97ee6aSChristoph Hellwig void *opaque) 1487b338082bSbellard { 1488b338082bSbellard bs->change_cb = change_cb; 1489b338082bSbellard bs->change_opaque = opaque; 1490b338082bSbellard } 1491b338082bSbellard 1492ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 1493ea2384d3Sbellard { 1494ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 1495ea2384d3Sbellard return 1; 1496ea2384d3Sbellard return bs->encrypted; 1497ea2384d3Sbellard } 1498ea2384d3Sbellard 1499c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 1500c0f4ce77Saliguori { 1501c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 1502c0f4ce77Saliguori 1503c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 1504c0f4ce77Saliguori return 1; 1505c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 1506c0f4ce77Saliguori } 1507c0f4ce77Saliguori 1508ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 1509ea2384d3Sbellard { 1510ea2384d3Sbellard int ret; 1511ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 1512ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 1513ea2384d3Sbellard if (ret < 0) 1514ea2384d3Sbellard return ret; 1515ea2384d3Sbellard if (!bs->encrypted) 1516ea2384d3Sbellard return 0; 1517ea2384d3Sbellard } 1518fd04a2aeSShahar Havivi if (!bs->encrypted) { 1519fd04a2aeSShahar Havivi return -EINVAL; 1520fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 1521fd04a2aeSShahar Havivi return -ENOMEDIUM; 1522fd04a2aeSShahar Havivi } 1523c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 1524bb5fc20fSaliguori if (ret < 0) { 1525bb5fc20fSaliguori bs->valid_key = 0; 1526bb5fc20fSaliguori } else if (!bs->valid_key) { 1527bb5fc20fSaliguori bs->valid_key = 1; 1528bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 1529bb5fc20fSaliguori bs->media_changed = 1; 1530bb5fc20fSaliguori if (bs->change_cb) 1531db97ee6aSChristoph Hellwig bs->change_cb(bs->change_opaque, CHANGE_MEDIA); 1532bb5fc20fSaliguori } 1533c0f4ce77Saliguori return ret; 1534ea2384d3Sbellard } 1535ea2384d3Sbellard 1536ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 1537ea2384d3Sbellard { 153819cb3738Sbellard if (!bs->drv) { 1539ea2384d3Sbellard buf[0] = '\0'; 1540ea2384d3Sbellard } else { 1541ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 1542ea2384d3Sbellard } 1543ea2384d3Sbellard } 1544ea2384d3Sbellard 1545ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 1546ea2384d3Sbellard void *opaque) 1547ea2384d3Sbellard { 1548ea2384d3Sbellard BlockDriver *drv; 1549ea2384d3Sbellard 15508a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 1551ea2384d3Sbellard it(opaque, drv->format_name); 1552ea2384d3Sbellard } 1553ea2384d3Sbellard } 1554ea2384d3Sbellard 1555b338082bSbellard BlockDriverState *bdrv_find(const char *name) 1556b338082bSbellard { 1557b338082bSbellard BlockDriverState *bs; 1558b338082bSbellard 15591b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 15601b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 1561b338082bSbellard return bs; 1562b338082bSbellard } 15631b7bdbc1SStefan Hajnoczi } 1564b338082bSbellard return NULL; 1565b338082bSbellard } 1566b338082bSbellard 15672f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 15682f399b0aSMarkus Armbruster { 15692f399b0aSMarkus Armbruster if (!bs) { 15702f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 15712f399b0aSMarkus Armbruster } 15722f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 15732f399b0aSMarkus Armbruster } 15742f399b0aSMarkus Armbruster 157551de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 157681d0912dSbellard { 157781d0912dSbellard BlockDriverState *bs; 157881d0912dSbellard 15791b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 158051de9760Saliguori it(opaque, bs); 158181d0912dSbellard } 158281d0912dSbellard } 158381d0912dSbellard 1584ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1585ea2384d3Sbellard { 1586ea2384d3Sbellard return bs->device_name; 1587ea2384d3Sbellard } 1588ea2384d3Sbellard 1589205ef796SKevin Wolf int bdrv_flush(BlockDriverState *bs) 15907a6cba61Spbrook { 1591016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 1592205ef796SKevin Wolf return 0; 1593016f5cf6SAlexander Graf } 1594016f5cf6SAlexander Graf 1595205ef796SKevin Wolf if (bs->drv && bs->drv->bdrv_flush) { 1596205ef796SKevin Wolf return bs->drv->bdrv_flush(bs); 1597205ef796SKevin Wolf } 1598205ef796SKevin Wolf 1599205ef796SKevin Wolf /* 1600205ef796SKevin Wolf * Some block drivers always operate in either writethrough or unsafe mode 1601205ef796SKevin Wolf * and don't support bdrv_flush therefore. Usually qemu doesn't know how 1602205ef796SKevin Wolf * the server works (because the behaviour is hardcoded or depends on 1603205ef796SKevin Wolf * server-side configuration), so we can't ensure that everything is safe 1604205ef796SKevin Wolf * on disk. Returning an error doesn't work because that would break guests 1605205ef796SKevin Wolf * even if the server operates in writethrough mode. 1606205ef796SKevin Wolf * 1607205ef796SKevin Wolf * Let's hope the user knows what he's doing. 1608205ef796SKevin Wolf */ 1609205ef796SKevin Wolf return 0; 16107a6cba61Spbrook } 16117a6cba61Spbrook 1612c6ca28d6Saliguori void bdrv_flush_all(void) 1613c6ca28d6Saliguori { 1614c6ca28d6Saliguori BlockDriverState *bs; 1615c6ca28d6Saliguori 16161b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1617c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 16181b7bdbc1SStefan Hajnoczi (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { 1619c6ca28d6Saliguori bdrv_flush(bs); 1620c6ca28d6Saliguori } 16211b7bdbc1SStefan Hajnoczi } 16221b7bdbc1SStefan Hajnoczi } 1623c6ca28d6Saliguori 1624f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 1625f2feebbdSKevin Wolf { 1626f2feebbdSKevin Wolf assert(bs->drv); 1627f2feebbdSKevin Wolf 1628336c1c12SKevin Wolf if (bs->drv->bdrv_has_zero_init) { 1629336c1c12SKevin Wolf return bs->drv->bdrv_has_zero_init(bs); 1630f2feebbdSKevin Wolf } 1631f2feebbdSKevin Wolf 1632f2feebbdSKevin Wolf return 1; 1633f2feebbdSKevin Wolf } 1634f2feebbdSKevin Wolf 1635bb8bf76fSChristoph Hellwig int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors) 1636bb8bf76fSChristoph Hellwig { 1637bb8bf76fSChristoph Hellwig if (!bs->drv) { 1638bb8bf76fSChristoph Hellwig return -ENOMEDIUM; 1639bb8bf76fSChristoph Hellwig } 1640bb8bf76fSChristoph Hellwig if (!bs->drv->bdrv_discard) { 1641bb8bf76fSChristoph Hellwig return 0; 1642bb8bf76fSChristoph Hellwig } 1643bb8bf76fSChristoph Hellwig return bs->drv->bdrv_discard(bs, sector_num, nb_sectors); 1644bb8bf76fSChristoph Hellwig } 1645bb8bf76fSChristoph Hellwig 1646f58c7b35Sths /* 1647f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1648f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1649f58c7b35Sths * hence all their sectors are reported as allocated. 1650f58c7b35Sths * 1651f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1652f58c7b35Sths * the specified sector) that are known to be in the same 1653f58c7b35Sths * allocated/unallocated state. 1654f58c7b35Sths * 1655f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1656f58c7b35Sths */ 1657f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1658f58c7b35Sths int *pnum) 1659f58c7b35Sths { 1660f58c7b35Sths int64_t n; 1661f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1662f58c7b35Sths if (sector_num >= bs->total_sectors) { 1663f58c7b35Sths *pnum = 0; 1664f58c7b35Sths return 0; 1665f58c7b35Sths } 1666f58c7b35Sths n = bs->total_sectors - sector_num; 1667f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1668f58c7b35Sths return 1; 1669f58c7b35Sths } 1670f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1671f58c7b35Sths } 1672f58c7b35Sths 16732582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv, 16742582bfedSLuiz Capitulino BlockMonEventAction action, int is_read) 16752582bfedSLuiz Capitulino { 16762582bfedSLuiz Capitulino QObject *data; 16772582bfedSLuiz Capitulino const char *action_str; 16782582bfedSLuiz Capitulino 16792582bfedSLuiz Capitulino switch (action) { 16802582bfedSLuiz Capitulino case BDRV_ACTION_REPORT: 16812582bfedSLuiz Capitulino action_str = "report"; 16822582bfedSLuiz Capitulino break; 16832582bfedSLuiz Capitulino case BDRV_ACTION_IGNORE: 16842582bfedSLuiz Capitulino action_str = "ignore"; 16852582bfedSLuiz Capitulino break; 16862582bfedSLuiz Capitulino case BDRV_ACTION_STOP: 16872582bfedSLuiz Capitulino action_str = "stop"; 16882582bfedSLuiz Capitulino break; 16892582bfedSLuiz Capitulino default: 16902582bfedSLuiz Capitulino abort(); 16912582bfedSLuiz Capitulino } 16922582bfedSLuiz Capitulino 16932582bfedSLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 16942582bfedSLuiz Capitulino bdrv->device_name, 16952582bfedSLuiz Capitulino action_str, 16962582bfedSLuiz Capitulino is_read ? "read" : "write"); 16972582bfedSLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 16982582bfedSLuiz Capitulino 16992582bfedSLuiz Capitulino qobject_decref(data); 17002582bfedSLuiz Capitulino } 17012582bfedSLuiz Capitulino 1702d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque) 1703b338082bSbellard { 1704d15e5465SLuiz Capitulino QDict *bs_dict; 1705d15e5465SLuiz Capitulino Monitor *mon = opaque; 1706b338082bSbellard 1707d15e5465SLuiz Capitulino bs_dict = qobject_to_qdict(obj); 1708d15e5465SLuiz Capitulino 1709d15e5465SLuiz Capitulino monitor_printf(mon, "%s: type=%s removable=%d", 1710d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "device"), 1711d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "type"), 1712d15e5465SLuiz Capitulino qdict_get_bool(bs_dict, "removable")); 1713d15e5465SLuiz Capitulino 1714d15e5465SLuiz Capitulino if (qdict_get_bool(bs_dict, "removable")) { 1715d15e5465SLuiz Capitulino monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); 1716b338082bSbellard } 1717d15e5465SLuiz Capitulino 1718d15e5465SLuiz Capitulino if (qdict_haskey(bs_dict, "inserted")) { 1719d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); 1720d15e5465SLuiz Capitulino 1721376253ecSaliguori monitor_printf(mon, " file="); 1722d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "file")); 1723d15e5465SLuiz Capitulino if (qdict_haskey(qdict, "backing_file")) { 1724376253ecSaliguori monitor_printf(mon, " backing_file="); 1725d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); 1726fef30743Sths } 1727d15e5465SLuiz Capitulino monitor_printf(mon, " ro=%d drv=%s encrypted=%d", 1728d15e5465SLuiz Capitulino qdict_get_bool(qdict, "ro"), 1729d15e5465SLuiz Capitulino qdict_get_str(qdict, "drv"), 1730d15e5465SLuiz Capitulino qdict_get_bool(qdict, "encrypted")); 1731b338082bSbellard } else { 1732376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1733b338082bSbellard } 1734d15e5465SLuiz Capitulino 1735376253ecSaliguori monitor_printf(mon, "\n"); 1736b338082bSbellard } 1737d15e5465SLuiz Capitulino 1738d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data) 1739d15e5465SLuiz Capitulino { 1740d15e5465SLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); 1741d15e5465SLuiz Capitulino } 1742d15e5465SLuiz Capitulino 1743d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data) 1744d15e5465SLuiz Capitulino { 1745d15e5465SLuiz Capitulino QList *bs_list; 1746d15e5465SLuiz Capitulino BlockDriverState *bs; 1747d15e5465SLuiz Capitulino 1748d15e5465SLuiz Capitulino bs_list = qlist_new(); 1749d15e5465SLuiz Capitulino 17501b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1751d15e5465SLuiz Capitulino QObject *bs_obj; 1752d15e5465SLuiz Capitulino const char *type = "unknown"; 1753d15e5465SLuiz Capitulino 1754d15e5465SLuiz Capitulino switch(bs->type) { 1755d15e5465SLuiz Capitulino case BDRV_TYPE_HD: 1756d15e5465SLuiz Capitulino type = "hd"; 1757d15e5465SLuiz Capitulino break; 1758d15e5465SLuiz Capitulino case BDRV_TYPE_CDROM: 1759d15e5465SLuiz Capitulino type = "cdrom"; 1760d15e5465SLuiz Capitulino break; 1761d15e5465SLuiz Capitulino case BDRV_TYPE_FLOPPY: 1762d15e5465SLuiz Capitulino type = "floppy"; 1763d15e5465SLuiz Capitulino break; 1764d15e5465SLuiz Capitulino } 1765d15e5465SLuiz Capitulino 1766d15e5465SLuiz Capitulino bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, " 1767d15e5465SLuiz Capitulino "'removable': %i, 'locked': %i }", 1768d15e5465SLuiz Capitulino bs->device_name, type, bs->removable, 1769d15e5465SLuiz Capitulino bs->locked); 1770d15e5465SLuiz Capitulino 1771d15e5465SLuiz Capitulino if (bs->drv) { 1772d15e5465SLuiz Capitulino QObject *obj; 1773d15e5465SLuiz Capitulino QDict *bs_dict = qobject_to_qdict(bs_obj); 1774d15e5465SLuiz Capitulino 1775d15e5465SLuiz Capitulino obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " 1776d15e5465SLuiz Capitulino "'encrypted': %i }", 1777d15e5465SLuiz Capitulino bs->filename, bs->read_only, 1778d15e5465SLuiz Capitulino bs->drv->format_name, 1779d15e5465SLuiz Capitulino bdrv_is_encrypted(bs)); 1780d15e5465SLuiz Capitulino if (bs->backing_file[0] != '\0') { 1781d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(obj); 1782d15e5465SLuiz Capitulino qdict_put(qdict, "backing_file", 1783d15e5465SLuiz Capitulino qstring_from_str(bs->backing_file)); 1784d15e5465SLuiz Capitulino } 1785d15e5465SLuiz Capitulino 1786d15e5465SLuiz Capitulino qdict_put_obj(bs_dict, "inserted", obj); 1787d15e5465SLuiz Capitulino } 1788d15e5465SLuiz Capitulino qlist_append_obj(bs_list, bs_obj); 1789d15e5465SLuiz Capitulino } 1790d15e5465SLuiz Capitulino 1791d15e5465SLuiz Capitulino *ret_data = QOBJECT(bs_list); 1792b338082bSbellard } 1793a36e69ddSths 1794218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque) 1795a36e69ddSths { 1796218a536aSLuiz Capitulino QDict *qdict; 1797218a536aSLuiz Capitulino Monitor *mon = opaque; 1798218a536aSLuiz Capitulino 1799218a536aSLuiz Capitulino qdict = qobject_to_qdict(data); 1800218a536aSLuiz Capitulino monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); 1801218a536aSLuiz Capitulino 1802218a536aSLuiz Capitulino qdict = qobject_to_qdict(qdict_get(qdict, "stats")); 1803218a536aSLuiz Capitulino monitor_printf(mon, " rd_bytes=%" PRId64 1804218a536aSLuiz Capitulino " wr_bytes=%" PRId64 1805218a536aSLuiz Capitulino " rd_operations=%" PRId64 1806218a536aSLuiz Capitulino " wr_operations=%" PRId64 1807218a536aSLuiz Capitulino "\n", 1808218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_bytes"), 1809218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_bytes"), 1810218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_operations"), 1811218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_operations")); 1812218a536aSLuiz Capitulino } 1813218a536aSLuiz Capitulino 1814218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data) 1815218a536aSLuiz Capitulino { 1816218a536aSLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); 1817218a536aSLuiz Capitulino } 1818218a536aSLuiz Capitulino 1819294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs) 1820294cc35fSKevin Wolf { 1821294cc35fSKevin Wolf QObject *res; 1822294cc35fSKevin Wolf QDict *dict; 1823294cc35fSKevin Wolf 1824294cc35fSKevin Wolf res = qobject_from_jsonf("{ 'stats': {" 1825294cc35fSKevin Wolf "'rd_bytes': %" PRId64 "," 1826294cc35fSKevin Wolf "'wr_bytes': %" PRId64 "," 1827294cc35fSKevin Wolf "'rd_operations': %" PRId64 "," 1828294cc35fSKevin Wolf "'wr_operations': %" PRId64 "," 1829294cc35fSKevin Wolf "'wr_highest_offset': %" PRId64 1830294cc35fSKevin Wolf "} }", 1831294cc35fSKevin Wolf bs->rd_bytes, bs->wr_bytes, 1832294cc35fSKevin Wolf bs->rd_ops, bs->wr_ops, 18335ffbbc67SBlue Swirl bs->wr_highest_sector * 18345ffbbc67SBlue Swirl (uint64_t)BDRV_SECTOR_SIZE); 1835294cc35fSKevin Wolf dict = qobject_to_qdict(res); 1836294cc35fSKevin Wolf 1837294cc35fSKevin Wolf if (*bs->device_name) { 1838294cc35fSKevin Wolf qdict_put(dict, "device", qstring_from_str(bs->device_name)); 1839294cc35fSKevin Wolf } 1840294cc35fSKevin Wolf 1841294cc35fSKevin Wolf if (bs->file) { 1842294cc35fSKevin Wolf QObject *parent = bdrv_info_stats_bs(bs->file); 1843294cc35fSKevin Wolf qdict_put_obj(dict, "parent", parent); 1844294cc35fSKevin Wolf } 1845294cc35fSKevin Wolf 1846294cc35fSKevin Wolf return res; 1847294cc35fSKevin Wolf } 1848294cc35fSKevin Wolf 1849218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data) 1850218a536aSLuiz Capitulino { 1851218a536aSLuiz Capitulino QObject *obj; 1852218a536aSLuiz Capitulino QList *devices; 1853a36e69ddSths BlockDriverState *bs; 1854a36e69ddSths 1855218a536aSLuiz Capitulino devices = qlist_new(); 1856218a536aSLuiz Capitulino 18571b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1858294cc35fSKevin Wolf obj = bdrv_info_stats_bs(bs); 1859218a536aSLuiz Capitulino qlist_append_obj(devices, obj); 1860a36e69ddSths } 1861218a536aSLuiz Capitulino 1862218a536aSLuiz Capitulino *ret_data = QOBJECT(devices); 1863a36e69ddSths } 1864ea2384d3Sbellard 1865045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1866045df330Saliguori { 1867045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1868045df330Saliguori return bs->backing_file; 1869045df330Saliguori else if (bs->encrypted) 1870045df330Saliguori return bs->filename; 1871045df330Saliguori else 1872045df330Saliguori return NULL; 1873045df330Saliguori } 1874045df330Saliguori 187583f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 187683f64091Sbellard char *filename, int filename_size) 187783f64091Sbellard { 1878b783e409SKevin Wolf if (!bs->backing_file) { 187983f64091Sbellard pstrcpy(filename, filename_size, ""); 188083f64091Sbellard } else { 188183f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 188283f64091Sbellard } 188383f64091Sbellard } 188483f64091Sbellard 1885faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1886faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1887faea38e7Sbellard { 1888faea38e7Sbellard BlockDriver *drv = bs->drv; 1889faea38e7Sbellard if (!drv) 189019cb3738Sbellard return -ENOMEDIUM; 1891faea38e7Sbellard if (!drv->bdrv_write_compressed) 1892faea38e7Sbellard return -ENOTSUP; 1893fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 1894fbb7b4e0SKevin Wolf return -EIO; 18957cd1e32aSlirans@il.ibm.com 1896c6d22830SJan Kiszka if (bs->dirty_bitmap) { 18977cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 18987cd1e32aSlirans@il.ibm.com } 18997cd1e32aSlirans@il.ibm.com 1900faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1901faea38e7Sbellard } 1902faea38e7Sbellard 1903faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1904faea38e7Sbellard { 1905faea38e7Sbellard BlockDriver *drv = bs->drv; 1906faea38e7Sbellard if (!drv) 190719cb3738Sbellard return -ENOMEDIUM; 1908faea38e7Sbellard if (!drv->bdrv_get_info) 1909faea38e7Sbellard return -ENOTSUP; 1910faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1911faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1912faea38e7Sbellard } 1913faea38e7Sbellard 191445566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 191545566e9cSChristoph Hellwig int64_t pos, int size) 1916178e08a5Saliguori { 1917178e08a5Saliguori BlockDriver *drv = bs->drv; 1918178e08a5Saliguori if (!drv) 1919178e08a5Saliguori return -ENOMEDIUM; 19207cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 192145566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 19227cdb1f6dSMORITA Kazutaka if (bs->file) 19237cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 19247cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1925178e08a5Saliguori } 1926178e08a5Saliguori 192745566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 192845566e9cSChristoph Hellwig int64_t pos, int size) 1929178e08a5Saliguori { 1930178e08a5Saliguori BlockDriver *drv = bs->drv; 1931178e08a5Saliguori if (!drv) 1932178e08a5Saliguori return -ENOMEDIUM; 19337cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 193445566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 19357cdb1f6dSMORITA Kazutaka if (bs->file) 19367cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 19377cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1938178e08a5Saliguori } 1939178e08a5Saliguori 19408b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 19418b9b0cc2SKevin Wolf { 19428b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 19438b9b0cc2SKevin Wolf 19448b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 19458b9b0cc2SKevin Wolf return; 19468b9b0cc2SKevin Wolf } 19478b9b0cc2SKevin Wolf 19488b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 19498b9b0cc2SKevin Wolf 19508b9b0cc2SKevin Wolf } 19518b9b0cc2SKevin Wolf 1952faea38e7Sbellard /**************************************************************/ 1953faea38e7Sbellard /* handling of snapshots */ 1954faea38e7Sbellard 1955feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 1956feeee5acSMiguel Di Ciurcio Filho { 1957feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 1958feeee5acSMiguel Di Ciurcio Filho if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) { 1959feeee5acSMiguel Di Ciurcio Filho return 0; 1960feeee5acSMiguel Di Ciurcio Filho } 1961feeee5acSMiguel Di Ciurcio Filho 1962feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 1963feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 1964feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 1965feeee5acSMiguel Di Ciurcio Filho } 1966feeee5acSMiguel Di Ciurcio Filho return 0; 1967feeee5acSMiguel Di Ciurcio Filho } 1968feeee5acSMiguel Di Ciurcio Filho 1969feeee5acSMiguel Di Ciurcio Filho return 1; 1970feeee5acSMiguel Di Ciurcio Filho } 1971feeee5acSMiguel Di Ciurcio Filho 1972199630b6SBlue Swirl int bdrv_is_snapshot(BlockDriverState *bs) 1973199630b6SBlue Swirl { 1974199630b6SBlue Swirl return !!(bs->open_flags & BDRV_O_SNAPSHOT); 1975199630b6SBlue Swirl } 1976199630b6SBlue Swirl 1977f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 1978f9092b10SMarkus Armbruster { 1979f9092b10SMarkus Armbruster BlockDriverState *bs; 1980f9092b10SMarkus Armbruster 19813ac906f7SMarkus Armbruster if (bs_snapshots) { 1982f9092b10SMarkus Armbruster return bs_snapshots; 19833ac906f7SMarkus Armbruster } 1984f9092b10SMarkus Armbruster 1985f9092b10SMarkus Armbruster bs = NULL; 1986f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 1987f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 19883ac906f7SMarkus Armbruster bs_snapshots = bs; 19893ac906f7SMarkus Armbruster return bs; 1990f9092b10SMarkus Armbruster } 1991f9092b10SMarkus Armbruster } 1992f9092b10SMarkus Armbruster return NULL; 1993f9092b10SMarkus Armbruster } 1994f9092b10SMarkus Armbruster 1995faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1996faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1997faea38e7Sbellard { 1998faea38e7Sbellard BlockDriver *drv = bs->drv; 1999faea38e7Sbellard if (!drv) 200019cb3738Sbellard return -ENOMEDIUM; 20017cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 2002faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 20037cdb1f6dSMORITA Kazutaka if (bs->file) 20047cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 20057cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2006faea38e7Sbellard } 2007faea38e7Sbellard 2008faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 2009faea38e7Sbellard const char *snapshot_id) 2010faea38e7Sbellard { 2011faea38e7Sbellard BlockDriver *drv = bs->drv; 20127cdb1f6dSMORITA Kazutaka int ret, open_ret; 20137cdb1f6dSMORITA Kazutaka 2014faea38e7Sbellard if (!drv) 201519cb3738Sbellard return -ENOMEDIUM; 20167cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 2017faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 20187cdb1f6dSMORITA Kazutaka 20197cdb1f6dSMORITA Kazutaka if (bs->file) { 20207cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 20217cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 20227cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 20237cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 20247cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 20257cdb1f6dSMORITA Kazutaka bs->drv = NULL; 20267cdb1f6dSMORITA Kazutaka return open_ret; 20277cdb1f6dSMORITA Kazutaka } 20287cdb1f6dSMORITA Kazutaka return ret; 20297cdb1f6dSMORITA Kazutaka } 20307cdb1f6dSMORITA Kazutaka 20317cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2032faea38e7Sbellard } 2033faea38e7Sbellard 2034faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 2035faea38e7Sbellard { 2036faea38e7Sbellard BlockDriver *drv = bs->drv; 2037faea38e7Sbellard if (!drv) 203819cb3738Sbellard return -ENOMEDIUM; 20397cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 2040faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 20417cdb1f6dSMORITA Kazutaka if (bs->file) 20427cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 20437cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2044faea38e7Sbellard } 2045faea38e7Sbellard 2046faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 2047faea38e7Sbellard QEMUSnapshotInfo **psn_info) 2048faea38e7Sbellard { 2049faea38e7Sbellard BlockDriver *drv = bs->drv; 2050faea38e7Sbellard if (!drv) 205119cb3738Sbellard return -ENOMEDIUM; 20527cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 2053faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 20547cdb1f6dSMORITA Kazutaka if (bs->file) 20557cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 20567cdb1f6dSMORITA Kazutaka return -ENOTSUP; 2057faea38e7Sbellard } 2058faea38e7Sbellard 205951ef6727Sedison int bdrv_snapshot_load_tmp(BlockDriverState *bs, 206051ef6727Sedison const char *snapshot_name) 206151ef6727Sedison { 206251ef6727Sedison BlockDriver *drv = bs->drv; 206351ef6727Sedison if (!drv) { 206451ef6727Sedison return -ENOMEDIUM; 206551ef6727Sedison } 206651ef6727Sedison if (!bs->read_only) { 206751ef6727Sedison return -EINVAL; 206851ef6727Sedison } 206951ef6727Sedison if (drv->bdrv_snapshot_load_tmp) { 207051ef6727Sedison return drv->bdrv_snapshot_load_tmp(bs, snapshot_name); 207151ef6727Sedison } 207251ef6727Sedison return -ENOTSUP; 207351ef6727Sedison } 207451ef6727Sedison 2075faea38e7Sbellard #define NB_SUFFIXES 4 2076faea38e7Sbellard 2077faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 2078faea38e7Sbellard { 2079faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 2080faea38e7Sbellard int64_t base; 2081faea38e7Sbellard int i; 2082faea38e7Sbellard 2083faea38e7Sbellard if (size <= 999) { 2084faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 2085faea38e7Sbellard } else { 2086faea38e7Sbellard base = 1024; 2087faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 2088faea38e7Sbellard if (size < (10 * base)) { 2089faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 2090faea38e7Sbellard (double)size / base, 2091faea38e7Sbellard suffixes[i]); 2092faea38e7Sbellard break; 2093faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 2094faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 2095faea38e7Sbellard ((size + (base >> 1)) / base), 2096faea38e7Sbellard suffixes[i]); 2097faea38e7Sbellard break; 2098faea38e7Sbellard } 2099faea38e7Sbellard base = base * 1024; 2100faea38e7Sbellard } 2101faea38e7Sbellard } 2102faea38e7Sbellard return buf; 2103faea38e7Sbellard } 2104faea38e7Sbellard 2105faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 2106faea38e7Sbellard { 2107faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 21083b9f94e1Sbellard #ifdef _WIN32 21093b9f94e1Sbellard struct tm *ptm; 21103b9f94e1Sbellard #else 2111faea38e7Sbellard struct tm tm; 21123b9f94e1Sbellard #endif 2113faea38e7Sbellard time_t ti; 2114faea38e7Sbellard int64_t secs; 2115faea38e7Sbellard 2116faea38e7Sbellard if (!sn) { 2117faea38e7Sbellard snprintf(buf, buf_size, 2118faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2119faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 2120faea38e7Sbellard } else { 2121faea38e7Sbellard ti = sn->date_sec; 21223b9f94e1Sbellard #ifdef _WIN32 21233b9f94e1Sbellard ptm = localtime(&ti); 21243b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 21253b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 21263b9f94e1Sbellard #else 2127faea38e7Sbellard localtime_r(&ti, &tm); 2128faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 2129faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 21303b9f94e1Sbellard #endif 2131faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 2132faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 2133faea38e7Sbellard "%02d:%02d:%02d.%03d", 2134faea38e7Sbellard (int)(secs / 3600), 2135faea38e7Sbellard (int)((secs / 60) % 60), 2136faea38e7Sbellard (int)(secs % 60), 2137faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 2138faea38e7Sbellard snprintf(buf, buf_size, 2139faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 2140faea38e7Sbellard sn->id_str, sn->name, 2141faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 2142faea38e7Sbellard date_buf, 2143faea38e7Sbellard clock_buf); 2144faea38e7Sbellard } 2145faea38e7Sbellard return buf; 2146faea38e7Sbellard } 2147faea38e7Sbellard 214883f64091Sbellard 2149ea2384d3Sbellard /**************************************************************/ 215083f64091Sbellard /* async I/Os */ 2151ea2384d3Sbellard 21523b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 2153f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 215483f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 2155ea2384d3Sbellard { 215683f64091Sbellard BlockDriver *drv = bs->drv; 2157a36e69ddSths BlockDriverAIOCB *ret; 2158ea2384d3Sbellard 2159bbf0a440SStefan Hajnoczi trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque); 2160bbf0a440SStefan Hajnoczi 216119cb3738Sbellard if (!drv) 2162ce1a14dcSpbrook return NULL; 216371d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 216471d0770cSaliguori return NULL; 216583f64091Sbellard 2166f141eafeSaliguori ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, 2167f141eafeSaliguori cb, opaque); 2168a36e69ddSths 2169a36e69ddSths if (ret) { 2170a36e69ddSths /* Update stats even though technically transfer has not happened. */ 21716ea44308SJan Kiszka bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 2172a36e69ddSths bs->rd_ops ++; 2173a36e69ddSths } 2174a36e69ddSths 2175a36e69ddSths return ret; 217683f64091Sbellard } 217783f64091Sbellard 21784dcafbb1SMarcelo Tosatti typedef struct BlockCompleteData { 21794dcafbb1SMarcelo Tosatti BlockDriverCompletionFunc *cb; 21804dcafbb1SMarcelo Tosatti void *opaque; 21814dcafbb1SMarcelo Tosatti BlockDriverState *bs; 21824dcafbb1SMarcelo Tosatti int64_t sector_num; 21834dcafbb1SMarcelo Tosatti int nb_sectors; 21844dcafbb1SMarcelo Tosatti } BlockCompleteData; 21854dcafbb1SMarcelo Tosatti 21864dcafbb1SMarcelo Tosatti static void block_complete_cb(void *opaque, int ret) 21874dcafbb1SMarcelo Tosatti { 21884dcafbb1SMarcelo Tosatti BlockCompleteData *b = opaque; 21894dcafbb1SMarcelo Tosatti 21904dcafbb1SMarcelo Tosatti if (b->bs->dirty_bitmap) { 21914dcafbb1SMarcelo Tosatti set_dirty_bitmap(b->bs, b->sector_num, b->nb_sectors, 1); 21924dcafbb1SMarcelo Tosatti } 21934dcafbb1SMarcelo Tosatti b->cb(b->opaque, ret); 21944dcafbb1SMarcelo Tosatti qemu_free(b); 21954dcafbb1SMarcelo Tosatti } 21964dcafbb1SMarcelo Tosatti 21974dcafbb1SMarcelo Tosatti static BlockCompleteData *blk_dirty_cb_alloc(BlockDriverState *bs, 21984dcafbb1SMarcelo Tosatti int64_t sector_num, 21994dcafbb1SMarcelo Tosatti int nb_sectors, 22004dcafbb1SMarcelo Tosatti BlockDriverCompletionFunc *cb, 22014dcafbb1SMarcelo Tosatti void *opaque) 22024dcafbb1SMarcelo Tosatti { 22034dcafbb1SMarcelo Tosatti BlockCompleteData *blkdata = qemu_mallocz(sizeof(BlockCompleteData)); 22044dcafbb1SMarcelo Tosatti 22054dcafbb1SMarcelo Tosatti blkdata->bs = bs; 22064dcafbb1SMarcelo Tosatti blkdata->cb = cb; 22074dcafbb1SMarcelo Tosatti blkdata->opaque = opaque; 22084dcafbb1SMarcelo Tosatti blkdata->sector_num = sector_num; 22094dcafbb1SMarcelo Tosatti blkdata->nb_sectors = nb_sectors; 22104dcafbb1SMarcelo Tosatti 22114dcafbb1SMarcelo Tosatti return blkdata; 22124dcafbb1SMarcelo Tosatti } 22134dcafbb1SMarcelo Tosatti 2214f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 2215f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 221683f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 22177674e7bfSbellard { 221883f64091Sbellard BlockDriver *drv = bs->drv; 2219a36e69ddSths BlockDriverAIOCB *ret; 22204dcafbb1SMarcelo Tosatti BlockCompleteData *blk_cb_data; 222183f64091Sbellard 2222bbf0a440SStefan Hajnoczi trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque); 2223bbf0a440SStefan Hajnoczi 222419cb3738Sbellard if (!drv) 2225ce1a14dcSpbrook return NULL; 222683f64091Sbellard if (bs->read_only) 2227ce1a14dcSpbrook return NULL; 222871d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 222971d0770cSaliguori return NULL; 223083f64091Sbellard 2231c6d22830SJan Kiszka if (bs->dirty_bitmap) { 22324dcafbb1SMarcelo Tosatti blk_cb_data = blk_dirty_cb_alloc(bs, sector_num, nb_sectors, cb, 22334dcafbb1SMarcelo Tosatti opaque); 22344dcafbb1SMarcelo Tosatti cb = &block_complete_cb; 22354dcafbb1SMarcelo Tosatti opaque = blk_cb_data; 22367cd1e32aSlirans@il.ibm.com } 22377cd1e32aSlirans@il.ibm.com 2238f141eafeSaliguori ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, 2239f141eafeSaliguori cb, opaque); 2240a36e69ddSths 2241a36e69ddSths if (ret) { 2242a36e69ddSths /* Update stats even though technically transfer has not happened. */ 22436ea44308SJan Kiszka bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 2244a36e69ddSths bs->wr_ops ++; 2245294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2246294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2247294cc35fSKevin Wolf } 2248a36e69ddSths } 2249a36e69ddSths 2250a36e69ddSths return ret; 225183f64091Sbellard } 225283f64091Sbellard 225340b4f539SKevin Wolf 225440b4f539SKevin Wolf typedef struct MultiwriteCB { 225540b4f539SKevin Wolf int error; 225640b4f539SKevin Wolf int num_requests; 225740b4f539SKevin Wolf int num_callbacks; 225840b4f539SKevin Wolf struct { 225940b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 226040b4f539SKevin Wolf void *opaque; 226140b4f539SKevin Wolf QEMUIOVector *free_qiov; 226240b4f539SKevin Wolf void *free_buf; 226340b4f539SKevin Wolf } callbacks[]; 226440b4f539SKevin Wolf } MultiwriteCB; 226540b4f539SKevin Wolf 226640b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 226740b4f539SKevin Wolf { 226840b4f539SKevin Wolf int i; 226940b4f539SKevin Wolf 227040b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 227140b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 22721e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 22731e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 22741e1ea48dSStefan Hajnoczi } 227540b4f539SKevin Wolf qemu_free(mcb->callbacks[i].free_qiov); 2276f8a83245SHerve Poussineau qemu_vfree(mcb->callbacks[i].free_buf); 227740b4f539SKevin Wolf } 227840b4f539SKevin Wolf } 227940b4f539SKevin Wolf 228040b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 228140b4f539SKevin Wolf { 228240b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 228340b4f539SKevin Wolf 22846d519a5fSStefan Hajnoczi trace_multiwrite_cb(mcb, ret); 22856d519a5fSStefan Hajnoczi 2286cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 228740b4f539SKevin Wolf mcb->error = ret; 228840b4f539SKevin Wolf } 228940b4f539SKevin Wolf 229040b4f539SKevin Wolf mcb->num_requests--; 229140b4f539SKevin Wolf if (mcb->num_requests == 0) { 229240b4f539SKevin Wolf multiwrite_user_cb(mcb); 229340b4f539SKevin Wolf qemu_free(mcb); 229440b4f539SKevin Wolf } 229540b4f539SKevin Wolf } 229640b4f539SKevin Wolf 229740b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 229840b4f539SKevin Wolf { 229977be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 230077be4366SChristoph Hellwig 230177be4366SChristoph Hellwig /* 230277be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 230377be4366SChristoph Hellwig * here as that could overflow the return value. 230477be4366SChristoph Hellwig */ 230577be4366SChristoph Hellwig if (req1->sector > req2->sector) { 230677be4366SChristoph Hellwig return 1; 230777be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 230877be4366SChristoph Hellwig return -1; 230977be4366SChristoph Hellwig } else { 231077be4366SChristoph Hellwig return 0; 231177be4366SChristoph Hellwig } 231240b4f539SKevin Wolf } 231340b4f539SKevin Wolf 231440b4f539SKevin Wolf /* 231540b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 231640b4f539SKevin Wolf * requests that remain after merging. 231740b4f539SKevin Wolf */ 231840b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 231940b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 232040b4f539SKevin Wolf { 232140b4f539SKevin Wolf int i, outidx; 232240b4f539SKevin Wolf 232340b4f539SKevin Wolf // Sort requests by start sector 232440b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 232540b4f539SKevin Wolf 232640b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 232740b4f539SKevin Wolf // filling up gaps with zero sectors. 232840b4f539SKevin Wolf outidx = 0; 232940b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 233040b4f539SKevin Wolf int merge = 0; 233140b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 233240b4f539SKevin Wolf 233340b4f539SKevin Wolf // This handles the cases that are valid for all block drivers, namely 233440b4f539SKevin Wolf // exactly sequential writes and overlapping writes. 233540b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 233640b4f539SKevin Wolf merge = 1; 233740b4f539SKevin Wolf } 233840b4f539SKevin Wolf 233940b4f539SKevin Wolf // The block driver may decide that it makes sense to combine requests 234040b4f539SKevin Wolf // even if there is a gap of some sectors between them. In this case, 234140b4f539SKevin Wolf // the gap is filled with zeros (therefore only applicable for yet 234240b4f539SKevin Wolf // unused space in format like qcow2). 234340b4f539SKevin Wolf if (!merge && bs->drv->bdrv_merge_requests) { 234440b4f539SKevin Wolf merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); 234540b4f539SKevin Wolf } 234640b4f539SKevin Wolf 2347e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 2348e2a305fbSChristoph Hellwig merge = 0; 2349e2a305fbSChristoph Hellwig } 2350e2a305fbSChristoph Hellwig 235140b4f539SKevin Wolf if (merge) { 235240b4f539SKevin Wolf size_t size; 235340b4f539SKevin Wolf QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); 235440b4f539SKevin Wolf qemu_iovec_init(qiov, 235540b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 235640b4f539SKevin Wolf 235740b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 235840b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 235940b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 236040b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 236140b4f539SKevin Wolf 236240b4f539SKevin Wolf // We might need to add some zeros between the two requests 236340b4f539SKevin Wolf if (reqs[i].sector > oldreq_last) { 236440b4f539SKevin Wolf size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; 236540b4f539SKevin Wolf uint8_t *buf = qemu_blockalign(bs, zero_bytes); 236640b4f539SKevin Wolf memset(buf, 0, zero_bytes); 236740b4f539SKevin Wolf qemu_iovec_add(qiov, buf, zero_bytes); 236840b4f539SKevin Wolf mcb->callbacks[i].free_buf = buf; 236940b4f539SKevin Wolf } 237040b4f539SKevin Wolf 237140b4f539SKevin Wolf // Add the second request 237240b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 237340b4f539SKevin Wolf 2374cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 237540b4f539SKevin Wolf reqs[outidx].qiov = qiov; 237640b4f539SKevin Wolf 237740b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 237840b4f539SKevin Wolf } else { 237940b4f539SKevin Wolf outidx++; 238040b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 238140b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 238240b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 238340b4f539SKevin Wolf } 238440b4f539SKevin Wolf } 238540b4f539SKevin Wolf 238640b4f539SKevin Wolf return outidx + 1; 238740b4f539SKevin Wolf } 238840b4f539SKevin Wolf 238940b4f539SKevin Wolf /* 239040b4f539SKevin Wolf * Submit multiple AIO write requests at once. 239140b4f539SKevin Wolf * 239240b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 239340b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 239440b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 239540b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 239640b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 239740b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 239840b4f539SKevin Wolf * 239940b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 240040b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 240140b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 240240b4f539SKevin Wolf */ 240340b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 240440b4f539SKevin Wolf { 240540b4f539SKevin Wolf BlockDriverAIOCB *acb; 240640b4f539SKevin Wolf MultiwriteCB *mcb; 240740b4f539SKevin Wolf int i; 240840b4f539SKevin Wolf 2409301db7c2SRyan Harper /* don't submit writes if we don't have a medium */ 2410301db7c2SRyan Harper if (bs->drv == NULL) { 2411301db7c2SRyan Harper for (i = 0; i < num_reqs; i++) { 2412301db7c2SRyan Harper reqs[i].error = -ENOMEDIUM; 2413301db7c2SRyan Harper } 2414301db7c2SRyan Harper return -1; 2415301db7c2SRyan Harper } 2416301db7c2SRyan Harper 241740b4f539SKevin Wolf if (num_reqs == 0) { 241840b4f539SKevin Wolf return 0; 241940b4f539SKevin Wolf } 242040b4f539SKevin Wolf 242140b4f539SKevin Wolf // Create MultiwriteCB structure 242240b4f539SKevin Wolf mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 242340b4f539SKevin Wolf mcb->num_requests = 0; 242440b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 242540b4f539SKevin Wolf 242640b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 242740b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 242840b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 242940b4f539SKevin Wolf } 243040b4f539SKevin Wolf 243140b4f539SKevin Wolf // Check for mergable requests 243240b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 243340b4f539SKevin Wolf 24346d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs); 24356d519a5fSStefan Hajnoczi 2436453f9a16SKevin Wolf /* 2437453f9a16SKevin Wolf * Run the aio requests. As soon as one request can't be submitted 2438453f9a16SKevin Wolf * successfully, fail all requests that are not yet submitted (we must 2439453f9a16SKevin Wolf * return failure for all requests anyway) 2440453f9a16SKevin Wolf * 2441453f9a16SKevin Wolf * num_requests cannot be set to the right value immediately: If 2442453f9a16SKevin Wolf * bdrv_aio_writev fails for some request, num_requests would be too high 2443453f9a16SKevin Wolf * and therefore multiwrite_cb() would never recognize the multiwrite 2444453f9a16SKevin Wolf * request as completed. We also cannot use the loop variable i to set it 2445453f9a16SKevin Wolf * when the first request fails because the callback may already have been 2446453f9a16SKevin Wolf * called for previously submitted requests. Thus, num_requests must be 2447453f9a16SKevin Wolf * incremented for each request that is submitted. 2448453f9a16SKevin Wolf * 2449453f9a16SKevin Wolf * The problem that callbacks may be called early also means that we need 2450453f9a16SKevin Wolf * to take care that num_requests doesn't become 0 before all requests are 2451453f9a16SKevin Wolf * submitted - multiwrite_cb() would consider the multiwrite request 2452453f9a16SKevin Wolf * completed. A dummy request that is "completed" by a manual call to 2453453f9a16SKevin Wolf * multiwrite_cb() takes care of this. 2454453f9a16SKevin Wolf */ 2455453f9a16SKevin Wolf mcb->num_requests = 1; 2456453f9a16SKevin Wolf 24576d519a5fSStefan Hajnoczi // Run the aio requests 245840b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 2459453f9a16SKevin Wolf mcb->num_requests++; 246040b4f539SKevin Wolf acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 246140b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 246240b4f539SKevin Wolf 246340b4f539SKevin Wolf if (acb == NULL) { 246440b4f539SKevin Wolf // We can only fail the whole thing if no request has been 246540b4f539SKevin Wolf // submitted yet. Otherwise we'll wait for the submitted AIOs to 246640b4f539SKevin Wolf // complete and report the error in the callback. 2467453f9a16SKevin Wolf if (i == 0) { 24686d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite_earlyfail(mcb); 246940b4f539SKevin Wolf goto fail; 247040b4f539SKevin Wolf } else { 24716d519a5fSStefan Hajnoczi trace_bdrv_aio_multiwrite_latefail(mcb, i); 24727eb58a6cSKevin Wolf multiwrite_cb(mcb, -EIO); 247340b4f539SKevin Wolf break; 247440b4f539SKevin Wolf } 247540b4f539SKevin Wolf } 247640b4f539SKevin Wolf } 247740b4f539SKevin Wolf 2478453f9a16SKevin Wolf /* Complete the dummy request */ 2479453f9a16SKevin Wolf multiwrite_cb(mcb, 0); 2480453f9a16SKevin Wolf 248140b4f539SKevin Wolf return 0; 248240b4f539SKevin Wolf 248340b4f539SKevin Wolf fail: 2484453f9a16SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 2485453f9a16SKevin Wolf reqs[i].error = -EIO; 2486453f9a16SKevin Wolf } 2487af474591SBruce Rogers qemu_free(mcb); 248840b4f539SKevin Wolf return -1; 248940b4f539SKevin Wolf } 249040b4f539SKevin Wolf 2491b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 2492b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2493b2e12bc6SChristoph Hellwig { 2494b2e12bc6SChristoph Hellwig BlockDriver *drv = bs->drv; 2495b2e12bc6SChristoph Hellwig 2496a13aac04SStefan Hajnoczi trace_bdrv_aio_flush(bs, opaque); 2497a13aac04SStefan Hajnoczi 2498016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 2499016f5cf6SAlexander Graf return bdrv_aio_noop_em(bs, cb, opaque); 2500016f5cf6SAlexander Graf } 2501016f5cf6SAlexander Graf 2502b2e12bc6SChristoph Hellwig if (!drv) 2503b2e12bc6SChristoph Hellwig return NULL; 2504b2e12bc6SChristoph Hellwig return drv->bdrv_aio_flush(bs, cb, opaque); 2505b2e12bc6SChristoph Hellwig } 2506b2e12bc6SChristoph Hellwig 250783f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 250883f64091Sbellard { 25096bbff9a0Saliguori acb->pool->cancel(acb); 251083f64091Sbellard } 251183f64091Sbellard 251283f64091Sbellard 251383f64091Sbellard /**************************************************************/ 251483f64091Sbellard /* async block device emulation */ 251583f64091Sbellard 2516c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 2517c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 2518c16b5a2cSChristoph Hellwig QEMUBH *bh; 2519c16b5a2cSChristoph Hellwig int ret; 2520c16b5a2cSChristoph Hellwig /* vector translation state */ 2521c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 2522c16b5a2cSChristoph Hellwig uint8_t *bounce; 2523c16b5a2cSChristoph Hellwig int is_write; 2524c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 2525c16b5a2cSChristoph Hellwig 2526c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 2527c16b5a2cSChristoph Hellwig { 2528b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 2529b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 25306a7ad299SDor Laor qemu_bh_delete(acb->bh); 253136afc451SAvi Kivity acb->bh = NULL; 2532c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 2533c16b5a2cSChristoph Hellwig } 2534c16b5a2cSChristoph Hellwig 2535c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 2536c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 2537c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 2538c16b5a2cSChristoph Hellwig }; 2539c16b5a2cSChristoph Hellwig 254083f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 2541beac80cdSbellard { 2542ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 2543f141eafeSaliguori 2544f141eafeSaliguori if (!acb->is_write) 2545f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 2546ceb42de8Saliguori qemu_vfree(acb->bounce); 2547ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 25486a7ad299SDor Laor qemu_bh_delete(acb->bh); 254936afc451SAvi Kivity acb->bh = NULL; 2550ce1a14dcSpbrook qemu_aio_release(acb); 2551beac80cdSbellard } 2552beac80cdSbellard 2553f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 2554f141eafeSaliguori int64_t sector_num, 2555f141eafeSaliguori QEMUIOVector *qiov, 2556f141eafeSaliguori int nb_sectors, 2557f141eafeSaliguori BlockDriverCompletionFunc *cb, 2558f141eafeSaliguori void *opaque, 2559f141eafeSaliguori int is_write) 2560f141eafeSaliguori 2561ea2384d3Sbellard { 2562ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 256383f64091Sbellard 2564c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2565f141eafeSaliguori acb->is_write = is_write; 2566f141eafeSaliguori acb->qiov = qiov; 2567e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 2568f141eafeSaliguori 2569ce1a14dcSpbrook if (!acb->bh) 2570ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2571f141eafeSaliguori 2572f141eafeSaliguori if (is_write) { 2573f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 2574f141eafeSaliguori acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 2575f141eafeSaliguori } else { 2576f141eafeSaliguori acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 2577f141eafeSaliguori } 2578f141eafeSaliguori 2579ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 2580f141eafeSaliguori 2581ce1a14dcSpbrook return &acb->common; 25827a6cba61Spbrook } 25837a6cba61Spbrook 2584f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 2585f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2586ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 258783f64091Sbellard { 2588f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 258983f64091Sbellard } 259083f64091Sbellard 2591f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 2592f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2593f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 2594f141eafeSaliguori { 2595f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 2596f141eafeSaliguori } 2597f141eafeSaliguori 2598b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 2599b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2600b2e12bc6SChristoph Hellwig { 2601b2e12bc6SChristoph Hellwig BlockDriverAIOCBSync *acb; 2602b2e12bc6SChristoph Hellwig 2603b2e12bc6SChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2604b2e12bc6SChristoph Hellwig acb->is_write = 1; /* don't bounce in the completion hadler */ 2605b2e12bc6SChristoph Hellwig acb->qiov = NULL; 2606b2e12bc6SChristoph Hellwig acb->bounce = NULL; 2607b2e12bc6SChristoph Hellwig acb->ret = 0; 2608b2e12bc6SChristoph Hellwig 2609b2e12bc6SChristoph Hellwig if (!acb->bh) 2610b2e12bc6SChristoph Hellwig acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2611b2e12bc6SChristoph Hellwig 2612b2e12bc6SChristoph Hellwig bdrv_flush(bs); 2613b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 2614b2e12bc6SChristoph Hellwig return &acb->common; 2615b2e12bc6SChristoph Hellwig } 2616b2e12bc6SChristoph Hellwig 2617016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 2618016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 2619016f5cf6SAlexander Graf { 2620016f5cf6SAlexander Graf BlockDriverAIOCBSync *acb; 2621016f5cf6SAlexander Graf 2622016f5cf6SAlexander Graf acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2623016f5cf6SAlexander Graf acb->is_write = 1; /* don't bounce in the completion handler */ 2624016f5cf6SAlexander Graf acb->qiov = NULL; 2625016f5cf6SAlexander Graf acb->bounce = NULL; 2626016f5cf6SAlexander Graf acb->ret = 0; 2627016f5cf6SAlexander Graf 2628016f5cf6SAlexander Graf if (!acb->bh) { 2629016f5cf6SAlexander Graf acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2630016f5cf6SAlexander Graf } 2631016f5cf6SAlexander Graf 2632016f5cf6SAlexander Graf qemu_bh_schedule(acb->bh); 2633016f5cf6SAlexander Graf return &acb->common; 2634016f5cf6SAlexander Graf } 2635016f5cf6SAlexander Graf 263683f64091Sbellard /**************************************************************/ 263783f64091Sbellard /* sync block device emulation */ 263883f64091Sbellard 263983f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 264083f64091Sbellard { 264183f64091Sbellard *(int *)opaque = ret; 264283f64091Sbellard } 264383f64091Sbellard 264483f64091Sbellard #define NOT_DONE 0x7fffffff 264583f64091Sbellard 264683f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 264783f64091Sbellard uint8_t *buf, int nb_sectors) 264883f64091Sbellard { 2649ce1a14dcSpbrook int async_ret; 2650ce1a14dcSpbrook BlockDriverAIOCB *acb; 2651f141eafeSaliguori struct iovec iov; 2652f141eafeSaliguori QEMUIOVector qiov; 265383f64091Sbellard 265465d6b3d8SKevin Wolf async_context_push(); 265565d6b3d8SKevin Wolf 265683f64091Sbellard async_ret = NOT_DONE; 26573f4cb3d3Sblueswir1 iov.iov_base = (void *)buf; 2658eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2659f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2660f141eafeSaliguori acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, 266183f64091Sbellard bdrv_rw_em_cb, &async_ret); 266265d6b3d8SKevin Wolf if (acb == NULL) { 266365d6b3d8SKevin Wolf async_ret = -1; 266465d6b3d8SKevin Wolf goto fail; 266565d6b3d8SKevin Wolf } 2666baf35cb9Saliguori 266783f64091Sbellard while (async_ret == NOT_DONE) { 266883f64091Sbellard qemu_aio_wait(); 266983f64091Sbellard } 2670baf35cb9Saliguori 267165d6b3d8SKevin Wolf 267265d6b3d8SKevin Wolf fail: 267365d6b3d8SKevin Wolf async_context_pop(); 267483f64091Sbellard return async_ret; 267583f64091Sbellard } 267683f64091Sbellard 267783f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 267883f64091Sbellard const uint8_t *buf, int nb_sectors) 267983f64091Sbellard { 2680ce1a14dcSpbrook int async_ret; 2681ce1a14dcSpbrook BlockDriverAIOCB *acb; 2682f141eafeSaliguori struct iovec iov; 2683f141eafeSaliguori QEMUIOVector qiov; 268483f64091Sbellard 268565d6b3d8SKevin Wolf async_context_push(); 268665d6b3d8SKevin Wolf 268783f64091Sbellard async_ret = NOT_DONE; 2688f141eafeSaliguori iov.iov_base = (void *)buf; 2689eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2690f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2691f141eafeSaliguori acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, 269283f64091Sbellard bdrv_rw_em_cb, &async_ret); 269365d6b3d8SKevin Wolf if (acb == NULL) { 269465d6b3d8SKevin Wolf async_ret = -1; 269565d6b3d8SKevin Wolf goto fail; 269665d6b3d8SKevin Wolf } 269783f64091Sbellard while (async_ret == NOT_DONE) { 269883f64091Sbellard qemu_aio_wait(); 269983f64091Sbellard } 270065d6b3d8SKevin Wolf 270165d6b3d8SKevin Wolf fail: 270265d6b3d8SKevin Wolf async_context_pop(); 270383f64091Sbellard return async_ret; 270483f64091Sbellard } 2705ea2384d3Sbellard 2706ea2384d3Sbellard void bdrv_init(void) 2707ea2384d3Sbellard { 27085efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 2709ea2384d3Sbellard } 2710ce1a14dcSpbrook 2711eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 2712eb852011SMarkus Armbruster { 2713eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 2714eb852011SMarkus Armbruster bdrv_init(); 2715eb852011SMarkus Armbruster } 2716eb852011SMarkus Armbruster 2717c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 27186bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 27196bbff9a0Saliguori { 2720ce1a14dcSpbrook BlockDriverAIOCB *acb; 2721ce1a14dcSpbrook 27226bbff9a0Saliguori if (pool->free_aiocb) { 27236bbff9a0Saliguori acb = pool->free_aiocb; 27246bbff9a0Saliguori pool->free_aiocb = acb->next; 2725ce1a14dcSpbrook } else { 27266bbff9a0Saliguori acb = qemu_mallocz(pool->aiocb_size); 27276bbff9a0Saliguori acb->pool = pool; 2728ce1a14dcSpbrook } 2729ce1a14dcSpbrook acb->bs = bs; 2730ce1a14dcSpbrook acb->cb = cb; 2731ce1a14dcSpbrook acb->opaque = opaque; 2732ce1a14dcSpbrook return acb; 2733ce1a14dcSpbrook } 2734ce1a14dcSpbrook 2735ce1a14dcSpbrook void qemu_aio_release(void *p) 2736ce1a14dcSpbrook { 27376bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 27386bbff9a0Saliguori AIOPool *pool = acb->pool; 27396bbff9a0Saliguori acb->next = pool->free_aiocb; 27406bbff9a0Saliguori pool->free_aiocb = acb; 2741ce1a14dcSpbrook } 274219cb3738Sbellard 274319cb3738Sbellard /**************************************************************/ 274419cb3738Sbellard /* removable device support */ 274519cb3738Sbellard 274619cb3738Sbellard /** 274719cb3738Sbellard * Return TRUE if the media is present 274819cb3738Sbellard */ 274919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 275019cb3738Sbellard { 275119cb3738Sbellard BlockDriver *drv = bs->drv; 275219cb3738Sbellard int ret; 275319cb3738Sbellard if (!drv) 275419cb3738Sbellard return 0; 275519cb3738Sbellard if (!drv->bdrv_is_inserted) 27564be9762aSMarkus Armbruster return !bs->tray_open; 275719cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 275819cb3738Sbellard return ret; 275919cb3738Sbellard } 276019cb3738Sbellard 276119cb3738Sbellard /** 276219cb3738Sbellard * Return TRUE if the media changed since the last call to this 276319cb3738Sbellard * function. It is currently only used for floppy disks 276419cb3738Sbellard */ 276519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 276619cb3738Sbellard { 276719cb3738Sbellard BlockDriver *drv = bs->drv; 276819cb3738Sbellard int ret; 276919cb3738Sbellard 277019cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 277119cb3738Sbellard ret = -ENOTSUP; 277219cb3738Sbellard else 277319cb3738Sbellard ret = drv->bdrv_media_changed(bs); 277419cb3738Sbellard if (ret == -ENOTSUP) 277519cb3738Sbellard ret = bs->media_changed; 277619cb3738Sbellard bs->media_changed = 0; 277719cb3738Sbellard return ret; 277819cb3738Sbellard } 277919cb3738Sbellard 278019cb3738Sbellard /** 278119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 278219cb3738Sbellard */ 2783aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag) 278419cb3738Sbellard { 278519cb3738Sbellard BlockDriver *drv = bs->drv; 278619cb3738Sbellard int ret; 278719cb3738Sbellard 2788aea2a33cSMark McLoughlin if (bs->locked) { 2789aea2a33cSMark McLoughlin return -EBUSY; 2790aea2a33cSMark McLoughlin } 2791aea2a33cSMark McLoughlin 279219cb3738Sbellard if (!drv || !drv->bdrv_eject) { 279319cb3738Sbellard ret = -ENOTSUP; 279419cb3738Sbellard } else { 279519cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 279619cb3738Sbellard } 279719cb3738Sbellard if (ret == -ENOTSUP) { 2798aea2a33cSMark McLoughlin ret = 0; 279919cb3738Sbellard } 28004be9762aSMarkus Armbruster if (ret >= 0) { 28014be9762aSMarkus Armbruster bs->tray_open = eject_flag; 28024be9762aSMarkus Armbruster } 2803aea2a33cSMark McLoughlin 2804aea2a33cSMark McLoughlin return ret; 280519cb3738Sbellard } 280619cb3738Sbellard 280719cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 280819cb3738Sbellard { 280919cb3738Sbellard return bs->locked; 281019cb3738Sbellard } 281119cb3738Sbellard 281219cb3738Sbellard /** 281319cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 281419cb3738Sbellard * to eject it manually). 281519cb3738Sbellard */ 281619cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 281719cb3738Sbellard { 281819cb3738Sbellard BlockDriver *drv = bs->drv; 281919cb3738Sbellard 2820*b8c6d095SStefan Hajnoczi trace_bdrv_set_locked(bs, locked); 2821*b8c6d095SStefan Hajnoczi 282219cb3738Sbellard bs->locked = locked; 282319cb3738Sbellard if (drv && drv->bdrv_set_locked) { 282419cb3738Sbellard drv->bdrv_set_locked(bs, locked); 282519cb3738Sbellard } 282619cb3738Sbellard } 2827985a03b0Sths 2828985a03b0Sths /* needed for generic scsi interface */ 2829985a03b0Sths 2830985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 2831985a03b0Sths { 2832985a03b0Sths BlockDriver *drv = bs->drv; 2833985a03b0Sths 2834985a03b0Sths if (drv && drv->bdrv_ioctl) 2835985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 2836985a03b0Sths return -ENOTSUP; 2837985a03b0Sths } 28387d780669Saliguori 2839221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 2840221f715dSaliguori unsigned long int req, void *buf, 28417d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 28427d780669Saliguori { 2843221f715dSaliguori BlockDriver *drv = bs->drv; 28447d780669Saliguori 2845221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 2846221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 2847221f715dSaliguori return NULL; 28487d780669Saliguori } 2849e268ca52Saliguori 28507cd1e32aSlirans@il.ibm.com 28517cd1e32aSlirans@il.ibm.com 2852e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 2853e268ca52Saliguori { 2854e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 2855e268ca52Saliguori } 28567cd1e32aSlirans@il.ibm.com 28577cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 28587cd1e32aSlirans@il.ibm.com { 28597cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 2860a55eb92cSJan Kiszka 2861aaa0eb75SLiran Schour bs->dirty_count = 0; 28627cd1e32aSlirans@il.ibm.com if (enable) { 2863c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 2864c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 2865c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 2866c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 28677cd1e32aSlirans@il.ibm.com 28687cd1e32aSlirans@il.ibm.com bs->dirty_bitmap = qemu_mallocz(bitmap_size); 28697cd1e32aSlirans@il.ibm.com } 28707cd1e32aSlirans@il.ibm.com } else { 2871c6d22830SJan Kiszka if (bs->dirty_bitmap) { 28727cd1e32aSlirans@il.ibm.com qemu_free(bs->dirty_bitmap); 2873c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 28747cd1e32aSlirans@il.ibm.com } 28757cd1e32aSlirans@il.ibm.com } 28767cd1e32aSlirans@il.ibm.com } 28777cd1e32aSlirans@il.ibm.com 28787cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 28797cd1e32aSlirans@il.ibm.com { 28806ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 28817cd1e32aSlirans@il.ibm.com 2882c6d22830SJan Kiszka if (bs->dirty_bitmap && 2883c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 28846d59fec1SMarcelo Tosatti return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 28856d59fec1SMarcelo Tosatti (1UL << (chunk % (sizeof(unsigned long) * 8)))); 28867cd1e32aSlirans@il.ibm.com } else { 28877cd1e32aSlirans@il.ibm.com return 0; 28887cd1e32aSlirans@il.ibm.com } 28897cd1e32aSlirans@il.ibm.com } 28907cd1e32aSlirans@il.ibm.com 28917cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 28927cd1e32aSlirans@il.ibm.com int nr_sectors) 28937cd1e32aSlirans@il.ibm.com { 28947cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 28957cd1e32aSlirans@il.ibm.com } 2896aaa0eb75SLiran Schour 2897aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 2898aaa0eb75SLiran Schour { 2899aaa0eb75SLiran Schour return bs->dirty_count; 2900aaa0eb75SLiran Schour } 2901f88e1a42SJes Sorensen 2902db593f25SMarcelo Tosatti void bdrv_set_in_use(BlockDriverState *bs, int in_use) 2903db593f25SMarcelo Tosatti { 2904db593f25SMarcelo Tosatti assert(bs->in_use != in_use); 2905db593f25SMarcelo Tosatti bs->in_use = in_use; 2906db593f25SMarcelo Tosatti } 2907db593f25SMarcelo Tosatti 2908db593f25SMarcelo Tosatti int bdrv_in_use(BlockDriverState *bs) 2909db593f25SMarcelo Tosatti { 2910db593f25SMarcelo Tosatti return bs->in_use; 2911db593f25SMarcelo Tosatti } 2912db593f25SMarcelo Tosatti 2913f88e1a42SJes Sorensen int bdrv_img_create(const char *filename, const char *fmt, 2914f88e1a42SJes Sorensen const char *base_filename, const char *base_fmt, 2915f88e1a42SJes Sorensen char *options, uint64_t img_size, int flags) 2916f88e1a42SJes Sorensen { 2917f88e1a42SJes Sorensen QEMUOptionParameter *param = NULL, *create_options = NULL; 2918792da93aSJes Sorensen QEMUOptionParameter *backing_fmt, *backing_file; 2919f88e1a42SJes Sorensen BlockDriverState *bs = NULL; 2920f88e1a42SJes Sorensen BlockDriver *drv, *proto_drv; 292196df67d1SStefan Hajnoczi BlockDriver *backing_drv = NULL; 2922f88e1a42SJes Sorensen int ret = 0; 2923f88e1a42SJes Sorensen 2924f88e1a42SJes Sorensen /* Find driver and parse its options */ 2925f88e1a42SJes Sorensen drv = bdrv_find_format(fmt); 2926f88e1a42SJes Sorensen if (!drv) { 2927f88e1a42SJes Sorensen error_report("Unknown file format '%s'", fmt); 29284f70f249SJes Sorensen ret = -EINVAL; 2929f88e1a42SJes Sorensen goto out; 2930f88e1a42SJes Sorensen } 2931f88e1a42SJes Sorensen 2932f88e1a42SJes Sorensen proto_drv = bdrv_find_protocol(filename); 2933f88e1a42SJes Sorensen if (!proto_drv) { 2934f88e1a42SJes Sorensen error_report("Unknown protocol '%s'", filename); 29354f70f249SJes Sorensen ret = -EINVAL; 2936f88e1a42SJes Sorensen goto out; 2937f88e1a42SJes Sorensen } 2938f88e1a42SJes Sorensen 2939f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 2940f88e1a42SJes Sorensen drv->create_options); 2941f88e1a42SJes Sorensen create_options = append_option_parameters(create_options, 2942f88e1a42SJes Sorensen proto_drv->create_options); 2943f88e1a42SJes Sorensen 2944f88e1a42SJes Sorensen /* Create parameter list with default values */ 2945f88e1a42SJes Sorensen param = parse_option_parameters("", create_options, param); 2946f88e1a42SJes Sorensen 2947f88e1a42SJes Sorensen set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size); 2948f88e1a42SJes Sorensen 2949f88e1a42SJes Sorensen /* Parse -o options */ 2950f88e1a42SJes Sorensen if (options) { 2951f88e1a42SJes Sorensen param = parse_option_parameters(options, create_options, param); 2952f88e1a42SJes Sorensen if (param == NULL) { 2953f88e1a42SJes Sorensen error_report("Invalid options for file format '%s'.", fmt); 29544f70f249SJes Sorensen ret = -EINVAL; 2955f88e1a42SJes Sorensen goto out; 2956f88e1a42SJes Sorensen } 2957f88e1a42SJes Sorensen } 2958f88e1a42SJes Sorensen 2959f88e1a42SJes Sorensen if (base_filename) { 2960f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE, 2961f88e1a42SJes Sorensen base_filename)) { 2962f88e1a42SJes Sorensen error_report("Backing file not supported for file format '%s'", 2963f88e1a42SJes Sorensen fmt); 29644f70f249SJes Sorensen ret = -EINVAL; 2965f88e1a42SJes Sorensen goto out; 2966f88e1a42SJes Sorensen } 2967f88e1a42SJes Sorensen } 2968f88e1a42SJes Sorensen 2969f88e1a42SJes Sorensen if (base_fmt) { 2970f88e1a42SJes Sorensen if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) { 2971f88e1a42SJes Sorensen error_report("Backing file format not supported for file " 2972f88e1a42SJes Sorensen "format '%s'", fmt); 29734f70f249SJes Sorensen ret = -EINVAL; 2974f88e1a42SJes Sorensen goto out; 2975f88e1a42SJes Sorensen } 2976f88e1a42SJes Sorensen } 2977f88e1a42SJes Sorensen 2978792da93aSJes Sorensen backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); 2979792da93aSJes Sorensen if (backing_file && backing_file->value.s) { 2980792da93aSJes Sorensen if (!strcmp(filename, backing_file->value.s)) { 2981792da93aSJes Sorensen error_report("Error: Trying to create an image with the " 2982792da93aSJes Sorensen "same filename as the backing file"); 29834f70f249SJes Sorensen ret = -EINVAL; 2984792da93aSJes Sorensen goto out; 2985792da93aSJes Sorensen } 2986792da93aSJes Sorensen } 2987792da93aSJes Sorensen 2988f88e1a42SJes Sorensen backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT); 2989f88e1a42SJes Sorensen if (backing_fmt && backing_fmt->value.s) { 299096df67d1SStefan Hajnoczi backing_drv = bdrv_find_format(backing_fmt->value.s); 299196df67d1SStefan Hajnoczi if (!backing_drv) { 2992f88e1a42SJes Sorensen error_report("Unknown backing file format '%s'", 2993f88e1a42SJes Sorensen backing_fmt->value.s); 29944f70f249SJes Sorensen ret = -EINVAL; 2995f88e1a42SJes Sorensen goto out; 2996f88e1a42SJes Sorensen } 2997f88e1a42SJes Sorensen } 2998f88e1a42SJes Sorensen 2999f88e1a42SJes Sorensen // The size for the image must always be specified, with one exception: 3000f88e1a42SJes Sorensen // If we are using a backing file, we can obtain the size from there 3001f88e1a42SJes Sorensen if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) { 3002f88e1a42SJes Sorensen if (backing_file && backing_file->value.s) { 3003f88e1a42SJes Sorensen uint64_t size; 3004f88e1a42SJes Sorensen char buf[32]; 3005f88e1a42SJes Sorensen 3006f88e1a42SJes Sorensen bs = bdrv_new(""); 3007f88e1a42SJes Sorensen 300896df67d1SStefan Hajnoczi ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv); 3009f88e1a42SJes Sorensen if (ret < 0) { 301096df67d1SStefan Hajnoczi error_report("Could not open '%s'", backing_file->value.s); 3011f88e1a42SJes Sorensen goto out; 3012f88e1a42SJes Sorensen } 3013f88e1a42SJes Sorensen bdrv_get_geometry(bs, &size); 3014f88e1a42SJes Sorensen size *= 512; 3015f88e1a42SJes Sorensen 3016f88e1a42SJes Sorensen snprintf(buf, sizeof(buf), "%" PRId64, size); 3017f88e1a42SJes Sorensen set_option_parameter(param, BLOCK_OPT_SIZE, buf); 3018f88e1a42SJes Sorensen } else { 3019f88e1a42SJes Sorensen error_report("Image creation needs a size parameter"); 30204f70f249SJes Sorensen ret = -EINVAL; 3021f88e1a42SJes Sorensen goto out; 3022f88e1a42SJes Sorensen } 3023f88e1a42SJes Sorensen } 3024f88e1a42SJes Sorensen 3025f88e1a42SJes Sorensen printf("Formatting '%s', fmt=%s ", filename, fmt); 3026f88e1a42SJes Sorensen print_option_parameters(param); 3027f88e1a42SJes Sorensen puts(""); 3028f88e1a42SJes Sorensen 3029f88e1a42SJes Sorensen ret = bdrv_create(drv, filename, param); 3030f88e1a42SJes Sorensen 3031f88e1a42SJes Sorensen if (ret < 0) { 3032f88e1a42SJes Sorensen if (ret == -ENOTSUP) { 3033f88e1a42SJes Sorensen error_report("Formatting or formatting option not supported for " 3034f88e1a42SJes Sorensen "file format '%s'", fmt); 3035f88e1a42SJes Sorensen } else if (ret == -EFBIG) { 3036f88e1a42SJes Sorensen error_report("The image size is too large for file format '%s'", 3037f88e1a42SJes Sorensen fmt); 3038f88e1a42SJes Sorensen } else { 3039f88e1a42SJes Sorensen error_report("%s: error while creating %s: %s", filename, fmt, 3040f88e1a42SJes Sorensen strerror(-ret)); 3041f88e1a42SJes Sorensen } 3042f88e1a42SJes Sorensen } 3043f88e1a42SJes Sorensen 3044f88e1a42SJes Sorensen out: 3045f88e1a42SJes Sorensen free_option_parameters(create_options); 3046f88e1a42SJes Sorensen free_option_parameters(param); 3047f88e1a42SJes Sorensen 3048f88e1a42SJes Sorensen if (bs) { 3049f88e1a42SJes Sorensen bdrv_delete(bs); 3050f88e1a42SJes Sorensen } 30514f70f249SJes Sorensen 30524f70f249SJes Sorensen return ret; 3053f88e1a42SJes Sorensen } 3054