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); 5383f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 5483f64091Sbellard uint8_t *buf, int nb_sectors); 5583f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 5683f64091Sbellard const uint8_t *buf, int nb_sectors); 57ec530c81Sbellard 58*1b7bdbc1SStefan Hajnoczi static QTAILQ_HEAD(, BlockDriverState) bdrv_states = 59*1b7bdbc1SStefan Hajnoczi QTAILQ_HEAD_INITIALIZER(bdrv_states); 607ee930d0Sblueswir1 61ea2384d3Sbellard static BlockDriver *first_drv; 62ea2384d3Sbellard 63eb852011SMarkus Armbruster /* If non-zero, use only whitelisted block drivers */ 64eb852011SMarkus Armbruster static int use_bdrv_whitelist; 65eb852011SMarkus Armbruster 6683f64091Sbellard int path_is_absolute(const char *path) 6783f64091Sbellard { 6883f64091Sbellard const char *p; 6921664424Sbellard #ifdef _WIN32 7021664424Sbellard /* specific case for names like: "\\.\d:" */ 7121664424Sbellard if (*path == '/' || *path == '\\') 7221664424Sbellard return 1; 7321664424Sbellard #endif 7483f64091Sbellard p = strchr(path, ':'); 7583f64091Sbellard if (p) 7683f64091Sbellard p++; 7783f64091Sbellard else 7883f64091Sbellard p = path; 793b9f94e1Sbellard #ifdef _WIN32 803b9f94e1Sbellard return (*p == '/' || *p == '\\'); 813b9f94e1Sbellard #else 823b9f94e1Sbellard return (*p == '/'); 833b9f94e1Sbellard #endif 8483f64091Sbellard } 8583f64091Sbellard 8683f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 8783f64091Sbellard path to it by considering it is relative to base_path. URL are 8883f64091Sbellard supported. */ 8983f64091Sbellard void path_combine(char *dest, int dest_size, 9083f64091Sbellard const char *base_path, 9183f64091Sbellard const char *filename) 9283f64091Sbellard { 9383f64091Sbellard const char *p, *p1; 9483f64091Sbellard int len; 9583f64091Sbellard 9683f64091Sbellard if (dest_size <= 0) 9783f64091Sbellard return; 9883f64091Sbellard if (path_is_absolute(filename)) { 9983f64091Sbellard pstrcpy(dest, dest_size, filename); 10083f64091Sbellard } else { 10183f64091Sbellard p = strchr(base_path, ':'); 10283f64091Sbellard if (p) 10383f64091Sbellard p++; 10483f64091Sbellard else 10583f64091Sbellard p = base_path; 1063b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1073b9f94e1Sbellard #ifdef _WIN32 1083b9f94e1Sbellard { 1093b9f94e1Sbellard const char *p2; 1103b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1113b9f94e1Sbellard if (!p1 || p2 > p1) 1123b9f94e1Sbellard p1 = p2; 1133b9f94e1Sbellard } 1143b9f94e1Sbellard #endif 11583f64091Sbellard if (p1) 11683f64091Sbellard p1++; 11783f64091Sbellard else 11883f64091Sbellard p1 = base_path; 11983f64091Sbellard if (p1 > p) 12083f64091Sbellard p = p1; 12183f64091Sbellard len = p - base_path; 12283f64091Sbellard if (len > dest_size - 1) 12383f64091Sbellard len = dest_size - 1; 12483f64091Sbellard memcpy(dest, base_path, len); 12583f64091Sbellard dest[len] = '\0'; 12683f64091Sbellard pstrcat(dest, dest_size, filename); 12783f64091Sbellard } 12883f64091Sbellard } 12983f64091Sbellard 1305efa9d5aSAnthony Liguori void bdrv_register(BlockDriver *bdrv) 131ea2384d3Sbellard { 132f141eafeSaliguori if (!bdrv->bdrv_aio_readv) { 13383f64091Sbellard /* add AIO emulation layer */ 134f141eafeSaliguori bdrv->bdrv_aio_readv = bdrv_aio_readv_em; 135f141eafeSaliguori bdrv->bdrv_aio_writev = bdrv_aio_writev_em; 136eda578e5Saliguori } else if (!bdrv->bdrv_read) { 13783f64091Sbellard /* add synchronous IO emulation layer */ 13883f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 13983f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 14083f64091Sbellard } 141b2e12bc6SChristoph Hellwig 142b2e12bc6SChristoph Hellwig if (!bdrv->bdrv_aio_flush) 143b2e12bc6SChristoph Hellwig bdrv->bdrv_aio_flush = bdrv_aio_flush_em; 144b2e12bc6SChristoph Hellwig 145ea2384d3Sbellard bdrv->next = first_drv; 146ea2384d3Sbellard first_drv = bdrv; 147ea2384d3Sbellard } 148b338082bSbellard 149b338082bSbellard /* create a new block device (by default it is empty) */ 150b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 151fc01f7e7Sbellard { 152*1b7bdbc1SStefan Hajnoczi BlockDriverState *bs; 153b338082bSbellard 154b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 155b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 156ea2384d3Sbellard if (device_name[0] != '\0') { 157*1b7bdbc1SStefan Hajnoczi QTAILQ_INSERT_TAIL(&bdrv_states, bs, list); 158ea2384d3Sbellard } 159b338082bSbellard return bs; 160b338082bSbellard } 161b338082bSbellard 162ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 163ea2384d3Sbellard { 164ea2384d3Sbellard BlockDriver *drv1; 165ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 166ea2384d3Sbellard if (!strcmp(drv1->format_name, format_name)) 167ea2384d3Sbellard return drv1; 168ea2384d3Sbellard } 169ea2384d3Sbellard return NULL; 170ea2384d3Sbellard } 171ea2384d3Sbellard 172eb852011SMarkus Armbruster static int bdrv_is_whitelisted(BlockDriver *drv) 173eb852011SMarkus Armbruster { 174eb852011SMarkus Armbruster static const char *whitelist[] = { 175eb852011SMarkus Armbruster CONFIG_BDRV_WHITELIST 176eb852011SMarkus Armbruster }; 177eb852011SMarkus Armbruster const char **p; 178eb852011SMarkus Armbruster 179eb852011SMarkus Armbruster if (!whitelist[0]) 180eb852011SMarkus Armbruster return 1; /* no whitelist, anything goes */ 181eb852011SMarkus Armbruster 182eb852011SMarkus Armbruster for (p = whitelist; *p; p++) { 183eb852011SMarkus Armbruster if (!strcmp(drv->format_name, *p)) { 184eb852011SMarkus Armbruster return 1; 185eb852011SMarkus Armbruster } 186eb852011SMarkus Armbruster } 187eb852011SMarkus Armbruster return 0; 188eb852011SMarkus Armbruster } 189eb852011SMarkus Armbruster 190eb852011SMarkus Armbruster BlockDriver *bdrv_find_whitelisted_format(const char *format_name) 191eb852011SMarkus Armbruster { 192eb852011SMarkus Armbruster BlockDriver *drv = bdrv_find_format(format_name); 193eb852011SMarkus Armbruster return drv && bdrv_is_whitelisted(drv) ? drv : NULL; 194eb852011SMarkus Armbruster } 195eb852011SMarkus Armbruster 1960e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 1970e7e1989SKevin Wolf QEMUOptionParameter *options) 198ea2384d3Sbellard { 199ea2384d3Sbellard if (!drv->bdrv_create) 200ea2384d3Sbellard return -ENOTSUP; 2010e7e1989SKevin Wolf 2020e7e1989SKevin Wolf return drv->bdrv_create(filename, options); 203ea2384d3Sbellard } 204ea2384d3Sbellard 205d5249393Sbellard #ifdef _WIN32 20695389c86Sbellard void get_tmp_filename(char *filename, int size) 207d5249393Sbellard { 2083b9f94e1Sbellard char temp_dir[MAX_PATH]; 2093b9f94e1Sbellard 2103b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 2113b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 212d5249393Sbellard } 213d5249393Sbellard #else 21495389c86Sbellard void get_tmp_filename(char *filename, int size) 215ea2384d3Sbellard { 216ea2384d3Sbellard int fd; 2177ccfb2ebSblueswir1 const char *tmpdir; 218d5249393Sbellard /* XXX: race condition possible */ 2190badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 2200badc1eeSaurel32 if (!tmpdir) 2210badc1eeSaurel32 tmpdir = "/tmp"; 2220badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 223ea2384d3Sbellard fd = mkstemp(filename); 224ea2384d3Sbellard close(fd); 225ea2384d3Sbellard } 226d5249393Sbellard #endif 227ea2384d3Sbellard 22819cb3738Sbellard #ifdef _WIN32 229f45512feSbellard static int is_windows_drive_prefix(const char *filename) 230f45512feSbellard { 231f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 232f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 233f45512feSbellard filename[1] == ':'); 234f45512feSbellard } 235f45512feSbellard 236508c7cb3SChristoph Hellwig int is_windows_drive(const char *filename) 23719cb3738Sbellard { 238f45512feSbellard if (is_windows_drive_prefix(filename) && 239f45512feSbellard filename[2] == '\0') 24019cb3738Sbellard return 1; 24119cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 24219cb3738Sbellard strstart(filename, "//./", NULL)) 24319cb3738Sbellard return 1; 24419cb3738Sbellard return 0; 24519cb3738Sbellard } 24619cb3738Sbellard #endif 24719cb3738Sbellard 24883f64091Sbellard static BlockDriver *find_protocol(const char *filename) 24983f64091Sbellard { 25083f64091Sbellard BlockDriver *drv1; 25183f64091Sbellard char protocol[128]; 2521cec71e3SAnthony Liguori int len; 25383f64091Sbellard const char *p; 25419cb3738Sbellard 25519cb3738Sbellard #ifdef _WIN32 256f45512feSbellard if (is_windows_drive(filename) || 257f45512feSbellard is_windows_drive_prefix(filename)) 2585efa9d5aSAnthony Liguori return bdrv_find_format("raw"); 25919cb3738Sbellard #endif 2601cec71e3SAnthony Liguori p = strchr(filename, ':'); 2611cec71e3SAnthony Liguori if (!p) 2625efa9d5aSAnthony Liguori return bdrv_find_format("raw"); 2631cec71e3SAnthony Liguori len = p - filename; 2641cec71e3SAnthony Liguori if (len > sizeof(protocol) - 1) 2651cec71e3SAnthony Liguori len = sizeof(protocol) - 1; 2661cec71e3SAnthony Liguori memcpy(protocol, filename, len); 2671cec71e3SAnthony Liguori protocol[len] = '\0'; 26883f64091Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 26983f64091Sbellard if (drv1->protocol_name && 27083f64091Sbellard !strcmp(drv1->protocol_name, protocol)) 27183f64091Sbellard return drv1; 27283f64091Sbellard } 27383f64091Sbellard return NULL; 27483f64091Sbellard } 27583f64091Sbellard 276f3a5d3f8SChristoph Hellwig /* 277f3a5d3f8SChristoph Hellwig * Detect host devices. By convention, /dev/cdrom[N] is always 278f3a5d3f8SChristoph Hellwig * recognized as a host CDROM. 279f3a5d3f8SChristoph Hellwig */ 280f3a5d3f8SChristoph Hellwig static BlockDriver *find_hdev_driver(const char *filename) 281f3a5d3f8SChristoph Hellwig { 282508c7cb3SChristoph Hellwig int score_max = 0, score; 283508c7cb3SChristoph Hellwig BlockDriver *drv = NULL, *d; 284f3a5d3f8SChristoph Hellwig 285508c7cb3SChristoph Hellwig for (d = first_drv; d; d = d->next) { 286508c7cb3SChristoph Hellwig if (d->bdrv_probe_device) { 287508c7cb3SChristoph Hellwig score = d->bdrv_probe_device(filename); 288508c7cb3SChristoph Hellwig if (score > score_max) { 289508c7cb3SChristoph Hellwig score_max = score; 290508c7cb3SChristoph Hellwig drv = d; 291f3a5d3f8SChristoph Hellwig } 292508c7cb3SChristoph Hellwig } 293f3a5d3f8SChristoph Hellwig } 294f3a5d3f8SChristoph Hellwig 295508c7cb3SChristoph Hellwig return drv; 296f3a5d3f8SChristoph Hellwig } 297f3a5d3f8SChristoph Hellwig 298ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 299ea2384d3Sbellard { 30083f64091Sbellard int ret, score, score_max; 301ea2384d3Sbellard BlockDriver *drv1, *drv; 30283f64091Sbellard uint8_t buf[2048]; 30383f64091Sbellard BlockDriverState *bs; 304ea2384d3Sbellard 30583f64091Sbellard drv = find_protocol(filename); 30619cb3738Sbellard /* no need to test disk image formats for vvfat */ 307c833ab73SAnthony Liguori if (drv && strcmp(drv->format_name, "vvfat") == 0) 30883f64091Sbellard return drv; 30983f64091Sbellard 310f5edb014SNaphtali Sprei ret = bdrv_file_open(&bs, filename, 0); 31183f64091Sbellard if (ret < 0) 3127674e7bfSbellard return NULL; 31383f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 31483f64091Sbellard bdrv_delete(bs); 315ea2384d3Sbellard if (ret < 0) { 316ea2384d3Sbellard return NULL; 317ea2384d3Sbellard } 318ea2384d3Sbellard 319ea2384d3Sbellard score_max = 0; 320ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 32183f64091Sbellard if (drv1->bdrv_probe) { 322ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 323ea2384d3Sbellard if (score > score_max) { 324ea2384d3Sbellard score_max = score; 325ea2384d3Sbellard drv = drv1; 326ea2384d3Sbellard } 327ea2384d3Sbellard } 32883f64091Sbellard } 329ea2384d3Sbellard return drv; 330ea2384d3Sbellard } 331ea2384d3Sbellard 33283f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 333b338082bSbellard { 33483f64091Sbellard BlockDriverState *bs; 3356db95603SChristoph Hellwig BlockDriver *drv; 33683f64091Sbellard int ret; 3373b0d4f61Sbellard 3386db95603SChristoph Hellwig drv = find_protocol(filename); 3396db95603SChristoph Hellwig if (!drv) { 3406db95603SChristoph Hellwig return -ENOENT; 3416db95603SChristoph Hellwig } 3426db95603SChristoph Hellwig 34383f64091Sbellard bs = bdrv_new(""); 3446db95603SChristoph Hellwig ret = bdrv_open(bs, filename, flags, drv); 34583f64091Sbellard if (ret < 0) { 34683f64091Sbellard bdrv_delete(bs); 34783f64091Sbellard return ret; 3483b0d4f61Sbellard } 34971d0770cSaliguori bs->growable = 1; 35083f64091Sbellard *pbs = bs; 35183f64091Sbellard return 0; 3523b0d4f61Sbellard } 3533b0d4f61Sbellard 354d6e9098eSKevin Wolf int bdrv_open(BlockDriverState *bs, const char *filename, int flags, 355ea2384d3Sbellard BlockDriver *drv) 356ea2384d3Sbellard { 357f5edb014SNaphtali Sprei int ret, open_flags; 358eb5c851fSths char tmp_filename[PATH_MAX]; 359eb5c851fSths char backing_filename[PATH_MAX]; 360fc01f7e7Sbellard 361ea2384d3Sbellard bs->is_temporary = 0; 362ea2384d3Sbellard bs->encrypted = 0; 363c0f4ce77Saliguori bs->valid_key = 0; 3644dca4b63SNaphtali Sprei bs->open_flags = flags; 365e268ca52Saliguori /* buffer_alignment defaulted to 512, drivers can change this value */ 366e268ca52Saliguori bs->buffer_alignment = 512; 36733e3963eSbellard 36883f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 369ea2384d3Sbellard BlockDriverState *bs1; 370ea2384d3Sbellard int64_t total_size; 3717c96d46eSaliguori int is_protocol = 0; 37291a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 37391a073a9SKevin Wolf QEMUOptionParameter *options; 37433e3963eSbellard 375ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 376ea2384d3Sbellard instead of opening 'filename' directly */ 377ea2384d3Sbellard 378ea2384d3Sbellard /* if there is a backing file, use it */ 379ea2384d3Sbellard bs1 = bdrv_new(""); 380d6e9098eSKevin Wolf ret = bdrv_open(bs1, filename, 0, drv); 38151d7c00cSaliguori if (ret < 0) { 382ea2384d3Sbellard bdrv_delete(bs1); 38351d7c00cSaliguori return ret; 384ea2384d3Sbellard } 3856ea44308SJan Kiszka total_size = bdrv_getlength(bs1) >> BDRV_SECTOR_BITS; 3867c96d46eSaliguori 3877c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 3887c96d46eSaliguori is_protocol = 1; 3897c96d46eSaliguori 390ea2384d3Sbellard bdrv_delete(bs1); 391ea2384d3Sbellard 392ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 3937c96d46eSaliguori 3947c96d46eSaliguori /* Real path is meaningless for protocols */ 3957c96d46eSaliguori if (is_protocol) 3967c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 3977c96d46eSaliguori "%s", filename); 398114cdfa9SKirill A. Shutemov else if (!realpath(filename, backing_filename)) 399114cdfa9SKirill A. Shutemov return -errno; 4007c96d46eSaliguori 40191a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 40291a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 40391a073a9SKevin Wolf 40491a073a9SKevin Wolf set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512); 40591a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 40691a073a9SKevin Wolf if (drv) { 40791a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 40891a073a9SKevin Wolf drv->format_name); 40991a073a9SKevin Wolf } 41091a073a9SKevin Wolf 41191a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 41251d7c00cSaliguori if (ret < 0) { 41351d7c00cSaliguori return ret; 414ea2384d3Sbellard } 41591a073a9SKevin Wolf 416ea2384d3Sbellard filename = tmp_filename; 41791a073a9SKevin Wolf drv = bdrv_qcow2; 418ea2384d3Sbellard bs->is_temporary = 1; 419ea2384d3Sbellard } 420ea2384d3Sbellard 421ea2384d3Sbellard pstrcpy(bs->filename, sizeof(bs->filename), filename); 4226db95603SChristoph Hellwig 4236db95603SChristoph Hellwig if (!drv) { 424f3a5d3f8SChristoph Hellwig drv = find_hdev_driver(filename); 425f3a5d3f8SChristoph Hellwig if (!drv) { 426ea2384d3Sbellard drv = find_image_format(filename); 427ea2384d3Sbellard } 428f3a5d3f8SChristoph Hellwig } 4296987307cSChristoph Hellwig 43051d7c00cSaliguori if (!drv) { 43151d7c00cSaliguori ret = -ENOENT; 43251d7c00cSaliguori goto unlink_and_fail; 43383f64091Sbellard } 4346987307cSChristoph Hellwig if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) { 4356987307cSChristoph Hellwig ret = -ENOTSUP; 4366987307cSChristoph Hellwig goto unlink_and_fail; 4376987307cSChristoph Hellwig } 4386987307cSChristoph Hellwig 439ea2384d3Sbellard bs->drv = drv; 440ea2384d3Sbellard bs->opaque = qemu_mallocz(drv->instance_size); 441e900a7b7SChristoph Hellwig 442e900a7b7SChristoph Hellwig /* 443e900a7b7SChristoph Hellwig * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a 444e900a7b7SChristoph Hellwig * write cache to the guest. We do need the fdatasync to flush 445e900a7b7SChristoph Hellwig * out transactions for block allocations, and we maybe have a 446e900a7b7SChristoph Hellwig * volatile write cache in our backing device to deal with. 447e900a7b7SChristoph Hellwig */ 448e900a7b7SChristoph Hellwig if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE)) 449e900a7b7SChristoph Hellwig bs->enable_write_cache = 1; 450e900a7b7SChristoph Hellwig 45115dc2697SChristoph Hellwig /* 45215dc2697SChristoph Hellwig * Clear flags that are internal to the block layer before opening the 45315dc2697SChristoph Hellwig * image. 45415dc2697SChristoph Hellwig */ 4556db95603SChristoph Hellwig open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING); 45615dc2697SChristoph Hellwig 45715dc2697SChristoph Hellwig /* 45815dc2697SChristoph Hellwig * Snapshots should be writeable. 45915dc2697SChristoph Hellwig */ 4606db95603SChristoph Hellwig if (bs->is_temporary) { 461f5edb014SNaphtali Sprei open_flags |= BDRV_O_RDWR; 462f5edb014SNaphtali Sprei } 4636987307cSChristoph Hellwig 4641cec71e3SAnthony Liguori ret = drv->bdrv_open(bs, filename, open_flags); 465ea2384d3Sbellard if (ret < 0) { 4666987307cSChristoph Hellwig goto free_and_fail; 467ea2384d3Sbellard } 4686987307cSChristoph Hellwig 4694dca4b63SNaphtali Sprei bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR); 470d15a771dSbellard if (drv->bdrv_getlength) { 4716ea44308SJan Kiszka bs->total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 472d15a771dSbellard } 473ea2384d3Sbellard #ifndef _WIN32 474ea2384d3Sbellard if (bs->is_temporary) { 475ea2384d3Sbellard unlink(filename); 47633e3963eSbellard } 47767b915a5Sbellard #endif 478b783e409SKevin Wolf if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') { 479ea2384d3Sbellard /* if there is a backing file, use it */ 4805eb45639Saliguori BlockDriver *back_drv = NULL; 481ea2384d3Sbellard bs->backing_hd = bdrv_new(""); 48283f64091Sbellard path_combine(backing_filename, sizeof(backing_filename), 48383f64091Sbellard filename, bs->backing_file); 4845eb45639Saliguori if (bs->backing_format[0] != '\0') 4855eb45639Saliguori back_drv = bdrv_find_format(bs->backing_format); 4864dca4b63SNaphtali Sprei 4874dca4b63SNaphtali Sprei /* backing files always opened read-only */ 4884dca4b63SNaphtali Sprei open_flags &= ~BDRV_O_RDWR; 4894dca4b63SNaphtali Sprei 490d6e9098eSKevin Wolf ret = bdrv_open(bs->backing_hd, backing_filename, open_flags, back_drv); 49151d7c00cSaliguori if (ret < 0) { 49251d7c00cSaliguori bdrv_close(bs); 49351d7c00cSaliguori return ret; 49451d7c00cSaliguori } 4954dca4b63SNaphtali Sprei if (bs->is_temporary) { 4964dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR); 4974dca4b63SNaphtali Sprei } else { 4984dca4b63SNaphtali Sprei /* base image inherits from "parent" */ 4994dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = bs->keep_read_only; 5004dca4b63SNaphtali Sprei } 501ea2384d3Sbellard } 50233e3963eSbellard 503bb5fc20fSaliguori if (!bdrv_key_required(bs)) { 504b338082bSbellard /* call the change callback */ 50519cb3738Sbellard bs->media_changed = 1; 506b338082bSbellard if (bs->change_cb) 507b338082bSbellard bs->change_cb(bs->change_opaque); 508bb5fc20fSaliguori } 509b338082bSbellard return 0; 5106987307cSChristoph Hellwig 5116987307cSChristoph Hellwig free_and_fail: 5126987307cSChristoph Hellwig qemu_free(bs->opaque); 5136987307cSChristoph Hellwig bs->opaque = NULL; 5146987307cSChristoph Hellwig bs->drv = NULL; 5156987307cSChristoph Hellwig unlink_and_fail: 5166987307cSChristoph Hellwig if (bs->is_temporary) 5176987307cSChristoph Hellwig unlink(filename); 5186987307cSChristoph Hellwig return ret; 519fc01f7e7Sbellard } 520fc01f7e7Sbellard 521fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 522fc01f7e7Sbellard { 52319cb3738Sbellard if (bs->drv) { 524ea2384d3Sbellard if (bs->backing_hd) 525ea2384d3Sbellard bdrv_delete(bs->backing_hd); 526ea2384d3Sbellard bs->drv->bdrv_close(bs); 527ea2384d3Sbellard qemu_free(bs->opaque); 528ea2384d3Sbellard #ifdef _WIN32 529ea2384d3Sbellard if (bs->is_temporary) { 530ea2384d3Sbellard unlink(bs->filename); 531ea2384d3Sbellard } 53267b915a5Sbellard #endif 533ea2384d3Sbellard bs->opaque = NULL; 534ea2384d3Sbellard bs->drv = NULL; 535b338082bSbellard 536b338082bSbellard /* call the change callback */ 53719cb3738Sbellard bs->media_changed = 1; 538b338082bSbellard if (bs->change_cb) 539b338082bSbellard bs->change_cb(bs->change_opaque); 540b338082bSbellard } 541b338082bSbellard } 542b338082bSbellard 543b338082bSbellard void bdrv_delete(BlockDriverState *bs) 544b338082bSbellard { 545*1b7bdbc1SStefan Hajnoczi /* remove from list, if necessary */ 546*1b7bdbc1SStefan Hajnoczi if (bs->device_name[0] != '\0') { 547*1b7bdbc1SStefan Hajnoczi QTAILQ_REMOVE(&bdrv_states, bs, list); 548*1b7bdbc1SStefan Hajnoczi } 54934c6f050Saurel32 550b338082bSbellard bdrv_close(bs); 551b338082bSbellard qemu_free(bs); 552fc01f7e7Sbellard } 553fc01f7e7Sbellard 554e97fc193Saliguori /* 555e97fc193Saliguori * Run consistency checks on an image 556e97fc193Saliguori * 557e97fc193Saliguori * Returns the number of errors or -errno when an internal error occurs 558e97fc193Saliguori */ 559e97fc193Saliguori int bdrv_check(BlockDriverState *bs) 560e97fc193Saliguori { 561e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 562e97fc193Saliguori return -ENOTSUP; 563e97fc193Saliguori } 564e97fc193Saliguori 565e97fc193Saliguori return bs->drv->bdrv_check(bs); 566e97fc193Saliguori } 567e97fc193Saliguori 56833e3963eSbellard /* commit COW file into the raw image */ 56933e3963eSbellard int bdrv_commit(BlockDriverState *bs) 57033e3963eSbellard { 57119cb3738Sbellard BlockDriver *drv = bs->drv; 57283f64091Sbellard int64_t i, total_sectors; 5734dca4b63SNaphtali Sprei int n, j, ro, open_flags; 5744dca4b63SNaphtali Sprei int ret = 0, rw_ret = 0; 575ea2384d3Sbellard unsigned char sector[512]; 5764dca4b63SNaphtali Sprei char filename[1024]; 5774dca4b63SNaphtali Sprei BlockDriverState *bs_rw, *bs_ro; 57833e3963eSbellard 57919cb3738Sbellard if (!drv) 58019cb3738Sbellard return -ENOMEDIUM; 58133e3963eSbellard 5824dca4b63SNaphtali Sprei if (!bs->backing_hd) { 5834dca4b63SNaphtali Sprei return -ENOTSUP; 5844dca4b63SNaphtali Sprei } 5854dca4b63SNaphtali Sprei 5864dca4b63SNaphtali Sprei if (bs->backing_hd->keep_read_only) { 587ea2384d3Sbellard return -EACCES; 58833e3963eSbellard } 58933e3963eSbellard 5904dca4b63SNaphtali Sprei ro = bs->backing_hd->read_only; 5914dca4b63SNaphtali Sprei strncpy(filename, bs->backing_hd->filename, sizeof(filename)); 5924dca4b63SNaphtali Sprei open_flags = bs->backing_hd->open_flags; 5934dca4b63SNaphtali Sprei 5944dca4b63SNaphtali Sprei if (ro) { 5954dca4b63SNaphtali Sprei /* re-open as RW */ 5964dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 5974dca4b63SNaphtali Sprei bs->backing_hd = NULL; 5984dca4b63SNaphtali Sprei bs_rw = bdrv_new(""); 599d6e9098eSKevin Wolf rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, NULL); 6004dca4b63SNaphtali Sprei if (rw_ret < 0) { 6014dca4b63SNaphtali Sprei bdrv_delete(bs_rw); 6024dca4b63SNaphtali Sprei /* try to re-open read-only */ 6034dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 604d6e9098eSKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL); 6054dca4b63SNaphtali Sprei if (ret < 0) { 6064dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 6074dca4b63SNaphtali Sprei /* drive not functional anymore */ 6084dca4b63SNaphtali Sprei bs->drv = NULL; 6094dca4b63SNaphtali Sprei return ret; 6104dca4b63SNaphtali Sprei } 6114dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 6124dca4b63SNaphtali Sprei return rw_ret; 6134dca4b63SNaphtali Sprei } 6144dca4b63SNaphtali Sprei bs->backing_hd = bs_rw; 615ea2384d3Sbellard } 616ea2384d3Sbellard 6176ea44308SJan Kiszka total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS; 61883f64091Sbellard for (i = 0; i < total_sectors;) { 61919cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 620ea2384d3Sbellard for(j = 0; j < n; j++) { 62133e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 6224dca4b63SNaphtali Sprei ret = -EIO; 6234dca4b63SNaphtali Sprei goto ro_cleanup; 62433e3963eSbellard } 62533e3963eSbellard 626ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 6274dca4b63SNaphtali Sprei ret = -EIO; 6284dca4b63SNaphtali Sprei goto ro_cleanup; 62933e3963eSbellard } 630ea2384d3Sbellard i++; 631ea2384d3Sbellard } 632ea2384d3Sbellard } else { 633ea2384d3Sbellard i += n; 63433e3963eSbellard } 63533e3963eSbellard } 63695389c86Sbellard 6371d44952fSChristoph Hellwig if (drv->bdrv_make_empty) { 6381d44952fSChristoph Hellwig ret = drv->bdrv_make_empty(bs); 6391d44952fSChristoph Hellwig bdrv_flush(bs); 6401d44952fSChristoph Hellwig } 64195389c86Sbellard 6423f5075aeSChristoph Hellwig /* 6433f5075aeSChristoph Hellwig * Make sure all data we wrote to the backing device is actually 6443f5075aeSChristoph Hellwig * stable on disk. 6453f5075aeSChristoph Hellwig */ 6463f5075aeSChristoph Hellwig if (bs->backing_hd) 6473f5075aeSChristoph Hellwig bdrv_flush(bs->backing_hd); 6484dca4b63SNaphtali Sprei 6494dca4b63SNaphtali Sprei ro_cleanup: 6504dca4b63SNaphtali Sprei 6514dca4b63SNaphtali Sprei if (ro) { 6524dca4b63SNaphtali Sprei /* re-open as RO */ 6534dca4b63SNaphtali Sprei bdrv_delete(bs->backing_hd); 6544dca4b63SNaphtali Sprei bs->backing_hd = NULL; 6554dca4b63SNaphtali Sprei bs_ro = bdrv_new(""); 656d6e9098eSKevin Wolf ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, NULL); 6574dca4b63SNaphtali Sprei if (ret < 0) { 6584dca4b63SNaphtali Sprei bdrv_delete(bs_ro); 6594dca4b63SNaphtali Sprei /* drive not functional anymore */ 6604dca4b63SNaphtali Sprei bs->drv = NULL; 6614dca4b63SNaphtali Sprei return ret; 6624dca4b63SNaphtali Sprei } 6634dca4b63SNaphtali Sprei bs->backing_hd = bs_ro; 6644dca4b63SNaphtali Sprei bs->backing_hd->keep_read_only = 0; 6654dca4b63SNaphtali Sprei } 6664dca4b63SNaphtali Sprei 6671d44952fSChristoph Hellwig return ret; 66833e3963eSbellard } 66933e3963eSbellard 670756e6736SKevin Wolf /* 671756e6736SKevin Wolf * Return values: 672756e6736SKevin Wolf * 0 - success 673756e6736SKevin Wolf * -EINVAL - backing format specified, but no file 674756e6736SKevin Wolf * -ENOSPC - can't update the backing file because no space is left in the 675756e6736SKevin Wolf * image file header 676756e6736SKevin Wolf * -ENOTSUP - format driver doesn't support changing the backing file 677756e6736SKevin Wolf */ 678756e6736SKevin Wolf int bdrv_change_backing_file(BlockDriverState *bs, 679756e6736SKevin Wolf const char *backing_file, const char *backing_fmt) 680756e6736SKevin Wolf { 681756e6736SKevin Wolf BlockDriver *drv = bs->drv; 682756e6736SKevin Wolf 683756e6736SKevin Wolf if (drv->bdrv_change_backing_file != NULL) { 684756e6736SKevin Wolf return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt); 685756e6736SKevin Wolf } else { 686756e6736SKevin Wolf return -ENOTSUP; 687756e6736SKevin Wolf } 688756e6736SKevin Wolf } 689756e6736SKevin Wolf 69071d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 69171d0770cSaliguori size_t size) 69271d0770cSaliguori { 69371d0770cSaliguori int64_t len; 69471d0770cSaliguori 69571d0770cSaliguori if (!bdrv_is_inserted(bs)) 69671d0770cSaliguori return -ENOMEDIUM; 69771d0770cSaliguori 69871d0770cSaliguori if (bs->growable) 69971d0770cSaliguori return 0; 70071d0770cSaliguori 70171d0770cSaliguori len = bdrv_getlength(bs); 70271d0770cSaliguori 703fbb7b4e0SKevin Wolf if (offset < 0) 704fbb7b4e0SKevin Wolf return -EIO; 705fbb7b4e0SKevin Wolf 706fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 70771d0770cSaliguori return -EIO; 70871d0770cSaliguori 70971d0770cSaliguori return 0; 71071d0770cSaliguori } 71171d0770cSaliguori 71271d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 71371d0770cSaliguori int nb_sectors) 71471d0770cSaliguori { 715999dec57Saliguori return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); 71671d0770cSaliguori } 71771d0770cSaliguori 71819cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 719fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 720fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 721fc01f7e7Sbellard { 722ea2384d3Sbellard BlockDriver *drv = bs->drv; 723fc01f7e7Sbellard 72419cb3738Sbellard if (!drv) 72519cb3738Sbellard return -ENOMEDIUM; 72671d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 72771d0770cSaliguori return -EIO; 728b338082bSbellard 72983f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 73083f64091Sbellard } 731fc01f7e7Sbellard 7327cd1e32aSlirans@il.ibm.com static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num, 7337cd1e32aSlirans@il.ibm.com int nb_sectors, int dirty) 7347cd1e32aSlirans@il.ibm.com { 7357cd1e32aSlirans@il.ibm.com int64_t start, end; 736c6d22830SJan Kiszka unsigned long val, idx, bit; 737a55eb92cSJan Kiszka 7386ea44308SJan Kiszka start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK; 739c6d22830SJan Kiszka end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK; 7407cd1e32aSlirans@il.ibm.com 7417cd1e32aSlirans@il.ibm.com for (; start <= end; start++) { 742c6d22830SJan Kiszka idx = start / (sizeof(unsigned long) * 8); 743c6d22830SJan Kiszka bit = start % (sizeof(unsigned long) * 8); 744c6d22830SJan Kiszka val = bs->dirty_bitmap[idx]; 745c6d22830SJan Kiszka if (dirty) { 746aaa0eb75SLiran Schour if (!(val & (1 << bit))) { 747aaa0eb75SLiran Schour bs->dirty_count++; 748c6d22830SJan Kiszka val |= 1 << bit; 749aaa0eb75SLiran Schour } 750c6d22830SJan Kiszka } else { 751aaa0eb75SLiran Schour if (val & (1 << bit)) { 752aaa0eb75SLiran Schour bs->dirty_count--; 753c6d22830SJan Kiszka val &= ~(1 << bit); 754c6d22830SJan Kiszka } 755aaa0eb75SLiran Schour } 756c6d22830SJan Kiszka bs->dirty_bitmap[idx] = val; 7577cd1e32aSlirans@il.ibm.com } 7587cd1e32aSlirans@il.ibm.com } 7597cd1e32aSlirans@il.ibm.com 76019cb3738Sbellard /* Return < 0 if error. Important errors are: 76119cb3738Sbellard -EIO generic I/O error (may happen for all errors) 76219cb3738Sbellard -ENOMEDIUM No media inserted. 76319cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 76419cb3738Sbellard -EACCES Trying to write a read-only device 76519cb3738Sbellard */ 766fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 767fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 768fc01f7e7Sbellard { 76983f64091Sbellard BlockDriver *drv = bs->drv; 77019cb3738Sbellard if (!bs->drv) 77119cb3738Sbellard return -ENOMEDIUM; 7720849bf08Sbellard if (bs->read_only) 77319cb3738Sbellard return -EACCES; 77471d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 77571d0770cSaliguori return -EIO; 77671d0770cSaliguori 777c6d22830SJan Kiszka if (bs->dirty_bitmap) { 7787cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 7797cd1e32aSlirans@il.ibm.com } 7807cd1e32aSlirans@il.ibm.com 78183f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 78283f64091Sbellard } 78383f64091Sbellard 784eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 785eda578e5Saliguori void *buf, int count1) 78683f64091Sbellard { 7876ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 78883f64091Sbellard int len, nb_sectors, count; 78983f64091Sbellard int64_t sector_num; 7909a8c4cceSKevin Wolf int ret; 79183f64091Sbellard 79283f64091Sbellard count = count1; 79383f64091Sbellard /* first read to align to sector start */ 7946ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 79583f64091Sbellard if (len > count) 79683f64091Sbellard len = count; 7976ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 79883f64091Sbellard if (len > 0) { 7999a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 8009a8c4cceSKevin Wolf return ret; 8016ea44308SJan Kiszka memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len); 80283f64091Sbellard count -= len; 80383f64091Sbellard if (count == 0) 80483f64091Sbellard return count1; 80583f64091Sbellard sector_num++; 80683f64091Sbellard buf += len; 80783f64091Sbellard } 80883f64091Sbellard 80983f64091Sbellard /* read the sectors "in place" */ 8106ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 81183f64091Sbellard if (nb_sectors > 0) { 8129a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0) 8139a8c4cceSKevin Wolf return ret; 81483f64091Sbellard sector_num += nb_sectors; 8156ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 81683f64091Sbellard buf += len; 81783f64091Sbellard count -= len; 81883f64091Sbellard } 81983f64091Sbellard 82083f64091Sbellard /* add data from the last sector */ 82183f64091Sbellard if (count > 0) { 8229a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 8239a8c4cceSKevin Wolf return ret; 82483f64091Sbellard memcpy(buf, tmp_buf, count); 82583f64091Sbellard } 82683f64091Sbellard return count1; 82783f64091Sbellard } 82883f64091Sbellard 829eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 830eda578e5Saliguori const void *buf, int count1) 83183f64091Sbellard { 8326ea44308SJan Kiszka uint8_t tmp_buf[BDRV_SECTOR_SIZE]; 83383f64091Sbellard int len, nb_sectors, count; 83483f64091Sbellard int64_t sector_num; 8359a8c4cceSKevin Wolf int ret; 83683f64091Sbellard 83783f64091Sbellard count = count1; 83883f64091Sbellard /* first write to align to sector start */ 8396ea44308SJan Kiszka len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1); 84083f64091Sbellard if (len > count) 84183f64091Sbellard len = count; 8426ea44308SJan Kiszka sector_num = offset >> BDRV_SECTOR_BITS; 84383f64091Sbellard if (len > 0) { 8449a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 8459a8c4cceSKevin Wolf return ret; 8466ea44308SJan Kiszka memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len); 8479a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 8489a8c4cceSKevin Wolf return ret; 84983f64091Sbellard count -= len; 85083f64091Sbellard if (count == 0) 85183f64091Sbellard return count1; 85283f64091Sbellard sector_num++; 85383f64091Sbellard buf += len; 85483f64091Sbellard } 85583f64091Sbellard 85683f64091Sbellard /* write the sectors "in place" */ 8576ea44308SJan Kiszka nb_sectors = count >> BDRV_SECTOR_BITS; 85883f64091Sbellard if (nb_sectors > 0) { 8599a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0) 8609a8c4cceSKevin Wolf return ret; 86183f64091Sbellard sector_num += nb_sectors; 8626ea44308SJan Kiszka len = nb_sectors << BDRV_SECTOR_BITS; 86383f64091Sbellard buf += len; 86483f64091Sbellard count -= len; 86583f64091Sbellard } 86683f64091Sbellard 86783f64091Sbellard /* add data from the last sector */ 86883f64091Sbellard if (count > 0) { 8699a8c4cceSKevin Wolf if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0) 8709a8c4cceSKevin Wolf return ret; 87183f64091Sbellard memcpy(tmp_buf, buf, count); 8729a8c4cceSKevin Wolf if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0) 8739a8c4cceSKevin Wolf return ret; 87483f64091Sbellard } 87583f64091Sbellard return count1; 87683f64091Sbellard } 87783f64091Sbellard 87883f64091Sbellard /** 87983f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 88083f64091Sbellard */ 88183f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 88283f64091Sbellard { 88383f64091Sbellard BlockDriver *drv = bs->drv; 88483f64091Sbellard if (!drv) 88519cb3738Sbellard return -ENOMEDIUM; 88683f64091Sbellard if (!drv->bdrv_truncate) 88783f64091Sbellard return -ENOTSUP; 88859f2689dSNaphtali Sprei if (bs->read_only) 88959f2689dSNaphtali Sprei return -EACCES; 89083f64091Sbellard return drv->bdrv_truncate(bs, offset); 89183f64091Sbellard } 89283f64091Sbellard 89383f64091Sbellard /** 89483f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 89583f64091Sbellard */ 89683f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 89783f64091Sbellard { 89883f64091Sbellard BlockDriver *drv = bs->drv; 89983f64091Sbellard if (!drv) 90019cb3738Sbellard return -ENOMEDIUM; 90183f64091Sbellard if (!drv->bdrv_getlength) { 90283f64091Sbellard /* legacy mode */ 9036ea44308SJan Kiszka return bs->total_sectors * BDRV_SECTOR_SIZE; 90483f64091Sbellard } 90583f64091Sbellard return drv->bdrv_getlength(bs); 906fc01f7e7Sbellard } 907fc01f7e7Sbellard 90819cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 90996b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 910fc01f7e7Sbellard { 91119cb3738Sbellard int64_t length; 91219cb3738Sbellard length = bdrv_getlength(bs); 91319cb3738Sbellard if (length < 0) 91419cb3738Sbellard length = 0; 91519cb3738Sbellard else 9166ea44308SJan Kiszka length = length >> BDRV_SECTOR_BITS; 91719cb3738Sbellard *nb_sectors_ptr = length; 918fc01f7e7Sbellard } 919cf98951bSbellard 920f3d54fc4Saliguori struct partition { 921f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 922f3d54fc4Saliguori uint8_t head; /* starting head */ 923f3d54fc4Saliguori uint8_t sector; /* starting sector */ 924f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 925f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 926f3d54fc4Saliguori uint8_t end_head; /* end head */ 927f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 928f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 929f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 930f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 931f3d54fc4Saliguori } __attribute__((packed)); 932f3d54fc4Saliguori 933f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 934f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 935f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 936f3d54fc4Saliguori { 937f3d54fc4Saliguori uint8_t buf[512]; 938f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 939f3d54fc4Saliguori struct partition *p; 940f3d54fc4Saliguori uint32_t nr_sects; 941a38131b6Sblueswir1 uint64_t nb_sectors; 942f3d54fc4Saliguori 943f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 944f3d54fc4Saliguori 945f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 946f3d54fc4Saliguori if (ret < 0) 947f3d54fc4Saliguori return -1; 948f3d54fc4Saliguori /* test msdos magic */ 949f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 950f3d54fc4Saliguori return -1; 951f3d54fc4Saliguori for(i = 0; i < 4; i++) { 952f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 953f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 954f3d54fc4Saliguori if (nr_sects && p->end_head) { 955f3d54fc4Saliguori /* We make the assumption that the partition terminates on 956f3d54fc4Saliguori a cylinder boundary */ 957f3d54fc4Saliguori heads = p->end_head + 1; 958f3d54fc4Saliguori sectors = p->end_sector & 63; 959f3d54fc4Saliguori if (sectors == 0) 960f3d54fc4Saliguori continue; 961f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 962f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 963f3d54fc4Saliguori continue; 964f3d54fc4Saliguori *pheads = heads; 965f3d54fc4Saliguori *psectors = sectors; 966f3d54fc4Saliguori *pcylinders = cylinders; 967f3d54fc4Saliguori #if 0 968f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 969f3d54fc4Saliguori cylinders, heads, sectors); 970f3d54fc4Saliguori #endif 971f3d54fc4Saliguori return 0; 972f3d54fc4Saliguori } 973f3d54fc4Saliguori } 974f3d54fc4Saliguori return -1; 975f3d54fc4Saliguori } 976f3d54fc4Saliguori 977f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 978f3d54fc4Saliguori { 979f3d54fc4Saliguori int translation, lba_detected = 0; 980f3d54fc4Saliguori int cylinders, heads, secs; 981a38131b6Sblueswir1 uint64_t nb_sectors; 982f3d54fc4Saliguori 983f3d54fc4Saliguori /* if a geometry hint is available, use it */ 984f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 985f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 986f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 987f3d54fc4Saliguori if (cylinders != 0) { 988f3d54fc4Saliguori *pcyls = cylinders; 989f3d54fc4Saliguori *pheads = heads; 990f3d54fc4Saliguori *psecs = secs; 991f3d54fc4Saliguori } else { 992f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 993f3d54fc4Saliguori if (heads > 16) { 994f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 995f3d54fc4Saliguori translation was active, so the default 996f3d54fc4Saliguori hardware geometry is OK */ 997f3d54fc4Saliguori lba_detected = 1; 998f3d54fc4Saliguori goto default_geometry; 999f3d54fc4Saliguori } else { 1000f3d54fc4Saliguori *pcyls = cylinders; 1001f3d54fc4Saliguori *pheads = heads; 1002f3d54fc4Saliguori *psecs = secs; 1003f3d54fc4Saliguori /* disable any translation to be in sync with 1004f3d54fc4Saliguori the logical geometry */ 1005f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 1006f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1007f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 1008f3d54fc4Saliguori } 1009f3d54fc4Saliguori } 1010f3d54fc4Saliguori } else { 1011f3d54fc4Saliguori default_geometry: 1012f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 1013f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 1014f3d54fc4Saliguori 1015f3d54fc4Saliguori if (cylinders > 16383) 1016f3d54fc4Saliguori cylinders = 16383; 1017f3d54fc4Saliguori else if (cylinders < 2) 1018f3d54fc4Saliguori cylinders = 2; 1019f3d54fc4Saliguori *pcyls = cylinders; 1020f3d54fc4Saliguori *pheads = 16; 1021f3d54fc4Saliguori *psecs = 63; 1022f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 1023f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 1024f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1025f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 1026f3d54fc4Saliguori } else { 1027f3d54fc4Saliguori bdrv_set_translation_hint(bs, 1028f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 1029f3d54fc4Saliguori } 1030f3d54fc4Saliguori } 1031f3d54fc4Saliguori } 1032f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 1033f3d54fc4Saliguori } 1034f3d54fc4Saliguori } 1035f3d54fc4Saliguori 1036b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 1037b338082bSbellard int cyls, int heads, int secs) 1038b338082bSbellard { 1039b338082bSbellard bs->cyls = cyls; 1040b338082bSbellard bs->heads = heads; 1041b338082bSbellard bs->secs = secs; 1042b338082bSbellard } 1043b338082bSbellard 1044b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 1045b338082bSbellard { 1046b338082bSbellard bs->type = type; 1047b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 1048b338082bSbellard type == BDRV_TYPE_FLOPPY)); 1049b338082bSbellard } 1050b338082bSbellard 105146d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 105246d4767dSbellard { 105346d4767dSbellard bs->translation = translation; 105446d4767dSbellard } 105546d4767dSbellard 1056b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 1057b338082bSbellard int *pcyls, int *pheads, int *psecs) 1058b338082bSbellard { 1059b338082bSbellard *pcyls = bs->cyls; 1060b338082bSbellard *pheads = bs->heads; 1061b338082bSbellard *psecs = bs->secs; 1062b338082bSbellard } 1063b338082bSbellard 1064b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 1065b338082bSbellard { 1066b338082bSbellard return bs->type; 1067b338082bSbellard } 1068b338082bSbellard 106946d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 107046d4767dSbellard { 107146d4767dSbellard return bs->translation; 107246d4767dSbellard } 107346d4767dSbellard 1074b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 1075b338082bSbellard { 1076b338082bSbellard return bs->removable; 1077b338082bSbellard } 1078b338082bSbellard 1079b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 1080b338082bSbellard { 1081b338082bSbellard return bs->read_only; 1082b338082bSbellard } 1083b338082bSbellard 1084985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 1085985a03b0Sths { 1086985a03b0Sths return bs->sg; 1087985a03b0Sths } 1088985a03b0Sths 1089e900a7b7SChristoph Hellwig int bdrv_enable_write_cache(BlockDriverState *bs) 1090e900a7b7SChristoph Hellwig { 1091e900a7b7SChristoph Hellwig return bs->enable_write_cache; 1092e900a7b7SChristoph Hellwig } 1093e900a7b7SChristoph Hellwig 109419cb3738Sbellard /* XXX: no longer used */ 1095b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 1096b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 1097b338082bSbellard { 1098b338082bSbellard bs->change_cb = change_cb; 1099b338082bSbellard bs->change_opaque = opaque; 1100b338082bSbellard } 1101b338082bSbellard 1102ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 1103ea2384d3Sbellard { 1104ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 1105ea2384d3Sbellard return 1; 1106ea2384d3Sbellard return bs->encrypted; 1107ea2384d3Sbellard } 1108ea2384d3Sbellard 1109c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 1110c0f4ce77Saliguori { 1111c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 1112c0f4ce77Saliguori 1113c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 1114c0f4ce77Saliguori return 1; 1115c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 1116c0f4ce77Saliguori } 1117c0f4ce77Saliguori 1118ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 1119ea2384d3Sbellard { 1120ea2384d3Sbellard int ret; 1121ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 1122ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 1123ea2384d3Sbellard if (ret < 0) 1124ea2384d3Sbellard return ret; 1125ea2384d3Sbellard if (!bs->encrypted) 1126ea2384d3Sbellard return 0; 1127ea2384d3Sbellard } 1128fd04a2aeSShahar Havivi if (!bs->encrypted) { 1129fd04a2aeSShahar Havivi return -EINVAL; 1130fd04a2aeSShahar Havivi } else if (!bs->drv || !bs->drv->bdrv_set_key) { 1131fd04a2aeSShahar Havivi return -ENOMEDIUM; 1132fd04a2aeSShahar Havivi } 1133c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 1134bb5fc20fSaliguori if (ret < 0) { 1135bb5fc20fSaliguori bs->valid_key = 0; 1136bb5fc20fSaliguori } else if (!bs->valid_key) { 1137bb5fc20fSaliguori bs->valid_key = 1; 1138bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 1139bb5fc20fSaliguori bs->media_changed = 1; 1140bb5fc20fSaliguori if (bs->change_cb) 1141bb5fc20fSaliguori bs->change_cb(bs->change_opaque); 1142bb5fc20fSaliguori } 1143c0f4ce77Saliguori return ret; 1144ea2384d3Sbellard } 1145ea2384d3Sbellard 1146ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 1147ea2384d3Sbellard { 114819cb3738Sbellard if (!bs->drv) { 1149ea2384d3Sbellard buf[0] = '\0'; 1150ea2384d3Sbellard } else { 1151ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 1152ea2384d3Sbellard } 1153ea2384d3Sbellard } 1154ea2384d3Sbellard 1155ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 1156ea2384d3Sbellard void *opaque) 1157ea2384d3Sbellard { 1158ea2384d3Sbellard BlockDriver *drv; 1159ea2384d3Sbellard 1160ea2384d3Sbellard for (drv = first_drv; drv != NULL; drv = drv->next) { 1161ea2384d3Sbellard it(opaque, drv->format_name); 1162ea2384d3Sbellard } 1163ea2384d3Sbellard } 1164ea2384d3Sbellard 1165b338082bSbellard BlockDriverState *bdrv_find(const char *name) 1166b338082bSbellard { 1167b338082bSbellard BlockDriverState *bs; 1168b338082bSbellard 1169*1b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1170*1b7bdbc1SStefan Hajnoczi if (!strcmp(name, bs->device_name)) { 1171b338082bSbellard return bs; 1172b338082bSbellard } 1173*1b7bdbc1SStefan Hajnoczi } 1174b338082bSbellard return NULL; 1175b338082bSbellard } 1176b338082bSbellard 117751de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 117881d0912dSbellard { 117981d0912dSbellard BlockDriverState *bs; 118081d0912dSbellard 1181*1b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 118251de9760Saliguori it(opaque, bs); 118381d0912dSbellard } 118481d0912dSbellard } 118581d0912dSbellard 1186ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1187ea2384d3Sbellard { 1188ea2384d3Sbellard return bs->device_name; 1189ea2384d3Sbellard } 1190ea2384d3Sbellard 11917a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 11927a6cba61Spbrook { 11933f5075aeSChristoph Hellwig if (bs->drv && bs->drv->bdrv_flush) 11947a6cba61Spbrook bs->drv->bdrv_flush(bs); 11957a6cba61Spbrook } 11967a6cba61Spbrook 1197c6ca28d6Saliguori void bdrv_flush_all(void) 1198c6ca28d6Saliguori { 1199c6ca28d6Saliguori BlockDriverState *bs; 1200c6ca28d6Saliguori 1201*1b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1202c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 1203*1b7bdbc1SStefan Hajnoczi (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) { 1204c6ca28d6Saliguori bdrv_flush(bs); 1205c6ca28d6Saliguori } 1206*1b7bdbc1SStefan Hajnoczi } 1207*1b7bdbc1SStefan Hajnoczi } 1208c6ca28d6Saliguori 1209f58c7b35Sths /* 1210f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1211f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1212f58c7b35Sths * hence all their sectors are reported as allocated. 1213f58c7b35Sths * 1214f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1215f58c7b35Sths * the specified sector) that are known to be in the same 1216f58c7b35Sths * allocated/unallocated state. 1217f58c7b35Sths * 1218f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1219f58c7b35Sths */ 1220f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1221f58c7b35Sths int *pnum) 1222f58c7b35Sths { 1223f58c7b35Sths int64_t n; 1224f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1225f58c7b35Sths if (sector_num >= bs->total_sectors) { 1226f58c7b35Sths *pnum = 0; 1227f58c7b35Sths return 0; 1228f58c7b35Sths } 1229f58c7b35Sths n = bs->total_sectors - sector_num; 1230f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1231f58c7b35Sths return 1; 1232f58c7b35Sths } 1233f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1234f58c7b35Sths } 1235f58c7b35Sths 12362582bfedSLuiz Capitulino void bdrv_mon_event(const BlockDriverState *bdrv, 12372582bfedSLuiz Capitulino BlockMonEventAction action, int is_read) 12382582bfedSLuiz Capitulino { 12392582bfedSLuiz Capitulino QObject *data; 12402582bfedSLuiz Capitulino const char *action_str; 12412582bfedSLuiz Capitulino 12422582bfedSLuiz Capitulino switch (action) { 12432582bfedSLuiz Capitulino case BDRV_ACTION_REPORT: 12442582bfedSLuiz Capitulino action_str = "report"; 12452582bfedSLuiz Capitulino break; 12462582bfedSLuiz Capitulino case BDRV_ACTION_IGNORE: 12472582bfedSLuiz Capitulino action_str = "ignore"; 12482582bfedSLuiz Capitulino break; 12492582bfedSLuiz Capitulino case BDRV_ACTION_STOP: 12502582bfedSLuiz Capitulino action_str = "stop"; 12512582bfedSLuiz Capitulino break; 12522582bfedSLuiz Capitulino default: 12532582bfedSLuiz Capitulino abort(); 12542582bfedSLuiz Capitulino } 12552582bfedSLuiz Capitulino 12562582bfedSLuiz Capitulino data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }", 12572582bfedSLuiz Capitulino bdrv->device_name, 12582582bfedSLuiz Capitulino action_str, 12592582bfedSLuiz Capitulino is_read ? "read" : "write"); 12602582bfedSLuiz Capitulino monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data); 12612582bfedSLuiz Capitulino 12622582bfedSLuiz Capitulino qobject_decref(data); 12632582bfedSLuiz Capitulino } 12642582bfedSLuiz Capitulino 1265d15e5465SLuiz Capitulino static void bdrv_print_dict(QObject *obj, void *opaque) 1266b338082bSbellard { 1267d15e5465SLuiz Capitulino QDict *bs_dict; 1268d15e5465SLuiz Capitulino Monitor *mon = opaque; 1269b338082bSbellard 1270d15e5465SLuiz Capitulino bs_dict = qobject_to_qdict(obj); 1271d15e5465SLuiz Capitulino 1272d15e5465SLuiz Capitulino monitor_printf(mon, "%s: type=%s removable=%d", 1273d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "device"), 1274d15e5465SLuiz Capitulino qdict_get_str(bs_dict, "type"), 1275d15e5465SLuiz Capitulino qdict_get_bool(bs_dict, "removable")); 1276d15e5465SLuiz Capitulino 1277d15e5465SLuiz Capitulino if (qdict_get_bool(bs_dict, "removable")) { 1278d15e5465SLuiz Capitulino monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked")); 1279b338082bSbellard } 1280d15e5465SLuiz Capitulino 1281d15e5465SLuiz Capitulino if (qdict_haskey(bs_dict, "inserted")) { 1282d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted")); 1283d15e5465SLuiz Capitulino 1284376253ecSaliguori monitor_printf(mon, " file="); 1285d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "file")); 1286d15e5465SLuiz Capitulino if (qdict_haskey(qdict, "backing_file")) { 1287376253ecSaliguori monitor_printf(mon, " backing_file="); 1288d15e5465SLuiz Capitulino monitor_print_filename(mon, qdict_get_str(qdict, "backing_file")); 1289fef30743Sths } 1290d15e5465SLuiz Capitulino monitor_printf(mon, " ro=%d drv=%s encrypted=%d", 1291d15e5465SLuiz Capitulino qdict_get_bool(qdict, "ro"), 1292d15e5465SLuiz Capitulino qdict_get_str(qdict, "drv"), 1293d15e5465SLuiz Capitulino qdict_get_bool(qdict, "encrypted")); 1294b338082bSbellard } else { 1295376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1296b338082bSbellard } 1297d15e5465SLuiz Capitulino 1298376253ecSaliguori monitor_printf(mon, "\n"); 1299b338082bSbellard } 1300d15e5465SLuiz Capitulino 1301d15e5465SLuiz Capitulino void bdrv_info_print(Monitor *mon, const QObject *data) 1302d15e5465SLuiz Capitulino { 1303d15e5465SLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon); 1304d15e5465SLuiz Capitulino } 1305d15e5465SLuiz Capitulino 1306d15e5465SLuiz Capitulino /** 1307d15e5465SLuiz Capitulino * bdrv_info(): Block devices information 1308d15e5465SLuiz Capitulino * 1309d15e5465SLuiz Capitulino * Each block device information is stored in a QDict and the 1310d15e5465SLuiz Capitulino * returned QObject is a QList of all devices. 1311d15e5465SLuiz Capitulino * 1312d15e5465SLuiz Capitulino * The QDict contains the following: 1313d15e5465SLuiz Capitulino * 1314d15e5465SLuiz Capitulino * - "device": device name 1315d15e5465SLuiz Capitulino * - "type": device type 1316d15e5465SLuiz Capitulino * - "removable": true if the device is removable, false otherwise 1317d15e5465SLuiz Capitulino * - "locked": true if the device is locked, false otherwise 1318d15e5465SLuiz Capitulino * - "inserted": only present if the device is inserted, it is a QDict 1319d15e5465SLuiz Capitulino * containing the following: 1320d15e5465SLuiz Capitulino * - "file": device file name 1321d15e5465SLuiz Capitulino * - "ro": true if read-only, false otherwise 1322d15e5465SLuiz Capitulino * - "drv": driver format name 1323d15e5465SLuiz Capitulino * - "backing_file": backing file name if one is used 1324d15e5465SLuiz Capitulino * - "encrypted": true if encrypted, false otherwise 1325d15e5465SLuiz Capitulino * 1326d15e5465SLuiz Capitulino * Example: 1327d15e5465SLuiz Capitulino * 1328d15e5465SLuiz Capitulino * [ { "device": "ide0-hd0", "type": "hd", "removable": false, "locked": false, 1329d15e5465SLuiz Capitulino * "inserted": { "file": "/tmp/foobar", "ro": false, "drv": "qcow2" } }, 1330d15e5465SLuiz Capitulino * { "device": "floppy0", "type": "floppy", "removable": true, 1331d15e5465SLuiz Capitulino * "locked": false } ] 1332d15e5465SLuiz Capitulino */ 1333d15e5465SLuiz Capitulino void bdrv_info(Monitor *mon, QObject **ret_data) 1334d15e5465SLuiz Capitulino { 1335d15e5465SLuiz Capitulino QList *bs_list; 1336d15e5465SLuiz Capitulino BlockDriverState *bs; 1337d15e5465SLuiz Capitulino 1338d15e5465SLuiz Capitulino bs_list = qlist_new(); 1339d15e5465SLuiz Capitulino 1340*1b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1341d15e5465SLuiz Capitulino QObject *bs_obj; 1342d15e5465SLuiz Capitulino const char *type = "unknown"; 1343d15e5465SLuiz Capitulino 1344d15e5465SLuiz Capitulino switch(bs->type) { 1345d15e5465SLuiz Capitulino case BDRV_TYPE_HD: 1346d15e5465SLuiz Capitulino type = "hd"; 1347d15e5465SLuiz Capitulino break; 1348d15e5465SLuiz Capitulino case BDRV_TYPE_CDROM: 1349d15e5465SLuiz Capitulino type = "cdrom"; 1350d15e5465SLuiz Capitulino break; 1351d15e5465SLuiz Capitulino case BDRV_TYPE_FLOPPY: 1352d15e5465SLuiz Capitulino type = "floppy"; 1353d15e5465SLuiz Capitulino break; 1354d15e5465SLuiz Capitulino } 1355d15e5465SLuiz Capitulino 1356d15e5465SLuiz Capitulino bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, " 1357d15e5465SLuiz Capitulino "'removable': %i, 'locked': %i }", 1358d15e5465SLuiz Capitulino bs->device_name, type, bs->removable, 1359d15e5465SLuiz Capitulino bs->locked); 1360d15e5465SLuiz Capitulino 1361d15e5465SLuiz Capitulino if (bs->drv) { 1362d15e5465SLuiz Capitulino QObject *obj; 1363d15e5465SLuiz Capitulino QDict *bs_dict = qobject_to_qdict(bs_obj); 1364d15e5465SLuiz Capitulino 1365d15e5465SLuiz Capitulino obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, " 1366d15e5465SLuiz Capitulino "'encrypted': %i }", 1367d15e5465SLuiz Capitulino bs->filename, bs->read_only, 1368d15e5465SLuiz Capitulino bs->drv->format_name, 1369d15e5465SLuiz Capitulino bdrv_is_encrypted(bs)); 1370d15e5465SLuiz Capitulino if (bs->backing_file[0] != '\0') { 1371d15e5465SLuiz Capitulino QDict *qdict = qobject_to_qdict(obj); 1372d15e5465SLuiz Capitulino qdict_put(qdict, "backing_file", 1373d15e5465SLuiz Capitulino qstring_from_str(bs->backing_file)); 1374d15e5465SLuiz Capitulino } 1375d15e5465SLuiz Capitulino 1376d15e5465SLuiz Capitulino qdict_put_obj(bs_dict, "inserted", obj); 1377d15e5465SLuiz Capitulino } 1378d15e5465SLuiz Capitulino qlist_append_obj(bs_list, bs_obj); 1379d15e5465SLuiz Capitulino } 1380d15e5465SLuiz Capitulino 1381d15e5465SLuiz Capitulino *ret_data = QOBJECT(bs_list); 1382b338082bSbellard } 1383a36e69ddSths 1384218a536aSLuiz Capitulino static void bdrv_stats_iter(QObject *data, void *opaque) 1385a36e69ddSths { 1386218a536aSLuiz Capitulino QDict *qdict; 1387218a536aSLuiz Capitulino Monitor *mon = opaque; 1388218a536aSLuiz Capitulino 1389218a536aSLuiz Capitulino qdict = qobject_to_qdict(data); 1390218a536aSLuiz Capitulino monitor_printf(mon, "%s:", qdict_get_str(qdict, "device")); 1391218a536aSLuiz Capitulino 1392218a536aSLuiz Capitulino qdict = qobject_to_qdict(qdict_get(qdict, "stats")); 1393218a536aSLuiz Capitulino monitor_printf(mon, " rd_bytes=%" PRId64 1394218a536aSLuiz Capitulino " wr_bytes=%" PRId64 1395218a536aSLuiz Capitulino " rd_operations=%" PRId64 1396218a536aSLuiz Capitulino " wr_operations=%" PRId64 1397218a536aSLuiz Capitulino "\n", 1398218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_bytes"), 1399218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_bytes"), 1400218a536aSLuiz Capitulino qdict_get_int(qdict, "rd_operations"), 1401218a536aSLuiz Capitulino qdict_get_int(qdict, "wr_operations")); 1402218a536aSLuiz Capitulino } 1403218a536aSLuiz Capitulino 1404218a536aSLuiz Capitulino void bdrv_stats_print(Monitor *mon, const QObject *data) 1405218a536aSLuiz Capitulino { 1406218a536aSLuiz Capitulino qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon); 1407218a536aSLuiz Capitulino } 1408218a536aSLuiz Capitulino 1409218a536aSLuiz Capitulino /** 1410218a536aSLuiz Capitulino * bdrv_info_stats(): show block device statistics 1411218a536aSLuiz Capitulino * 1412218a536aSLuiz Capitulino * Each device statistic information is stored in a QDict and 1413218a536aSLuiz Capitulino * the returned QObject is a QList of all devices. 1414218a536aSLuiz Capitulino * 1415218a536aSLuiz Capitulino * The QDict contains the following: 1416218a536aSLuiz Capitulino * 1417218a536aSLuiz Capitulino * - "device": device name 1418218a536aSLuiz Capitulino * - "stats": A QDict with the statistics information, it contains: 1419218a536aSLuiz Capitulino * - "rd_bytes": bytes read 1420218a536aSLuiz Capitulino * - "wr_bytes": bytes written 1421218a536aSLuiz Capitulino * - "rd_operations": read operations 1422218a536aSLuiz Capitulino * - "wr_operations": write operations 1423218a536aSLuiz Capitulino * 1424218a536aSLuiz Capitulino * Example: 1425218a536aSLuiz Capitulino * 1426218a536aSLuiz Capitulino * [ { "device": "ide0-hd0", 1427218a536aSLuiz Capitulino * "stats": { "rd_bytes": 512, 1428218a536aSLuiz Capitulino * "wr_bytes": 0, 1429218a536aSLuiz Capitulino * "rd_operations": 1, 1430218a536aSLuiz Capitulino * "wr_operations": 0 } }, 1431218a536aSLuiz Capitulino * { "device": "ide1-cd0", 1432218a536aSLuiz Capitulino * "stats": { "rd_bytes": 0, 1433218a536aSLuiz Capitulino * "wr_bytes": 0, 1434218a536aSLuiz Capitulino * "rd_operations": 0, 1435218a536aSLuiz Capitulino * "wr_operations": 0 } } ] 1436218a536aSLuiz Capitulino */ 1437218a536aSLuiz Capitulino void bdrv_info_stats(Monitor *mon, QObject **ret_data) 1438218a536aSLuiz Capitulino { 1439218a536aSLuiz Capitulino QObject *obj; 1440218a536aSLuiz Capitulino QList *devices; 1441a36e69ddSths BlockDriverState *bs; 1442a36e69ddSths 1443218a536aSLuiz Capitulino devices = qlist_new(); 1444218a536aSLuiz Capitulino 1445*1b7bdbc1SStefan Hajnoczi QTAILQ_FOREACH(bs, &bdrv_states, list) { 1446218a536aSLuiz Capitulino obj = qobject_from_jsonf("{ 'device': %s, 'stats': {" 1447218a536aSLuiz Capitulino "'rd_bytes': %" PRId64 "," 1448218a536aSLuiz Capitulino "'wr_bytes': %" PRId64 "," 1449218a536aSLuiz Capitulino "'rd_operations': %" PRId64 "," 1450218a536aSLuiz Capitulino "'wr_operations': %" PRId64 1451218a536aSLuiz Capitulino "} }", 1452a36e69ddSths bs->device_name, 1453a36e69ddSths bs->rd_bytes, bs->wr_bytes, 1454a36e69ddSths bs->rd_ops, bs->wr_ops); 1455218a536aSLuiz Capitulino qlist_append_obj(devices, obj); 1456a36e69ddSths } 1457218a536aSLuiz Capitulino 1458218a536aSLuiz Capitulino *ret_data = QOBJECT(devices); 1459a36e69ddSths } 1460ea2384d3Sbellard 1461045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1462045df330Saliguori { 1463045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1464045df330Saliguori return bs->backing_file; 1465045df330Saliguori else if (bs->encrypted) 1466045df330Saliguori return bs->filename; 1467045df330Saliguori else 1468045df330Saliguori return NULL; 1469045df330Saliguori } 1470045df330Saliguori 147183f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 147283f64091Sbellard char *filename, int filename_size) 147383f64091Sbellard { 1474b783e409SKevin Wolf if (!bs->backing_file) { 147583f64091Sbellard pstrcpy(filename, filename_size, ""); 147683f64091Sbellard } else { 147783f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 147883f64091Sbellard } 147983f64091Sbellard } 148083f64091Sbellard 1481faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1482faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1483faea38e7Sbellard { 1484faea38e7Sbellard BlockDriver *drv = bs->drv; 1485faea38e7Sbellard if (!drv) 148619cb3738Sbellard return -ENOMEDIUM; 1487faea38e7Sbellard if (!drv->bdrv_write_compressed) 1488faea38e7Sbellard return -ENOTSUP; 1489fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 1490fbb7b4e0SKevin Wolf return -EIO; 14917cd1e32aSlirans@il.ibm.com 1492c6d22830SJan Kiszka if (bs->dirty_bitmap) { 14937cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 14947cd1e32aSlirans@il.ibm.com } 14957cd1e32aSlirans@il.ibm.com 1496faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1497faea38e7Sbellard } 1498faea38e7Sbellard 1499faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1500faea38e7Sbellard { 1501faea38e7Sbellard BlockDriver *drv = bs->drv; 1502faea38e7Sbellard if (!drv) 150319cb3738Sbellard return -ENOMEDIUM; 1504faea38e7Sbellard if (!drv->bdrv_get_info) 1505faea38e7Sbellard return -ENOTSUP; 1506faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1507faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1508faea38e7Sbellard } 1509faea38e7Sbellard 151045566e9cSChristoph Hellwig int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf, 151145566e9cSChristoph Hellwig int64_t pos, int size) 1512178e08a5Saliguori { 1513178e08a5Saliguori BlockDriver *drv = bs->drv; 1514178e08a5Saliguori if (!drv) 1515178e08a5Saliguori return -ENOMEDIUM; 151645566e9cSChristoph Hellwig if (!drv->bdrv_save_vmstate) 1517178e08a5Saliguori return -ENOTSUP; 151845566e9cSChristoph Hellwig return drv->bdrv_save_vmstate(bs, buf, pos, size); 1519178e08a5Saliguori } 1520178e08a5Saliguori 152145566e9cSChristoph Hellwig int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf, 152245566e9cSChristoph Hellwig int64_t pos, int size) 1523178e08a5Saliguori { 1524178e08a5Saliguori BlockDriver *drv = bs->drv; 1525178e08a5Saliguori if (!drv) 1526178e08a5Saliguori return -ENOMEDIUM; 152745566e9cSChristoph Hellwig if (!drv->bdrv_load_vmstate) 1528178e08a5Saliguori return -ENOTSUP; 152945566e9cSChristoph Hellwig return drv->bdrv_load_vmstate(bs, buf, pos, size); 1530178e08a5Saliguori } 1531178e08a5Saliguori 15328b9b0cc2SKevin Wolf void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event) 15338b9b0cc2SKevin Wolf { 15348b9b0cc2SKevin Wolf BlockDriver *drv = bs->drv; 15358b9b0cc2SKevin Wolf 15368b9b0cc2SKevin Wolf if (!drv || !drv->bdrv_debug_event) { 15378b9b0cc2SKevin Wolf return; 15388b9b0cc2SKevin Wolf } 15398b9b0cc2SKevin Wolf 15408b9b0cc2SKevin Wolf return drv->bdrv_debug_event(bs, event); 15418b9b0cc2SKevin Wolf 15428b9b0cc2SKevin Wolf } 15438b9b0cc2SKevin Wolf 1544faea38e7Sbellard /**************************************************************/ 1545faea38e7Sbellard /* handling of snapshots */ 1546faea38e7Sbellard 1547faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1548faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1549faea38e7Sbellard { 1550faea38e7Sbellard BlockDriver *drv = bs->drv; 1551faea38e7Sbellard if (!drv) 155219cb3738Sbellard return -ENOMEDIUM; 1553faea38e7Sbellard if (!drv->bdrv_snapshot_create) 1554faea38e7Sbellard return -ENOTSUP; 1555faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 1556faea38e7Sbellard } 1557faea38e7Sbellard 1558faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1559faea38e7Sbellard const char *snapshot_id) 1560faea38e7Sbellard { 1561faea38e7Sbellard BlockDriver *drv = bs->drv; 1562faea38e7Sbellard if (!drv) 156319cb3738Sbellard return -ENOMEDIUM; 1564faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 1565faea38e7Sbellard return -ENOTSUP; 1566faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 1567faea38e7Sbellard } 1568faea38e7Sbellard 1569faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1570faea38e7Sbellard { 1571faea38e7Sbellard BlockDriver *drv = bs->drv; 1572faea38e7Sbellard if (!drv) 157319cb3738Sbellard return -ENOMEDIUM; 1574faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 1575faea38e7Sbellard return -ENOTSUP; 1576faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 1577faea38e7Sbellard } 1578faea38e7Sbellard 1579faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1580faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1581faea38e7Sbellard { 1582faea38e7Sbellard BlockDriver *drv = bs->drv; 1583faea38e7Sbellard if (!drv) 158419cb3738Sbellard return -ENOMEDIUM; 1585faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1586faea38e7Sbellard return -ENOTSUP; 1587faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1588faea38e7Sbellard } 1589faea38e7Sbellard 1590faea38e7Sbellard #define NB_SUFFIXES 4 1591faea38e7Sbellard 1592faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1593faea38e7Sbellard { 1594faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1595faea38e7Sbellard int64_t base; 1596faea38e7Sbellard int i; 1597faea38e7Sbellard 1598faea38e7Sbellard if (size <= 999) { 1599faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1600faea38e7Sbellard } else { 1601faea38e7Sbellard base = 1024; 1602faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1603faea38e7Sbellard if (size < (10 * base)) { 1604faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1605faea38e7Sbellard (double)size / base, 1606faea38e7Sbellard suffixes[i]); 1607faea38e7Sbellard break; 1608faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1609faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1610faea38e7Sbellard ((size + (base >> 1)) / base), 1611faea38e7Sbellard suffixes[i]); 1612faea38e7Sbellard break; 1613faea38e7Sbellard } 1614faea38e7Sbellard base = base * 1024; 1615faea38e7Sbellard } 1616faea38e7Sbellard } 1617faea38e7Sbellard return buf; 1618faea38e7Sbellard } 1619faea38e7Sbellard 1620faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1621faea38e7Sbellard { 1622faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 16233b9f94e1Sbellard #ifdef _WIN32 16243b9f94e1Sbellard struct tm *ptm; 16253b9f94e1Sbellard #else 1626faea38e7Sbellard struct tm tm; 16273b9f94e1Sbellard #endif 1628faea38e7Sbellard time_t ti; 1629faea38e7Sbellard int64_t secs; 1630faea38e7Sbellard 1631faea38e7Sbellard if (!sn) { 1632faea38e7Sbellard snprintf(buf, buf_size, 1633faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1634faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1635faea38e7Sbellard } else { 1636faea38e7Sbellard ti = sn->date_sec; 16373b9f94e1Sbellard #ifdef _WIN32 16383b9f94e1Sbellard ptm = localtime(&ti); 16393b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 16403b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 16413b9f94e1Sbellard #else 1642faea38e7Sbellard localtime_r(&ti, &tm); 1643faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1644faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 16453b9f94e1Sbellard #endif 1646faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1647faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1648faea38e7Sbellard "%02d:%02d:%02d.%03d", 1649faea38e7Sbellard (int)(secs / 3600), 1650faea38e7Sbellard (int)((secs / 60) % 60), 1651faea38e7Sbellard (int)(secs % 60), 1652faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1653faea38e7Sbellard snprintf(buf, buf_size, 1654faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1655faea38e7Sbellard sn->id_str, sn->name, 1656faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1657faea38e7Sbellard date_buf, 1658faea38e7Sbellard clock_buf); 1659faea38e7Sbellard } 1660faea38e7Sbellard return buf; 1661faea38e7Sbellard } 1662faea38e7Sbellard 166383f64091Sbellard 1664ea2384d3Sbellard /**************************************************************/ 166583f64091Sbellard /* async I/Os */ 1666ea2384d3Sbellard 16673b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 1668f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 166983f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1670ea2384d3Sbellard { 167183f64091Sbellard BlockDriver *drv = bs->drv; 1672a36e69ddSths BlockDriverAIOCB *ret; 1673ea2384d3Sbellard 167419cb3738Sbellard if (!drv) 1675ce1a14dcSpbrook return NULL; 167671d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 167771d0770cSaliguori return NULL; 167883f64091Sbellard 1679f141eafeSaliguori ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, 1680f141eafeSaliguori cb, opaque); 1681a36e69ddSths 1682a36e69ddSths if (ret) { 1683a36e69ddSths /* Update stats even though technically transfer has not happened. */ 16846ea44308SJan Kiszka bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 1685a36e69ddSths bs->rd_ops ++; 1686a36e69ddSths } 1687a36e69ddSths 1688a36e69ddSths return ret; 168983f64091Sbellard } 169083f64091Sbellard 1691f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 1692f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 169383f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 16947674e7bfSbellard { 169583f64091Sbellard BlockDriver *drv = bs->drv; 1696a36e69ddSths BlockDriverAIOCB *ret; 169783f64091Sbellard 169819cb3738Sbellard if (!drv) 1699ce1a14dcSpbrook return NULL; 170083f64091Sbellard if (bs->read_only) 1701ce1a14dcSpbrook return NULL; 170271d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 170371d0770cSaliguori return NULL; 170483f64091Sbellard 1705c6d22830SJan Kiszka if (bs->dirty_bitmap) { 17067cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, sector_num, nb_sectors, 1); 17077cd1e32aSlirans@il.ibm.com } 17087cd1e32aSlirans@il.ibm.com 1709f141eafeSaliguori ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, 1710f141eafeSaliguori cb, opaque); 1711a36e69ddSths 1712a36e69ddSths if (ret) { 1713a36e69ddSths /* Update stats even though technically transfer has not happened. */ 17146ea44308SJan Kiszka bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE; 1715a36e69ddSths bs->wr_ops ++; 1716a36e69ddSths } 1717a36e69ddSths 1718a36e69ddSths return ret; 171983f64091Sbellard } 172083f64091Sbellard 172140b4f539SKevin Wolf 172240b4f539SKevin Wolf typedef struct MultiwriteCB { 172340b4f539SKevin Wolf int error; 172440b4f539SKevin Wolf int num_requests; 172540b4f539SKevin Wolf int num_callbacks; 172640b4f539SKevin Wolf struct { 172740b4f539SKevin Wolf BlockDriverCompletionFunc *cb; 172840b4f539SKevin Wolf void *opaque; 172940b4f539SKevin Wolf QEMUIOVector *free_qiov; 173040b4f539SKevin Wolf void *free_buf; 173140b4f539SKevin Wolf } callbacks[]; 173240b4f539SKevin Wolf } MultiwriteCB; 173340b4f539SKevin Wolf 173440b4f539SKevin Wolf static void multiwrite_user_cb(MultiwriteCB *mcb) 173540b4f539SKevin Wolf { 173640b4f539SKevin Wolf int i; 173740b4f539SKevin Wolf 173840b4f539SKevin Wolf for (i = 0; i < mcb->num_callbacks; i++) { 173940b4f539SKevin Wolf mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error); 174040b4f539SKevin Wolf qemu_free(mcb->callbacks[i].free_qiov); 1741f8a83245SHerve Poussineau qemu_vfree(mcb->callbacks[i].free_buf); 174240b4f539SKevin Wolf } 174340b4f539SKevin Wolf } 174440b4f539SKevin Wolf 174540b4f539SKevin Wolf static void multiwrite_cb(void *opaque, int ret) 174640b4f539SKevin Wolf { 174740b4f539SKevin Wolf MultiwriteCB *mcb = opaque; 174840b4f539SKevin Wolf 1749cb6d3ca0SKevin Wolf if (ret < 0 && !mcb->error) { 175040b4f539SKevin Wolf mcb->error = ret; 175140b4f539SKevin Wolf multiwrite_user_cb(mcb); 175240b4f539SKevin Wolf } 175340b4f539SKevin Wolf 175440b4f539SKevin Wolf mcb->num_requests--; 175540b4f539SKevin Wolf if (mcb->num_requests == 0) { 175640b4f539SKevin Wolf if (mcb->error == 0) { 175740b4f539SKevin Wolf multiwrite_user_cb(mcb); 175840b4f539SKevin Wolf } 175940b4f539SKevin Wolf qemu_free(mcb); 176040b4f539SKevin Wolf } 176140b4f539SKevin Wolf } 176240b4f539SKevin Wolf 176340b4f539SKevin Wolf static int multiwrite_req_compare(const void *a, const void *b) 176440b4f539SKevin Wolf { 176540b4f539SKevin Wolf return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector); 176640b4f539SKevin Wolf } 176740b4f539SKevin Wolf 176840b4f539SKevin Wolf /* 176940b4f539SKevin Wolf * Takes a bunch of requests and tries to merge them. Returns the number of 177040b4f539SKevin Wolf * requests that remain after merging. 177140b4f539SKevin Wolf */ 177240b4f539SKevin Wolf static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs, 177340b4f539SKevin Wolf int num_reqs, MultiwriteCB *mcb) 177440b4f539SKevin Wolf { 177540b4f539SKevin Wolf int i, outidx; 177640b4f539SKevin Wolf 177740b4f539SKevin Wolf // Sort requests by start sector 177840b4f539SKevin Wolf qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare); 177940b4f539SKevin Wolf 178040b4f539SKevin Wolf // Check if adjacent requests touch the same clusters. If so, combine them, 178140b4f539SKevin Wolf // filling up gaps with zero sectors. 178240b4f539SKevin Wolf outidx = 0; 178340b4f539SKevin Wolf for (i = 1; i < num_reqs; i++) { 178440b4f539SKevin Wolf int merge = 0; 178540b4f539SKevin Wolf int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors; 178640b4f539SKevin Wolf 178740b4f539SKevin Wolf // This handles the cases that are valid for all block drivers, namely 178840b4f539SKevin Wolf // exactly sequential writes and overlapping writes. 178940b4f539SKevin Wolf if (reqs[i].sector <= oldreq_last) { 179040b4f539SKevin Wolf merge = 1; 179140b4f539SKevin Wolf } 179240b4f539SKevin Wolf 179340b4f539SKevin Wolf // The block driver may decide that it makes sense to combine requests 179440b4f539SKevin Wolf // even if there is a gap of some sectors between them. In this case, 179540b4f539SKevin Wolf // the gap is filled with zeros (therefore only applicable for yet 179640b4f539SKevin Wolf // unused space in format like qcow2). 179740b4f539SKevin Wolf if (!merge && bs->drv->bdrv_merge_requests) { 179840b4f539SKevin Wolf merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]); 179940b4f539SKevin Wolf } 180040b4f539SKevin Wolf 1801e2a305fbSChristoph Hellwig if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) { 1802e2a305fbSChristoph Hellwig merge = 0; 1803e2a305fbSChristoph Hellwig } 1804e2a305fbSChristoph Hellwig 180540b4f539SKevin Wolf if (merge) { 180640b4f539SKevin Wolf size_t size; 180740b4f539SKevin Wolf QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov)); 180840b4f539SKevin Wolf qemu_iovec_init(qiov, 180940b4f539SKevin Wolf reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1); 181040b4f539SKevin Wolf 181140b4f539SKevin Wolf // Add the first request to the merged one. If the requests are 181240b4f539SKevin Wolf // overlapping, drop the last sectors of the first request. 181340b4f539SKevin Wolf size = (reqs[i].sector - reqs[outidx].sector) << 9; 181440b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[outidx].qiov, size); 181540b4f539SKevin Wolf 181640b4f539SKevin Wolf // We might need to add some zeros between the two requests 181740b4f539SKevin Wolf if (reqs[i].sector > oldreq_last) { 181840b4f539SKevin Wolf size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9; 181940b4f539SKevin Wolf uint8_t *buf = qemu_blockalign(bs, zero_bytes); 182040b4f539SKevin Wolf memset(buf, 0, zero_bytes); 182140b4f539SKevin Wolf qemu_iovec_add(qiov, buf, zero_bytes); 182240b4f539SKevin Wolf mcb->callbacks[i].free_buf = buf; 182340b4f539SKevin Wolf } 182440b4f539SKevin Wolf 182540b4f539SKevin Wolf // Add the second request 182640b4f539SKevin Wolf qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size); 182740b4f539SKevin Wolf 182840b4f539SKevin Wolf reqs[outidx].nb_sectors += reqs[i].nb_sectors; 182940b4f539SKevin Wolf reqs[outidx].qiov = qiov; 183040b4f539SKevin Wolf 183140b4f539SKevin Wolf mcb->callbacks[i].free_qiov = reqs[outidx].qiov; 183240b4f539SKevin Wolf } else { 183340b4f539SKevin Wolf outidx++; 183440b4f539SKevin Wolf reqs[outidx].sector = reqs[i].sector; 183540b4f539SKevin Wolf reqs[outidx].nb_sectors = reqs[i].nb_sectors; 183640b4f539SKevin Wolf reqs[outidx].qiov = reqs[i].qiov; 183740b4f539SKevin Wolf } 183840b4f539SKevin Wolf } 183940b4f539SKevin Wolf 184040b4f539SKevin Wolf return outidx + 1; 184140b4f539SKevin Wolf } 184240b4f539SKevin Wolf 184340b4f539SKevin Wolf /* 184440b4f539SKevin Wolf * Submit multiple AIO write requests at once. 184540b4f539SKevin Wolf * 184640b4f539SKevin Wolf * On success, the function returns 0 and all requests in the reqs array have 184740b4f539SKevin Wolf * been submitted. In error case this function returns -1, and any of the 184840b4f539SKevin Wolf * requests may or may not be submitted yet. In particular, this means that the 184940b4f539SKevin Wolf * callback will be called for some of the requests, for others it won't. The 185040b4f539SKevin Wolf * caller must check the error field of the BlockRequest to wait for the right 185140b4f539SKevin Wolf * callbacks (if error != 0, no callback will be called). 185240b4f539SKevin Wolf * 185340b4f539SKevin Wolf * The implementation may modify the contents of the reqs array, e.g. to merge 185440b4f539SKevin Wolf * requests. However, the fields opaque and error are left unmodified as they 185540b4f539SKevin Wolf * are used to signal failure for a single request to the caller. 185640b4f539SKevin Wolf */ 185740b4f539SKevin Wolf int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs) 185840b4f539SKevin Wolf { 185940b4f539SKevin Wolf BlockDriverAIOCB *acb; 186040b4f539SKevin Wolf MultiwriteCB *mcb; 186140b4f539SKevin Wolf int i; 186240b4f539SKevin Wolf 186340b4f539SKevin Wolf if (num_reqs == 0) { 186440b4f539SKevin Wolf return 0; 186540b4f539SKevin Wolf } 186640b4f539SKevin Wolf 186740b4f539SKevin Wolf // Create MultiwriteCB structure 186840b4f539SKevin Wolf mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks)); 186940b4f539SKevin Wolf mcb->num_requests = 0; 187040b4f539SKevin Wolf mcb->num_callbacks = num_reqs; 187140b4f539SKevin Wolf 187240b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 187340b4f539SKevin Wolf mcb->callbacks[i].cb = reqs[i].cb; 187440b4f539SKevin Wolf mcb->callbacks[i].opaque = reqs[i].opaque; 187540b4f539SKevin Wolf } 187640b4f539SKevin Wolf 187740b4f539SKevin Wolf // Check for mergable requests 187840b4f539SKevin Wolf num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb); 187940b4f539SKevin Wolf 188040b4f539SKevin Wolf // Run the aio requests 188140b4f539SKevin Wolf for (i = 0; i < num_reqs; i++) { 188240b4f539SKevin Wolf acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov, 188340b4f539SKevin Wolf reqs[i].nb_sectors, multiwrite_cb, mcb); 188440b4f539SKevin Wolf 188540b4f539SKevin Wolf if (acb == NULL) { 188640b4f539SKevin Wolf // We can only fail the whole thing if no request has been 188740b4f539SKevin Wolf // submitted yet. Otherwise we'll wait for the submitted AIOs to 188840b4f539SKevin Wolf // complete and report the error in the callback. 188940b4f539SKevin Wolf if (mcb->num_requests == 0) { 18900f0b604bSKevin Wolf reqs[i].error = -EIO; 189140b4f539SKevin Wolf goto fail; 189240b4f539SKevin Wolf } else { 18937eb58a6cSKevin Wolf mcb->num_requests++; 18947eb58a6cSKevin Wolf multiwrite_cb(mcb, -EIO); 189540b4f539SKevin Wolf break; 189640b4f539SKevin Wolf } 189740b4f539SKevin Wolf } else { 189840b4f539SKevin Wolf mcb->num_requests++; 189940b4f539SKevin Wolf } 190040b4f539SKevin Wolf } 190140b4f539SKevin Wolf 190240b4f539SKevin Wolf return 0; 190340b4f539SKevin Wolf 190440b4f539SKevin Wolf fail: 190540b4f539SKevin Wolf free(mcb); 190640b4f539SKevin Wolf return -1; 190740b4f539SKevin Wolf } 190840b4f539SKevin Wolf 1909b2e12bc6SChristoph Hellwig BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs, 1910b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 1911b2e12bc6SChristoph Hellwig { 1912b2e12bc6SChristoph Hellwig BlockDriver *drv = bs->drv; 1913b2e12bc6SChristoph Hellwig 1914b2e12bc6SChristoph Hellwig if (!drv) 1915b2e12bc6SChristoph Hellwig return NULL; 1916b2e12bc6SChristoph Hellwig return drv->bdrv_aio_flush(bs, cb, opaque); 1917b2e12bc6SChristoph Hellwig } 1918b2e12bc6SChristoph Hellwig 191983f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 192083f64091Sbellard { 19216bbff9a0Saliguori acb->pool->cancel(acb); 192283f64091Sbellard } 192383f64091Sbellard 192483f64091Sbellard 192583f64091Sbellard /**************************************************************/ 192683f64091Sbellard /* async block device emulation */ 192783f64091Sbellard 1928c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 1929c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 1930c16b5a2cSChristoph Hellwig QEMUBH *bh; 1931c16b5a2cSChristoph Hellwig int ret; 1932c16b5a2cSChristoph Hellwig /* vector translation state */ 1933c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 1934c16b5a2cSChristoph Hellwig uint8_t *bounce; 1935c16b5a2cSChristoph Hellwig int is_write; 1936c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 1937c16b5a2cSChristoph Hellwig 1938c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 1939c16b5a2cSChristoph Hellwig { 1940c16b5a2cSChristoph Hellwig BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; 19416a7ad299SDor Laor qemu_bh_delete(acb->bh); 194236afc451SAvi Kivity acb->bh = NULL; 1943c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 1944c16b5a2cSChristoph Hellwig } 1945c16b5a2cSChristoph Hellwig 1946c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 1947c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 1948c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 1949c16b5a2cSChristoph Hellwig }; 1950c16b5a2cSChristoph Hellwig 195183f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 1952beac80cdSbellard { 1953ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 1954f141eafeSaliguori 1955f141eafeSaliguori if (!acb->is_write) 1956f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 1957ceb42de8Saliguori qemu_vfree(acb->bounce); 1958ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 19596a7ad299SDor Laor qemu_bh_delete(acb->bh); 196036afc451SAvi Kivity acb->bh = NULL; 1961ce1a14dcSpbrook qemu_aio_release(acb); 1962beac80cdSbellard } 1963beac80cdSbellard 1964f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 1965f141eafeSaliguori int64_t sector_num, 1966f141eafeSaliguori QEMUIOVector *qiov, 1967f141eafeSaliguori int nb_sectors, 1968f141eafeSaliguori BlockDriverCompletionFunc *cb, 1969f141eafeSaliguori void *opaque, 1970f141eafeSaliguori int is_write) 1971f141eafeSaliguori 1972ea2384d3Sbellard { 1973ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 197483f64091Sbellard 1975c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 1976f141eafeSaliguori acb->is_write = is_write; 1977f141eafeSaliguori acb->qiov = qiov; 1978e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 1979f141eafeSaliguori 1980ce1a14dcSpbrook if (!acb->bh) 1981ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1982f141eafeSaliguori 1983f141eafeSaliguori if (is_write) { 1984f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 1985f141eafeSaliguori acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 1986f141eafeSaliguori } else { 1987f141eafeSaliguori acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 1988f141eafeSaliguori } 1989f141eafeSaliguori 1990ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1991f141eafeSaliguori 1992ce1a14dcSpbrook return &acb->common; 19937a6cba61Spbrook } 19947a6cba61Spbrook 1995f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 1996f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 1997ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 199883f64091Sbellard { 1999f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 200083f64091Sbellard } 200183f64091Sbellard 2002f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 2003f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 2004f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 2005f141eafeSaliguori { 2006f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 2007f141eafeSaliguori } 2008f141eafeSaliguori 2009b2e12bc6SChristoph Hellwig static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs, 2010b2e12bc6SChristoph Hellwig BlockDriverCompletionFunc *cb, void *opaque) 2011b2e12bc6SChristoph Hellwig { 2012b2e12bc6SChristoph Hellwig BlockDriverAIOCBSync *acb; 2013b2e12bc6SChristoph Hellwig 2014b2e12bc6SChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 2015b2e12bc6SChristoph Hellwig acb->is_write = 1; /* don't bounce in the completion hadler */ 2016b2e12bc6SChristoph Hellwig acb->qiov = NULL; 2017b2e12bc6SChristoph Hellwig acb->bounce = NULL; 2018b2e12bc6SChristoph Hellwig acb->ret = 0; 2019b2e12bc6SChristoph Hellwig 2020b2e12bc6SChristoph Hellwig if (!acb->bh) 2021b2e12bc6SChristoph Hellwig acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 2022b2e12bc6SChristoph Hellwig 2023b2e12bc6SChristoph Hellwig bdrv_flush(bs); 2024b2e12bc6SChristoph Hellwig qemu_bh_schedule(acb->bh); 2025b2e12bc6SChristoph Hellwig return &acb->common; 2026b2e12bc6SChristoph Hellwig } 2027b2e12bc6SChristoph Hellwig 202883f64091Sbellard /**************************************************************/ 202983f64091Sbellard /* sync block device emulation */ 203083f64091Sbellard 203183f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 203283f64091Sbellard { 203383f64091Sbellard *(int *)opaque = ret; 203483f64091Sbellard } 203583f64091Sbellard 203683f64091Sbellard #define NOT_DONE 0x7fffffff 203783f64091Sbellard 203883f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 203983f64091Sbellard uint8_t *buf, int nb_sectors) 204083f64091Sbellard { 2041ce1a14dcSpbrook int async_ret; 2042ce1a14dcSpbrook BlockDriverAIOCB *acb; 2043f141eafeSaliguori struct iovec iov; 2044f141eafeSaliguori QEMUIOVector qiov; 204583f64091Sbellard 204665d6b3d8SKevin Wolf async_context_push(); 204765d6b3d8SKevin Wolf 204883f64091Sbellard async_ret = NOT_DONE; 20493f4cb3d3Sblueswir1 iov.iov_base = (void *)buf; 2050f141eafeSaliguori iov.iov_len = nb_sectors * 512; 2051f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2052f141eafeSaliguori acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, 205383f64091Sbellard bdrv_rw_em_cb, &async_ret); 205465d6b3d8SKevin Wolf if (acb == NULL) { 205565d6b3d8SKevin Wolf async_ret = -1; 205665d6b3d8SKevin Wolf goto fail; 205765d6b3d8SKevin Wolf } 2058baf35cb9Saliguori 205983f64091Sbellard while (async_ret == NOT_DONE) { 206083f64091Sbellard qemu_aio_wait(); 206183f64091Sbellard } 2062baf35cb9Saliguori 206365d6b3d8SKevin Wolf 206465d6b3d8SKevin Wolf fail: 206565d6b3d8SKevin Wolf async_context_pop(); 206683f64091Sbellard return async_ret; 206783f64091Sbellard } 206883f64091Sbellard 206983f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 207083f64091Sbellard const uint8_t *buf, int nb_sectors) 207183f64091Sbellard { 2072ce1a14dcSpbrook int async_ret; 2073ce1a14dcSpbrook BlockDriverAIOCB *acb; 2074f141eafeSaliguori struct iovec iov; 2075f141eafeSaliguori QEMUIOVector qiov; 207683f64091Sbellard 207765d6b3d8SKevin Wolf async_context_push(); 207865d6b3d8SKevin Wolf 207983f64091Sbellard async_ret = NOT_DONE; 2080f141eafeSaliguori iov.iov_base = (void *)buf; 2081f141eafeSaliguori iov.iov_len = nb_sectors * 512; 2082f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 2083f141eafeSaliguori acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, 208483f64091Sbellard bdrv_rw_em_cb, &async_ret); 208565d6b3d8SKevin Wolf if (acb == NULL) { 208665d6b3d8SKevin Wolf async_ret = -1; 208765d6b3d8SKevin Wolf goto fail; 208865d6b3d8SKevin Wolf } 208983f64091Sbellard while (async_ret == NOT_DONE) { 209083f64091Sbellard qemu_aio_wait(); 209183f64091Sbellard } 209265d6b3d8SKevin Wolf 209365d6b3d8SKevin Wolf fail: 209465d6b3d8SKevin Wolf async_context_pop(); 209583f64091Sbellard return async_ret; 209683f64091Sbellard } 2097ea2384d3Sbellard 2098ea2384d3Sbellard void bdrv_init(void) 2099ea2384d3Sbellard { 21005efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 2101ea2384d3Sbellard } 2102ce1a14dcSpbrook 2103eb852011SMarkus Armbruster void bdrv_init_with_whitelist(void) 2104eb852011SMarkus Armbruster { 2105eb852011SMarkus Armbruster use_bdrv_whitelist = 1; 2106eb852011SMarkus Armbruster bdrv_init(); 2107eb852011SMarkus Armbruster } 2108eb852011SMarkus Armbruster 2109c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 21106bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 21116bbff9a0Saliguori { 2112ce1a14dcSpbrook BlockDriverAIOCB *acb; 2113ce1a14dcSpbrook 21146bbff9a0Saliguori if (pool->free_aiocb) { 21156bbff9a0Saliguori acb = pool->free_aiocb; 21166bbff9a0Saliguori pool->free_aiocb = acb->next; 2117ce1a14dcSpbrook } else { 21186bbff9a0Saliguori acb = qemu_mallocz(pool->aiocb_size); 21196bbff9a0Saliguori acb->pool = pool; 2120ce1a14dcSpbrook } 2121ce1a14dcSpbrook acb->bs = bs; 2122ce1a14dcSpbrook acb->cb = cb; 2123ce1a14dcSpbrook acb->opaque = opaque; 2124ce1a14dcSpbrook return acb; 2125ce1a14dcSpbrook } 2126ce1a14dcSpbrook 2127ce1a14dcSpbrook void qemu_aio_release(void *p) 2128ce1a14dcSpbrook { 21296bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 21306bbff9a0Saliguori AIOPool *pool = acb->pool; 21316bbff9a0Saliguori acb->next = pool->free_aiocb; 21326bbff9a0Saliguori pool->free_aiocb = acb; 2133ce1a14dcSpbrook } 213419cb3738Sbellard 213519cb3738Sbellard /**************************************************************/ 213619cb3738Sbellard /* removable device support */ 213719cb3738Sbellard 213819cb3738Sbellard /** 213919cb3738Sbellard * Return TRUE if the media is present 214019cb3738Sbellard */ 214119cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 214219cb3738Sbellard { 214319cb3738Sbellard BlockDriver *drv = bs->drv; 214419cb3738Sbellard int ret; 214519cb3738Sbellard if (!drv) 214619cb3738Sbellard return 0; 214719cb3738Sbellard if (!drv->bdrv_is_inserted) 214819cb3738Sbellard return 1; 214919cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 215019cb3738Sbellard return ret; 215119cb3738Sbellard } 215219cb3738Sbellard 215319cb3738Sbellard /** 215419cb3738Sbellard * Return TRUE if the media changed since the last call to this 215519cb3738Sbellard * function. It is currently only used for floppy disks 215619cb3738Sbellard */ 215719cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 215819cb3738Sbellard { 215919cb3738Sbellard BlockDriver *drv = bs->drv; 216019cb3738Sbellard int ret; 216119cb3738Sbellard 216219cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 216319cb3738Sbellard ret = -ENOTSUP; 216419cb3738Sbellard else 216519cb3738Sbellard ret = drv->bdrv_media_changed(bs); 216619cb3738Sbellard if (ret == -ENOTSUP) 216719cb3738Sbellard ret = bs->media_changed; 216819cb3738Sbellard bs->media_changed = 0; 216919cb3738Sbellard return ret; 217019cb3738Sbellard } 217119cb3738Sbellard 217219cb3738Sbellard /** 217319cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 217419cb3738Sbellard */ 2175aea2a33cSMark McLoughlin int bdrv_eject(BlockDriverState *bs, int eject_flag) 217619cb3738Sbellard { 217719cb3738Sbellard BlockDriver *drv = bs->drv; 217819cb3738Sbellard int ret; 217919cb3738Sbellard 2180aea2a33cSMark McLoughlin if (bs->locked) { 2181aea2a33cSMark McLoughlin return -EBUSY; 2182aea2a33cSMark McLoughlin } 2183aea2a33cSMark McLoughlin 218419cb3738Sbellard if (!drv || !drv->bdrv_eject) { 218519cb3738Sbellard ret = -ENOTSUP; 218619cb3738Sbellard } else { 218719cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 218819cb3738Sbellard } 218919cb3738Sbellard if (ret == -ENOTSUP) { 219019cb3738Sbellard if (eject_flag) 219119cb3738Sbellard bdrv_close(bs); 2192aea2a33cSMark McLoughlin ret = 0; 219319cb3738Sbellard } 2194aea2a33cSMark McLoughlin 2195aea2a33cSMark McLoughlin return ret; 219619cb3738Sbellard } 219719cb3738Sbellard 219819cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 219919cb3738Sbellard { 220019cb3738Sbellard return bs->locked; 220119cb3738Sbellard } 220219cb3738Sbellard 220319cb3738Sbellard /** 220419cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 220519cb3738Sbellard * to eject it manually). 220619cb3738Sbellard */ 220719cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 220819cb3738Sbellard { 220919cb3738Sbellard BlockDriver *drv = bs->drv; 221019cb3738Sbellard 221119cb3738Sbellard bs->locked = locked; 221219cb3738Sbellard if (drv && drv->bdrv_set_locked) { 221319cb3738Sbellard drv->bdrv_set_locked(bs, locked); 221419cb3738Sbellard } 221519cb3738Sbellard } 2216985a03b0Sths 2217985a03b0Sths /* needed for generic scsi interface */ 2218985a03b0Sths 2219985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 2220985a03b0Sths { 2221985a03b0Sths BlockDriver *drv = bs->drv; 2222985a03b0Sths 2223985a03b0Sths if (drv && drv->bdrv_ioctl) 2224985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 2225985a03b0Sths return -ENOTSUP; 2226985a03b0Sths } 22277d780669Saliguori 2228221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 2229221f715dSaliguori unsigned long int req, void *buf, 22307d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 22317d780669Saliguori { 2232221f715dSaliguori BlockDriver *drv = bs->drv; 22337d780669Saliguori 2234221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 2235221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 2236221f715dSaliguori return NULL; 22377d780669Saliguori } 2238e268ca52Saliguori 22397cd1e32aSlirans@il.ibm.com 22407cd1e32aSlirans@il.ibm.com 2241e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 2242e268ca52Saliguori { 2243e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 2244e268ca52Saliguori } 22457cd1e32aSlirans@il.ibm.com 22467cd1e32aSlirans@il.ibm.com void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable) 22477cd1e32aSlirans@il.ibm.com { 22487cd1e32aSlirans@il.ibm.com int64_t bitmap_size; 2249a55eb92cSJan Kiszka 2250aaa0eb75SLiran Schour bs->dirty_count = 0; 22517cd1e32aSlirans@il.ibm.com if (enable) { 2252c6d22830SJan Kiszka if (!bs->dirty_bitmap) { 2253c6d22830SJan Kiszka bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) + 2254c6d22830SJan Kiszka BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1; 2255c6d22830SJan Kiszka bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8; 22567cd1e32aSlirans@il.ibm.com 22577cd1e32aSlirans@il.ibm.com bs->dirty_bitmap = qemu_mallocz(bitmap_size); 22587cd1e32aSlirans@il.ibm.com } 22597cd1e32aSlirans@il.ibm.com } else { 2260c6d22830SJan Kiszka if (bs->dirty_bitmap) { 22617cd1e32aSlirans@il.ibm.com qemu_free(bs->dirty_bitmap); 2262c6d22830SJan Kiszka bs->dirty_bitmap = NULL; 22637cd1e32aSlirans@il.ibm.com } 22647cd1e32aSlirans@il.ibm.com } 22657cd1e32aSlirans@il.ibm.com } 22667cd1e32aSlirans@il.ibm.com 22677cd1e32aSlirans@il.ibm.com int bdrv_get_dirty(BlockDriverState *bs, int64_t sector) 22687cd1e32aSlirans@il.ibm.com { 22696ea44308SJan Kiszka int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK; 22707cd1e32aSlirans@il.ibm.com 2271c6d22830SJan Kiszka if (bs->dirty_bitmap && 2272c6d22830SJan Kiszka (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) { 2273c6d22830SJan Kiszka return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] & 2274c6d22830SJan Kiszka (1 << (chunk % (sizeof(unsigned long) * 8))); 22757cd1e32aSlirans@il.ibm.com } else { 22767cd1e32aSlirans@il.ibm.com return 0; 22777cd1e32aSlirans@il.ibm.com } 22787cd1e32aSlirans@il.ibm.com } 22797cd1e32aSlirans@il.ibm.com 22807cd1e32aSlirans@il.ibm.com void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector, 22817cd1e32aSlirans@il.ibm.com int nr_sectors) 22827cd1e32aSlirans@il.ibm.com { 22837cd1e32aSlirans@il.ibm.com set_dirty_bitmap(bs, cur_sector, nr_sectors, 0); 22847cd1e32aSlirans@il.ibm.com } 2285aaa0eb75SLiran Schour 2286aaa0eb75SLiran Schour int64_t bdrv_get_dirty_count(BlockDriverState *bs) 2287aaa0eb75SLiran Schour { 2288aaa0eb75SLiran Schour return bs->dirty_count; 2289aaa0eb75SLiran Schour } 2290