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" 25179a2c19Sblueswir1 #ifdef HOST_BSD 263990d09aSblueswir1 /* include native header before sys-queue.h */ 273990d09aSblueswir1 #include <sys/queue.h> 283990d09aSblueswir1 #endif 293990d09aSblueswir1 30faf07963Spbrook #include "qemu-common.h" 31376253ecSaliguori #include "monitor.h" 32ea2384d3Sbellard #include "block_int.h" 335efa9d5aSAnthony Liguori #include "module.h" 34fc01f7e7Sbellard 35179a2c19Sblueswir1 #ifdef HOST_BSD 367674e7bfSbellard #include <sys/types.h> 377674e7bfSbellard #include <sys/stat.h> 387674e7bfSbellard #include <sys/ioctl.h> 39c5e97233Sblueswir1 #ifndef __DragonFly__ 407674e7bfSbellard #include <sys/disk.h> 417674e7bfSbellard #endif 42c5e97233Sblueswir1 #endif 437674e7bfSbellard 4449dc768dSaliguori #ifdef _WIN32 4549dc768dSaliguori #include <windows.h> 4649dc768dSaliguori #endif 4749dc768dSaliguori 4883f64091Sbellard #define SECTOR_BITS 9 4983f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS) 503b0d4f61Sbellard 51f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 52f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 53c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 54f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 55f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 56ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 5783f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 5883f64091Sbellard uint8_t *buf, int nb_sectors); 5983f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 6083f64091Sbellard const uint8_t *buf, int nb_sectors); 61ec530c81Sbellard 627ee930d0Sblueswir1 BlockDriverState *bdrv_first; 637ee930d0Sblueswir1 64ea2384d3Sbellard static BlockDriver *first_drv; 65ea2384d3Sbellard 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 } 141ea2384d3Sbellard bdrv->next = first_drv; 142ea2384d3Sbellard first_drv = bdrv; 143ea2384d3Sbellard } 144b338082bSbellard 145b338082bSbellard /* create a new block device (by default it is empty) */ 146b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 147fc01f7e7Sbellard { 148b338082bSbellard BlockDriverState **pbs, *bs; 149b338082bSbellard 150b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 151b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 152ea2384d3Sbellard if (device_name[0] != '\0') { 153b338082bSbellard /* insert at the end */ 154b338082bSbellard pbs = &bdrv_first; 155b338082bSbellard while (*pbs != NULL) 156b338082bSbellard pbs = &(*pbs)->next; 157b338082bSbellard *pbs = bs; 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 1720e7e1989SKevin Wolf int bdrv_create(BlockDriver *drv, const char* filename, 1730e7e1989SKevin Wolf QEMUOptionParameter *options) 174ea2384d3Sbellard { 175ea2384d3Sbellard if (!drv->bdrv_create) 176ea2384d3Sbellard return -ENOTSUP; 1770e7e1989SKevin Wolf 1780e7e1989SKevin Wolf return drv->bdrv_create(filename, options); 179ea2384d3Sbellard } 180ea2384d3Sbellard 181d5249393Sbellard #ifdef _WIN32 18295389c86Sbellard void get_tmp_filename(char *filename, int size) 183d5249393Sbellard { 1843b9f94e1Sbellard char temp_dir[MAX_PATH]; 1853b9f94e1Sbellard 1863b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 1873b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 188d5249393Sbellard } 189d5249393Sbellard #else 19095389c86Sbellard void get_tmp_filename(char *filename, int size) 191ea2384d3Sbellard { 192ea2384d3Sbellard int fd; 1937ccfb2ebSblueswir1 const char *tmpdir; 194d5249393Sbellard /* XXX: race condition possible */ 1950badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 1960badc1eeSaurel32 if (!tmpdir) 1970badc1eeSaurel32 tmpdir = "/tmp"; 1980badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 199ea2384d3Sbellard fd = mkstemp(filename); 200ea2384d3Sbellard close(fd); 201ea2384d3Sbellard } 202d5249393Sbellard #endif 203ea2384d3Sbellard 20419cb3738Sbellard #ifdef _WIN32 205f45512feSbellard static int is_windows_drive_prefix(const char *filename) 206f45512feSbellard { 207f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 208f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 209f45512feSbellard filename[1] == ':'); 210f45512feSbellard } 211f45512feSbellard 21219cb3738Sbellard static int is_windows_drive(const char *filename) 21319cb3738Sbellard { 214f45512feSbellard if (is_windows_drive_prefix(filename) && 215f45512feSbellard filename[2] == '\0') 21619cb3738Sbellard return 1; 21719cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 21819cb3738Sbellard strstart(filename, "//./", NULL)) 21919cb3738Sbellard return 1; 22019cb3738Sbellard return 0; 22119cb3738Sbellard } 22219cb3738Sbellard #endif 22319cb3738Sbellard 22483f64091Sbellard static BlockDriver *find_protocol(const char *filename) 22583f64091Sbellard { 22683f64091Sbellard BlockDriver *drv1; 22783f64091Sbellard char protocol[128]; 22883f64091Sbellard int len; 22983f64091Sbellard const char *p; 23019cb3738Sbellard 23119cb3738Sbellard #ifdef _WIN32 232f45512feSbellard if (is_windows_drive(filename) || 233f45512feSbellard is_windows_drive_prefix(filename)) 2345efa9d5aSAnthony Liguori return bdrv_find_format("raw"); 23519cb3738Sbellard #endif 23683f64091Sbellard p = strchr(filename, ':'); 23783f64091Sbellard if (!p) 2385efa9d5aSAnthony Liguori return bdrv_find_format("raw"); 23983f64091Sbellard len = p - filename; 24083f64091Sbellard if (len > sizeof(protocol) - 1) 24183f64091Sbellard len = sizeof(protocol) - 1; 24283f64091Sbellard memcpy(protocol, filename, len); 24383f64091Sbellard protocol[len] = '\0'; 24483f64091Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 24583f64091Sbellard if (drv1->protocol_name && 24683f64091Sbellard !strcmp(drv1->protocol_name, protocol)) 24783f64091Sbellard return drv1; 24883f64091Sbellard } 24983f64091Sbellard return NULL; 25083f64091Sbellard } 25183f64091Sbellard 2527674e7bfSbellard /* XXX: force raw format if block or character device ? It would 2537674e7bfSbellard simplify the BSD case */ 254ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 255ea2384d3Sbellard { 25683f64091Sbellard int ret, score, score_max; 257ea2384d3Sbellard BlockDriver *drv1, *drv; 25883f64091Sbellard uint8_t buf[2048]; 25983f64091Sbellard BlockDriverState *bs; 260ea2384d3Sbellard 26119cb3738Sbellard /* detect host devices. By convention, /dev/cdrom[N] is always 26219cb3738Sbellard recognized as a host CDROM */ 26319cb3738Sbellard if (strstart(filename, "/dev/cdrom", NULL)) 2645efa9d5aSAnthony Liguori return bdrv_find_format("host_device"); 26519cb3738Sbellard #ifdef _WIN32 26619cb3738Sbellard if (is_windows_drive(filename)) 2675efa9d5aSAnthony Liguori return bdrv_find_format("host_device"); 26819cb3738Sbellard #else 26919cb3738Sbellard { 27019cb3738Sbellard struct stat st; 27119cb3738Sbellard if (stat(filename, &st) >= 0 && 27219cb3738Sbellard (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 2735efa9d5aSAnthony Liguori return bdrv_find_format("host_device"); 27419cb3738Sbellard } 27519cb3738Sbellard } 27619cb3738Sbellard #endif 27719cb3738Sbellard 27883f64091Sbellard drv = find_protocol(filename); 27919cb3738Sbellard /* no need to test disk image formats for vvfat */ 280c833ab73SAnthony Liguori if (drv && strcmp(drv->format_name, "vvfat") == 0) 28183f64091Sbellard return drv; 28283f64091Sbellard 28383f64091Sbellard ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); 28483f64091Sbellard if (ret < 0) 2857674e7bfSbellard return NULL; 28683f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 28783f64091Sbellard bdrv_delete(bs); 288ea2384d3Sbellard if (ret < 0) { 289ea2384d3Sbellard return NULL; 290ea2384d3Sbellard } 291ea2384d3Sbellard 292ea2384d3Sbellard score_max = 0; 293ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 29483f64091Sbellard if (drv1->bdrv_probe) { 295ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 296ea2384d3Sbellard if (score > score_max) { 297ea2384d3Sbellard score_max = score; 298ea2384d3Sbellard drv = drv1; 299ea2384d3Sbellard } 300ea2384d3Sbellard } 30183f64091Sbellard } 302ea2384d3Sbellard return drv; 303ea2384d3Sbellard } 304ea2384d3Sbellard 30583f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 306b338082bSbellard { 30783f64091Sbellard BlockDriverState *bs; 30883f64091Sbellard int ret; 3093b0d4f61Sbellard 31083f64091Sbellard bs = bdrv_new(""); 31183f64091Sbellard ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); 31283f64091Sbellard if (ret < 0) { 31383f64091Sbellard bdrv_delete(bs); 31483f64091Sbellard return ret; 3153b0d4f61Sbellard } 31671d0770cSaliguori bs->growable = 1; 31783f64091Sbellard *pbs = bs; 31883f64091Sbellard return 0; 3193b0d4f61Sbellard } 3203b0d4f61Sbellard 32183f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags) 32283f64091Sbellard { 32383f64091Sbellard return bdrv_open2(bs, filename, flags, NULL); 324ea2384d3Sbellard } 325ea2384d3Sbellard 32683f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 327ea2384d3Sbellard BlockDriver *drv) 328ea2384d3Sbellard { 32983f64091Sbellard int ret, open_flags; 330eb5c851fSths char tmp_filename[PATH_MAX]; 331eb5c851fSths char backing_filename[PATH_MAX]; 332fc01f7e7Sbellard 3330849bf08Sbellard bs->read_only = 0; 334ea2384d3Sbellard bs->is_temporary = 0; 335ea2384d3Sbellard bs->encrypted = 0; 336c0f4ce77Saliguori bs->valid_key = 0; 337e268ca52Saliguori /* buffer_alignment defaulted to 512, drivers can change this value */ 338e268ca52Saliguori bs->buffer_alignment = 512; 33933e3963eSbellard 34083f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 341ea2384d3Sbellard BlockDriverState *bs1; 342ea2384d3Sbellard int64_t total_size; 3437c96d46eSaliguori int is_protocol = 0; 34491a073a9SKevin Wolf BlockDriver *bdrv_qcow2; 34591a073a9SKevin Wolf QEMUOptionParameter *options; 34633e3963eSbellard 347ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 348ea2384d3Sbellard instead of opening 'filename' directly */ 349ea2384d3Sbellard 350ea2384d3Sbellard /* if there is a backing file, use it */ 351ea2384d3Sbellard bs1 = bdrv_new(""); 3525eb45639Saliguori ret = bdrv_open2(bs1, filename, 0, drv); 35351d7c00cSaliguori if (ret < 0) { 354ea2384d3Sbellard bdrv_delete(bs1); 35551d7c00cSaliguori return ret; 356ea2384d3Sbellard } 35783f64091Sbellard total_size = bdrv_getlength(bs1) >> SECTOR_BITS; 3587c96d46eSaliguori 3597c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 3607c96d46eSaliguori is_protocol = 1; 3617c96d46eSaliguori 362ea2384d3Sbellard bdrv_delete(bs1); 363ea2384d3Sbellard 364ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 3657c96d46eSaliguori 3667c96d46eSaliguori /* Real path is meaningless for protocols */ 3677c96d46eSaliguori if (is_protocol) 3687c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 3697c96d46eSaliguori "%s", filename); 3707c96d46eSaliguori else 371a817d936Sbellard realpath(filename, backing_filename); 3727c96d46eSaliguori 37391a073a9SKevin Wolf bdrv_qcow2 = bdrv_find_format("qcow2"); 37491a073a9SKevin Wolf options = parse_option_parameters("", bdrv_qcow2->create_options, NULL); 37591a073a9SKevin Wolf 37691a073a9SKevin Wolf set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512); 37791a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename); 37891a073a9SKevin Wolf if (drv) { 37991a073a9SKevin Wolf set_option_parameter(options, BLOCK_OPT_BACKING_FMT, 38091a073a9SKevin Wolf drv->format_name); 38191a073a9SKevin Wolf } 38291a073a9SKevin Wolf 38391a073a9SKevin Wolf ret = bdrv_create(bdrv_qcow2, tmp_filename, options); 38451d7c00cSaliguori if (ret < 0) { 38551d7c00cSaliguori return ret; 386ea2384d3Sbellard } 38791a073a9SKevin Wolf 388ea2384d3Sbellard filename = tmp_filename; 38991a073a9SKevin Wolf drv = bdrv_qcow2; 390ea2384d3Sbellard bs->is_temporary = 1; 391ea2384d3Sbellard } 392ea2384d3Sbellard 393ea2384d3Sbellard pstrcpy(bs->filename, sizeof(bs->filename), filename); 39483f64091Sbellard if (flags & BDRV_O_FILE) { 39583f64091Sbellard drv = find_protocol(filename); 39651d7c00cSaliguori } else if (!drv) { 397ea2384d3Sbellard drv = find_image_format(filename); 398ea2384d3Sbellard } 39951d7c00cSaliguori if (!drv) { 40051d7c00cSaliguori ret = -ENOENT; 40151d7c00cSaliguori goto unlink_and_fail; 40283f64091Sbellard } 403ea2384d3Sbellard bs->drv = drv; 404ea2384d3Sbellard bs->opaque = qemu_mallocz(drv->instance_size); 40583f64091Sbellard /* Note: for compatibility, we open disk image files as RDWR, and 40683f64091Sbellard RDONLY as fallback */ 40783f64091Sbellard if (!(flags & BDRV_O_FILE)) 4089f7965c7Saliguori open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); 40983f64091Sbellard else 41083f64091Sbellard open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); 41183f64091Sbellard ret = drv->bdrv_open(bs, filename, open_flags); 412a0a83536Saurel32 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { 4139f7965c7Saliguori ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); 41483f64091Sbellard bs->read_only = 1; 41583f64091Sbellard } 416ea2384d3Sbellard if (ret < 0) { 417ea2384d3Sbellard qemu_free(bs->opaque); 4186b21b973Sbellard bs->opaque = NULL; 4196b21b973Sbellard bs->drv = NULL; 42051d7c00cSaliguori unlink_and_fail: 42151d7c00cSaliguori if (bs->is_temporary) 42251d7c00cSaliguori unlink(filename); 42383f64091Sbellard return ret; 424ea2384d3Sbellard } 425d15a771dSbellard if (drv->bdrv_getlength) { 426d15a771dSbellard bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 427d15a771dSbellard } 428ea2384d3Sbellard #ifndef _WIN32 429ea2384d3Sbellard if (bs->is_temporary) { 430ea2384d3Sbellard unlink(filename); 43133e3963eSbellard } 43267b915a5Sbellard #endif 43383f64091Sbellard if (bs->backing_file[0] != '\0') { 434ea2384d3Sbellard /* if there is a backing file, use it */ 4355eb45639Saliguori BlockDriver *back_drv = NULL; 436ea2384d3Sbellard bs->backing_hd = bdrv_new(""); 43783f64091Sbellard path_combine(backing_filename, sizeof(backing_filename), 43883f64091Sbellard filename, bs->backing_file); 4395eb45639Saliguori if (bs->backing_format[0] != '\0') 4405eb45639Saliguori back_drv = bdrv_find_format(bs->backing_format); 4415eb45639Saliguori ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags, 4425eb45639Saliguori back_drv); 44351d7c00cSaliguori if (ret < 0) { 44451d7c00cSaliguori bdrv_close(bs); 44551d7c00cSaliguori return ret; 44651d7c00cSaliguori } 447ea2384d3Sbellard } 44833e3963eSbellard 449bb5fc20fSaliguori if (!bdrv_key_required(bs)) { 450b338082bSbellard /* call the change callback */ 45119cb3738Sbellard bs->media_changed = 1; 452b338082bSbellard if (bs->change_cb) 453b338082bSbellard bs->change_cb(bs->change_opaque); 454bb5fc20fSaliguori } 455b338082bSbellard return 0; 456fc01f7e7Sbellard } 457fc01f7e7Sbellard 458fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 459fc01f7e7Sbellard { 46019cb3738Sbellard if (bs->drv) { 461ea2384d3Sbellard if (bs->backing_hd) 462ea2384d3Sbellard bdrv_delete(bs->backing_hd); 463ea2384d3Sbellard bs->drv->bdrv_close(bs); 464ea2384d3Sbellard qemu_free(bs->opaque); 465ea2384d3Sbellard #ifdef _WIN32 466ea2384d3Sbellard if (bs->is_temporary) { 467ea2384d3Sbellard unlink(bs->filename); 468ea2384d3Sbellard } 46967b915a5Sbellard #endif 470ea2384d3Sbellard bs->opaque = NULL; 471ea2384d3Sbellard bs->drv = NULL; 472b338082bSbellard 473b338082bSbellard /* call the change callback */ 47419cb3738Sbellard bs->media_changed = 1; 475b338082bSbellard if (bs->change_cb) 476b338082bSbellard bs->change_cb(bs->change_opaque); 477b338082bSbellard } 478b338082bSbellard } 479b338082bSbellard 480b338082bSbellard void bdrv_delete(BlockDriverState *bs) 481b338082bSbellard { 48234c6f050Saurel32 BlockDriverState **pbs; 48334c6f050Saurel32 48434c6f050Saurel32 pbs = &bdrv_first; 48534c6f050Saurel32 while (*pbs != bs && *pbs != NULL) 48634c6f050Saurel32 pbs = &(*pbs)->next; 48734c6f050Saurel32 if (*pbs == bs) 48834c6f050Saurel32 *pbs = bs->next; 48934c6f050Saurel32 490b338082bSbellard bdrv_close(bs); 491b338082bSbellard qemu_free(bs); 492fc01f7e7Sbellard } 493fc01f7e7Sbellard 494e97fc193Saliguori /* 495e97fc193Saliguori * Run consistency checks on an image 496e97fc193Saliguori * 497e97fc193Saliguori * Returns the number of errors or -errno when an internal error occurs 498e97fc193Saliguori */ 499e97fc193Saliguori int bdrv_check(BlockDriverState *bs) 500e97fc193Saliguori { 501e97fc193Saliguori if (bs->drv->bdrv_check == NULL) { 502e97fc193Saliguori return -ENOTSUP; 503e97fc193Saliguori } 504e97fc193Saliguori 505e97fc193Saliguori return bs->drv->bdrv_check(bs); 506e97fc193Saliguori } 507e97fc193Saliguori 50833e3963eSbellard /* commit COW file into the raw image */ 50933e3963eSbellard int bdrv_commit(BlockDriverState *bs) 51033e3963eSbellard { 51119cb3738Sbellard BlockDriver *drv = bs->drv; 51283f64091Sbellard int64_t i, total_sectors; 513ea2384d3Sbellard int n, j; 514ea2384d3Sbellard unsigned char sector[512]; 51533e3963eSbellard 51619cb3738Sbellard if (!drv) 51719cb3738Sbellard return -ENOMEDIUM; 51833e3963eSbellard 51933e3963eSbellard if (bs->read_only) { 520ea2384d3Sbellard return -EACCES; 52133e3963eSbellard } 52233e3963eSbellard 523ea2384d3Sbellard if (!bs->backing_hd) { 524ea2384d3Sbellard return -ENOTSUP; 525ea2384d3Sbellard } 526ea2384d3Sbellard 52783f64091Sbellard total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 52883f64091Sbellard for (i = 0; i < total_sectors;) { 52919cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 530ea2384d3Sbellard for(j = 0; j < n; j++) { 53133e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 532ea2384d3Sbellard return -EIO; 53333e3963eSbellard } 53433e3963eSbellard 535ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 536ea2384d3Sbellard return -EIO; 53733e3963eSbellard } 538ea2384d3Sbellard i++; 539ea2384d3Sbellard } 540ea2384d3Sbellard } else { 541ea2384d3Sbellard i += n; 54233e3963eSbellard } 54333e3963eSbellard } 54495389c86Sbellard 54519cb3738Sbellard if (drv->bdrv_make_empty) 54619cb3738Sbellard return drv->bdrv_make_empty(bs); 54795389c86Sbellard 54833e3963eSbellard return 0; 54933e3963eSbellard } 55033e3963eSbellard 55171d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 55271d0770cSaliguori size_t size) 55371d0770cSaliguori { 55471d0770cSaliguori int64_t len; 55571d0770cSaliguori 55671d0770cSaliguori if (!bdrv_is_inserted(bs)) 55771d0770cSaliguori return -ENOMEDIUM; 55871d0770cSaliguori 55971d0770cSaliguori if (bs->growable) 56071d0770cSaliguori return 0; 56171d0770cSaliguori 56271d0770cSaliguori len = bdrv_getlength(bs); 56371d0770cSaliguori 564fbb7b4e0SKevin Wolf if (offset < 0) 565fbb7b4e0SKevin Wolf return -EIO; 566fbb7b4e0SKevin Wolf 567fbb7b4e0SKevin Wolf if ((offset > len) || (len - offset < size)) 56871d0770cSaliguori return -EIO; 56971d0770cSaliguori 57071d0770cSaliguori return 0; 57171d0770cSaliguori } 57271d0770cSaliguori 57371d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 57471d0770cSaliguori int nb_sectors) 57571d0770cSaliguori { 576999dec57Saliguori return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); 57771d0770cSaliguori } 57871d0770cSaliguori 57919cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 580fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 581fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 582fc01f7e7Sbellard { 583ea2384d3Sbellard BlockDriver *drv = bs->drv; 584fc01f7e7Sbellard 58519cb3738Sbellard if (!drv) 58619cb3738Sbellard return -ENOMEDIUM; 58771d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 58871d0770cSaliguori return -EIO; 589b338082bSbellard 59083f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 59183f64091Sbellard } 592fc01f7e7Sbellard 59319cb3738Sbellard /* Return < 0 if error. Important errors are: 59419cb3738Sbellard -EIO generic I/O error (may happen for all errors) 59519cb3738Sbellard -ENOMEDIUM No media inserted. 59619cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 59719cb3738Sbellard -EACCES Trying to write a read-only device 59819cb3738Sbellard */ 599fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 600fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 601fc01f7e7Sbellard { 60283f64091Sbellard BlockDriver *drv = bs->drv; 60319cb3738Sbellard if (!bs->drv) 60419cb3738Sbellard return -ENOMEDIUM; 6050849bf08Sbellard if (bs->read_only) 60619cb3738Sbellard return -EACCES; 60771d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 60871d0770cSaliguori return -EIO; 60971d0770cSaliguori 61083f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 61183f64091Sbellard } 61283f64091Sbellard 613eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 614eda578e5Saliguori void *buf, int count1) 61583f64091Sbellard { 61683f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 61783f64091Sbellard int len, nb_sectors, count; 61883f64091Sbellard int64_t sector_num; 61983f64091Sbellard 62083f64091Sbellard count = count1; 62183f64091Sbellard /* first read to align to sector start */ 62283f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 62383f64091Sbellard if (len > count) 62483f64091Sbellard len = count; 62583f64091Sbellard sector_num = offset >> SECTOR_BITS; 62683f64091Sbellard if (len > 0) { 62783f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 62883f64091Sbellard return -EIO; 62983f64091Sbellard memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); 63083f64091Sbellard count -= len; 63183f64091Sbellard if (count == 0) 63283f64091Sbellard return count1; 63383f64091Sbellard sector_num++; 63483f64091Sbellard buf += len; 63583f64091Sbellard } 63683f64091Sbellard 63783f64091Sbellard /* read the sectors "in place" */ 63883f64091Sbellard nb_sectors = count >> SECTOR_BITS; 63983f64091Sbellard if (nb_sectors > 0) { 64083f64091Sbellard if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) 64183f64091Sbellard return -EIO; 64283f64091Sbellard sector_num += nb_sectors; 64383f64091Sbellard len = nb_sectors << SECTOR_BITS; 64483f64091Sbellard buf += len; 64583f64091Sbellard count -= len; 64683f64091Sbellard } 64783f64091Sbellard 64883f64091Sbellard /* add data from the last sector */ 64983f64091Sbellard if (count > 0) { 65083f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 65183f64091Sbellard return -EIO; 65283f64091Sbellard memcpy(buf, tmp_buf, count); 65383f64091Sbellard } 65483f64091Sbellard return count1; 65583f64091Sbellard } 65683f64091Sbellard 657eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 658eda578e5Saliguori const void *buf, int count1) 65983f64091Sbellard { 66083f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 66183f64091Sbellard int len, nb_sectors, count; 66283f64091Sbellard int64_t sector_num; 66383f64091Sbellard 66483f64091Sbellard count = count1; 66583f64091Sbellard /* first write to align to sector start */ 66683f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 66783f64091Sbellard if (len > count) 66883f64091Sbellard len = count; 66983f64091Sbellard sector_num = offset >> SECTOR_BITS; 67083f64091Sbellard if (len > 0) { 67183f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 67283f64091Sbellard return -EIO; 67383f64091Sbellard memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); 67483f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 67583f64091Sbellard return -EIO; 67683f64091Sbellard count -= len; 67783f64091Sbellard if (count == 0) 67883f64091Sbellard return count1; 67983f64091Sbellard sector_num++; 68083f64091Sbellard buf += len; 68183f64091Sbellard } 68283f64091Sbellard 68383f64091Sbellard /* write the sectors "in place" */ 68483f64091Sbellard nb_sectors = count >> SECTOR_BITS; 68583f64091Sbellard if (nb_sectors > 0) { 68683f64091Sbellard if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) 68783f64091Sbellard return -EIO; 68883f64091Sbellard sector_num += nb_sectors; 68983f64091Sbellard len = nb_sectors << SECTOR_BITS; 69083f64091Sbellard buf += len; 69183f64091Sbellard count -= len; 69283f64091Sbellard } 69383f64091Sbellard 69483f64091Sbellard /* add data from the last sector */ 69583f64091Sbellard if (count > 0) { 69683f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 69783f64091Sbellard return -EIO; 69883f64091Sbellard memcpy(tmp_buf, buf, count); 69983f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 70083f64091Sbellard return -EIO; 70183f64091Sbellard } 70283f64091Sbellard return count1; 70383f64091Sbellard } 70483f64091Sbellard 70583f64091Sbellard /** 70683f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 70783f64091Sbellard */ 70883f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 70983f64091Sbellard { 71083f64091Sbellard BlockDriver *drv = bs->drv; 71183f64091Sbellard if (!drv) 71219cb3738Sbellard return -ENOMEDIUM; 71383f64091Sbellard if (!drv->bdrv_truncate) 71483f64091Sbellard return -ENOTSUP; 71583f64091Sbellard return drv->bdrv_truncate(bs, offset); 71683f64091Sbellard } 71783f64091Sbellard 71883f64091Sbellard /** 71983f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 72083f64091Sbellard */ 72183f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 72283f64091Sbellard { 72383f64091Sbellard BlockDriver *drv = bs->drv; 72483f64091Sbellard if (!drv) 72519cb3738Sbellard return -ENOMEDIUM; 72683f64091Sbellard if (!drv->bdrv_getlength) { 72783f64091Sbellard /* legacy mode */ 72883f64091Sbellard return bs->total_sectors * SECTOR_SIZE; 72983f64091Sbellard } 73083f64091Sbellard return drv->bdrv_getlength(bs); 731fc01f7e7Sbellard } 732fc01f7e7Sbellard 73319cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 73496b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 735fc01f7e7Sbellard { 73619cb3738Sbellard int64_t length; 73719cb3738Sbellard length = bdrv_getlength(bs); 73819cb3738Sbellard if (length < 0) 73919cb3738Sbellard length = 0; 74019cb3738Sbellard else 74119cb3738Sbellard length = length >> SECTOR_BITS; 74219cb3738Sbellard *nb_sectors_ptr = length; 743fc01f7e7Sbellard } 744cf98951bSbellard 745f3d54fc4Saliguori struct partition { 746f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 747f3d54fc4Saliguori uint8_t head; /* starting head */ 748f3d54fc4Saliguori uint8_t sector; /* starting sector */ 749f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 750f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 751f3d54fc4Saliguori uint8_t end_head; /* end head */ 752f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 753f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 754f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 755f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 756f3d54fc4Saliguori } __attribute__((packed)); 757f3d54fc4Saliguori 758f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 759f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 760f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 761f3d54fc4Saliguori { 762f3d54fc4Saliguori uint8_t buf[512]; 763f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 764f3d54fc4Saliguori struct partition *p; 765f3d54fc4Saliguori uint32_t nr_sects; 766a38131b6Sblueswir1 uint64_t nb_sectors; 767f3d54fc4Saliguori 768f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 769f3d54fc4Saliguori 770f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 771f3d54fc4Saliguori if (ret < 0) 772f3d54fc4Saliguori return -1; 773f3d54fc4Saliguori /* test msdos magic */ 774f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 775f3d54fc4Saliguori return -1; 776f3d54fc4Saliguori for(i = 0; i < 4; i++) { 777f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 778f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 779f3d54fc4Saliguori if (nr_sects && p->end_head) { 780f3d54fc4Saliguori /* We make the assumption that the partition terminates on 781f3d54fc4Saliguori a cylinder boundary */ 782f3d54fc4Saliguori heads = p->end_head + 1; 783f3d54fc4Saliguori sectors = p->end_sector & 63; 784f3d54fc4Saliguori if (sectors == 0) 785f3d54fc4Saliguori continue; 786f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 787f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 788f3d54fc4Saliguori continue; 789f3d54fc4Saliguori *pheads = heads; 790f3d54fc4Saliguori *psectors = sectors; 791f3d54fc4Saliguori *pcylinders = cylinders; 792f3d54fc4Saliguori #if 0 793f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 794f3d54fc4Saliguori cylinders, heads, sectors); 795f3d54fc4Saliguori #endif 796f3d54fc4Saliguori return 0; 797f3d54fc4Saliguori } 798f3d54fc4Saliguori } 799f3d54fc4Saliguori return -1; 800f3d54fc4Saliguori } 801f3d54fc4Saliguori 802f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 803f3d54fc4Saliguori { 804f3d54fc4Saliguori int translation, lba_detected = 0; 805f3d54fc4Saliguori int cylinders, heads, secs; 806a38131b6Sblueswir1 uint64_t nb_sectors; 807f3d54fc4Saliguori 808f3d54fc4Saliguori /* if a geometry hint is available, use it */ 809f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 810f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 811f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 812f3d54fc4Saliguori if (cylinders != 0) { 813f3d54fc4Saliguori *pcyls = cylinders; 814f3d54fc4Saliguori *pheads = heads; 815f3d54fc4Saliguori *psecs = secs; 816f3d54fc4Saliguori } else { 817f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 818f3d54fc4Saliguori if (heads > 16) { 819f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 820f3d54fc4Saliguori translation was active, so the default 821f3d54fc4Saliguori hardware geometry is OK */ 822f3d54fc4Saliguori lba_detected = 1; 823f3d54fc4Saliguori goto default_geometry; 824f3d54fc4Saliguori } else { 825f3d54fc4Saliguori *pcyls = cylinders; 826f3d54fc4Saliguori *pheads = heads; 827f3d54fc4Saliguori *psecs = secs; 828f3d54fc4Saliguori /* disable any translation to be in sync with 829f3d54fc4Saliguori the logical geometry */ 830f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 831f3d54fc4Saliguori bdrv_set_translation_hint(bs, 832f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 833f3d54fc4Saliguori } 834f3d54fc4Saliguori } 835f3d54fc4Saliguori } else { 836f3d54fc4Saliguori default_geometry: 837f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 838f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 839f3d54fc4Saliguori 840f3d54fc4Saliguori if (cylinders > 16383) 841f3d54fc4Saliguori cylinders = 16383; 842f3d54fc4Saliguori else if (cylinders < 2) 843f3d54fc4Saliguori cylinders = 2; 844f3d54fc4Saliguori *pcyls = cylinders; 845f3d54fc4Saliguori *pheads = 16; 846f3d54fc4Saliguori *psecs = 63; 847f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 848f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 849f3d54fc4Saliguori bdrv_set_translation_hint(bs, 850f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 851f3d54fc4Saliguori } else { 852f3d54fc4Saliguori bdrv_set_translation_hint(bs, 853f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 854f3d54fc4Saliguori } 855f3d54fc4Saliguori } 856f3d54fc4Saliguori } 857f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 858f3d54fc4Saliguori } 859f3d54fc4Saliguori } 860f3d54fc4Saliguori 861b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 862b338082bSbellard int cyls, int heads, int secs) 863b338082bSbellard { 864b338082bSbellard bs->cyls = cyls; 865b338082bSbellard bs->heads = heads; 866b338082bSbellard bs->secs = secs; 867b338082bSbellard } 868b338082bSbellard 869b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 870b338082bSbellard { 871b338082bSbellard bs->type = type; 872b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 873b338082bSbellard type == BDRV_TYPE_FLOPPY)); 874b338082bSbellard } 875b338082bSbellard 87646d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 87746d4767dSbellard { 87846d4767dSbellard bs->translation = translation; 87946d4767dSbellard } 88046d4767dSbellard 881b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 882b338082bSbellard int *pcyls, int *pheads, int *psecs) 883b338082bSbellard { 884b338082bSbellard *pcyls = bs->cyls; 885b338082bSbellard *pheads = bs->heads; 886b338082bSbellard *psecs = bs->secs; 887b338082bSbellard } 888b338082bSbellard 889b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 890b338082bSbellard { 891b338082bSbellard return bs->type; 892b338082bSbellard } 893b338082bSbellard 89446d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 89546d4767dSbellard { 89646d4767dSbellard return bs->translation; 89746d4767dSbellard } 89846d4767dSbellard 899b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 900b338082bSbellard { 901b338082bSbellard return bs->removable; 902b338082bSbellard } 903b338082bSbellard 904b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 905b338082bSbellard { 906b338082bSbellard return bs->read_only; 907b338082bSbellard } 908b338082bSbellard 909985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 910985a03b0Sths { 911985a03b0Sths return bs->sg; 912985a03b0Sths } 913985a03b0Sths 91419cb3738Sbellard /* XXX: no longer used */ 915b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 916b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 917b338082bSbellard { 918b338082bSbellard bs->change_cb = change_cb; 919b338082bSbellard bs->change_opaque = opaque; 920b338082bSbellard } 921b338082bSbellard 922ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 923ea2384d3Sbellard { 924ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 925ea2384d3Sbellard return 1; 926ea2384d3Sbellard return bs->encrypted; 927ea2384d3Sbellard } 928ea2384d3Sbellard 929c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 930c0f4ce77Saliguori { 931c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 932c0f4ce77Saliguori 933c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 934c0f4ce77Saliguori return 1; 935c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 936c0f4ce77Saliguori } 937c0f4ce77Saliguori 938ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 939ea2384d3Sbellard { 940ea2384d3Sbellard int ret; 941ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 942ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 943ea2384d3Sbellard if (ret < 0) 944ea2384d3Sbellard return ret; 945ea2384d3Sbellard if (!bs->encrypted) 946ea2384d3Sbellard return 0; 947ea2384d3Sbellard } 948ea2384d3Sbellard if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) 949ea2384d3Sbellard return -1; 950c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 951bb5fc20fSaliguori if (ret < 0) { 952bb5fc20fSaliguori bs->valid_key = 0; 953bb5fc20fSaliguori } else if (!bs->valid_key) { 954bb5fc20fSaliguori bs->valid_key = 1; 955bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 956bb5fc20fSaliguori bs->media_changed = 1; 957bb5fc20fSaliguori if (bs->change_cb) 958bb5fc20fSaliguori bs->change_cb(bs->change_opaque); 959bb5fc20fSaliguori } 960c0f4ce77Saliguori return ret; 961ea2384d3Sbellard } 962ea2384d3Sbellard 963ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 964ea2384d3Sbellard { 96519cb3738Sbellard if (!bs->drv) { 966ea2384d3Sbellard buf[0] = '\0'; 967ea2384d3Sbellard } else { 968ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 969ea2384d3Sbellard } 970ea2384d3Sbellard } 971ea2384d3Sbellard 972ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 973ea2384d3Sbellard void *opaque) 974ea2384d3Sbellard { 975ea2384d3Sbellard BlockDriver *drv; 976ea2384d3Sbellard 977ea2384d3Sbellard for (drv = first_drv; drv != NULL; drv = drv->next) { 978ea2384d3Sbellard it(opaque, drv->format_name); 979ea2384d3Sbellard } 980ea2384d3Sbellard } 981ea2384d3Sbellard 982b338082bSbellard BlockDriverState *bdrv_find(const char *name) 983b338082bSbellard { 984b338082bSbellard BlockDriverState *bs; 985b338082bSbellard 986b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 987b338082bSbellard if (!strcmp(name, bs->device_name)) 988b338082bSbellard return bs; 989b338082bSbellard } 990b338082bSbellard return NULL; 991b338082bSbellard } 992b338082bSbellard 99351de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 99481d0912dSbellard { 99581d0912dSbellard BlockDriverState *bs; 99681d0912dSbellard 99781d0912dSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 99851de9760Saliguori it(opaque, bs); 99981d0912dSbellard } 100081d0912dSbellard } 100181d0912dSbellard 1002ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1003ea2384d3Sbellard { 1004ea2384d3Sbellard return bs->device_name; 1005ea2384d3Sbellard } 1006ea2384d3Sbellard 10077a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 10087a6cba61Spbrook { 1009081501daSaliguori if (!bs->drv) 1010081501daSaliguori return; 10117a6cba61Spbrook if (bs->drv->bdrv_flush) 10127a6cba61Spbrook bs->drv->bdrv_flush(bs); 10137a6cba61Spbrook if (bs->backing_hd) 10147a6cba61Spbrook bdrv_flush(bs->backing_hd); 10157a6cba61Spbrook } 10167a6cba61Spbrook 1017c6ca28d6Saliguori void bdrv_flush_all(void) 1018c6ca28d6Saliguori { 1019c6ca28d6Saliguori BlockDriverState *bs; 1020c6ca28d6Saliguori 1021c6ca28d6Saliguori for (bs = bdrv_first; bs != NULL; bs = bs->next) 1022c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 1023c6ca28d6Saliguori (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) 1024c6ca28d6Saliguori bdrv_flush(bs); 1025c6ca28d6Saliguori } 1026c6ca28d6Saliguori 1027f58c7b35Sths /* 1028f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1029f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1030f58c7b35Sths * hence all their sectors are reported as allocated. 1031f58c7b35Sths * 1032f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1033f58c7b35Sths * the specified sector) that are known to be in the same 1034f58c7b35Sths * allocated/unallocated state. 1035f58c7b35Sths * 1036f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1037f58c7b35Sths */ 1038f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1039f58c7b35Sths int *pnum) 1040f58c7b35Sths { 1041f58c7b35Sths int64_t n; 1042f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1043f58c7b35Sths if (sector_num >= bs->total_sectors) { 1044f58c7b35Sths *pnum = 0; 1045f58c7b35Sths return 0; 1046f58c7b35Sths } 1047f58c7b35Sths n = bs->total_sectors - sector_num; 1048f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1049f58c7b35Sths return 1; 1050f58c7b35Sths } 1051f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1052f58c7b35Sths } 1053f58c7b35Sths 1054376253ecSaliguori void bdrv_info(Monitor *mon) 1055b338082bSbellard { 1056b338082bSbellard BlockDriverState *bs; 1057b338082bSbellard 1058b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1059376253ecSaliguori monitor_printf(mon, "%s:", bs->device_name); 1060376253ecSaliguori monitor_printf(mon, " type="); 1061b338082bSbellard switch(bs->type) { 1062b338082bSbellard case BDRV_TYPE_HD: 1063376253ecSaliguori monitor_printf(mon, "hd"); 1064b338082bSbellard break; 1065b338082bSbellard case BDRV_TYPE_CDROM: 1066376253ecSaliguori monitor_printf(mon, "cdrom"); 1067b338082bSbellard break; 1068b338082bSbellard case BDRV_TYPE_FLOPPY: 1069376253ecSaliguori monitor_printf(mon, "floppy"); 1070b338082bSbellard break; 1071b338082bSbellard } 1072376253ecSaliguori monitor_printf(mon, " removable=%d", bs->removable); 1073b338082bSbellard if (bs->removable) { 1074376253ecSaliguori monitor_printf(mon, " locked=%d", bs->locked); 1075b338082bSbellard } 107619cb3738Sbellard if (bs->drv) { 1077376253ecSaliguori monitor_printf(mon, " file="); 1078376253ecSaliguori monitor_print_filename(mon, bs->filename); 1079fef30743Sths if (bs->backing_file[0] != '\0') { 1080376253ecSaliguori monitor_printf(mon, " backing_file="); 1081376253ecSaliguori monitor_print_filename(mon, bs->backing_file); 1082fef30743Sths } 1083376253ecSaliguori monitor_printf(mon, " ro=%d", bs->read_only); 1084376253ecSaliguori monitor_printf(mon, " drv=%s", bs->drv->format_name); 1085376253ecSaliguori monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs)); 1086b338082bSbellard } else { 1087376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1088b338082bSbellard } 1089376253ecSaliguori monitor_printf(mon, "\n"); 1090b338082bSbellard } 1091b338082bSbellard } 1092a36e69ddSths 1093a36e69ddSths /* The "info blockstats" command. */ 1094376253ecSaliguori void bdrv_info_stats(Monitor *mon) 1095a36e69ddSths { 1096a36e69ddSths BlockDriverState *bs; 1097a36e69ddSths 1098a36e69ddSths for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1099376253ecSaliguori monitor_printf(mon, "%s:" 1100a36e69ddSths " rd_bytes=%" PRIu64 1101a36e69ddSths " wr_bytes=%" PRIu64 1102a36e69ddSths " rd_operations=%" PRIu64 1103a36e69ddSths " wr_operations=%" PRIu64 1104ebf53fcdSaliguori "\n", 1105a36e69ddSths bs->device_name, 1106a36e69ddSths bs->rd_bytes, bs->wr_bytes, 1107a36e69ddSths bs->rd_ops, bs->wr_ops); 1108a36e69ddSths } 1109a36e69ddSths } 1110ea2384d3Sbellard 1111045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1112045df330Saliguori { 1113045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1114045df330Saliguori return bs->backing_file; 1115045df330Saliguori else if (bs->encrypted) 1116045df330Saliguori return bs->filename; 1117045df330Saliguori else 1118045df330Saliguori return NULL; 1119045df330Saliguori } 1120045df330Saliguori 112183f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 112283f64091Sbellard char *filename, int filename_size) 112383f64091Sbellard { 112483f64091Sbellard if (!bs->backing_hd) { 112583f64091Sbellard pstrcpy(filename, filename_size, ""); 112683f64091Sbellard } else { 112783f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 112883f64091Sbellard } 112983f64091Sbellard } 113083f64091Sbellard 1131faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1132faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1133faea38e7Sbellard { 1134faea38e7Sbellard BlockDriver *drv = bs->drv; 1135faea38e7Sbellard if (!drv) 113619cb3738Sbellard return -ENOMEDIUM; 1137faea38e7Sbellard if (!drv->bdrv_write_compressed) 1138faea38e7Sbellard return -ENOTSUP; 1139fbb7b4e0SKevin Wolf if (bdrv_check_request(bs, sector_num, nb_sectors)) 1140fbb7b4e0SKevin Wolf return -EIO; 1141faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1142faea38e7Sbellard } 1143faea38e7Sbellard 1144faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1145faea38e7Sbellard { 1146faea38e7Sbellard BlockDriver *drv = bs->drv; 1147faea38e7Sbellard if (!drv) 114819cb3738Sbellard return -ENOMEDIUM; 1149faea38e7Sbellard if (!drv->bdrv_get_info) 1150faea38e7Sbellard return -ENOTSUP; 1151faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1152faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1153faea38e7Sbellard } 1154faea38e7Sbellard 1155178e08a5Saliguori int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size) 1156178e08a5Saliguori { 1157178e08a5Saliguori BlockDriver *drv = bs->drv; 1158178e08a5Saliguori if (!drv) 1159178e08a5Saliguori return -ENOMEDIUM; 1160178e08a5Saliguori if (!drv->bdrv_put_buffer) 1161178e08a5Saliguori return -ENOTSUP; 1162178e08a5Saliguori return drv->bdrv_put_buffer(bs, buf, pos, size); 1163178e08a5Saliguori } 1164178e08a5Saliguori 1165178e08a5Saliguori int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size) 1166178e08a5Saliguori { 1167178e08a5Saliguori BlockDriver *drv = bs->drv; 1168178e08a5Saliguori if (!drv) 1169178e08a5Saliguori return -ENOMEDIUM; 1170178e08a5Saliguori if (!drv->bdrv_get_buffer) 1171178e08a5Saliguori return -ENOTSUP; 1172178e08a5Saliguori return drv->bdrv_get_buffer(bs, buf, pos, size); 1173178e08a5Saliguori } 1174178e08a5Saliguori 1175faea38e7Sbellard /**************************************************************/ 1176faea38e7Sbellard /* handling of snapshots */ 1177faea38e7Sbellard 1178faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1179faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1180faea38e7Sbellard { 1181faea38e7Sbellard BlockDriver *drv = bs->drv; 1182faea38e7Sbellard if (!drv) 118319cb3738Sbellard return -ENOMEDIUM; 1184faea38e7Sbellard if (!drv->bdrv_snapshot_create) 1185faea38e7Sbellard return -ENOTSUP; 1186faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 1187faea38e7Sbellard } 1188faea38e7Sbellard 1189faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1190faea38e7Sbellard const char *snapshot_id) 1191faea38e7Sbellard { 1192faea38e7Sbellard BlockDriver *drv = bs->drv; 1193faea38e7Sbellard if (!drv) 119419cb3738Sbellard return -ENOMEDIUM; 1195faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 1196faea38e7Sbellard return -ENOTSUP; 1197faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 1198faea38e7Sbellard } 1199faea38e7Sbellard 1200faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1201faea38e7Sbellard { 1202faea38e7Sbellard BlockDriver *drv = bs->drv; 1203faea38e7Sbellard if (!drv) 120419cb3738Sbellard return -ENOMEDIUM; 1205faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 1206faea38e7Sbellard return -ENOTSUP; 1207faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 1208faea38e7Sbellard } 1209faea38e7Sbellard 1210faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1211faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1212faea38e7Sbellard { 1213faea38e7Sbellard BlockDriver *drv = bs->drv; 1214faea38e7Sbellard if (!drv) 121519cb3738Sbellard return -ENOMEDIUM; 1216faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1217faea38e7Sbellard return -ENOTSUP; 1218faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1219faea38e7Sbellard } 1220faea38e7Sbellard 1221faea38e7Sbellard #define NB_SUFFIXES 4 1222faea38e7Sbellard 1223faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1224faea38e7Sbellard { 1225faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1226faea38e7Sbellard int64_t base; 1227faea38e7Sbellard int i; 1228faea38e7Sbellard 1229faea38e7Sbellard if (size <= 999) { 1230faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1231faea38e7Sbellard } else { 1232faea38e7Sbellard base = 1024; 1233faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1234faea38e7Sbellard if (size < (10 * base)) { 1235faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1236faea38e7Sbellard (double)size / base, 1237faea38e7Sbellard suffixes[i]); 1238faea38e7Sbellard break; 1239faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1240faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1241faea38e7Sbellard ((size + (base >> 1)) / base), 1242faea38e7Sbellard suffixes[i]); 1243faea38e7Sbellard break; 1244faea38e7Sbellard } 1245faea38e7Sbellard base = base * 1024; 1246faea38e7Sbellard } 1247faea38e7Sbellard } 1248faea38e7Sbellard return buf; 1249faea38e7Sbellard } 1250faea38e7Sbellard 1251faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1252faea38e7Sbellard { 1253faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 12543b9f94e1Sbellard #ifdef _WIN32 12553b9f94e1Sbellard struct tm *ptm; 12563b9f94e1Sbellard #else 1257faea38e7Sbellard struct tm tm; 12583b9f94e1Sbellard #endif 1259faea38e7Sbellard time_t ti; 1260faea38e7Sbellard int64_t secs; 1261faea38e7Sbellard 1262faea38e7Sbellard if (!sn) { 1263faea38e7Sbellard snprintf(buf, buf_size, 1264faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1265faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1266faea38e7Sbellard } else { 1267faea38e7Sbellard ti = sn->date_sec; 12683b9f94e1Sbellard #ifdef _WIN32 12693b9f94e1Sbellard ptm = localtime(&ti); 12703b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 12713b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 12723b9f94e1Sbellard #else 1273faea38e7Sbellard localtime_r(&ti, &tm); 1274faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1275faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 12763b9f94e1Sbellard #endif 1277faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1278faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1279faea38e7Sbellard "%02d:%02d:%02d.%03d", 1280faea38e7Sbellard (int)(secs / 3600), 1281faea38e7Sbellard (int)((secs / 60) % 60), 1282faea38e7Sbellard (int)(secs % 60), 1283faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1284faea38e7Sbellard snprintf(buf, buf_size, 1285faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1286faea38e7Sbellard sn->id_str, sn->name, 1287faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1288faea38e7Sbellard date_buf, 1289faea38e7Sbellard clock_buf); 1290faea38e7Sbellard } 1291faea38e7Sbellard return buf; 1292faea38e7Sbellard } 1293faea38e7Sbellard 129483f64091Sbellard 1295ea2384d3Sbellard /**************************************************************/ 129683f64091Sbellard /* async I/Os */ 1297ea2384d3Sbellard 12983b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 1299f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 130083f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1301ea2384d3Sbellard { 130283f64091Sbellard BlockDriver *drv = bs->drv; 1303a36e69ddSths BlockDriverAIOCB *ret; 1304ea2384d3Sbellard 130519cb3738Sbellard if (!drv) 1306ce1a14dcSpbrook return NULL; 130771d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 130871d0770cSaliguori return NULL; 130983f64091Sbellard 1310f141eafeSaliguori ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors, 1311f141eafeSaliguori cb, opaque); 1312a36e69ddSths 1313a36e69ddSths if (ret) { 1314a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1315a36e69ddSths bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1316a36e69ddSths bs->rd_ops ++; 1317a36e69ddSths } 1318a36e69ddSths 1319a36e69ddSths return ret; 132083f64091Sbellard } 132183f64091Sbellard 1322f141eafeSaliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 1323f141eafeSaliguori QEMUIOVector *qiov, int nb_sectors, 132483f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 13257674e7bfSbellard { 132683f64091Sbellard BlockDriver *drv = bs->drv; 1327a36e69ddSths BlockDriverAIOCB *ret; 132883f64091Sbellard 132919cb3738Sbellard if (!drv) 1330ce1a14dcSpbrook return NULL; 133183f64091Sbellard if (bs->read_only) 1332ce1a14dcSpbrook return NULL; 133371d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 133471d0770cSaliguori return NULL; 133583f64091Sbellard 1336f141eafeSaliguori ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors, 1337f141eafeSaliguori cb, opaque); 1338a36e69ddSths 1339a36e69ddSths if (ret) { 1340a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1341a36e69ddSths bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1342a36e69ddSths bs->wr_ops ++; 1343a36e69ddSths } 1344a36e69ddSths 1345a36e69ddSths return ret; 134683f64091Sbellard } 134783f64091Sbellard 134883f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 134983f64091Sbellard { 13506bbff9a0Saliguori acb->pool->cancel(acb); 135183f64091Sbellard } 135283f64091Sbellard 135383f64091Sbellard 135483f64091Sbellard /**************************************************************/ 135583f64091Sbellard /* async block device emulation */ 135683f64091Sbellard 1357*c16b5a2cSChristoph Hellwig typedef struct BlockDriverAIOCBSync { 1358*c16b5a2cSChristoph Hellwig BlockDriverAIOCB common; 1359*c16b5a2cSChristoph Hellwig QEMUBH *bh; 1360*c16b5a2cSChristoph Hellwig int ret; 1361*c16b5a2cSChristoph Hellwig /* vector translation state */ 1362*c16b5a2cSChristoph Hellwig QEMUIOVector *qiov; 1363*c16b5a2cSChristoph Hellwig uint8_t *bounce; 1364*c16b5a2cSChristoph Hellwig int is_write; 1365*c16b5a2cSChristoph Hellwig } BlockDriverAIOCBSync; 1366*c16b5a2cSChristoph Hellwig 1367*c16b5a2cSChristoph Hellwig static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 1368*c16b5a2cSChristoph Hellwig { 1369*c16b5a2cSChristoph Hellwig BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; 1370*c16b5a2cSChristoph Hellwig qemu_bh_cancel(acb->bh); 1371*c16b5a2cSChristoph Hellwig qemu_aio_release(acb); 1372*c16b5a2cSChristoph Hellwig } 1373*c16b5a2cSChristoph Hellwig 1374*c16b5a2cSChristoph Hellwig static AIOPool bdrv_em_aio_pool = { 1375*c16b5a2cSChristoph Hellwig .aiocb_size = sizeof(BlockDriverAIOCBSync), 1376*c16b5a2cSChristoph Hellwig .cancel = bdrv_aio_cancel_em, 1377*c16b5a2cSChristoph Hellwig }; 1378*c16b5a2cSChristoph Hellwig 137983f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 1380beac80cdSbellard { 1381ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 1382f141eafeSaliguori 1383f141eafeSaliguori if (!acb->is_write) 1384f141eafeSaliguori qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size); 1385ceb42de8Saliguori qemu_vfree(acb->bounce); 1386ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 1387f141eafeSaliguori 1388ce1a14dcSpbrook qemu_aio_release(acb); 1389beac80cdSbellard } 1390beac80cdSbellard 1391f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 1392f141eafeSaliguori int64_t sector_num, 1393f141eafeSaliguori QEMUIOVector *qiov, 1394f141eafeSaliguori int nb_sectors, 1395f141eafeSaliguori BlockDriverCompletionFunc *cb, 1396f141eafeSaliguori void *opaque, 1397f141eafeSaliguori int is_write) 1398f141eafeSaliguori 1399ea2384d3Sbellard { 1400ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 140183f64091Sbellard 1402*c16b5a2cSChristoph Hellwig acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque); 1403f141eafeSaliguori acb->is_write = is_write; 1404f141eafeSaliguori acb->qiov = qiov; 1405e268ca52Saliguori acb->bounce = qemu_blockalign(bs, qiov->size); 1406f141eafeSaliguori 1407ce1a14dcSpbrook if (!acb->bh) 1408ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1409f141eafeSaliguori 1410f141eafeSaliguori if (is_write) { 1411f141eafeSaliguori qemu_iovec_to_buffer(acb->qiov, acb->bounce); 1412f141eafeSaliguori acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors); 1413f141eafeSaliguori } else { 1414f141eafeSaliguori acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors); 1415f141eafeSaliguori } 1416f141eafeSaliguori 1417ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1418f141eafeSaliguori 1419ce1a14dcSpbrook return &acb->common; 14207a6cba61Spbrook } 14217a6cba61Spbrook 1422f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs, 1423f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 1424ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 142583f64091Sbellard { 1426f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0); 142783f64091Sbellard } 142883f64091Sbellard 1429f141eafeSaliguori static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs, 1430f141eafeSaliguori int64_t sector_num, QEMUIOVector *qiov, int nb_sectors, 1431f141eafeSaliguori BlockDriverCompletionFunc *cb, void *opaque) 1432f141eafeSaliguori { 1433f141eafeSaliguori return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1); 1434f141eafeSaliguori } 1435f141eafeSaliguori 143683f64091Sbellard /**************************************************************/ 143783f64091Sbellard /* sync block device emulation */ 143883f64091Sbellard 143983f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 144083f64091Sbellard { 144183f64091Sbellard *(int *)opaque = ret; 144283f64091Sbellard } 144383f64091Sbellard 144483f64091Sbellard #define NOT_DONE 0x7fffffff 144583f64091Sbellard 144683f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 144783f64091Sbellard uint8_t *buf, int nb_sectors) 144883f64091Sbellard { 1449ce1a14dcSpbrook int async_ret; 1450ce1a14dcSpbrook BlockDriverAIOCB *acb; 1451f141eafeSaliguori struct iovec iov; 1452f141eafeSaliguori QEMUIOVector qiov; 145383f64091Sbellard 145483f64091Sbellard async_ret = NOT_DONE; 14553f4cb3d3Sblueswir1 iov.iov_base = (void *)buf; 1456f141eafeSaliguori iov.iov_len = nb_sectors * 512; 1457f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 1458f141eafeSaliguori acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors, 145983f64091Sbellard bdrv_rw_em_cb, &async_ret); 1460baf35cb9Saliguori if (acb == NULL) 1461ce1a14dcSpbrook return -1; 1462baf35cb9Saliguori 146383f64091Sbellard while (async_ret == NOT_DONE) { 146483f64091Sbellard qemu_aio_wait(); 146583f64091Sbellard } 1466baf35cb9Saliguori 146783f64091Sbellard return async_ret; 146883f64091Sbellard } 146983f64091Sbellard 147083f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 147183f64091Sbellard const uint8_t *buf, int nb_sectors) 147283f64091Sbellard { 1473ce1a14dcSpbrook int async_ret; 1474ce1a14dcSpbrook BlockDriverAIOCB *acb; 1475f141eafeSaliguori struct iovec iov; 1476f141eafeSaliguori QEMUIOVector qiov; 147783f64091Sbellard 147883f64091Sbellard async_ret = NOT_DONE; 1479f141eafeSaliguori iov.iov_base = (void *)buf; 1480f141eafeSaliguori iov.iov_len = nb_sectors * 512; 1481f141eafeSaliguori qemu_iovec_init_external(&qiov, &iov, 1); 1482f141eafeSaliguori acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors, 148383f64091Sbellard bdrv_rw_em_cb, &async_ret); 1484baf35cb9Saliguori if (acb == NULL) 1485ce1a14dcSpbrook return -1; 148683f64091Sbellard while (async_ret == NOT_DONE) { 148783f64091Sbellard qemu_aio_wait(); 148883f64091Sbellard } 148983f64091Sbellard return async_ret; 149083f64091Sbellard } 1491ea2384d3Sbellard 1492ea2384d3Sbellard void bdrv_init(void) 1493ea2384d3Sbellard { 14945efa9d5aSAnthony Liguori module_call_init(MODULE_INIT_BLOCK); 1495ea2384d3Sbellard } 1496ce1a14dcSpbrook 1497*c16b5a2cSChristoph Hellwig void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs, 14986bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 14996bbff9a0Saliguori { 1500ce1a14dcSpbrook BlockDriverAIOCB *acb; 1501ce1a14dcSpbrook 15026bbff9a0Saliguori if (pool->free_aiocb) { 15036bbff9a0Saliguori acb = pool->free_aiocb; 15046bbff9a0Saliguori pool->free_aiocb = acb->next; 1505ce1a14dcSpbrook } else { 15066bbff9a0Saliguori acb = qemu_mallocz(pool->aiocb_size); 15076bbff9a0Saliguori acb->pool = pool; 1508ce1a14dcSpbrook } 1509ce1a14dcSpbrook acb->bs = bs; 1510ce1a14dcSpbrook acb->cb = cb; 1511ce1a14dcSpbrook acb->opaque = opaque; 1512ce1a14dcSpbrook return acb; 1513ce1a14dcSpbrook } 1514ce1a14dcSpbrook 1515ce1a14dcSpbrook void qemu_aio_release(void *p) 1516ce1a14dcSpbrook { 15176bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 15186bbff9a0Saliguori AIOPool *pool = acb->pool; 15196bbff9a0Saliguori acb->next = pool->free_aiocb; 15206bbff9a0Saliguori pool->free_aiocb = acb; 1521ce1a14dcSpbrook } 152219cb3738Sbellard 152319cb3738Sbellard /**************************************************************/ 152419cb3738Sbellard /* removable device support */ 152519cb3738Sbellard 152619cb3738Sbellard /** 152719cb3738Sbellard * Return TRUE if the media is present 152819cb3738Sbellard */ 152919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 153019cb3738Sbellard { 153119cb3738Sbellard BlockDriver *drv = bs->drv; 153219cb3738Sbellard int ret; 153319cb3738Sbellard if (!drv) 153419cb3738Sbellard return 0; 153519cb3738Sbellard if (!drv->bdrv_is_inserted) 153619cb3738Sbellard return 1; 153719cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 153819cb3738Sbellard return ret; 153919cb3738Sbellard } 154019cb3738Sbellard 154119cb3738Sbellard /** 154219cb3738Sbellard * Return TRUE if the media changed since the last call to this 154319cb3738Sbellard * function. It is currently only used for floppy disks 154419cb3738Sbellard */ 154519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 154619cb3738Sbellard { 154719cb3738Sbellard BlockDriver *drv = bs->drv; 154819cb3738Sbellard int ret; 154919cb3738Sbellard 155019cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 155119cb3738Sbellard ret = -ENOTSUP; 155219cb3738Sbellard else 155319cb3738Sbellard ret = drv->bdrv_media_changed(bs); 155419cb3738Sbellard if (ret == -ENOTSUP) 155519cb3738Sbellard ret = bs->media_changed; 155619cb3738Sbellard bs->media_changed = 0; 155719cb3738Sbellard return ret; 155819cb3738Sbellard } 155919cb3738Sbellard 156019cb3738Sbellard /** 156119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 156219cb3738Sbellard */ 156319cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag) 156419cb3738Sbellard { 156519cb3738Sbellard BlockDriver *drv = bs->drv; 156619cb3738Sbellard int ret; 156719cb3738Sbellard 156819cb3738Sbellard if (!drv || !drv->bdrv_eject) { 156919cb3738Sbellard ret = -ENOTSUP; 157019cb3738Sbellard } else { 157119cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 157219cb3738Sbellard } 157319cb3738Sbellard if (ret == -ENOTSUP) { 157419cb3738Sbellard if (eject_flag) 157519cb3738Sbellard bdrv_close(bs); 157619cb3738Sbellard } 157719cb3738Sbellard } 157819cb3738Sbellard 157919cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 158019cb3738Sbellard { 158119cb3738Sbellard return bs->locked; 158219cb3738Sbellard } 158319cb3738Sbellard 158419cb3738Sbellard /** 158519cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 158619cb3738Sbellard * to eject it manually). 158719cb3738Sbellard */ 158819cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 158919cb3738Sbellard { 159019cb3738Sbellard BlockDriver *drv = bs->drv; 159119cb3738Sbellard 159219cb3738Sbellard bs->locked = locked; 159319cb3738Sbellard if (drv && drv->bdrv_set_locked) { 159419cb3738Sbellard drv->bdrv_set_locked(bs, locked); 159519cb3738Sbellard } 159619cb3738Sbellard } 1597985a03b0Sths 1598985a03b0Sths /* needed for generic scsi interface */ 1599985a03b0Sths 1600985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 1601985a03b0Sths { 1602985a03b0Sths BlockDriver *drv = bs->drv; 1603985a03b0Sths 1604985a03b0Sths if (drv && drv->bdrv_ioctl) 1605985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 1606985a03b0Sths return -ENOTSUP; 1607985a03b0Sths } 16087d780669Saliguori 1609221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 1610221f715dSaliguori unsigned long int req, void *buf, 16117d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 16127d780669Saliguori { 1613221f715dSaliguori BlockDriver *drv = bs->drv; 16147d780669Saliguori 1615221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 1616221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 1617221f715dSaliguori return NULL; 16187d780669Saliguori } 1619e268ca52Saliguori 1620e268ca52Saliguori void *qemu_blockalign(BlockDriverState *bs, size_t size) 1621e268ca52Saliguori { 1622e268ca52Saliguori return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size); 1623e268ca52Saliguori } 1624