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 if(!bs) 155b338082bSbellard return NULL; 156b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 157ea2384d3Sbellard if (device_name[0] != '\0') { 158b338082bSbellard /* insert at the end */ 159b338082bSbellard pbs = &bdrv_first; 160b338082bSbellard while (*pbs != NULL) 161b338082bSbellard pbs = &(*pbs)->next; 162b338082bSbellard *pbs = bs; 163ea2384d3Sbellard } 164b338082bSbellard return bs; 165b338082bSbellard } 166b338082bSbellard 167ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 168ea2384d3Sbellard { 169ea2384d3Sbellard BlockDriver *drv1; 170ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 171ea2384d3Sbellard if (!strcmp(drv1->format_name, format_name)) 172ea2384d3Sbellard return drv1; 173ea2384d3Sbellard } 174ea2384d3Sbellard return NULL; 175ea2384d3Sbellard } 176ea2384d3Sbellard 177ea2384d3Sbellard int bdrv_create(BlockDriver *drv, 178ea2384d3Sbellard const char *filename, int64_t size_in_sectors, 179ea2384d3Sbellard const char *backing_file, int flags) 180ea2384d3Sbellard { 181ea2384d3Sbellard if (!drv->bdrv_create) 182ea2384d3Sbellard return -ENOTSUP; 183ea2384d3Sbellard return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); 184ea2384d3Sbellard } 185ea2384d3Sbellard 186d5249393Sbellard #ifdef _WIN32 18795389c86Sbellard void get_tmp_filename(char *filename, int size) 188d5249393Sbellard { 1893b9f94e1Sbellard char temp_dir[MAX_PATH]; 1903b9f94e1Sbellard 1913b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 1923b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 193d5249393Sbellard } 194d5249393Sbellard #else 19595389c86Sbellard void get_tmp_filename(char *filename, int size) 196ea2384d3Sbellard { 197ea2384d3Sbellard int fd; 1987ccfb2ebSblueswir1 const char *tmpdir; 199d5249393Sbellard /* XXX: race condition possible */ 2000badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 2010badc1eeSaurel32 if (!tmpdir) 2020badc1eeSaurel32 tmpdir = "/tmp"; 2030badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 204ea2384d3Sbellard fd = mkstemp(filename); 205ea2384d3Sbellard close(fd); 206ea2384d3Sbellard } 207d5249393Sbellard #endif 208ea2384d3Sbellard 20919cb3738Sbellard #ifdef _WIN32 210f45512feSbellard static int is_windows_drive_prefix(const char *filename) 211f45512feSbellard { 212f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 213f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 214f45512feSbellard filename[1] == ':'); 215f45512feSbellard } 216f45512feSbellard 21719cb3738Sbellard static int is_windows_drive(const char *filename) 21819cb3738Sbellard { 219f45512feSbellard if (is_windows_drive_prefix(filename) && 220f45512feSbellard filename[2] == '\0') 22119cb3738Sbellard return 1; 22219cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 22319cb3738Sbellard strstart(filename, "//./", NULL)) 22419cb3738Sbellard return 1; 22519cb3738Sbellard return 0; 22619cb3738Sbellard } 22719cb3738Sbellard #endif 22819cb3738Sbellard 22983f64091Sbellard static BlockDriver *find_protocol(const char *filename) 23083f64091Sbellard { 23183f64091Sbellard BlockDriver *drv1; 23283f64091Sbellard char protocol[128]; 23383f64091Sbellard int len; 23483f64091Sbellard const char *p; 23519cb3738Sbellard 23619cb3738Sbellard #ifdef _WIN32 237f45512feSbellard if (is_windows_drive(filename) || 238f45512feSbellard is_windows_drive_prefix(filename)) 23919cb3738Sbellard return &bdrv_raw; 24019cb3738Sbellard #endif 24183f64091Sbellard p = strchr(filename, ':'); 24283f64091Sbellard if (!p) 24383f64091Sbellard return &bdrv_raw; 24483f64091Sbellard len = p - filename; 24583f64091Sbellard if (len > sizeof(protocol) - 1) 24683f64091Sbellard len = sizeof(protocol) - 1; 24783f64091Sbellard memcpy(protocol, filename, len); 24883f64091Sbellard protocol[len] = '\0'; 24983f64091Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 25083f64091Sbellard if (drv1->protocol_name && 25183f64091Sbellard !strcmp(drv1->protocol_name, protocol)) 25283f64091Sbellard return drv1; 25383f64091Sbellard } 25483f64091Sbellard return NULL; 25583f64091Sbellard } 25683f64091Sbellard 2577674e7bfSbellard /* XXX: force raw format if block or character device ? It would 2587674e7bfSbellard simplify the BSD case */ 259ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 260ea2384d3Sbellard { 26183f64091Sbellard int ret, score, score_max; 262ea2384d3Sbellard BlockDriver *drv1, *drv; 26383f64091Sbellard uint8_t buf[2048]; 26483f64091Sbellard BlockDriverState *bs; 265ea2384d3Sbellard 26619cb3738Sbellard /* detect host devices. By convention, /dev/cdrom[N] is always 26719cb3738Sbellard recognized as a host CDROM */ 26819cb3738Sbellard if (strstart(filename, "/dev/cdrom", NULL)) 26919cb3738Sbellard return &bdrv_host_device; 27019cb3738Sbellard #ifdef _WIN32 27119cb3738Sbellard if (is_windows_drive(filename)) 27219cb3738Sbellard return &bdrv_host_device; 27319cb3738Sbellard #else 27419cb3738Sbellard { 27519cb3738Sbellard struct stat st; 27619cb3738Sbellard if (stat(filename, &st) >= 0 && 27719cb3738Sbellard (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 27819cb3738Sbellard return &bdrv_host_device; 27919cb3738Sbellard } 28019cb3738Sbellard } 28119cb3738Sbellard #endif 28219cb3738Sbellard 28383f64091Sbellard drv = find_protocol(filename); 28419cb3738Sbellard /* no need to test disk image formats for vvfat */ 28583f64091Sbellard if (drv == &bdrv_vvfat) 28683f64091Sbellard return drv; 28783f64091Sbellard 28883f64091Sbellard ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); 28983f64091Sbellard if (ret < 0) 2907674e7bfSbellard return NULL; 29183f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 29283f64091Sbellard bdrv_delete(bs); 293ea2384d3Sbellard if (ret < 0) { 294ea2384d3Sbellard return NULL; 295ea2384d3Sbellard } 296ea2384d3Sbellard 297ea2384d3Sbellard score_max = 0; 298ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 29983f64091Sbellard if (drv1->bdrv_probe) { 300ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 301ea2384d3Sbellard if (score > score_max) { 302ea2384d3Sbellard score_max = score; 303ea2384d3Sbellard drv = drv1; 304ea2384d3Sbellard } 305ea2384d3Sbellard } 30683f64091Sbellard } 307ea2384d3Sbellard return drv; 308ea2384d3Sbellard } 309ea2384d3Sbellard 31083f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 311b338082bSbellard { 31283f64091Sbellard BlockDriverState *bs; 31383f64091Sbellard int ret; 3143b0d4f61Sbellard 31583f64091Sbellard bs = bdrv_new(""); 31683f64091Sbellard if (!bs) 31783f64091Sbellard return -ENOMEM; 31883f64091Sbellard ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); 31983f64091Sbellard if (ret < 0) { 32083f64091Sbellard bdrv_delete(bs); 32183f64091Sbellard return ret; 3223b0d4f61Sbellard } 32383f64091Sbellard *pbs = bs; 32483f64091Sbellard return 0; 3253b0d4f61Sbellard } 3263b0d4f61Sbellard 32783f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags) 32883f64091Sbellard { 32983f64091Sbellard return bdrv_open2(bs, filename, flags, NULL); 330ea2384d3Sbellard } 331ea2384d3Sbellard 33283f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 333ea2384d3Sbellard BlockDriver *drv) 334ea2384d3Sbellard { 33583f64091Sbellard int ret, open_flags; 336eb5c851fSths char tmp_filename[PATH_MAX]; 337eb5c851fSths char backing_filename[PATH_MAX]; 338fc01f7e7Sbellard 3390849bf08Sbellard bs->read_only = 0; 340ea2384d3Sbellard bs->is_temporary = 0; 341ea2384d3Sbellard bs->encrypted = 0; 34233e3963eSbellard 34383f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 344ea2384d3Sbellard BlockDriverState *bs1; 345ea2384d3Sbellard int64_t total_size; 3467c96d46eSaliguori int is_protocol = 0; 34733e3963eSbellard 348ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 349ea2384d3Sbellard instead of opening 'filename' directly */ 350ea2384d3Sbellard 351ea2384d3Sbellard /* if there is a backing file, use it */ 352ea2384d3Sbellard bs1 = bdrv_new(""); 353ea2384d3Sbellard if (!bs1) { 35483f64091Sbellard return -ENOMEM; 355ea2384d3Sbellard } 356ea2384d3Sbellard if (bdrv_open(bs1, filename, 0) < 0) { 357ea2384d3Sbellard bdrv_delete(bs1); 358ea2384d3Sbellard return -1; 359ea2384d3Sbellard } 36083f64091Sbellard total_size = bdrv_getlength(bs1) >> SECTOR_BITS; 3617c96d46eSaliguori 3627c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 3637c96d46eSaliguori is_protocol = 1; 3647c96d46eSaliguori 365ea2384d3Sbellard bdrv_delete(bs1); 366ea2384d3Sbellard 367ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 3687c96d46eSaliguori 3697c96d46eSaliguori /* Real path is meaningless for protocols */ 3707c96d46eSaliguori if (is_protocol) 3717c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 3727c96d46eSaliguori "%s", filename); 3737c96d46eSaliguori else 374a817d936Sbellard realpath(filename, backing_filename); 3757c96d46eSaliguori 376d15a771dSbellard if (bdrv_create(&bdrv_qcow2, tmp_filename, 377a817d936Sbellard total_size, backing_filename, 0) < 0) { 378ea2384d3Sbellard return -1; 379ea2384d3Sbellard } 380ea2384d3Sbellard filename = tmp_filename; 381ea2384d3Sbellard bs->is_temporary = 1; 382ea2384d3Sbellard } 383ea2384d3Sbellard 384ea2384d3Sbellard pstrcpy(bs->filename, sizeof(bs->filename), filename); 38583f64091Sbellard if (flags & BDRV_O_FILE) { 38683f64091Sbellard drv = find_protocol(filename); 38783f64091Sbellard if (!drv) 38883f64091Sbellard return -ENOENT; 38983f64091Sbellard } else { 390ea2384d3Sbellard if (!drv) { 391ea2384d3Sbellard drv = find_image_format(filename); 392ea2384d3Sbellard if (!drv) 393ea2384d3Sbellard return -1; 394ea2384d3Sbellard } 39583f64091Sbellard } 396ea2384d3Sbellard bs->drv = drv; 397ea2384d3Sbellard bs->opaque = qemu_mallocz(drv->instance_size); 398ea2384d3Sbellard if (bs->opaque == NULL && drv->instance_size > 0) 399ea2384d3Sbellard return -1; 40083f64091Sbellard /* Note: for compatibility, we open disk image files as RDWR, and 40183f64091Sbellard RDONLY as fallback */ 40283f64091Sbellard if (!(flags & BDRV_O_FILE)) 4039f7965c7Saliguori open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); 40483f64091Sbellard else 40583f64091Sbellard open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); 40683f64091Sbellard ret = drv->bdrv_open(bs, filename, open_flags); 407a0a83536Saurel32 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { 4089f7965c7Saliguori ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); 40983f64091Sbellard bs->read_only = 1; 41083f64091Sbellard } 411ea2384d3Sbellard if (ret < 0) { 412ea2384d3Sbellard qemu_free(bs->opaque); 4136b21b973Sbellard bs->opaque = NULL; 4146b21b973Sbellard bs->drv = NULL; 41583f64091Sbellard return ret; 416ea2384d3Sbellard } 417d15a771dSbellard if (drv->bdrv_getlength) { 418d15a771dSbellard bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 419d15a771dSbellard } 420ea2384d3Sbellard #ifndef _WIN32 421ea2384d3Sbellard if (bs->is_temporary) { 422ea2384d3Sbellard unlink(filename); 42333e3963eSbellard } 42467b915a5Sbellard #endif 42583f64091Sbellard if (bs->backing_file[0] != '\0') { 426ea2384d3Sbellard /* if there is a backing file, use it */ 427ea2384d3Sbellard bs->backing_hd = bdrv_new(""); 428ea2384d3Sbellard if (!bs->backing_hd) { 429ea2384d3Sbellard fail: 430ea2384d3Sbellard bdrv_close(bs); 4316b21b973Sbellard return -ENOMEM; 432ea2384d3Sbellard } 43383f64091Sbellard path_combine(backing_filename, sizeof(backing_filename), 43483f64091Sbellard filename, bs->backing_file); 4359f7965c7Saliguori if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 0) 436ea2384d3Sbellard goto fail; 437ea2384d3Sbellard } 43833e3963eSbellard 439b338082bSbellard /* call the change callback */ 44019cb3738Sbellard bs->media_changed = 1; 441b338082bSbellard if (bs->change_cb) 442b338082bSbellard bs->change_cb(bs->change_opaque); 443b338082bSbellard 444b338082bSbellard return 0; 445fc01f7e7Sbellard } 446fc01f7e7Sbellard 447fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 448fc01f7e7Sbellard { 44919cb3738Sbellard if (bs->drv) { 450ea2384d3Sbellard if (bs->backing_hd) 451ea2384d3Sbellard bdrv_delete(bs->backing_hd); 452ea2384d3Sbellard bs->drv->bdrv_close(bs); 453ea2384d3Sbellard qemu_free(bs->opaque); 454ea2384d3Sbellard #ifdef _WIN32 455ea2384d3Sbellard if (bs->is_temporary) { 456ea2384d3Sbellard unlink(bs->filename); 457ea2384d3Sbellard } 45867b915a5Sbellard #endif 459ea2384d3Sbellard bs->opaque = NULL; 460ea2384d3Sbellard bs->drv = NULL; 461b338082bSbellard 462b338082bSbellard /* call the change callback */ 46319cb3738Sbellard bs->media_changed = 1; 464b338082bSbellard if (bs->change_cb) 465b338082bSbellard bs->change_cb(bs->change_opaque); 466b338082bSbellard } 467b338082bSbellard } 468b338082bSbellard 469b338082bSbellard void bdrv_delete(BlockDriverState *bs) 470b338082bSbellard { 47134c6f050Saurel32 BlockDriverState **pbs; 47234c6f050Saurel32 47334c6f050Saurel32 pbs = &bdrv_first; 47434c6f050Saurel32 while (*pbs != bs && *pbs != NULL) 47534c6f050Saurel32 pbs = &(*pbs)->next; 47634c6f050Saurel32 if (*pbs == bs) 47734c6f050Saurel32 *pbs = bs->next; 47834c6f050Saurel32 479b338082bSbellard bdrv_close(bs); 480b338082bSbellard qemu_free(bs); 481fc01f7e7Sbellard } 482fc01f7e7Sbellard 48333e3963eSbellard /* commit COW file into the raw image */ 48433e3963eSbellard int bdrv_commit(BlockDriverState *bs) 48533e3963eSbellard { 48619cb3738Sbellard BlockDriver *drv = bs->drv; 48783f64091Sbellard int64_t i, total_sectors; 488ea2384d3Sbellard int n, j; 489ea2384d3Sbellard unsigned char sector[512]; 49033e3963eSbellard 49119cb3738Sbellard if (!drv) 49219cb3738Sbellard return -ENOMEDIUM; 49333e3963eSbellard 49433e3963eSbellard if (bs->read_only) { 495ea2384d3Sbellard return -EACCES; 49633e3963eSbellard } 49733e3963eSbellard 498ea2384d3Sbellard if (!bs->backing_hd) { 499ea2384d3Sbellard return -ENOTSUP; 500ea2384d3Sbellard } 501ea2384d3Sbellard 50283f64091Sbellard total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 50383f64091Sbellard for (i = 0; i < total_sectors;) { 50419cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 505ea2384d3Sbellard for(j = 0; j < n; j++) { 50633e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 507ea2384d3Sbellard return -EIO; 50833e3963eSbellard } 50933e3963eSbellard 510ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 511ea2384d3Sbellard return -EIO; 51233e3963eSbellard } 513ea2384d3Sbellard i++; 514ea2384d3Sbellard } 515ea2384d3Sbellard } else { 516ea2384d3Sbellard i += n; 51733e3963eSbellard } 51833e3963eSbellard } 51995389c86Sbellard 52019cb3738Sbellard if (drv->bdrv_make_empty) 52119cb3738Sbellard return drv->bdrv_make_empty(bs); 52295389c86Sbellard 52333e3963eSbellard return 0; 52433e3963eSbellard } 52533e3963eSbellard 52619cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 527fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 528fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 529fc01f7e7Sbellard { 530ea2384d3Sbellard BlockDriver *drv = bs->drv; 531fc01f7e7Sbellard 53219cb3738Sbellard if (!drv) 53319cb3738Sbellard return -ENOMEDIUM; 534b338082bSbellard 53583f64091Sbellard if (drv->bdrv_pread) { 53683f64091Sbellard int ret, len; 53783f64091Sbellard len = nb_sectors * 512; 53883f64091Sbellard ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); 53983f64091Sbellard if (ret < 0) 54083f64091Sbellard return ret; 54183f64091Sbellard else if (ret != len) 54219cb3738Sbellard return -EINVAL; 543a36e69ddSths else { 544a36e69ddSths bs->rd_bytes += (unsigned) len; 545a36e69ddSths bs->rd_ops ++; 54683f64091Sbellard return 0; 547a36e69ddSths } 54883f64091Sbellard } else { 54983f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 55083f64091Sbellard } 55183f64091Sbellard } 552fc01f7e7Sbellard 55319cb3738Sbellard /* Return < 0 if error. Important errors are: 55419cb3738Sbellard -EIO generic I/O error (may happen for all errors) 55519cb3738Sbellard -ENOMEDIUM No media inserted. 55619cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 55719cb3738Sbellard -EACCES Trying to write a read-only device 55819cb3738Sbellard */ 559fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 560fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 561fc01f7e7Sbellard { 56283f64091Sbellard BlockDriver *drv = bs->drv; 56319cb3738Sbellard if (!bs->drv) 56419cb3738Sbellard return -ENOMEDIUM; 5650849bf08Sbellard if (bs->read_only) 56619cb3738Sbellard return -EACCES; 56783f64091Sbellard if (drv->bdrv_pwrite) { 56883f64091Sbellard int ret, len; 56983f64091Sbellard len = nb_sectors * 512; 57083f64091Sbellard ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len); 57183f64091Sbellard if (ret < 0) 57283f64091Sbellard return ret; 57383f64091Sbellard else if (ret != len) 57483f64091Sbellard return -EIO; 575a36e69ddSths else { 576a36e69ddSths bs->wr_bytes += (unsigned) len; 577a36e69ddSths bs->wr_ops ++; 57883f64091Sbellard return 0; 579a36e69ddSths } 58083f64091Sbellard } else { 58183f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 58283f64091Sbellard } 58383f64091Sbellard } 58483f64091Sbellard 58583f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, 586faea38e7Sbellard uint8_t *buf, int count1) 58783f64091Sbellard { 58883f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 58983f64091Sbellard int len, nb_sectors, count; 59083f64091Sbellard int64_t sector_num; 59183f64091Sbellard 59283f64091Sbellard count = count1; 59383f64091Sbellard /* first read to align to sector start */ 59483f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 59583f64091Sbellard if (len > count) 59683f64091Sbellard len = count; 59783f64091Sbellard sector_num = offset >> SECTOR_BITS; 59883f64091Sbellard if (len > 0) { 59983f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 60083f64091Sbellard return -EIO; 60183f64091Sbellard memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); 60283f64091Sbellard count -= len; 60383f64091Sbellard if (count == 0) 60483f64091Sbellard return count1; 60583f64091Sbellard sector_num++; 60683f64091Sbellard buf += len; 60783f64091Sbellard } 60883f64091Sbellard 60983f64091Sbellard /* read the sectors "in place" */ 61083f64091Sbellard nb_sectors = count >> SECTOR_BITS; 61183f64091Sbellard if (nb_sectors > 0) { 61283f64091Sbellard if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) 61383f64091Sbellard return -EIO; 61483f64091Sbellard sector_num += nb_sectors; 61583f64091Sbellard len = nb_sectors << SECTOR_BITS; 61683f64091Sbellard buf += len; 61783f64091Sbellard count -= len; 61883f64091Sbellard } 61983f64091Sbellard 62083f64091Sbellard /* add data from the last sector */ 62183f64091Sbellard if (count > 0) { 62283f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 62383f64091Sbellard return -EIO; 62483f64091Sbellard memcpy(buf, tmp_buf, count); 62583f64091Sbellard } 62683f64091Sbellard return count1; 62783f64091Sbellard } 62883f64091Sbellard 62983f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, 630faea38e7Sbellard const uint8_t *buf, int count1) 63183f64091Sbellard { 63283f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 63383f64091Sbellard int len, nb_sectors, count; 63483f64091Sbellard int64_t sector_num; 63583f64091Sbellard 63683f64091Sbellard count = count1; 63783f64091Sbellard /* first write to align to sector start */ 63883f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 63983f64091Sbellard if (len > count) 64083f64091Sbellard len = count; 64183f64091Sbellard sector_num = offset >> SECTOR_BITS; 64283f64091Sbellard if (len > 0) { 64383f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 64483f64091Sbellard return -EIO; 64583f64091Sbellard memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); 64683f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 64783f64091Sbellard return -EIO; 64883f64091Sbellard count -= len; 64983f64091Sbellard if (count == 0) 65083f64091Sbellard return count1; 65183f64091Sbellard sector_num++; 65283f64091Sbellard buf += len; 65383f64091Sbellard } 65483f64091Sbellard 65583f64091Sbellard /* write the sectors "in place" */ 65683f64091Sbellard nb_sectors = count >> SECTOR_BITS; 65783f64091Sbellard if (nb_sectors > 0) { 65883f64091Sbellard if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) 65983f64091Sbellard return -EIO; 66083f64091Sbellard sector_num += nb_sectors; 66183f64091Sbellard len = nb_sectors << SECTOR_BITS; 66283f64091Sbellard buf += len; 66383f64091Sbellard count -= len; 66483f64091Sbellard } 66583f64091Sbellard 66683f64091Sbellard /* add data from the last sector */ 66783f64091Sbellard if (count > 0) { 66883f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 66983f64091Sbellard return -EIO; 67083f64091Sbellard memcpy(tmp_buf, buf, count); 67183f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 67283f64091Sbellard return -EIO; 67383f64091Sbellard } 67483f64091Sbellard return count1; 67583f64091Sbellard } 67683f64091Sbellard 67783f64091Sbellard /** 67883f64091Sbellard * Read with byte offsets (needed only for file protocols) 67983f64091Sbellard */ 68083f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset, 68183f64091Sbellard void *buf1, int count1) 68283f64091Sbellard { 68383f64091Sbellard BlockDriver *drv = bs->drv; 68483f64091Sbellard 68583f64091Sbellard if (!drv) 68619cb3738Sbellard return -ENOMEDIUM; 68783f64091Sbellard if (!drv->bdrv_pread) 688faea38e7Sbellard return bdrv_pread_em(bs, offset, buf1, count1); 68983f64091Sbellard return drv->bdrv_pread(bs, offset, buf1, count1); 69083f64091Sbellard } 69183f64091Sbellard 69283f64091Sbellard /** 69383f64091Sbellard * Write with byte offsets (needed only for file protocols) 69483f64091Sbellard */ 69583f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 69683f64091Sbellard const void *buf1, int count1) 69783f64091Sbellard { 69883f64091Sbellard BlockDriver *drv = bs->drv; 69983f64091Sbellard 70083f64091Sbellard if (!drv) 70119cb3738Sbellard return -ENOMEDIUM; 70283f64091Sbellard if (!drv->bdrv_pwrite) 703faea38e7Sbellard return bdrv_pwrite_em(bs, offset, buf1, count1); 70483f64091Sbellard return drv->bdrv_pwrite(bs, offset, buf1, count1); 70583f64091Sbellard } 70683f64091Sbellard 70783f64091Sbellard /** 70883f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 70983f64091Sbellard */ 71083f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 71183f64091Sbellard { 71283f64091Sbellard BlockDriver *drv = bs->drv; 71383f64091Sbellard if (!drv) 71419cb3738Sbellard return -ENOMEDIUM; 71583f64091Sbellard if (!drv->bdrv_truncate) 71683f64091Sbellard return -ENOTSUP; 71783f64091Sbellard return drv->bdrv_truncate(bs, offset); 71883f64091Sbellard } 71983f64091Sbellard 72083f64091Sbellard /** 72183f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 72283f64091Sbellard */ 72383f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 72483f64091Sbellard { 72583f64091Sbellard BlockDriver *drv = bs->drv; 72683f64091Sbellard if (!drv) 72719cb3738Sbellard return -ENOMEDIUM; 72883f64091Sbellard if (!drv->bdrv_getlength) { 72983f64091Sbellard /* legacy mode */ 73083f64091Sbellard return bs->total_sectors * SECTOR_SIZE; 73183f64091Sbellard } 73283f64091Sbellard return drv->bdrv_getlength(bs); 733fc01f7e7Sbellard } 734fc01f7e7Sbellard 73519cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 73696b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 737fc01f7e7Sbellard { 73819cb3738Sbellard int64_t length; 73919cb3738Sbellard length = bdrv_getlength(bs); 74019cb3738Sbellard if (length < 0) 74119cb3738Sbellard length = 0; 74219cb3738Sbellard else 74319cb3738Sbellard length = length >> SECTOR_BITS; 74419cb3738Sbellard *nb_sectors_ptr = length; 745fc01f7e7Sbellard } 746cf98951bSbellard 747f3d54fc4Saliguori struct partition { 748f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 749f3d54fc4Saliguori uint8_t head; /* starting head */ 750f3d54fc4Saliguori uint8_t sector; /* starting sector */ 751f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 752f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 753f3d54fc4Saliguori uint8_t end_head; /* end head */ 754f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 755f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 756f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 757f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 758f3d54fc4Saliguori } __attribute__((packed)); 759f3d54fc4Saliguori 760f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 761f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 762f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 763f3d54fc4Saliguori { 764f3d54fc4Saliguori uint8_t buf[512]; 765f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 766f3d54fc4Saliguori struct partition *p; 767f3d54fc4Saliguori uint32_t nr_sects; 768*a38131b6Sblueswir1 uint64_t nb_sectors; 769f3d54fc4Saliguori 770f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 771f3d54fc4Saliguori 772f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 773f3d54fc4Saliguori if (ret < 0) 774f3d54fc4Saliguori return -1; 775f3d54fc4Saliguori /* test msdos magic */ 776f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 777f3d54fc4Saliguori return -1; 778f3d54fc4Saliguori for(i = 0; i < 4; i++) { 779f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 780f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 781f3d54fc4Saliguori if (nr_sects && p->end_head) { 782f3d54fc4Saliguori /* We make the assumption that the partition terminates on 783f3d54fc4Saliguori a cylinder boundary */ 784f3d54fc4Saliguori heads = p->end_head + 1; 785f3d54fc4Saliguori sectors = p->end_sector & 63; 786f3d54fc4Saliguori if (sectors == 0) 787f3d54fc4Saliguori continue; 788f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 789f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 790f3d54fc4Saliguori continue; 791f3d54fc4Saliguori *pheads = heads; 792f3d54fc4Saliguori *psectors = sectors; 793f3d54fc4Saliguori *pcylinders = cylinders; 794f3d54fc4Saliguori #if 0 795f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 796f3d54fc4Saliguori cylinders, heads, sectors); 797f3d54fc4Saliguori #endif 798f3d54fc4Saliguori return 0; 799f3d54fc4Saliguori } 800f3d54fc4Saliguori } 801f3d54fc4Saliguori return -1; 802f3d54fc4Saliguori } 803f3d54fc4Saliguori 804f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 805f3d54fc4Saliguori { 806f3d54fc4Saliguori int translation, lba_detected = 0; 807f3d54fc4Saliguori int cylinders, heads, secs; 808*a38131b6Sblueswir1 uint64_t nb_sectors; 809f3d54fc4Saliguori 810f3d54fc4Saliguori /* if a geometry hint is available, use it */ 811f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 812f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 813f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 814f3d54fc4Saliguori if (cylinders != 0) { 815f3d54fc4Saliguori *pcyls = cylinders; 816f3d54fc4Saliguori *pheads = heads; 817f3d54fc4Saliguori *psecs = secs; 818f3d54fc4Saliguori } else { 819f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 820f3d54fc4Saliguori if (heads > 16) { 821f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 822f3d54fc4Saliguori translation was active, so the default 823f3d54fc4Saliguori hardware geometry is OK */ 824f3d54fc4Saliguori lba_detected = 1; 825f3d54fc4Saliguori goto default_geometry; 826f3d54fc4Saliguori } else { 827f3d54fc4Saliguori *pcyls = cylinders; 828f3d54fc4Saliguori *pheads = heads; 829f3d54fc4Saliguori *psecs = secs; 830f3d54fc4Saliguori /* disable any translation to be in sync with 831f3d54fc4Saliguori the logical geometry */ 832f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 833f3d54fc4Saliguori bdrv_set_translation_hint(bs, 834f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 835f3d54fc4Saliguori } 836f3d54fc4Saliguori } 837f3d54fc4Saliguori } else { 838f3d54fc4Saliguori default_geometry: 839f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 840f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 841f3d54fc4Saliguori 842f3d54fc4Saliguori if (cylinders > 16383) 843f3d54fc4Saliguori cylinders = 16383; 844f3d54fc4Saliguori else if (cylinders < 2) 845f3d54fc4Saliguori cylinders = 2; 846f3d54fc4Saliguori *pcyls = cylinders; 847f3d54fc4Saliguori *pheads = 16; 848f3d54fc4Saliguori *psecs = 63; 849f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 850f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 851f3d54fc4Saliguori bdrv_set_translation_hint(bs, 852f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 853f3d54fc4Saliguori } else { 854f3d54fc4Saliguori bdrv_set_translation_hint(bs, 855f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 856f3d54fc4Saliguori } 857f3d54fc4Saliguori } 858f3d54fc4Saliguori } 859f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 860f3d54fc4Saliguori } 861f3d54fc4Saliguori } 862f3d54fc4Saliguori 863b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 864b338082bSbellard int cyls, int heads, int secs) 865b338082bSbellard { 866b338082bSbellard bs->cyls = cyls; 867b338082bSbellard bs->heads = heads; 868b338082bSbellard bs->secs = secs; 869b338082bSbellard } 870b338082bSbellard 871b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 872b338082bSbellard { 873b338082bSbellard bs->type = type; 874b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 875b338082bSbellard type == BDRV_TYPE_FLOPPY)); 876b338082bSbellard } 877b338082bSbellard 87846d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 87946d4767dSbellard { 88046d4767dSbellard bs->translation = translation; 88146d4767dSbellard } 88246d4767dSbellard 883b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 884b338082bSbellard int *pcyls, int *pheads, int *psecs) 885b338082bSbellard { 886b338082bSbellard *pcyls = bs->cyls; 887b338082bSbellard *pheads = bs->heads; 888b338082bSbellard *psecs = bs->secs; 889b338082bSbellard } 890b338082bSbellard 891b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 892b338082bSbellard { 893b338082bSbellard return bs->type; 894b338082bSbellard } 895b338082bSbellard 89646d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 89746d4767dSbellard { 89846d4767dSbellard return bs->translation; 89946d4767dSbellard } 90046d4767dSbellard 901b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 902b338082bSbellard { 903b338082bSbellard return bs->removable; 904b338082bSbellard } 905b338082bSbellard 906b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 907b338082bSbellard { 908b338082bSbellard return bs->read_only; 909b338082bSbellard } 910b338082bSbellard 911985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 912985a03b0Sths { 913985a03b0Sths return bs->sg; 914985a03b0Sths } 915985a03b0Sths 91619cb3738Sbellard /* XXX: no longer used */ 917b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 918b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 919b338082bSbellard { 920b338082bSbellard bs->change_cb = change_cb; 921b338082bSbellard bs->change_opaque = opaque; 922b338082bSbellard } 923b338082bSbellard 924ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 925ea2384d3Sbellard { 926ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 927ea2384d3Sbellard return 1; 928ea2384d3Sbellard return bs->encrypted; 929ea2384d3Sbellard } 930ea2384d3Sbellard 931ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 932ea2384d3Sbellard { 933ea2384d3Sbellard int ret; 934ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 935ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 936ea2384d3Sbellard if (ret < 0) 937ea2384d3Sbellard return ret; 938ea2384d3Sbellard if (!bs->encrypted) 939ea2384d3Sbellard return 0; 940ea2384d3Sbellard } 941ea2384d3Sbellard if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) 942ea2384d3Sbellard return -1; 943ea2384d3Sbellard return bs->drv->bdrv_set_key(bs, key); 944ea2384d3Sbellard } 945ea2384d3Sbellard 946ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 947ea2384d3Sbellard { 94819cb3738Sbellard if (!bs->drv) { 949ea2384d3Sbellard buf[0] = '\0'; 950ea2384d3Sbellard } else { 951ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 952ea2384d3Sbellard } 953ea2384d3Sbellard } 954ea2384d3Sbellard 955ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 956ea2384d3Sbellard void *opaque) 957ea2384d3Sbellard { 958ea2384d3Sbellard BlockDriver *drv; 959ea2384d3Sbellard 960ea2384d3Sbellard for (drv = first_drv; drv != NULL; drv = drv->next) { 961ea2384d3Sbellard it(opaque, drv->format_name); 962ea2384d3Sbellard } 963ea2384d3Sbellard } 964ea2384d3Sbellard 965b338082bSbellard BlockDriverState *bdrv_find(const char *name) 966b338082bSbellard { 967b338082bSbellard BlockDriverState *bs; 968b338082bSbellard 969b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 970b338082bSbellard if (!strcmp(name, bs->device_name)) 971b338082bSbellard return bs; 972b338082bSbellard } 973b338082bSbellard return NULL; 974b338082bSbellard } 975b338082bSbellard 97681d0912dSbellard void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) 97781d0912dSbellard { 97881d0912dSbellard BlockDriverState *bs; 97981d0912dSbellard 98081d0912dSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 98181d0912dSbellard it(opaque, bs->device_name); 98281d0912dSbellard } 98381d0912dSbellard } 98481d0912dSbellard 985ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 986ea2384d3Sbellard { 987ea2384d3Sbellard return bs->device_name; 988ea2384d3Sbellard } 989ea2384d3Sbellard 9907a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 9917a6cba61Spbrook { 9927a6cba61Spbrook if (bs->drv->bdrv_flush) 9937a6cba61Spbrook bs->drv->bdrv_flush(bs); 9947a6cba61Spbrook if (bs->backing_hd) 9957a6cba61Spbrook bdrv_flush(bs->backing_hd); 9967a6cba61Spbrook } 9977a6cba61Spbrook 998c6ca28d6Saliguori void bdrv_flush_all(void) 999c6ca28d6Saliguori { 1000c6ca28d6Saliguori BlockDriverState *bs; 1001c6ca28d6Saliguori 1002c6ca28d6Saliguori for (bs = bdrv_first; bs != NULL; bs = bs->next) 1003c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 1004c6ca28d6Saliguori (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) 1005c6ca28d6Saliguori bdrv_flush(bs); 1006c6ca28d6Saliguori } 1007c6ca28d6Saliguori 1008f58c7b35Sths /* 1009f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1010f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1011f58c7b35Sths * hence all their sectors are reported as allocated. 1012f58c7b35Sths * 1013f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1014f58c7b35Sths * the specified sector) that are known to be in the same 1015f58c7b35Sths * allocated/unallocated state. 1016f58c7b35Sths * 1017f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1018f58c7b35Sths */ 1019f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1020f58c7b35Sths int *pnum) 1021f58c7b35Sths { 1022f58c7b35Sths int64_t n; 1023f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1024f58c7b35Sths if (sector_num >= bs->total_sectors) { 1025f58c7b35Sths *pnum = 0; 1026f58c7b35Sths return 0; 1027f58c7b35Sths } 1028f58c7b35Sths n = bs->total_sectors - sector_num; 1029f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1030f58c7b35Sths return 1; 1031f58c7b35Sths } 1032f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1033f58c7b35Sths } 1034f58c7b35Sths 1035b338082bSbellard void bdrv_info(void) 1036b338082bSbellard { 1037b338082bSbellard BlockDriverState *bs; 1038b338082bSbellard 1039b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1040b338082bSbellard term_printf("%s:", bs->device_name); 1041b338082bSbellard term_printf(" type="); 1042b338082bSbellard switch(bs->type) { 1043b338082bSbellard case BDRV_TYPE_HD: 1044b338082bSbellard term_printf("hd"); 1045b338082bSbellard break; 1046b338082bSbellard case BDRV_TYPE_CDROM: 1047b338082bSbellard term_printf("cdrom"); 1048b338082bSbellard break; 1049b338082bSbellard case BDRV_TYPE_FLOPPY: 1050b338082bSbellard term_printf("floppy"); 1051b338082bSbellard break; 1052b338082bSbellard } 1053b338082bSbellard term_printf(" removable=%d", bs->removable); 1054b338082bSbellard if (bs->removable) { 1055b338082bSbellard term_printf(" locked=%d", bs->locked); 1056b338082bSbellard } 105719cb3738Sbellard if (bs->drv) { 1058fef30743Sths term_printf(" file="); 1059fef30743Sths term_print_filename(bs->filename); 1060fef30743Sths if (bs->backing_file[0] != '\0') { 1061fef30743Sths term_printf(" backing_file="); 1062fef30743Sths term_print_filename(bs->backing_file); 1063fef30743Sths } 1064b338082bSbellard term_printf(" ro=%d", bs->read_only); 1065ea2384d3Sbellard term_printf(" drv=%s", bs->drv->format_name); 1066ea2384d3Sbellard if (bs->encrypted) 1067ea2384d3Sbellard term_printf(" encrypted"); 1068b338082bSbellard } else { 1069b338082bSbellard term_printf(" [not inserted]"); 1070b338082bSbellard } 1071b338082bSbellard term_printf("\n"); 1072b338082bSbellard } 1073b338082bSbellard } 1074a36e69ddSths 1075a36e69ddSths /* The "info blockstats" command. */ 1076a36e69ddSths void bdrv_info_stats (void) 1077a36e69ddSths { 1078a36e69ddSths BlockDriverState *bs; 1079a36e69ddSths 1080a36e69ddSths for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1081a36e69ddSths term_printf ("%s:" 1082a36e69ddSths " rd_bytes=%" PRIu64 1083a36e69ddSths " wr_bytes=%" PRIu64 1084a36e69ddSths " rd_operations=%" PRIu64 1085a36e69ddSths " wr_operations=%" PRIu64 1086a36e69ddSths "\n", 1087a36e69ddSths bs->device_name, 1088a36e69ddSths bs->rd_bytes, bs->wr_bytes, 1089a36e69ddSths bs->rd_ops, bs->wr_ops); 1090a36e69ddSths } 1091a36e69ddSths } 1092ea2384d3Sbellard 109383f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 109483f64091Sbellard char *filename, int filename_size) 109583f64091Sbellard { 109683f64091Sbellard if (!bs->backing_hd) { 109783f64091Sbellard pstrcpy(filename, filename_size, ""); 109883f64091Sbellard } else { 109983f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 110083f64091Sbellard } 110183f64091Sbellard } 110283f64091Sbellard 1103faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1104faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1105faea38e7Sbellard { 1106faea38e7Sbellard BlockDriver *drv = bs->drv; 1107faea38e7Sbellard if (!drv) 110819cb3738Sbellard return -ENOMEDIUM; 1109faea38e7Sbellard if (!drv->bdrv_write_compressed) 1110faea38e7Sbellard return -ENOTSUP; 1111faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1112faea38e7Sbellard } 1113faea38e7Sbellard 1114faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1115faea38e7Sbellard { 1116faea38e7Sbellard BlockDriver *drv = bs->drv; 1117faea38e7Sbellard if (!drv) 111819cb3738Sbellard return -ENOMEDIUM; 1119faea38e7Sbellard if (!drv->bdrv_get_info) 1120faea38e7Sbellard return -ENOTSUP; 1121faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1122faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1123faea38e7Sbellard } 1124faea38e7Sbellard 1125faea38e7Sbellard /**************************************************************/ 1126faea38e7Sbellard /* handling of snapshots */ 1127faea38e7Sbellard 1128faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1129faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1130faea38e7Sbellard { 1131faea38e7Sbellard BlockDriver *drv = bs->drv; 1132faea38e7Sbellard if (!drv) 113319cb3738Sbellard return -ENOMEDIUM; 1134faea38e7Sbellard if (!drv->bdrv_snapshot_create) 1135faea38e7Sbellard return -ENOTSUP; 1136faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 1137faea38e7Sbellard } 1138faea38e7Sbellard 1139faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1140faea38e7Sbellard const char *snapshot_id) 1141faea38e7Sbellard { 1142faea38e7Sbellard BlockDriver *drv = bs->drv; 1143faea38e7Sbellard if (!drv) 114419cb3738Sbellard return -ENOMEDIUM; 1145faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 1146faea38e7Sbellard return -ENOTSUP; 1147faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 1148faea38e7Sbellard } 1149faea38e7Sbellard 1150faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1151faea38e7Sbellard { 1152faea38e7Sbellard BlockDriver *drv = bs->drv; 1153faea38e7Sbellard if (!drv) 115419cb3738Sbellard return -ENOMEDIUM; 1155faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 1156faea38e7Sbellard return -ENOTSUP; 1157faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 1158faea38e7Sbellard } 1159faea38e7Sbellard 1160faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1161faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1162faea38e7Sbellard { 1163faea38e7Sbellard BlockDriver *drv = bs->drv; 1164faea38e7Sbellard if (!drv) 116519cb3738Sbellard return -ENOMEDIUM; 1166faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1167faea38e7Sbellard return -ENOTSUP; 1168faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1169faea38e7Sbellard } 1170faea38e7Sbellard 1171faea38e7Sbellard #define NB_SUFFIXES 4 1172faea38e7Sbellard 1173faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1174faea38e7Sbellard { 1175faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1176faea38e7Sbellard int64_t base; 1177faea38e7Sbellard int i; 1178faea38e7Sbellard 1179faea38e7Sbellard if (size <= 999) { 1180faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1181faea38e7Sbellard } else { 1182faea38e7Sbellard base = 1024; 1183faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1184faea38e7Sbellard if (size < (10 * base)) { 1185faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1186faea38e7Sbellard (double)size / base, 1187faea38e7Sbellard suffixes[i]); 1188faea38e7Sbellard break; 1189faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1190faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1191faea38e7Sbellard ((size + (base >> 1)) / base), 1192faea38e7Sbellard suffixes[i]); 1193faea38e7Sbellard break; 1194faea38e7Sbellard } 1195faea38e7Sbellard base = base * 1024; 1196faea38e7Sbellard } 1197faea38e7Sbellard } 1198faea38e7Sbellard return buf; 1199faea38e7Sbellard } 1200faea38e7Sbellard 1201faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1202faea38e7Sbellard { 1203faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 12043b9f94e1Sbellard #ifdef _WIN32 12053b9f94e1Sbellard struct tm *ptm; 12063b9f94e1Sbellard #else 1207faea38e7Sbellard struct tm tm; 12083b9f94e1Sbellard #endif 1209faea38e7Sbellard time_t ti; 1210faea38e7Sbellard int64_t secs; 1211faea38e7Sbellard 1212faea38e7Sbellard if (!sn) { 1213faea38e7Sbellard snprintf(buf, buf_size, 1214faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1215faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1216faea38e7Sbellard } else { 1217faea38e7Sbellard ti = sn->date_sec; 12183b9f94e1Sbellard #ifdef _WIN32 12193b9f94e1Sbellard ptm = localtime(&ti); 12203b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 12213b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 12223b9f94e1Sbellard #else 1223faea38e7Sbellard localtime_r(&ti, &tm); 1224faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1225faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 12263b9f94e1Sbellard #endif 1227faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1228faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1229faea38e7Sbellard "%02d:%02d:%02d.%03d", 1230faea38e7Sbellard (int)(secs / 3600), 1231faea38e7Sbellard (int)((secs / 60) % 60), 1232faea38e7Sbellard (int)(secs % 60), 1233faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1234faea38e7Sbellard snprintf(buf, buf_size, 1235faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1236faea38e7Sbellard sn->id_str, sn->name, 1237faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1238faea38e7Sbellard date_buf, 1239faea38e7Sbellard clock_buf); 1240faea38e7Sbellard } 1241faea38e7Sbellard return buf; 1242faea38e7Sbellard } 1243faea38e7Sbellard 124483f64091Sbellard 1245ea2384d3Sbellard /**************************************************************/ 124683f64091Sbellard /* async I/Os */ 1247ea2384d3Sbellard 1248ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, 124983f64091Sbellard uint8_t *buf, int nb_sectors, 125083f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1251ea2384d3Sbellard { 125283f64091Sbellard BlockDriver *drv = bs->drv; 1253a36e69ddSths BlockDriverAIOCB *ret; 1254ea2384d3Sbellard 125519cb3738Sbellard if (!drv) 1256ce1a14dcSpbrook return NULL; 125783f64091Sbellard 1258a36e69ddSths ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); 1259a36e69ddSths 1260a36e69ddSths if (ret) { 1261a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1262a36e69ddSths bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1263a36e69ddSths bs->rd_ops ++; 1264a36e69ddSths } 1265a36e69ddSths 1266a36e69ddSths return ret; 126783f64091Sbellard } 126883f64091Sbellard 1269ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, 127083f64091Sbellard const uint8_t *buf, int nb_sectors, 127183f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 12727674e7bfSbellard { 127383f64091Sbellard BlockDriver *drv = bs->drv; 1274a36e69ddSths BlockDriverAIOCB *ret; 127583f64091Sbellard 127619cb3738Sbellard if (!drv) 1277ce1a14dcSpbrook return NULL; 127883f64091Sbellard if (bs->read_only) 1279ce1a14dcSpbrook return NULL; 128083f64091Sbellard 1281a36e69ddSths ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); 1282a36e69ddSths 1283a36e69ddSths if (ret) { 1284a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1285a36e69ddSths bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1286a36e69ddSths bs->wr_ops ++; 1287a36e69ddSths } 1288a36e69ddSths 1289a36e69ddSths return ret; 129083f64091Sbellard } 129183f64091Sbellard 129283f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 129383f64091Sbellard { 1294ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 129583f64091Sbellard 129683f64091Sbellard drv->bdrv_aio_cancel(acb); 129783f64091Sbellard } 129883f64091Sbellard 129983f64091Sbellard 130083f64091Sbellard /**************************************************************/ 130183f64091Sbellard /* async block device emulation */ 130283f64091Sbellard 130383f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 1304beac80cdSbellard { 1305ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 1306ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 1307ce1a14dcSpbrook qemu_aio_release(acb); 1308beac80cdSbellard } 1309beac80cdSbellard 1310ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 1311ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 1312ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 1313ea2384d3Sbellard { 1314ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 131583f64091Sbellard int ret; 131683f64091Sbellard 1317ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1318ce1a14dcSpbrook if (!acb->bh) 1319ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1320ce1a14dcSpbrook ret = bdrv_read(bs, sector_num, buf, nb_sectors); 1321ce1a14dcSpbrook acb->ret = ret; 1322ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1323ce1a14dcSpbrook return &acb->common; 13247a6cba61Spbrook } 13257a6cba61Spbrook 1326ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 1327ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 1328ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 132983f64091Sbellard { 1330ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 133183f64091Sbellard int ret; 133283f64091Sbellard 1333ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1334ce1a14dcSpbrook if (!acb->bh) 1335ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1336ce1a14dcSpbrook ret = bdrv_write(bs, sector_num, buf, nb_sectors); 1337ce1a14dcSpbrook acb->ret = ret; 1338ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1339ce1a14dcSpbrook return &acb->common; 134083f64091Sbellard } 134183f64091Sbellard 1342ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 134383f64091Sbellard { 1344ce1a14dcSpbrook BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; 1345ce1a14dcSpbrook qemu_bh_cancel(acb->bh); 1346ce1a14dcSpbrook qemu_aio_release(acb); 134783f64091Sbellard } 134883f64091Sbellard 134983f64091Sbellard /**************************************************************/ 135083f64091Sbellard /* sync block device emulation */ 135183f64091Sbellard 135283f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 135383f64091Sbellard { 135483f64091Sbellard *(int *)opaque = ret; 135583f64091Sbellard } 135683f64091Sbellard 135783f64091Sbellard #define NOT_DONE 0x7fffffff 135883f64091Sbellard 135983f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 136083f64091Sbellard uint8_t *buf, int nb_sectors) 136183f64091Sbellard { 1362ce1a14dcSpbrook int async_ret; 1363ce1a14dcSpbrook BlockDriverAIOCB *acb; 136483f64091Sbellard 136583f64091Sbellard async_ret = NOT_DONE; 1366ce1a14dcSpbrook acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, 136783f64091Sbellard bdrv_rw_em_cb, &async_ret); 1368baf35cb9Saliguori if (acb == NULL) 1369ce1a14dcSpbrook return -1; 1370baf35cb9Saliguori 137183f64091Sbellard while (async_ret == NOT_DONE) { 137283f64091Sbellard qemu_aio_wait(); 137383f64091Sbellard } 1374baf35cb9Saliguori 137583f64091Sbellard return async_ret; 137683f64091Sbellard } 137783f64091Sbellard 137883f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 137983f64091Sbellard const uint8_t *buf, int nb_sectors) 138083f64091Sbellard { 1381ce1a14dcSpbrook int async_ret; 1382ce1a14dcSpbrook BlockDriverAIOCB *acb; 138383f64091Sbellard 138483f64091Sbellard async_ret = NOT_DONE; 1385ce1a14dcSpbrook acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, 138683f64091Sbellard bdrv_rw_em_cb, &async_ret); 1387baf35cb9Saliguori if (acb == NULL) 1388ce1a14dcSpbrook return -1; 138983f64091Sbellard while (async_ret == NOT_DONE) { 139083f64091Sbellard qemu_aio_wait(); 139183f64091Sbellard } 139283f64091Sbellard return async_ret; 139383f64091Sbellard } 1394ea2384d3Sbellard 1395ea2384d3Sbellard void bdrv_init(void) 1396ea2384d3Sbellard { 1397ea2384d3Sbellard bdrv_register(&bdrv_raw); 139819cb3738Sbellard bdrv_register(&bdrv_host_device); 1399ea2384d3Sbellard #ifndef _WIN32 1400ea2384d3Sbellard bdrv_register(&bdrv_cow); 1401ea2384d3Sbellard #endif 1402ea2384d3Sbellard bdrv_register(&bdrv_qcow); 1403ea2384d3Sbellard bdrv_register(&bdrv_vmdk); 14043c56521bSbellard bdrv_register(&bdrv_cloop); 1405585d0ed9Sbellard bdrv_register(&bdrv_dmg); 1406a8753c34Sbellard bdrv_register(&bdrv_bochs); 14076a0f9e82Sbellard bdrv_register(&bdrv_vpc); 1408712e7874Sbellard bdrv_register(&bdrv_vvfat); 1409faea38e7Sbellard bdrv_register(&bdrv_qcow2); 14106ada7453Sths bdrv_register(&bdrv_parallels); 141175818250Sths bdrv_register(&bdrv_nbd); 1412ea2384d3Sbellard } 1413ce1a14dcSpbrook 1414ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, 1415ce1a14dcSpbrook void *opaque) 1416ce1a14dcSpbrook { 1417ce1a14dcSpbrook BlockDriver *drv; 1418ce1a14dcSpbrook BlockDriverAIOCB *acb; 1419ce1a14dcSpbrook 1420ce1a14dcSpbrook drv = bs->drv; 1421ce1a14dcSpbrook if (drv->free_aiocb) { 1422ce1a14dcSpbrook acb = drv->free_aiocb; 1423ce1a14dcSpbrook drv->free_aiocb = acb->next; 1424ce1a14dcSpbrook } else { 1425ce1a14dcSpbrook acb = qemu_mallocz(drv->aiocb_size); 1426ce1a14dcSpbrook if (!acb) 1427ce1a14dcSpbrook return NULL; 1428ce1a14dcSpbrook } 1429ce1a14dcSpbrook acb->bs = bs; 1430ce1a14dcSpbrook acb->cb = cb; 1431ce1a14dcSpbrook acb->opaque = opaque; 1432ce1a14dcSpbrook return acb; 1433ce1a14dcSpbrook } 1434ce1a14dcSpbrook 1435ce1a14dcSpbrook void qemu_aio_release(void *p) 1436ce1a14dcSpbrook { 1437ce1a14dcSpbrook BlockDriverAIOCB *acb = p; 1438ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 1439ce1a14dcSpbrook acb->next = drv->free_aiocb; 1440ce1a14dcSpbrook drv->free_aiocb = acb; 1441ce1a14dcSpbrook } 144219cb3738Sbellard 144319cb3738Sbellard /**************************************************************/ 144419cb3738Sbellard /* removable device support */ 144519cb3738Sbellard 144619cb3738Sbellard /** 144719cb3738Sbellard * Return TRUE if the media is present 144819cb3738Sbellard */ 144919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 145019cb3738Sbellard { 145119cb3738Sbellard BlockDriver *drv = bs->drv; 145219cb3738Sbellard int ret; 145319cb3738Sbellard if (!drv) 145419cb3738Sbellard return 0; 145519cb3738Sbellard if (!drv->bdrv_is_inserted) 145619cb3738Sbellard return 1; 145719cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 145819cb3738Sbellard return ret; 145919cb3738Sbellard } 146019cb3738Sbellard 146119cb3738Sbellard /** 146219cb3738Sbellard * Return TRUE if the media changed since the last call to this 146319cb3738Sbellard * function. It is currently only used for floppy disks 146419cb3738Sbellard */ 146519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 146619cb3738Sbellard { 146719cb3738Sbellard BlockDriver *drv = bs->drv; 146819cb3738Sbellard int ret; 146919cb3738Sbellard 147019cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 147119cb3738Sbellard ret = -ENOTSUP; 147219cb3738Sbellard else 147319cb3738Sbellard ret = drv->bdrv_media_changed(bs); 147419cb3738Sbellard if (ret == -ENOTSUP) 147519cb3738Sbellard ret = bs->media_changed; 147619cb3738Sbellard bs->media_changed = 0; 147719cb3738Sbellard return ret; 147819cb3738Sbellard } 147919cb3738Sbellard 148019cb3738Sbellard /** 148119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 148219cb3738Sbellard */ 148319cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag) 148419cb3738Sbellard { 148519cb3738Sbellard BlockDriver *drv = bs->drv; 148619cb3738Sbellard int ret; 148719cb3738Sbellard 148819cb3738Sbellard if (!drv || !drv->bdrv_eject) { 148919cb3738Sbellard ret = -ENOTSUP; 149019cb3738Sbellard } else { 149119cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 149219cb3738Sbellard } 149319cb3738Sbellard if (ret == -ENOTSUP) { 149419cb3738Sbellard if (eject_flag) 149519cb3738Sbellard bdrv_close(bs); 149619cb3738Sbellard } 149719cb3738Sbellard } 149819cb3738Sbellard 149919cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 150019cb3738Sbellard { 150119cb3738Sbellard return bs->locked; 150219cb3738Sbellard } 150319cb3738Sbellard 150419cb3738Sbellard /** 150519cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 150619cb3738Sbellard * to eject it manually). 150719cb3738Sbellard */ 150819cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 150919cb3738Sbellard { 151019cb3738Sbellard BlockDriver *drv = bs->drv; 151119cb3738Sbellard 151219cb3738Sbellard bs->locked = locked; 151319cb3738Sbellard if (drv && drv->bdrv_set_locked) { 151419cb3738Sbellard drv->bdrv_set_locked(bs, locked); 151519cb3738Sbellard } 151619cb3738Sbellard } 1517985a03b0Sths 1518985a03b0Sths /* needed for generic scsi interface */ 1519985a03b0Sths 1520985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 1521985a03b0Sths { 1522985a03b0Sths BlockDriver *drv = bs->drv; 1523985a03b0Sths 1524985a03b0Sths if (drv && drv->bdrv_ioctl) 1525985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 1526985a03b0Sths return -ENOTSUP; 1527985a03b0Sths } 1528