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; 29120993081SKevin Wolf int is_drive; 29284a12e66SChristoph Hellwig 29366f82ceeSKevin Wolf /* TODO Drivers without bdrv_file_open must be specified explicitly */ 29466f82ceeSKevin Wolf 29584a12e66SChristoph Hellwig #ifdef _WIN32 29620993081SKevin Wolf is_drive = is_windows_drive(filename) || 29720993081SKevin Wolf is_windows_drive_prefix(filename); 29820993081SKevin Wolf #else 29920993081SKevin Wolf is_drive = 0; 30084a12e66SChristoph Hellwig #endif 30184a12e66SChristoph Hellwig p = strchr(filename, ':'); 30220993081SKevin Wolf if (!p || is_drive) { 30384a12e66SChristoph Hellwig drv1 = find_hdev_driver(filename); 30484a12e66SChristoph Hellwig if (!drv1) { 30584a12e66SChristoph Hellwig drv1 = bdrv_find_format("file"); 30684a12e66SChristoph Hellwig } 30784a12e66SChristoph Hellwig return drv1; 30884a12e66SChristoph Hellwig } 30984a12e66SChristoph Hellwig len = p - filename; 31084a12e66SChristoph Hellwig if (len > sizeof(protocol) - 1) 31184a12e66SChristoph Hellwig len = sizeof(protocol) - 1; 31284a12e66SChristoph Hellwig memcpy(protocol, filename, len); 31384a12e66SChristoph Hellwig protocol[len] = '\0'; 31484a12e66SChristoph Hellwig QLIST_FOREACH(drv1, &bdrv_drivers, list) { 31584a12e66SChristoph Hellwig if (drv1->protocol_name && 31684a12e66SChristoph Hellwig !strcmp(drv1->protocol_name, protocol)) { 31784a12e66SChristoph Hellwig return drv1; 31884a12e66SChristoph Hellwig } 31984a12e66SChristoph Hellwig } 32084a12e66SChristoph Hellwig return NULL; 32184a12e66SChristoph Hellwig } 32284a12e66SChristoph Hellwig 323ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 324ea2384d3Sbellard { 32583f64091Sbellard int ret, score, score_max; 326ea2384d3Sbellard BlockDriver *drv1, *drv; 32783f64091Sbellard uint8_t buf[2048]; 32883f64091Sbellard BlockDriverState *bs; 329ea2384d3Sbellard 330f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 33183f64091Sbellard if (ret < 0) 3327674e7bfSbellard return NULL; 333f8ea0b00SNicholas Bellinger 33408a00559SKevin Wolf /* Return the raw BlockDriver * to scsi-generic devices or empty drives */ 33508a00559SKevin Wolf if (bs->sg || !bdrv_is_inserted(bs)) { 3361a396859SNicholas A. Bellinger bdrv_delete(bs); 337f8ea0b00SNicholas Bellinger return bdrv_find_format("raw"); 3381a396859SNicholas A. Bellinger } 339f8ea0b00SNicholas Bellinger 34083f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 34183f64091Sbellard bdrv_delete(bs); 342ea2384d3Sbellard if (ret < 0) { 343ea2384d3Sbellard return NULL; 344ea2384d3Sbellard } 345ea2384d3Sbellard 346ea2384d3Sbellard score_max = 0; 34784a12e66SChristoph Hellwig drv = NULL; 3488a22f02aSStefan Hajnoczi QLIST_FOREACH(drv1, &bdrv_drivers, list) { 34983f64091Sbellard if (drv1->bdrv_probe) { 350ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 351ea2384d3Sbellard if (score > score_max) { 352ea2384d3Sbellard score_max = score; 353ea2384d3Sbellard drv = drv1; 354ea2384d3Sbellard } 355ea2384d3Sbellard } 35683f64091Sbellard } 357ea2384d3Sbellard return drv; 358ea2384d3Sbellard } 359ea2384d3Sbellard 36051762288SStefan Hajnoczi /** 36151762288SStefan Hajnoczi * Set the current 'total_sectors' value 36251762288SStefan Hajnoczi */ 36351762288SStefan Hajnoczi static int refresh_total_sectors(BlockDriverState *bs, int64_t hint) 36451762288SStefan Hajnoczi { 36551762288SStefan Hajnoczi BlockDriver *drv = bs->drv; 36651762288SStefan Hajnoczi 367396759adSNicholas Bellinger /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */ 368396759adSNicholas Bellinger if (bs->sg) 369396759adSNicholas Bellinger return 0; 370396759adSNicholas Bellinger 37151762288SStefan Hajnoczi /* query actual device if possible, otherwise just trust the hint */ 37251762288SStefan Hajnoczi if (drv->bdrv_getlength) { 37351762288SStefan Hajnoczi int64_t length = drv->bdrv_getlength(bs); 37451762288SStefan Hajnoczi if (length < 0) { 37551762288SStefan Hajnoczi return length; 37651762288SStefan Hajnoczi } 37751762288SStefan Hajnoczi hint = length >> BDRV_SECTOR_BITS; 37851762288SStefan Hajnoczi } 37951762288SStefan Hajnoczi 38051762288SStefan Hajnoczi bs->total_sectors = hint; 38151762288SStefan Hajnoczi return 0; 38251762288SStefan Hajnoczi } 38351762288SStefan Hajnoczi 384b6ce07aaSKevin Wolf /* 38557915332SKevin Wolf * Common part for opening disk images and files 38657915332SKevin Wolf */ 38757915332SKevin Wolf static int bdrv_open_common(BlockDriverState *bs, const char *filename, 38857915332SKevin Wolf int flags, BlockDriver *drv) 38957915332SKevin Wolf { 39057915332SKevin Wolf int ret, open_flags; 39157915332SKevin Wolf 39257915332SKevin Wolf assert(drv != NULL); 39357915332SKevin Wolf 39466f82ceeSKevin Wolf bs->file = NULL; 39551762288SStefan Hajnoczi bs->total_sectors = 0; 39657915332SKevin Wolf bs->is_temporary = 0; 39757915332SKevin Wolf bs->encrypted = 0; 39857915332SKevin Wolf bs->valid_key = 0; 39957915332SKevin Wolf bs->open_flags = flags; 40057915332SKevin Wolf /* buffer_alignment defaulted to 512, drivers can change this value */ 40157915332SKevin Wolf bs->buffer_alignment = 512; 40257915332SKevin Wolf 40357915332SKevin Wolf pstrcpy(bs->filename, sizeof(bs->filename), filename); 40457915332SKevin Wolf 40557915332SKevin Wolf if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 40657915332SKevin Wolf return -ENOTSUP; 40757915332SKevin Wolf } 40857915332SKevin Wolf 40957915332SKevin Wolf bs->drv = drv; 41057915332SKevin Wolf bs->opaque = qemu_mallocz(drv->instance_size); 41157915332SKevin Wolf 41257915332SKevin Wolf /* 41357915332SKevin Wolf * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a 41457915332SKevin Wolf * write cache to the guest. We do need the fdatasync to flush 41557915332SKevin Wolf * out transactions for block allocations, and we maybe have a 41657915332SKevin Wolf * volatile write cache in our backing device to deal with. 41757915332SKevin Wolf */ 41857915332SKevin Wolf if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) 41957915332SKevin Wolf bs->enable_write_cache = 1; 42057915332SKevin Wolf 42157915332SKevin Wolf /* 42257915332SKevin Wolf * Clear flags that are internal to the block layer before opening the 42357915332SKevin Wolf * image. 42457915332SKevin Wolf */ 42557915332SKevin Wolf open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 42657915332SKevin Wolf 42757915332SKevin Wolf /* 42857915332SKevin Wolf * Snapshots should be writeable. 42957915332SKevin Wolf */ 43057915332SKevin Wolf if (bs->is_temporary) { 43157915332SKevin Wolf open_flags |= BDRV_O_RDWR; 43257915332SKevin Wolf } 43357915332SKevin Wolf 43466f82ceeSKevin Wolf /* Open the image, either directly or using a protocol */ 43566f82ceeSKevin Wolf if (drv->bdrv_file_open) { 43666f82ceeSKevin Wolf ret = drv->bdrv_file_open(bs, filename, open_flags); 43766f82ceeSKevin Wolf } else { 43866f82ceeSKevin Wolf ret = bdrv_file_open(&bs->file, filename, open_flags); 43966f82ceeSKevin Wolf if (ret >= 0) { 44066f82ceeSKevin Wolf ret = drv->bdrv_open(bs, open_flags); 44166f82ceeSKevin Wolf } 44266f82ceeSKevin Wolf } 44366f82ceeSKevin Wolf 44457915332SKevin Wolf if (ret < 0) { 44557915332SKevin Wolf goto free_and_fail; 44657915332SKevin Wolf } 44757915332SKevin Wolf 44857915332SKevin Wolf bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 44951762288SStefan Hajnoczi 45051762288SStefan Hajnoczi ret = refresh_total_sectors(bs, bs->total_sectors); 45151762288SStefan Hajnoczi if (ret < 0) { 45251762288SStefan Hajnoczi goto free_and_fail; 45357915332SKevin Wolf } 45451762288SStefan Hajnoczi 45557915332SKevin Wolf #ifndef _WIN32 45657915332SKevin Wolf if (bs->is_temporary) { 45757915332SKevin Wolf unlink(filename); 45857915332SKevin Wolf } 45957915332SKevin Wolf #endif 46057915332SKevin Wolf return 0; 46157915332SKevin Wolf 46257915332SKevin Wolf free_and_fail: 46366f82ceeSKevin Wolf if (bs->file) { 46466f82ceeSKevin Wolf bdrv_delete(bs->file); 46566f82ceeSKevin Wolf bs->file = NULL; 46666f82ceeSKevin Wolf } 46757915332SKevin Wolf qemu_free(bs->opaque); 46857915332SKevin Wolf bs->opaque = NULL; 46957915332SKevin Wolf bs->drv = NULL; 47057915332SKevin Wolf return ret; 47157915332SKevin Wolf } 47257915332SKevin Wolf 47357915332SKevin Wolf /* 474b6ce07aaSKevin Wolf * Opens a file using a protocol (file, host_device, nbd, ...) 475b6ce07aaSKevin Wolf */ 47683f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 477b338082bSbellard { 47883f64091Sbellard BlockDriverState *bs; 4796db95603SChristoph Hellwig BlockDriver *drv; 48083f64091Sbellard int ret; 4813b0d4f61Sbellard 482b50cbabcSMORITA Kazutaka drv = bdrv_find_protocol(filename); 4836db95603SChristoph Hellwig if (!drv) { 4846db95603SChristoph Hellwig return -ENOENT; 4856db95603SChristoph Hellwig } 4866db95603SChristoph Hellwig 48783f64091Sbellard bs = bdrv_new(""); 488b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 48983f64091Sbellard if (ret < 0) { 49083f64091Sbellard bdrv_delete(bs); 49183f64091Sbellard return ret; 4923b0d4f61Sbellard } 49371d0770cSaliguori bs->growable = 1; 49483f64091Sbellard *pbs = bs; 49583f64091Sbellard return 0; 4963b0d4f61Sbellard } 4973b0d4f61Sbellard 498b6ce07aaSKevin Wolf /* 499b6ce07aaSKevin Wolf * Opens a disk image (raw, qcow2, vmdk, ...) 500b6ce07aaSKevin Wolf */ 501d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 502ea2384d3Sbellard BlockDriver *drv) 503ea2384d3Sbellard { 504b6ce07aaSKevin Wolf int ret; 50533e3963eSbellard 50683f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 507ea2384d3Sbellard BlockDriverState *bs1; 508ea2384d3Sbellard int64_t total_size; 5097c96d46eSaliguori int is_protocol = 0; 51091a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 51191a073a9SKevin Wolf QEMUOptionParameter *options; 512b6ce07aaSKevin Wolf char tmp_filename[PATH_MAX]; 513b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 51433e3963eSbellard 515ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 516ea2384d3Sbellard instead of opening 'filename' directly */ 517ea2384d3Sbellard 518ea2384d3Sbellard /* if there is a backing file, use it */ 519ea2384d3Sbellard bs1 = bdrv_new(""); 520d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 52151d7c00cSaliguori if (ret < 0) { 522ea2384d3Sbellard bdrv_delete(bs1); 52351d7c00cSaliguori return ret; 524ea2384d3Sbellard } 5253e82990bSJes Sorensen total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK; 5267c96d46eSaliguori 5277c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 5287c96d46eSaliguori is_protocol = 1; 5297c96d46eSaliguori 530ea2384d3Sbellard bdrv_delete(bs1); 531ea2384d3Sbellard 532ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 5337c96d46eSaliguori 5347c96d46eSaliguori /* Real path is meaningless for protocols */ 5357c96d46eSaliguori if (is_protocol) 5367c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 5377c96d46eSaliguori "%s", filename); 538114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 539114cdfa9SKirill A. Shutemov return -errno; 5407c96d46eSaliguori 54191a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 54291a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 54391a073a9SKevin Wolf 5443e82990bSJes Sorensen set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size); 54591a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 54691a073a9SKevin Wolf if (drv) { 54791a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 54891a073a9SKevin Wolf drv->format_name); 54991a073a9SKevin Wolf } 55091a073a9SKevin Wolf 55191a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 552d748768cSJan Kiszka free_option_parameters(options); 55351d7c00cSaliguori if (ret < 0) { 55451d7c00cSaliguori return ret; 555ea2384d3Sbellard } 55691a073a9SKevin Wolf 557ea2384d3Sbellard filename = tmp_filename; 55891a073a9SKevin Wolf drv = bdrv_qcow2; 559ea2384d3Sbellard bs->is_temporary = 1; 560ea2384d3Sbellard } 561ea2384d3Sbellard 562b6ce07aaSKevin Wolf /* Find the right image format driver */ 5636db95603SChristoph Hellwig if (!drv) { 564ea2384d3Sbellard drv = find_image_format(filename); 565ea2384d3Sbellard } 5666987307cSChristoph Hellwig 56751d7c00cSaliguori if (!drv) { 56851d7c00cSaliguori ret = -ENOENT; 56951d7c00cSaliguori goto unlink_and_fail; 57083f64091Sbellard } 571b6ce07aaSKevin Wolf 572b6ce07aaSKevin Wolf /* Open the image */ 573b6ce07aaSKevin Wolf ret = bdrv_open_common(bs, filename, flags, drv); 574b6ce07aaSKevin Wolf if (ret < 0) { 5756987307cSChristoph Hellwig goto unlink_and_fail; 5766987307cSChristoph Hellwig } 5776987307cSChristoph Hellwig 578b6ce07aaSKevin Wolf /* If there is a backing file, use it */ 579b6ce07aaSKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 580b6ce07aaSKevin Wolf char backing_filename[PATH_MAX]; 581b6ce07aaSKevin Wolf int back_flags; 582b6ce07aaSKevin Wolf BlockDriver *back_drv = NULL; 583b6ce07aaSKevin Wolf 584b6ce07aaSKevin Wolf bs->backing_hd = bdrv_new(""); 585b6ce07aaSKevin Wolf path_combine(backing_filename, sizeof(backing_filename), 586b6ce07aaSKevin Wolf filename, bs->backing_file); 587b6ce07aaSKevin Wolf if (bs->backing_format[0] != '\0') 588b6ce07aaSKevin Wolf back_drv = bdrv_find_format(bs->backing_format); 589b6ce07aaSKevin Wolf 590b6ce07aaSKevin Wolf /* backing files always opened read-only */ 591b6ce07aaSKevin Wolf back_flags = 592b6ce07aaSKevin Wolf flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 593b6ce07aaSKevin Wolf 594b6ce07aaSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv); 595b6ce07aaSKevin Wolf if (ret < 0) { 596b6ce07aaSKevin Wolf bdrv_close(bs); 597b6ce07aaSKevin Wolf return ret; 598b6ce07aaSKevin Wolf } 599b6ce07aaSKevin Wolf if (bs->is_temporary) { 600b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 601b6ce07aaSKevin Wolf } else { 602b6ce07aaSKevin Wolf /* base image inherits from "parent" */ 603b6ce07aaSKevin Wolf bs->backing_hd->keep_read_only = bs->keep_read_only; 604b6ce07aaSKevin Wolf } 605b6ce07aaSKevin Wolf } 606b6ce07aaSKevin Wolf 607b6ce07aaSKevin Wolf if (!bdrv_key_required(bs)) { 608b6ce07aaSKevin Wolf /* call the change callback */ 609b6ce07aaSKevin Wolf bs->media_changed = 1; 610b6ce07aaSKevin Wolf if (bs->change_cb) 611b6ce07aaSKevin Wolf bs->change_cb(bs->change_opaque); 612b6ce07aaSKevin Wolf } 613b6ce07aaSKevin Wolf 614b6ce07aaSKevin Wolf return 0; 615b6ce07aaSKevin Wolf 616b6ce07aaSKevin Wolf unlink_and_fail: 617b6ce07aaSKevin Wolf if (bs->is_temporary) { 618b6ce07aaSKevin Wolf unlink(filename); 619b6ce07aaSKevin Wolf } 620b6ce07aaSKevin Wolf return ret; 621b6ce07aaSKevin Wolf } 622b6ce07aaSKevin Wolf 623fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 624fc01f7e7Sbellard { 62519cb3738Sbellard if (bs->drv) { 626557df6acSStefan Hajnoczi if (bs->backing_hd) { 627ea2384d3Sbellard bdrv_delete(bs->backing_hd); 628557df6acSStefan Hajnoczi bs->backing_hd = NULL; 629557df6acSStefan Hajnoczi } 630ea2384d3Sbellard bs->drv->bdrv_close(bs); 631ea2384d3Sbellard qemu_free(bs->opaque); 632ea2384d3Sbellard #ifdef _WIN32 633ea2384d3Sbellard if (bs->is_temporary) { 634ea2384d3Sbellard unlink(bs->filename); 635ea2384d3Sbellard } 63667b915a5Sbellard #endif 637ea2384d3Sbellard bs->opaque = NULL; 638ea2384d3Sbellard bs->drv = NULL; 639b338082bSbellard 64066f82ceeSKevin Wolf if (bs->file != NULL) { 64166f82ceeSKevin Wolf bdrv_close(bs->file); 64266f82ceeSKevin Wolf } 64366f82ceeSKevin Wolf 644b338082bSbellard /* call the change callback */ 64519cb3738Sbellard bs->media_changed = 1; 646b338082bSbellard if (bs->change_cb) 647b338082bSbellard bs->change_cb(bs->change_opaque); 648b338082bSbellard } 649b338082bSbellard } 650b338082bSbellard 651*2bc93fedSMORITA Kazutaka void bdrv_close_all(void) 652*2bc93fedSMORITA Kazutaka { 653*2bc93fedSMORITA Kazutaka BlockDriverState *bs; 654*2bc93fedSMORITA Kazutaka 655*2bc93fedSMORITA Kazutaka QTAILQ_FOREACH(bs, &bdrv_states, list) { 656*2bc93fedSMORITA Kazutaka bdrv_close(bs); 657*2bc93fedSMORITA Kazutaka } 658*2bc93fedSMORITA Kazutaka } 659*2bc93fedSMORITA Kazutaka 660b338082bSbellard void bdrv_delete(BlockDriverState *bs) 661b338082bSbellard { 6621b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 6631b7bdbc1SStefan Hajnoczi if (bs->device_name[0] != '\0') { 6641b7bdbc1SStefan Hajnoczi QTAILQ_REMOVE(&bdrv_states, bs, list); 6651b7bdbc1SStefan Hajnoczi } 66634c6f050Saurel32 667b338082bSbellard bdrv_close(bs); 66866f82ceeSKevin Wolf if (bs->file != NULL) { 66966f82ceeSKevin Wolf bdrv_delete(bs->file); 67066f82ceeSKevin Wolf } 67166f82ceeSKevin Wolf 672b338082bSbellard qemu_free(bs); 673fc01f7e7Sbellard } 674fc01f7e7Sbellard 675e97fc193Saliguori /* 676e97fc193Saliguori * Run consistency checks on an image 677e97fc193Saliguori * 678e97fc193Saliguori * Returns the number of errors or -errno when an internal error occurs 679e97fc193Saliguori */ 680e97fc193Saliguori int bdrv_check(BlockDriverState *bs) 681e97fc193Saliguori { 682e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 683e97fc193Saliguori return -ENOTSUP; 684e97fc193Saliguori } 685e97fc193Saliguori 686e97fc193Saliguori return bs->drv->bdrv_check(bs); 687e97fc193Saliguori } 688e97fc193Saliguori 68933e3963eSbellard /* commit COW file into the raw image */ 69033e3963eSbellard int bdrv_commit(BlockDriverState *bs) 69133e3963eSbellard { 69219cb3738Sbellard BlockDriver *drv = bs->drv; 69383f64091Sbellard int64_t i, total_sectors; 6944dca4b63SNaphtali Sprei int n, j, ro, open_flags; 6954dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 696eb5a3165SJes Sorensen unsigned char sector[BDRV_SECTOR_SIZE]; 6974dca4b63SNaphtali Sprei char filename[1024]; 6984dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 69933e3963eSbellard 70019cb3738Sbellard if (!drv) 70119cb3738Sbellard return -ENOMEDIUM; 70233e3963eSbellard 7034dca4b63SNaphtali Sprei if (!bs->backing_hd) { 7044dca4b63SNaphtali Sprei return -ENOTSUP; 7054dca4b63SNaphtali Sprei } 7064dca4b63SNaphtali Sprei 7074dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 708ea2384d3Sbellard return -EACCES; 70933e3963eSbellard } 71033e3963eSbellard 7114dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 7124dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 7134dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 7144dca4b63SNaphtali Sprei 7154dca4b63SNaphtali Sprei if (ro) { 7164dca4b63SNaphtali Sprei /* re-open as RW */ 7174dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 7184dca4b63SNaphtali Sprei bs->backing_hd = NULL; 7194dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 720c3349197SKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv); 7214dca4b63SNaphtali Sprei if (rw_ret < 0) { 7224dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 7234dca4b63SNaphtali Sprei /* try to re-open read-only */ 7244dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 725c3349197SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); 7264dca4b63SNaphtali Sprei if (ret < 0) { 7274dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 7284dca4b63SNaphtali Sprei /* drive not functional anymore */ 7294dca4b63SNaphtali Sprei bs->drv = NULL; 7304dca4b63SNaphtali Sprei return ret; 7314dca4b63SNaphtali Sprei } 7324dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 7334dca4b63SNaphtali Sprei return rw_ret; 7344dca4b63SNaphtali Sprei } 7354dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 736ea2384d3Sbellard } 737ea2384d3Sbellard 7386ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 73983f64091Sbellard for (i = 0; i < total_sectors;) { 74019cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 741ea2384d3Sbellard for(j = 0; j < n; j++) { 74233e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 7434dca4b63SNaphtali Sprei ret = -EIO; 7444dca4b63SNaphtali Sprei goto ro_cleanup; 74533e3963eSbellard } 74633e3963eSbellard 747ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 7484dca4b63SNaphtali Sprei ret = -EIO; 7494dca4b63SNaphtali Sprei goto ro_cleanup; 75033e3963eSbellard } 751ea2384d3Sbellard i++; 752ea2384d3Sbellard } 753ea2384d3Sbellard } else { 754ea2384d3Sbellard i += n; 75533e3963eSbellard } 75633e3963eSbellard } 75795389c86Sbellard 7581d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 7591d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 7601d44952fSChristoph Hellwig bdrv_flush(bs); 7611d44952fSChristoph Hellwig } 76295389c86Sbellard 7633f5075aeSChristoph Hellwig /* 7643f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 7653f5075aeSChristoph Hellwig * stable on disk. 7663f5075aeSChristoph Hellwig */ 7673f5075aeSChristoph Hellwig if (bs->backing_hd) 7683f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 7694dca4b63SNaphtali Sprei 7704dca4b63SNaphtali Sprei ro_cleanup: 7714dca4b63SNaphtali Sprei 7724dca4b63SNaphtali Sprei if (ro) { 7734dca4b63SNaphtali Sprei /* re-open as RO */ 7744dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 7754dca4b63SNaphtali Sprei bs->backing_hd = NULL; 7764dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 777c3349197SKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv); 7784dca4b63SNaphtali Sprei if (ret < 0) { 7794dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 7804dca4b63SNaphtali Sprei /* drive not functional anymore */ 7814dca4b63SNaphtali Sprei bs->drv = NULL; 7824dca4b63SNaphtali Sprei return ret; 7834dca4b63SNaphtali Sprei } 7844dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 7854dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 7864dca4b63SNaphtali Sprei } 7874dca4b63SNaphtali Sprei 7881d44952fSChristoph Hellwig return ret; 78933e3963eSbellard } 79033e3963eSbellard 791756e6736SKevin Wolf /* 792756e6736SKevin Wolf * Return values: 793756e6736SKevin Wolf * 0 - success 794756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 795756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 796756e6736SKevin Wolf * image file header 797756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 798756e6736SKevin Wolf */ 799756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 800756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 801756e6736SKevin Wolf { 802756e6736SKevin Wolf BlockDriver *drv = bs->drv; 803756e6736SKevin Wolf 804756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 805756e6736SKevin Wolf return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 806756e6736SKevin Wolf } else { 807756e6736SKevin Wolf return -ENOTSUP; 808756e6736SKevin Wolf } 809756e6736SKevin Wolf } 810756e6736SKevin Wolf 81171d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 81271d0770cSaliguori size_t size) 81371d0770cSaliguori { 81471d0770cSaliguori int64_t len; 81571d0770cSaliguori 81671d0770cSaliguori if (!bdrv_is_inserted(bs)) 81771d0770cSaliguori return -ENOMEDIUM; 81871d0770cSaliguori 81971d0770cSaliguori if (bs->growable) 82071d0770cSaliguori return 0; 82171d0770cSaliguori 82271d0770cSaliguori len = bdrv_getlength(bs); 82371d0770cSaliguori 824fbb7b4e0SKevin Wolf if (offset < 0) 825fbb7b4e0SKevin Wolf return -EIO; 826fbb7b4e0SKevin Wolf 827fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 82871d0770cSaliguori return -EIO; 82971d0770cSaliguori 83071d0770cSaliguori return 0; 83171d0770cSaliguori } 83271d0770cSaliguori 83371d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 83471d0770cSaliguori int nb_sectors) 83571d0770cSaliguori { 836eb5a3165SJes Sorensen return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE, 837eb5a3165SJes Sorensen nb_sectors * BDRV_SECTOR_SIZE); 83871d0770cSaliguori } 83971d0770cSaliguori 84019cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 841fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 842fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 843fc01f7e7Sbellard { 844ea2384d3Sbellard BlockDriver *drv = bs->drv; 845fc01f7e7Sbellard 84619cb3738Sbellard if (!drv) 84719cb3738Sbellard return -ENOMEDIUM; 84871d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 84971d0770cSaliguori return -EIO; 850b338082bSbellard 85183f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 85283f64091Sbellard } 853fc01f7e7Sbellard 8547cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 8557cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 8567cd1e32aSlirans@il.ibm.com { 8577cd1e32aSlirans@il.ibm.com int64_t start, end; 858c6d22830SJan Kiszka unsigned long val, idx, bit; 859a55eb92cSJan Kiszka 8606ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 861c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 8627cd1e32aSlirans@il.ibm.com 8637cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 864c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 865c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 866c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 867c6d22830SJan Kiszka if (dirty) { 868aaa0eb75SLiran Schour if (!(val & (1 << bit))) { 869aaa0eb75SLiran Schour bs->dirty_count++; 870c6d22830SJan Kiszka val |= 1 << bit; 871aaa0eb75SLiran Schour } 872c6d22830SJan Kiszka } else { 873aaa0eb75SLiran Schour if (val & (1 << bit)) { 874aaa0eb75SLiran Schour bs->dirty_count--; 875c6d22830SJan Kiszka val &= ~(1 << bit); 876c6d22830SJan Kiszka } 877aaa0eb75SLiran Schour } 878c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 8797cd1e32aSlirans@il.ibm.com } 8807cd1e32aSlirans@il.ibm.com } 8817cd1e32aSlirans@il.ibm.com 88219cb3738Sbellard /* Return < 0 if error. Important errors are: 88319cb3738Sbellard -EIO generic I/O error (may happen for all errors) 88419cb3738Sbellard -ENOMEDIUM No media inserted. 88519cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 88619cb3738Sbellard -EACCES Trying to write a read-only device 88719cb3738Sbellard */ 888fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 889fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 890fc01f7e7Sbellard { 89183f64091Sbellard BlockDriver *drv = bs->drv; 89219cb3738Sbellard if (!bs->drv) 89319cb3738Sbellard return -ENOMEDIUM; 8940849bf08Sbellard if (bs->read_only) 89519cb3738Sbellard return -EACCES; 89671d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 89771d0770cSaliguori return -EIO; 89871d0770cSaliguori 899c6d22830SJan Kiszka if (bs->dirty_bitmap) { 9007cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 9017cd1e32aSlirans@il.ibm.com } 9027cd1e32aSlirans@il.ibm.com 903294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 904294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 905294cc35fSKevin Wolf } 906294cc35fSKevin Wolf 90783f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 90883f64091Sbellard } 90983f64091Sbellard 910eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 911eda578e5Saliguori void *buf, int count1) 91283f64091Sbellard { 9136ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 91483f64091Sbellard int len, nb_sectors, count; 91583f64091Sbellard int64_t sector_num; 9169a8c4cceSKevin Wolf int ret; 91783f64091Sbellard 91883f64091Sbellard count = count1; 91983f64091Sbellard /* first read to align to sector start */ 9206ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 92183f64091Sbellard if (len > count) 92283f64091Sbellard len = count; 9236ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 92483f64091Sbellard if (len > 0) { 9259a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9269a8c4cceSKevin Wolf return ret; 9276ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 92883f64091Sbellard count -= len; 92983f64091Sbellard if (count == 0) 93083f64091Sbellard return count1; 93183f64091Sbellard sector_num++; 93283f64091Sbellard buf += len; 93383f64091Sbellard } 93483f64091Sbellard 93583f64091Sbellard /* read the sectors "in place" */ 9366ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 93783f64091Sbellard if (nb_sectors > 0) { 9389a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 9399a8c4cceSKevin Wolf return ret; 94083f64091Sbellard sector_num += nb_sectors; 9416ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 94283f64091Sbellard buf += len; 94383f64091Sbellard count -= len; 94483f64091Sbellard } 94583f64091Sbellard 94683f64091Sbellard /* add data from the last sector */ 94783f64091Sbellard if (count > 0) { 9489a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9499a8c4cceSKevin Wolf return ret; 95083f64091Sbellard memcpy(buf, tmp_buf, count); 95183f64091Sbellard } 95283f64091Sbellard return count1; 95383f64091Sbellard } 95483f64091Sbellard 955eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 956eda578e5Saliguori const void *buf, int count1) 95783f64091Sbellard { 9586ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 95983f64091Sbellard int len, nb_sectors, count; 96083f64091Sbellard int64_t sector_num; 9619a8c4cceSKevin Wolf int ret; 96283f64091Sbellard 96383f64091Sbellard count = count1; 96483f64091Sbellard /* first write to align to sector start */ 9656ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 96683f64091Sbellard if (len > count) 96783f64091Sbellard len = count; 9686ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 96983f64091Sbellard if (len > 0) { 9709a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9719a8c4cceSKevin Wolf return ret; 9726ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 9739a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 9749a8c4cceSKevin Wolf return ret; 97583f64091Sbellard count -= len; 97683f64091Sbellard if (count == 0) 97783f64091Sbellard return count1; 97883f64091Sbellard sector_num++; 97983f64091Sbellard buf += len; 98083f64091Sbellard } 98183f64091Sbellard 98283f64091Sbellard /* write the sectors "in place" */ 9836ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 98483f64091Sbellard if (nb_sectors > 0) { 9859a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 9869a8c4cceSKevin Wolf return ret; 98783f64091Sbellard sector_num += nb_sectors; 9886ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 98983f64091Sbellard buf += len; 99083f64091Sbellard count -= len; 99183f64091Sbellard } 99283f64091Sbellard 99383f64091Sbellard /* add data from the last sector */ 99483f64091Sbellard if (count > 0) { 9959a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 9969a8c4cceSKevin Wolf return ret; 99783f64091Sbellard memcpy(tmp_buf, buf, count); 9989a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 9999a8c4cceSKevin Wolf return ret; 100083f64091Sbellard } 100183f64091Sbellard return count1; 100283f64091Sbellard } 100383f64091Sbellard 100483f64091Sbellard /** 100583f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 100683f64091Sbellard */ 100783f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 100883f64091Sbellard { 100983f64091Sbellard BlockDriver *drv = bs->drv; 101051762288SStefan Hajnoczi int ret; 101183f64091Sbellard if (!drv) 101219cb3738Sbellard return -ENOMEDIUM; 101383f64091Sbellard if (!drv->bdrv_truncate) 101483f64091Sbellard return -ENOTSUP; 101559f2689dSNaphtali Sprei if (bs->read_only) 101659f2689dSNaphtali Sprei return -EACCES; 101751762288SStefan Hajnoczi ret = drv->bdrv_truncate(bs, offset); 101851762288SStefan Hajnoczi if (ret == 0) { 101951762288SStefan Hajnoczi ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS); 102051762288SStefan Hajnoczi } 102151762288SStefan Hajnoczi return ret; 102283f64091Sbellard } 102383f64091Sbellard 102483f64091Sbellard /** 102583f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 102683f64091Sbellard */ 102783f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 102883f64091Sbellard { 102983f64091Sbellard BlockDriver *drv = bs->drv; 103083f64091Sbellard if (!drv) 103119cb3738Sbellard return -ENOMEDIUM; 103251762288SStefan Hajnoczi 103351762288SStefan Hajnoczi /* Fixed size devices use the total_sectors value for speed instead of 103451762288SStefan Hajnoczi issuing a length query (like lseek) on each call. Also, legacy block 103551762288SStefan Hajnoczi drivers don't provide a bdrv_getlength function and must use 103651762288SStefan Hajnoczi total_sectors. */ 103751762288SStefan Hajnoczi if (!bs->growable || !drv->bdrv_getlength) { 10386ea44308SJan Kiszka return bs->total_sectors * BDRV_SECTOR_SIZE; 103983f64091Sbellard } 104083f64091Sbellard return drv->bdrv_getlength(bs); 1041fc01f7e7Sbellard } 1042fc01f7e7Sbellard 104319cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 104496b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 1045fc01f7e7Sbellard { 104619cb3738Sbellard int64_t length; 104719cb3738Sbellard length = bdrv_getlength(bs); 104819cb3738Sbellard if (length < 0) 104919cb3738Sbellard length = 0; 105019cb3738Sbellard else 10516ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 105219cb3738Sbellard *nb_sectors_ptr = length; 1053fc01f7e7Sbellard } 1054cf98951bSbellard 1055f3d54fc4Saliguori struct partition { 1056f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 1057f3d54fc4Saliguori uint8_t head; /* starting head */ 1058f3d54fc4Saliguori uint8_t sector; /* starting sector */ 1059f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 1060f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 1061f3d54fc4Saliguori uint8_t end_head; /* end head */ 1062f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 1063f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 1064f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 1065f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 1066f3d54fc4Saliguori } __attribute__((packed)); 1067f3d54fc4Saliguori 1068f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 1069f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 1070f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 1071f3d54fc4Saliguori { 1072eb5a3165SJes Sorensen uint8_t buf[BDRV_SECTOR_SIZE]; 1073f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 1074f3d54fc4Saliguori struct partition *p; 1075f3d54fc4Saliguori uint32_t nr_sects; 1076a38131b6Sblueswir1 uint64_t nb_sectors; 1077f3d54fc4Saliguori 1078f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1079f3d54fc4Saliguori 1080f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 1081f3d54fc4Saliguori if (ret < 0) 1082f3d54fc4Saliguori return -1; 1083f3d54fc4Saliguori /* test msdos magic */ 1084f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 1085f3d54fc4Saliguori return -1; 1086f3d54fc4Saliguori for(i = 0; i < 4; i++) { 1087f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 1088f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 1089f3d54fc4Saliguori if (nr_sects && p->end_head) { 1090f3d54fc4Saliguori /* We make the assumption that the partition terminates on 1091f3d54fc4Saliguori a cylinder boundary */ 1092f3d54fc4Saliguori heads = p->end_head + 1; 1093f3d54fc4Saliguori sectors = p->end_sector & 63; 1094f3d54fc4Saliguori if (sectors == 0) 1095f3d54fc4Saliguori continue; 1096f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 1097f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 1098f3d54fc4Saliguori continue; 1099f3d54fc4Saliguori *pheads = heads; 1100f3d54fc4Saliguori *psectors = sectors; 1101f3d54fc4Saliguori *pcylinders = cylinders; 1102f3d54fc4Saliguori #if 0 1103f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 1104f3d54fc4Saliguori cylinders, heads, sectors); 1105f3d54fc4Saliguori #endif 1106f3d54fc4Saliguori return 0; 1107f3d54fc4Saliguori } 1108f3d54fc4Saliguori } 1109f3d54fc4Saliguori return -1; 1110f3d54fc4Saliguori } 1111f3d54fc4Saliguori 1112f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 1113f3d54fc4Saliguori { 1114f3d54fc4Saliguori int translation, lba_detected = 0; 1115f3d54fc4Saliguori int cylinders, heads, secs; 1116a38131b6Sblueswir1 uint64_t nb_sectors; 1117f3d54fc4Saliguori 1118f3d54fc4Saliguori /* if a geometry hint is available, use it */ 1119f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 1120f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 1121f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 1122f3d54fc4Saliguori if (cylinders != 0) { 1123f3d54fc4Saliguori *pcyls = cylinders; 1124f3d54fc4Saliguori *pheads = heads; 1125f3d54fc4Saliguori *psecs = secs; 1126f3d54fc4Saliguori } else { 1127f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 1128f3d54fc4Saliguori if (heads > 16) { 1129f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 1130f3d54fc4Saliguori translation was active, so the default 1131f3d54fc4Saliguori hardware geometry is OK */ 1132f3d54fc4Saliguori lba_detected = 1; 1133f3d54fc4Saliguori goto default_geometry; 1134f3d54fc4Saliguori } else { 1135f3d54fc4Saliguori *pcyls = cylinders; 1136f3d54fc4Saliguori *pheads = heads; 1137f3d54fc4Saliguori *psecs = secs; 1138f3d54fc4Saliguori /* disable any translation to be in sync with 1139f3d54fc4Saliguori the logical geometry */ 1140f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 1141f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1142f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 1143f3d54fc4Saliguori } 1144f3d54fc4Saliguori } 1145f3d54fc4Saliguori } else { 1146f3d54fc4Saliguori default_geometry: 1147f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 1148f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 1149f3d54fc4Saliguori 1150f3d54fc4Saliguori if (cylinders > 16383) 1151f3d54fc4Saliguori cylinders = 16383; 1152f3d54fc4Saliguori else if (cylinders < 2) 1153f3d54fc4Saliguori cylinders = 2; 1154f3d54fc4Saliguori *pcyls = cylinders; 1155f3d54fc4Saliguori *pheads = 16; 1156f3d54fc4Saliguori *psecs = 63; 1157f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 1158f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 1159f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1160f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 1161f3d54fc4Saliguori } else { 1162f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1163f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 1164f3d54fc4Saliguori } 1165f3d54fc4Saliguori } 1166f3d54fc4Saliguori } 1167f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 1168f3d54fc4Saliguori } 1169f3d54fc4Saliguori } 1170f3d54fc4Saliguori 1171b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 1172b338082bSbellard int cyls, int heads, int secs) 1173b338082bSbellard { 1174b338082bSbellard bs->cyls = cyls; 1175b338082bSbellard bs->heads = heads; 1176b338082bSbellard bs->secs = secs; 1177b338082bSbellard } 1178b338082bSbellard 1179b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 1180b338082bSbellard { 1181b338082bSbellard bs->type = type; 1182b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 1183b338082bSbellard type == BDRV_TYPE_FLOPPY)); 1184b338082bSbellard } 1185b338082bSbellard 118646d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 118746d4767dSbellard { 118846d4767dSbellard bs->translation = translation; 118946d4767dSbellard } 119046d4767dSbellard 1191b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 1192b338082bSbellard int *pcyls, int *pheads, int *psecs) 1193b338082bSbellard { 1194b338082bSbellard *pcyls = bs->cyls; 1195b338082bSbellard *pheads = bs->heads; 1196b338082bSbellard *psecs = bs->secs; 1197b338082bSbellard } 1198b338082bSbellard 1199b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 1200b338082bSbellard { 1201b338082bSbellard return bs->type; 1202b338082bSbellard } 1203b338082bSbellard 120446d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 120546d4767dSbellard { 120646d4767dSbellard return bs->translation; 120746d4767dSbellard } 120846d4767dSbellard 1209b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 1210b338082bSbellard { 1211b338082bSbellard return bs->removable; 1212b338082bSbellard } 1213b338082bSbellard 1214b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 1215b338082bSbellard { 1216b338082bSbellard return bs->read_only; 1217b338082bSbellard } 1218b338082bSbellard 1219985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 1220985a03b0Sths { 1221985a03b0Sths return bs->sg; 1222985a03b0Sths } 1223985a03b0Sths 1224e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 1225e900a7b7SChristoph Hellwig { 1226e900a7b7SChristoph Hellwig return bs->enable_write_cache; 1227e900a7b7SChristoph Hellwig } 1228e900a7b7SChristoph Hellwig 122919cb3738Sbellard /* XXX: no longer used */ 1230b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 1231b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 1232b338082bSbellard { 1233b338082bSbellard bs->change_cb = change_cb; 1234b338082bSbellard bs->change_opaque = opaque; 1235b338082bSbellard } 1236b338082bSbellard 1237ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 1238ea2384d3Sbellard { 1239ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 1240ea2384d3Sbellard return 1; 1241ea2384d3Sbellard return bs->encrypted; 1242ea2384d3Sbellard } 1243ea2384d3Sbellard 1244c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 1245c0f4ce77Saliguori { 1246c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 1247c0f4ce77Saliguori 1248c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 1249c0f4ce77Saliguori return 1; 1250c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 1251c0f4ce77Saliguori } 1252c0f4ce77Saliguori 1253ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 1254ea2384d3Sbellard { 1255ea2384d3Sbellard int ret; 1256ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 1257ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 1258ea2384d3Sbellard if (ret < 0) 1259ea2384d3Sbellard return ret; 1260ea2384d3Sbellard if (!bs->encrypted) 1261ea2384d3Sbellard return 0; 1262ea2384d3Sbellard } 1263fd04a2aeSShahar Havivi if (!bs->encrypted) { 1264fd04a2aeSShahar Havivi return -EINVAL; 1265fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 1266fd04a2aeSShahar Havivi return -ENOMEDIUM; 1267fd04a2aeSShahar Havivi } 1268c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 1269bb5fc20fSaliguori if (ret < 0) { 1270bb5fc20fSaliguori bs->valid_key = 0; 1271bb5fc20fSaliguori } else if (!bs->valid_key) { 1272bb5fc20fSaliguori bs->valid_key = 1; 1273bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 1274bb5fc20fSaliguori bs->media_changed = 1; 1275bb5fc20fSaliguori if (bs->change_cb) 1276bb5fc20fSaliguori bs->change_cb(bs->change_opaque); 1277bb5fc20fSaliguori } 1278c0f4ce77Saliguori return ret; 1279ea2384d3Sbellard } 1280ea2384d3Sbellard 1281ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 1282ea2384d3Sbellard { 128319cb3738Sbellard if (!bs->drv) { 1284ea2384d3Sbellard buf[0] = '\0'; 1285ea2384d3Sbellard } else { 1286ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 1287ea2384d3Sbellard } 1288ea2384d3Sbellard } 1289ea2384d3Sbellard 1290ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 1291ea2384d3Sbellard void *opaque) 1292ea2384d3Sbellard { 1293ea2384d3Sbellard BlockDriver *drv; 1294ea2384d3Sbellard 12958a22f02aSStefan Hajnoczi QLIST_FOREACH(drv, &bdrv_drivers, list) { 1296ea2384d3Sbellard it(opaque, drv->format_name); 1297ea2384d3Sbellard } 1298ea2384d3Sbellard } 1299ea2384d3Sbellard 1300b338082bSbellard BlockDriverState *bdrv_find(const char *name) 1301b338082bSbellard { 1302b338082bSbellard BlockDriverState *bs; 1303b338082bSbellard 13041b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 13051b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 1306b338082bSbellard return bs; 1307b338082bSbellard } 13081b7bdbc1SStefan Hajnoczi } 1309b338082bSbellard return NULL; 1310b338082bSbellard } 1311b338082bSbellard 131251de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 131381d0912dSbellard { 131481d0912dSbellard BlockDriverState *bs; 131581d0912dSbellard 13161b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 131751de9760Saliguori it(opaque, bs); 131881d0912dSbellard } 131981d0912dSbellard } 132081d0912dSbellard 1321ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1322ea2384d3Sbellard { 1323ea2384d3Sbellard return bs->device_name; 1324ea2384d3Sbellard } 1325ea2384d3Sbellard 13267a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 13277a6cba61Spbrook { 1328016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 1329016f5cf6SAlexander Graf return; 1330016f5cf6SAlexander Graf } 1331016f5cf6SAlexander Graf 13323f5075aeSChristoph Hellwig if (bs->drv && bs->drv->bdrv_flush) 13337a6cba61Spbrook bs->drv->bdrv_flush(bs); 13347a6cba61Spbrook } 13357a6cba61Spbrook 1336c6ca28d6Saliguori void bdrv_flush_all(void) 1337c6ca28d6Saliguori { 1338c6ca28d6Saliguori BlockDriverState *bs; 1339c6ca28d6Saliguori 13401b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1341c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 13421b7bdbc1SStefan Hajnoczi (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { 1343c6ca28d6Saliguori bdrv_flush(bs); 1344c6ca28d6Saliguori } 13451b7bdbc1SStefan Hajnoczi } 13461b7bdbc1SStefan Hajnoczi } 1347c6ca28d6Saliguori 1348f2feebbdSKevin Wolf int bdrv_has_zero_init(BlockDriverState *bs) 1349f2feebbdSKevin Wolf { 1350f2feebbdSKevin Wolf assert(bs->drv); 1351f2feebbdSKevin Wolf 1352f2feebbdSKevin Wolf if (bs->drv->no_zero_init) { 1353f2feebbdSKevin Wolf return 0; 1354f2feebbdSKevin Wolf } else if (bs->file) { 1355f2feebbdSKevin Wolf return bdrv_has_zero_init(bs->file); 1356f2feebbdSKevin Wolf } 1357f2feebbdSKevin Wolf 1358f2feebbdSKevin Wolf return 1; 1359f2feebbdSKevin Wolf } 1360f2feebbdSKevin Wolf 1361f58c7b35Sths /* 1362f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1363f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1364f58c7b35Sths * hence all their sectors are reported as allocated. 1365f58c7b35Sths * 1366f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1367f58c7b35Sths * the specified sector) that are known to be in the same 1368f58c7b35Sths * allocated/unallocated state. 1369f58c7b35Sths * 1370f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1371f58c7b35Sths */ 1372f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1373f58c7b35Sths int *pnum) 1374f58c7b35Sths { 1375f58c7b35Sths int64_t n; 1376f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1377f58c7b35Sths if (sector_num >= bs->total_sectors) { 1378f58c7b35Sths *pnum = 0; 1379f58c7b35Sths return 0; 1380f58c7b35Sths } 1381f58c7b35Sths n = bs->total_sectors - sector_num; 1382f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1383f58c7b35Sths return 1; 1384f58c7b35Sths } 1385f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1386f58c7b35Sths } 1387f58c7b35Sths 13882582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv, 13892582bfedSLuiz Capitulino BlockMonEventAction action, int is_read) 13902582bfedSLuiz Capitulino { 13912582bfedSLuiz Capitulino QObject *data; 13922582bfedSLuiz Capitulino const char *action_str; 13932582bfedSLuiz Capitulino 13942582bfedSLuiz Capitulino switch (action) { 13952582bfedSLuiz Capitulino case BDRV_ACTION_REPORT: 13962582bfedSLuiz Capitulino action_str = "report"; 13972582bfedSLuiz Capitulino break; 13982582bfedSLuiz Capitulino case BDRV_ACTION_IGNORE: 13992582bfedSLuiz Capitulino action_str = "ignore"; 14002582bfedSLuiz Capitulino break; 14012582bfedSLuiz Capitulino case BDRV_ACTION_STOP: 14022582bfedSLuiz Capitulino action_str = "stop"; 14032582bfedSLuiz Capitulino break; 14042582bfedSLuiz Capitulino default: 14052582bfedSLuiz Capitulino abort(); 14062582bfedSLuiz Capitulino } 14072582bfedSLuiz Capitulino 14082582bfedSLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 14092582bfedSLuiz Capitulino bdrv->device_name, 14102582bfedSLuiz Capitulino action_str, 14112582bfedSLuiz Capitulino is_read ? "read" : "write"); 14122582bfedSLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 14132582bfedSLuiz Capitulino 14142582bfedSLuiz Capitulino qobject_decref(data); 14152582bfedSLuiz Capitulino } 14162582bfedSLuiz Capitulino 1417d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque) 1418b338082bSbellard { 1419d15e5465SLuiz Capitulino QDict *bs_dict; 1420d15e5465SLuiz Capitulino Monitor *mon = opaque; 1421b338082bSbellard 1422d15e5465SLuiz Capitulino bs_dict = qobject_to_qdict(obj); 1423d15e5465SLuiz Capitulino 1424d15e5465SLuiz Capitulino monitor_printf(mon, "%s: type=%s removable=%d", 1425d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "device"), 1426d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "type"), 1427d15e5465SLuiz Capitulino qdict_get_bool(bs_dict, "removable")); 1428d15e5465SLuiz Capitulino 1429d15e5465SLuiz Capitulino if (qdict_get_bool(bs_dict, "removable")) { 1430d15e5465SLuiz Capitulino monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); 1431b338082bSbellard } 1432d15e5465SLuiz Capitulino 1433d15e5465SLuiz Capitulino if (qdict_haskey(bs_dict, "inserted")) { 1434d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); 1435d15e5465SLuiz Capitulino 1436376253ecSaliguori monitor_printf(mon, " file="); 1437d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "file")); 1438d15e5465SLuiz Capitulino if (qdict_haskey(qdict, "backing_file")) { 1439376253ecSaliguori monitor_printf(mon, " backing_file="); 1440d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); 1441fef30743Sths } 1442d15e5465SLuiz Capitulino monitor_printf(mon, " ro=%d drv=%s encrypted=%d", 1443d15e5465SLuiz Capitulino qdict_get_bool(qdict, "ro"), 1444d15e5465SLuiz Capitulino qdict_get_str(qdict, "drv"), 1445d15e5465SLuiz Capitulino qdict_get_bool(qdict, "encrypted")); 1446b338082bSbellard } else { 1447376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1448b338082bSbellard } 1449d15e5465SLuiz Capitulino 1450376253ecSaliguori monitor_printf(mon, "\n"); 1451b338082bSbellard } 1452d15e5465SLuiz Capitulino 1453d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data) 1454d15e5465SLuiz Capitulino { 1455d15e5465SLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); 1456d15e5465SLuiz Capitulino } 1457d15e5465SLuiz Capitulino 1458d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data) 1459d15e5465SLuiz Capitulino { 1460d15e5465SLuiz Capitulino QList *bs_list; 1461d15e5465SLuiz Capitulino BlockDriverState *bs; 1462d15e5465SLuiz Capitulino 1463d15e5465SLuiz Capitulino bs_list = qlist_new(); 1464d15e5465SLuiz Capitulino 14651b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1466d15e5465SLuiz Capitulino QObject *bs_obj; 1467d15e5465SLuiz Capitulino const char *type = "unknown"; 1468d15e5465SLuiz Capitulino 1469d15e5465SLuiz Capitulino switch(bs->type) { 1470d15e5465SLuiz Capitulino case BDRV_TYPE_HD: 1471d15e5465SLuiz Capitulino type = "hd"; 1472d15e5465SLuiz Capitulino break; 1473d15e5465SLuiz Capitulino case BDRV_TYPE_CDROM: 1474d15e5465SLuiz Capitulino type = "cdrom"; 1475d15e5465SLuiz Capitulino break; 1476d15e5465SLuiz Capitulino case BDRV_TYPE_FLOPPY: 1477d15e5465SLuiz Capitulino type = "floppy"; 1478d15e5465SLuiz Capitulino break; 1479d15e5465SLuiz Capitulino } 1480d15e5465SLuiz Capitulino 1481d15e5465SLuiz Capitulino bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, " 1482d15e5465SLuiz Capitulino "'removable': %i, 'locked': %i }", 1483d15e5465SLuiz Capitulino bs->device_name, type, bs->removable, 1484d15e5465SLuiz Capitulino bs->locked); 1485d15e5465SLuiz Capitulino 1486d15e5465SLuiz Capitulino if (bs->drv) { 1487d15e5465SLuiz Capitulino QObject *obj; 1488d15e5465SLuiz Capitulino QDict *bs_dict = qobject_to_qdict(bs_obj); 1489d15e5465SLuiz Capitulino 1490d15e5465SLuiz Capitulino obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " 1491d15e5465SLuiz Capitulino "'encrypted': %i }", 1492d15e5465SLuiz Capitulino bs->filename, bs->read_only, 1493d15e5465SLuiz Capitulino bs->drv->format_name, 1494d15e5465SLuiz Capitulino bdrv_is_encrypted(bs)); 1495d15e5465SLuiz Capitulino if (bs->backing_file[0] != '\0') { 1496d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(obj); 1497d15e5465SLuiz Capitulino qdict_put(qdict, "backing_file", 1498d15e5465SLuiz Capitulino qstring_from_str(bs->backing_file)); 1499d15e5465SLuiz Capitulino } 1500d15e5465SLuiz Capitulino 1501d15e5465SLuiz Capitulino qdict_put_obj(bs_dict, "inserted", obj); 1502d15e5465SLuiz Capitulino } 1503d15e5465SLuiz Capitulino qlist_append_obj(bs_list, bs_obj); 1504d15e5465SLuiz Capitulino } 1505d15e5465SLuiz Capitulino 1506d15e5465SLuiz Capitulino *ret_data = QOBJECT(bs_list); 1507b338082bSbellard } 1508a36e69ddSths 1509218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque) 1510a36e69ddSths { 1511218a536aSLuiz Capitulino QDict *qdict; 1512218a536aSLuiz Capitulino Monitor *mon = opaque; 1513218a536aSLuiz Capitulino 1514218a536aSLuiz Capitulino qdict = qobject_to_qdict(data); 1515218a536aSLuiz Capitulino monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); 1516218a536aSLuiz Capitulino 1517218a536aSLuiz Capitulino qdict = qobject_to_qdict(qdict_get(qdict, "stats")); 1518218a536aSLuiz Capitulino monitor_printf(mon, " rd_bytes=%" PRId64 1519218a536aSLuiz Capitulino " wr_bytes=%" PRId64 1520218a536aSLuiz Capitulino " rd_operations=%" PRId64 1521218a536aSLuiz Capitulino " wr_operations=%" PRId64 1522218a536aSLuiz Capitulino "\n", 1523218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_bytes"), 1524218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_bytes"), 1525218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_operations"), 1526218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_operations")); 1527218a536aSLuiz Capitulino } 1528218a536aSLuiz Capitulino 1529218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data) 1530218a536aSLuiz Capitulino { 1531218a536aSLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); 1532218a536aSLuiz Capitulino } 1533218a536aSLuiz Capitulino 1534294cc35fSKevin Wolf static QObject* bdrv_info_stats_bs(BlockDriverState *bs) 1535294cc35fSKevin Wolf { 1536294cc35fSKevin Wolf QObject *res; 1537294cc35fSKevin Wolf QDict *dict; 1538294cc35fSKevin Wolf 1539294cc35fSKevin Wolf res = qobject_from_jsonf("{ 'stats': {" 1540294cc35fSKevin Wolf "'rd_bytes': %" PRId64 "," 1541294cc35fSKevin Wolf "'wr_bytes': %" PRId64 "," 1542294cc35fSKevin Wolf "'rd_operations': %" PRId64 "," 1543294cc35fSKevin Wolf "'wr_operations': %" PRId64 "," 1544294cc35fSKevin Wolf "'wr_highest_offset': %" PRId64 1545294cc35fSKevin Wolf "} }", 1546294cc35fSKevin Wolf bs->rd_bytes, bs->wr_bytes, 1547294cc35fSKevin Wolf bs->rd_ops, bs->wr_ops, 1548eb5a3165SJes Sorensen bs->wr_highest_sector * (long)BDRV_SECTOR_SIZE); 1549294cc35fSKevin Wolf dict = qobject_to_qdict(res); 1550294cc35fSKevin Wolf 1551294cc35fSKevin Wolf if (*bs->device_name) { 1552294cc35fSKevin Wolf qdict_put(dict, "device", qstring_from_str(bs->device_name)); 1553294cc35fSKevin Wolf } 1554294cc35fSKevin Wolf 1555294cc35fSKevin Wolf if (bs->file) { 1556294cc35fSKevin Wolf QObject *parent = bdrv_info_stats_bs(bs->file); 1557294cc35fSKevin Wolf qdict_put_obj(dict, "parent", parent); 1558294cc35fSKevin Wolf } 1559294cc35fSKevin Wolf 1560294cc35fSKevin Wolf return res; 1561294cc35fSKevin Wolf } 1562294cc35fSKevin Wolf 1563218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data) 1564218a536aSLuiz Capitulino { 1565218a536aSLuiz Capitulino QObject *obj; 1566218a536aSLuiz Capitulino QList *devices; 1567a36e69ddSths BlockDriverState *bs; 1568a36e69ddSths 1569218a536aSLuiz Capitulino devices = qlist_new(); 1570218a536aSLuiz Capitulino 15711b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1572294cc35fSKevin Wolf obj = bdrv_info_stats_bs(bs); 1573218a536aSLuiz Capitulino qlist_append_obj(devices, obj); 1574a36e69ddSths } 1575218a536aSLuiz Capitulino 1576218a536aSLuiz Capitulino *ret_data = QOBJECT(devices); 1577a36e69ddSths } 1578ea2384d3Sbellard 1579045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1580045df330Saliguori { 1581045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1582045df330Saliguori return bs->backing_file; 1583045df330Saliguori else if (bs->encrypted) 1584045df330Saliguori return bs->filename; 1585045df330Saliguori else 1586045df330Saliguori return NULL; 1587045df330Saliguori } 1588045df330Saliguori 158983f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 159083f64091Sbellard char *filename, int filename_size) 159183f64091Sbellard { 1592b783e409SKevin Wolf if (!bs->backing_file) { 159383f64091Sbellard pstrcpy(filename, filename_size, ""); 159483f64091Sbellard } else { 159583f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 159683f64091Sbellard } 159783f64091Sbellard } 159883f64091Sbellard 1599faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1600faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1601faea38e7Sbellard { 1602faea38e7Sbellard BlockDriver *drv = bs->drv; 1603faea38e7Sbellard if (!drv) 160419cb3738Sbellard return -ENOMEDIUM; 1605faea38e7Sbellard if (!drv->bdrv_write_compressed) 1606faea38e7Sbellard return -ENOTSUP; 1607fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 1608fbb7b4e0SKevin Wolf return -EIO; 16097cd1e32aSlirans@il.ibm.com 1610c6d22830SJan Kiszka if (bs->dirty_bitmap) { 16117cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 16127cd1e32aSlirans@il.ibm.com } 16137cd1e32aSlirans@il.ibm.com 1614faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1615faea38e7Sbellard } 1616faea38e7Sbellard 1617faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1618faea38e7Sbellard { 1619faea38e7Sbellard BlockDriver *drv = bs->drv; 1620faea38e7Sbellard if (!drv) 162119cb3738Sbellard return -ENOMEDIUM; 1622faea38e7Sbellard if (!drv->bdrv_get_info) 1623faea38e7Sbellard return -ENOTSUP; 1624faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1625faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1626faea38e7Sbellard } 1627faea38e7Sbellard 162845566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 162945566e9cSChristoph Hellwig int64_t pos, int size) 1630178e08a5Saliguori { 1631178e08a5Saliguori BlockDriver *drv = bs->drv; 1632178e08a5Saliguori if (!drv) 1633178e08a5Saliguori return -ENOMEDIUM; 163445566e9cSChristoph Hellwig if (!drv->bdrv_save_vmstate) 1635178e08a5Saliguori return -ENOTSUP; 163645566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 1637178e08a5Saliguori } 1638178e08a5Saliguori 163945566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 164045566e9cSChristoph Hellwig int64_t pos, int size) 1641178e08a5Saliguori { 1642178e08a5Saliguori BlockDriver *drv = bs->drv; 1643178e08a5Saliguori if (!drv) 1644178e08a5Saliguori return -ENOMEDIUM; 164545566e9cSChristoph Hellwig if (!drv->bdrv_load_vmstate) 1646178e08a5Saliguori return -ENOTSUP; 164745566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 1648178e08a5Saliguori } 1649178e08a5Saliguori 16508b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 16518b9b0cc2SKevin Wolf { 16528b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 16538b9b0cc2SKevin Wolf 16548b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 16558b9b0cc2SKevin Wolf return; 16568b9b0cc2SKevin Wolf } 16578b9b0cc2SKevin Wolf 16588b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 16598b9b0cc2SKevin Wolf 16608b9b0cc2SKevin Wolf } 16618b9b0cc2SKevin Wolf 1662faea38e7Sbellard /**************************************************************/ 1663faea38e7Sbellard /* handling of snapshots */ 1664faea38e7Sbellard 1665faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1666faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1667faea38e7Sbellard { 1668faea38e7Sbellard BlockDriver *drv = bs->drv; 1669faea38e7Sbellard if (!drv) 167019cb3738Sbellard return -ENOMEDIUM; 1671faea38e7Sbellard if (!drv->bdrv_snapshot_create) 1672faea38e7Sbellard return -ENOTSUP; 1673faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 1674faea38e7Sbellard } 1675faea38e7Sbellard 1676faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1677faea38e7Sbellard const char *snapshot_id) 1678faea38e7Sbellard { 1679faea38e7Sbellard BlockDriver *drv = bs->drv; 1680faea38e7Sbellard if (!drv) 168119cb3738Sbellard return -ENOMEDIUM; 1682faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 1683faea38e7Sbellard return -ENOTSUP; 1684faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 1685faea38e7Sbellard } 1686faea38e7Sbellard 1687faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1688faea38e7Sbellard { 1689faea38e7Sbellard BlockDriver *drv = bs->drv; 1690faea38e7Sbellard if (!drv) 169119cb3738Sbellard return -ENOMEDIUM; 1692faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 1693faea38e7Sbellard return -ENOTSUP; 1694faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 1695faea38e7Sbellard } 1696faea38e7Sbellard 1697faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1698faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1699faea38e7Sbellard { 1700faea38e7Sbellard BlockDriver *drv = bs->drv; 1701faea38e7Sbellard if (!drv) 170219cb3738Sbellard return -ENOMEDIUM; 1703faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1704faea38e7Sbellard return -ENOTSUP; 1705faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1706faea38e7Sbellard } 1707faea38e7Sbellard 1708faea38e7Sbellard #define NB_SUFFIXES 4 1709faea38e7Sbellard 1710faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1711faea38e7Sbellard { 1712faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1713faea38e7Sbellard int64_t base; 1714faea38e7Sbellard int i; 1715faea38e7Sbellard 1716faea38e7Sbellard if (size <= 999) { 1717faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1718faea38e7Sbellard } else { 1719faea38e7Sbellard base = 1024; 1720faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1721faea38e7Sbellard if (size < (10 * base)) { 1722faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1723faea38e7Sbellard (double)size / base, 1724faea38e7Sbellard suffixes[i]); 1725faea38e7Sbellard break; 1726faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1727faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1728faea38e7Sbellard ((size + (base >> 1)) / base), 1729faea38e7Sbellard suffixes[i]); 1730faea38e7Sbellard break; 1731faea38e7Sbellard } 1732faea38e7Sbellard base = base * 1024; 1733faea38e7Sbellard } 1734faea38e7Sbellard } 1735faea38e7Sbellard return buf; 1736faea38e7Sbellard } 1737faea38e7Sbellard 1738faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1739faea38e7Sbellard { 1740faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 17413b9f94e1Sbellard #ifdef _WIN32 17423b9f94e1Sbellard struct tm *ptm; 17433b9f94e1Sbellard #else 1744faea38e7Sbellard struct tm tm; 17453b9f94e1Sbellard #endif 1746faea38e7Sbellard time_t ti; 1747faea38e7Sbellard int64_t secs; 1748faea38e7Sbellard 1749faea38e7Sbellard if (!sn) { 1750faea38e7Sbellard snprintf(buf, buf_size, 1751faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1752faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1753faea38e7Sbellard } else { 1754faea38e7Sbellard ti = sn->date_sec; 17553b9f94e1Sbellard #ifdef _WIN32 17563b9f94e1Sbellard ptm = localtime(&ti); 17573b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 17583b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 17593b9f94e1Sbellard #else 1760faea38e7Sbellard localtime_r(&ti, &tm); 1761faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1762faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 17633b9f94e1Sbellard #endif 1764faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1765faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1766faea38e7Sbellard "%02d:%02d:%02d.%03d", 1767faea38e7Sbellard (int)(secs / 3600), 1768faea38e7Sbellard (int)((secs / 60) % 60), 1769faea38e7Sbellard (int)(secs % 60), 1770faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1771faea38e7Sbellard snprintf(buf, buf_size, 1772faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1773faea38e7Sbellard sn->id_str, sn->name, 1774faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1775faea38e7Sbellard date_buf, 1776faea38e7Sbellard clock_buf); 1777faea38e7Sbellard } 1778faea38e7Sbellard return buf; 1779faea38e7Sbellard } 1780faea38e7Sbellard 178183f64091Sbellard 1782ea2384d3Sbellard /**************************************************************/ 178383f64091Sbellard /* async I/Os */ 1784ea2384d3Sbellard 17853b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 1786f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 178783f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1788ea2384d3Sbellard { 178983f64091Sbellard BlockDriver *drv = bs->drv; 1790a36e69ddSths BlockDriverAIOCB *ret; 1791ea2384d3Sbellard 179219cb3738Sbellard if (!drv) 1793ce1a14dcSpbrook return NULL; 179471d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 179571d0770cSaliguori return NULL; 179683f64091Sbellard 1797f141eafeSaliguori ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, 1798f141eafeSaliguori cb, opaque); 1799a36e69ddSths 1800a36e69ddSths if (ret) { 1801a36e69ddSths /* Update stats even though technically transfer has not happened. */ 18026ea44308SJan Kiszka bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 1803a36e69ddSths bs->rd_ops ++; 1804a36e69ddSths } 1805a36e69ddSths 1806a36e69ddSths return ret; 180783f64091Sbellard } 180883f64091Sbellard 1809f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 1810f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 181183f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 18127674e7bfSbellard { 181383f64091Sbellard BlockDriver *drv = bs->drv; 1814a36e69ddSths BlockDriverAIOCB *ret; 181583f64091Sbellard 181619cb3738Sbellard if (!drv) 1817ce1a14dcSpbrook return NULL; 181883f64091Sbellard if (bs->read_only) 1819ce1a14dcSpbrook return NULL; 182071d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 182171d0770cSaliguori return NULL; 182283f64091Sbellard 1823c6d22830SJan Kiszka if (bs->dirty_bitmap) { 18247cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 18257cd1e32aSlirans@il.ibm.com } 18267cd1e32aSlirans@il.ibm.com 1827f141eafeSaliguori ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, 1828f141eafeSaliguori cb, opaque); 1829a36e69ddSths 1830a36e69ddSths if (ret) { 1831a36e69ddSths /* Update stats even though technically transfer has not happened. */ 18326ea44308SJan Kiszka bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 1833a36e69ddSths bs->wr_ops ++; 1834294cc35fSKevin Wolf if (bs->wr_highest_sector < sector_num + nb_sectors - 1) { 1835294cc35fSKevin Wolf bs->wr_highest_sector = sector_num + nb_sectors - 1; 1836294cc35fSKevin Wolf } 1837a36e69ddSths } 1838a36e69ddSths 1839a36e69ddSths return ret; 184083f64091Sbellard } 184183f64091Sbellard 184240b4f539SKevin Wolf 184340b4f539SKevin Wolf typedef struct MultiwriteCB { 184440b4f539SKevin Wolf int error; 184540b4f539SKevin Wolf int num_requests; 184640b4f539SKevin Wolf int num_callbacks; 184740b4f539SKevin Wolf struct { 184840b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 184940b4f539SKevin Wolf void *opaque; 185040b4f539SKevin Wolf QEMUIOVector *free_qiov; 185140b4f539SKevin Wolf void *free_buf; 185240b4f539SKevin Wolf } callbacks[]; 185340b4f539SKevin Wolf } MultiwriteCB; 185440b4f539SKevin Wolf 185540b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 185640b4f539SKevin Wolf { 185740b4f539SKevin Wolf int i; 185840b4f539SKevin Wolf 185940b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 186040b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 18611e1ea48dSStefan Hajnoczi if (mcb->callbacks[i].free_qiov) { 18621e1ea48dSStefan Hajnoczi qemu_iovec_destroy(mcb->callbacks[i].free_qiov); 18631e1ea48dSStefan Hajnoczi } 186440b4f539SKevin Wolf qemu_free(mcb->callbacks[i].free_qiov); 1865f8a83245SHerve Poussineau qemu_vfree(mcb->callbacks[i].free_buf); 186640b4f539SKevin Wolf } 186740b4f539SKevin Wolf } 186840b4f539SKevin Wolf 186940b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 187040b4f539SKevin Wolf { 187140b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 187240b4f539SKevin Wolf 1873cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 187440b4f539SKevin Wolf mcb->error = ret; 187540b4f539SKevin Wolf multiwrite_user_cb(mcb); 187640b4f539SKevin Wolf } 187740b4f539SKevin Wolf 187840b4f539SKevin Wolf mcb->num_requests--; 187940b4f539SKevin Wolf if (mcb->num_requests == 0) { 188040b4f539SKevin Wolf if (mcb->error == 0) { 188140b4f539SKevin Wolf multiwrite_user_cb(mcb); 188240b4f539SKevin Wolf } 188340b4f539SKevin Wolf qemu_free(mcb); 188440b4f539SKevin Wolf } 188540b4f539SKevin Wolf } 188640b4f539SKevin Wolf 188740b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 188840b4f539SKevin Wolf { 188977be4366SChristoph Hellwig const BlockRequest *req1 = a, *req2 = b; 189077be4366SChristoph Hellwig 189177be4366SChristoph Hellwig /* 189277be4366SChristoph Hellwig * Note that we can't simply subtract req2->sector from req1->sector 189377be4366SChristoph Hellwig * here as that could overflow the return value. 189477be4366SChristoph Hellwig */ 189577be4366SChristoph Hellwig if (req1->sector > req2->sector) { 189677be4366SChristoph Hellwig return 1; 189777be4366SChristoph Hellwig } else if (req1->sector < req2->sector) { 189877be4366SChristoph Hellwig return -1; 189977be4366SChristoph Hellwig } else { 190077be4366SChristoph Hellwig return 0; 190177be4366SChristoph Hellwig } 190240b4f539SKevin Wolf } 190340b4f539SKevin Wolf 190440b4f539SKevin Wolf /* 190540b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 190640b4f539SKevin Wolf * requests that remain after merging. 190740b4f539SKevin Wolf */ 190840b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 190940b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 191040b4f539SKevin Wolf { 191140b4f539SKevin Wolf int i, outidx; 191240b4f539SKevin Wolf 191340b4f539SKevin Wolf // Sort requests by start sector 191440b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 191540b4f539SKevin Wolf 191640b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 191740b4f539SKevin Wolf // filling up gaps with zero sectors. 191840b4f539SKevin Wolf outidx = 0; 191940b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 192040b4f539SKevin Wolf int merge = 0; 192140b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 192240b4f539SKevin Wolf 192340b4f539SKevin Wolf // This handles the cases that are valid for all block drivers, namely 192440b4f539SKevin Wolf // exactly sequential writes and overlapping writes. 192540b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 192640b4f539SKevin Wolf merge = 1; 192740b4f539SKevin Wolf } 192840b4f539SKevin Wolf 192940b4f539SKevin Wolf // The block driver may decide that it makes sense to combine requests 193040b4f539SKevin Wolf // even if there is a gap of some sectors between them. In this case, 193140b4f539SKevin Wolf // the gap is filled with zeros (therefore only applicable for yet 193240b4f539SKevin Wolf // unused space in format like qcow2). 193340b4f539SKevin Wolf if (!merge && bs->drv->bdrv_merge_requests) { 193440b4f539SKevin Wolf merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); 193540b4f539SKevin Wolf } 193640b4f539SKevin Wolf 1937e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 1938e2a305fbSChristoph Hellwig merge = 0; 1939e2a305fbSChristoph Hellwig } 1940e2a305fbSChristoph Hellwig 194140b4f539SKevin Wolf if (merge) { 194240b4f539SKevin Wolf size_t size; 194340b4f539SKevin Wolf QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); 194440b4f539SKevin Wolf qemu_iovec_init(qiov, 194540b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 194640b4f539SKevin Wolf 194740b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 194840b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 194940b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 195040b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 195140b4f539SKevin Wolf 195240b4f539SKevin Wolf // We might need to add some zeros between the two requests 195340b4f539SKevin Wolf if (reqs[i].sector > oldreq_last) { 195440b4f539SKevin Wolf size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; 195540b4f539SKevin Wolf uint8_t *buf = qemu_blockalign(bs, zero_bytes); 195640b4f539SKevin Wolf memset(buf, 0, zero_bytes); 195740b4f539SKevin Wolf qemu_iovec_add(qiov, buf, zero_bytes); 195840b4f539SKevin Wolf mcb->callbacks[i].free_buf = buf; 195940b4f539SKevin Wolf } 196040b4f539SKevin Wolf 196140b4f539SKevin Wolf // Add the second request 196240b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 196340b4f539SKevin Wolf 1964cbf1dff2SKevin Wolf reqs[outidx].nb_sectors = qiov->size >> 9; 196540b4f539SKevin Wolf reqs[outidx].qiov = qiov; 196640b4f539SKevin Wolf 196740b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 196840b4f539SKevin Wolf } else { 196940b4f539SKevin Wolf outidx++; 197040b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 197140b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 197240b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 197340b4f539SKevin Wolf } 197440b4f539SKevin Wolf } 197540b4f539SKevin Wolf 197640b4f539SKevin Wolf return outidx + 1; 197740b4f539SKevin Wolf } 197840b4f539SKevin Wolf 197940b4f539SKevin Wolf /* 198040b4f539SKevin Wolf * Submit multiple AIO write requests at once. 198140b4f539SKevin Wolf * 198240b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 198340b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 198440b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 198540b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 198640b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 198740b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 198840b4f539SKevin Wolf * 198940b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 199040b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 199140b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 199240b4f539SKevin Wolf */ 199340b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 199440b4f539SKevin Wolf { 199540b4f539SKevin Wolf BlockDriverAIOCB *acb; 199640b4f539SKevin Wolf MultiwriteCB *mcb; 199740b4f539SKevin Wolf int i; 199840b4f539SKevin Wolf 199940b4f539SKevin Wolf if (num_reqs == 0) { 200040b4f539SKevin Wolf return 0; 200140b4f539SKevin Wolf } 200240b4f539SKevin Wolf 200340b4f539SKevin Wolf // Create MultiwriteCB structure 200440b4f539SKevin Wolf mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 200540b4f539SKevin Wolf mcb->num_requests = 0; 200640b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 200740b4f539SKevin Wolf 200840b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 200940b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 201040b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 201140b4f539SKevin Wolf } 201240b4f539SKevin Wolf 201340b4f539SKevin Wolf // Check for mergable requests 201440b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 201540b4f539SKevin Wolf 201640b4f539SKevin Wolf // Run the aio requests 201740b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 201840b4f539SKevin Wolf acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 201940b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 202040b4f539SKevin Wolf 202140b4f539SKevin Wolf if (acb == NULL) { 202240b4f539SKevin Wolf // We can only fail the whole thing if no request has been 202340b4f539SKevin Wolf // submitted yet. Otherwise we'll wait for the submitted AIOs to 202440b4f539SKevin Wolf // complete and report the error in the callback. 202540b4f539SKevin Wolf if (mcb->num_requests == 0) { 20260f0b604bSKevin Wolf reqs[i].error = -EIO; 202740b4f539SKevin Wolf goto fail; 202840b4f539SKevin Wolf } else { 20297eb58a6cSKevin Wolf mcb->num_requests++; 20307eb58a6cSKevin Wolf multiwrite_cb(mcb, -EIO); 203140b4f539SKevin Wolf break; 203240b4f539SKevin Wolf } 203340b4f539SKevin Wolf } else { 203440b4f539SKevin Wolf mcb->num_requests++; 203540b4f539SKevin Wolf } 203640b4f539SKevin Wolf } 203740b4f539SKevin Wolf 203840b4f539SKevin Wolf return 0; 203940b4f539SKevin Wolf 204040b4f539SKevin Wolf fail: 2041af474591SBruce Rogers qemu_free(mcb); 204240b4f539SKevin Wolf return -1; 204340b4f539SKevin Wolf } 204440b4f539SKevin Wolf 2045b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 2046b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2047b2e12bc6SChristoph Hellwig { 2048b2e12bc6SChristoph Hellwig BlockDriver *drv = bs->drv; 2049b2e12bc6SChristoph Hellwig 2050016f5cf6SAlexander Graf if (bs->open_flags & BDRV_O_NO_FLUSH) { 2051016f5cf6SAlexander Graf return bdrv_aio_noop_em(bs, cb, opaque); 2052016f5cf6SAlexander Graf } 2053016f5cf6SAlexander Graf 2054b2e12bc6SChristoph Hellwig if (!drv) 2055b2e12bc6SChristoph Hellwig return NULL; 2056b2e12bc6SChristoph Hellwig return drv->bdrv_aio_flush(bs, cb, opaque); 2057b2e12bc6SChristoph Hellwig } 2058b2e12bc6SChristoph Hellwig 205983f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 206083f64091Sbellard { 20616bbff9a0Saliguori acb->pool->cancel(acb); 206283f64091Sbellard } 206383f64091Sbellard 206483f64091Sbellard 206583f64091Sbellard /**************************************************************/ 206683f64091Sbellard /* async block device emulation */ 206783f64091Sbellard 2068c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 2069c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 2070c16b5a2cSChristoph Hellwig QEMUBH *bh; 2071c16b5a2cSChristoph Hellwig int ret; 2072c16b5a2cSChristoph Hellwig /* vector translation state */ 2073c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 2074c16b5a2cSChristoph Hellwig uint8_t *bounce; 2075c16b5a2cSChristoph Hellwig int is_write; 2076c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 2077c16b5a2cSChristoph Hellwig 2078c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 2079c16b5a2cSChristoph Hellwig { 2080b666d239SKevin Wolf BlockDriverAIOCBSync *acb = 2081b666d239SKevin Wolf container_of(blockacb, BlockDriverAIOCBSync, common); 20826a7ad299SDor Laor qemu_bh_delete(acb->bh); 208336afc451SAvi Kivity acb->bh = NULL; 2084c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 2085c16b5a2cSChristoph Hellwig } 2086c16b5a2cSChristoph Hellwig 2087c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 2088c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 2089c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 2090c16b5a2cSChristoph Hellwig }; 2091c16b5a2cSChristoph Hellwig 209283f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 2093beac80cdSbellard { 2094ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 2095f141eafeSaliguori 2096f141eafeSaliguori if (!acb->is_write) 2097f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 2098ceb42de8Saliguori qemu_vfree(acb->bounce); 2099ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 21006a7ad299SDor Laor qemu_bh_delete(acb->bh); 210136afc451SAvi Kivity acb->bh = NULL; 2102ce1a14dcSpbrook qemu_aio_release(acb); 2103beac80cdSbellard } 2104beac80cdSbellard 2105f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 2106f141eafeSaliguori int64_t sector_num, 2107f141eafeSaliguori QEMUIOVector *qiov, 2108f141eafeSaliguori int nb_sectors, 2109f141eafeSaliguori BlockDriverCompletionFunc *cb, 2110f141eafeSaliguori void *opaque, 2111f141eafeSaliguori int is_write) 2112f141eafeSaliguori 2113ea2384d3Sbellard { 2114ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 211583f64091Sbellard 2116c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2117f141eafeSaliguori acb->is_write = is_write; 2118f141eafeSaliguori acb->qiov = qiov; 2119e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 2120f141eafeSaliguori 2121ce1a14dcSpbrook if (!acb->bh) 2122ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2123f141eafeSaliguori 2124f141eafeSaliguori if (is_write) { 2125f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 2126f141eafeSaliguori acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 2127f141eafeSaliguori } else { 2128f141eafeSaliguori acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 2129f141eafeSaliguori } 2130f141eafeSaliguori 2131ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 2132f141eafeSaliguori 2133ce1a14dcSpbrook return &acb->common; 21347a6cba61Spbrook } 21357a6cba61Spbrook 2136f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 2137f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2138ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 213983f64091Sbellard { 2140f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 214183f64091Sbellard } 214283f64091Sbellard 2143f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 2144f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2145f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 2146f141eafeSaliguori { 2147f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 2148f141eafeSaliguori } 2149f141eafeSaliguori 2150b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 2151b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2152b2e12bc6SChristoph Hellwig { 2153b2e12bc6SChristoph Hellwig BlockDriverAIOCBSync *acb; 2154b2e12bc6SChristoph Hellwig 2155b2e12bc6SChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2156b2e12bc6SChristoph Hellwig acb->is_write = 1; /* don't bounce in the completion hadler */ 2157b2e12bc6SChristoph Hellwig acb->qiov = NULL; 2158b2e12bc6SChristoph Hellwig acb->bounce = NULL; 2159b2e12bc6SChristoph Hellwig acb->ret = 0; 2160b2e12bc6SChristoph Hellwig 2161b2e12bc6SChristoph Hellwig if (!acb->bh) 2162b2e12bc6SChristoph Hellwig acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2163b2e12bc6SChristoph Hellwig 2164b2e12bc6SChristoph Hellwig bdrv_flush(bs); 2165b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 2166b2e12bc6SChristoph Hellwig return &acb->common; 2167b2e12bc6SChristoph Hellwig } 2168b2e12bc6SChristoph Hellwig 2169016f5cf6SAlexander Graf static BlockDriverAIOCB *bdrv_aio_noop_em(BlockDriverState *bs, 2170016f5cf6SAlexander Graf BlockDriverCompletionFunc *cb, void *opaque) 2171016f5cf6SAlexander Graf { 2172016f5cf6SAlexander Graf BlockDriverAIOCBSync *acb; 2173016f5cf6SAlexander Graf 2174016f5cf6SAlexander Graf acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2175016f5cf6SAlexander Graf acb->is_write = 1; /* don't bounce in the completion handler */ 2176016f5cf6SAlexander Graf acb->qiov = NULL; 2177016f5cf6SAlexander Graf acb->bounce = NULL; 2178016f5cf6SAlexander Graf acb->ret = 0; 2179016f5cf6SAlexander Graf 2180016f5cf6SAlexander Graf if (!acb->bh) { 2181016f5cf6SAlexander Graf acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2182016f5cf6SAlexander Graf } 2183016f5cf6SAlexander Graf 2184016f5cf6SAlexander Graf qemu_bh_schedule(acb->bh); 2185016f5cf6SAlexander Graf return &acb->common; 2186016f5cf6SAlexander Graf } 2187016f5cf6SAlexander Graf 218883f64091Sbellard /**************************************************************/ 218983f64091Sbellard /* sync block device emulation */ 219083f64091Sbellard 219183f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 219283f64091Sbellard { 219383f64091Sbellard *(int *)opaque = ret; 219483f64091Sbellard } 219583f64091Sbellard 219683f64091Sbellard #define NOT_DONE 0x7fffffff 219783f64091Sbellard 219883f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 219983f64091Sbellard uint8_t *buf, int nb_sectors) 220083f64091Sbellard { 2201ce1a14dcSpbrook int async_ret; 2202ce1a14dcSpbrook BlockDriverAIOCB *acb; 2203f141eafeSaliguori struct iovec iov; 2204f141eafeSaliguori QEMUIOVector qiov; 220583f64091Sbellard 220665d6b3d8SKevin Wolf async_context_push(); 220765d6b3d8SKevin Wolf 220883f64091Sbellard async_ret = NOT_DONE; 22093f4cb3d3Sblueswir1 iov.iov_base = (void *)buf; 2210eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2211f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2212f141eafeSaliguori acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, 221383f64091Sbellard bdrv_rw_em_cb, &async_ret); 221465d6b3d8SKevin Wolf if (acb == NULL) { 221565d6b3d8SKevin Wolf async_ret = -1; 221665d6b3d8SKevin Wolf goto fail; 221765d6b3d8SKevin Wolf } 2218baf35cb9Saliguori 221983f64091Sbellard while (async_ret == NOT_DONE) { 222083f64091Sbellard qemu_aio_wait(); 222183f64091Sbellard } 2222baf35cb9Saliguori 222365d6b3d8SKevin Wolf 222465d6b3d8SKevin Wolf fail: 222565d6b3d8SKevin Wolf async_context_pop(); 222683f64091Sbellard return async_ret; 222783f64091Sbellard } 222883f64091Sbellard 222983f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 223083f64091Sbellard const uint8_t *buf, int nb_sectors) 223183f64091Sbellard { 2232ce1a14dcSpbrook int async_ret; 2233ce1a14dcSpbrook BlockDriverAIOCB *acb; 2234f141eafeSaliguori struct iovec iov; 2235f141eafeSaliguori QEMUIOVector qiov; 223683f64091Sbellard 223765d6b3d8SKevin Wolf async_context_push(); 223865d6b3d8SKevin Wolf 223983f64091Sbellard async_ret = NOT_DONE; 2240f141eafeSaliguori iov.iov_base = (void *)buf; 2241eb5a3165SJes Sorensen iov.iov_len = nb_sectors * BDRV_SECTOR_SIZE; 2242f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2243f141eafeSaliguori acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, 224483f64091Sbellard bdrv_rw_em_cb, &async_ret); 224565d6b3d8SKevin Wolf if (acb == NULL) { 224665d6b3d8SKevin Wolf async_ret = -1; 224765d6b3d8SKevin Wolf goto fail; 224865d6b3d8SKevin Wolf } 224983f64091Sbellard while (async_ret == NOT_DONE) { 225083f64091Sbellard qemu_aio_wait(); 225183f64091Sbellard } 225265d6b3d8SKevin Wolf 225365d6b3d8SKevin Wolf fail: 225465d6b3d8SKevin Wolf async_context_pop(); 225583f64091Sbellard return async_ret; 225683f64091Sbellard } 2257ea2384d3Sbellard 2258ea2384d3Sbellard void bdrv_init(void) 2259ea2384d3Sbellard { 22605efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 2261ea2384d3Sbellard } 2262ce1a14dcSpbrook 2263eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 2264eb852011SMarkus Armbruster { 2265eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 2266eb852011SMarkus Armbruster bdrv_init(); 2267eb852011SMarkus Armbruster } 2268eb852011SMarkus Armbruster 2269c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 22706bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 22716bbff9a0Saliguori { 2272ce1a14dcSpbrook BlockDriverAIOCB *acb; 2273ce1a14dcSpbrook 22746bbff9a0Saliguori if (pool->free_aiocb) { 22756bbff9a0Saliguori acb = pool->free_aiocb; 22766bbff9a0Saliguori pool->free_aiocb = acb->next; 2277ce1a14dcSpbrook } else { 22786bbff9a0Saliguori acb = qemu_mallocz(pool->aiocb_size); 22796bbff9a0Saliguori acb->pool = pool; 2280ce1a14dcSpbrook } 2281ce1a14dcSpbrook acb->bs = bs; 2282ce1a14dcSpbrook acb->cb = cb; 2283ce1a14dcSpbrook acb->opaque = opaque; 2284ce1a14dcSpbrook return acb; 2285ce1a14dcSpbrook } 2286ce1a14dcSpbrook 2287ce1a14dcSpbrook void qemu_aio_release(void *p) 2288ce1a14dcSpbrook { 22896bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 22906bbff9a0Saliguori AIOPool *pool = acb->pool; 22916bbff9a0Saliguori acb->next = pool->free_aiocb; 22926bbff9a0Saliguori pool->free_aiocb = acb; 2293ce1a14dcSpbrook } 229419cb3738Sbellard 229519cb3738Sbellard /**************************************************************/ 229619cb3738Sbellard /* removable device support */ 229719cb3738Sbellard 229819cb3738Sbellard /** 229919cb3738Sbellard * Return TRUE if the media is present 230019cb3738Sbellard */ 230119cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 230219cb3738Sbellard { 230319cb3738Sbellard BlockDriver *drv = bs->drv; 230419cb3738Sbellard int ret; 230519cb3738Sbellard if (!drv) 230619cb3738Sbellard return 0; 230719cb3738Sbellard if (!drv->bdrv_is_inserted) 230819cb3738Sbellard return 1; 230919cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 231019cb3738Sbellard return ret; 231119cb3738Sbellard } 231219cb3738Sbellard 231319cb3738Sbellard /** 231419cb3738Sbellard * Return TRUE if the media changed since the last call to this 231519cb3738Sbellard * function. It is currently only used for floppy disks 231619cb3738Sbellard */ 231719cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 231819cb3738Sbellard { 231919cb3738Sbellard BlockDriver *drv = bs->drv; 232019cb3738Sbellard int ret; 232119cb3738Sbellard 232219cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 232319cb3738Sbellard ret = -ENOTSUP; 232419cb3738Sbellard else 232519cb3738Sbellard ret = drv->bdrv_media_changed(bs); 232619cb3738Sbellard if (ret == -ENOTSUP) 232719cb3738Sbellard ret = bs->media_changed; 232819cb3738Sbellard bs->media_changed = 0; 232919cb3738Sbellard return ret; 233019cb3738Sbellard } 233119cb3738Sbellard 233219cb3738Sbellard /** 233319cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 233419cb3738Sbellard */ 2335aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag) 233619cb3738Sbellard { 233719cb3738Sbellard BlockDriver *drv = bs->drv; 233819cb3738Sbellard int ret; 233919cb3738Sbellard 2340aea2a33cSMark McLoughlin if (bs->locked) { 2341aea2a33cSMark McLoughlin return -EBUSY; 2342aea2a33cSMark McLoughlin } 2343aea2a33cSMark McLoughlin 234419cb3738Sbellard if (!drv || !drv->bdrv_eject) { 234519cb3738Sbellard ret = -ENOTSUP; 234619cb3738Sbellard } else { 234719cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 234819cb3738Sbellard } 234919cb3738Sbellard if (ret == -ENOTSUP) { 235019cb3738Sbellard if (eject_flag) 235119cb3738Sbellard bdrv_close(bs); 2352aea2a33cSMark McLoughlin ret = 0; 235319cb3738Sbellard } 2354aea2a33cSMark McLoughlin 2355aea2a33cSMark McLoughlin return ret; 235619cb3738Sbellard } 235719cb3738Sbellard 235819cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 235919cb3738Sbellard { 236019cb3738Sbellard return bs->locked; 236119cb3738Sbellard } 236219cb3738Sbellard 236319cb3738Sbellard /** 236419cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 236519cb3738Sbellard * to eject it manually). 236619cb3738Sbellard */ 236719cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 236819cb3738Sbellard { 236919cb3738Sbellard BlockDriver *drv = bs->drv; 237019cb3738Sbellard 237119cb3738Sbellard bs->locked = locked; 237219cb3738Sbellard if (drv && drv->bdrv_set_locked) { 237319cb3738Sbellard drv->bdrv_set_locked(bs, locked); 237419cb3738Sbellard } 237519cb3738Sbellard } 2376985a03b0Sths 2377985a03b0Sths /* needed for generic scsi interface */ 2378985a03b0Sths 2379985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 2380985a03b0Sths { 2381985a03b0Sths BlockDriver *drv = bs->drv; 2382985a03b0Sths 2383985a03b0Sths if (drv && drv->bdrv_ioctl) 2384985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 2385985a03b0Sths return -ENOTSUP; 2386985a03b0Sths } 23877d780669Saliguori 2388221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 2389221f715dSaliguori unsigned long int req, void *buf, 23907d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 23917d780669Saliguori { 2392221f715dSaliguori BlockDriver *drv = bs->drv; 23937d780669Saliguori 2394221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 2395221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 2396221f715dSaliguori return NULL; 23977d780669Saliguori } 2398e268ca52Saliguori 23997cd1e32aSlirans@il.ibm.com 24007cd1e32aSlirans@il.ibm.com 2401e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 2402e268ca52Saliguori { 2403e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 2404e268ca52Saliguori } 24057cd1e32aSlirans@il.ibm.com 24067cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 24077cd1e32aSlirans@il.ibm.com { 24087cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 2409a55eb92cSJan Kiszka 2410aaa0eb75SLiran Schour bs->dirty_count = 0; 24117cd1e32aSlirans@il.ibm.com if (enable) { 2412c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 2413c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 2414c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 2415c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 24167cd1e32aSlirans@il.ibm.com 24177cd1e32aSlirans@il.ibm.com bs->dirty_bitmap = qemu_mallocz(bitmap_size); 24187cd1e32aSlirans@il.ibm.com } 24197cd1e32aSlirans@il.ibm.com } else { 2420c6d22830SJan Kiszka if (bs->dirty_bitmap) { 24217cd1e32aSlirans@il.ibm.com qemu_free(bs->dirty_bitmap); 2422c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 24237cd1e32aSlirans@il.ibm.com } 24247cd1e32aSlirans@il.ibm.com } 24257cd1e32aSlirans@il.ibm.com } 24267cd1e32aSlirans@il.ibm.com 24277cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 24287cd1e32aSlirans@il.ibm.com { 24296ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 24307cd1e32aSlirans@il.ibm.com 2431c6d22830SJan Kiszka if (bs->dirty_bitmap && 2432c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 2433c6d22830SJan Kiszka return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 2434c6d22830SJan Kiszka (1 << (chunk % (sizeof(unsigned long) * 8))); 24357cd1e32aSlirans@il.ibm.com } else { 24367cd1e32aSlirans@il.ibm.com return 0; 24377cd1e32aSlirans@il.ibm.com } 24387cd1e32aSlirans@il.ibm.com } 24397cd1e32aSlirans@il.ibm.com 24407cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 24417cd1e32aSlirans@il.ibm.com int nr_sectors) 24427cd1e32aSlirans@il.ibm.com { 24437cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 24447cd1e32aSlirans@il.ibm.com } 2445aaa0eb75SLiran Schour 2446aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 2447aaa0eb75SLiran Schour { 2448aaa0eb75SLiran Schour return bs->dirty_count; 2449aaa0eb75SLiran Schour } 2450