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" 26376253ecSaliguori #include "monitor.h" 27ea2384d3Sbellard #include "block_int.h" 285efa9d5aSAnthony Liguori #include "module.h" 29d15e5465SLuiz Capitulino #include "qemu-objects.h" 30fc01f7e7Sbellard 3171e72a19SJuan Quintela #ifdef CONFIG_BSD 327674e7bfSbellard #include <sys/types.h> 337674e7bfSbellard #include <sys/stat.h> 347674e7bfSbellard #include <sys/ioctl.h> 3572cf2d4fSBlue Swirl #include <sys/queue.h> 36c5e97233Sblueswir1 #ifndef __DragonFly__ 377674e7bfSbellard #include <sys/disk.h> 387674e7bfSbellard #endif 39c5e97233Sblueswir1 #endif 407674e7bfSbellard 4149dc768dSaliguori #ifdef _WIN32 4249dc768dSaliguori #include <windows.h> 4349dc768dSaliguori #endif 4449dc768dSaliguori 45f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 46f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 47c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 48f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 49f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 50ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 51b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 52b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque); 53016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 54016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque); 5583f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 5683f64091Sbellard uint8_t *buf, int nb_sectors); 5783f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 5883f64091Sbellard const uint8_t *buf, int nb_sectors); 59ec530c81Sbellard 601b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 611b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 627ee930d0Sblueswir1 638a22f02aSStefan Hajnoczi static QLIST_HEAD(, BlockDriver) bdrv_drivers = 648a22f02aSStefan Hajnoczi QLIST_HEAD_INITIALIZER(bdrv_drivers); 65ea2384d3Sbellard 66f9092b10SMarkus Armbruster /* The device to use for VM snapshots */ 67f9092b10SMarkus Armbruster static BlockDriverState *bs_snapshots; 68f9092b10SMarkus Armbruster 69eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 70eb852011SMarkus Armbruster static int use_bdrv_whitelist; 71eb852011SMarkus Armbruster 7283f64091Sbellard int path_is_absolute(const char *path) 7383f64091Sbellard { 7483f64091Sbellard const char *p; 7521664424Sbellard #ifdef _WIN32 7621664424Sbellard /* specific case for names like: "\\.\d:" */ 7721664424Sbellard if (*path == '/' || *path == '\\') 7821664424Sbellard return 1; 7921664424Sbellard #endif 8083f64091Sbellard p = strchr(path, ':'); 8183f64091Sbellard if (p) 8283f64091Sbellard p++; 8383f64091Sbellard else 8483f64091Sbellard p = path; 853b9f94e1Sbellard #ifdef _WIN32 863b9f94e1Sbellard return (*p == '/' || *p == '\\'); 873b9f94e1Sbellard #else 883b9f94e1Sbellard return (*p == '/'); 893b9f94e1Sbellard #endif 9083f64091Sbellard } 9183f64091Sbellard 9283f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 9383f64091Sbellard path to it by considering it is relative to base_path. URL are 9483f64091Sbellard supported. */ 9583f64091Sbellard void path_combine(char *dest, int dest_size, 9683f64091Sbellard const char *base_path, 9783f64091Sbellard const char *filename) 9883f64091Sbellard { 9983f64091Sbellard const char *p, *p1; 10083f64091Sbellard int len; 10183f64091Sbellard 10283f64091Sbellard if (dest_size <= 0) 10383f64091Sbellard return; 10483f64091Sbellard if (path_is_absolute(filename)) { 10583f64091Sbellard pstrcpy(dest, dest_size, filename); 10683f64091Sbellard } else { 10783f64091Sbellard p = strchr(base_path, ':'); 10883f64091Sbellard if (p) 10983f64091Sbellard p++; 11083f64091Sbellard else 11183f64091Sbellard p = base_path; 1123b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1133b9f94e1Sbellard #ifdef _WIN32 1143b9f94e1Sbellard { 1153b9f94e1Sbellard const char *p2; 1163b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1173b9f94e1Sbellard if (!p1 || p2 > p1) 1183b9f94e1Sbellard p1 = p2; 1193b9f94e1Sbellard } 1203b9f94e1Sbellard #endif 12183f64091Sbellard if (p1) 12283f64091Sbellard p1++; 12383f64091Sbellard else 12483f64091Sbellard p1 = base_path; 12583f64091Sbellard if (p1 > p) 12683f64091Sbellard p = p1; 12783f64091Sbellard len = p - base_path; 12883f64091Sbellard if (len > dest_size - 1) 12983f64091Sbellard len = dest_size - 1; 13083f64091Sbellard memcpy(dest, base_path, len); 13183f64091Sbellard dest[len] = '\0'; 13283f64091Sbellard pstrcat(dest, dest_size, filename); 13383f64091Sbellard } 13483f64091Sbellard } 13583f64091Sbellard 1365efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 137ea2384d3Sbellard { 138f141eafeSaliguori if (!bdrv->bdrv_aio_readv) { 13983f64091Sbellard /* add AIO emulation layer */ 140f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 141f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 142eda578e5Saliguori } else if (!bdrv->bdrv_read) { 14383f64091Sbellard /* add synchronous IO emulation layer */ 14483f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 14583f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 14683f64091Sbellard } 147b2e12bc6SChristoph Hellwig 148b2e12bc6SChristoph Hellwig if (!bdrv->bdrv_aio_flush) 149b2e12bc6SChristoph Hellwig bdrv->bdrv_aio_flush = bdrv_aio_flush_em; 150b2e12bc6SChristoph Hellwig 1518a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 152ea2384d3Sbellard } 153b338082bSbellard 154b338082bSbellard /* create a new block device (by default it is empty) */ 155b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 156fc01f7e7Sbellard { 1571b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 158b338082bSbellard 159b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 160b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 161ea2384d3Sbellard if (device_name[0] != '\0') { 1621b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 163ea2384d3Sbellard } 164b338082bSbellard return bs; 165b338082bSbellard } 166b338082bSbellard 167ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 168ea2384d3Sbellard { 169ea2384d3Sbellard BlockDriver *drv1; 1708a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 1718a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 172ea2384d3Sbellard return drv1; 173ea2384d3Sbellard } 1748a22f02aSStefan Hajnoczi } 175ea2384d3Sbellard return NULL; 176ea2384d3Sbellard } 177ea2384d3Sbellard 178eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 179eb852011SMarkus Armbruster { 180eb852011SMarkus Armbruster static const char *whitelist[] = { 181eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 182eb852011SMarkus Armbruster }; 183eb852011SMarkus Armbruster const char **p; 184eb852011SMarkus Armbruster 185eb852011SMarkus Armbruster if (!whitelist[0]) 186eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 187eb852011SMarkus Armbruster 188eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 189eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 190eb852011SMarkus Armbruster return 1; 191eb852011SMarkus Armbruster } 192eb852011SMarkus Armbruster } 193eb852011SMarkus Armbruster return 0; 194eb852011SMarkus Armbruster } 195eb852011SMarkus Armbruster 196eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 197eb852011SMarkus Armbruster { 198eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 199eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 200eb852011SMarkus Armbruster } 201eb852011SMarkus Armbruster 2020e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 2030e7e1989SKevin Wolf QEMUOptionParameter *options) 204ea2384d3Sbellard { 205ea2384d3Sbellard if (!drv->bdrv_create) 206ea2384d3Sbellard return -ENOTSUP; 2070e7e1989SKevin Wolf 2080e7e1989SKevin Wolf return drv->bdrv_create(filename, options); 209ea2384d3Sbellard } 210ea2384d3Sbellard 21184a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 21284a12e66SChristoph Hellwig { 21384a12e66SChristoph Hellwig BlockDriver *drv; 21484a12e66SChristoph Hellwig 215b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 21684a12e66SChristoph Hellwig if (drv == NULL) { 21784a12e66SChristoph Hellwig drv = bdrv_find_format("file"); 21884a12e66SChristoph Hellwig } 21984a12e66SChristoph Hellwig 22084a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 22184a12e66SChristoph Hellwig } 22284a12e66SChristoph Hellwig 223d5249393Sbellard #ifdef _WIN32 22495389c86Sbellard void get_tmp_filename(char *filename, int size) 225d5249393Sbellard { 2263b9f94e1Sbellard char temp_dir[MAX_PATH]; 2273b9f94e1Sbellard 2283b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 2293b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 230d5249393Sbellard } 231d5249393Sbellard #else 23295389c86Sbellard void get_tmp_filename(char *filename, int size) 233ea2384d3Sbellard { 234ea2384d3Sbellard int fd; 2357ccfb2ebSblueswir1 const char *tmpdir; 236d5249393Sbellard /* XXX: race condition possible */ 2370badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 2380badc1eeSaurel32 if (!tmpdir) 2390badc1eeSaurel32 tmpdir = "/tmp"; 2400badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 241ea2384d3Sbellard fd = mkstemp(filename); 242ea2384d3Sbellard close(fd); 243ea2384d3Sbellard } 244d5249393Sbellard #endif 245ea2384d3Sbellard 24619cb3738Sbellard #ifdef _WIN32 247f45512feSbellard static int is_windows_drive_prefix(const char *filename) 248f45512feSbellard { 249f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 250f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 251f45512feSbellard filename[1] == ':'); 252f45512feSbellard } 253f45512feSbellard 254508c7cb3SChristoph Hellwig int is_windows_drive(const char *filename) 25519cb3738Sbellard { 256f45512feSbellard if (is_windows_drive_prefix(filename) && 257f45512feSbellard filename[2] == '\0') 25819cb3738Sbellard return 1; 25919cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 26019cb3738Sbellard strstart(filename, "//./", NULL)) 26119cb3738Sbellard return 1; 26219cb3738Sbellard return 0; 26319cb3738Sbellard } 26419cb3738Sbellard #endif 26519cb3738Sbellard 266f3a5d3f8SChristoph Hellwig /* 267f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 268f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 269f3a5d3f8SChristoph Hellwig */ 270f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 271f3a5d3f8SChristoph Hellwig { 272508c7cb3SChristoph Hellwig int score_max = 0, score; 273508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 274f3a5d3f8SChristoph Hellwig 2758a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 276508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 277508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 278508c7cb3SChristoph Hellwig if (score > score_max) { 279508c7cb3SChristoph Hellwig score_max = score; 280508c7cb3SChristoph Hellwig drv = d; 281f3a5d3f8SChristoph Hellwig } 282508c7cb3SChristoph Hellwig } 283f3a5d3f8SChristoph Hellwig } 284f3a5d3f8SChristoph Hellwig 285508c7cb3SChristoph Hellwig return drv; 286f3a5d3f8SChristoph Hellwig } 287f3a5d3f8SChristoph Hellwig 288b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 28984a12e66SChristoph Hellwig { 29084a12e66SChristoph Hellwig BlockDriver *drv1; 29184a12e66SChristoph Hellwig char protocol[128]; 29284a12e66SChristoph Hellwig int len; 29384a12e66SChristoph Hellwig const char *p; 29484a12e66SChristoph Hellwig 29566f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 29666f82ceeSKevin Wolf 29739508e7aSChristoph Hellwig /* 29839508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 29939508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 30039508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 30139508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 30239508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 30339508e7aSChristoph Hellwig */ 30484a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 30539508e7aSChristoph Hellwig if (drv1) { 30684a12e66SChristoph Hellwig return drv1; 30784a12e66SChristoph Hellwig } 30839508e7aSChristoph Hellwig 30939508e7aSChristoph Hellwig #ifdef _WIN32 31039508e7aSChristoph Hellwig if (is_windows_drive(filename) || 31139508e7aSChristoph Hellwig is_windows_drive_prefix(filename)) 31239508e7aSChristoph Hellwig return bdrv_find_format("file"); 31339508e7aSChristoph Hellwig #endif 31439508e7aSChristoph Hellwig 31539508e7aSChristoph Hellwig p = strchr(filename, ':'); 31639508e7aSChristoph Hellwig if (!p) { 31739508e7aSChristoph Hellwig return bdrv_find_format("file"); 31839508e7aSChristoph Hellwig } 31984a12e66SChristoph Hellwig len = p - filename; 32084a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 32184a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 32284a12e66SChristoph Hellwig memcpy(protocol, filename, len); 32384a12e66SChristoph Hellwig protocol[len] = '\0'; 32484a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 32584a12e66SChristoph Hellwig if (drv1->protocol_name && 32684a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 32784a12e66SChristoph Hellwig return drv1; 32884a12e66SChristoph Hellwig } 32984a12e66SChristoph Hellwig } 33084a12e66SChristoph Hellwig return NULL; 33184a12e66SChristoph Hellwig } 33284a12e66SChristoph Hellwig 333ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 334ea2384d3Sbellard { 33583f64091Sbellard int ret, score, score_max; 336ea2384d3Sbellard BlockDriver *drv1, *drv; 33783f64091Sbellard uint8_t buf[2048]; 33883f64091Sbellard BlockDriverState *bs; 339ea2384d3Sbellard 340f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 34183f64091Sbellard if (ret < 0) 3427674e7bfSbellard return NULL; 343f8ea0b00SNicholas Bellinger 34408a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 34508a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 3461a396859SNicholas A. Bellinger bdrv_delete(bs); 347f8ea0b00SNicholas Bellinger return bdrv_find_format("raw"); 3481a396859SNicholas A. Bellinger } 349f8ea0b00SNicholas Bellinger 35083f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 35183f64091Sbellard bdrv_delete(bs); 352ea2384d3Sbellard if (ret < 0) { 353ea2384d3Sbellard return NULL; 354ea2384d3Sbellard } 355ea2384d3Sbellard 356ea2384d3Sbellard score_max = 0; 35784a12e66SChristoph Hellwig drv = NULL; 3588a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 35983f64091Sbellard if (drv1->bdrv_probe) { 360ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 361ea2384d3Sbellard if (score > score_max) { 362ea2384d3Sbellard score_max = score; 363ea2384d3Sbellard drv = drv1; 364ea2384d3Sbellard } 365ea2384d3Sbellard } 36683f64091Sbellard } 367ea2384d3Sbellard return drv; 368ea2384d3Sbellard } 369ea2384d3Sbellard 37051762288SStefan Hajnoczi /** 37151762288SStefan Hajnoczi * Set the current 'total_sectors' value 37251762288SStefan Hajnoczi */ 37351762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 37451762288SStefan Hajnoczi { 37551762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 37651762288SStefan Hajnoczi 377396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 378396759adSNicholas Bellinger if (bs->sg) 379396759adSNicholas Bellinger return 0; 380396759adSNicholas Bellinger 38151762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 38251762288SStefan Hajnoczi if (drv->bdrv_getlength) { 38351762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 38451762288SStefan Hajnoczi if (length < 0) { 38551762288SStefan Hajnoczi return length; 38651762288SStefan Hajnoczi } 38751762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 38851762288SStefan Hajnoczi } 38951762288SStefan Hajnoczi 39051762288SStefan Hajnoczi bs->total_sectors = hint; 39151762288SStefan Hajnoczi return 0; 39251762288SStefan Hajnoczi } 39351762288SStefan Hajnoczi 394b6ce07aaSKevin Wolf /* 39557915332SKevin Wolf * Common part for opening disk images and files 39657915332SKevin Wolf */ 39757915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 39857915332SKevin Wolf int flags, BlockDriver *drv) 39957915332SKevin Wolf { 40057915332SKevin Wolf int ret, open_flags; 40157915332SKevin Wolf 40257915332SKevin Wolf assert(drv != NULL); 40357915332SKevin Wolf 40466f82ceeSKevin Wolf bs->file = NULL; 40551762288SStefan Hajnoczi bs->total_sectors = 0; 40657915332SKevin Wolf bs->encrypted = 0; 40757915332SKevin Wolf bs->valid_key = 0; 40857915332SKevin Wolf bs->open_flags = flags; 40957915332SKevin Wolf /* buffer_alignment defaulted to 512, drivers can change this value */ 41057915332SKevin Wolf bs->buffer_alignment = 512; 41157915332SKevin Wolf 41257915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 41357915332SKevin Wolf 41457915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 41557915332SKevin Wolf return -ENOTSUP; 41657915332SKevin Wolf } 41757915332SKevin Wolf 41857915332SKevin Wolf bs->drv = drv; 41957915332SKevin Wolf bs->opaque = qemu_mallocz(drv->instance_size); 42057915332SKevin Wolf 42157915332SKevin Wolf /* 42257915332SKevin Wolf * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a 42357915332SKevin Wolf * write cache to the guest. We do need the fdatasync to flush 42457915332SKevin Wolf * out transactions for block allocations, and we maybe have a 42557915332SKevin Wolf * volatile write cache in our backing device to deal with. 42657915332SKevin Wolf */ 42757915332SKevin Wolf if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) 42857915332SKevin Wolf bs->enable_write_cache = 1; 42957915332SKevin Wolf 43057915332SKevin Wolf /* 43157915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 43257915332SKevin Wolf * image. 43357915332SKevin Wolf */ 43457915332SKevin Wolf open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 43557915332SKevin Wolf 43657915332SKevin Wolf /* 43757915332SKevin Wolf * Snapshots should be writeable. 43857915332SKevin Wolf */ 43957915332SKevin Wolf if (bs->is_temporary) { 44057915332SKevin Wolf open_flags |= BDRV_O_RDWR; 44157915332SKevin Wolf } 44257915332SKevin Wolf 44366f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 44466f82ceeSKevin Wolf if (drv->bdrv_file_open) { 44566f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 44666f82ceeSKevin Wolf } else { 44766f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 44866f82ceeSKevin Wolf if (ret >= 0) { 44966f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 45066f82ceeSKevin Wolf } 45166f82ceeSKevin Wolf } 45266f82ceeSKevin Wolf 45357915332SKevin Wolf if (ret < 0) { 45457915332SKevin Wolf goto free_and_fail; 45557915332SKevin Wolf } 45657915332SKevin Wolf 45757915332SKevin Wolf bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 45851762288SStefan Hajnoczi 45951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 46051762288SStefan Hajnoczi if (ret < 0) { 46151762288SStefan Hajnoczi goto free_and_fail; 46257915332SKevin Wolf } 46351762288SStefan Hajnoczi 46457915332SKevin Wolf #ifndef _WIN32 46557915332SKevin Wolf if (bs->is_temporary) { 46657915332SKevin Wolf unlink(filename); 46757915332SKevin Wolf } 46857915332SKevin Wolf #endif 46957915332SKevin Wolf return 0; 47057915332SKevin Wolf 47157915332SKevin Wolf free_and_fail: 47266f82ceeSKevin Wolf if (bs->file) { 47366f82ceeSKevin Wolf bdrv_delete(bs->file); 47466f82ceeSKevin Wolf bs->file = NULL; 47566f82ceeSKevin Wolf } 47657915332SKevin Wolf qemu_free(bs->opaque); 47757915332SKevin Wolf bs->opaque = NULL; 47857915332SKevin Wolf bs->drv = NULL; 47957915332SKevin Wolf return ret; 48057915332SKevin Wolf } 48157915332SKevin Wolf 48257915332SKevin Wolf /* 483b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 484b6ce07aaSKevin Wolf */ 48583f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 486b338082bSbellard { 48783f64091Sbellard BlockDriverState *bs; 4886db95603SChristoph Hellwig BlockDriver *drv; 48983f64091Sbellard int ret; 4903b0d4f61Sbellard 491b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 4926db95603SChristoph Hellwig if (!drv) { 4936db95603SChristoph Hellwig return -ENOENT; 4946db95603SChristoph Hellwig } 4956db95603SChristoph Hellwig 49683f64091Sbellard bs = bdrv_new(""); 497b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 49883f64091Sbellard if (ret < 0) { 49983f64091Sbellard bdrv_delete(bs); 50083f64091Sbellard return ret; 5013b0d4f61Sbellard } 50271d0770cSaliguori bs->growable = 1; 50383f64091Sbellard *pbs = bs; 50483f64091Sbellard return 0; 5053b0d4f61Sbellard } 5063b0d4f61Sbellard 507b6ce07aaSKevin Wolf /* 508b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 509b6ce07aaSKevin Wolf */ 510d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 511ea2384d3Sbellard BlockDriver *drv) 512ea2384d3Sbellard { 513b6ce07aaSKevin Wolf int ret; 51433e3963eSbellard 51583f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 516ea2384d3Sbellard BlockDriverState *bs1; 517ea2384d3Sbellard int64_t total_size; 5187c96d46eSaliguori int is_protocol = 0; 51991a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 52091a073a9SKevin Wolf QEMUOptionParameter *options; 521b6ce07aaSKevin Wolf char tmp_filename[PATH_MAX]; 522b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 52333e3963eSbellard 524ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 525ea2384d3Sbellard instead of opening 'filename' directly */ 526ea2384d3Sbellard 527ea2384d3Sbellard /* if there is a backing file, use it */ 528ea2384d3Sbellard bs1 = bdrv_new(""); 529d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 53051d7c00cSaliguori if (ret < 0) { 531ea2384d3Sbellard bdrv_delete(bs1); 53251d7c00cSaliguori return ret; 533ea2384d3Sbellard } 5343e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 5357c96d46eSaliguori 5367c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 5377c96d46eSaliguori is_protocol = 1; 5387c96d46eSaliguori 539ea2384d3Sbellard bdrv_delete(bs1); 540ea2384d3Sbellard 541ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 5427c96d46eSaliguori 5437c96d46eSaliguori /* Real path is meaningless for protocols */ 5447c96d46eSaliguori if (is_protocol) 5457c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 5467c96d46eSaliguori "%s", filename); 547114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 548114cdfa9SKirill A. Shutemov return -errno; 5497c96d46eSaliguori 55091a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 55191a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 55291a073a9SKevin Wolf 5533e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 55491a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 55591a073a9SKevin Wolf if (drv) { 55691a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 55791a073a9SKevin Wolf drv->format_name); 55891a073a9SKevin Wolf } 55991a073a9SKevin Wolf 56091a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 561d748768cSJan Kiszka free_option_parameters(options); 56251d7c00cSaliguori if (ret < 0) { 56351d7c00cSaliguori return ret; 564ea2384d3Sbellard } 56591a073a9SKevin Wolf 566ea2384d3Sbellard filename = tmp_filename; 56791a073a9SKevin Wolf drv = bdrv_qcow2; 568ea2384d3Sbellard bs->is_temporary = 1; 569ea2384d3Sbellard } 570ea2384d3Sbellard 571b6ce07aaSKevin Wolf /* Find the right image format driver */ 5726db95603SChristoph Hellwig if (!drv) { 573ea2384d3Sbellard drv = find_image_format(filename); 574ea2384d3Sbellard } 5756987307cSChristoph Hellwig 57651d7c00cSaliguori if (!drv) { 57751d7c00cSaliguori ret = -ENOENT; 57851d7c00cSaliguori goto unlink_and_fail; 57983f64091Sbellard } 580b6ce07aaSKevin Wolf 581b6ce07aaSKevin Wolf /* Open the image */ 582b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 583b6ce07aaSKevin Wolf if (ret < 0) { 5846987307cSChristoph Hellwig goto unlink_and_fail; 5856987307cSChristoph Hellwig } 5866987307cSChristoph Hellwig 587b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 588b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 589b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 590b6ce07aaSKevin Wolf int back_flags; 591b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 592b6ce07aaSKevin Wolf 593b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 594b6ce07aaSKevin Wolf path_combine(backing_filename, sizeof(backing_filename), 595b6ce07aaSKevin Wolf filename, bs->backing_file); 596b6ce07aaSKevin Wolf if (bs->backing_format[0] != '\0') 597b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 598b6ce07aaSKevin Wolf 599b6ce07aaSKevin Wolf /* backing files always opened read-only */ 600b6ce07aaSKevin Wolf back_flags = 601b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 602b6ce07aaSKevin Wolf 603b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 604b6ce07aaSKevin Wolf if (ret < 0) { 605b6ce07aaSKevin Wolf bdrv_close(bs); 606b6ce07aaSKevin Wolf return ret; 607b6ce07aaSKevin Wolf } 608b6ce07aaSKevin Wolf if (bs->is_temporary) { 609b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 610b6ce07aaSKevin Wolf } else { 611b6ce07aaSKevin Wolf /* base image inherits from "parent" */ 612b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = bs->keep_read_only; 613b6ce07aaSKevin Wolf } 614b6ce07aaSKevin Wolf } 615b6ce07aaSKevin Wolf 616b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 617b6ce07aaSKevin Wolf /* call the change callback */ 618b6ce07aaSKevin Wolf bs->media_changed = 1; 619b6ce07aaSKevin Wolf if (bs->change_cb) 620b6ce07aaSKevin Wolf bs->change_cb(bs->change_opaque); 621b6ce07aaSKevin Wolf } 622b6ce07aaSKevin Wolf 623b6ce07aaSKevin Wolf return 0; 624b6ce07aaSKevin Wolf 625b6ce07aaSKevin Wolf unlink_and_fail: 626b6ce07aaSKevin Wolf if (bs->is_temporary) { 627b6ce07aaSKevin Wolf unlink(filename); 628b6ce07aaSKevin Wolf } 629b6ce07aaSKevin Wolf return ret; 630b6ce07aaSKevin Wolf } 631b6ce07aaSKevin Wolf 632fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 633fc01f7e7Sbellard { 63419cb3738Sbellard if (bs->drv) { 635f9092b10SMarkus Armbruster if (bs == bs_snapshots) { 636f9092b10SMarkus Armbruster bs_snapshots = NULL; 637f9092b10SMarkus Armbruster } 638557df6acSStefan Hajnoczi if (bs->backing_hd) { 639ea2384d3Sbellard bdrv_delete(bs->backing_hd); 640557df6acSStefan Hajnoczi bs->backing_hd = NULL; 641557df6acSStefan Hajnoczi } 642ea2384d3Sbellard bs->drv->bdrv_close(bs); 643ea2384d3Sbellard qemu_free(bs->opaque); 644ea2384d3Sbellard #ifdef _WIN32 645ea2384d3Sbellard if (bs->is_temporary) { 646ea2384d3Sbellard unlink(bs->filename); 647ea2384d3Sbellard } 64867b915a5Sbellard #endif 649ea2384d3Sbellard bs->opaque = NULL; 650ea2384d3Sbellard bs->drv = NULL; 651b338082bSbellard 65266f82ceeSKevin Wolf if (bs->file != NULL) { 65366f82ceeSKevin Wolf bdrv_close(bs->file); 65466f82ceeSKevin Wolf } 65566f82ceeSKevin Wolf 656b338082bSbellard /* call the change callback */ 65719cb3738Sbellard bs->media_changed = 1; 658b338082bSbellard if (bs->change_cb) 659b338082bSbellard bs->change_cb(bs->change_opaque); 660b338082bSbellard } 661b338082bSbellard } 662b338082bSbellard 6632bc93fedSMORITA Kazutaka void bdrv_close_all(void) 6642bc93fedSMORITA Kazutaka { 6652bc93fedSMORITA Kazutaka BlockDriverState *bs; 6662bc93fedSMORITA Kazutaka 6672bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 6682bc93fedSMORITA Kazutaka bdrv_close(bs); 6692bc93fedSMORITA Kazutaka } 6702bc93fedSMORITA Kazutaka } 6712bc93fedSMORITA Kazutaka 672b338082bSbellard void bdrv_delete(BlockDriverState *bs) 673b338082bSbellard { 67418846deeSMarkus Armbruster assert(!bs->peer); 67518846deeSMarkus Armbruster 6761b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 6771b7bdbc1SStefan Hajnoczi if (bs->device_name[0] != '\0') { 6781b7bdbc1SStefan Hajnoczi QTAILQ_REMOVE(&bdrv_states, bs, list); 6791b7bdbc1SStefan Hajnoczi } 68034c6f050Saurel32 681b338082bSbellard bdrv_close(bs); 68266f82ceeSKevin Wolf if (bs->file != NULL) { 68366f82ceeSKevin Wolf bdrv_delete(bs->file); 68466f82ceeSKevin Wolf } 68566f82ceeSKevin Wolf 686f9092b10SMarkus Armbruster assert(bs != bs_snapshots); 687b338082bSbellard qemu_free(bs); 688fc01f7e7Sbellard } 689fc01f7e7Sbellard 69018846deeSMarkus Armbruster int bdrv_attach(BlockDriverState *bs, DeviceState *qdev) 69118846deeSMarkus Armbruster { 69218846deeSMarkus Armbruster if (bs->peer) { 69318846deeSMarkus Armbruster return -EBUSY; 69418846deeSMarkus Armbruster } 69518846deeSMarkus Armbruster bs->peer = qdev; 69618846deeSMarkus Armbruster return 0; 69718846deeSMarkus Armbruster } 69818846deeSMarkus Armbruster 69918846deeSMarkus Armbruster void bdrv_detach(BlockDriverState *bs, DeviceState *qdev) 70018846deeSMarkus Armbruster { 70118846deeSMarkus Armbruster assert(bs->peer == qdev); 70218846deeSMarkus Armbruster bs->peer = NULL; 70318846deeSMarkus Armbruster } 70418846deeSMarkus Armbruster 70518846deeSMarkus Armbruster DeviceState *bdrv_get_attached(BlockDriverState *bs) 70618846deeSMarkus Armbruster { 70718846deeSMarkus Armbruster return bs->peer; 70818846deeSMarkus Armbruster } 70918846deeSMarkus Armbruster 710e97fc193Saliguori /* 711e97fc193Saliguori * Run consistency checks on an image 712e97fc193Saliguori * 713*e076f338SKevin Wolf * Returns 0 if the check could be completed (it doesn't mean that the image is 714*e076f338SKevin Wolf * free of errors) or -errno when an internal error occured. The results of the 715*e076f338SKevin Wolf * check are stored in res. 716e97fc193Saliguori */ 717*e076f338SKevin Wolf int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res) 718e97fc193Saliguori { 719e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 720e97fc193Saliguori return -ENOTSUP; 721e97fc193Saliguori } 722e97fc193Saliguori 723*e076f338SKevin Wolf memset(res, 0, sizeof(*res)); 724*e076f338SKevin Wolf res->corruptions = bs->drv->bdrv_check(bs); 725*e076f338SKevin Wolf return res->corruptions < 0 ? res->corruptions : 0; 726e97fc193Saliguori } 727e97fc193Saliguori 72833e3963eSbellard /* commit COW file into the raw image */ 72933e3963eSbellard int bdrv_commit(BlockDriverState *bs) 73033e3963eSbellard { 73119cb3738Sbellard BlockDriver *drv = bs->drv; 73283f64091Sbellard int64_t i, total_sectors; 7334dca4b63SNaphtali Sprei int n, j, ro, open_flags; 7344dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 735eb5a3165SJes Sorensen unsigned char sector[BDRV_SECTOR_SIZE]; 7364dca4b63SNaphtali Sprei char filename[1024]; 7374dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 73833e3963eSbellard 73919cb3738Sbellard if (!drv) 74019cb3738Sbellard return -ENOMEDIUM; 74133e3963eSbellard 7424dca4b63SNaphtali Sprei if (!bs->backing_hd) { 7434dca4b63SNaphtali Sprei return -ENOTSUP; 7444dca4b63SNaphtali Sprei } 7454dca4b63SNaphtali Sprei 7464dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 747ea2384d3Sbellard return -EACCES; 74833e3963eSbellard } 74933e3963eSbellard 7504dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 7514dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 7524dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 7534dca4b63SNaphtali Sprei 7544dca4b63SNaphtali Sprei if (ro) { 7554dca4b63SNaphtali Sprei /* re-open as RW */ 7564dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 7574dca4b63SNaphtali Sprei bs->backing_hd = NULL; 7584dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 759c3349197SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv); 7604dca4b63SNaphtali Sprei if (rw_ret < 0) { 7614dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 7624dca4b63SNaphtali Sprei /* try to re-open read-only */ 7634dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 764c3349197SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); 7654dca4b63SNaphtali Sprei if (ret < 0) { 7664dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 7674dca4b63SNaphtali Sprei /* drive not functional anymore */ 7684dca4b63SNaphtali Sprei bs->drv = NULL; 7694dca4b63SNaphtali Sprei return ret; 7704dca4b63SNaphtali Sprei } 7714dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 7724dca4b63SNaphtali Sprei return rw_ret; 7734dca4b63SNaphtali Sprei } 7744dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 775ea2384d3Sbellard } 776ea2384d3Sbellard 7776ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 77883f64091Sbellard for (i = 0; i < total_sectors;) { 77919cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 780ea2384d3Sbellard for(j = 0; j < n; j++) { 78133e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 7824dca4b63SNaphtali Sprei ret = -EIO; 7834dca4b63SNaphtali Sprei goto ro_cleanup; 78433e3963eSbellard } 78533e3963eSbellard 786ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 7874dca4b63SNaphtali Sprei ret = -EIO; 7884dca4b63SNaphtali Sprei goto ro_cleanup; 78933e3963eSbellard } 790ea2384d3Sbellard i++; 791ea2384d3Sbellard } 792ea2384d3Sbellard } else { 793ea2384d3Sbellard i += n; 79433e3963eSbellard } 79533e3963eSbellard } 79695389c86Sbellard 7971d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 7981d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 7991d44952fSChristoph Hellwig bdrv_flush(bs); 8001d44952fSChristoph Hellwig } 80195389c86Sbellard 8023f5075aeSChristoph Hellwig /* 8033f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 8043f5075aeSChristoph Hellwig * stable on disk. 8053f5075aeSChristoph Hellwig */ 8063f5075aeSChristoph Hellwig if (bs->backing_hd) 8073f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 8084dca4b63SNaphtali Sprei 8094dca4b63SNaphtali Sprei ro_cleanup: 8104dca4b63SNaphtali Sprei 8114dca4b63SNaphtali Sprei if (ro) { 8124dca4b63SNaphtali Sprei /* re-open as RO */ 8134dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 8144dca4b63SNaphtali Sprei bs->backing_hd = NULL; 8154dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 816c3349197SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); 8174dca4b63SNaphtali Sprei if (ret < 0) { 8184dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 8194dca4b63SNaphtali Sprei /* drive not functional anymore */ 8204dca4b63SNaphtali Sprei bs->drv = NULL; 8214dca4b63SNaphtali Sprei return ret; 8224dca4b63SNaphtali Sprei } 8234dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 8244dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 8254dca4b63SNaphtali Sprei } 8264dca4b63SNaphtali Sprei 8271d44952fSChristoph Hellwig return ret; 82833e3963eSbellard } 82933e3963eSbellard 8306ab4b5abSMarkus Armbruster void bdrv_commit_all(void) 8316ab4b5abSMarkus Armbruster { 8326ab4b5abSMarkus Armbruster BlockDriverState *bs; 8336ab4b5abSMarkus Armbruster 8346ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 8356ab4b5abSMarkus Armbruster bdrv_commit(bs); 8366ab4b5abSMarkus Armbruster } 8376ab4b5abSMarkus Armbruster } 8386ab4b5abSMarkus Armbruster 839756e6736SKevin Wolf /* 840756e6736SKevin Wolf * Return values: 841756e6736SKevin Wolf * 0 - success 842756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 843756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 844756e6736SKevin Wolf * image file header 845756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 846756e6736SKevin Wolf */ 847756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 848756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 849756e6736SKevin Wolf { 850756e6736SKevin Wolf BlockDriver *drv = bs->drv; 851756e6736SKevin Wolf 852756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 853756e6736SKevin Wolf return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 854756e6736SKevin Wolf } else { 855756e6736SKevin Wolf return -ENOTSUP; 856756e6736SKevin Wolf } 857756e6736SKevin Wolf } 858756e6736SKevin Wolf 85971d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 86071d0770cSaliguori size_t size) 86171d0770cSaliguori { 86271d0770cSaliguori int64_t len; 86371d0770cSaliguori 86471d0770cSaliguori if (!bdrv_is_inserted(bs)) 86571d0770cSaliguori return -ENOMEDIUM; 86671d0770cSaliguori 86771d0770cSaliguori if (bs->growable) 86871d0770cSaliguori return 0; 86971d0770cSaliguori 87071d0770cSaliguori len = bdrv_getlength(bs); 87171d0770cSaliguori 872fbb7b4e0SKevin Wolf if (offset < 0) 873fbb7b4e0SKevin Wolf return -EIO; 874fbb7b4e0SKevin Wolf 875fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 87671d0770cSaliguori return -EIO; 87771d0770cSaliguori 87871d0770cSaliguori return 0; 87971d0770cSaliguori } 88071d0770cSaliguori 88171d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 88271d0770cSaliguori int nb_sectors) 88371d0770cSaliguori { 884eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 885eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 88671d0770cSaliguori } 88771d0770cSaliguori 88819cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 889fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 890fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 891fc01f7e7Sbellard { 892ea2384d3Sbellard BlockDriver *drv = bs->drv; 893fc01f7e7Sbellard 89419cb3738Sbellard if (!drv) 89519cb3738Sbellard return -ENOMEDIUM; 89671d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 89771d0770cSaliguori return -EIO; 898b338082bSbellard 89983f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 90083f64091Sbellard } 901fc01f7e7Sbellard 9027cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 9037cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 9047cd1e32aSlirans@il.ibm.com { 9057cd1e32aSlirans@il.ibm.com int64_t start, end; 906c6d22830SJan Kiszka unsigned long val, idx, bit; 907a55eb92cSJan Kiszka 9086ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 909c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 9107cd1e32aSlirans@il.ibm.com 9117cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 912c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 913c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 914c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 915c6d22830SJan Kiszka if (dirty) { 916aaa0eb75SLiran Schour if (!(val & (1 << bit))) { 917aaa0eb75SLiran Schour bs->dirty_count++; 918c6d22830SJan Kiszka val |= 1 << bit; 919aaa0eb75SLiran Schour } 920c6d22830SJan Kiszka } else { 921aaa0eb75SLiran Schour if (val & (1 << bit)) { 922aaa0eb75SLiran Schour bs->dirty_count--; 923c6d22830SJan Kiszka val &= ~(1 << bit); 924c6d22830SJan Kiszka } 925aaa0eb75SLiran Schour } 926c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 9277cd1e32aSlirans@il.ibm.com } 9287cd1e32aSlirans@il.ibm.com } 9297cd1e32aSlirans@il.ibm.com 93019cb3738Sbellard /* Return < 0 if error. Important errors are: 93119cb3738Sbellard -EIO generic I/O error (may happen for all errors) 93219cb3738Sbellard -ENOMEDIUM No media inserted. 93319cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 93419cb3738Sbellard -EACCES Trying to write a read-only device 93519cb3738Sbellard */ 936fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 937fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 938fc01f7e7Sbellard { 93983f64091Sbellard BlockDriver *drv = bs->drv; 94019cb3738Sbellard if (!bs->drv) 94119cb3738Sbellard return -ENOMEDIUM; 9420849bf08Sbellard if (bs->read_only) 94319cb3738Sbellard return -EACCES; 94471d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 94571d0770cSaliguori return -EIO; 94671d0770cSaliguori 947c6d22830SJan Kiszka if (bs->dirty_bitmap) { 9487cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 9497cd1e32aSlirans@il.ibm.com } 9507cd1e32aSlirans@il.ibm.com 951294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 952294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 953294cc35fSKevin Wolf } 954294cc35fSKevin Wolf 95583f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 95683f64091Sbellard } 95783f64091Sbellard 958eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 959eda578e5Saliguori void *buf, int count1) 96083f64091Sbellard { 9616ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 96283f64091Sbellard int len, nb_sectors, count; 96383f64091Sbellard int64_t sector_num; 9649a8c4cceSKevin Wolf int ret; 96583f64091Sbellard 96683f64091Sbellard count = count1; 96783f64091Sbellard /* first read to align to sector start */ 9686ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 96983f64091Sbellard if (len > count) 97083f64091Sbellard len = count; 9716ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 97283f64091Sbellard if (len > 0) { 9739a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9749a8c4cceSKevin Wolf return ret; 9756ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 97683f64091Sbellard count -= len; 97783f64091Sbellard if (count == 0) 97883f64091Sbellard return count1; 97983f64091Sbellard sector_num++; 98083f64091Sbellard buf += len; 98183f64091Sbellard } 98283f64091Sbellard 98383f64091Sbellard /* read the sectors "in place" */ 9846ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 98583f64091Sbellard if (nb_sectors > 0) { 9869a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 9879a8c4cceSKevin Wolf return ret; 98883f64091Sbellard sector_num += nb_sectors; 9896ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 99083f64091Sbellard buf += len; 99183f64091Sbellard count -= len; 99283f64091Sbellard } 99383f64091Sbellard 99483f64091Sbellard /* add data from the last sector */ 99583f64091Sbellard if (count > 0) { 9969a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9979a8c4cceSKevin Wolf return ret; 99883f64091Sbellard memcpy(buf, tmp_buf, count); 99983f64091Sbellard } 100083f64091Sbellard return count1; 100183f64091Sbellard } 100283f64091Sbellard 1003eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 1004eda578e5Saliguori const void *buf, int count1) 100583f64091Sbellard { 10066ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 100783f64091Sbellard int len, nb_sectors, count; 100883f64091Sbellard int64_t sector_num; 10099a8c4cceSKevin Wolf int ret; 101083f64091Sbellard 101183f64091Sbellard count = count1; 101283f64091Sbellard /* first write to align to sector start */ 10136ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 101483f64091Sbellard if (len > count) 101583f64091Sbellard len = count; 10166ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 101783f64091Sbellard if (len > 0) { 10189a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10199a8c4cceSKevin Wolf return ret; 10206ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 10219a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 10229a8c4cceSKevin Wolf return ret; 102383f64091Sbellard count -= len; 102483f64091Sbellard if (count == 0) 102583f64091Sbellard return count1; 102683f64091Sbellard sector_num++; 102783f64091Sbellard buf += len; 102883f64091Sbellard } 102983f64091Sbellard 103083f64091Sbellard /* write the sectors "in place" */ 10316ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 103283f64091Sbellard if (nb_sectors > 0) { 10339a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 10349a8c4cceSKevin Wolf return ret; 103583f64091Sbellard sector_num += nb_sectors; 10366ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 103783f64091Sbellard buf += len; 103883f64091Sbellard count -= len; 103983f64091Sbellard } 104083f64091Sbellard 104183f64091Sbellard /* add data from the last sector */ 104283f64091Sbellard if (count > 0) { 10439a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10449a8c4cceSKevin Wolf return ret; 104583f64091Sbellard memcpy(tmp_buf, buf, count); 10469a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 10479a8c4cceSKevin Wolf return ret; 104883f64091Sbellard } 104983f64091Sbellard return count1; 105083f64091Sbellard } 105183f64091Sbellard 1052f08145feSKevin Wolf /* 1053f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1054f08145feSKevin Wolf * request (acts as a barrier) 1055f08145feSKevin Wolf * 1056f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1057f08145feSKevin Wolf */ 1058f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1059f08145feSKevin Wolf const void *buf, int count) 1060f08145feSKevin Wolf { 1061f08145feSKevin Wolf int ret; 1062f08145feSKevin Wolf 1063f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1064f08145feSKevin Wolf if (ret < 0) { 1065f08145feSKevin Wolf return ret; 1066f08145feSKevin Wolf } 1067f08145feSKevin Wolf 1068f08145feSKevin Wolf /* No flush needed for cache=writethrough, it uses O_DSYNC */ 1069f08145feSKevin Wolf if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) { 1070f08145feSKevin Wolf bdrv_flush(bs); 1071f08145feSKevin Wolf } 1072f08145feSKevin Wolf 1073f08145feSKevin Wolf return 0; 1074f08145feSKevin Wolf } 1075f08145feSKevin Wolf 1076f08145feSKevin Wolf /* 1077f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1078f08145feSKevin Wolf * request (acts as a barrier) 1079f08145feSKevin Wolf * 1080f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1081f08145feSKevin Wolf */ 1082f08145feSKevin Wolf int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num, 1083f08145feSKevin Wolf const uint8_t *buf, int nb_sectors) 1084f08145feSKevin Wolf { 1085f08145feSKevin Wolf return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num, 1086f08145feSKevin Wolf buf, BDRV_SECTOR_SIZE * nb_sectors); 1087f08145feSKevin Wolf } 1088f08145feSKevin Wolf 108983f64091Sbellard /** 109083f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 109183f64091Sbellard */ 109283f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 109383f64091Sbellard { 109483f64091Sbellard BlockDriver *drv = bs->drv; 109551762288SStefan Hajnoczi int ret; 109683f64091Sbellard if (!drv) 109719cb3738Sbellard return -ENOMEDIUM; 109883f64091Sbellard if (!drv->bdrv_truncate) 109983f64091Sbellard return -ENOTSUP; 110059f2689dSNaphtali Sprei if (bs->read_only) 110159f2689dSNaphtali Sprei return -EACCES; 110251762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 110351762288SStefan Hajnoczi if (ret == 0) { 110451762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 110551762288SStefan Hajnoczi } 110651762288SStefan Hajnoczi return ret; 110783f64091Sbellard } 110883f64091Sbellard 110983f64091Sbellard /** 111083f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 111183f64091Sbellard */ 111283f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 111383f64091Sbellard { 111483f64091Sbellard BlockDriver *drv = bs->drv; 111583f64091Sbellard if (!drv) 111619cb3738Sbellard return -ENOMEDIUM; 111751762288SStefan Hajnoczi 111851762288SStefan Hajnoczi /* Fixed size devices use the total_sectors value for speed instead of 111951762288SStefan Hajnoczi issuing a length query (like lseek) on each call. Also, legacy block 112051762288SStefan Hajnoczi drivers don't provide a bdrv_getlength function and must use 112151762288SStefan Hajnoczi total_sectors. */ 112251762288SStefan Hajnoczi if (!bs->growable || !drv->bdrv_getlength) { 11236ea44308SJan Kiszka return bs->total_sectors * BDRV_SECTOR_SIZE; 112483f64091Sbellard } 112583f64091Sbellard return drv->bdrv_getlength(bs); 1126fc01f7e7Sbellard } 1127fc01f7e7Sbellard 112819cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 112996b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 1130fc01f7e7Sbellard { 113119cb3738Sbellard int64_t length; 113219cb3738Sbellard length = bdrv_getlength(bs); 113319cb3738Sbellard if (length < 0) 113419cb3738Sbellard length = 0; 113519cb3738Sbellard else 11366ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 113719cb3738Sbellard *nb_sectors_ptr = length; 1138fc01f7e7Sbellard } 1139cf98951bSbellard 1140f3d54fc4Saliguori struct partition { 1141f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 1142f3d54fc4Saliguori uint8_t head; /* starting head */ 1143f3d54fc4Saliguori uint8_t sector; /* starting sector */ 1144f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 1145f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 1146f3d54fc4Saliguori uint8_t end_head; /* end head */ 1147f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 1148f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 1149f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 1150f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 1151f3d54fc4Saliguori } __attribute__((packed)); 1152f3d54fc4Saliguori 1153f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 1154f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 1155f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 1156f3d54fc4Saliguori { 1157eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 1158f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 1159f3d54fc4Saliguori struct partition *p; 1160f3d54fc4Saliguori uint32_t nr_sects; 1161a38131b6Sblueswir1 uint64_t nb_sectors; 1162f3d54fc4Saliguori 1163f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1164f3d54fc4Saliguori 1165f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 1166f3d54fc4Saliguori if (ret < 0) 1167f3d54fc4Saliguori return -1; 1168f3d54fc4Saliguori /* test msdos magic */ 1169f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 1170f3d54fc4Saliguori return -1; 1171f3d54fc4Saliguori for(i = 0; i < 4; i++) { 1172f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 1173f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 1174f3d54fc4Saliguori if (nr_sects && p->end_head) { 1175f3d54fc4Saliguori /* We make the assumption that the partition terminates on 1176f3d54fc4Saliguori a cylinder boundary */ 1177f3d54fc4Saliguori heads = p->end_head + 1; 1178f3d54fc4Saliguori sectors = p->end_sector & 63; 1179f3d54fc4Saliguori if (sectors == 0) 1180f3d54fc4Saliguori continue; 1181f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 1182f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 1183f3d54fc4Saliguori continue; 1184f3d54fc4Saliguori *pheads = heads; 1185f3d54fc4Saliguori *psectors = sectors; 1186f3d54fc4Saliguori *pcylinders = cylinders; 1187f3d54fc4Saliguori #if 0 1188f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 1189f3d54fc4Saliguori cylinders, heads, sectors); 1190f3d54fc4Saliguori #endif 1191f3d54fc4Saliguori return 0; 1192f3d54fc4Saliguori } 1193f3d54fc4Saliguori } 1194f3d54fc4Saliguori return -1; 1195f3d54fc4Saliguori } 1196f3d54fc4Saliguori 1197f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 1198f3d54fc4Saliguori { 1199f3d54fc4Saliguori int translation, lba_detected = 0; 1200f3d54fc4Saliguori int cylinders, heads, secs; 1201a38131b6Sblueswir1 uint64_t nb_sectors; 1202f3d54fc4Saliguori 1203f3d54fc4Saliguori /* if a geometry hint is available, use it */ 1204f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1205f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 1206f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 1207f3d54fc4Saliguori if (cylinders != 0) { 1208f3d54fc4Saliguori *pcyls = cylinders; 1209f3d54fc4Saliguori *pheads = heads; 1210f3d54fc4Saliguori *psecs = secs; 1211f3d54fc4Saliguori } else { 1212f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 1213f3d54fc4Saliguori if (heads > 16) { 1214f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 1215f3d54fc4Saliguori translation was active, so the default 1216f3d54fc4Saliguori hardware geometry is OK */ 1217f3d54fc4Saliguori lba_detected = 1; 1218f3d54fc4Saliguori goto default_geometry; 1219f3d54fc4Saliguori } else { 1220f3d54fc4Saliguori *pcyls = cylinders; 1221f3d54fc4Saliguori *pheads = heads; 1222f3d54fc4Saliguori *psecs = secs; 1223f3d54fc4Saliguori /* disable any translation to be in sync with 1224f3d54fc4Saliguori the logical geometry */ 1225f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 1226f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1227f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 1228f3d54fc4Saliguori } 1229f3d54fc4Saliguori } 1230f3d54fc4Saliguori } else { 1231f3d54fc4Saliguori default_geometry: 1232f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 1233f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 1234f3d54fc4Saliguori 1235f3d54fc4Saliguori if (cylinders > 16383) 1236f3d54fc4Saliguori cylinders = 16383; 1237f3d54fc4Saliguori else if (cylinders < 2) 1238f3d54fc4Saliguori cylinders = 2; 1239f3d54fc4Saliguori *pcyls = cylinders; 1240f3d54fc4Saliguori *pheads = 16; 1241f3d54fc4Saliguori *psecs = 63; 1242f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 1243f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 1244f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1245f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 1246f3d54fc4Saliguori } else { 1247f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1248f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 1249f3d54fc4Saliguori } 1250f3d54fc4Saliguori } 1251f3d54fc4Saliguori } 1252f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 1253f3d54fc4Saliguori } 1254f3d54fc4Saliguori } 1255f3d54fc4Saliguori 1256b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 1257b338082bSbellard int cyls, int heads, int secs) 1258b338082bSbellard { 1259b338082bSbellard bs->cyls = cyls; 1260b338082bSbellard bs->heads = heads; 1261b338082bSbellard bs->secs = secs; 1262b338082bSbellard } 1263b338082bSbellard 1264b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 1265b338082bSbellard { 1266b338082bSbellard bs->type = type; 1267b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 1268b338082bSbellard type == BDRV_TYPE_FLOPPY)); 1269b338082bSbellard } 1270b338082bSbellard 127146d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 127246d4767dSbellard { 127346d4767dSbellard bs->translation = translation; 127446d4767dSbellard } 127546d4767dSbellard 1276b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 1277b338082bSbellard int *pcyls, int *pheads, int *psecs) 1278b338082bSbellard { 1279b338082bSbellard *pcyls = bs->cyls; 1280b338082bSbellard *pheads = bs->heads; 1281b338082bSbellard *psecs = bs->secs; 1282b338082bSbellard } 1283b338082bSbellard 1284b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 1285b338082bSbellard { 1286b338082bSbellard return bs->type; 1287b338082bSbellard } 1288b338082bSbellard 128946d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 129046d4767dSbellard { 129146d4767dSbellard return bs->translation; 129246d4767dSbellard } 129346d4767dSbellard 1294abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 1295abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 1296abd7f68dSMarkus Armbruster { 1297abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 1298abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 1299abd7f68dSMarkus Armbruster } 1300abd7f68dSMarkus Armbruster 1301abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 1302abd7f68dSMarkus Armbruster { 1303abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 1304abd7f68dSMarkus Armbruster } 1305abd7f68dSMarkus Armbruster 13067d0d6950SMarkus Armbruster void bdrv_set_removable(BlockDriverState *bs, int removable) 13077d0d6950SMarkus Armbruster { 13087d0d6950SMarkus Armbruster bs->removable = removable; 13097d0d6950SMarkus Armbruster if (removable && bs == bs_snapshots) { 13107d0d6950SMarkus Armbruster bs_snapshots = NULL; 13117d0d6950SMarkus Armbruster } 13127d0d6950SMarkus Armbruster } 13137d0d6950SMarkus Armbruster 1314b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 1315b338082bSbellard { 1316b338082bSbellard return bs->removable; 1317b338082bSbellard } 1318b338082bSbellard 1319b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 1320b338082bSbellard { 1321b338082bSbellard return bs->read_only; 1322b338082bSbellard } 1323b338082bSbellard 1324985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 1325985a03b0Sths { 1326985a03b0Sths return bs->sg; 1327985a03b0Sths } 1328985a03b0Sths 1329e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 1330e900a7b7SChristoph Hellwig { 1331e900a7b7SChristoph Hellwig return bs->enable_write_cache; 1332e900a7b7SChristoph Hellwig } 1333e900a7b7SChristoph Hellwig 133419cb3738Sbellard /* XXX: no longer used */ 1335b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 1336b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 1337b338082bSbellard { 1338b338082bSbellard bs->change_cb = change_cb; 1339b338082bSbellard bs->change_opaque = opaque; 1340b338082bSbellard } 1341b338082bSbellard 1342ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 1343ea2384d3Sbellard { 1344ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 1345ea2384d3Sbellard return 1; 1346ea2384d3Sbellard return bs->encrypted; 1347ea2384d3Sbellard } 1348ea2384d3Sbellard 1349c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 1350c0f4ce77Saliguori { 1351c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 1352c0f4ce77Saliguori 1353c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 1354c0f4ce77Saliguori return 1; 1355c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 1356c0f4ce77Saliguori } 1357c0f4ce77Saliguori 1358ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 1359ea2384d3Sbellard { 1360ea2384d3Sbellard int ret; 1361ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 1362ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 1363ea2384d3Sbellard if (ret < 0) 1364ea2384d3Sbellard return ret; 1365ea2384d3Sbellard if (!bs->encrypted) 1366ea2384d3Sbellard return 0; 1367ea2384d3Sbellard } 1368fd04a2aeSShahar Havivi if (!bs->encrypted) { 1369fd04a2aeSShahar Havivi return -EINVAL; 1370fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 1371fd04a2aeSShahar Havivi return -ENOMEDIUM; 1372fd04a2aeSShahar Havivi } 1373c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 1374bb5fc20fSaliguori if (ret < 0) { 1375bb5fc20fSaliguori bs->valid_key = 0; 1376bb5fc20fSaliguori } else if (!bs->valid_key) { 1377bb5fc20fSaliguori bs->valid_key = 1; 1378bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 1379bb5fc20fSaliguori bs->media_changed = 1; 1380bb5fc20fSaliguori if (bs->change_cb) 1381bb5fc20fSaliguori bs->change_cb(bs->change_opaque); 1382bb5fc20fSaliguori } 1383c0f4ce77Saliguori return ret; 1384ea2384d3Sbellard } 1385ea2384d3Sbellard 1386ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 1387ea2384d3Sbellard { 138819cb3738Sbellard if (!bs->drv) { 1389ea2384d3Sbellard buf[0] = '\0'; 1390ea2384d3Sbellard } else { 1391ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 1392ea2384d3Sbellard } 1393ea2384d3Sbellard } 1394ea2384d3Sbellard 1395ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 1396ea2384d3Sbellard void *opaque) 1397ea2384d3Sbellard { 1398ea2384d3Sbellard BlockDriver *drv; 1399ea2384d3Sbellard 14008a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 1401ea2384d3Sbellard it(opaque, drv->format_name); 1402ea2384d3Sbellard } 1403ea2384d3Sbellard } 1404ea2384d3Sbellard 1405b338082bSbellard BlockDriverState *bdrv_find(const char *name) 1406b338082bSbellard { 1407b338082bSbellard BlockDriverState *bs; 1408b338082bSbellard 14091b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 14101b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 1411b338082bSbellard return bs; 1412b338082bSbellard } 14131b7bdbc1SStefan Hajnoczi } 1414b338082bSbellard return NULL; 1415b338082bSbellard } 1416b338082bSbellard 14172f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 14182f399b0aSMarkus Armbruster { 14192f399b0aSMarkus Armbruster if (!bs) { 14202f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 14212f399b0aSMarkus Armbruster } 14222f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 14232f399b0aSMarkus Armbruster } 14242f399b0aSMarkus Armbruster 142551de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 142681d0912dSbellard { 142781d0912dSbellard BlockDriverState *bs; 142881d0912dSbellard 14291b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 143051de9760Saliguori it(opaque, bs); 143181d0912dSbellard } 143281d0912dSbellard } 143381d0912dSbellard 1434ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1435ea2384d3Sbellard { 1436ea2384d3Sbellard return bs->device_name; 1437ea2384d3Sbellard } 1438ea2384d3Sbellard 14397a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 14407a6cba61Spbrook { 1441016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 1442016f5cf6SAlexander Graf return; 1443016f5cf6SAlexander Graf } 1444016f5cf6SAlexander Graf 14453f5075aeSChristoph Hellwig if (bs->drv && bs->drv->bdrv_flush) 14467a6cba61Spbrook bs->drv->bdrv_flush(bs); 14477a6cba61Spbrook } 14487a6cba61Spbrook 1449c6ca28d6Saliguori void bdrv_flush_all(void) 1450c6ca28d6Saliguori { 1451c6ca28d6Saliguori BlockDriverState *bs; 1452c6ca28d6Saliguori 14531b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1454c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 14551b7bdbc1SStefan Hajnoczi (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { 1456c6ca28d6Saliguori bdrv_flush(bs); 1457c6ca28d6Saliguori } 14581b7bdbc1SStefan Hajnoczi } 14591b7bdbc1SStefan Hajnoczi } 1460c6ca28d6Saliguori 1461f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 1462f2feebbdSKevin Wolf { 1463f2feebbdSKevin Wolf assert(bs->drv); 1464f2feebbdSKevin Wolf 1465f2feebbdSKevin Wolf if (bs->drv->no_zero_init) { 1466f2feebbdSKevin Wolf return 0; 1467f2feebbdSKevin Wolf } else if (bs->file) { 1468f2feebbdSKevin Wolf return bdrv_has_zero_init(bs->file); 1469f2feebbdSKevin Wolf } 1470f2feebbdSKevin Wolf 1471f2feebbdSKevin Wolf return 1; 1472f2feebbdSKevin Wolf } 1473f2feebbdSKevin Wolf 1474f58c7b35Sths /* 1475f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1476f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1477f58c7b35Sths * hence all their sectors are reported as allocated. 1478f58c7b35Sths * 1479f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1480f58c7b35Sths * the specified sector) that are known to be in the same 1481f58c7b35Sths * allocated/unallocated state. 1482f58c7b35Sths * 1483f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1484f58c7b35Sths */ 1485f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1486f58c7b35Sths int *pnum) 1487f58c7b35Sths { 1488f58c7b35Sths int64_t n; 1489f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1490f58c7b35Sths if (sector_num >= bs->total_sectors) { 1491f58c7b35Sths *pnum = 0; 1492f58c7b35Sths return 0; 1493f58c7b35Sths } 1494f58c7b35Sths n = bs->total_sectors - sector_num; 1495f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1496f58c7b35Sths return 1; 1497f58c7b35Sths } 1498f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1499f58c7b35Sths } 1500f58c7b35Sths 15012582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv, 15022582bfedSLuiz Capitulino BlockMonEventAction action, int is_read) 15032582bfedSLuiz Capitulino { 15042582bfedSLuiz Capitulino QObject *data; 15052582bfedSLuiz Capitulino const char *action_str; 15062582bfedSLuiz Capitulino 15072582bfedSLuiz Capitulino switch (action) { 15082582bfedSLuiz Capitulino case BDRV_ACTION_REPORT: 15092582bfedSLuiz Capitulino action_str = "report"; 15102582bfedSLuiz Capitulino break; 15112582bfedSLuiz Capitulino case BDRV_ACTION_IGNORE: 15122582bfedSLuiz Capitulino action_str = "ignore"; 15132582bfedSLuiz Capitulino break; 15142582bfedSLuiz Capitulino case BDRV_ACTION_STOP: 15152582bfedSLuiz Capitulino action_str = "stop"; 15162582bfedSLuiz Capitulino break; 15172582bfedSLuiz Capitulino default: 15182582bfedSLuiz Capitulino abort(); 15192582bfedSLuiz Capitulino } 15202582bfedSLuiz Capitulino 15212582bfedSLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 15222582bfedSLuiz Capitulino bdrv->device_name, 15232582bfedSLuiz Capitulino action_str, 15242582bfedSLuiz Capitulino is_read ? "read" : "write"); 15252582bfedSLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 15262582bfedSLuiz Capitulino 15272582bfedSLuiz Capitulino qobject_decref(data); 15282582bfedSLuiz Capitulino } 15292582bfedSLuiz Capitulino 1530d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque) 1531b338082bSbellard { 1532d15e5465SLuiz Capitulino QDict *bs_dict; 1533d15e5465SLuiz Capitulino Monitor *mon = opaque; 1534b338082bSbellard 1535d15e5465SLuiz Capitulino bs_dict = qobject_to_qdict(obj); 1536d15e5465SLuiz Capitulino 1537d15e5465SLuiz Capitulino monitor_printf(mon, "%s: type=%s removable=%d", 1538d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "device"), 1539d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "type"), 1540d15e5465SLuiz Capitulino qdict_get_bool(bs_dict, "removable")); 1541d15e5465SLuiz Capitulino 1542d15e5465SLuiz Capitulino if (qdict_get_bool(bs_dict, "removable")) { 1543d15e5465SLuiz Capitulino monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); 1544b338082bSbellard } 1545d15e5465SLuiz Capitulino 1546d15e5465SLuiz Capitulino if (qdict_haskey(bs_dict, "inserted")) { 1547d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); 1548d15e5465SLuiz Capitulino 1549376253ecSaliguori monitor_printf(mon, " file="); 1550d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "file")); 1551d15e5465SLuiz Capitulino if (qdict_haskey(qdict, "backing_file")) { 1552376253ecSaliguori monitor_printf(mon, " backing_file="); 1553d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); 1554fef30743Sths } 1555d15e5465SLuiz Capitulino monitor_printf(mon, " ro=%d drv=%s encrypted=%d", 1556d15e5465SLuiz Capitulino qdict_get_bool(qdict, "ro"), 1557d15e5465SLuiz Capitulino qdict_get_str(qdict, "drv"), 1558d15e5465SLuiz Capitulino qdict_get_bool(qdict, "encrypted")); 1559b338082bSbellard } else { 1560376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1561b338082bSbellard } 1562d15e5465SLuiz Capitulino 1563376253ecSaliguori monitor_printf(mon, "\n"); 1564b338082bSbellard } 1565d15e5465SLuiz Capitulino 1566d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data) 1567d15e5465SLuiz Capitulino { 1568d15e5465SLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); 1569d15e5465SLuiz Capitulino } 1570d15e5465SLuiz Capitulino 1571d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data) 1572d15e5465SLuiz Capitulino { 1573d15e5465SLuiz Capitulino QList *bs_list; 1574d15e5465SLuiz Capitulino BlockDriverState *bs; 1575d15e5465SLuiz Capitulino 1576d15e5465SLuiz Capitulino bs_list = qlist_new(); 1577d15e5465SLuiz Capitulino 15781b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1579d15e5465SLuiz Capitulino QObject *bs_obj; 1580d15e5465SLuiz Capitulino const char *type = "unknown"; 1581d15e5465SLuiz Capitulino 1582d15e5465SLuiz Capitulino switch(bs->type) { 1583d15e5465SLuiz Capitulino case BDRV_TYPE_HD: 1584d15e5465SLuiz Capitulino type = "hd"; 1585d15e5465SLuiz Capitulino break; 1586d15e5465SLuiz Capitulino case BDRV_TYPE_CDROM: 1587d15e5465SLuiz Capitulino type = "cdrom"; 1588d15e5465SLuiz Capitulino break; 1589d15e5465SLuiz Capitulino case BDRV_TYPE_FLOPPY: 1590d15e5465SLuiz Capitulino type = "floppy"; 1591d15e5465SLuiz Capitulino break; 1592d15e5465SLuiz Capitulino } 1593d15e5465SLuiz Capitulino 1594d15e5465SLuiz Capitulino bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, " 1595d15e5465SLuiz Capitulino "'removable': %i, 'locked': %i }", 1596d15e5465SLuiz Capitulino bs->device_name, type, bs->removable, 1597d15e5465SLuiz Capitulino bs->locked); 1598d15e5465SLuiz Capitulino 1599d15e5465SLuiz Capitulino if (bs->drv) { 1600d15e5465SLuiz Capitulino QObject *obj; 1601d15e5465SLuiz Capitulino QDict *bs_dict = qobject_to_qdict(bs_obj); 1602d15e5465SLuiz Capitulino 1603d15e5465SLuiz Capitulino obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " 1604d15e5465SLuiz Capitulino "'encrypted': %i }", 1605d15e5465SLuiz Capitulino bs->filename, bs->read_only, 1606d15e5465SLuiz Capitulino bs->drv->format_name, 1607d15e5465SLuiz Capitulino bdrv_is_encrypted(bs)); 1608d15e5465SLuiz Capitulino if (bs->backing_file[0] != '\0') { 1609d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(obj); 1610d15e5465SLuiz Capitulino qdict_put(qdict, "backing_file", 1611d15e5465SLuiz Capitulino qstring_from_str(bs->backing_file)); 1612d15e5465SLuiz Capitulino } 1613d15e5465SLuiz Capitulino 1614d15e5465SLuiz Capitulino qdict_put_obj(bs_dict, "inserted", obj); 1615d15e5465SLuiz Capitulino } 1616d15e5465SLuiz Capitulino qlist_append_obj(bs_list, bs_obj); 1617d15e5465SLuiz Capitulino } 1618d15e5465SLuiz Capitulino 1619d15e5465SLuiz Capitulino *ret_data = QOBJECT(bs_list); 1620b338082bSbellard } 1621a36e69ddSths 1622218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque) 1623a36e69ddSths { 1624218a536aSLuiz Capitulino QDict *qdict; 1625218a536aSLuiz Capitulino Monitor *mon = opaque; 1626218a536aSLuiz Capitulino 1627218a536aSLuiz Capitulino qdict = qobject_to_qdict(data); 1628218a536aSLuiz Capitulino monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); 1629218a536aSLuiz Capitulino 1630218a536aSLuiz Capitulino qdict = qobject_to_qdict(qdict_get(qdict, "stats")); 1631218a536aSLuiz Capitulino monitor_printf(mon, " rd_bytes=%" PRId64 1632218a536aSLuiz Capitulino " wr_bytes=%" PRId64 1633218a536aSLuiz Capitulino " rd_operations=%" PRId64 1634218a536aSLuiz Capitulino " wr_operations=%" PRId64 1635218a536aSLuiz Capitulino "\n", 1636218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_bytes"), 1637218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_bytes"), 1638218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_operations"), 1639218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_operations")); 1640218a536aSLuiz Capitulino } 1641218a536aSLuiz Capitulino 1642218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data) 1643218a536aSLuiz Capitulino { 1644218a536aSLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); 1645218a536aSLuiz Capitulino } 1646218a536aSLuiz Capitulino 1647294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs) 1648294cc35fSKevin Wolf { 1649294cc35fSKevin Wolf QObject *res; 1650294cc35fSKevin Wolf QDict *dict; 1651294cc35fSKevin Wolf 1652294cc35fSKevin Wolf res = qobject_from_jsonf("{ 'stats': {" 1653294cc35fSKevin Wolf "'rd_bytes': %" PRId64 "," 1654294cc35fSKevin Wolf "'wr_bytes': %" PRId64 "," 1655294cc35fSKevin Wolf "'rd_operations': %" PRId64 "," 1656294cc35fSKevin Wolf "'wr_operations': %" PRId64 "," 1657294cc35fSKevin Wolf "'wr_highest_offset': %" PRId64 1658294cc35fSKevin Wolf "} }", 1659294cc35fSKevin Wolf bs->rd_bytes, bs->wr_bytes, 1660294cc35fSKevin Wolf bs->rd_ops, bs->wr_ops, 16615ffbbc67SBlue Swirl bs->wr_highest_sector * 16625ffbbc67SBlue Swirl (uint64_t)BDRV_SECTOR_SIZE); 1663294cc35fSKevin Wolf dict = qobject_to_qdict(res); 1664294cc35fSKevin Wolf 1665294cc35fSKevin Wolf if (*bs->device_name) { 1666294cc35fSKevin Wolf qdict_put(dict, "device", qstring_from_str(bs->device_name)); 1667294cc35fSKevin Wolf } 1668294cc35fSKevin Wolf 1669294cc35fSKevin Wolf if (bs->file) { 1670294cc35fSKevin Wolf QObject *parent = bdrv_info_stats_bs(bs->file); 1671294cc35fSKevin Wolf qdict_put_obj(dict, "parent", parent); 1672294cc35fSKevin Wolf } 1673294cc35fSKevin Wolf 1674294cc35fSKevin Wolf return res; 1675294cc35fSKevin Wolf } 1676294cc35fSKevin Wolf 1677218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data) 1678218a536aSLuiz Capitulino { 1679218a536aSLuiz Capitulino QObject *obj; 1680218a536aSLuiz Capitulino QList *devices; 1681a36e69ddSths BlockDriverState *bs; 1682a36e69ddSths 1683218a536aSLuiz Capitulino devices = qlist_new(); 1684218a536aSLuiz Capitulino 16851b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1686294cc35fSKevin Wolf obj = bdrv_info_stats_bs(bs); 1687218a536aSLuiz Capitulino qlist_append_obj(devices, obj); 1688a36e69ddSths } 1689218a536aSLuiz Capitulino 1690218a536aSLuiz Capitulino *ret_data = QOBJECT(devices); 1691a36e69ddSths } 1692ea2384d3Sbellard 1693045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1694045df330Saliguori { 1695045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1696045df330Saliguori return bs->backing_file; 1697045df330Saliguori else if (bs->encrypted) 1698045df330Saliguori return bs->filename; 1699045df330Saliguori else 1700045df330Saliguori return NULL; 1701045df330Saliguori } 1702045df330Saliguori 170383f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 170483f64091Sbellard char *filename, int filename_size) 170583f64091Sbellard { 1706b783e409SKevin Wolf if (!bs->backing_file) { 170783f64091Sbellard pstrcpy(filename, filename_size, ""); 170883f64091Sbellard } else { 170983f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 171083f64091Sbellard } 171183f64091Sbellard } 171283f64091Sbellard 1713faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1714faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1715faea38e7Sbellard { 1716faea38e7Sbellard BlockDriver *drv = bs->drv; 1717faea38e7Sbellard if (!drv) 171819cb3738Sbellard return -ENOMEDIUM; 1719faea38e7Sbellard if (!drv->bdrv_write_compressed) 1720faea38e7Sbellard return -ENOTSUP; 1721fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 1722fbb7b4e0SKevin Wolf return -EIO; 17237cd1e32aSlirans@il.ibm.com 1724c6d22830SJan Kiszka if (bs->dirty_bitmap) { 17257cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 17267cd1e32aSlirans@il.ibm.com } 17277cd1e32aSlirans@il.ibm.com 1728faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1729faea38e7Sbellard } 1730faea38e7Sbellard 1731faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1732faea38e7Sbellard { 1733faea38e7Sbellard BlockDriver *drv = bs->drv; 1734faea38e7Sbellard if (!drv) 173519cb3738Sbellard return -ENOMEDIUM; 1736faea38e7Sbellard if (!drv->bdrv_get_info) 1737faea38e7Sbellard return -ENOTSUP; 1738faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1739faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1740faea38e7Sbellard } 1741faea38e7Sbellard 174245566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 174345566e9cSChristoph Hellwig int64_t pos, int size) 1744178e08a5Saliguori { 1745178e08a5Saliguori BlockDriver *drv = bs->drv; 1746178e08a5Saliguori if (!drv) 1747178e08a5Saliguori return -ENOMEDIUM; 17487cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 174945566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 17507cdb1f6dSMORITA Kazutaka if (bs->file) 17517cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 17527cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1753178e08a5Saliguori } 1754178e08a5Saliguori 175545566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 175645566e9cSChristoph Hellwig int64_t pos, int size) 1757178e08a5Saliguori { 1758178e08a5Saliguori BlockDriver *drv = bs->drv; 1759178e08a5Saliguori if (!drv) 1760178e08a5Saliguori return -ENOMEDIUM; 17617cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 176245566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 17637cdb1f6dSMORITA Kazutaka if (bs->file) 17647cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 17657cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1766178e08a5Saliguori } 1767178e08a5Saliguori 17688b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 17698b9b0cc2SKevin Wolf { 17708b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 17718b9b0cc2SKevin Wolf 17728b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 17738b9b0cc2SKevin Wolf return; 17748b9b0cc2SKevin Wolf } 17758b9b0cc2SKevin Wolf 17768b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 17778b9b0cc2SKevin Wolf 17788b9b0cc2SKevin Wolf } 17798b9b0cc2SKevin Wolf 1780faea38e7Sbellard /**************************************************************/ 1781faea38e7Sbellard /* handling of snapshots */ 1782faea38e7Sbellard 1783feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 1784feeee5acSMiguel Di Ciurcio Filho { 1785feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 1786feeee5acSMiguel Di Ciurcio Filho if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) { 1787feeee5acSMiguel Di Ciurcio Filho return 0; 1788feeee5acSMiguel Di Ciurcio Filho } 1789feeee5acSMiguel Di Ciurcio Filho 1790feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 1791feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 1792feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 1793feeee5acSMiguel Di Ciurcio Filho } 1794feeee5acSMiguel Di Ciurcio Filho return 0; 1795feeee5acSMiguel Di Ciurcio Filho } 1796feeee5acSMiguel Di Ciurcio Filho 1797feeee5acSMiguel Di Ciurcio Filho return 1; 1798feeee5acSMiguel Di Ciurcio Filho } 1799feeee5acSMiguel Di Ciurcio Filho 1800f9092b10SMarkus Armbruster BlockDriverState *bdrv_snapshots(void) 1801f9092b10SMarkus Armbruster { 1802f9092b10SMarkus Armbruster BlockDriverState *bs; 1803f9092b10SMarkus Armbruster 18043ac906f7SMarkus Armbruster if (bs_snapshots) { 1805f9092b10SMarkus Armbruster return bs_snapshots; 18063ac906f7SMarkus Armbruster } 1807f9092b10SMarkus Armbruster 1808f9092b10SMarkus Armbruster bs = NULL; 1809f9092b10SMarkus Armbruster while ((bs = bdrv_next(bs))) { 1810f9092b10SMarkus Armbruster if (bdrv_can_snapshot(bs)) { 18113ac906f7SMarkus Armbruster bs_snapshots = bs; 18123ac906f7SMarkus Armbruster return bs; 1813f9092b10SMarkus Armbruster } 1814f9092b10SMarkus Armbruster } 1815f9092b10SMarkus Armbruster return NULL; 1816f9092b10SMarkus Armbruster } 1817f9092b10SMarkus Armbruster 1818faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1819faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1820faea38e7Sbellard { 1821faea38e7Sbellard BlockDriver *drv = bs->drv; 1822faea38e7Sbellard if (!drv) 182319cb3738Sbellard return -ENOMEDIUM; 18247cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 1825faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 18267cdb1f6dSMORITA Kazutaka if (bs->file) 18277cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 18287cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1829faea38e7Sbellard } 1830faea38e7Sbellard 1831faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1832faea38e7Sbellard const char *snapshot_id) 1833faea38e7Sbellard { 1834faea38e7Sbellard BlockDriver *drv = bs->drv; 18357cdb1f6dSMORITA Kazutaka int ret, open_ret; 18367cdb1f6dSMORITA Kazutaka 1837faea38e7Sbellard if (!drv) 183819cb3738Sbellard return -ENOMEDIUM; 18397cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 1840faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 18417cdb1f6dSMORITA Kazutaka 18427cdb1f6dSMORITA Kazutaka if (bs->file) { 18437cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 18447cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 18457cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 18467cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 18477cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 18487cdb1f6dSMORITA Kazutaka bs->drv = NULL; 18497cdb1f6dSMORITA Kazutaka return open_ret; 18507cdb1f6dSMORITA Kazutaka } 18517cdb1f6dSMORITA Kazutaka return ret; 18527cdb1f6dSMORITA Kazutaka } 18537cdb1f6dSMORITA Kazutaka 18547cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1855faea38e7Sbellard } 1856faea38e7Sbellard 1857faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1858faea38e7Sbellard { 1859faea38e7Sbellard BlockDriver *drv = bs->drv; 1860faea38e7Sbellard if (!drv) 186119cb3738Sbellard return -ENOMEDIUM; 18627cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 1863faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 18647cdb1f6dSMORITA Kazutaka if (bs->file) 18657cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 18667cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1867faea38e7Sbellard } 1868faea38e7Sbellard 1869faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1870faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1871faea38e7Sbellard { 1872faea38e7Sbellard BlockDriver *drv = bs->drv; 1873faea38e7Sbellard if (!drv) 187419cb3738Sbellard return -ENOMEDIUM; 18757cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 1876faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 18777cdb1f6dSMORITA Kazutaka if (bs->file) 18787cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 18797cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1880faea38e7Sbellard } 1881faea38e7Sbellard 1882faea38e7Sbellard #define NB_SUFFIXES 4 1883faea38e7Sbellard 1884faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1885faea38e7Sbellard { 1886faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1887faea38e7Sbellard int64_t base; 1888faea38e7Sbellard int i; 1889faea38e7Sbellard 1890faea38e7Sbellard if (size <= 999) { 1891faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1892faea38e7Sbellard } else { 1893faea38e7Sbellard base = 1024; 1894faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1895faea38e7Sbellard if (size < (10 * base)) { 1896faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1897faea38e7Sbellard (double)size / base, 1898faea38e7Sbellard suffixes[i]); 1899faea38e7Sbellard break; 1900faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1901faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1902faea38e7Sbellard ((size + (base >> 1)) / base), 1903faea38e7Sbellard suffixes[i]); 1904faea38e7Sbellard break; 1905faea38e7Sbellard } 1906faea38e7Sbellard base = base * 1024; 1907faea38e7Sbellard } 1908faea38e7Sbellard } 1909faea38e7Sbellard return buf; 1910faea38e7Sbellard } 1911faea38e7Sbellard 1912faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1913faea38e7Sbellard { 1914faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 19153b9f94e1Sbellard #ifdef _WIN32 19163b9f94e1Sbellard struct tm *ptm; 19173b9f94e1Sbellard #else 1918faea38e7Sbellard struct tm tm; 19193b9f94e1Sbellard #endif 1920faea38e7Sbellard time_t ti; 1921faea38e7Sbellard int64_t secs; 1922faea38e7Sbellard 1923faea38e7Sbellard if (!sn) { 1924faea38e7Sbellard snprintf(buf, buf_size, 1925faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1926faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1927faea38e7Sbellard } else { 1928faea38e7Sbellard ti = sn->date_sec; 19293b9f94e1Sbellard #ifdef _WIN32 19303b9f94e1Sbellard ptm = localtime(&ti); 19313b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 19323b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 19333b9f94e1Sbellard #else 1934faea38e7Sbellard localtime_r(&ti, &tm); 1935faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1936faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 19373b9f94e1Sbellard #endif 1938faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1939faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1940faea38e7Sbellard "%02d:%02d:%02d.%03d", 1941faea38e7Sbellard (int)(secs / 3600), 1942faea38e7Sbellard (int)((secs / 60) % 60), 1943faea38e7Sbellard (int)(secs % 60), 1944faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1945faea38e7Sbellard snprintf(buf, buf_size, 1946faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1947faea38e7Sbellard sn->id_str, sn->name, 1948faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1949faea38e7Sbellard date_buf, 1950faea38e7Sbellard clock_buf); 1951faea38e7Sbellard } 1952faea38e7Sbellard return buf; 1953faea38e7Sbellard } 1954faea38e7Sbellard 195583f64091Sbellard 1956ea2384d3Sbellard /**************************************************************/ 195783f64091Sbellard /* async I/Os */ 1958ea2384d3Sbellard 19593b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 1960f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 196183f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1962ea2384d3Sbellard { 196383f64091Sbellard BlockDriver *drv = bs->drv; 1964a36e69ddSths BlockDriverAIOCB *ret; 1965ea2384d3Sbellard 196619cb3738Sbellard if (!drv) 1967ce1a14dcSpbrook return NULL; 196871d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 196971d0770cSaliguori return NULL; 197083f64091Sbellard 1971f141eafeSaliguori ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, 1972f141eafeSaliguori cb, opaque); 1973a36e69ddSths 1974a36e69ddSths if (ret) { 1975a36e69ddSths /* Update stats even though technically transfer has not happened. */ 19766ea44308SJan Kiszka bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 1977a36e69ddSths bs->rd_ops ++; 1978a36e69ddSths } 1979a36e69ddSths 1980a36e69ddSths return ret; 198183f64091Sbellard } 198283f64091Sbellard 1983f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 1984f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 198583f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 19867674e7bfSbellard { 198783f64091Sbellard BlockDriver *drv = bs->drv; 1988a36e69ddSths BlockDriverAIOCB *ret; 198983f64091Sbellard 199019cb3738Sbellard if (!drv) 1991ce1a14dcSpbrook return NULL; 199283f64091Sbellard if (bs->read_only) 1993ce1a14dcSpbrook return NULL; 199471d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 199571d0770cSaliguori return NULL; 199683f64091Sbellard 1997c6d22830SJan Kiszka if (bs->dirty_bitmap) { 19987cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 19997cd1e32aSlirans@il.ibm.com } 20007cd1e32aSlirans@il.ibm.com 2001f141eafeSaliguori ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, 2002f141eafeSaliguori cb, opaque); 2003a36e69ddSths 2004a36e69ddSths if (ret) { 2005a36e69ddSths /* Update stats even though technically transfer has not happened. */ 20066ea44308SJan Kiszka bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 2007a36e69ddSths bs->wr_ops ++; 2008294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 2009294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 2010294cc35fSKevin Wolf } 2011a36e69ddSths } 2012a36e69ddSths 2013a36e69ddSths return ret; 201483f64091Sbellard } 201583f64091Sbellard 201640b4f539SKevin Wolf 201740b4f539SKevin Wolf typedef struct MultiwriteCB { 201840b4f539SKevin Wolf int error; 201940b4f539SKevin Wolf int num_requests; 202040b4f539SKevin Wolf int num_callbacks; 202140b4f539SKevin Wolf struct { 202240b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 202340b4f539SKevin Wolf void *opaque; 202440b4f539SKevin Wolf QEMUIOVector *free_qiov; 202540b4f539SKevin Wolf void *free_buf; 202640b4f539SKevin Wolf } callbacks[]; 202740b4f539SKevin Wolf } MultiwriteCB; 202840b4f539SKevin Wolf 202940b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 203040b4f539SKevin Wolf { 203140b4f539SKevin Wolf int i; 203240b4f539SKevin Wolf 203340b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 203440b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 20351e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 20361e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 20371e1ea48dSStefan Hajnoczi } 203840b4f539SKevin Wolf qemu_free(mcb->callbacks[i].free_qiov); 2039f8a83245SHerve Poussineau qemu_vfree(mcb->callbacks[i].free_buf); 204040b4f539SKevin Wolf } 204140b4f539SKevin Wolf } 204240b4f539SKevin Wolf 204340b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 204440b4f539SKevin Wolf { 204540b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 204640b4f539SKevin Wolf 2047cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 204840b4f539SKevin Wolf mcb->error = ret; 204940b4f539SKevin Wolf } 205040b4f539SKevin Wolf 205140b4f539SKevin Wolf mcb->num_requests--; 205240b4f539SKevin Wolf if (mcb->num_requests == 0) { 205340b4f539SKevin Wolf multiwrite_user_cb(mcb); 205440b4f539SKevin Wolf qemu_free(mcb); 205540b4f539SKevin Wolf } 205640b4f539SKevin Wolf } 205740b4f539SKevin Wolf 205840b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 205940b4f539SKevin Wolf { 206077be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 206177be4366SChristoph Hellwig 206277be4366SChristoph Hellwig /* 206377be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 206477be4366SChristoph Hellwig * here as that could overflow the return value. 206577be4366SChristoph Hellwig */ 206677be4366SChristoph Hellwig if (req1->sector > req2->sector) { 206777be4366SChristoph Hellwig return 1; 206877be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 206977be4366SChristoph Hellwig return -1; 207077be4366SChristoph Hellwig } else { 207177be4366SChristoph Hellwig return 0; 207277be4366SChristoph Hellwig } 207340b4f539SKevin Wolf } 207440b4f539SKevin Wolf 207540b4f539SKevin Wolf /* 207640b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 207740b4f539SKevin Wolf * requests that remain after merging. 207840b4f539SKevin Wolf */ 207940b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 208040b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 208140b4f539SKevin Wolf { 208240b4f539SKevin Wolf int i, outidx; 208340b4f539SKevin Wolf 208440b4f539SKevin Wolf // Sort requests by start sector 208540b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 208640b4f539SKevin Wolf 208740b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 208840b4f539SKevin Wolf // filling up gaps with zero sectors. 208940b4f539SKevin Wolf outidx = 0; 209040b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 209140b4f539SKevin Wolf int merge = 0; 209240b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 209340b4f539SKevin Wolf 209440b4f539SKevin Wolf // This handles the cases that are valid for all block drivers, namely 209540b4f539SKevin Wolf // exactly sequential writes and overlapping writes. 209640b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 209740b4f539SKevin Wolf merge = 1; 209840b4f539SKevin Wolf } 209940b4f539SKevin Wolf 210040b4f539SKevin Wolf // The block driver may decide that it makes sense to combine requests 210140b4f539SKevin Wolf // even if there is a gap of some sectors between them. In this case, 210240b4f539SKevin Wolf // the gap is filled with zeros (therefore only applicable for yet 210340b4f539SKevin Wolf // unused space in format like qcow2). 210440b4f539SKevin Wolf if (!merge && bs->drv->bdrv_merge_requests) { 210540b4f539SKevin Wolf merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); 210640b4f539SKevin Wolf } 210740b4f539SKevin Wolf 2108e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 2109e2a305fbSChristoph Hellwig merge = 0; 2110e2a305fbSChristoph Hellwig } 2111e2a305fbSChristoph Hellwig 211240b4f539SKevin Wolf if (merge) { 211340b4f539SKevin Wolf size_t size; 211440b4f539SKevin Wolf QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); 211540b4f539SKevin Wolf qemu_iovec_init(qiov, 211640b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 211740b4f539SKevin Wolf 211840b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 211940b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 212040b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 212140b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 212240b4f539SKevin Wolf 212340b4f539SKevin Wolf // We might need to add some zeros between the two requests 212440b4f539SKevin Wolf if (reqs[i].sector > oldreq_last) { 212540b4f539SKevin Wolf size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; 212640b4f539SKevin Wolf uint8_t *buf = qemu_blockalign(bs, zero_bytes); 212740b4f539SKevin Wolf memset(buf, 0, zero_bytes); 212840b4f539SKevin Wolf qemu_iovec_add(qiov, buf, zero_bytes); 212940b4f539SKevin Wolf mcb->callbacks[i].free_buf = buf; 213040b4f539SKevin Wolf } 213140b4f539SKevin Wolf 213240b4f539SKevin Wolf // Add the second request 213340b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 213440b4f539SKevin Wolf 2135cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 213640b4f539SKevin Wolf reqs[outidx].qiov = qiov; 213740b4f539SKevin Wolf 213840b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 213940b4f539SKevin Wolf } else { 214040b4f539SKevin Wolf outidx++; 214140b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 214240b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 214340b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 214440b4f539SKevin Wolf } 214540b4f539SKevin Wolf } 214640b4f539SKevin Wolf 214740b4f539SKevin Wolf return outidx + 1; 214840b4f539SKevin Wolf } 214940b4f539SKevin Wolf 215040b4f539SKevin Wolf /* 215140b4f539SKevin Wolf * Submit multiple AIO write requests at once. 215240b4f539SKevin Wolf * 215340b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 215440b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 215540b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 215640b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 215740b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 215840b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 215940b4f539SKevin Wolf * 216040b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 216140b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 216240b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 216340b4f539SKevin Wolf */ 216440b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 216540b4f539SKevin Wolf { 216640b4f539SKevin Wolf BlockDriverAIOCB *acb; 216740b4f539SKevin Wolf MultiwriteCB *mcb; 216840b4f539SKevin Wolf int i; 216940b4f539SKevin Wolf 217040b4f539SKevin Wolf if (num_reqs == 0) { 217140b4f539SKevin Wolf return 0; 217240b4f539SKevin Wolf } 217340b4f539SKevin Wolf 217440b4f539SKevin Wolf // Create MultiwriteCB structure 217540b4f539SKevin Wolf mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 217640b4f539SKevin Wolf mcb->num_requests = 0; 217740b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 217840b4f539SKevin Wolf 217940b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 218040b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 218140b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 218240b4f539SKevin Wolf } 218340b4f539SKevin Wolf 218440b4f539SKevin Wolf // Check for mergable requests 218540b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 218640b4f539SKevin Wolf 2187453f9a16SKevin Wolf /* 2188453f9a16SKevin Wolf * Run the aio requests. As soon as one request can't be submitted 2189453f9a16SKevin Wolf * successfully, fail all requests that are not yet submitted (we must 2190453f9a16SKevin Wolf * return failure for all requests anyway) 2191453f9a16SKevin Wolf * 2192453f9a16SKevin Wolf * num_requests cannot be set to the right value immediately: If 2193453f9a16SKevin Wolf * bdrv_aio_writev fails for some request, num_requests would be too high 2194453f9a16SKevin Wolf * and therefore multiwrite_cb() would never recognize the multiwrite 2195453f9a16SKevin Wolf * request as completed. We also cannot use the loop variable i to set it 2196453f9a16SKevin Wolf * when the first request fails because the callback may already have been 2197453f9a16SKevin Wolf * called for previously submitted requests. Thus, num_requests must be 2198453f9a16SKevin Wolf * incremented for each request that is submitted. 2199453f9a16SKevin Wolf * 2200453f9a16SKevin Wolf * The problem that callbacks may be called early also means that we need 2201453f9a16SKevin Wolf * to take care that num_requests doesn't become 0 before all requests are 2202453f9a16SKevin Wolf * submitted - multiwrite_cb() would consider the multiwrite request 2203453f9a16SKevin Wolf * completed. A dummy request that is "completed" by a manual call to 2204453f9a16SKevin Wolf * multiwrite_cb() takes care of this. 2205453f9a16SKevin Wolf */ 2206453f9a16SKevin Wolf mcb->num_requests = 1; 2207453f9a16SKevin Wolf 220840b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 2209453f9a16SKevin Wolf mcb->num_requests++; 221040b4f539SKevin Wolf acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 221140b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 221240b4f539SKevin Wolf 221340b4f539SKevin Wolf if (acb == NULL) { 221440b4f539SKevin Wolf // We can only fail the whole thing if no request has been 221540b4f539SKevin Wolf // submitted yet. Otherwise we'll wait for the submitted AIOs to 221640b4f539SKevin Wolf // complete and report the error in the callback. 2217453f9a16SKevin Wolf if (i == 0) { 221840b4f539SKevin Wolf goto fail; 221940b4f539SKevin Wolf } else { 22207eb58a6cSKevin Wolf multiwrite_cb(mcb, -EIO); 222140b4f539SKevin Wolf break; 222240b4f539SKevin Wolf } 222340b4f539SKevin Wolf } 222440b4f539SKevin Wolf } 222540b4f539SKevin Wolf 2226453f9a16SKevin Wolf /* Complete the dummy request */ 2227453f9a16SKevin Wolf multiwrite_cb(mcb, 0); 2228453f9a16SKevin Wolf 222940b4f539SKevin Wolf return 0; 223040b4f539SKevin Wolf 223140b4f539SKevin Wolf fail: 2232453f9a16SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 2233453f9a16SKevin Wolf reqs[i].error = -EIO; 2234453f9a16SKevin Wolf } 2235af474591SBruce Rogers qemu_free(mcb); 223640b4f539SKevin Wolf return -1; 223740b4f539SKevin Wolf } 223840b4f539SKevin Wolf 2239b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 2240b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2241b2e12bc6SChristoph Hellwig { 2242b2e12bc6SChristoph Hellwig BlockDriver *drv = bs->drv; 2243b2e12bc6SChristoph Hellwig 2244016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 2245016f5cf6SAlexander Graf return bdrv_aio_noop_em(bs, cb, opaque); 2246016f5cf6SAlexander Graf } 2247016f5cf6SAlexander Graf 2248b2e12bc6SChristoph Hellwig if (!drv) 2249b2e12bc6SChristoph Hellwig return NULL; 2250b2e12bc6SChristoph Hellwig return drv->bdrv_aio_flush(bs, cb, opaque); 2251b2e12bc6SChristoph Hellwig } 2252b2e12bc6SChristoph Hellwig 225383f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 225483f64091Sbellard { 22556bbff9a0Saliguori acb->pool->cancel(acb); 225683f64091Sbellard } 225783f64091Sbellard 225883f64091Sbellard 225983f64091Sbellard /**************************************************************/ 226083f64091Sbellard /* async block device emulation */ 226183f64091Sbellard 2262c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 2263c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 2264c16b5a2cSChristoph Hellwig QEMUBH *bh; 2265c16b5a2cSChristoph Hellwig int ret; 2266c16b5a2cSChristoph Hellwig /* vector translation state */ 2267c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 2268c16b5a2cSChristoph Hellwig uint8_t *bounce; 2269c16b5a2cSChristoph Hellwig int is_write; 2270c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 2271c16b5a2cSChristoph Hellwig 2272c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 2273c16b5a2cSChristoph Hellwig { 2274b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 2275b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 22766a7ad299SDor Laor qemu_bh_delete(acb->bh); 227736afc451SAvi Kivity acb->bh = NULL; 2278c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 2279c16b5a2cSChristoph Hellwig } 2280c16b5a2cSChristoph Hellwig 2281c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 2282c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 2283c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 2284c16b5a2cSChristoph Hellwig }; 2285c16b5a2cSChristoph Hellwig 228683f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 2287beac80cdSbellard { 2288ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 2289f141eafeSaliguori 2290f141eafeSaliguori if (!acb->is_write) 2291f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 2292ceb42de8Saliguori qemu_vfree(acb->bounce); 2293ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 22946a7ad299SDor Laor qemu_bh_delete(acb->bh); 229536afc451SAvi Kivity acb->bh = NULL; 2296ce1a14dcSpbrook qemu_aio_release(acb); 2297beac80cdSbellard } 2298beac80cdSbellard 2299f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 2300f141eafeSaliguori int64_t sector_num, 2301f141eafeSaliguori QEMUIOVector *qiov, 2302f141eafeSaliguori int nb_sectors, 2303f141eafeSaliguori BlockDriverCompletionFunc *cb, 2304f141eafeSaliguori void *opaque, 2305f141eafeSaliguori int is_write) 2306f141eafeSaliguori 2307ea2384d3Sbellard { 2308ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 230983f64091Sbellard 2310c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2311f141eafeSaliguori acb->is_write = is_write; 2312f141eafeSaliguori acb->qiov = qiov; 2313e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 2314f141eafeSaliguori 2315ce1a14dcSpbrook if (!acb->bh) 2316ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2317f141eafeSaliguori 2318f141eafeSaliguori if (is_write) { 2319f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 2320f141eafeSaliguori acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 2321f141eafeSaliguori } else { 2322f141eafeSaliguori acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 2323f141eafeSaliguori } 2324f141eafeSaliguori 2325ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 2326f141eafeSaliguori 2327ce1a14dcSpbrook return &acb->common; 23287a6cba61Spbrook } 23297a6cba61Spbrook 2330f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 2331f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2332ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 233383f64091Sbellard { 2334f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 233583f64091Sbellard } 233683f64091Sbellard 2337f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 2338f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2339f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 2340f141eafeSaliguori { 2341f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 2342f141eafeSaliguori } 2343f141eafeSaliguori 2344b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 2345b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2346b2e12bc6SChristoph Hellwig { 2347b2e12bc6SChristoph Hellwig BlockDriverAIOCBSync *acb; 2348b2e12bc6SChristoph Hellwig 2349b2e12bc6SChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2350b2e12bc6SChristoph Hellwig acb->is_write = 1; /* don't bounce in the completion hadler */ 2351b2e12bc6SChristoph Hellwig acb->qiov = NULL; 2352b2e12bc6SChristoph Hellwig acb->bounce = NULL; 2353b2e12bc6SChristoph Hellwig acb->ret = 0; 2354b2e12bc6SChristoph Hellwig 2355b2e12bc6SChristoph Hellwig if (!acb->bh) 2356b2e12bc6SChristoph Hellwig acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2357b2e12bc6SChristoph Hellwig 2358b2e12bc6SChristoph Hellwig bdrv_flush(bs); 2359b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 2360b2e12bc6SChristoph Hellwig return &acb->common; 2361b2e12bc6SChristoph Hellwig } 2362b2e12bc6SChristoph Hellwig 2363016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 2364016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 2365016f5cf6SAlexander Graf { 2366016f5cf6SAlexander Graf BlockDriverAIOCBSync *acb; 2367016f5cf6SAlexander Graf 2368016f5cf6SAlexander Graf acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2369016f5cf6SAlexander Graf acb->is_write = 1; /* don't bounce in the completion handler */ 2370016f5cf6SAlexander Graf acb->qiov = NULL; 2371016f5cf6SAlexander Graf acb->bounce = NULL; 2372016f5cf6SAlexander Graf acb->ret = 0; 2373016f5cf6SAlexander Graf 2374016f5cf6SAlexander Graf if (!acb->bh) { 2375016f5cf6SAlexander Graf acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2376016f5cf6SAlexander Graf } 2377016f5cf6SAlexander Graf 2378016f5cf6SAlexander Graf qemu_bh_schedule(acb->bh); 2379016f5cf6SAlexander Graf return &acb->common; 2380016f5cf6SAlexander Graf } 2381016f5cf6SAlexander Graf 238283f64091Sbellard /**************************************************************/ 238383f64091Sbellard /* sync block device emulation */ 238483f64091Sbellard 238583f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 238683f64091Sbellard { 238783f64091Sbellard *(int *)opaque = ret; 238883f64091Sbellard } 238983f64091Sbellard 239083f64091Sbellard #define NOT_DONE 0x7fffffff 239183f64091Sbellard 239283f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 239383f64091Sbellard uint8_t *buf, int nb_sectors) 239483f64091Sbellard { 2395ce1a14dcSpbrook int async_ret; 2396ce1a14dcSpbrook BlockDriverAIOCB *acb; 2397f141eafeSaliguori struct iovec iov; 2398f141eafeSaliguori QEMUIOVector qiov; 239983f64091Sbellard 240065d6b3d8SKevin Wolf async_context_push(); 240165d6b3d8SKevin Wolf 240283f64091Sbellard async_ret = NOT_DONE; 24033f4cb3d3Sblueswir1 iov.iov_base = (void *)buf; 2404eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2405f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2406f141eafeSaliguori acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, 240783f64091Sbellard bdrv_rw_em_cb, &async_ret); 240865d6b3d8SKevin Wolf if (acb == NULL) { 240965d6b3d8SKevin Wolf async_ret = -1; 241065d6b3d8SKevin Wolf goto fail; 241165d6b3d8SKevin Wolf } 2412baf35cb9Saliguori 241383f64091Sbellard while (async_ret == NOT_DONE) { 241483f64091Sbellard qemu_aio_wait(); 241583f64091Sbellard } 2416baf35cb9Saliguori 241765d6b3d8SKevin Wolf 241865d6b3d8SKevin Wolf fail: 241965d6b3d8SKevin Wolf async_context_pop(); 242083f64091Sbellard return async_ret; 242183f64091Sbellard } 242283f64091Sbellard 242383f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 242483f64091Sbellard const uint8_t *buf, int nb_sectors) 242583f64091Sbellard { 2426ce1a14dcSpbrook int async_ret; 2427ce1a14dcSpbrook BlockDriverAIOCB *acb; 2428f141eafeSaliguori struct iovec iov; 2429f141eafeSaliguori QEMUIOVector qiov; 243083f64091Sbellard 243165d6b3d8SKevin Wolf async_context_push(); 243265d6b3d8SKevin Wolf 243383f64091Sbellard async_ret = NOT_DONE; 2434f141eafeSaliguori iov.iov_base = (void *)buf; 2435eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2436f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2437f141eafeSaliguori acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, 243883f64091Sbellard bdrv_rw_em_cb, &async_ret); 243965d6b3d8SKevin Wolf if (acb == NULL) { 244065d6b3d8SKevin Wolf async_ret = -1; 244165d6b3d8SKevin Wolf goto fail; 244265d6b3d8SKevin Wolf } 244383f64091Sbellard while (async_ret == NOT_DONE) { 244483f64091Sbellard qemu_aio_wait(); 244583f64091Sbellard } 244665d6b3d8SKevin Wolf 244765d6b3d8SKevin Wolf fail: 244865d6b3d8SKevin Wolf async_context_pop(); 244983f64091Sbellard return async_ret; 245083f64091Sbellard } 2451ea2384d3Sbellard 2452ea2384d3Sbellard void bdrv_init(void) 2453ea2384d3Sbellard { 24545efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 2455ea2384d3Sbellard } 2456ce1a14dcSpbrook 2457eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 2458eb852011SMarkus Armbruster { 2459eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 2460eb852011SMarkus Armbruster bdrv_init(); 2461eb852011SMarkus Armbruster } 2462eb852011SMarkus Armbruster 2463c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 24646bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 24656bbff9a0Saliguori { 2466ce1a14dcSpbrook BlockDriverAIOCB *acb; 2467ce1a14dcSpbrook 24686bbff9a0Saliguori if (pool->free_aiocb) { 24696bbff9a0Saliguori acb = pool->free_aiocb; 24706bbff9a0Saliguori pool->free_aiocb = acb->next; 2471ce1a14dcSpbrook } else { 24726bbff9a0Saliguori acb = qemu_mallocz(pool->aiocb_size); 24736bbff9a0Saliguori acb->pool = pool; 2474ce1a14dcSpbrook } 2475ce1a14dcSpbrook acb->bs = bs; 2476ce1a14dcSpbrook acb->cb = cb; 2477ce1a14dcSpbrook acb->opaque = opaque; 2478ce1a14dcSpbrook return acb; 2479ce1a14dcSpbrook } 2480ce1a14dcSpbrook 2481ce1a14dcSpbrook void qemu_aio_release(void *p) 2482ce1a14dcSpbrook { 24836bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 24846bbff9a0Saliguori AIOPool *pool = acb->pool; 24856bbff9a0Saliguori acb->next = pool->free_aiocb; 24866bbff9a0Saliguori pool->free_aiocb = acb; 2487ce1a14dcSpbrook } 248819cb3738Sbellard 248919cb3738Sbellard /**************************************************************/ 249019cb3738Sbellard /* removable device support */ 249119cb3738Sbellard 249219cb3738Sbellard /** 249319cb3738Sbellard * Return TRUE if the media is present 249419cb3738Sbellard */ 249519cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 249619cb3738Sbellard { 249719cb3738Sbellard BlockDriver *drv = bs->drv; 249819cb3738Sbellard int ret; 249919cb3738Sbellard if (!drv) 250019cb3738Sbellard return 0; 250119cb3738Sbellard if (!drv->bdrv_is_inserted) 250219cb3738Sbellard return 1; 250319cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 250419cb3738Sbellard return ret; 250519cb3738Sbellard } 250619cb3738Sbellard 250719cb3738Sbellard /** 250819cb3738Sbellard * Return TRUE if the media changed since the last call to this 250919cb3738Sbellard * function. It is currently only used for floppy disks 251019cb3738Sbellard */ 251119cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 251219cb3738Sbellard { 251319cb3738Sbellard BlockDriver *drv = bs->drv; 251419cb3738Sbellard int ret; 251519cb3738Sbellard 251619cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 251719cb3738Sbellard ret = -ENOTSUP; 251819cb3738Sbellard else 251919cb3738Sbellard ret = drv->bdrv_media_changed(bs); 252019cb3738Sbellard if (ret == -ENOTSUP) 252119cb3738Sbellard ret = bs->media_changed; 252219cb3738Sbellard bs->media_changed = 0; 252319cb3738Sbellard return ret; 252419cb3738Sbellard } 252519cb3738Sbellard 252619cb3738Sbellard /** 252719cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 252819cb3738Sbellard */ 2529aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag) 253019cb3738Sbellard { 253119cb3738Sbellard BlockDriver *drv = bs->drv; 253219cb3738Sbellard int ret; 253319cb3738Sbellard 2534aea2a33cSMark McLoughlin if (bs->locked) { 2535aea2a33cSMark McLoughlin return -EBUSY; 2536aea2a33cSMark McLoughlin } 2537aea2a33cSMark McLoughlin 253819cb3738Sbellard if (!drv || !drv->bdrv_eject) { 253919cb3738Sbellard ret = -ENOTSUP; 254019cb3738Sbellard } else { 254119cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 254219cb3738Sbellard } 254319cb3738Sbellard if (ret == -ENOTSUP) { 254419cb3738Sbellard if (eject_flag) 254519cb3738Sbellard bdrv_close(bs); 2546aea2a33cSMark McLoughlin ret = 0; 254719cb3738Sbellard } 2548aea2a33cSMark McLoughlin 2549aea2a33cSMark McLoughlin return ret; 255019cb3738Sbellard } 255119cb3738Sbellard 255219cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 255319cb3738Sbellard { 255419cb3738Sbellard return bs->locked; 255519cb3738Sbellard } 255619cb3738Sbellard 255719cb3738Sbellard /** 255819cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 255919cb3738Sbellard * to eject it manually). 256019cb3738Sbellard */ 256119cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 256219cb3738Sbellard { 256319cb3738Sbellard BlockDriver *drv = bs->drv; 256419cb3738Sbellard 256519cb3738Sbellard bs->locked = locked; 256619cb3738Sbellard if (drv && drv->bdrv_set_locked) { 256719cb3738Sbellard drv->bdrv_set_locked(bs, locked); 256819cb3738Sbellard } 256919cb3738Sbellard } 2570985a03b0Sths 2571985a03b0Sths /* needed for generic scsi interface */ 2572985a03b0Sths 2573985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 2574985a03b0Sths { 2575985a03b0Sths BlockDriver *drv = bs->drv; 2576985a03b0Sths 2577985a03b0Sths if (drv && drv->bdrv_ioctl) 2578985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 2579985a03b0Sths return -ENOTSUP; 2580985a03b0Sths } 25817d780669Saliguori 2582221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 2583221f715dSaliguori unsigned long int req, void *buf, 25847d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 25857d780669Saliguori { 2586221f715dSaliguori BlockDriver *drv = bs->drv; 25877d780669Saliguori 2588221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 2589221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 2590221f715dSaliguori return NULL; 25917d780669Saliguori } 2592e268ca52Saliguori 25937cd1e32aSlirans@il.ibm.com 25947cd1e32aSlirans@il.ibm.com 2595e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 2596e268ca52Saliguori { 2597e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 2598e268ca52Saliguori } 25997cd1e32aSlirans@il.ibm.com 26007cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 26017cd1e32aSlirans@il.ibm.com { 26027cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 2603a55eb92cSJan Kiszka 2604aaa0eb75SLiran Schour bs->dirty_count = 0; 26057cd1e32aSlirans@il.ibm.com if (enable) { 2606c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 2607c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 2608c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 2609c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 26107cd1e32aSlirans@il.ibm.com 26117cd1e32aSlirans@il.ibm.com bs->dirty_bitmap = qemu_mallocz(bitmap_size); 26127cd1e32aSlirans@il.ibm.com } 26137cd1e32aSlirans@il.ibm.com } else { 2614c6d22830SJan Kiszka if (bs->dirty_bitmap) { 26157cd1e32aSlirans@il.ibm.com qemu_free(bs->dirty_bitmap); 2616c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 26177cd1e32aSlirans@il.ibm.com } 26187cd1e32aSlirans@il.ibm.com } 26197cd1e32aSlirans@il.ibm.com } 26207cd1e32aSlirans@il.ibm.com 26217cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 26227cd1e32aSlirans@il.ibm.com { 26236ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 26247cd1e32aSlirans@il.ibm.com 2625c6d22830SJan Kiszka if (bs->dirty_bitmap && 2626c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 2627c6d22830SJan Kiszka return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 2628c6d22830SJan Kiszka (1 << (chunk % (sizeof(unsigned long) * 8))); 26297cd1e32aSlirans@il.ibm.com } else { 26307cd1e32aSlirans@il.ibm.com return 0; 26317cd1e32aSlirans@il.ibm.com } 26327cd1e32aSlirans@il.ibm.com } 26337cd1e32aSlirans@il.ibm.com 26347cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 26357cd1e32aSlirans@il.ibm.com int nr_sectors) 26367cd1e32aSlirans@il.ibm.com { 26377cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 26387cd1e32aSlirans@il.ibm.com } 2639aaa0eb75SLiran Schour 2640aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 2641aaa0eb75SLiran Schour { 2642aaa0eb75SLiran Schour return bs->dirty_count; 2643aaa0eb75SLiran Schour } 2644