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 66eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 67eb852011SMarkus Armbruster static int use_bdrv_whitelist; 68eb852011SMarkus Armbruster 6983f64091Sbellard int path_is_absolute(const char *path) 7083f64091Sbellard { 7183f64091Sbellard const char *p; 7221664424Sbellard #ifdef _WIN32 7321664424Sbellard /* specific case for names like: "\\.\d:" */ 7421664424Sbellard if (*path == '/' || *path == '\\') 7521664424Sbellard return 1; 7621664424Sbellard #endif 7783f64091Sbellard p = strchr(path, ':'); 7883f64091Sbellard if (p) 7983f64091Sbellard p++; 8083f64091Sbellard else 8183f64091Sbellard p = path; 823b9f94e1Sbellard #ifdef _WIN32 833b9f94e1Sbellard return (*p == '/' || *p == '\\'); 843b9f94e1Sbellard #else 853b9f94e1Sbellard return (*p == '/'); 863b9f94e1Sbellard #endif 8783f64091Sbellard } 8883f64091Sbellard 8983f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 9083f64091Sbellard path to it by considering it is relative to base_path. URL are 9183f64091Sbellard supported. */ 9283f64091Sbellard void path_combine(char *dest, int dest_size, 9383f64091Sbellard const char *base_path, 9483f64091Sbellard const char *filename) 9583f64091Sbellard { 9683f64091Sbellard const char *p, *p1; 9783f64091Sbellard int len; 9883f64091Sbellard 9983f64091Sbellard if (dest_size <= 0) 10083f64091Sbellard return; 10183f64091Sbellard if (path_is_absolute(filename)) { 10283f64091Sbellard pstrcpy(dest, dest_size, filename); 10383f64091Sbellard } else { 10483f64091Sbellard p = strchr(base_path, ':'); 10583f64091Sbellard if (p) 10683f64091Sbellard p++; 10783f64091Sbellard else 10883f64091Sbellard p = base_path; 1093b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1103b9f94e1Sbellard #ifdef _WIN32 1113b9f94e1Sbellard { 1123b9f94e1Sbellard const char *p2; 1133b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1143b9f94e1Sbellard if (!p1 || p2 > p1) 1153b9f94e1Sbellard p1 = p2; 1163b9f94e1Sbellard } 1173b9f94e1Sbellard #endif 11883f64091Sbellard if (p1) 11983f64091Sbellard p1++; 12083f64091Sbellard else 12183f64091Sbellard p1 = base_path; 12283f64091Sbellard if (p1 > p) 12383f64091Sbellard p = p1; 12483f64091Sbellard len = p - base_path; 12583f64091Sbellard if (len > dest_size - 1) 12683f64091Sbellard len = dest_size - 1; 12783f64091Sbellard memcpy(dest, base_path, len); 12883f64091Sbellard dest[len] = '\0'; 12983f64091Sbellard pstrcat(dest, dest_size, filename); 13083f64091Sbellard } 13183f64091Sbellard } 13283f64091Sbellard 1335efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 134ea2384d3Sbellard { 135f141eafeSaliguori if (!bdrv->bdrv_aio_readv) { 13683f64091Sbellard /* add AIO emulation layer */ 137f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 138f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 139eda578e5Saliguori } else if (!bdrv->bdrv_read) { 14083f64091Sbellard /* add synchronous IO emulation layer */ 14183f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 14283f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 14383f64091Sbellard } 144b2e12bc6SChristoph Hellwig 145b2e12bc6SChristoph Hellwig if (!bdrv->bdrv_aio_flush) 146b2e12bc6SChristoph Hellwig bdrv->bdrv_aio_flush = bdrv_aio_flush_em; 147b2e12bc6SChristoph Hellwig 1488a22f02aSStefan Hajnoczi QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list); 149ea2384d3Sbellard } 150b338082bSbellard 151b338082bSbellard /* create a new block device (by default it is empty) */ 152b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 153fc01f7e7Sbellard { 1541b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 155b338082bSbellard 156b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 157b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 158ea2384d3Sbellard if (device_name[0] != '\0') { 1591b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 160ea2384d3Sbellard } 161b338082bSbellard return bs; 162b338082bSbellard } 163b338082bSbellard 164ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 165ea2384d3Sbellard { 166ea2384d3Sbellard BlockDriver *drv1; 1678a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 1688a22f02aSStefan Hajnoczi if (!strcmp(drv1->format_name, format_name)) { 169ea2384d3Sbellard return drv1; 170ea2384d3Sbellard } 1718a22f02aSStefan Hajnoczi } 172ea2384d3Sbellard return NULL; 173ea2384d3Sbellard } 174ea2384d3Sbellard 175eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 176eb852011SMarkus Armbruster { 177eb852011SMarkus Armbruster static const char *whitelist[] = { 178eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 179eb852011SMarkus Armbruster }; 180eb852011SMarkus Armbruster const char **p; 181eb852011SMarkus Armbruster 182eb852011SMarkus Armbruster if (!whitelist[0]) 183eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 184eb852011SMarkus Armbruster 185eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 186eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 187eb852011SMarkus Armbruster return 1; 188eb852011SMarkus Armbruster } 189eb852011SMarkus Armbruster } 190eb852011SMarkus Armbruster return 0; 191eb852011SMarkus Armbruster } 192eb852011SMarkus Armbruster 193eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 194eb852011SMarkus Armbruster { 195eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 196eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 197eb852011SMarkus Armbruster } 198eb852011SMarkus Armbruster 1990e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 2000e7e1989SKevin Wolf QEMUOptionParameter *options) 201ea2384d3Sbellard { 202ea2384d3Sbellard if (!drv->bdrv_create) 203ea2384d3Sbellard return -ENOTSUP; 2040e7e1989SKevin Wolf 2050e7e1989SKevin Wolf return drv->bdrv_create(filename, options); 206ea2384d3Sbellard } 207ea2384d3Sbellard 20884a12e66SChristoph Hellwig int bdrv_create_file(const char* filename, QEMUOptionParameter *options) 20984a12e66SChristoph Hellwig { 21084a12e66SChristoph Hellwig BlockDriver *drv; 21184a12e66SChristoph Hellwig 212b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 21384a12e66SChristoph Hellwig if (drv == NULL) { 21484a12e66SChristoph Hellwig drv = bdrv_find_format("file"); 21584a12e66SChristoph Hellwig } 21684a12e66SChristoph Hellwig 21784a12e66SChristoph Hellwig return bdrv_create(drv, filename, options); 21884a12e66SChristoph Hellwig } 21984a12e66SChristoph Hellwig 220d5249393Sbellard #ifdef _WIN32 22195389c86Sbellard void get_tmp_filename(char *filename, int size) 222d5249393Sbellard { 2233b9f94e1Sbellard char temp_dir[MAX_PATH]; 2243b9f94e1Sbellard 2253b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 2263b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 227d5249393Sbellard } 228d5249393Sbellard #else 22995389c86Sbellard void get_tmp_filename(char *filename, int size) 230ea2384d3Sbellard { 231ea2384d3Sbellard int fd; 2327ccfb2ebSblueswir1 const char *tmpdir; 233d5249393Sbellard /* XXX: race condition possible */ 2340badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 2350badc1eeSaurel32 if (!tmpdir) 2360badc1eeSaurel32 tmpdir = "/tmp"; 2370badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 238ea2384d3Sbellard fd = mkstemp(filename); 239ea2384d3Sbellard close(fd); 240ea2384d3Sbellard } 241d5249393Sbellard #endif 242ea2384d3Sbellard 24319cb3738Sbellard #ifdef _WIN32 244f45512feSbellard static int is_windows_drive_prefix(const char *filename) 245f45512feSbellard { 246f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 247f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 248f45512feSbellard filename[1] == ':'); 249f45512feSbellard } 250f45512feSbellard 251508c7cb3SChristoph Hellwig int is_windows_drive(const char *filename) 25219cb3738Sbellard { 253f45512feSbellard if (is_windows_drive_prefix(filename) && 254f45512feSbellard filename[2] == '\0') 25519cb3738Sbellard return 1; 25619cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 25719cb3738Sbellard strstart(filename, "//./", NULL)) 25819cb3738Sbellard return 1; 25919cb3738Sbellard return 0; 26019cb3738Sbellard } 26119cb3738Sbellard #endif 26219cb3738Sbellard 263f3a5d3f8SChristoph Hellwig /* 264f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 265f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 266f3a5d3f8SChristoph Hellwig */ 267f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 268f3a5d3f8SChristoph Hellwig { 269508c7cb3SChristoph Hellwig int score_max = 0, score; 270508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 271f3a5d3f8SChristoph Hellwig 2728a22f02aSStefan Hajnoczi QLIST_FOREACH(d, &bdrv_drivers, list) { 273508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 274508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 275508c7cb3SChristoph Hellwig if (score > score_max) { 276508c7cb3SChristoph Hellwig score_max = score; 277508c7cb3SChristoph Hellwig drv = d; 278f3a5d3f8SChristoph Hellwig } 279508c7cb3SChristoph Hellwig } 280f3a5d3f8SChristoph Hellwig } 281f3a5d3f8SChristoph Hellwig 282508c7cb3SChristoph Hellwig return drv; 283f3a5d3f8SChristoph Hellwig } 284f3a5d3f8SChristoph Hellwig 285b50cbabcSMORITA Kazutaka BlockDriver *bdrv_find_protocol(const char *filename) 28684a12e66SChristoph Hellwig { 28784a12e66SChristoph Hellwig BlockDriver *drv1; 28884a12e66SChristoph Hellwig char protocol[128]; 28984a12e66SChristoph Hellwig int len; 29084a12e66SChristoph Hellwig const char *p; 29184a12e66SChristoph Hellwig 29266f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 29366f82ceeSKevin Wolf 294*39508e7aSChristoph Hellwig /* 295*39508e7aSChristoph Hellwig * XXX(hch): we really should not let host device detection 296*39508e7aSChristoph Hellwig * override an explicit protocol specification, but moving this 297*39508e7aSChristoph Hellwig * later breaks access to device names with colons in them. 298*39508e7aSChristoph Hellwig * Thanks to the brain-dead persistent naming schemes on udev- 299*39508e7aSChristoph Hellwig * based Linux systems those actually are quite common. 300*39508e7aSChristoph Hellwig */ 30184a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 302*39508e7aSChristoph Hellwig if (drv1) { 30384a12e66SChristoph Hellwig return drv1; 30484a12e66SChristoph Hellwig } 305*39508e7aSChristoph Hellwig 306*39508e7aSChristoph Hellwig #ifdef _WIN32 307*39508e7aSChristoph Hellwig if (is_windows_drive(filename) || 308*39508e7aSChristoph Hellwig is_windows_drive_prefix(filename)) 309*39508e7aSChristoph Hellwig return bdrv_find_format("file"); 310*39508e7aSChristoph Hellwig #endif 311*39508e7aSChristoph Hellwig 312*39508e7aSChristoph Hellwig p = strchr(filename, ':'); 313*39508e7aSChristoph Hellwig if (!p) { 314*39508e7aSChristoph Hellwig return bdrv_find_format("file"); 315*39508e7aSChristoph Hellwig } 31684a12e66SChristoph Hellwig len = p - filename; 31784a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 31884a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 31984a12e66SChristoph Hellwig memcpy(protocol, filename, len); 32084a12e66SChristoph Hellwig protocol[len] = '\0'; 32184a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 32284a12e66SChristoph Hellwig if (drv1->protocol_name && 32384a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 32484a12e66SChristoph Hellwig return drv1; 32584a12e66SChristoph Hellwig } 32684a12e66SChristoph Hellwig } 32784a12e66SChristoph Hellwig return NULL; 32884a12e66SChristoph Hellwig } 32984a12e66SChristoph Hellwig 330ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 331ea2384d3Sbellard { 33283f64091Sbellard int ret, score, score_max; 333ea2384d3Sbellard BlockDriver *drv1, *drv; 33483f64091Sbellard uint8_t buf[2048]; 33583f64091Sbellard BlockDriverState *bs; 336ea2384d3Sbellard 337f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 33883f64091Sbellard if (ret < 0) 3397674e7bfSbellard return NULL; 340f8ea0b00SNicholas Bellinger 34108a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 34208a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 3431a396859SNicholas A. Bellinger bdrv_delete(bs); 344f8ea0b00SNicholas Bellinger return bdrv_find_format("raw"); 3451a396859SNicholas A. Bellinger } 346f8ea0b00SNicholas Bellinger 34783f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 34883f64091Sbellard bdrv_delete(bs); 349ea2384d3Sbellard if (ret < 0) { 350ea2384d3Sbellard return NULL; 351ea2384d3Sbellard } 352ea2384d3Sbellard 353ea2384d3Sbellard score_max = 0; 35484a12e66SChristoph Hellwig drv = NULL; 3558a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 35683f64091Sbellard if (drv1->bdrv_probe) { 357ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 358ea2384d3Sbellard if (score > score_max) { 359ea2384d3Sbellard score_max = score; 360ea2384d3Sbellard drv = drv1; 361ea2384d3Sbellard } 362ea2384d3Sbellard } 36383f64091Sbellard } 364ea2384d3Sbellard return drv; 365ea2384d3Sbellard } 366ea2384d3Sbellard 36751762288SStefan Hajnoczi /** 36851762288SStefan Hajnoczi * Set the current 'total_sectors' value 36951762288SStefan Hajnoczi */ 37051762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 37151762288SStefan Hajnoczi { 37251762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 37351762288SStefan Hajnoczi 374396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 375396759adSNicholas Bellinger if (bs->sg) 376396759adSNicholas Bellinger return 0; 377396759adSNicholas Bellinger 37851762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 37951762288SStefan Hajnoczi if (drv->bdrv_getlength) { 38051762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 38151762288SStefan Hajnoczi if (length < 0) { 38251762288SStefan Hajnoczi return length; 38351762288SStefan Hajnoczi } 38451762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 38551762288SStefan Hajnoczi } 38651762288SStefan Hajnoczi 38751762288SStefan Hajnoczi bs->total_sectors = hint; 38851762288SStefan Hajnoczi return 0; 38951762288SStefan Hajnoczi } 39051762288SStefan Hajnoczi 391b6ce07aaSKevin Wolf /* 39257915332SKevin Wolf * Common part for opening disk images and files 39357915332SKevin Wolf */ 39457915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 39557915332SKevin Wolf int flags, BlockDriver *drv) 39657915332SKevin Wolf { 39757915332SKevin Wolf int ret, open_flags; 39857915332SKevin Wolf 39957915332SKevin Wolf assert(drv != NULL); 40057915332SKevin Wolf 40166f82ceeSKevin Wolf bs->file = NULL; 40251762288SStefan Hajnoczi bs->total_sectors = 0; 40357915332SKevin Wolf bs->is_temporary = 0; 40457915332SKevin Wolf bs->encrypted = 0; 40557915332SKevin Wolf bs->valid_key = 0; 40657915332SKevin Wolf bs->open_flags = flags; 40757915332SKevin Wolf /* buffer_alignment defaulted to 512, drivers can change this value */ 40857915332SKevin Wolf bs->buffer_alignment = 512; 40957915332SKevin Wolf 41057915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 41157915332SKevin Wolf 41257915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 41357915332SKevin Wolf return -ENOTSUP; 41457915332SKevin Wolf } 41557915332SKevin Wolf 41657915332SKevin Wolf bs->drv = drv; 41757915332SKevin Wolf bs->opaque = qemu_mallocz(drv->instance_size); 41857915332SKevin Wolf 41957915332SKevin Wolf /* 42057915332SKevin Wolf * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a 42157915332SKevin Wolf * write cache to the guest. We do need the fdatasync to flush 42257915332SKevin Wolf * out transactions for block allocations, and we maybe have a 42357915332SKevin Wolf * volatile write cache in our backing device to deal with. 42457915332SKevin Wolf */ 42557915332SKevin Wolf if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) 42657915332SKevin Wolf bs->enable_write_cache = 1; 42757915332SKevin Wolf 42857915332SKevin Wolf /* 42957915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 43057915332SKevin Wolf * image. 43157915332SKevin Wolf */ 43257915332SKevin Wolf open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 43357915332SKevin Wolf 43457915332SKevin Wolf /* 43557915332SKevin Wolf * Snapshots should be writeable. 43657915332SKevin Wolf */ 43757915332SKevin Wolf if (bs->is_temporary) { 43857915332SKevin Wolf open_flags |= BDRV_O_RDWR; 43957915332SKevin Wolf } 44057915332SKevin Wolf 44166f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 44266f82ceeSKevin Wolf if (drv->bdrv_file_open) { 44366f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 44466f82ceeSKevin Wolf } else { 44566f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 44666f82ceeSKevin Wolf if (ret >= 0) { 44766f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 44866f82ceeSKevin Wolf } 44966f82ceeSKevin Wolf } 45066f82ceeSKevin Wolf 45157915332SKevin Wolf if (ret < 0) { 45257915332SKevin Wolf goto free_and_fail; 45357915332SKevin Wolf } 45457915332SKevin Wolf 45557915332SKevin Wolf bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 45651762288SStefan Hajnoczi 45751762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 45851762288SStefan Hajnoczi if (ret < 0) { 45951762288SStefan Hajnoczi goto free_and_fail; 46057915332SKevin Wolf } 46151762288SStefan Hajnoczi 46257915332SKevin Wolf #ifndef _WIN32 46357915332SKevin Wolf if (bs->is_temporary) { 46457915332SKevin Wolf unlink(filename); 46557915332SKevin Wolf } 46657915332SKevin Wolf #endif 46757915332SKevin Wolf return 0; 46857915332SKevin Wolf 46957915332SKevin Wolf free_and_fail: 47066f82ceeSKevin Wolf if (bs->file) { 47166f82ceeSKevin Wolf bdrv_delete(bs->file); 47266f82ceeSKevin Wolf bs->file = NULL; 47366f82ceeSKevin Wolf } 47457915332SKevin Wolf qemu_free(bs->opaque); 47557915332SKevin Wolf bs->opaque = NULL; 47657915332SKevin Wolf bs->drv = NULL; 47757915332SKevin Wolf return ret; 47857915332SKevin Wolf } 47957915332SKevin Wolf 48057915332SKevin Wolf /* 481b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 482b6ce07aaSKevin Wolf */ 48383f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 484b338082bSbellard { 48583f64091Sbellard BlockDriverState *bs; 4866db95603SChristoph Hellwig BlockDriver *drv; 48783f64091Sbellard int ret; 4883b0d4f61Sbellard 489b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 4906db95603SChristoph Hellwig if (!drv) { 4916db95603SChristoph Hellwig return -ENOENT; 4926db95603SChristoph Hellwig } 4936db95603SChristoph Hellwig 49483f64091Sbellard bs = bdrv_new(""); 495b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 49683f64091Sbellard if (ret < 0) { 49783f64091Sbellard bdrv_delete(bs); 49883f64091Sbellard return ret; 4993b0d4f61Sbellard } 50071d0770cSaliguori bs->growable = 1; 50183f64091Sbellard *pbs = bs; 50283f64091Sbellard return 0; 5033b0d4f61Sbellard } 5043b0d4f61Sbellard 505b6ce07aaSKevin Wolf /* 506b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 507b6ce07aaSKevin Wolf */ 508d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 509ea2384d3Sbellard BlockDriver *drv) 510ea2384d3Sbellard { 511b6ce07aaSKevin Wolf int ret; 51233e3963eSbellard 51383f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 514ea2384d3Sbellard BlockDriverState *bs1; 515ea2384d3Sbellard int64_t total_size; 5167c96d46eSaliguori int is_protocol = 0; 51791a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 51891a073a9SKevin Wolf QEMUOptionParameter *options; 519b6ce07aaSKevin Wolf char tmp_filename[PATH_MAX]; 520b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 52133e3963eSbellard 522ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 523ea2384d3Sbellard instead of opening 'filename' directly */ 524ea2384d3Sbellard 525ea2384d3Sbellard /* if there is a backing file, use it */ 526ea2384d3Sbellard bs1 = bdrv_new(""); 527d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 52851d7c00cSaliguori if (ret < 0) { 529ea2384d3Sbellard bdrv_delete(bs1); 53051d7c00cSaliguori return ret; 531ea2384d3Sbellard } 5323e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 5337c96d46eSaliguori 5347c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 5357c96d46eSaliguori is_protocol = 1; 5367c96d46eSaliguori 537ea2384d3Sbellard bdrv_delete(bs1); 538ea2384d3Sbellard 539ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 5407c96d46eSaliguori 5417c96d46eSaliguori /* Real path is meaningless for protocols */ 5427c96d46eSaliguori if (is_protocol) 5437c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 5447c96d46eSaliguori "%s", filename); 545114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 546114cdfa9SKirill A. Shutemov return -errno; 5477c96d46eSaliguori 54891a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 54991a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 55091a073a9SKevin Wolf 5513e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 55291a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 55391a073a9SKevin Wolf if (drv) { 55491a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 55591a073a9SKevin Wolf drv->format_name); 55691a073a9SKevin Wolf } 55791a073a9SKevin Wolf 55891a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 559d748768cSJan Kiszka free_option_parameters(options); 56051d7c00cSaliguori if (ret < 0) { 56151d7c00cSaliguori return ret; 562ea2384d3Sbellard } 56391a073a9SKevin Wolf 564ea2384d3Sbellard filename = tmp_filename; 56591a073a9SKevin Wolf drv = bdrv_qcow2; 566ea2384d3Sbellard bs->is_temporary = 1; 567ea2384d3Sbellard } 568ea2384d3Sbellard 569b6ce07aaSKevin Wolf /* Find the right image format driver */ 5706db95603SChristoph Hellwig if (!drv) { 571ea2384d3Sbellard drv = find_image_format(filename); 572ea2384d3Sbellard } 5736987307cSChristoph Hellwig 57451d7c00cSaliguori if (!drv) { 57551d7c00cSaliguori ret = -ENOENT; 57651d7c00cSaliguori goto unlink_and_fail; 57783f64091Sbellard } 578b6ce07aaSKevin Wolf 579b6ce07aaSKevin Wolf /* Open the image */ 580b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 581b6ce07aaSKevin Wolf if (ret < 0) { 5826987307cSChristoph Hellwig goto unlink_and_fail; 5836987307cSChristoph Hellwig } 5846987307cSChristoph Hellwig 585b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 586b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 587b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 588b6ce07aaSKevin Wolf int back_flags; 589b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 590b6ce07aaSKevin Wolf 591b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 592b6ce07aaSKevin Wolf path_combine(backing_filename, sizeof(backing_filename), 593b6ce07aaSKevin Wolf filename, bs->backing_file); 594b6ce07aaSKevin Wolf if (bs->backing_format[0] != '\0') 595b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 596b6ce07aaSKevin Wolf 597b6ce07aaSKevin Wolf /* backing files always opened read-only */ 598b6ce07aaSKevin Wolf back_flags = 599b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 600b6ce07aaSKevin Wolf 601b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 602b6ce07aaSKevin Wolf if (ret < 0) { 603b6ce07aaSKevin Wolf bdrv_close(bs); 604b6ce07aaSKevin Wolf return ret; 605b6ce07aaSKevin Wolf } 606b6ce07aaSKevin Wolf if (bs->is_temporary) { 607b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 608b6ce07aaSKevin Wolf } else { 609b6ce07aaSKevin Wolf /* base image inherits from "parent" */ 610b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = bs->keep_read_only; 611b6ce07aaSKevin Wolf } 612b6ce07aaSKevin Wolf } 613b6ce07aaSKevin Wolf 614b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 615b6ce07aaSKevin Wolf /* call the change callback */ 616b6ce07aaSKevin Wolf bs->media_changed = 1; 617b6ce07aaSKevin Wolf if (bs->change_cb) 618b6ce07aaSKevin Wolf bs->change_cb(bs->change_opaque); 619b6ce07aaSKevin Wolf } 620b6ce07aaSKevin Wolf 621b6ce07aaSKevin Wolf return 0; 622b6ce07aaSKevin Wolf 623b6ce07aaSKevin Wolf unlink_and_fail: 624b6ce07aaSKevin Wolf if (bs->is_temporary) { 625b6ce07aaSKevin Wolf unlink(filename); 626b6ce07aaSKevin Wolf } 627b6ce07aaSKevin Wolf return ret; 628b6ce07aaSKevin Wolf } 629b6ce07aaSKevin Wolf 630fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 631fc01f7e7Sbellard { 63219cb3738Sbellard if (bs->drv) { 633557df6acSStefan Hajnoczi if (bs->backing_hd) { 634ea2384d3Sbellard bdrv_delete(bs->backing_hd); 635557df6acSStefan Hajnoczi bs->backing_hd = NULL; 636557df6acSStefan Hajnoczi } 637ea2384d3Sbellard bs->drv->bdrv_close(bs); 638ea2384d3Sbellard qemu_free(bs->opaque); 639ea2384d3Sbellard #ifdef _WIN32 640ea2384d3Sbellard if (bs->is_temporary) { 641ea2384d3Sbellard unlink(bs->filename); 642ea2384d3Sbellard } 64367b915a5Sbellard #endif 644ea2384d3Sbellard bs->opaque = NULL; 645ea2384d3Sbellard bs->drv = NULL; 646b338082bSbellard 64766f82ceeSKevin Wolf if (bs->file != NULL) { 64866f82ceeSKevin Wolf bdrv_close(bs->file); 64966f82ceeSKevin Wolf } 65066f82ceeSKevin Wolf 651b338082bSbellard /* call the change callback */ 65219cb3738Sbellard bs->media_changed = 1; 653b338082bSbellard if (bs->change_cb) 654b338082bSbellard bs->change_cb(bs->change_opaque); 655b338082bSbellard } 656b338082bSbellard } 657b338082bSbellard 6582bc93fedSMORITA Kazutaka void bdrv_close_all(void) 6592bc93fedSMORITA Kazutaka { 6602bc93fedSMORITA Kazutaka BlockDriverState *bs; 6612bc93fedSMORITA Kazutaka 6622bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 6632bc93fedSMORITA Kazutaka bdrv_close(bs); 6642bc93fedSMORITA Kazutaka } 6652bc93fedSMORITA Kazutaka } 6662bc93fedSMORITA Kazutaka 667b338082bSbellard void bdrv_delete(BlockDriverState *bs) 668b338082bSbellard { 6691b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 6701b7bdbc1SStefan Hajnoczi if (bs->device_name[0] != '\0') { 6711b7bdbc1SStefan Hajnoczi QTAILQ_REMOVE(&bdrv_states, bs, list); 6721b7bdbc1SStefan Hajnoczi } 67334c6f050Saurel32 674b338082bSbellard bdrv_close(bs); 67566f82ceeSKevin Wolf if (bs->file != NULL) { 67666f82ceeSKevin Wolf bdrv_delete(bs->file); 67766f82ceeSKevin Wolf } 67866f82ceeSKevin Wolf 679b338082bSbellard qemu_free(bs); 680fc01f7e7Sbellard } 681fc01f7e7Sbellard 682e97fc193Saliguori /* 683e97fc193Saliguori * Run consistency checks on an image 684e97fc193Saliguori * 685e97fc193Saliguori * Returns the number of errors or -errno when an internal error occurs 686e97fc193Saliguori */ 687e97fc193Saliguori int bdrv_check(BlockDriverState *bs) 688e97fc193Saliguori { 689e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 690e97fc193Saliguori return -ENOTSUP; 691e97fc193Saliguori } 692e97fc193Saliguori 693e97fc193Saliguori return bs->drv->bdrv_check(bs); 694e97fc193Saliguori } 695e97fc193Saliguori 69633e3963eSbellard /* commit COW file into the raw image */ 69733e3963eSbellard int bdrv_commit(BlockDriverState *bs) 69833e3963eSbellard { 69919cb3738Sbellard BlockDriver *drv = bs->drv; 70083f64091Sbellard int64_t i, total_sectors; 7014dca4b63SNaphtali Sprei int n, j, ro, open_flags; 7024dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 703eb5a3165SJes Sorensen unsigned char sector[BDRV_SECTOR_SIZE]; 7044dca4b63SNaphtali Sprei char filename[1024]; 7054dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 70633e3963eSbellard 70719cb3738Sbellard if (!drv) 70819cb3738Sbellard return -ENOMEDIUM; 70933e3963eSbellard 7104dca4b63SNaphtali Sprei if (!bs->backing_hd) { 7114dca4b63SNaphtali Sprei return -ENOTSUP; 7124dca4b63SNaphtali Sprei } 7134dca4b63SNaphtali Sprei 7144dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 715ea2384d3Sbellard return -EACCES; 71633e3963eSbellard } 71733e3963eSbellard 7184dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 7194dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 7204dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 7214dca4b63SNaphtali Sprei 7224dca4b63SNaphtali Sprei if (ro) { 7234dca4b63SNaphtali Sprei /* re-open as RW */ 7244dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 7254dca4b63SNaphtali Sprei bs->backing_hd = NULL; 7264dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 727c3349197SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv); 7284dca4b63SNaphtali Sprei if (rw_ret < 0) { 7294dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 7304dca4b63SNaphtali Sprei /* try to re-open read-only */ 7314dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 732c3349197SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); 7334dca4b63SNaphtali Sprei if (ret < 0) { 7344dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 7354dca4b63SNaphtali Sprei /* drive not functional anymore */ 7364dca4b63SNaphtali Sprei bs->drv = NULL; 7374dca4b63SNaphtali Sprei return ret; 7384dca4b63SNaphtali Sprei } 7394dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 7404dca4b63SNaphtali Sprei return rw_ret; 7414dca4b63SNaphtali Sprei } 7424dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 743ea2384d3Sbellard } 744ea2384d3Sbellard 7456ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 74683f64091Sbellard for (i = 0; i < total_sectors;) { 74719cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 748ea2384d3Sbellard for(j = 0; j < n; j++) { 74933e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 7504dca4b63SNaphtali Sprei ret = -EIO; 7514dca4b63SNaphtali Sprei goto ro_cleanup; 75233e3963eSbellard } 75333e3963eSbellard 754ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 7554dca4b63SNaphtali Sprei ret = -EIO; 7564dca4b63SNaphtali Sprei goto ro_cleanup; 75733e3963eSbellard } 758ea2384d3Sbellard i++; 759ea2384d3Sbellard } 760ea2384d3Sbellard } else { 761ea2384d3Sbellard i += n; 76233e3963eSbellard } 76333e3963eSbellard } 76495389c86Sbellard 7651d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 7661d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 7671d44952fSChristoph Hellwig bdrv_flush(bs); 7681d44952fSChristoph Hellwig } 76995389c86Sbellard 7703f5075aeSChristoph Hellwig /* 7713f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 7723f5075aeSChristoph Hellwig * stable on disk. 7733f5075aeSChristoph Hellwig */ 7743f5075aeSChristoph Hellwig if (bs->backing_hd) 7753f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 7764dca4b63SNaphtali Sprei 7774dca4b63SNaphtali Sprei ro_cleanup: 7784dca4b63SNaphtali Sprei 7794dca4b63SNaphtali Sprei if (ro) { 7804dca4b63SNaphtali Sprei /* re-open as RO */ 7814dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 7824dca4b63SNaphtali Sprei bs->backing_hd = NULL; 7834dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 784c3349197SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); 7854dca4b63SNaphtali Sprei if (ret < 0) { 7864dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 7874dca4b63SNaphtali Sprei /* drive not functional anymore */ 7884dca4b63SNaphtali Sprei bs->drv = NULL; 7894dca4b63SNaphtali Sprei return ret; 7904dca4b63SNaphtali Sprei } 7914dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 7924dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 7934dca4b63SNaphtali Sprei } 7944dca4b63SNaphtali Sprei 7951d44952fSChristoph Hellwig return ret; 79633e3963eSbellard } 79733e3963eSbellard 7986ab4b5abSMarkus Armbruster void bdrv_commit_all(void) 7996ab4b5abSMarkus Armbruster { 8006ab4b5abSMarkus Armbruster BlockDriverState *bs; 8016ab4b5abSMarkus Armbruster 8026ab4b5abSMarkus Armbruster QTAILQ_FOREACH(bs, &bdrv_states, list) { 8036ab4b5abSMarkus Armbruster bdrv_commit(bs); 8046ab4b5abSMarkus Armbruster } 8056ab4b5abSMarkus Armbruster } 8066ab4b5abSMarkus Armbruster 807756e6736SKevin Wolf /* 808756e6736SKevin Wolf * Return values: 809756e6736SKevin Wolf * 0 - success 810756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 811756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 812756e6736SKevin Wolf * image file header 813756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 814756e6736SKevin Wolf */ 815756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 816756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 817756e6736SKevin Wolf { 818756e6736SKevin Wolf BlockDriver *drv = bs->drv; 819756e6736SKevin Wolf 820756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 821756e6736SKevin Wolf return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 822756e6736SKevin Wolf } else { 823756e6736SKevin Wolf return -ENOTSUP; 824756e6736SKevin Wolf } 825756e6736SKevin Wolf } 826756e6736SKevin Wolf 82771d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 82871d0770cSaliguori size_t size) 82971d0770cSaliguori { 83071d0770cSaliguori int64_t len; 83171d0770cSaliguori 83271d0770cSaliguori if (!bdrv_is_inserted(bs)) 83371d0770cSaliguori return -ENOMEDIUM; 83471d0770cSaliguori 83571d0770cSaliguori if (bs->growable) 83671d0770cSaliguori return 0; 83771d0770cSaliguori 83871d0770cSaliguori len = bdrv_getlength(bs); 83971d0770cSaliguori 840fbb7b4e0SKevin Wolf if (offset < 0) 841fbb7b4e0SKevin Wolf return -EIO; 842fbb7b4e0SKevin Wolf 843fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 84471d0770cSaliguori return -EIO; 84571d0770cSaliguori 84671d0770cSaliguori return 0; 84771d0770cSaliguori } 84871d0770cSaliguori 84971d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 85071d0770cSaliguori int nb_sectors) 85171d0770cSaliguori { 852eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 853eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 85471d0770cSaliguori } 85571d0770cSaliguori 85619cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 857fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 858fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 859fc01f7e7Sbellard { 860ea2384d3Sbellard BlockDriver *drv = bs->drv; 861fc01f7e7Sbellard 86219cb3738Sbellard if (!drv) 86319cb3738Sbellard return -ENOMEDIUM; 86471d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 86571d0770cSaliguori return -EIO; 866b338082bSbellard 86783f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 86883f64091Sbellard } 869fc01f7e7Sbellard 8707cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 8717cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 8727cd1e32aSlirans@il.ibm.com { 8737cd1e32aSlirans@il.ibm.com int64_t start, end; 874c6d22830SJan Kiszka unsigned long val, idx, bit; 875a55eb92cSJan Kiszka 8766ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 877c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 8787cd1e32aSlirans@il.ibm.com 8797cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 880c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 881c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 882c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 883c6d22830SJan Kiszka if (dirty) { 884aaa0eb75SLiran Schour if (!(val & (1 << bit))) { 885aaa0eb75SLiran Schour bs->dirty_count++; 886c6d22830SJan Kiszka val |= 1 << bit; 887aaa0eb75SLiran Schour } 888c6d22830SJan Kiszka } else { 889aaa0eb75SLiran Schour if (val & (1 << bit)) { 890aaa0eb75SLiran Schour bs->dirty_count--; 891c6d22830SJan Kiszka val &= ~(1 << bit); 892c6d22830SJan Kiszka } 893aaa0eb75SLiran Schour } 894c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 8957cd1e32aSlirans@il.ibm.com } 8967cd1e32aSlirans@il.ibm.com } 8977cd1e32aSlirans@il.ibm.com 89819cb3738Sbellard /* Return < 0 if error. Important errors are: 89919cb3738Sbellard -EIO generic I/O error (may happen for all errors) 90019cb3738Sbellard -ENOMEDIUM No media inserted. 90119cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 90219cb3738Sbellard -EACCES Trying to write a read-only device 90319cb3738Sbellard */ 904fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 905fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 906fc01f7e7Sbellard { 90783f64091Sbellard BlockDriver *drv = bs->drv; 90819cb3738Sbellard if (!bs->drv) 90919cb3738Sbellard return -ENOMEDIUM; 9100849bf08Sbellard if (bs->read_only) 91119cb3738Sbellard return -EACCES; 91271d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 91371d0770cSaliguori return -EIO; 91471d0770cSaliguori 915c6d22830SJan Kiszka if (bs->dirty_bitmap) { 9167cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 9177cd1e32aSlirans@il.ibm.com } 9187cd1e32aSlirans@il.ibm.com 919294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 920294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 921294cc35fSKevin Wolf } 922294cc35fSKevin Wolf 92383f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 92483f64091Sbellard } 92583f64091Sbellard 926eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 927eda578e5Saliguori void *buf, int count1) 92883f64091Sbellard { 9296ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 93083f64091Sbellard int len, nb_sectors, count; 93183f64091Sbellard int64_t sector_num; 9329a8c4cceSKevin Wolf int ret; 93383f64091Sbellard 93483f64091Sbellard count = count1; 93583f64091Sbellard /* first read to align to sector start */ 9366ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 93783f64091Sbellard if (len > count) 93883f64091Sbellard len = count; 9396ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 94083f64091Sbellard if (len > 0) { 9419a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9429a8c4cceSKevin Wolf return ret; 9436ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 94483f64091Sbellard count -= len; 94583f64091Sbellard if (count == 0) 94683f64091Sbellard return count1; 94783f64091Sbellard sector_num++; 94883f64091Sbellard buf += len; 94983f64091Sbellard } 95083f64091Sbellard 95183f64091Sbellard /* read the sectors "in place" */ 9526ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 95383f64091Sbellard if (nb_sectors > 0) { 9549a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 9559a8c4cceSKevin Wolf return ret; 95683f64091Sbellard sector_num += nb_sectors; 9576ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 95883f64091Sbellard buf += len; 95983f64091Sbellard count -= len; 96083f64091Sbellard } 96183f64091Sbellard 96283f64091Sbellard /* add data from the last sector */ 96383f64091Sbellard if (count > 0) { 9649a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9659a8c4cceSKevin Wolf return ret; 96683f64091Sbellard memcpy(buf, tmp_buf, count); 96783f64091Sbellard } 96883f64091Sbellard return count1; 96983f64091Sbellard } 97083f64091Sbellard 971eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 972eda578e5Saliguori const void *buf, int count1) 97383f64091Sbellard { 9746ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 97583f64091Sbellard int len, nb_sectors, count; 97683f64091Sbellard int64_t sector_num; 9779a8c4cceSKevin Wolf int ret; 97883f64091Sbellard 97983f64091Sbellard count = count1; 98083f64091Sbellard /* first write to align to sector start */ 9816ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 98283f64091Sbellard if (len > count) 98383f64091Sbellard len = count; 9846ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 98583f64091Sbellard if (len > 0) { 9869a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9879a8c4cceSKevin Wolf return ret; 9886ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 9899a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 9909a8c4cceSKevin Wolf return ret; 99183f64091Sbellard count -= len; 99283f64091Sbellard if (count == 0) 99383f64091Sbellard return count1; 99483f64091Sbellard sector_num++; 99583f64091Sbellard buf += len; 99683f64091Sbellard } 99783f64091Sbellard 99883f64091Sbellard /* write the sectors "in place" */ 9996ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 100083f64091Sbellard if (nb_sectors > 0) { 10019a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 10029a8c4cceSKevin Wolf return ret; 100383f64091Sbellard sector_num += nb_sectors; 10046ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 100583f64091Sbellard buf += len; 100683f64091Sbellard count -= len; 100783f64091Sbellard } 100883f64091Sbellard 100983f64091Sbellard /* add data from the last sector */ 101083f64091Sbellard if (count > 0) { 10119a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 10129a8c4cceSKevin Wolf return ret; 101383f64091Sbellard memcpy(tmp_buf, buf, count); 10149a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 10159a8c4cceSKevin Wolf return ret; 101683f64091Sbellard } 101783f64091Sbellard return count1; 101883f64091Sbellard } 101983f64091Sbellard 1020f08145feSKevin Wolf /* 1021f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1022f08145feSKevin Wolf * request (acts as a barrier) 1023f08145feSKevin Wolf * 1024f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1025f08145feSKevin Wolf */ 1026f08145feSKevin Wolf int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset, 1027f08145feSKevin Wolf const void *buf, int count) 1028f08145feSKevin Wolf { 1029f08145feSKevin Wolf int ret; 1030f08145feSKevin Wolf 1031f08145feSKevin Wolf ret = bdrv_pwrite(bs, offset, buf, count); 1032f08145feSKevin Wolf if (ret < 0) { 1033f08145feSKevin Wolf return ret; 1034f08145feSKevin Wolf } 1035f08145feSKevin Wolf 1036f08145feSKevin Wolf /* No flush needed for cache=writethrough, it uses O_DSYNC */ 1037f08145feSKevin Wolf if ((bs->open_flags & BDRV_O_CACHE_MASK) != 0) { 1038f08145feSKevin Wolf bdrv_flush(bs); 1039f08145feSKevin Wolf } 1040f08145feSKevin Wolf 1041f08145feSKevin Wolf return 0; 1042f08145feSKevin Wolf } 1043f08145feSKevin Wolf 1044f08145feSKevin Wolf /* 1045f08145feSKevin Wolf * Writes to the file and ensures that no writes are reordered across this 1046f08145feSKevin Wolf * request (acts as a barrier) 1047f08145feSKevin Wolf * 1048f08145feSKevin Wolf * Returns 0 on success, -errno in error cases. 1049f08145feSKevin Wolf */ 1050f08145feSKevin Wolf int bdrv_write_sync(BlockDriverState *bs, int64_t sector_num, 1051f08145feSKevin Wolf const uint8_t *buf, int nb_sectors) 1052f08145feSKevin Wolf { 1053f08145feSKevin Wolf return bdrv_pwrite_sync(bs, BDRV_SECTOR_SIZE * sector_num, 1054f08145feSKevin Wolf buf, BDRV_SECTOR_SIZE * nb_sectors); 1055f08145feSKevin Wolf } 1056f08145feSKevin Wolf 105783f64091Sbellard /** 105883f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 105983f64091Sbellard */ 106083f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 106183f64091Sbellard { 106283f64091Sbellard BlockDriver *drv = bs->drv; 106351762288SStefan Hajnoczi int ret; 106483f64091Sbellard if (!drv) 106519cb3738Sbellard return -ENOMEDIUM; 106683f64091Sbellard if (!drv->bdrv_truncate) 106783f64091Sbellard return -ENOTSUP; 106859f2689dSNaphtali Sprei if (bs->read_only) 106959f2689dSNaphtali Sprei return -EACCES; 107051762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 107151762288SStefan Hajnoczi if (ret == 0) { 107251762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 107351762288SStefan Hajnoczi } 107451762288SStefan Hajnoczi return ret; 107583f64091Sbellard } 107683f64091Sbellard 107783f64091Sbellard /** 107883f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 107983f64091Sbellard */ 108083f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 108183f64091Sbellard { 108283f64091Sbellard BlockDriver *drv = bs->drv; 108383f64091Sbellard if (!drv) 108419cb3738Sbellard return -ENOMEDIUM; 108551762288SStefan Hajnoczi 108651762288SStefan Hajnoczi /* Fixed size devices use the total_sectors value for speed instead of 108751762288SStefan Hajnoczi issuing a length query (like lseek) on each call. Also, legacy block 108851762288SStefan Hajnoczi drivers don't provide a bdrv_getlength function and must use 108951762288SStefan Hajnoczi total_sectors. */ 109051762288SStefan Hajnoczi if (!bs->growable || !drv->bdrv_getlength) { 10916ea44308SJan Kiszka return bs->total_sectors * BDRV_SECTOR_SIZE; 109283f64091Sbellard } 109383f64091Sbellard return drv->bdrv_getlength(bs); 1094fc01f7e7Sbellard } 1095fc01f7e7Sbellard 109619cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 109796b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 1098fc01f7e7Sbellard { 109919cb3738Sbellard int64_t length; 110019cb3738Sbellard length = bdrv_getlength(bs); 110119cb3738Sbellard if (length < 0) 110219cb3738Sbellard length = 0; 110319cb3738Sbellard else 11046ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 110519cb3738Sbellard *nb_sectors_ptr = length; 1106fc01f7e7Sbellard } 1107cf98951bSbellard 1108f3d54fc4Saliguori struct partition { 1109f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 1110f3d54fc4Saliguori uint8_t head; /* starting head */ 1111f3d54fc4Saliguori uint8_t sector; /* starting sector */ 1112f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 1113f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 1114f3d54fc4Saliguori uint8_t end_head; /* end head */ 1115f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 1116f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 1117f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 1118f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 1119f3d54fc4Saliguori } __attribute__((packed)); 1120f3d54fc4Saliguori 1121f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 1122f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 1123f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 1124f3d54fc4Saliguori { 1125eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 1126f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 1127f3d54fc4Saliguori struct partition *p; 1128f3d54fc4Saliguori uint32_t nr_sects; 1129a38131b6Sblueswir1 uint64_t nb_sectors; 1130f3d54fc4Saliguori 1131f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1132f3d54fc4Saliguori 1133f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 1134f3d54fc4Saliguori if (ret < 0) 1135f3d54fc4Saliguori return -1; 1136f3d54fc4Saliguori /* test msdos magic */ 1137f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 1138f3d54fc4Saliguori return -1; 1139f3d54fc4Saliguori for(i = 0; i < 4; i++) { 1140f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 1141f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 1142f3d54fc4Saliguori if (nr_sects && p->end_head) { 1143f3d54fc4Saliguori /* We make the assumption that the partition terminates on 1144f3d54fc4Saliguori a cylinder boundary */ 1145f3d54fc4Saliguori heads = p->end_head + 1; 1146f3d54fc4Saliguori sectors = p->end_sector & 63; 1147f3d54fc4Saliguori if (sectors == 0) 1148f3d54fc4Saliguori continue; 1149f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 1150f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 1151f3d54fc4Saliguori continue; 1152f3d54fc4Saliguori *pheads = heads; 1153f3d54fc4Saliguori *psectors = sectors; 1154f3d54fc4Saliguori *pcylinders = cylinders; 1155f3d54fc4Saliguori #if 0 1156f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 1157f3d54fc4Saliguori cylinders, heads, sectors); 1158f3d54fc4Saliguori #endif 1159f3d54fc4Saliguori return 0; 1160f3d54fc4Saliguori } 1161f3d54fc4Saliguori } 1162f3d54fc4Saliguori return -1; 1163f3d54fc4Saliguori } 1164f3d54fc4Saliguori 1165f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 1166f3d54fc4Saliguori { 1167f3d54fc4Saliguori int translation, lba_detected = 0; 1168f3d54fc4Saliguori int cylinders, heads, secs; 1169a38131b6Sblueswir1 uint64_t nb_sectors; 1170f3d54fc4Saliguori 1171f3d54fc4Saliguori /* if a geometry hint is available, use it */ 1172f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1173f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 1174f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 1175f3d54fc4Saliguori if (cylinders != 0) { 1176f3d54fc4Saliguori *pcyls = cylinders; 1177f3d54fc4Saliguori *pheads = heads; 1178f3d54fc4Saliguori *psecs = secs; 1179f3d54fc4Saliguori } else { 1180f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 1181f3d54fc4Saliguori if (heads > 16) { 1182f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 1183f3d54fc4Saliguori translation was active, so the default 1184f3d54fc4Saliguori hardware geometry is OK */ 1185f3d54fc4Saliguori lba_detected = 1; 1186f3d54fc4Saliguori goto default_geometry; 1187f3d54fc4Saliguori } else { 1188f3d54fc4Saliguori *pcyls = cylinders; 1189f3d54fc4Saliguori *pheads = heads; 1190f3d54fc4Saliguori *psecs = secs; 1191f3d54fc4Saliguori /* disable any translation to be in sync with 1192f3d54fc4Saliguori the logical geometry */ 1193f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 1194f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1195f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 1196f3d54fc4Saliguori } 1197f3d54fc4Saliguori } 1198f3d54fc4Saliguori } else { 1199f3d54fc4Saliguori default_geometry: 1200f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 1201f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 1202f3d54fc4Saliguori 1203f3d54fc4Saliguori if (cylinders > 16383) 1204f3d54fc4Saliguori cylinders = 16383; 1205f3d54fc4Saliguori else if (cylinders < 2) 1206f3d54fc4Saliguori cylinders = 2; 1207f3d54fc4Saliguori *pcyls = cylinders; 1208f3d54fc4Saliguori *pheads = 16; 1209f3d54fc4Saliguori *psecs = 63; 1210f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 1211f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 1212f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1213f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 1214f3d54fc4Saliguori } else { 1215f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1216f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 1217f3d54fc4Saliguori } 1218f3d54fc4Saliguori } 1219f3d54fc4Saliguori } 1220f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 1221f3d54fc4Saliguori } 1222f3d54fc4Saliguori } 1223f3d54fc4Saliguori 1224b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 1225b338082bSbellard int cyls, int heads, int secs) 1226b338082bSbellard { 1227b338082bSbellard bs->cyls = cyls; 1228b338082bSbellard bs->heads = heads; 1229b338082bSbellard bs->secs = secs; 1230b338082bSbellard } 1231b338082bSbellard 1232b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 1233b338082bSbellard { 1234b338082bSbellard bs->type = type; 1235b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 1236b338082bSbellard type == BDRV_TYPE_FLOPPY)); 1237b338082bSbellard } 1238b338082bSbellard 123946d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 124046d4767dSbellard { 124146d4767dSbellard bs->translation = translation; 124246d4767dSbellard } 124346d4767dSbellard 1244b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 1245b338082bSbellard int *pcyls, int *pheads, int *psecs) 1246b338082bSbellard { 1247b338082bSbellard *pcyls = bs->cyls; 1248b338082bSbellard *pheads = bs->heads; 1249b338082bSbellard *psecs = bs->secs; 1250b338082bSbellard } 1251b338082bSbellard 1252b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 1253b338082bSbellard { 1254b338082bSbellard return bs->type; 1255b338082bSbellard } 1256b338082bSbellard 125746d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 125846d4767dSbellard { 125946d4767dSbellard return bs->translation; 126046d4767dSbellard } 126146d4767dSbellard 1262abd7f68dSMarkus Armbruster void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error, 1263abd7f68dSMarkus Armbruster BlockErrorAction on_write_error) 1264abd7f68dSMarkus Armbruster { 1265abd7f68dSMarkus Armbruster bs->on_read_error = on_read_error; 1266abd7f68dSMarkus Armbruster bs->on_write_error = on_write_error; 1267abd7f68dSMarkus Armbruster } 1268abd7f68dSMarkus Armbruster 1269abd7f68dSMarkus Armbruster BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read) 1270abd7f68dSMarkus Armbruster { 1271abd7f68dSMarkus Armbruster return is_read ? bs->on_read_error : bs->on_write_error; 1272abd7f68dSMarkus Armbruster } 1273abd7f68dSMarkus Armbruster 1274b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 1275b338082bSbellard { 1276b338082bSbellard return bs->removable; 1277b338082bSbellard } 1278b338082bSbellard 1279b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 1280b338082bSbellard { 1281b338082bSbellard return bs->read_only; 1282b338082bSbellard } 1283b338082bSbellard 1284985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 1285985a03b0Sths { 1286985a03b0Sths return bs->sg; 1287985a03b0Sths } 1288985a03b0Sths 1289e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 1290e900a7b7SChristoph Hellwig { 1291e900a7b7SChristoph Hellwig return bs->enable_write_cache; 1292e900a7b7SChristoph Hellwig } 1293e900a7b7SChristoph Hellwig 129419cb3738Sbellard /* XXX: no longer used */ 1295b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 1296b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 1297b338082bSbellard { 1298b338082bSbellard bs->change_cb = change_cb; 1299b338082bSbellard bs->change_opaque = opaque; 1300b338082bSbellard } 1301b338082bSbellard 1302ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 1303ea2384d3Sbellard { 1304ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 1305ea2384d3Sbellard return 1; 1306ea2384d3Sbellard return bs->encrypted; 1307ea2384d3Sbellard } 1308ea2384d3Sbellard 1309c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 1310c0f4ce77Saliguori { 1311c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 1312c0f4ce77Saliguori 1313c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 1314c0f4ce77Saliguori return 1; 1315c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 1316c0f4ce77Saliguori } 1317c0f4ce77Saliguori 1318ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 1319ea2384d3Sbellard { 1320ea2384d3Sbellard int ret; 1321ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 1322ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 1323ea2384d3Sbellard if (ret < 0) 1324ea2384d3Sbellard return ret; 1325ea2384d3Sbellard if (!bs->encrypted) 1326ea2384d3Sbellard return 0; 1327ea2384d3Sbellard } 1328fd04a2aeSShahar Havivi if (!bs->encrypted) { 1329fd04a2aeSShahar Havivi return -EINVAL; 1330fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 1331fd04a2aeSShahar Havivi return -ENOMEDIUM; 1332fd04a2aeSShahar Havivi } 1333c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 1334bb5fc20fSaliguori if (ret < 0) { 1335bb5fc20fSaliguori bs->valid_key = 0; 1336bb5fc20fSaliguori } else if (!bs->valid_key) { 1337bb5fc20fSaliguori bs->valid_key = 1; 1338bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 1339bb5fc20fSaliguori bs->media_changed = 1; 1340bb5fc20fSaliguori if (bs->change_cb) 1341bb5fc20fSaliguori bs->change_cb(bs->change_opaque); 1342bb5fc20fSaliguori } 1343c0f4ce77Saliguori return ret; 1344ea2384d3Sbellard } 1345ea2384d3Sbellard 1346ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 1347ea2384d3Sbellard { 134819cb3738Sbellard if (!bs->drv) { 1349ea2384d3Sbellard buf[0] = '\0'; 1350ea2384d3Sbellard } else { 1351ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 1352ea2384d3Sbellard } 1353ea2384d3Sbellard } 1354ea2384d3Sbellard 1355ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 1356ea2384d3Sbellard void *opaque) 1357ea2384d3Sbellard { 1358ea2384d3Sbellard BlockDriver *drv; 1359ea2384d3Sbellard 13608a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 1361ea2384d3Sbellard it(opaque, drv->format_name); 1362ea2384d3Sbellard } 1363ea2384d3Sbellard } 1364ea2384d3Sbellard 1365b338082bSbellard BlockDriverState *bdrv_find(const char *name) 1366b338082bSbellard { 1367b338082bSbellard BlockDriverState *bs; 1368b338082bSbellard 13691b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 13701b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 1371b338082bSbellard return bs; 1372b338082bSbellard } 13731b7bdbc1SStefan Hajnoczi } 1374b338082bSbellard return NULL; 1375b338082bSbellard } 1376b338082bSbellard 13772f399b0aSMarkus Armbruster BlockDriverState *bdrv_next(BlockDriverState *bs) 13782f399b0aSMarkus Armbruster { 13792f399b0aSMarkus Armbruster if (!bs) { 13802f399b0aSMarkus Armbruster return QTAILQ_FIRST(&bdrv_states); 13812f399b0aSMarkus Armbruster } 13822f399b0aSMarkus Armbruster return QTAILQ_NEXT(bs, list); 13832f399b0aSMarkus Armbruster } 13842f399b0aSMarkus Armbruster 138551de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 138681d0912dSbellard { 138781d0912dSbellard BlockDriverState *bs; 138881d0912dSbellard 13891b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 139051de9760Saliguori it(opaque, bs); 139181d0912dSbellard } 139281d0912dSbellard } 139381d0912dSbellard 1394ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1395ea2384d3Sbellard { 1396ea2384d3Sbellard return bs->device_name; 1397ea2384d3Sbellard } 1398ea2384d3Sbellard 13997a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 14007a6cba61Spbrook { 1401016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 1402016f5cf6SAlexander Graf return; 1403016f5cf6SAlexander Graf } 1404016f5cf6SAlexander Graf 14053f5075aeSChristoph Hellwig if (bs->drv && bs->drv->bdrv_flush) 14067a6cba61Spbrook bs->drv->bdrv_flush(bs); 14077a6cba61Spbrook } 14087a6cba61Spbrook 1409c6ca28d6Saliguori void bdrv_flush_all(void) 1410c6ca28d6Saliguori { 1411c6ca28d6Saliguori BlockDriverState *bs; 1412c6ca28d6Saliguori 14131b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1414c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 14151b7bdbc1SStefan Hajnoczi (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { 1416c6ca28d6Saliguori bdrv_flush(bs); 1417c6ca28d6Saliguori } 14181b7bdbc1SStefan Hajnoczi } 14191b7bdbc1SStefan Hajnoczi } 1420c6ca28d6Saliguori 1421f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 1422f2feebbdSKevin Wolf { 1423f2feebbdSKevin Wolf assert(bs->drv); 1424f2feebbdSKevin Wolf 1425f2feebbdSKevin Wolf if (bs->drv->no_zero_init) { 1426f2feebbdSKevin Wolf return 0; 1427f2feebbdSKevin Wolf } else if (bs->file) { 1428f2feebbdSKevin Wolf return bdrv_has_zero_init(bs->file); 1429f2feebbdSKevin Wolf } 1430f2feebbdSKevin Wolf 1431f2feebbdSKevin Wolf return 1; 1432f2feebbdSKevin Wolf } 1433f2feebbdSKevin Wolf 1434f58c7b35Sths /* 1435f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1436f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1437f58c7b35Sths * hence all their sectors are reported as allocated. 1438f58c7b35Sths * 1439f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1440f58c7b35Sths * the specified sector) that are known to be in the same 1441f58c7b35Sths * allocated/unallocated state. 1442f58c7b35Sths * 1443f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1444f58c7b35Sths */ 1445f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1446f58c7b35Sths int *pnum) 1447f58c7b35Sths { 1448f58c7b35Sths int64_t n; 1449f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1450f58c7b35Sths if (sector_num >= bs->total_sectors) { 1451f58c7b35Sths *pnum = 0; 1452f58c7b35Sths return 0; 1453f58c7b35Sths } 1454f58c7b35Sths n = bs->total_sectors - sector_num; 1455f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1456f58c7b35Sths return 1; 1457f58c7b35Sths } 1458f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1459f58c7b35Sths } 1460f58c7b35Sths 14612582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv, 14622582bfedSLuiz Capitulino BlockMonEventAction action, int is_read) 14632582bfedSLuiz Capitulino { 14642582bfedSLuiz Capitulino QObject *data; 14652582bfedSLuiz Capitulino const char *action_str; 14662582bfedSLuiz Capitulino 14672582bfedSLuiz Capitulino switch (action) { 14682582bfedSLuiz Capitulino case BDRV_ACTION_REPORT: 14692582bfedSLuiz Capitulino action_str = "report"; 14702582bfedSLuiz Capitulino break; 14712582bfedSLuiz Capitulino case BDRV_ACTION_IGNORE: 14722582bfedSLuiz Capitulino action_str = "ignore"; 14732582bfedSLuiz Capitulino break; 14742582bfedSLuiz Capitulino case BDRV_ACTION_STOP: 14752582bfedSLuiz Capitulino action_str = "stop"; 14762582bfedSLuiz Capitulino break; 14772582bfedSLuiz Capitulino default: 14782582bfedSLuiz Capitulino abort(); 14792582bfedSLuiz Capitulino } 14802582bfedSLuiz Capitulino 14812582bfedSLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 14822582bfedSLuiz Capitulino bdrv->device_name, 14832582bfedSLuiz Capitulino action_str, 14842582bfedSLuiz Capitulino is_read ? "read" : "write"); 14852582bfedSLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 14862582bfedSLuiz Capitulino 14872582bfedSLuiz Capitulino qobject_decref(data); 14882582bfedSLuiz Capitulino } 14892582bfedSLuiz Capitulino 1490d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque) 1491b338082bSbellard { 1492d15e5465SLuiz Capitulino QDict *bs_dict; 1493d15e5465SLuiz Capitulino Monitor *mon = opaque; 1494b338082bSbellard 1495d15e5465SLuiz Capitulino bs_dict = qobject_to_qdict(obj); 1496d15e5465SLuiz Capitulino 1497d15e5465SLuiz Capitulino monitor_printf(mon, "%s: type=%s removable=%d", 1498d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "device"), 1499d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "type"), 1500d15e5465SLuiz Capitulino qdict_get_bool(bs_dict, "removable")); 1501d15e5465SLuiz Capitulino 1502d15e5465SLuiz Capitulino if (qdict_get_bool(bs_dict, "removable")) { 1503d15e5465SLuiz Capitulino monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); 1504b338082bSbellard } 1505d15e5465SLuiz Capitulino 1506d15e5465SLuiz Capitulino if (qdict_haskey(bs_dict, "inserted")) { 1507d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); 1508d15e5465SLuiz Capitulino 1509376253ecSaliguori monitor_printf(mon, " file="); 1510d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "file")); 1511d15e5465SLuiz Capitulino if (qdict_haskey(qdict, "backing_file")) { 1512376253ecSaliguori monitor_printf(mon, " backing_file="); 1513d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); 1514fef30743Sths } 1515d15e5465SLuiz Capitulino monitor_printf(mon, " ro=%d drv=%s encrypted=%d", 1516d15e5465SLuiz Capitulino qdict_get_bool(qdict, "ro"), 1517d15e5465SLuiz Capitulino qdict_get_str(qdict, "drv"), 1518d15e5465SLuiz Capitulino qdict_get_bool(qdict, "encrypted")); 1519b338082bSbellard } else { 1520376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1521b338082bSbellard } 1522d15e5465SLuiz Capitulino 1523376253ecSaliguori monitor_printf(mon, "\n"); 1524b338082bSbellard } 1525d15e5465SLuiz Capitulino 1526d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data) 1527d15e5465SLuiz Capitulino { 1528d15e5465SLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); 1529d15e5465SLuiz Capitulino } 1530d15e5465SLuiz Capitulino 1531d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data) 1532d15e5465SLuiz Capitulino { 1533d15e5465SLuiz Capitulino QList *bs_list; 1534d15e5465SLuiz Capitulino BlockDriverState *bs; 1535d15e5465SLuiz Capitulino 1536d15e5465SLuiz Capitulino bs_list = qlist_new(); 1537d15e5465SLuiz Capitulino 15381b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1539d15e5465SLuiz Capitulino QObject *bs_obj; 1540d15e5465SLuiz Capitulino const char *type = "unknown"; 1541d15e5465SLuiz Capitulino 1542d15e5465SLuiz Capitulino switch(bs->type) { 1543d15e5465SLuiz Capitulino case BDRV_TYPE_HD: 1544d15e5465SLuiz Capitulino type = "hd"; 1545d15e5465SLuiz Capitulino break; 1546d15e5465SLuiz Capitulino case BDRV_TYPE_CDROM: 1547d15e5465SLuiz Capitulino type = "cdrom"; 1548d15e5465SLuiz Capitulino break; 1549d15e5465SLuiz Capitulino case BDRV_TYPE_FLOPPY: 1550d15e5465SLuiz Capitulino type = "floppy"; 1551d15e5465SLuiz Capitulino break; 1552d15e5465SLuiz Capitulino } 1553d15e5465SLuiz Capitulino 1554d15e5465SLuiz Capitulino bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, " 1555d15e5465SLuiz Capitulino "'removable': %i, 'locked': %i }", 1556d15e5465SLuiz Capitulino bs->device_name, type, bs->removable, 1557d15e5465SLuiz Capitulino bs->locked); 1558d15e5465SLuiz Capitulino 1559d15e5465SLuiz Capitulino if (bs->drv) { 1560d15e5465SLuiz Capitulino QObject *obj; 1561d15e5465SLuiz Capitulino QDict *bs_dict = qobject_to_qdict(bs_obj); 1562d15e5465SLuiz Capitulino 1563d15e5465SLuiz Capitulino obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " 1564d15e5465SLuiz Capitulino "'encrypted': %i }", 1565d15e5465SLuiz Capitulino bs->filename, bs->read_only, 1566d15e5465SLuiz Capitulino bs->drv->format_name, 1567d15e5465SLuiz Capitulino bdrv_is_encrypted(bs)); 1568d15e5465SLuiz Capitulino if (bs->backing_file[0] != '\0') { 1569d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(obj); 1570d15e5465SLuiz Capitulino qdict_put(qdict, "backing_file", 1571d15e5465SLuiz Capitulino qstring_from_str(bs->backing_file)); 1572d15e5465SLuiz Capitulino } 1573d15e5465SLuiz Capitulino 1574d15e5465SLuiz Capitulino qdict_put_obj(bs_dict, "inserted", obj); 1575d15e5465SLuiz Capitulino } 1576d15e5465SLuiz Capitulino qlist_append_obj(bs_list, bs_obj); 1577d15e5465SLuiz Capitulino } 1578d15e5465SLuiz Capitulino 1579d15e5465SLuiz Capitulino *ret_data = QOBJECT(bs_list); 1580b338082bSbellard } 1581a36e69ddSths 1582218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque) 1583a36e69ddSths { 1584218a536aSLuiz Capitulino QDict *qdict; 1585218a536aSLuiz Capitulino Monitor *mon = opaque; 1586218a536aSLuiz Capitulino 1587218a536aSLuiz Capitulino qdict = qobject_to_qdict(data); 1588218a536aSLuiz Capitulino monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); 1589218a536aSLuiz Capitulino 1590218a536aSLuiz Capitulino qdict = qobject_to_qdict(qdict_get(qdict, "stats")); 1591218a536aSLuiz Capitulino monitor_printf(mon, " rd_bytes=%" PRId64 1592218a536aSLuiz Capitulino " wr_bytes=%" PRId64 1593218a536aSLuiz Capitulino " rd_operations=%" PRId64 1594218a536aSLuiz Capitulino " wr_operations=%" PRId64 1595218a536aSLuiz Capitulino "\n", 1596218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_bytes"), 1597218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_bytes"), 1598218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_operations"), 1599218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_operations")); 1600218a536aSLuiz Capitulino } 1601218a536aSLuiz Capitulino 1602218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data) 1603218a536aSLuiz Capitulino { 1604218a536aSLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); 1605218a536aSLuiz Capitulino } 1606218a536aSLuiz Capitulino 1607294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs) 1608294cc35fSKevin Wolf { 1609294cc35fSKevin Wolf QObject *res; 1610294cc35fSKevin Wolf QDict *dict; 1611294cc35fSKevin Wolf 1612294cc35fSKevin Wolf res = qobject_from_jsonf("{ 'stats': {" 1613294cc35fSKevin Wolf "'rd_bytes': %" PRId64 "," 1614294cc35fSKevin Wolf "'wr_bytes': %" PRId64 "," 1615294cc35fSKevin Wolf "'rd_operations': %" PRId64 "," 1616294cc35fSKevin Wolf "'wr_operations': %" PRId64 "," 1617294cc35fSKevin Wolf "'wr_highest_offset': %" PRId64 1618294cc35fSKevin Wolf "} }", 1619294cc35fSKevin Wolf bs->rd_bytes, bs->wr_bytes, 1620294cc35fSKevin Wolf bs->rd_ops, bs->wr_ops, 16215ffbbc67SBlue Swirl bs->wr_highest_sector * 16225ffbbc67SBlue Swirl (uint64_t)BDRV_SECTOR_SIZE); 1623294cc35fSKevin Wolf dict = qobject_to_qdict(res); 1624294cc35fSKevin Wolf 1625294cc35fSKevin Wolf if (*bs->device_name) { 1626294cc35fSKevin Wolf qdict_put(dict, "device", qstring_from_str(bs->device_name)); 1627294cc35fSKevin Wolf } 1628294cc35fSKevin Wolf 1629294cc35fSKevin Wolf if (bs->file) { 1630294cc35fSKevin Wolf QObject *parent = bdrv_info_stats_bs(bs->file); 1631294cc35fSKevin Wolf qdict_put_obj(dict, "parent", parent); 1632294cc35fSKevin Wolf } 1633294cc35fSKevin Wolf 1634294cc35fSKevin Wolf return res; 1635294cc35fSKevin Wolf } 1636294cc35fSKevin Wolf 1637218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data) 1638218a536aSLuiz Capitulino { 1639218a536aSLuiz Capitulino QObject *obj; 1640218a536aSLuiz Capitulino QList *devices; 1641a36e69ddSths BlockDriverState *bs; 1642a36e69ddSths 1643218a536aSLuiz Capitulino devices = qlist_new(); 1644218a536aSLuiz Capitulino 16451b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1646294cc35fSKevin Wolf obj = bdrv_info_stats_bs(bs); 1647218a536aSLuiz Capitulino qlist_append_obj(devices, obj); 1648a36e69ddSths } 1649218a536aSLuiz Capitulino 1650218a536aSLuiz Capitulino *ret_data = QOBJECT(devices); 1651a36e69ddSths } 1652ea2384d3Sbellard 1653045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1654045df330Saliguori { 1655045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1656045df330Saliguori return bs->backing_file; 1657045df330Saliguori else if (bs->encrypted) 1658045df330Saliguori return bs->filename; 1659045df330Saliguori else 1660045df330Saliguori return NULL; 1661045df330Saliguori } 1662045df330Saliguori 166383f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 166483f64091Sbellard char *filename, int filename_size) 166583f64091Sbellard { 1666b783e409SKevin Wolf if (!bs->backing_file) { 166783f64091Sbellard pstrcpy(filename, filename_size, ""); 166883f64091Sbellard } else { 166983f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 167083f64091Sbellard } 167183f64091Sbellard } 167283f64091Sbellard 1673faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1674faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1675faea38e7Sbellard { 1676faea38e7Sbellard BlockDriver *drv = bs->drv; 1677faea38e7Sbellard if (!drv) 167819cb3738Sbellard return -ENOMEDIUM; 1679faea38e7Sbellard if (!drv->bdrv_write_compressed) 1680faea38e7Sbellard return -ENOTSUP; 1681fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 1682fbb7b4e0SKevin Wolf return -EIO; 16837cd1e32aSlirans@il.ibm.com 1684c6d22830SJan Kiszka if (bs->dirty_bitmap) { 16857cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 16867cd1e32aSlirans@il.ibm.com } 16877cd1e32aSlirans@il.ibm.com 1688faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1689faea38e7Sbellard } 1690faea38e7Sbellard 1691faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1692faea38e7Sbellard { 1693faea38e7Sbellard BlockDriver *drv = bs->drv; 1694faea38e7Sbellard if (!drv) 169519cb3738Sbellard return -ENOMEDIUM; 1696faea38e7Sbellard if (!drv->bdrv_get_info) 1697faea38e7Sbellard return -ENOTSUP; 1698faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1699faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1700faea38e7Sbellard } 1701faea38e7Sbellard 170245566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 170345566e9cSChristoph Hellwig int64_t pos, int size) 1704178e08a5Saliguori { 1705178e08a5Saliguori BlockDriver *drv = bs->drv; 1706178e08a5Saliguori if (!drv) 1707178e08a5Saliguori return -ENOMEDIUM; 17087cdb1f6dSMORITA Kazutaka if (drv->bdrv_save_vmstate) 170945566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 17107cdb1f6dSMORITA Kazutaka if (bs->file) 17117cdb1f6dSMORITA Kazutaka return bdrv_save_vmstate(bs->file, buf, pos, size); 17127cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1713178e08a5Saliguori } 1714178e08a5Saliguori 171545566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 171645566e9cSChristoph Hellwig int64_t pos, int size) 1717178e08a5Saliguori { 1718178e08a5Saliguori BlockDriver *drv = bs->drv; 1719178e08a5Saliguori if (!drv) 1720178e08a5Saliguori return -ENOMEDIUM; 17217cdb1f6dSMORITA Kazutaka if (drv->bdrv_load_vmstate) 172245566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 17237cdb1f6dSMORITA Kazutaka if (bs->file) 17247cdb1f6dSMORITA Kazutaka return bdrv_load_vmstate(bs->file, buf, pos, size); 17257cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1726178e08a5Saliguori } 1727178e08a5Saliguori 17288b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 17298b9b0cc2SKevin Wolf { 17308b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 17318b9b0cc2SKevin Wolf 17328b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 17338b9b0cc2SKevin Wolf return; 17348b9b0cc2SKevin Wolf } 17358b9b0cc2SKevin Wolf 17368b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 17378b9b0cc2SKevin Wolf 17388b9b0cc2SKevin Wolf } 17398b9b0cc2SKevin Wolf 1740faea38e7Sbellard /**************************************************************/ 1741faea38e7Sbellard /* handling of snapshots */ 1742faea38e7Sbellard 1743feeee5acSMiguel Di Ciurcio Filho int bdrv_can_snapshot(BlockDriverState *bs) 1744feeee5acSMiguel Di Ciurcio Filho { 1745feeee5acSMiguel Di Ciurcio Filho BlockDriver *drv = bs->drv; 1746feeee5acSMiguel Di Ciurcio Filho if (!drv || bdrv_is_removable(bs) || bdrv_is_read_only(bs)) { 1747feeee5acSMiguel Di Ciurcio Filho return 0; 1748feeee5acSMiguel Di Ciurcio Filho } 1749feeee5acSMiguel Di Ciurcio Filho 1750feeee5acSMiguel Di Ciurcio Filho if (!drv->bdrv_snapshot_create) { 1751feeee5acSMiguel Di Ciurcio Filho if (bs->file != NULL) { 1752feeee5acSMiguel Di Ciurcio Filho return bdrv_can_snapshot(bs->file); 1753feeee5acSMiguel Di Ciurcio Filho } 1754feeee5acSMiguel Di Ciurcio Filho return 0; 1755feeee5acSMiguel Di Ciurcio Filho } 1756feeee5acSMiguel Di Ciurcio Filho 1757feeee5acSMiguel Di Ciurcio Filho return 1; 1758feeee5acSMiguel Di Ciurcio Filho } 1759feeee5acSMiguel Di Ciurcio Filho 1760faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1761faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1762faea38e7Sbellard { 1763faea38e7Sbellard BlockDriver *drv = bs->drv; 1764faea38e7Sbellard if (!drv) 176519cb3738Sbellard return -ENOMEDIUM; 17667cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_create) 1767faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 17687cdb1f6dSMORITA Kazutaka if (bs->file) 17697cdb1f6dSMORITA Kazutaka return bdrv_snapshot_create(bs->file, sn_info); 17707cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1771faea38e7Sbellard } 1772faea38e7Sbellard 1773faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1774faea38e7Sbellard const char *snapshot_id) 1775faea38e7Sbellard { 1776faea38e7Sbellard BlockDriver *drv = bs->drv; 17777cdb1f6dSMORITA Kazutaka int ret, open_ret; 17787cdb1f6dSMORITA Kazutaka 1779faea38e7Sbellard if (!drv) 178019cb3738Sbellard return -ENOMEDIUM; 17817cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_goto) 1782faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 17837cdb1f6dSMORITA Kazutaka 17847cdb1f6dSMORITA Kazutaka if (bs->file) { 17857cdb1f6dSMORITA Kazutaka drv->bdrv_close(bs); 17867cdb1f6dSMORITA Kazutaka ret = bdrv_snapshot_goto(bs->file, snapshot_id); 17877cdb1f6dSMORITA Kazutaka open_ret = drv->bdrv_open(bs, bs->open_flags); 17887cdb1f6dSMORITA Kazutaka if (open_ret < 0) { 17897cdb1f6dSMORITA Kazutaka bdrv_delete(bs->file); 17907cdb1f6dSMORITA Kazutaka bs->drv = NULL; 17917cdb1f6dSMORITA Kazutaka return open_ret; 17927cdb1f6dSMORITA Kazutaka } 17937cdb1f6dSMORITA Kazutaka return ret; 17947cdb1f6dSMORITA Kazutaka } 17957cdb1f6dSMORITA Kazutaka 17967cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1797faea38e7Sbellard } 1798faea38e7Sbellard 1799faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1800faea38e7Sbellard { 1801faea38e7Sbellard BlockDriver *drv = bs->drv; 1802faea38e7Sbellard if (!drv) 180319cb3738Sbellard return -ENOMEDIUM; 18047cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_delete) 1805faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 18067cdb1f6dSMORITA Kazutaka if (bs->file) 18077cdb1f6dSMORITA Kazutaka return bdrv_snapshot_delete(bs->file, snapshot_id); 18087cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1809faea38e7Sbellard } 1810faea38e7Sbellard 1811faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1812faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1813faea38e7Sbellard { 1814faea38e7Sbellard BlockDriver *drv = bs->drv; 1815faea38e7Sbellard if (!drv) 181619cb3738Sbellard return -ENOMEDIUM; 18177cdb1f6dSMORITA Kazutaka if (drv->bdrv_snapshot_list) 1818faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 18197cdb1f6dSMORITA Kazutaka if (bs->file) 18207cdb1f6dSMORITA Kazutaka return bdrv_snapshot_list(bs->file, psn_info); 18217cdb1f6dSMORITA Kazutaka return -ENOTSUP; 1822faea38e7Sbellard } 1823faea38e7Sbellard 1824faea38e7Sbellard #define NB_SUFFIXES 4 1825faea38e7Sbellard 1826faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1827faea38e7Sbellard { 1828faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1829faea38e7Sbellard int64_t base; 1830faea38e7Sbellard int i; 1831faea38e7Sbellard 1832faea38e7Sbellard if (size <= 999) { 1833faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1834faea38e7Sbellard } else { 1835faea38e7Sbellard base = 1024; 1836faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1837faea38e7Sbellard if (size < (10 * base)) { 1838faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1839faea38e7Sbellard (double)size / base, 1840faea38e7Sbellard suffixes[i]); 1841faea38e7Sbellard break; 1842faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1843faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1844faea38e7Sbellard ((size + (base >> 1)) / base), 1845faea38e7Sbellard suffixes[i]); 1846faea38e7Sbellard break; 1847faea38e7Sbellard } 1848faea38e7Sbellard base = base * 1024; 1849faea38e7Sbellard } 1850faea38e7Sbellard } 1851faea38e7Sbellard return buf; 1852faea38e7Sbellard } 1853faea38e7Sbellard 1854faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1855faea38e7Sbellard { 1856faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 18573b9f94e1Sbellard #ifdef _WIN32 18583b9f94e1Sbellard struct tm *ptm; 18593b9f94e1Sbellard #else 1860faea38e7Sbellard struct tm tm; 18613b9f94e1Sbellard #endif 1862faea38e7Sbellard time_t ti; 1863faea38e7Sbellard int64_t secs; 1864faea38e7Sbellard 1865faea38e7Sbellard if (!sn) { 1866faea38e7Sbellard snprintf(buf, buf_size, 1867faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1868faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1869faea38e7Sbellard } else { 1870faea38e7Sbellard ti = sn->date_sec; 18713b9f94e1Sbellard #ifdef _WIN32 18723b9f94e1Sbellard ptm = localtime(&ti); 18733b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 18743b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 18753b9f94e1Sbellard #else 1876faea38e7Sbellard localtime_r(&ti, &tm); 1877faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1878faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 18793b9f94e1Sbellard #endif 1880faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1881faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1882faea38e7Sbellard "%02d:%02d:%02d.%03d", 1883faea38e7Sbellard (int)(secs / 3600), 1884faea38e7Sbellard (int)((secs / 60) % 60), 1885faea38e7Sbellard (int)(secs % 60), 1886faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1887faea38e7Sbellard snprintf(buf, buf_size, 1888faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1889faea38e7Sbellard sn->id_str, sn->name, 1890faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1891faea38e7Sbellard date_buf, 1892faea38e7Sbellard clock_buf); 1893faea38e7Sbellard } 1894faea38e7Sbellard return buf; 1895faea38e7Sbellard } 1896faea38e7Sbellard 189783f64091Sbellard 1898ea2384d3Sbellard /**************************************************************/ 189983f64091Sbellard /* async I/Os */ 1900ea2384d3Sbellard 19013b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 1902f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 190383f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1904ea2384d3Sbellard { 190583f64091Sbellard BlockDriver *drv = bs->drv; 1906a36e69ddSths BlockDriverAIOCB *ret; 1907ea2384d3Sbellard 190819cb3738Sbellard if (!drv) 1909ce1a14dcSpbrook return NULL; 191071d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 191171d0770cSaliguori return NULL; 191283f64091Sbellard 1913f141eafeSaliguori ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, 1914f141eafeSaliguori cb, opaque); 1915a36e69ddSths 1916a36e69ddSths if (ret) { 1917a36e69ddSths /* Update stats even though technically transfer has not happened. */ 19186ea44308SJan Kiszka bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 1919a36e69ddSths bs->rd_ops ++; 1920a36e69ddSths } 1921a36e69ddSths 1922a36e69ddSths return ret; 192383f64091Sbellard } 192483f64091Sbellard 1925f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 1926f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 192783f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 19287674e7bfSbellard { 192983f64091Sbellard BlockDriver *drv = bs->drv; 1930a36e69ddSths BlockDriverAIOCB *ret; 193183f64091Sbellard 193219cb3738Sbellard if (!drv) 1933ce1a14dcSpbrook return NULL; 193483f64091Sbellard if (bs->read_only) 1935ce1a14dcSpbrook return NULL; 193671d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 193771d0770cSaliguori return NULL; 193883f64091Sbellard 1939c6d22830SJan Kiszka if (bs->dirty_bitmap) { 19407cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 19417cd1e32aSlirans@il.ibm.com } 19427cd1e32aSlirans@il.ibm.com 1943f141eafeSaliguori ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, 1944f141eafeSaliguori cb, opaque); 1945a36e69ddSths 1946a36e69ddSths if (ret) { 1947a36e69ddSths /* Update stats even though technically transfer has not happened. */ 19486ea44308SJan Kiszka bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 1949a36e69ddSths bs->wr_ops ++; 1950294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 1951294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 1952294cc35fSKevin Wolf } 1953a36e69ddSths } 1954a36e69ddSths 1955a36e69ddSths return ret; 195683f64091Sbellard } 195783f64091Sbellard 195840b4f539SKevin Wolf 195940b4f539SKevin Wolf typedef struct MultiwriteCB { 196040b4f539SKevin Wolf int error; 196140b4f539SKevin Wolf int num_requests; 196240b4f539SKevin Wolf int num_callbacks; 196340b4f539SKevin Wolf struct { 196440b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 196540b4f539SKevin Wolf void *opaque; 196640b4f539SKevin Wolf QEMUIOVector *free_qiov; 196740b4f539SKevin Wolf void *free_buf; 196840b4f539SKevin Wolf } callbacks[]; 196940b4f539SKevin Wolf } MultiwriteCB; 197040b4f539SKevin Wolf 197140b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 197240b4f539SKevin Wolf { 197340b4f539SKevin Wolf int i; 197440b4f539SKevin Wolf 197540b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 197640b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 19771e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 19781e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 19791e1ea48dSStefan Hajnoczi } 198040b4f539SKevin Wolf qemu_free(mcb->callbacks[i].free_qiov); 1981f8a83245SHerve Poussineau qemu_vfree(mcb->callbacks[i].free_buf); 198240b4f539SKevin Wolf } 198340b4f539SKevin Wolf } 198440b4f539SKevin Wolf 198540b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 198640b4f539SKevin Wolf { 198740b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 198840b4f539SKevin Wolf 1989cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 199040b4f539SKevin Wolf mcb->error = ret; 199140b4f539SKevin Wolf multiwrite_user_cb(mcb); 199240b4f539SKevin Wolf } 199340b4f539SKevin Wolf 199440b4f539SKevin Wolf mcb->num_requests--; 199540b4f539SKevin Wolf if (mcb->num_requests == 0) { 199640b4f539SKevin Wolf if (mcb->error == 0) { 199740b4f539SKevin Wolf multiwrite_user_cb(mcb); 199840b4f539SKevin Wolf } 199940b4f539SKevin Wolf qemu_free(mcb); 200040b4f539SKevin Wolf } 200140b4f539SKevin Wolf } 200240b4f539SKevin Wolf 200340b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 200440b4f539SKevin Wolf { 200577be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 200677be4366SChristoph Hellwig 200777be4366SChristoph Hellwig /* 200877be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 200977be4366SChristoph Hellwig * here as that could overflow the return value. 201077be4366SChristoph Hellwig */ 201177be4366SChristoph Hellwig if (req1->sector > req2->sector) { 201277be4366SChristoph Hellwig return 1; 201377be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 201477be4366SChristoph Hellwig return -1; 201577be4366SChristoph Hellwig } else { 201677be4366SChristoph Hellwig return 0; 201777be4366SChristoph Hellwig } 201840b4f539SKevin Wolf } 201940b4f539SKevin Wolf 202040b4f539SKevin Wolf /* 202140b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 202240b4f539SKevin Wolf * requests that remain after merging. 202340b4f539SKevin Wolf */ 202440b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 202540b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 202640b4f539SKevin Wolf { 202740b4f539SKevin Wolf int i, outidx; 202840b4f539SKevin Wolf 202940b4f539SKevin Wolf // Sort requests by start sector 203040b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 203140b4f539SKevin Wolf 203240b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 203340b4f539SKevin Wolf // filling up gaps with zero sectors. 203440b4f539SKevin Wolf outidx = 0; 203540b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 203640b4f539SKevin Wolf int merge = 0; 203740b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 203840b4f539SKevin Wolf 203940b4f539SKevin Wolf // This handles the cases that are valid for all block drivers, namely 204040b4f539SKevin Wolf // exactly sequential writes and overlapping writes. 204140b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 204240b4f539SKevin Wolf merge = 1; 204340b4f539SKevin Wolf } 204440b4f539SKevin Wolf 204540b4f539SKevin Wolf // The block driver may decide that it makes sense to combine requests 204640b4f539SKevin Wolf // even if there is a gap of some sectors between them. In this case, 204740b4f539SKevin Wolf // the gap is filled with zeros (therefore only applicable for yet 204840b4f539SKevin Wolf // unused space in format like qcow2). 204940b4f539SKevin Wolf if (!merge && bs->drv->bdrv_merge_requests) { 205040b4f539SKevin Wolf merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); 205140b4f539SKevin Wolf } 205240b4f539SKevin Wolf 2053e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 2054e2a305fbSChristoph Hellwig merge = 0; 2055e2a305fbSChristoph Hellwig } 2056e2a305fbSChristoph Hellwig 205740b4f539SKevin Wolf if (merge) { 205840b4f539SKevin Wolf size_t size; 205940b4f539SKevin Wolf QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); 206040b4f539SKevin Wolf qemu_iovec_init(qiov, 206140b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 206240b4f539SKevin Wolf 206340b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 206440b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 206540b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 206640b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 206740b4f539SKevin Wolf 206840b4f539SKevin Wolf // We might need to add some zeros between the two requests 206940b4f539SKevin Wolf if (reqs[i].sector > oldreq_last) { 207040b4f539SKevin Wolf size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; 207140b4f539SKevin Wolf uint8_t *buf = qemu_blockalign(bs, zero_bytes); 207240b4f539SKevin Wolf memset(buf, 0, zero_bytes); 207340b4f539SKevin Wolf qemu_iovec_add(qiov, buf, zero_bytes); 207440b4f539SKevin Wolf mcb->callbacks[i].free_buf = buf; 207540b4f539SKevin Wolf } 207640b4f539SKevin Wolf 207740b4f539SKevin Wolf // Add the second request 207840b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 207940b4f539SKevin Wolf 2080cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 208140b4f539SKevin Wolf reqs[outidx].qiov = qiov; 208240b4f539SKevin Wolf 208340b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 208440b4f539SKevin Wolf } else { 208540b4f539SKevin Wolf outidx++; 208640b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 208740b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 208840b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 208940b4f539SKevin Wolf } 209040b4f539SKevin Wolf } 209140b4f539SKevin Wolf 209240b4f539SKevin Wolf return outidx + 1; 209340b4f539SKevin Wolf } 209440b4f539SKevin Wolf 209540b4f539SKevin Wolf /* 209640b4f539SKevin Wolf * Submit multiple AIO write requests at once. 209740b4f539SKevin Wolf * 209840b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 209940b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 210040b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 210140b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 210240b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 210340b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 210440b4f539SKevin Wolf * 210540b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 210640b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 210740b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 210840b4f539SKevin Wolf */ 210940b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 211040b4f539SKevin Wolf { 211140b4f539SKevin Wolf BlockDriverAIOCB *acb; 211240b4f539SKevin Wolf MultiwriteCB *mcb; 211340b4f539SKevin Wolf int i; 211440b4f539SKevin Wolf 211540b4f539SKevin Wolf if (num_reqs == 0) { 211640b4f539SKevin Wolf return 0; 211740b4f539SKevin Wolf } 211840b4f539SKevin Wolf 211940b4f539SKevin Wolf // Create MultiwriteCB structure 212040b4f539SKevin Wolf mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 212140b4f539SKevin Wolf mcb->num_requests = 0; 212240b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 212340b4f539SKevin Wolf 212440b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 212540b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 212640b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 212740b4f539SKevin Wolf } 212840b4f539SKevin Wolf 212940b4f539SKevin Wolf // Check for mergable requests 213040b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 213140b4f539SKevin Wolf 213240b4f539SKevin Wolf // Run the aio requests 213340b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 213440b4f539SKevin Wolf acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 213540b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 213640b4f539SKevin Wolf 213740b4f539SKevin Wolf if (acb == NULL) { 213840b4f539SKevin Wolf // We can only fail the whole thing if no request has been 213940b4f539SKevin Wolf // submitted yet. Otherwise we'll wait for the submitted AIOs to 214040b4f539SKevin Wolf // complete and report the error in the callback. 214140b4f539SKevin Wolf if (mcb->num_requests == 0) { 21420f0b604bSKevin Wolf reqs[i].error = -EIO; 214340b4f539SKevin Wolf goto fail; 214440b4f539SKevin Wolf } else { 21457eb58a6cSKevin Wolf mcb->num_requests++; 21467eb58a6cSKevin Wolf multiwrite_cb(mcb, -EIO); 214740b4f539SKevin Wolf break; 214840b4f539SKevin Wolf } 214940b4f539SKevin Wolf } else { 215040b4f539SKevin Wolf mcb->num_requests++; 215140b4f539SKevin Wolf } 215240b4f539SKevin Wolf } 215340b4f539SKevin Wolf 215440b4f539SKevin Wolf return 0; 215540b4f539SKevin Wolf 215640b4f539SKevin Wolf fail: 2157af474591SBruce Rogers qemu_free(mcb); 215840b4f539SKevin Wolf return -1; 215940b4f539SKevin Wolf } 216040b4f539SKevin Wolf 2161b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 2162b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2163b2e12bc6SChristoph Hellwig { 2164b2e12bc6SChristoph Hellwig BlockDriver *drv = bs->drv; 2165b2e12bc6SChristoph Hellwig 2166016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 2167016f5cf6SAlexander Graf return bdrv_aio_noop_em(bs, cb, opaque); 2168016f5cf6SAlexander Graf } 2169016f5cf6SAlexander Graf 2170b2e12bc6SChristoph Hellwig if (!drv) 2171b2e12bc6SChristoph Hellwig return NULL; 2172b2e12bc6SChristoph Hellwig return drv->bdrv_aio_flush(bs, cb, opaque); 2173b2e12bc6SChristoph Hellwig } 2174b2e12bc6SChristoph Hellwig 217583f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 217683f64091Sbellard { 21776bbff9a0Saliguori acb->pool->cancel(acb); 217883f64091Sbellard } 217983f64091Sbellard 218083f64091Sbellard 218183f64091Sbellard /**************************************************************/ 218283f64091Sbellard /* async block device emulation */ 218383f64091Sbellard 2184c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 2185c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 2186c16b5a2cSChristoph Hellwig QEMUBH *bh; 2187c16b5a2cSChristoph Hellwig int ret; 2188c16b5a2cSChristoph Hellwig /* vector translation state */ 2189c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 2190c16b5a2cSChristoph Hellwig uint8_t *bounce; 2191c16b5a2cSChristoph Hellwig int is_write; 2192c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 2193c16b5a2cSChristoph Hellwig 2194c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 2195c16b5a2cSChristoph Hellwig { 2196b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 2197b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 21986a7ad299SDor Laor qemu_bh_delete(acb->bh); 219936afc451SAvi Kivity acb->bh = NULL; 2200c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 2201c16b5a2cSChristoph Hellwig } 2202c16b5a2cSChristoph Hellwig 2203c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 2204c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 2205c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 2206c16b5a2cSChristoph Hellwig }; 2207c16b5a2cSChristoph Hellwig 220883f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 2209beac80cdSbellard { 2210ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 2211f141eafeSaliguori 2212f141eafeSaliguori if (!acb->is_write) 2213f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 2214ceb42de8Saliguori qemu_vfree(acb->bounce); 2215ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 22166a7ad299SDor Laor qemu_bh_delete(acb->bh); 221736afc451SAvi Kivity acb->bh = NULL; 2218ce1a14dcSpbrook qemu_aio_release(acb); 2219beac80cdSbellard } 2220beac80cdSbellard 2221f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 2222f141eafeSaliguori int64_t sector_num, 2223f141eafeSaliguori QEMUIOVector *qiov, 2224f141eafeSaliguori int nb_sectors, 2225f141eafeSaliguori BlockDriverCompletionFunc *cb, 2226f141eafeSaliguori void *opaque, 2227f141eafeSaliguori int is_write) 2228f141eafeSaliguori 2229ea2384d3Sbellard { 2230ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 223183f64091Sbellard 2232c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2233f141eafeSaliguori acb->is_write = is_write; 2234f141eafeSaliguori acb->qiov = qiov; 2235e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 2236f141eafeSaliguori 2237ce1a14dcSpbrook if (!acb->bh) 2238ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2239f141eafeSaliguori 2240f141eafeSaliguori if (is_write) { 2241f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 2242f141eafeSaliguori acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 2243f141eafeSaliguori } else { 2244f141eafeSaliguori acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 2245f141eafeSaliguori } 2246f141eafeSaliguori 2247ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 2248f141eafeSaliguori 2249ce1a14dcSpbrook return &acb->common; 22507a6cba61Spbrook } 22517a6cba61Spbrook 2252f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 2253f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2254ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 225583f64091Sbellard { 2256f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 225783f64091Sbellard } 225883f64091Sbellard 2259f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 2260f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2261f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 2262f141eafeSaliguori { 2263f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 2264f141eafeSaliguori } 2265f141eafeSaliguori 2266b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 2267b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2268b2e12bc6SChristoph Hellwig { 2269b2e12bc6SChristoph Hellwig BlockDriverAIOCBSync *acb; 2270b2e12bc6SChristoph Hellwig 2271b2e12bc6SChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2272b2e12bc6SChristoph Hellwig acb->is_write = 1; /* don't bounce in the completion hadler */ 2273b2e12bc6SChristoph Hellwig acb->qiov = NULL; 2274b2e12bc6SChristoph Hellwig acb->bounce = NULL; 2275b2e12bc6SChristoph Hellwig acb->ret = 0; 2276b2e12bc6SChristoph Hellwig 2277b2e12bc6SChristoph Hellwig if (!acb->bh) 2278b2e12bc6SChristoph Hellwig acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2279b2e12bc6SChristoph Hellwig 2280b2e12bc6SChristoph Hellwig bdrv_flush(bs); 2281b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 2282b2e12bc6SChristoph Hellwig return &acb->common; 2283b2e12bc6SChristoph Hellwig } 2284b2e12bc6SChristoph Hellwig 2285016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 2286016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 2287016f5cf6SAlexander Graf { 2288016f5cf6SAlexander Graf BlockDriverAIOCBSync *acb; 2289016f5cf6SAlexander Graf 2290016f5cf6SAlexander Graf acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2291016f5cf6SAlexander Graf acb->is_write = 1; /* don't bounce in the completion handler */ 2292016f5cf6SAlexander Graf acb->qiov = NULL; 2293016f5cf6SAlexander Graf acb->bounce = NULL; 2294016f5cf6SAlexander Graf acb->ret = 0; 2295016f5cf6SAlexander Graf 2296016f5cf6SAlexander Graf if (!acb->bh) { 2297016f5cf6SAlexander Graf acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2298016f5cf6SAlexander Graf } 2299016f5cf6SAlexander Graf 2300016f5cf6SAlexander Graf qemu_bh_schedule(acb->bh); 2301016f5cf6SAlexander Graf return &acb->common; 2302016f5cf6SAlexander Graf } 2303016f5cf6SAlexander Graf 230483f64091Sbellard /**************************************************************/ 230583f64091Sbellard /* sync block device emulation */ 230683f64091Sbellard 230783f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 230883f64091Sbellard { 230983f64091Sbellard *(int *)opaque = ret; 231083f64091Sbellard } 231183f64091Sbellard 231283f64091Sbellard #define NOT_DONE 0x7fffffff 231383f64091Sbellard 231483f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 231583f64091Sbellard uint8_t *buf, int nb_sectors) 231683f64091Sbellard { 2317ce1a14dcSpbrook int async_ret; 2318ce1a14dcSpbrook BlockDriverAIOCB *acb; 2319f141eafeSaliguori struct iovec iov; 2320f141eafeSaliguori QEMUIOVector qiov; 232183f64091Sbellard 232265d6b3d8SKevin Wolf async_context_push(); 232365d6b3d8SKevin Wolf 232483f64091Sbellard async_ret = NOT_DONE; 23253f4cb3d3Sblueswir1 iov.iov_base = (void *)buf; 2326eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2327f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2328f141eafeSaliguori acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, 232983f64091Sbellard bdrv_rw_em_cb, &async_ret); 233065d6b3d8SKevin Wolf if (acb == NULL) { 233165d6b3d8SKevin Wolf async_ret = -1; 233265d6b3d8SKevin Wolf goto fail; 233365d6b3d8SKevin Wolf } 2334baf35cb9Saliguori 233583f64091Sbellard while (async_ret == NOT_DONE) { 233683f64091Sbellard qemu_aio_wait(); 233783f64091Sbellard } 2338baf35cb9Saliguori 233965d6b3d8SKevin Wolf 234065d6b3d8SKevin Wolf fail: 234165d6b3d8SKevin Wolf async_context_pop(); 234283f64091Sbellard return async_ret; 234383f64091Sbellard } 234483f64091Sbellard 234583f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 234683f64091Sbellard const uint8_t *buf, int nb_sectors) 234783f64091Sbellard { 2348ce1a14dcSpbrook int async_ret; 2349ce1a14dcSpbrook BlockDriverAIOCB *acb; 2350f141eafeSaliguori struct iovec iov; 2351f141eafeSaliguori QEMUIOVector qiov; 235283f64091Sbellard 235365d6b3d8SKevin Wolf async_context_push(); 235465d6b3d8SKevin Wolf 235583f64091Sbellard async_ret = NOT_DONE; 2356f141eafeSaliguori iov.iov_base = (void *)buf; 2357eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2358f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2359f141eafeSaliguori acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, 236083f64091Sbellard bdrv_rw_em_cb, &async_ret); 236165d6b3d8SKevin Wolf if (acb == NULL) { 236265d6b3d8SKevin Wolf async_ret = -1; 236365d6b3d8SKevin Wolf goto fail; 236465d6b3d8SKevin Wolf } 236583f64091Sbellard while (async_ret == NOT_DONE) { 236683f64091Sbellard qemu_aio_wait(); 236783f64091Sbellard } 236865d6b3d8SKevin Wolf 236965d6b3d8SKevin Wolf fail: 237065d6b3d8SKevin Wolf async_context_pop(); 237183f64091Sbellard return async_ret; 237283f64091Sbellard } 2373ea2384d3Sbellard 2374ea2384d3Sbellard void bdrv_init(void) 2375ea2384d3Sbellard { 23765efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 2377ea2384d3Sbellard } 2378ce1a14dcSpbrook 2379eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 2380eb852011SMarkus Armbruster { 2381eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 2382eb852011SMarkus Armbruster bdrv_init(); 2383eb852011SMarkus Armbruster } 2384eb852011SMarkus Armbruster 2385c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 23866bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 23876bbff9a0Saliguori { 2388ce1a14dcSpbrook BlockDriverAIOCB *acb; 2389ce1a14dcSpbrook 23906bbff9a0Saliguori if (pool->free_aiocb) { 23916bbff9a0Saliguori acb = pool->free_aiocb; 23926bbff9a0Saliguori pool->free_aiocb = acb->next; 2393ce1a14dcSpbrook } else { 23946bbff9a0Saliguori acb = qemu_mallocz(pool->aiocb_size); 23956bbff9a0Saliguori acb->pool = pool; 2396ce1a14dcSpbrook } 2397ce1a14dcSpbrook acb->bs = bs; 2398ce1a14dcSpbrook acb->cb = cb; 2399ce1a14dcSpbrook acb->opaque = opaque; 2400ce1a14dcSpbrook return acb; 2401ce1a14dcSpbrook } 2402ce1a14dcSpbrook 2403ce1a14dcSpbrook void qemu_aio_release(void *p) 2404ce1a14dcSpbrook { 24056bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 24066bbff9a0Saliguori AIOPool *pool = acb->pool; 24076bbff9a0Saliguori acb->next = pool->free_aiocb; 24086bbff9a0Saliguori pool->free_aiocb = acb; 2409ce1a14dcSpbrook } 241019cb3738Sbellard 241119cb3738Sbellard /**************************************************************/ 241219cb3738Sbellard /* removable device support */ 241319cb3738Sbellard 241419cb3738Sbellard /** 241519cb3738Sbellard * Return TRUE if the media is present 241619cb3738Sbellard */ 241719cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 241819cb3738Sbellard { 241919cb3738Sbellard BlockDriver *drv = bs->drv; 242019cb3738Sbellard int ret; 242119cb3738Sbellard if (!drv) 242219cb3738Sbellard return 0; 242319cb3738Sbellard if (!drv->bdrv_is_inserted) 242419cb3738Sbellard return 1; 242519cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 242619cb3738Sbellard return ret; 242719cb3738Sbellard } 242819cb3738Sbellard 242919cb3738Sbellard /** 243019cb3738Sbellard * Return TRUE if the media changed since the last call to this 243119cb3738Sbellard * function. It is currently only used for floppy disks 243219cb3738Sbellard */ 243319cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 243419cb3738Sbellard { 243519cb3738Sbellard BlockDriver *drv = bs->drv; 243619cb3738Sbellard int ret; 243719cb3738Sbellard 243819cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 243919cb3738Sbellard ret = -ENOTSUP; 244019cb3738Sbellard else 244119cb3738Sbellard ret = drv->bdrv_media_changed(bs); 244219cb3738Sbellard if (ret == -ENOTSUP) 244319cb3738Sbellard ret = bs->media_changed; 244419cb3738Sbellard bs->media_changed = 0; 244519cb3738Sbellard return ret; 244619cb3738Sbellard } 244719cb3738Sbellard 244819cb3738Sbellard /** 244919cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 245019cb3738Sbellard */ 2451aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag) 245219cb3738Sbellard { 245319cb3738Sbellard BlockDriver *drv = bs->drv; 245419cb3738Sbellard int ret; 245519cb3738Sbellard 2456aea2a33cSMark McLoughlin if (bs->locked) { 2457aea2a33cSMark McLoughlin return -EBUSY; 2458aea2a33cSMark McLoughlin } 2459aea2a33cSMark McLoughlin 246019cb3738Sbellard if (!drv || !drv->bdrv_eject) { 246119cb3738Sbellard ret = -ENOTSUP; 246219cb3738Sbellard } else { 246319cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 246419cb3738Sbellard } 246519cb3738Sbellard if (ret == -ENOTSUP) { 246619cb3738Sbellard if (eject_flag) 246719cb3738Sbellard bdrv_close(bs); 2468aea2a33cSMark McLoughlin ret = 0; 246919cb3738Sbellard } 2470aea2a33cSMark McLoughlin 2471aea2a33cSMark McLoughlin return ret; 247219cb3738Sbellard } 247319cb3738Sbellard 247419cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 247519cb3738Sbellard { 247619cb3738Sbellard return bs->locked; 247719cb3738Sbellard } 247819cb3738Sbellard 247919cb3738Sbellard /** 248019cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 248119cb3738Sbellard * to eject it manually). 248219cb3738Sbellard */ 248319cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 248419cb3738Sbellard { 248519cb3738Sbellard BlockDriver *drv = bs->drv; 248619cb3738Sbellard 248719cb3738Sbellard bs->locked = locked; 248819cb3738Sbellard if (drv && drv->bdrv_set_locked) { 248919cb3738Sbellard drv->bdrv_set_locked(bs, locked); 249019cb3738Sbellard } 249119cb3738Sbellard } 2492985a03b0Sths 2493985a03b0Sths /* needed for generic scsi interface */ 2494985a03b0Sths 2495985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 2496985a03b0Sths { 2497985a03b0Sths BlockDriver *drv = bs->drv; 2498985a03b0Sths 2499985a03b0Sths if (drv && drv->bdrv_ioctl) 2500985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 2501985a03b0Sths return -ENOTSUP; 2502985a03b0Sths } 25037d780669Saliguori 2504221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 2505221f715dSaliguori unsigned long int req, void *buf, 25067d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 25077d780669Saliguori { 2508221f715dSaliguori BlockDriver *drv = bs->drv; 25097d780669Saliguori 2510221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 2511221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 2512221f715dSaliguori return NULL; 25137d780669Saliguori } 2514e268ca52Saliguori 25157cd1e32aSlirans@il.ibm.com 25167cd1e32aSlirans@il.ibm.com 2517e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 2518e268ca52Saliguori { 2519e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 2520e268ca52Saliguori } 25217cd1e32aSlirans@il.ibm.com 25227cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 25237cd1e32aSlirans@il.ibm.com { 25247cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 2525a55eb92cSJan Kiszka 2526aaa0eb75SLiran Schour bs->dirty_count = 0; 25277cd1e32aSlirans@il.ibm.com if (enable) { 2528c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 2529c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 2530c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 2531c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 25327cd1e32aSlirans@il.ibm.com 25337cd1e32aSlirans@il.ibm.com bs->dirty_bitmap = qemu_mallocz(bitmap_size); 25347cd1e32aSlirans@il.ibm.com } 25357cd1e32aSlirans@il.ibm.com } else { 2536c6d22830SJan Kiszka if (bs->dirty_bitmap) { 25377cd1e32aSlirans@il.ibm.com qemu_free(bs->dirty_bitmap); 2538c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 25397cd1e32aSlirans@il.ibm.com } 25407cd1e32aSlirans@il.ibm.com } 25417cd1e32aSlirans@il.ibm.com } 25427cd1e32aSlirans@il.ibm.com 25437cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 25447cd1e32aSlirans@il.ibm.com { 25456ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 25467cd1e32aSlirans@il.ibm.com 2547c6d22830SJan Kiszka if (bs->dirty_bitmap && 2548c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 2549c6d22830SJan Kiszka return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 2550c6d22830SJan Kiszka (1 << (chunk % (sizeof(unsigned long) * 8))); 25517cd1e32aSlirans@il.ibm.com } else { 25527cd1e32aSlirans@il.ibm.com return 0; 25537cd1e32aSlirans@il.ibm.com } 25547cd1e32aSlirans@il.ibm.com } 25557cd1e32aSlirans@il.ibm.com 25567cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 25577cd1e32aSlirans@il.ibm.com int nr_sectors) 25587cd1e32aSlirans@il.ibm.com { 25597cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 25607cd1e32aSlirans@il.ibm.com } 2561aaa0eb75SLiran Schour 2562aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 2563aaa0eb75SLiran Schour { 2564aaa0eb75SLiran Schour return bs->dirty_count; 2565aaa0eb75SLiran Schour } 2566