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" 253990d09aSblueswir1 #ifdef _BSD 263990d09aSblueswir1 /* include native header before sys-queue.h */ 273990d09aSblueswir1 #include <sys/queue.h> 283990d09aSblueswir1 #endif 293990d09aSblueswir1 30faf07963Spbrook #include "qemu-common.h" 3187ecb68bSpbrook #include "console.h" 32ea2384d3Sbellard #include "block_int.h" 33fc01f7e7Sbellard 347674e7bfSbellard #ifdef _BSD 357674e7bfSbellard #include <sys/types.h> 367674e7bfSbellard #include <sys/stat.h> 377674e7bfSbellard #include <sys/ioctl.h> 387674e7bfSbellard #include <sys/disk.h> 397674e7bfSbellard #endif 407674e7bfSbellard 4183f64091Sbellard #define SECTOR_BITS 9 4283f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS) 433b0d4f61Sbellard 4490765429Sbellard typedef struct BlockDriverAIOCBSync { 4590765429Sbellard BlockDriverAIOCB common; 4690765429Sbellard QEMUBH *bh; 4790765429Sbellard int ret; 4890765429Sbellard } BlockDriverAIOCBSync; 4990765429Sbellard 50ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 51ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 52ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 53ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 54ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 55ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 5683f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); 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 13083f64091Sbellard 1319596ebb7Spbrook static void bdrv_register(BlockDriver *bdrv) 132ea2384d3Sbellard { 133ce1a14dcSpbrook if (!bdrv->bdrv_aio_read) { 13483f64091Sbellard /* add AIO emulation layer */ 13583f64091Sbellard bdrv->bdrv_aio_read = bdrv_aio_read_em; 13683f64091Sbellard bdrv->bdrv_aio_write = bdrv_aio_write_em; 13783f64091Sbellard bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; 13890765429Sbellard bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync); 13983f64091Sbellard } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) { 14083f64091Sbellard /* add synchronous IO emulation layer */ 14183f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 14283f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 14383f64091Sbellard } 144ea2384d3Sbellard bdrv->next = first_drv; 145ea2384d3Sbellard first_drv = bdrv; 146ea2384d3Sbellard } 147b338082bSbellard 148b338082bSbellard /* create a new block device (by default it is empty) */ 149b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 150fc01f7e7Sbellard { 151b338082bSbellard BlockDriverState **pbs, *bs; 152b338082bSbellard 153b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 154b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 155ea2384d3Sbellard if (device_name[0] != '\0') { 156b338082bSbellard /* insert at the end */ 157b338082bSbellard pbs = &bdrv_first; 158b338082bSbellard while (*pbs != NULL) 159b338082bSbellard pbs = &(*pbs)->next; 160b338082bSbellard *pbs = bs; 161ea2384d3Sbellard } 162b338082bSbellard return bs; 163b338082bSbellard } 164b338082bSbellard 165ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 166ea2384d3Sbellard { 167ea2384d3Sbellard BlockDriver *drv1; 168ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 169ea2384d3Sbellard if (!strcmp(drv1->format_name, format_name)) 170ea2384d3Sbellard return drv1; 171ea2384d3Sbellard } 172ea2384d3Sbellard return NULL; 173ea2384d3Sbellard } 174ea2384d3Sbellard 175ea2384d3Sbellard int bdrv_create(BlockDriver *drv, 176ea2384d3Sbellard const char *filename, int64_t size_in_sectors, 177ea2384d3Sbellard const char *backing_file, int flags) 178ea2384d3Sbellard { 179ea2384d3Sbellard if (!drv->bdrv_create) 180ea2384d3Sbellard return -ENOTSUP; 181ea2384d3Sbellard return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); 182ea2384d3Sbellard } 183ea2384d3Sbellard 184d5249393Sbellard #ifdef _WIN32 18595389c86Sbellard void get_tmp_filename(char *filename, int size) 186d5249393Sbellard { 1873b9f94e1Sbellard char temp_dir[MAX_PATH]; 1883b9f94e1Sbellard 1893b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 1903b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 191d5249393Sbellard } 192d5249393Sbellard #else 19395389c86Sbellard void get_tmp_filename(char *filename, int size) 194ea2384d3Sbellard { 195ea2384d3Sbellard int fd; 1967ccfb2ebSblueswir1 const char *tmpdir; 197d5249393Sbellard /* XXX: race condition possible */ 1980badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 1990badc1eeSaurel32 if (!tmpdir) 2000badc1eeSaurel32 tmpdir = "/tmp"; 2010badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 202ea2384d3Sbellard fd = mkstemp(filename); 203ea2384d3Sbellard close(fd); 204ea2384d3Sbellard } 205d5249393Sbellard #endif 206ea2384d3Sbellard 20719cb3738Sbellard #ifdef _WIN32 208f45512feSbellard static int is_windows_drive_prefix(const char *filename) 209f45512feSbellard { 210f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 211f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 212f45512feSbellard filename[1] == ':'); 213f45512feSbellard } 214f45512feSbellard 21519cb3738Sbellard static int is_windows_drive(const char *filename) 21619cb3738Sbellard { 217f45512feSbellard if (is_windows_drive_prefix(filename) && 218f45512feSbellard filename[2] == '\0') 21919cb3738Sbellard return 1; 22019cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 22119cb3738Sbellard strstart(filename, "//./", NULL)) 22219cb3738Sbellard return 1; 22319cb3738Sbellard return 0; 22419cb3738Sbellard } 22519cb3738Sbellard #endif 22619cb3738Sbellard 22783f64091Sbellard static BlockDriver *find_protocol(const char *filename) 22883f64091Sbellard { 22983f64091Sbellard BlockDriver *drv1; 23083f64091Sbellard char protocol[128]; 23183f64091Sbellard int len; 23283f64091Sbellard const char *p; 23319cb3738Sbellard 23419cb3738Sbellard #ifdef _WIN32 235f45512feSbellard if (is_windows_drive(filename) || 236f45512feSbellard is_windows_drive_prefix(filename)) 23719cb3738Sbellard return &bdrv_raw; 23819cb3738Sbellard #endif 23983f64091Sbellard p = strchr(filename, ':'); 24083f64091Sbellard if (!p) 24183f64091Sbellard return &bdrv_raw; 24283f64091Sbellard len = p - filename; 24383f64091Sbellard if (len > sizeof(protocol) - 1) 24483f64091Sbellard len = sizeof(protocol) - 1; 24583f64091Sbellard memcpy(protocol, filename, len); 24683f64091Sbellard protocol[len] = '\0'; 24783f64091Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 24883f64091Sbellard if (drv1->protocol_name && 24983f64091Sbellard !strcmp(drv1->protocol_name, protocol)) 25083f64091Sbellard return drv1; 25183f64091Sbellard } 25283f64091Sbellard return NULL; 25383f64091Sbellard } 25483f64091Sbellard 2557674e7bfSbellard /* XXX: force raw format if block or character device ? It would 2567674e7bfSbellard simplify the BSD case */ 257ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 258ea2384d3Sbellard { 25983f64091Sbellard int ret, score, score_max; 260ea2384d3Sbellard BlockDriver *drv1, *drv; 26183f64091Sbellard uint8_t buf[2048]; 26283f64091Sbellard BlockDriverState *bs; 263ea2384d3Sbellard 26419cb3738Sbellard /* detect host devices. By convention, /dev/cdrom[N] is always 26519cb3738Sbellard recognized as a host CDROM */ 26619cb3738Sbellard if (strstart(filename, "/dev/cdrom", NULL)) 26719cb3738Sbellard return &bdrv_host_device; 26819cb3738Sbellard #ifdef _WIN32 26919cb3738Sbellard if (is_windows_drive(filename)) 27019cb3738Sbellard return &bdrv_host_device; 27119cb3738Sbellard #else 27219cb3738Sbellard { 27319cb3738Sbellard struct stat st; 27419cb3738Sbellard if (stat(filename, &st) >= 0 && 27519cb3738Sbellard (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 27619cb3738Sbellard return &bdrv_host_device; 27719cb3738Sbellard } 27819cb3738Sbellard } 27919cb3738Sbellard #endif 28019cb3738Sbellard 28183f64091Sbellard drv = find_protocol(filename); 28219cb3738Sbellard /* no need to test disk image formats for vvfat */ 28383f64091Sbellard if (drv == &bdrv_vvfat) 28483f64091Sbellard return drv; 28583f64091Sbellard 28683f64091Sbellard ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); 28783f64091Sbellard if (ret < 0) 2887674e7bfSbellard return NULL; 28983f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 29083f64091Sbellard bdrv_delete(bs); 291ea2384d3Sbellard if (ret < 0) { 292ea2384d3Sbellard return NULL; 293ea2384d3Sbellard } 294ea2384d3Sbellard 295ea2384d3Sbellard score_max = 0; 296ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 29783f64091Sbellard if (drv1->bdrv_probe) { 298ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 299ea2384d3Sbellard if (score > score_max) { 300ea2384d3Sbellard score_max = score; 301ea2384d3Sbellard drv = drv1; 302ea2384d3Sbellard } 303ea2384d3Sbellard } 30483f64091Sbellard } 305ea2384d3Sbellard return drv; 306ea2384d3Sbellard } 307ea2384d3Sbellard 30883f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 309b338082bSbellard { 31083f64091Sbellard BlockDriverState *bs; 31183f64091Sbellard int ret; 3123b0d4f61Sbellard 31383f64091Sbellard bs = bdrv_new(""); 31483f64091Sbellard ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); 31583f64091Sbellard if (ret < 0) { 31683f64091Sbellard bdrv_delete(bs); 31783f64091Sbellard return ret; 3183b0d4f61Sbellard } 31971d0770cSaliguori bs->growable = 1; 32083f64091Sbellard *pbs = bs; 32183f64091Sbellard return 0; 3223b0d4f61Sbellard } 3233b0d4f61Sbellard 32483f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags) 32583f64091Sbellard { 32683f64091Sbellard return bdrv_open2(bs, filename, flags, NULL); 327ea2384d3Sbellard } 328ea2384d3Sbellard 32983f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 330ea2384d3Sbellard BlockDriver *drv) 331ea2384d3Sbellard { 33283f64091Sbellard int ret, open_flags; 333eb5c851fSths char tmp_filename[PATH_MAX]; 334eb5c851fSths char backing_filename[PATH_MAX]; 335fc01f7e7Sbellard 3360849bf08Sbellard bs->read_only = 0; 337ea2384d3Sbellard bs->is_temporary = 0; 338ea2384d3Sbellard bs->encrypted = 0; 33933e3963eSbellard 34083f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 341ea2384d3Sbellard BlockDriverState *bs1; 342ea2384d3Sbellard int64_t total_size; 3437c96d46eSaliguori int is_protocol = 0; 34433e3963eSbellard 345ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 346ea2384d3Sbellard instead of opening 'filename' directly */ 347ea2384d3Sbellard 348ea2384d3Sbellard /* if there is a backing file, use it */ 349ea2384d3Sbellard bs1 = bdrv_new(""); 35051d7c00cSaliguori ret = bdrv_open(bs1, filename, 0); 35151d7c00cSaliguori if (ret < 0) { 352ea2384d3Sbellard bdrv_delete(bs1); 35351d7c00cSaliguori return ret; 354ea2384d3Sbellard } 35583f64091Sbellard total_size = bdrv_getlength(bs1) >> SECTOR_BITS; 3567c96d46eSaliguori 3577c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 3587c96d46eSaliguori is_protocol = 1; 3597c96d46eSaliguori 360ea2384d3Sbellard bdrv_delete(bs1); 361ea2384d3Sbellard 362ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 3637c96d46eSaliguori 3647c96d46eSaliguori /* Real path is meaningless for protocols */ 3657c96d46eSaliguori if (is_protocol) 3667c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 3677c96d46eSaliguori "%s", filename); 3687c96d46eSaliguori else 369a817d936Sbellard realpath(filename, backing_filename); 3707c96d46eSaliguori 37151d7c00cSaliguori ret = bdrv_create(&bdrv_qcow2, tmp_filename, 37251d7c00cSaliguori total_size, backing_filename, 0); 37351d7c00cSaliguori if (ret < 0) { 37451d7c00cSaliguori return ret; 375ea2384d3Sbellard } 376ea2384d3Sbellard filename = tmp_filename; 377ea2384d3Sbellard bs->is_temporary = 1; 378ea2384d3Sbellard } 379ea2384d3Sbellard 380ea2384d3Sbellard pstrcpy(bs->filename, sizeof(bs->filename), filename); 38183f64091Sbellard if (flags & BDRV_O_FILE) { 38283f64091Sbellard drv = find_protocol(filename); 38351d7c00cSaliguori } else if (!drv) { 384ea2384d3Sbellard drv = find_image_format(filename); 385ea2384d3Sbellard } 38651d7c00cSaliguori if (!drv) { 38751d7c00cSaliguori ret = -ENOENT; 38851d7c00cSaliguori goto unlink_and_fail; 38983f64091Sbellard } 390ea2384d3Sbellard bs->drv = drv; 391ea2384d3Sbellard bs->opaque = qemu_mallocz(drv->instance_size); 39283f64091Sbellard /* Note: for compatibility, we open disk image files as RDWR, and 39383f64091Sbellard RDONLY as fallback */ 39483f64091Sbellard if (!(flags & BDRV_O_FILE)) 3959f7965c7Saliguori open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); 39683f64091Sbellard else 39783f64091Sbellard open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); 39883f64091Sbellard ret = drv->bdrv_open(bs, filename, open_flags); 399a0a83536Saurel32 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { 4009f7965c7Saliguori ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); 40183f64091Sbellard bs->read_only = 1; 40283f64091Sbellard } 403ea2384d3Sbellard if (ret < 0) { 404ea2384d3Sbellard qemu_free(bs->opaque); 4056b21b973Sbellard bs->opaque = NULL; 4066b21b973Sbellard bs->drv = NULL; 40751d7c00cSaliguori unlink_and_fail: 40851d7c00cSaliguori if (bs->is_temporary) 40951d7c00cSaliguori unlink(filename); 41083f64091Sbellard return ret; 411ea2384d3Sbellard } 412d15a771dSbellard if (drv->bdrv_getlength) { 413d15a771dSbellard bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 414d15a771dSbellard } 415ea2384d3Sbellard #ifndef _WIN32 416ea2384d3Sbellard if (bs->is_temporary) { 417ea2384d3Sbellard unlink(filename); 41833e3963eSbellard } 41967b915a5Sbellard #endif 42083f64091Sbellard if (bs->backing_file[0] != '\0') { 421ea2384d3Sbellard /* if there is a backing file, use it */ 422ea2384d3Sbellard bs->backing_hd = bdrv_new(""); 42383f64091Sbellard path_combine(backing_filename, sizeof(backing_filename), 42483f64091Sbellard filename, bs->backing_file); 42551d7c00cSaliguori ret = bdrv_open(bs->backing_hd, backing_filename, open_flags); 42651d7c00cSaliguori if (ret < 0) { 42751d7c00cSaliguori bdrv_close(bs); 42851d7c00cSaliguori return ret; 42951d7c00cSaliguori } 430ea2384d3Sbellard } 43133e3963eSbellard 432b338082bSbellard /* call the change callback */ 43319cb3738Sbellard bs->media_changed = 1; 434b338082bSbellard if (bs->change_cb) 435b338082bSbellard bs->change_cb(bs->change_opaque); 436b338082bSbellard 437b338082bSbellard return 0; 438fc01f7e7Sbellard } 439fc01f7e7Sbellard 440fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 441fc01f7e7Sbellard { 44219cb3738Sbellard if (bs->drv) { 443ea2384d3Sbellard if (bs->backing_hd) 444ea2384d3Sbellard bdrv_delete(bs->backing_hd); 445ea2384d3Sbellard bs->drv->bdrv_close(bs); 446ea2384d3Sbellard qemu_free(bs->opaque); 447ea2384d3Sbellard #ifdef _WIN32 448ea2384d3Sbellard if (bs->is_temporary) { 449ea2384d3Sbellard unlink(bs->filename); 450ea2384d3Sbellard } 45167b915a5Sbellard #endif 452ea2384d3Sbellard bs->opaque = NULL; 453ea2384d3Sbellard bs->drv = NULL; 454b338082bSbellard 455b338082bSbellard /* call the change callback */ 45619cb3738Sbellard bs->media_changed = 1; 457b338082bSbellard if (bs->change_cb) 458b338082bSbellard bs->change_cb(bs->change_opaque); 459b338082bSbellard } 460b338082bSbellard } 461b338082bSbellard 462b338082bSbellard void bdrv_delete(BlockDriverState *bs) 463b338082bSbellard { 46434c6f050Saurel32 BlockDriverState **pbs; 46534c6f050Saurel32 46634c6f050Saurel32 pbs = &bdrv_first; 46734c6f050Saurel32 while (*pbs != bs && *pbs != NULL) 46834c6f050Saurel32 pbs = &(*pbs)->next; 46934c6f050Saurel32 if (*pbs == bs) 47034c6f050Saurel32 *pbs = bs->next; 47134c6f050Saurel32 472b338082bSbellard bdrv_close(bs); 473b338082bSbellard qemu_free(bs); 474fc01f7e7Sbellard } 475fc01f7e7Sbellard 47633e3963eSbellard /* commit COW file into the raw image */ 47733e3963eSbellard int bdrv_commit(BlockDriverState *bs) 47833e3963eSbellard { 47919cb3738Sbellard BlockDriver *drv = bs->drv; 48083f64091Sbellard int64_t i, total_sectors; 481ea2384d3Sbellard int n, j; 482ea2384d3Sbellard unsigned char sector[512]; 48333e3963eSbellard 48419cb3738Sbellard if (!drv) 48519cb3738Sbellard return -ENOMEDIUM; 48633e3963eSbellard 48733e3963eSbellard if (bs->read_only) { 488ea2384d3Sbellard return -EACCES; 48933e3963eSbellard } 49033e3963eSbellard 491ea2384d3Sbellard if (!bs->backing_hd) { 492ea2384d3Sbellard return -ENOTSUP; 493ea2384d3Sbellard } 494ea2384d3Sbellard 49583f64091Sbellard total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 49683f64091Sbellard for (i = 0; i < total_sectors;) { 49719cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 498ea2384d3Sbellard for(j = 0; j < n; j++) { 49933e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 500ea2384d3Sbellard return -EIO; 50133e3963eSbellard } 50233e3963eSbellard 503ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 504ea2384d3Sbellard return -EIO; 50533e3963eSbellard } 506ea2384d3Sbellard i++; 507ea2384d3Sbellard } 508ea2384d3Sbellard } else { 509ea2384d3Sbellard i += n; 51033e3963eSbellard } 51133e3963eSbellard } 51295389c86Sbellard 51319cb3738Sbellard if (drv->bdrv_make_empty) 51419cb3738Sbellard return drv->bdrv_make_empty(bs); 51595389c86Sbellard 51633e3963eSbellard return 0; 51733e3963eSbellard } 51833e3963eSbellard 51971d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 52071d0770cSaliguori size_t size) 52171d0770cSaliguori { 52271d0770cSaliguori int64_t len; 52371d0770cSaliguori 52471d0770cSaliguori if (!bdrv_is_inserted(bs)) 52571d0770cSaliguori return -ENOMEDIUM; 52671d0770cSaliguori 52771d0770cSaliguori if (bs->growable) 52871d0770cSaliguori return 0; 52971d0770cSaliguori 53071d0770cSaliguori len = bdrv_getlength(bs); 53171d0770cSaliguori 53271d0770cSaliguori if ((offset + size) > len) 53371d0770cSaliguori return -EIO; 53471d0770cSaliguori 53571d0770cSaliguori return 0; 53671d0770cSaliguori } 53771d0770cSaliguori 53871d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 53971d0770cSaliguori int nb_sectors) 54071d0770cSaliguori { 54171d0770cSaliguori int64_t offset; 54271d0770cSaliguori 54371d0770cSaliguori /* Deal with byte accesses */ 54471d0770cSaliguori if (sector_num < 0) 54571d0770cSaliguori offset = -sector_num; 54671d0770cSaliguori else 54771d0770cSaliguori offset = sector_num * 512; 54871d0770cSaliguori 54971d0770cSaliguori return bdrv_check_byte_request(bs, offset, nb_sectors * 512); 55071d0770cSaliguori } 55171d0770cSaliguori 55219cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 553fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 554fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 555fc01f7e7Sbellard { 556ea2384d3Sbellard BlockDriver *drv = bs->drv; 557fc01f7e7Sbellard 55819cb3738Sbellard if (!drv) 55919cb3738Sbellard return -ENOMEDIUM; 56071d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 56171d0770cSaliguori return -EIO; 562b338082bSbellard 56383f64091Sbellard if (drv->bdrv_pread) { 56483f64091Sbellard int ret, len; 56583f64091Sbellard len = nb_sectors * 512; 56683f64091Sbellard ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); 56783f64091Sbellard if (ret < 0) 56883f64091Sbellard return ret; 56983f64091Sbellard else if (ret != len) 57019cb3738Sbellard return -EINVAL; 571a36e69ddSths else { 572a36e69ddSths bs->rd_bytes += (unsigned) len; 573a36e69ddSths bs->rd_ops ++; 57483f64091Sbellard return 0; 575a36e69ddSths } 57683f64091Sbellard } else { 57783f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 57883f64091Sbellard } 57983f64091Sbellard } 580fc01f7e7Sbellard 58119cb3738Sbellard /* Return < 0 if error. Important errors are: 58219cb3738Sbellard -EIO generic I/O error (may happen for all errors) 58319cb3738Sbellard -ENOMEDIUM No media inserted. 58419cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 58519cb3738Sbellard -EACCES Trying to write a read-only device 58619cb3738Sbellard */ 587fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 588fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 589fc01f7e7Sbellard { 59083f64091Sbellard BlockDriver *drv = bs->drv; 59119cb3738Sbellard if (!bs->drv) 59219cb3738Sbellard return -ENOMEDIUM; 5930849bf08Sbellard if (bs->read_only) 59419cb3738Sbellard return -EACCES; 59571d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 59671d0770cSaliguori return -EIO; 59771d0770cSaliguori 59883f64091Sbellard if (drv->bdrv_pwrite) { 59942fb2807Saliguori int ret, len, count = 0; 60083f64091Sbellard len = nb_sectors * 512; 60142fb2807Saliguori do { 60242fb2807Saliguori ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count); 60342fb2807Saliguori if (ret < 0) { 60442fb2807Saliguori printf("bdrv_write ret=%d\n", ret); 60583f64091Sbellard return ret; 60642fb2807Saliguori } 60742fb2807Saliguori count += ret; 60842fb2807Saliguori buf += ret; 60942fb2807Saliguori } while (count != len); 610a36e69ddSths bs->wr_bytes += (unsigned) len; 611a36e69ddSths bs->wr_ops ++; 61283f64091Sbellard return 0; 613a36e69ddSths } 61483f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 61583f64091Sbellard } 61683f64091Sbellard 61783f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, 618faea38e7Sbellard uint8_t *buf, int count1) 61983f64091Sbellard { 62083f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 62183f64091Sbellard int len, nb_sectors, count; 62283f64091Sbellard int64_t sector_num; 62383f64091Sbellard 62483f64091Sbellard count = count1; 62583f64091Sbellard /* first read to align to sector start */ 62683f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 62783f64091Sbellard if (len > count) 62883f64091Sbellard len = count; 62983f64091Sbellard sector_num = offset >> SECTOR_BITS; 63083f64091Sbellard if (len > 0) { 63183f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 63283f64091Sbellard return -EIO; 63383f64091Sbellard memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); 63483f64091Sbellard count -= len; 63583f64091Sbellard if (count == 0) 63683f64091Sbellard return count1; 63783f64091Sbellard sector_num++; 63883f64091Sbellard buf += len; 63983f64091Sbellard } 64083f64091Sbellard 64183f64091Sbellard /* read the sectors "in place" */ 64283f64091Sbellard nb_sectors = count >> SECTOR_BITS; 64383f64091Sbellard if (nb_sectors > 0) { 64483f64091Sbellard if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) 64583f64091Sbellard return -EIO; 64683f64091Sbellard sector_num += nb_sectors; 64783f64091Sbellard len = nb_sectors << SECTOR_BITS; 64883f64091Sbellard buf += len; 64983f64091Sbellard count -= len; 65083f64091Sbellard } 65183f64091Sbellard 65283f64091Sbellard /* add data from the last sector */ 65383f64091Sbellard if (count > 0) { 65483f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 65583f64091Sbellard return -EIO; 65683f64091Sbellard memcpy(buf, tmp_buf, count); 65783f64091Sbellard } 65883f64091Sbellard return count1; 65983f64091Sbellard } 66083f64091Sbellard 66183f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, 662faea38e7Sbellard const uint8_t *buf, int count1) 66383f64091Sbellard { 66483f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 66583f64091Sbellard int len, nb_sectors, count; 66683f64091Sbellard int64_t sector_num; 66783f64091Sbellard 66883f64091Sbellard count = count1; 66983f64091Sbellard /* first write to align to sector start */ 67083f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 67183f64091Sbellard if (len > count) 67283f64091Sbellard len = count; 67383f64091Sbellard sector_num = offset >> SECTOR_BITS; 67483f64091Sbellard if (len > 0) { 67583f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 67683f64091Sbellard return -EIO; 67783f64091Sbellard memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); 67883f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 67983f64091Sbellard return -EIO; 68083f64091Sbellard count -= len; 68183f64091Sbellard if (count == 0) 68283f64091Sbellard return count1; 68383f64091Sbellard sector_num++; 68483f64091Sbellard buf += len; 68583f64091Sbellard } 68683f64091Sbellard 68783f64091Sbellard /* write the sectors "in place" */ 68883f64091Sbellard nb_sectors = count >> SECTOR_BITS; 68983f64091Sbellard if (nb_sectors > 0) { 69083f64091Sbellard if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) 69183f64091Sbellard return -EIO; 69283f64091Sbellard sector_num += nb_sectors; 69383f64091Sbellard len = nb_sectors << SECTOR_BITS; 69483f64091Sbellard buf += len; 69583f64091Sbellard count -= len; 69683f64091Sbellard } 69783f64091Sbellard 69883f64091Sbellard /* add data from the last sector */ 69983f64091Sbellard if (count > 0) { 70083f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 70183f64091Sbellard return -EIO; 70283f64091Sbellard memcpy(tmp_buf, buf, count); 70383f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 70483f64091Sbellard return -EIO; 70583f64091Sbellard } 70683f64091Sbellard return count1; 70783f64091Sbellard } 70883f64091Sbellard 70983f64091Sbellard /** 71083f64091Sbellard * Read with byte offsets (needed only for file protocols) 71183f64091Sbellard */ 71283f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset, 71383f64091Sbellard void *buf1, int count1) 71483f64091Sbellard { 71583f64091Sbellard BlockDriver *drv = bs->drv; 71683f64091Sbellard 71783f64091Sbellard if (!drv) 71819cb3738Sbellard return -ENOMEDIUM; 71971d0770cSaliguori if (bdrv_check_byte_request(bs, offset, count1)) 72071d0770cSaliguori return -EIO; 72171d0770cSaliguori 72283f64091Sbellard if (!drv->bdrv_pread) 723faea38e7Sbellard return bdrv_pread_em(bs, offset, buf1, count1); 72483f64091Sbellard return drv->bdrv_pread(bs, offset, buf1, count1); 72583f64091Sbellard } 72683f64091Sbellard 72783f64091Sbellard /** 72883f64091Sbellard * Write with byte offsets (needed only for file protocols) 72983f64091Sbellard */ 73083f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 73183f64091Sbellard const void *buf1, int count1) 73283f64091Sbellard { 73383f64091Sbellard BlockDriver *drv = bs->drv; 73483f64091Sbellard 73583f64091Sbellard if (!drv) 73619cb3738Sbellard return -ENOMEDIUM; 73771d0770cSaliguori if (bdrv_check_byte_request(bs, offset, count1)) 73871d0770cSaliguori return -EIO; 73971d0770cSaliguori 74083f64091Sbellard if (!drv->bdrv_pwrite) 741faea38e7Sbellard return bdrv_pwrite_em(bs, offset, buf1, count1); 74283f64091Sbellard return drv->bdrv_pwrite(bs, offset, buf1, count1); 74383f64091Sbellard } 74483f64091Sbellard 74583f64091Sbellard /** 74683f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 74783f64091Sbellard */ 74883f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 74983f64091Sbellard { 75083f64091Sbellard BlockDriver *drv = bs->drv; 75183f64091Sbellard if (!drv) 75219cb3738Sbellard return -ENOMEDIUM; 75383f64091Sbellard if (!drv->bdrv_truncate) 75483f64091Sbellard return -ENOTSUP; 75583f64091Sbellard return drv->bdrv_truncate(bs, offset); 75683f64091Sbellard } 75783f64091Sbellard 75883f64091Sbellard /** 75983f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 76083f64091Sbellard */ 76183f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 76283f64091Sbellard { 76383f64091Sbellard BlockDriver *drv = bs->drv; 76483f64091Sbellard if (!drv) 76519cb3738Sbellard return -ENOMEDIUM; 76683f64091Sbellard if (!drv->bdrv_getlength) { 76783f64091Sbellard /* legacy mode */ 76883f64091Sbellard return bs->total_sectors * SECTOR_SIZE; 76983f64091Sbellard } 77083f64091Sbellard return drv->bdrv_getlength(bs); 771fc01f7e7Sbellard } 772fc01f7e7Sbellard 77319cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 77496b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 775fc01f7e7Sbellard { 77619cb3738Sbellard int64_t length; 77719cb3738Sbellard length = bdrv_getlength(bs); 77819cb3738Sbellard if (length < 0) 77919cb3738Sbellard length = 0; 78019cb3738Sbellard else 78119cb3738Sbellard length = length >> SECTOR_BITS; 78219cb3738Sbellard *nb_sectors_ptr = length; 783fc01f7e7Sbellard } 784cf98951bSbellard 785f3d54fc4Saliguori struct partition { 786f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 787f3d54fc4Saliguori uint8_t head; /* starting head */ 788f3d54fc4Saliguori uint8_t sector; /* starting sector */ 789f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 790f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 791f3d54fc4Saliguori uint8_t end_head; /* end head */ 792f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 793f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 794f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 795f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 796f3d54fc4Saliguori } __attribute__((packed)); 797f3d54fc4Saliguori 798f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 799f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 800f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 801f3d54fc4Saliguori { 802f3d54fc4Saliguori uint8_t buf[512]; 803f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 804f3d54fc4Saliguori struct partition *p; 805f3d54fc4Saliguori uint32_t nr_sects; 806a38131b6Sblueswir1 uint64_t nb_sectors; 807f3d54fc4Saliguori 808f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 809f3d54fc4Saliguori 810f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 811f3d54fc4Saliguori if (ret < 0) 812f3d54fc4Saliguori return -1; 813f3d54fc4Saliguori /* test msdos magic */ 814f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 815f3d54fc4Saliguori return -1; 816f3d54fc4Saliguori for(i = 0; i < 4; i++) { 817f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 818f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 819f3d54fc4Saliguori if (nr_sects && p->end_head) { 820f3d54fc4Saliguori /* We make the assumption that the partition terminates on 821f3d54fc4Saliguori a cylinder boundary */ 822f3d54fc4Saliguori heads = p->end_head + 1; 823f3d54fc4Saliguori sectors = p->end_sector & 63; 824f3d54fc4Saliguori if (sectors == 0) 825f3d54fc4Saliguori continue; 826f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 827f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 828f3d54fc4Saliguori continue; 829f3d54fc4Saliguori *pheads = heads; 830f3d54fc4Saliguori *psectors = sectors; 831f3d54fc4Saliguori *pcylinders = cylinders; 832f3d54fc4Saliguori #if 0 833f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 834f3d54fc4Saliguori cylinders, heads, sectors); 835f3d54fc4Saliguori #endif 836f3d54fc4Saliguori return 0; 837f3d54fc4Saliguori } 838f3d54fc4Saliguori } 839f3d54fc4Saliguori return -1; 840f3d54fc4Saliguori } 841f3d54fc4Saliguori 842f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 843f3d54fc4Saliguori { 844f3d54fc4Saliguori int translation, lba_detected = 0; 845f3d54fc4Saliguori int cylinders, heads, secs; 846a38131b6Sblueswir1 uint64_t nb_sectors; 847f3d54fc4Saliguori 848f3d54fc4Saliguori /* if a geometry hint is available, use it */ 849f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 850f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 851f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 852f3d54fc4Saliguori if (cylinders != 0) { 853f3d54fc4Saliguori *pcyls = cylinders; 854f3d54fc4Saliguori *pheads = heads; 855f3d54fc4Saliguori *psecs = secs; 856f3d54fc4Saliguori } else { 857f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 858f3d54fc4Saliguori if (heads > 16) { 859f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 860f3d54fc4Saliguori translation was active, so the default 861f3d54fc4Saliguori hardware geometry is OK */ 862f3d54fc4Saliguori lba_detected = 1; 863f3d54fc4Saliguori goto default_geometry; 864f3d54fc4Saliguori } else { 865f3d54fc4Saliguori *pcyls = cylinders; 866f3d54fc4Saliguori *pheads = heads; 867f3d54fc4Saliguori *psecs = secs; 868f3d54fc4Saliguori /* disable any translation to be in sync with 869f3d54fc4Saliguori the logical geometry */ 870f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 871f3d54fc4Saliguori bdrv_set_translation_hint(bs, 872f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 873f3d54fc4Saliguori } 874f3d54fc4Saliguori } 875f3d54fc4Saliguori } else { 876f3d54fc4Saliguori default_geometry: 877f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 878f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 879f3d54fc4Saliguori 880f3d54fc4Saliguori if (cylinders > 16383) 881f3d54fc4Saliguori cylinders = 16383; 882f3d54fc4Saliguori else if (cylinders < 2) 883f3d54fc4Saliguori cylinders = 2; 884f3d54fc4Saliguori *pcyls = cylinders; 885f3d54fc4Saliguori *pheads = 16; 886f3d54fc4Saliguori *psecs = 63; 887f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 888f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 889f3d54fc4Saliguori bdrv_set_translation_hint(bs, 890f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 891f3d54fc4Saliguori } else { 892f3d54fc4Saliguori bdrv_set_translation_hint(bs, 893f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 894f3d54fc4Saliguori } 895f3d54fc4Saliguori } 896f3d54fc4Saliguori } 897f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 898f3d54fc4Saliguori } 899f3d54fc4Saliguori } 900f3d54fc4Saliguori 901b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 902b338082bSbellard int cyls, int heads, int secs) 903b338082bSbellard { 904b338082bSbellard bs->cyls = cyls; 905b338082bSbellard bs->heads = heads; 906b338082bSbellard bs->secs = secs; 907b338082bSbellard } 908b338082bSbellard 909b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 910b338082bSbellard { 911b338082bSbellard bs->type = type; 912b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 913b338082bSbellard type == BDRV_TYPE_FLOPPY)); 914b338082bSbellard } 915b338082bSbellard 91646d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 91746d4767dSbellard { 91846d4767dSbellard bs->translation = translation; 91946d4767dSbellard } 92046d4767dSbellard 921b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 922b338082bSbellard int *pcyls, int *pheads, int *psecs) 923b338082bSbellard { 924b338082bSbellard *pcyls = bs->cyls; 925b338082bSbellard *pheads = bs->heads; 926b338082bSbellard *psecs = bs->secs; 927b338082bSbellard } 928b338082bSbellard 929b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 930b338082bSbellard { 931b338082bSbellard return bs->type; 932b338082bSbellard } 933b338082bSbellard 93446d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 93546d4767dSbellard { 93646d4767dSbellard return bs->translation; 93746d4767dSbellard } 93846d4767dSbellard 939b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 940b338082bSbellard { 941b338082bSbellard return bs->removable; 942b338082bSbellard } 943b338082bSbellard 944b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 945b338082bSbellard { 946b338082bSbellard return bs->read_only; 947b338082bSbellard } 948b338082bSbellard 949985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 950985a03b0Sths { 951985a03b0Sths return bs->sg; 952985a03b0Sths } 953985a03b0Sths 95419cb3738Sbellard /* XXX: no longer used */ 955b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 956b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 957b338082bSbellard { 958b338082bSbellard bs->change_cb = change_cb; 959b338082bSbellard bs->change_opaque = opaque; 960b338082bSbellard } 961b338082bSbellard 962ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 963ea2384d3Sbellard { 964ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 965ea2384d3Sbellard return 1; 966ea2384d3Sbellard return bs->encrypted; 967ea2384d3Sbellard } 968ea2384d3Sbellard 969ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 970ea2384d3Sbellard { 971ea2384d3Sbellard int ret; 972ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 973ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 974ea2384d3Sbellard if (ret < 0) 975ea2384d3Sbellard return ret; 976ea2384d3Sbellard if (!bs->encrypted) 977ea2384d3Sbellard return 0; 978ea2384d3Sbellard } 979ea2384d3Sbellard if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) 980ea2384d3Sbellard return -1; 981ea2384d3Sbellard return bs->drv->bdrv_set_key(bs, key); 982ea2384d3Sbellard } 983ea2384d3Sbellard 984ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 985ea2384d3Sbellard { 98619cb3738Sbellard if (!bs->drv) { 987ea2384d3Sbellard buf[0] = '\0'; 988ea2384d3Sbellard } else { 989ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 990ea2384d3Sbellard } 991ea2384d3Sbellard } 992ea2384d3Sbellard 993ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 994ea2384d3Sbellard void *opaque) 995ea2384d3Sbellard { 996ea2384d3Sbellard BlockDriver *drv; 997ea2384d3Sbellard 998ea2384d3Sbellard for (drv = first_drv; drv != NULL; drv = drv->next) { 999ea2384d3Sbellard it(opaque, drv->format_name); 1000ea2384d3Sbellard } 1001ea2384d3Sbellard } 1002ea2384d3Sbellard 1003b338082bSbellard BlockDriverState *bdrv_find(const char *name) 1004b338082bSbellard { 1005b338082bSbellard BlockDriverState *bs; 1006b338082bSbellard 1007b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1008b338082bSbellard if (!strcmp(name, bs->device_name)) 1009b338082bSbellard return bs; 1010b338082bSbellard } 1011b338082bSbellard return NULL; 1012b338082bSbellard } 1013b338082bSbellard 101451de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 101581d0912dSbellard { 101681d0912dSbellard BlockDriverState *bs; 101781d0912dSbellard 101881d0912dSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 101951de9760Saliguori it(opaque, bs); 102081d0912dSbellard } 102181d0912dSbellard } 102281d0912dSbellard 1023ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1024ea2384d3Sbellard { 1025ea2384d3Sbellard return bs->device_name; 1026ea2384d3Sbellard } 1027ea2384d3Sbellard 10287a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 10297a6cba61Spbrook { 10307a6cba61Spbrook if (bs->drv->bdrv_flush) 10317a6cba61Spbrook bs->drv->bdrv_flush(bs); 10327a6cba61Spbrook if (bs->backing_hd) 10337a6cba61Spbrook bdrv_flush(bs->backing_hd); 10347a6cba61Spbrook } 10357a6cba61Spbrook 1036c6ca28d6Saliguori void bdrv_flush_all(void) 1037c6ca28d6Saliguori { 1038c6ca28d6Saliguori BlockDriverState *bs; 1039c6ca28d6Saliguori 1040c6ca28d6Saliguori for (bs = bdrv_first; bs != NULL; bs = bs->next) 1041c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 1042c6ca28d6Saliguori (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) 1043c6ca28d6Saliguori bdrv_flush(bs); 1044c6ca28d6Saliguori } 1045c6ca28d6Saliguori 1046f58c7b35Sths /* 1047f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1048f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1049f58c7b35Sths * hence all their sectors are reported as allocated. 1050f58c7b35Sths * 1051f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1052f58c7b35Sths * the specified sector) that are known to be in the same 1053f58c7b35Sths * allocated/unallocated state. 1054f58c7b35Sths * 1055f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1056f58c7b35Sths */ 1057f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1058f58c7b35Sths int *pnum) 1059f58c7b35Sths { 1060f58c7b35Sths int64_t n; 1061f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1062f58c7b35Sths if (sector_num >= bs->total_sectors) { 1063f58c7b35Sths *pnum = 0; 1064f58c7b35Sths return 0; 1065f58c7b35Sths } 1066f58c7b35Sths n = bs->total_sectors - sector_num; 1067f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1068f58c7b35Sths return 1; 1069f58c7b35Sths } 1070f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1071f58c7b35Sths } 1072f58c7b35Sths 1073b338082bSbellard void bdrv_info(void) 1074b338082bSbellard { 1075b338082bSbellard BlockDriverState *bs; 1076b338082bSbellard 1077b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1078b338082bSbellard term_printf("%s:", bs->device_name); 1079b338082bSbellard term_printf(" type="); 1080b338082bSbellard switch(bs->type) { 1081b338082bSbellard case BDRV_TYPE_HD: 1082b338082bSbellard term_printf("hd"); 1083b338082bSbellard break; 1084b338082bSbellard case BDRV_TYPE_CDROM: 1085b338082bSbellard term_printf("cdrom"); 1086b338082bSbellard break; 1087b338082bSbellard case BDRV_TYPE_FLOPPY: 1088b338082bSbellard term_printf("floppy"); 1089b338082bSbellard break; 1090b338082bSbellard } 1091b338082bSbellard term_printf(" removable=%d", bs->removable); 1092b338082bSbellard if (bs->removable) { 1093b338082bSbellard term_printf(" locked=%d", bs->locked); 1094b338082bSbellard } 109519cb3738Sbellard if (bs->drv) { 1096fef30743Sths term_printf(" file="); 1097fef30743Sths term_print_filename(bs->filename); 1098fef30743Sths if (bs->backing_file[0] != '\0') { 1099fef30743Sths term_printf(" backing_file="); 1100fef30743Sths term_print_filename(bs->backing_file); 1101fef30743Sths } 1102b338082bSbellard term_printf(" ro=%d", bs->read_only); 1103ea2384d3Sbellard term_printf(" drv=%s", bs->drv->format_name); 1104ea2384d3Sbellard if (bs->encrypted) 1105ea2384d3Sbellard term_printf(" encrypted"); 1106b338082bSbellard } else { 1107b338082bSbellard term_printf(" [not inserted]"); 1108b338082bSbellard } 1109b338082bSbellard term_printf("\n"); 1110b338082bSbellard } 1111b338082bSbellard } 1112a36e69ddSths 1113a36e69ddSths /* The "info blockstats" command. */ 1114a36e69ddSths void bdrv_info_stats (void) 1115a36e69ddSths { 1116a36e69ddSths BlockDriverState *bs; 1117a7cbfae0Saliguori BlockDriverInfo bdi; 1118a36e69ddSths 1119a36e69ddSths for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1120a36e69ddSths term_printf ("%s:" 1121a36e69ddSths " rd_bytes=%" PRIu64 1122a36e69ddSths " wr_bytes=%" PRIu64 1123a36e69ddSths " rd_operations=%" PRIu64 1124a36e69ddSths " wr_operations=%" PRIu64 1125a7cbfae0Saliguori , 1126a36e69ddSths bs->device_name, 1127a36e69ddSths bs->rd_bytes, bs->wr_bytes, 1128a36e69ddSths bs->rd_ops, bs->wr_ops); 1129a7cbfae0Saliguori if (bdrv_get_info(bs, &bdi) == 0) 11301987530fSaliguori term_printf(" high=%" PRId64 11311987530fSaliguori " bytes_free=%" PRId64, 11321987530fSaliguori bdi.highest_alloc, bdi.num_free_bytes); 1133a7cbfae0Saliguori term_printf("\n"); 1134a36e69ddSths } 1135a36e69ddSths } 1136ea2384d3Sbellard 1137*045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1138*045df330Saliguori { 1139*045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1140*045df330Saliguori return bs->backing_file; 1141*045df330Saliguori else if (bs->encrypted) 1142*045df330Saliguori return bs->filename; 1143*045df330Saliguori else 1144*045df330Saliguori return NULL; 1145*045df330Saliguori } 1146*045df330Saliguori 114783f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 114883f64091Sbellard char *filename, int filename_size) 114983f64091Sbellard { 115083f64091Sbellard if (!bs->backing_hd) { 115183f64091Sbellard pstrcpy(filename, filename_size, ""); 115283f64091Sbellard } else { 115383f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 115483f64091Sbellard } 115583f64091Sbellard } 115683f64091Sbellard 1157faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1158faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1159faea38e7Sbellard { 1160faea38e7Sbellard BlockDriver *drv = bs->drv; 1161faea38e7Sbellard if (!drv) 116219cb3738Sbellard return -ENOMEDIUM; 1163faea38e7Sbellard if (!drv->bdrv_write_compressed) 1164faea38e7Sbellard return -ENOTSUP; 1165faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1166faea38e7Sbellard } 1167faea38e7Sbellard 1168faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1169faea38e7Sbellard { 1170faea38e7Sbellard BlockDriver *drv = bs->drv; 1171faea38e7Sbellard if (!drv) 117219cb3738Sbellard return -ENOMEDIUM; 1173faea38e7Sbellard if (!drv->bdrv_get_info) 1174faea38e7Sbellard return -ENOTSUP; 1175faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1176faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1177faea38e7Sbellard } 1178faea38e7Sbellard 1179faea38e7Sbellard /**************************************************************/ 1180faea38e7Sbellard /* handling of snapshots */ 1181faea38e7Sbellard 1182faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1183faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1184faea38e7Sbellard { 1185faea38e7Sbellard BlockDriver *drv = bs->drv; 1186faea38e7Sbellard if (!drv) 118719cb3738Sbellard return -ENOMEDIUM; 1188faea38e7Sbellard if (!drv->bdrv_snapshot_create) 1189faea38e7Sbellard return -ENOTSUP; 1190faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 1191faea38e7Sbellard } 1192faea38e7Sbellard 1193faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1194faea38e7Sbellard const char *snapshot_id) 1195faea38e7Sbellard { 1196faea38e7Sbellard BlockDriver *drv = bs->drv; 1197faea38e7Sbellard if (!drv) 119819cb3738Sbellard return -ENOMEDIUM; 1199faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 1200faea38e7Sbellard return -ENOTSUP; 1201faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 1202faea38e7Sbellard } 1203faea38e7Sbellard 1204faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1205faea38e7Sbellard { 1206faea38e7Sbellard BlockDriver *drv = bs->drv; 1207faea38e7Sbellard if (!drv) 120819cb3738Sbellard return -ENOMEDIUM; 1209faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 1210faea38e7Sbellard return -ENOTSUP; 1211faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 1212faea38e7Sbellard } 1213faea38e7Sbellard 1214faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1215faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1216faea38e7Sbellard { 1217faea38e7Sbellard BlockDriver *drv = bs->drv; 1218faea38e7Sbellard if (!drv) 121919cb3738Sbellard return -ENOMEDIUM; 1220faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1221faea38e7Sbellard return -ENOTSUP; 1222faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1223faea38e7Sbellard } 1224faea38e7Sbellard 1225faea38e7Sbellard #define NB_SUFFIXES 4 1226faea38e7Sbellard 1227faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1228faea38e7Sbellard { 1229faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1230faea38e7Sbellard int64_t base; 1231faea38e7Sbellard int i; 1232faea38e7Sbellard 1233faea38e7Sbellard if (size <= 999) { 1234faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1235faea38e7Sbellard } else { 1236faea38e7Sbellard base = 1024; 1237faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1238faea38e7Sbellard if (size < (10 * base)) { 1239faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1240faea38e7Sbellard (double)size / base, 1241faea38e7Sbellard suffixes[i]); 1242faea38e7Sbellard break; 1243faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1244faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1245faea38e7Sbellard ((size + (base >> 1)) / base), 1246faea38e7Sbellard suffixes[i]); 1247faea38e7Sbellard break; 1248faea38e7Sbellard } 1249faea38e7Sbellard base = base * 1024; 1250faea38e7Sbellard } 1251faea38e7Sbellard } 1252faea38e7Sbellard return buf; 1253faea38e7Sbellard } 1254faea38e7Sbellard 1255faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1256faea38e7Sbellard { 1257faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 12583b9f94e1Sbellard #ifdef _WIN32 12593b9f94e1Sbellard struct tm *ptm; 12603b9f94e1Sbellard #else 1261faea38e7Sbellard struct tm tm; 12623b9f94e1Sbellard #endif 1263faea38e7Sbellard time_t ti; 1264faea38e7Sbellard int64_t secs; 1265faea38e7Sbellard 1266faea38e7Sbellard if (!sn) { 1267faea38e7Sbellard snprintf(buf, buf_size, 1268faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1269faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1270faea38e7Sbellard } else { 1271faea38e7Sbellard ti = sn->date_sec; 12723b9f94e1Sbellard #ifdef _WIN32 12733b9f94e1Sbellard ptm = localtime(&ti); 12743b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 12753b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 12763b9f94e1Sbellard #else 1277faea38e7Sbellard localtime_r(&ti, &tm); 1278faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1279faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 12803b9f94e1Sbellard #endif 1281faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1282faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1283faea38e7Sbellard "%02d:%02d:%02d.%03d", 1284faea38e7Sbellard (int)(secs / 3600), 1285faea38e7Sbellard (int)((secs / 60) % 60), 1286faea38e7Sbellard (int)(secs % 60), 1287faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1288faea38e7Sbellard snprintf(buf, buf_size, 1289faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1290faea38e7Sbellard sn->id_str, sn->name, 1291faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1292faea38e7Sbellard date_buf, 1293faea38e7Sbellard clock_buf); 1294faea38e7Sbellard } 1295faea38e7Sbellard return buf; 1296faea38e7Sbellard } 1297faea38e7Sbellard 129883f64091Sbellard 1299ea2384d3Sbellard /**************************************************************/ 130083f64091Sbellard /* async I/Os */ 1301ea2384d3Sbellard 13023b69e4b9Saliguori typedef struct VectorTranslationState { 13033b69e4b9Saliguori QEMUIOVector *iov; 13043b69e4b9Saliguori uint8_t *bounce; 13053b69e4b9Saliguori int is_write; 13063b69e4b9Saliguori BlockDriverAIOCB *aiocb; 13073b69e4b9Saliguori BlockDriverAIOCB *this_aiocb; 13083b69e4b9Saliguori } VectorTranslationState; 13093b69e4b9Saliguori 13103b69e4b9Saliguori static void bdrv_aio_rw_vector_cb(void *opaque, int ret) 13113b69e4b9Saliguori { 13123b69e4b9Saliguori VectorTranslationState *s = opaque; 13133b69e4b9Saliguori 13143b69e4b9Saliguori if (!s->is_write) { 1315249aa745Saliguori qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size); 13163b69e4b9Saliguori } 1317d905dba4Saurel32 qemu_vfree(s->bounce); 13183b69e4b9Saliguori s->this_aiocb->cb(s->this_aiocb->opaque, ret); 13193b69e4b9Saliguori qemu_aio_release(s->this_aiocb); 13203b69e4b9Saliguori } 13213b69e4b9Saliguori 13223b69e4b9Saliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 13233b69e4b9Saliguori int64_t sector_num, 13243b69e4b9Saliguori QEMUIOVector *iov, 13253b69e4b9Saliguori int nb_sectors, 13263b69e4b9Saliguori BlockDriverCompletionFunc *cb, 13273b69e4b9Saliguori void *opaque, 13283b69e4b9Saliguori int is_write) 13293b69e4b9Saliguori 13303b69e4b9Saliguori { 13313b69e4b9Saliguori VectorTranslationState *s = qemu_mallocz(sizeof(*s)); 13323b69e4b9Saliguori BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque); 13333b69e4b9Saliguori 13343b69e4b9Saliguori s->this_aiocb = aiocb; 13353b69e4b9Saliguori s->iov = iov; 13363b69e4b9Saliguori s->bounce = qemu_memalign(512, nb_sectors * 512); 13373b69e4b9Saliguori s->is_write = is_write; 13383b69e4b9Saliguori if (is_write) { 13393b69e4b9Saliguori qemu_iovec_to_buffer(s->iov, s->bounce); 13403b69e4b9Saliguori s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors, 13413b69e4b9Saliguori bdrv_aio_rw_vector_cb, s); 13423b69e4b9Saliguori } else { 13433b69e4b9Saliguori s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors, 13443b69e4b9Saliguori bdrv_aio_rw_vector_cb, s); 13453b69e4b9Saliguori } 13463b69e4b9Saliguori return aiocb; 13473b69e4b9Saliguori } 13483b69e4b9Saliguori 13493b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 13503b69e4b9Saliguori QEMUIOVector *iov, int nb_sectors, 13513b69e4b9Saliguori BlockDriverCompletionFunc *cb, void *opaque) 13523b69e4b9Saliguori { 135371d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 135471d0770cSaliguori return NULL; 135571d0770cSaliguori 13563b69e4b9Saliguori return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, 13573b69e4b9Saliguori cb, opaque, 0); 13583b69e4b9Saliguori } 13593b69e4b9Saliguori 13603b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 13613b69e4b9Saliguori QEMUIOVector *iov, int nb_sectors, 13623b69e4b9Saliguori BlockDriverCompletionFunc *cb, void *opaque) 13633b69e4b9Saliguori { 136471d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 136571d0770cSaliguori return NULL; 136671d0770cSaliguori 13673b69e4b9Saliguori return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, 13683b69e4b9Saliguori cb, opaque, 1); 13693b69e4b9Saliguori } 13703b69e4b9Saliguori 1371ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, 137283f64091Sbellard uint8_t *buf, int nb_sectors, 137383f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1374ea2384d3Sbellard { 137583f64091Sbellard BlockDriver *drv = bs->drv; 1376a36e69ddSths BlockDriverAIOCB *ret; 1377ea2384d3Sbellard 137819cb3738Sbellard if (!drv) 1379ce1a14dcSpbrook return NULL; 138071d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 138171d0770cSaliguori return NULL; 138283f64091Sbellard 1383a36e69ddSths ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); 1384a36e69ddSths 1385a36e69ddSths if (ret) { 1386a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1387a36e69ddSths bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1388a36e69ddSths bs->rd_ops ++; 1389a36e69ddSths } 1390a36e69ddSths 1391a36e69ddSths return ret; 139283f64091Sbellard } 139383f64091Sbellard 1394ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, 139583f64091Sbellard const uint8_t *buf, int nb_sectors, 139683f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 13977674e7bfSbellard { 139883f64091Sbellard BlockDriver *drv = bs->drv; 1399a36e69ddSths BlockDriverAIOCB *ret; 140083f64091Sbellard 140119cb3738Sbellard if (!drv) 1402ce1a14dcSpbrook return NULL; 140383f64091Sbellard if (bs->read_only) 1404ce1a14dcSpbrook return NULL; 140571d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 140671d0770cSaliguori return NULL; 140783f64091Sbellard 1408a36e69ddSths ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); 1409a36e69ddSths 1410a36e69ddSths if (ret) { 1411a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1412a36e69ddSths bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1413a36e69ddSths bs->wr_ops ++; 1414a36e69ddSths } 1415a36e69ddSths 1416a36e69ddSths return ret; 141783f64091Sbellard } 141883f64091Sbellard 141983f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 142083f64091Sbellard { 1421ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 142283f64091Sbellard 14233b69e4b9Saliguori if (acb->cb == bdrv_aio_rw_vector_cb) { 14243b69e4b9Saliguori VectorTranslationState *s = acb->opaque; 14253b69e4b9Saliguori acb = s->aiocb; 14263b69e4b9Saliguori } 14273b69e4b9Saliguori 142883f64091Sbellard drv->bdrv_aio_cancel(acb); 142983f64091Sbellard } 143083f64091Sbellard 143183f64091Sbellard 143283f64091Sbellard /**************************************************************/ 143383f64091Sbellard /* async block device emulation */ 143483f64091Sbellard 143583f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 1436beac80cdSbellard { 1437ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 1438ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 1439ce1a14dcSpbrook qemu_aio_release(acb); 1440beac80cdSbellard } 1441beac80cdSbellard 1442ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 1443ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 1444ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 1445ea2384d3Sbellard { 1446ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 144783f64091Sbellard int ret; 144883f64091Sbellard 1449ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1450ce1a14dcSpbrook if (!acb->bh) 1451ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1452ce1a14dcSpbrook ret = bdrv_read(bs, sector_num, buf, nb_sectors); 1453ce1a14dcSpbrook acb->ret = ret; 1454ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1455ce1a14dcSpbrook return &acb->common; 14567a6cba61Spbrook } 14577a6cba61Spbrook 1458ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 1459ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 1460ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 146183f64091Sbellard { 1462ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 146383f64091Sbellard int ret; 146483f64091Sbellard 1465ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1466ce1a14dcSpbrook if (!acb->bh) 1467ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1468ce1a14dcSpbrook ret = bdrv_write(bs, sector_num, buf, nb_sectors); 1469ce1a14dcSpbrook acb->ret = ret; 1470ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1471ce1a14dcSpbrook return &acb->common; 147283f64091Sbellard } 147383f64091Sbellard 1474ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 147583f64091Sbellard { 1476ce1a14dcSpbrook BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; 1477ce1a14dcSpbrook qemu_bh_cancel(acb->bh); 1478ce1a14dcSpbrook qemu_aio_release(acb); 147983f64091Sbellard } 148083f64091Sbellard 148183f64091Sbellard /**************************************************************/ 148283f64091Sbellard /* sync block device emulation */ 148383f64091Sbellard 148483f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 148583f64091Sbellard { 148683f64091Sbellard *(int *)opaque = ret; 148783f64091Sbellard } 148883f64091Sbellard 148983f64091Sbellard #define NOT_DONE 0x7fffffff 149083f64091Sbellard 149183f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 149283f64091Sbellard uint8_t *buf, int nb_sectors) 149383f64091Sbellard { 1494ce1a14dcSpbrook int async_ret; 1495ce1a14dcSpbrook BlockDriverAIOCB *acb; 149683f64091Sbellard 149783f64091Sbellard async_ret = NOT_DONE; 1498ce1a14dcSpbrook acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, 149983f64091Sbellard bdrv_rw_em_cb, &async_ret); 1500baf35cb9Saliguori if (acb == NULL) 1501ce1a14dcSpbrook return -1; 1502baf35cb9Saliguori 150383f64091Sbellard while (async_ret == NOT_DONE) { 150483f64091Sbellard qemu_aio_wait(); 150583f64091Sbellard } 1506baf35cb9Saliguori 150783f64091Sbellard return async_ret; 150883f64091Sbellard } 150983f64091Sbellard 151083f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 151183f64091Sbellard const uint8_t *buf, int nb_sectors) 151283f64091Sbellard { 1513ce1a14dcSpbrook int async_ret; 1514ce1a14dcSpbrook BlockDriverAIOCB *acb; 151583f64091Sbellard 151683f64091Sbellard async_ret = NOT_DONE; 1517ce1a14dcSpbrook acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, 151883f64091Sbellard bdrv_rw_em_cb, &async_ret); 1519baf35cb9Saliguori if (acb == NULL) 1520ce1a14dcSpbrook return -1; 152183f64091Sbellard while (async_ret == NOT_DONE) { 152283f64091Sbellard qemu_aio_wait(); 152383f64091Sbellard } 152483f64091Sbellard return async_ret; 152583f64091Sbellard } 1526ea2384d3Sbellard 1527ea2384d3Sbellard void bdrv_init(void) 1528ea2384d3Sbellard { 1529ea2384d3Sbellard bdrv_register(&bdrv_raw); 153019cb3738Sbellard bdrv_register(&bdrv_host_device); 1531ea2384d3Sbellard #ifndef _WIN32 1532ea2384d3Sbellard bdrv_register(&bdrv_cow); 1533ea2384d3Sbellard #endif 1534ea2384d3Sbellard bdrv_register(&bdrv_qcow); 1535ea2384d3Sbellard bdrv_register(&bdrv_vmdk); 15363c56521bSbellard bdrv_register(&bdrv_cloop); 1537585d0ed9Sbellard bdrv_register(&bdrv_dmg); 1538a8753c34Sbellard bdrv_register(&bdrv_bochs); 15396a0f9e82Sbellard bdrv_register(&bdrv_vpc); 1540712e7874Sbellard bdrv_register(&bdrv_vvfat); 1541faea38e7Sbellard bdrv_register(&bdrv_qcow2); 15426ada7453Sths bdrv_register(&bdrv_parallels); 154375818250Sths bdrv_register(&bdrv_nbd); 1544ea2384d3Sbellard } 1545ce1a14dcSpbrook 1546ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, 1547ce1a14dcSpbrook void *opaque) 1548ce1a14dcSpbrook { 1549ce1a14dcSpbrook BlockDriver *drv; 1550ce1a14dcSpbrook BlockDriverAIOCB *acb; 1551ce1a14dcSpbrook 1552ce1a14dcSpbrook drv = bs->drv; 1553ce1a14dcSpbrook if (drv->free_aiocb) { 1554ce1a14dcSpbrook acb = drv->free_aiocb; 1555ce1a14dcSpbrook drv->free_aiocb = acb->next; 1556ce1a14dcSpbrook } else { 1557ce1a14dcSpbrook acb = qemu_mallocz(drv->aiocb_size); 1558ce1a14dcSpbrook } 1559ce1a14dcSpbrook acb->bs = bs; 1560ce1a14dcSpbrook acb->cb = cb; 1561ce1a14dcSpbrook acb->opaque = opaque; 1562ce1a14dcSpbrook return acb; 1563ce1a14dcSpbrook } 1564ce1a14dcSpbrook 1565ce1a14dcSpbrook void qemu_aio_release(void *p) 1566ce1a14dcSpbrook { 1567ce1a14dcSpbrook BlockDriverAIOCB *acb = p; 1568ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 1569ce1a14dcSpbrook acb->next = drv->free_aiocb; 1570ce1a14dcSpbrook drv->free_aiocb = acb; 1571ce1a14dcSpbrook } 157219cb3738Sbellard 157319cb3738Sbellard /**************************************************************/ 157419cb3738Sbellard /* removable device support */ 157519cb3738Sbellard 157619cb3738Sbellard /** 157719cb3738Sbellard * Return TRUE if the media is present 157819cb3738Sbellard */ 157919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 158019cb3738Sbellard { 158119cb3738Sbellard BlockDriver *drv = bs->drv; 158219cb3738Sbellard int ret; 158319cb3738Sbellard if (!drv) 158419cb3738Sbellard return 0; 158519cb3738Sbellard if (!drv->bdrv_is_inserted) 158619cb3738Sbellard return 1; 158719cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 158819cb3738Sbellard return ret; 158919cb3738Sbellard } 159019cb3738Sbellard 159119cb3738Sbellard /** 159219cb3738Sbellard * Return TRUE if the media changed since the last call to this 159319cb3738Sbellard * function. It is currently only used for floppy disks 159419cb3738Sbellard */ 159519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 159619cb3738Sbellard { 159719cb3738Sbellard BlockDriver *drv = bs->drv; 159819cb3738Sbellard int ret; 159919cb3738Sbellard 160019cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 160119cb3738Sbellard ret = -ENOTSUP; 160219cb3738Sbellard else 160319cb3738Sbellard ret = drv->bdrv_media_changed(bs); 160419cb3738Sbellard if (ret == -ENOTSUP) 160519cb3738Sbellard ret = bs->media_changed; 160619cb3738Sbellard bs->media_changed = 0; 160719cb3738Sbellard return ret; 160819cb3738Sbellard } 160919cb3738Sbellard 161019cb3738Sbellard /** 161119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 161219cb3738Sbellard */ 161319cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag) 161419cb3738Sbellard { 161519cb3738Sbellard BlockDriver *drv = bs->drv; 161619cb3738Sbellard int ret; 161719cb3738Sbellard 161819cb3738Sbellard if (!drv || !drv->bdrv_eject) { 161919cb3738Sbellard ret = -ENOTSUP; 162019cb3738Sbellard } else { 162119cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 162219cb3738Sbellard } 162319cb3738Sbellard if (ret == -ENOTSUP) { 162419cb3738Sbellard if (eject_flag) 162519cb3738Sbellard bdrv_close(bs); 162619cb3738Sbellard } 162719cb3738Sbellard } 162819cb3738Sbellard 162919cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 163019cb3738Sbellard { 163119cb3738Sbellard return bs->locked; 163219cb3738Sbellard } 163319cb3738Sbellard 163419cb3738Sbellard /** 163519cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 163619cb3738Sbellard * to eject it manually). 163719cb3738Sbellard */ 163819cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 163919cb3738Sbellard { 164019cb3738Sbellard BlockDriver *drv = bs->drv; 164119cb3738Sbellard 164219cb3738Sbellard bs->locked = locked; 164319cb3738Sbellard if (drv && drv->bdrv_set_locked) { 164419cb3738Sbellard drv->bdrv_set_locked(bs, locked); 164519cb3738Sbellard } 164619cb3738Sbellard } 1647985a03b0Sths 1648985a03b0Sths /* needed for generic scsi interface */ 1649985a03b0Sths 1650985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 1651985a03b0Sths { 1652985a03b0Sths BlockDriver *drv = bs->drv; 1653985a03b0Sths 1654985a03b0Sths if (drv && drv->bdrv_ioctl) 1655985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 1656985a03b0Sths return -ENOTSUP; 1657985a03b0Sths } 1658