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 if (!bs) 31583f64091Sbellard return -ENOMEM; 31683f64091Sbellard ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); 31783f64091Sbellard if (ret < 0) { 31883f64091Sbellard bdrv_delete(bs); 31983f64091Sbellard return ret; 3203b0d4f61Sbellard } 32183f64091Sbellard *pbs = bs; 32283f64091Sbellard return 0; 3233b0d4f61Sbellard } 3243b0d4f61Sbellard 32583f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags) 32683f64091Sbellard { 32783f64091Sbellard return bdrv_open2(bs, filename, flags, NULL); 328ea2384d3Sbellard } 329ea2384d3Sbellard 33083f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 331ea2384d3Sbellard BlockDriver *drv) 332ea2384d3Sbellard { 33383f64091Sbellard int ret, open_flags; 334eb5c851fSths char tmp_filename[PATH_MAX]; 335eb5c851fSths char backing_filename[PATH_MAX]; 336fc01f7e7Sbellard 3370849bf08Sbellard bs->read_only = 0; 338ea2384d3Sbellard bs->is_temporary = 0; 339ea2384d3Sbellard bs->encrypted = 0; 34033e3963eSbellard 34183f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 342ea2384d3Sbellard BlockDriverState *bs1; 343ea2384d3Sbellard int64_t total_size; 3447c96d46eSaliguori int is_protocol = 0; 34533e3963eSbellard 346ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 347ea2384d3Sbellard instead of opening 'filename' directly */ 348ea2384d3Sbellard 349ea2384d3Sbellard /* if there is a backing file, use it */ 350ea2384d3Sbellard bs1 = bdrv_new(""); 351ea2384d3Sbellard if (!bs1) { 35283f64091Sbellard return -ENOMEM; 353ea2384d3Sbellard } 354ea2384d3Sbellard if (bdrv_open(bs1, filename, 0) < 0) { 355ea2384d3Sbellard bdrv_delete(bs1); 356ea2384d3Sbellard return -1; 357ea2384d3Sbellard } 35883f64091Sbellard total_size = bdrv_getlength(bs1) >> SECTOR_BITS; 3597c96d46eSaliguori 3607c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 3617c96d46eSaliguori is_protocol = 1; 3627c96d46eSaliguori 363ea2384d3Sbellard bdrv_delete(bs1); 364ea2384d3Sbellard 365ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 3667c96d46eSaliguori 3677c96d46eSaliguori /* Real path is meaningless for protocols */ 3687c96d46eSaliguori if (is_protocol) 3697c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 3707c96d46eSaliguori "%s", filename); 3717c96d46eSaliguori else 372a817d936Sbellard realpath(filename, backing_filename); 3737c96d46eSaliguori 374d15a771dSbellard if (bdrv_create(&bdrv_qcow2, tmp_filename, 375a817d936Sbellard total_size, backing_filename, 0) < 0) { 376ea2384d3Sbellard return -1; 377ea2384d3Sbellard } 378ea2384d3Sbellard filename = tmp_filename; 379ea2384d3Sbellard bs->is_temporary = 1; 380ea2384d3Sbellard } 381ea2384d3Sbellard 382ea2384d3Sbellard pstrcpy(bs->filename, sizeof(bs->filename), filename); 38383f64091Sbellard if (flags & BDRV_O_FILE) { 38483f64091Sbellard drv = find_protocol(filename); 38583f64091Sbellard if (!drv) 38683f64091Sbellard return -ENOENT; 38783f64091Sbellard } else { 388ea2384d3Sbellard if (!drv) { 389ea2384d3Sbellard drv = find_image_format(filename); 390ea2384d3Sbellard if (!drv) 391ea2384d3Sbellard return -1; 392ea2384d3Sbellard } 39383f64091Sbellard } 394ea2384d3Sbellard bs->drv = drv; 395ea2384d3Sbellard bs->opaque = qemu_mallocz(drv->instance_size); 39683f64091Sbellard /* Note: for compatibility, we open disk image files as RDWR, and 39783f64091Sbellard RDONLY as fallback */ 39883f64091Sbellard if (!(flags & BDRV_O_FILE)) 3999f7965c7Saliguori open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); 40083f64091Sbellard else 40183f64091Sbellard open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); 40283f64091Sbellard ret = drv->bdrv_open(bs, filename, open_flags); 403a0a83536Saurel32 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { 4049f7965c7Saliguori ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); 40583f64091Sbellard bs->read_only = 1; 40683f64091Sbellard } 407ea2384d3Sbellard if (ret < 0) { 408ea2384d3Sbellard qemu_free(bs->opaque); 4096b21b973Sbellard bs->opaque = NULL; 4106b21b973Sbellard bs->drv = NULL; 41183f64091Sbellard return ret; 412ea2384d3Sbellard } 413d15a771dSbellard if (drv->bdrv_getlength) { 414d15a771dSbellard bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 415d15a771dSbellard } 416ea2384d3Sbellard #ifndef _WIN32 417ea2384d3Sbellard if (bs->is_temporary) { 418ea2384d3Sbellard unlink(filename); 41933e3963eSbellard } 42067b915a5Sbellard #endif 42183f64091Sbellard if (bs->backing_file[0] != '\0') { 422ea2384d3Sbellard /* if there is a backing file, use it */ 423ea2384d3Sbellard bs->backing_hd = bdrv_new(""); 424ea2384d3Sbellard if (!bs->backing_hd) { 425ea2384d3Sbellard fail: 426ea2384d3Sbellard bdrv_close(bs); 4276b21b973Sbellard return -ENOMEM; 428ea2384d3Sbellard } 42983f64091Sbellard path_combine(backing_filename, sizeof(backing_filename), 43083f64091Sbellard filename, bs->backing_file); 4319f7965c7Saliguori if (bdrv_open(bs->backing_hd, backing_filename, open_flags) < 0) 432ea2384d3Sbellard goto fail; 433ea2384d3Sbellard } 43433e3963eSbellard 435b338082bSbellard /* call the change callback */ 43619cb3738Sbellard bs->media_changed = 1; 437b338082bSbellard if (bs->change_cb) 438b338082bSbellard bs->change_cb(bs->change_opaque); 439b338082bSbellard 440b338082bSbellard return 0; 441fc01f7e7Sbellard } 442fc01f7e7Sbellard 443fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 444fc01f7e7Sbellard { 44519cb3738Sbellard if (bs->drv) { 446ea2384d3Sbellard if (bs->backing_hd) 447ea2384d3Sbellard bdrv_delete(bs->backing_hd); 448ea2384d3Sbellard bs->drv->bdrv_close(bs); 449ea2384d3Sbellard qemu_free(bs->opaque); 450ea2384d3Sbellard #ifdef _WIN32 451ea2384d3Sbellard if (bs->is_temporary) { 452ea2384d3Sbellard unlink(bs->filename); 453ea2384d3Sbellard } 45467b915a5Sbellard #endif 455ea2384d3Sbellard bs->opaque = NULL; 456ea2384d3Sbellard bs->drv = NULL; 457b338082bSbellard 458b338082bSbellard /* call the change callback */ 45919cb3738Sbellard bs->media_changed = 1; 460b338082bSbellard if (bs->change_cb) 461b338082bSbellard bs->change_cb(bs->change_opaque); 462b338082bSbellard } 463b338082bSbellard } 464b338082bSbellard 465b338082bSbellard void bdrv_delete(BlockDriverState *bs) 466b338082bSbellard { 46734c6f050Saurel32 BlockDriverState **pbs; 46834c6f050Saurel32 46934c6f050Saurel32 pbs = &bdrv_first; 47034c6f050Saurel32 while (*pbs != bs && *pbs != NULL) 47134c6f050Saurel32 pbs = &(*pbs)->next; 47234c6f050Saurel32 if (*pbs == bs) 47334c6f050Saurel32 *pbs = bs->next; 47434c6f050Saurel32 475b338082bSbellard bdrv_close(bs); 476b338082bSbellard qemu_free(bs); 477fc01f7e7Sbellard } 478fc01f7e7Sbellard 47933e3963eSbellard /* commit COW file into the raw image */ 48033e3963eSbellard int bdrv_commit(BlockDriverState *bs) 48133e3963eSbellard { 48219cb3738Sbellard BlockDriver *drv = bs->drv; 48383f64091Sbellard int64_t i, total_sectors; 484ea2384d3Sbellard int n, j; 485ea2384d3Sbellard unsigned char sector[512]; 48633e3963eSbellard 48719cb3738Sbellard if (!drv) 48819cb3738Sbellard return -ENOMEDIUM; 48933e3963eSbellard 49033e3963eSbellard if (bs->read_only) { 491ea2384d3Sbellard return -EACCES; 49233e3963eSbellard } 49333e3963eSbellard 494ea2384d3Sbellard if (!bs->backing_hd) { 495ea2384d3Sbellard return -ENOTSUP; 496ea2384d3Sbellard } 497ea2384d3Sbellard 49883f64091Sbellard total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 49983f64091Sbellard for (i = 0; i < total_sectors;) { 50019cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 501ea2384d3Sbellard for(j = 0; j < n; j++) { 50233e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 503ea2384d3Sbellard return -EIO; 50433e3963eSbellard } 50533e3963eSbellard 506ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 507ea2384d3Sbellard return -EIO; 50833e3963eSbellard } 509ea2384d3Sbellard i++; 510ea2384d3Sbellard } 511ea2384d3Sbellard } else { 512ea2384d3Sbellard i += n; 51333e3963eSbellard } 51433e3963eSbellard } 51595389c86Sbellard 51619cb3738Sbellard if (drv->bdrv_make_empty) 51719cb3738Sbellard return drv->bdrv_make_empty(bs); 51895389c86Sbellard 51933e3963eSbellard return 0; 52033e3963eSbellard } 52133e3963eSbellard 52219cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 523fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 524fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 525fc01f7e7Sbellard { 526ea2384d3Sbellard BlockDriver *drv = bs->drv; 527fc01f7e7Sbellard 52819cb3738Sbellard if (!drv) 52919cb3738Sbellard return -ENOMEDIUM; 530b338082bSbellard 53183f64091Sbellard if (drv->bdrv_pread) { 53283f64091Sbellard int ret, len; 53383f64091Sbellard len = nb_sectors * 512; 53483f64091Sbellard ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); 53583f64091Sbellard if (ret < 0) 53683f64091Sbellard return ret; 53783f64091Sbellard else if (ret != len) 53819cb3738Sbellard return -EINVAL; 539a36e69ddSths else { 540a36e69ddSths bs->rd_bytes += (unsigned) len; 541a36e69ddSths bs->rd_ops ++; 54283f64091Sbellard return 0; 543a36e69ddSths } 54483f64091Sbellard } else { 54583f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 54683f64091Sbellard } 54783f64091Sbellard } 548fc01f7e7Sbellard 54919cb3738Sbellard /* Return < 0 if error. Important errors are: 55019cb3738Sbellard -EIO generic I/O error (may happen for all errors) 55119cb3738Sbellard -ENOMEDIUM No media inserted. 55219cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 55319cb3738Sbellard -EACCES Trying to write a read-only device 55419cb3738Sbellard */ 555fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 556fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 557fc01f7e7Sbellard { 55883f64091Sbellard BlockDriver *drv = bs->drv; 55919cb3738Sbellard if (!bs->drv) 56019cb3738Sbellard return -ENOMEDIUM; 5610849bf08Sbellard if (bs->read_only) 56219cb3738Sbellard return -EACCES; 56383f64091Sbellard if (drv->bdrv_pwrite) { 56442fb2807Saliguori int ret, len, count = 0; 56583f64091Sbellard len = nb_sectors * 512; 56642fb2807Saliguori do { 56742fb2807Saliguori ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count); 56842fb2807Saliguori if (ret < 0) { 56942fb2807Saliguori printf("bdrv_write ret=%d\n", ret); 57083f64091Sbellard return ret; 57142fb2807Saliguori } 57242fb2807Saliguori count += ret; 57342fb2807Saliguori buf += ret; 57442fb2807Saliguori } while (count != len); 575a36e69ddSths bs->wr_bytes += (unsigned) len; 576a36e69ddSths bs->wr_ops ++; 57783f64091Sbellard return 0; 578a36e69ddSths } 57983f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 58083f64091Sbellard } 58183f64091Sbellard 58283f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, 583faea38e7Sbellard uint8_t *buf, int count1) 58483f64091Sbellard { 58583f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 58683f64091Sbellard int len, nb_sectors, count; 58783f64091Sbellard int64_t sector_num; 58883f64091Sbellard 58983f64091Sbellard count = count1; 59083f64091Sbellard /* first read to align to sector start */ 59183f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 59283f64091Sbellard if (len > count) 59383f64091Sbellard len = count; 59483f64091Sbellard sector_num = offset >> SECTOR_BITS; 59583f64091Sbellard if (len > 0) { 59683f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 59783f64091Sbellard return -EIO; 59883f64091Sbellard memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); 59983f64091Sbellard count -= len; 60083f64091Sbellard if (count == 0) 60183f64091Sbellard return count1; 60283f64091Sbellard sector_num++; 60383f64091Sbellard buf += len; 60483f64091Sbellard } 60583f64091Sbellard 60683f64091Sbellard /* read the sectors "in place" */ 60783f64091Sbellard nb_sectors = count >> SECTOR_BITS; 60883f64091Sbellard if (nb_sectors > 0) { 60983f64091Sbellard if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) 61083f64091Sbellard return -EIO; 61183f64091Sbellard sector_num += nb_sectors; 61283f64091Sbellard len = nb_sectors << SECTOR_BITS; 61383f64091Sbellard buf += len; 61483f64091Sbellard count -= len; 61583f64091Sbellard } 61683f64091Sbellard 61783f64091Sbellard /* add data from the last sector */ 61883f64091Sbellard if (count > 0) { 61983f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 62083f64091Sbellard return -EIO; 62183f64091Sbellard memcpy(buf, tmp_buf, count); 62283f64091Sbellard } 62383f64091Sbellard return count1; 62483f64091Sbellard } 62583f64091Sbellard 62683f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, 627faea38e7Sbellard const uint8_t *buf, int count1) 62883f64091Sbellard { 62983f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 63083f64091Sbellard int len, nb_sectors, count; 63183f64091Sbellard int64_t sector_num; 63283f64091Sbellard 63383f64091Sbellard count = count1; 63483f64091Sbellard /* first write to align to sector start */ 63583f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 63683f64091Sbellard if (len > count) 63783f64091Sbellard len = count; 63883f64091Sbellard sector_num = offset >> SECTOR_BITS; 63983f64091Sbellard if (len > 0) { 64083f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 64183f64091Sbellard return -EIO; 64283f64091Sbellard memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); 64383f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 64483f64091Sbellard return -EIO; 64583f64091Sbellard count -= len; 64683f64091Sbellard if (count == 0) 64783f64091Sbellard return count1; 64883f64091Sbellard sector_num++; 64983f64091Sbellard buf += len; 65083f64091Sbellard } 65183f64091Sbellard 65283f64091Sbellard /* write the sectors "in place" */ 65383f64091Sbellard nb_sectors = count >> SECTOR_BITS; 65483f64091Sbellard if (nb_sectors > 0) { 65583f64091Sbellard if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) 65683f64091Sbellard return -EIO; 65783f64091Sbellard sector_num += nb_sectors; 65883f64091Sbellard len = nb_sectors << SECTOR_BITS; 65983f64091Sbellard buf += len; 66083f64091Sbellard count -= len; 66183f64091Sbellard } 66283f64091Sbellard 66383f64091Sbellard /* add data from the last sector */ 66483f64091Sbellard if (count > 0) { 66583f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 66683f64091Sbellard return -EIO; 66783f64091Sbellard memcpy(tmp_buf, buf, count); 66883f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 66983f64091Sbellard return -EIO; 67083f64091Sbellard } 67183f64091Sbellard return count1; 67283f64091Sbellard } 67383f64091Sbellard 67483f64091Sbellard /** 67583f64091Sbellard * Read with byte offsets (needed only for file protocols) 67683f64091Sbellard */ 67783f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset, 67883f64091Sbellard void *buf1, int count1) 67983f64091Sbellard { 68083f64091Sbellard BlockDriver *drv = bs->drv; 68183f64091Sbellard 68283f64091Sbellard if (!drv) 68319cb3738Sbellard return -ENOMEDIUM; 68483f64091Sbellard if (!drv->bdrv_pread) 685faea38e7Sbellard return bdrv_pread_em(bs, offset, buf1, count1); 68683f64091Sbellard return drv->bdrv_pread(bs, offset, buf1, count1); 68783f64091Sbellard } 68883f64091Sbellard 68983f64091Sbellard /** 69083f64091Sbellard * Write with byte offsets (needed only for file protocols) 69183f64091Sbellard */ 69283f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 69383f64091Sbellard const void *buf1, int count1) 69483f64091Sbellard { 69583f64091Sbellard BlockDriver *drv = bs->drv; 69683f64091Sbellard 69783f64091Sbellard if (!drv) 69819cb3738Sbellard return -ENOMEDIUM; 69983f64091Sbellard if (!drv->bdrv_pwrite) 700faea38e7Sbellard return bdrv_pwrite_em(bs, offset, buf1, count1); 70183f64091Sbellard return drv->bdrv_pwrite(bs, offset, buf1, count1); 70283f64091Sbellard } 70383f64091Sbellard 70483f64091Sbellard /** 70583f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 70683f64091Sbellard */ 70783f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 70883f64091Sbellard { 70983f64091Sbellard BlockDriver *drv = bs->drv; 71083f64091Sbellard if (!drv) 71119cb3738Sbellard return -ENOMEDIUM; 71283f64091Sbellard if (!drv->bdrv_truncate) 71383f64091Sbellard return -ENOTSUP; 71483f64091Sbellard return drv->bdrv_truncate(bs, offset); 71583f64091Sbellard } 71683f64091Sbellard 71783f64091Sbellard /** 71883f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 71983f64091Sbellard */ 72083f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 72183f64091Sbellard { 72283f64091Sbellard BlockDriver *drv = bs->drv; 72383f64091Sbellard if (!drv) 72419cb3738Sbellard return -ENOMEDIUM; 72583f64091Sbellard if (!drv->bdrv_getlength) { 72683f64091Sbellard /* legacy mode */ 72783f64091Sbellard return bs->total_sectors * SECTOR_SIZE; 72883f64091Sbellard } 72983f64091Sbellard return drv->bdrv_getlength(bs); 730fc01f7e7Sbellard } 731fc01f7e7Sbellard 73219cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 73396b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 734fc01f7e7Sbellard { 73519cb3738Sbellard int64_t length; 73619cb3738Sbellard length = bdrv_getlength(bs); 73719cb3738Sbellard if (length < 0) 73819cb3738Sbellard length = 0; 73919cb3738Sbellard else 74019cb3738Sbellard length = length >> SECTOR_BITS; 74119cb3738Sbellard *nb_sectors_ptr = length; 742fc01f7e7Sbellard } 743cf98951bSbellard 744f3d54fc4Saliguori struct partition { 745f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 746f3d54fc4Saliguori uint8_t head; /* starting head */ 747f3d54fc4Saliguori uint8_t sector; /* starting sector */ 748f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 749f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 750f3d54fc4Saliguori uint8_t end_head; /* end head */ 751f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 752f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 753f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 754f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 755f3d54fc4Saliguori } __attribute__((packed)); 756f3d54fc4Saliguori 757f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 758f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 759f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 760f3d54fc4Saliguori { 761f3d54fc4Saliguori uint8_t buf[512]; 762f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 763f3d54fc4Saliguori struct partition *p; 764f3d54fc4Saliguori uint32_t nr_sects; 765a38131b6Sblueswir1 uint64_t nb_sectors; 766f3d54fc4Saliguori 767f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 768f3d54fc4Saliguori 769f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 770f3d54fc4Saliguori if (ret < 0) 771f3d54fc4Saliguori return -1; 772f3d54fc4Saliguori /* test msdos magic */ 773f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 774f3d54fc4Saliguori return -1; 775f3d54fc4Saliguori for(i = 0; i < 4; i++) { 776f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 777f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 778f3d54fc4Saliguori if (nr_sects && p->end_head) { 779f3d54fc4Saliguori /* We make the assumption that the partition terminates on 780f3d54fc4Saliguori a cylinder boundary */ 781f3d54fc4Saliguori heads = p->end_head + 1; 782f3d54fc4Saliguori sectors = p->end_sector & 63; 783f3d54fc4Saliguori if (sectors == 0) 784f3d54fc4Saliguori continue; 785f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 786f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 787f3d54fc4Saliguori continue; 788f3d54fc4Saliguori *pheads = heads; 789f3d54fc4Saliguori *psectors = sectors; 790f3d54fc4Saliguori *pcylinders = cylinders; 791f3d54fc4Saliguori #if 0 792f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 793f3d54fc4Saliguori cylinders, heads, sectors); 794f3d54fc4Saliguori #endif 795f3d54fc4Saliguori return 0; 796f3d54fc4Saliguori } 797f3d54fc4Saliguori } 798f3d54fc4Saliguori return -1; 799f3d54fc4Saliguori } 800f3d54fc4Saliguori 801f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 802f3d54fc4Saliguori { 803f3d54fc4Saliguori int translation, lba_detected = 0; 804f3d54fc4Saliguori int cylinders, heads, secs; 805a38131b6Sblueswir1 uint64_t nb_sectors; 806f3d54fc4Saliguori 807f3d54fc4Saliguori /* if a geometry hint is available, use it */ 808f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 809f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 810f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 811f3d54fc4Saliguori if (cylinders != 0) { 812f3d54fc4Saliguori *pcyls = cylinders; 813f3d54fc4Saliguori *pheads = heads; 814f3d54fc4Saliguori *psecs = secs; 815f3d54fc4Saliguori } else { 816f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 817f3d54fc4Saliguori if (heads > 16) { 818f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 819f3d54fc4Saliguori translation was active, so the default 820f3d54fc4Saliguori hardware geometry is OK */ 821f3d54fc4Saliguori lba_detected = 1; 822f3d54fc4Saliguori goto default_geometry; 823f3d54fc4Saliguori } else { 824f3d54fc4Saliguori *pcyls = cylinders; 825f3d54fc4Saliguori *pheads = heads; 826f3d54fc4Saliguori *psecs = secs; 827f3d54fc4Saliguori /* disable any translation to be in sync with 828f3d54fc4Saliguori the logical geometry */ 829f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 830f3d54fc4Saliguori bdrv_set_translation_hint(bs, 831f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 832f3d54fc4Saliguori } 833f3d54fc4Saliguori } 834f3d54fc4Saliguori } else { 835f3d54fc4Saliguori default_geometry: 836f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 837f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 838f3d54fc4Saliguori 839f3d54fc4Saliguori if (cylinders > 16383) 840f3d54fc4Saliguori cylinders = 16383; 841f3d54fc4Saliguori else if (cylinders < 2) 842f3d54fc4Saliguori cylinders = 2; 843f3d54fc4Saliguori *pcyls = cylinders; 844f3d54fc4Saliguori *pheads = 16; 845f3d54fc4Saliguori *psecs = 63; 846f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 847f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 848f3d54fc4Saliguori bdrv_set_translation_hint(bs, 849f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 850f3d54fc4Saliguori } else { 851f3d54fc4Saliguori bdrv_set_translation_hint(bs, 852f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 853f3d54fc4Saliguori } 854f3d54fc4Saliguori } 855f3d54fc4Saliguori } 856f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 857f3d54fc4Saliguori } 858f3d54fc4Saliguori } 859f3d54fc4Saliguori 860b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 861b338082bSbellard int cyls, int heads, int secs) 862b338082bSbellard { 863b338082bSbellard bs->cyls = cyls; 864b338082bSbellard bs->heads = heads; 865b338082bSbellard bs->secs = secs; 866b338082bSbellard } 867b338082bSbellard 868b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 869b338082bSbellard { 870b338082bSbellard bs->type = type; 871b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 872b338082bSbellard type == BDRV_TYPE_FLOPPY)); 873b338082bSbellard } 874b338082bSbellard 87546d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 87646d4767dSbellard { 87746d4767dSbellard bs->translation = translation; 87846d4767dSbellard } 87946d4767dSbellard 880b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 881b338082bSbellard int *pcyls, int *pheads, int *psecs) 882b338082bSbellard { 883b338082bSbellard *pcyls = bs->cyls; 884b338082bSbellard *pheads = bs->heads; 885b338082bSbellard *psecs = bs->secs; 886b338082bSbellard } 887b338082bSbellard 888b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 889b338082bSbellard { 890b338082bSbellard return bs->type; 891b338082bSbellard } 892b338082bSbellard 89346d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 89446d4767dSbellard { 89546d4767dSbellard return bs->translation; 89646d4767dSbellard } 89746d4767dSbellard 898b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 899b338082bSbellard { 900b338082bSbellard return bs->removable; 901b338082bSbellard } 902b338082bSbellard 903b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 904b338082bSbellard { 905b338082bSbellard return bs->read_only; 906b338082bSbellard } 907b338082bSbellard 908985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 909985a03b0Sths { 910985a03b0Sths return bs->sg; 911985a03b0Sths } 912985a03b0Sths 91319cb3738Sbellard /* XXX: no longer used */ 914b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 915b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 916b338082bSbellard { 917b338082bSbellard bs->change_cb = change_cb; 918b338082bSbellard bs->change_opaque = opaque; 919b338082bSbellard } 920b338082bSbellard 921ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 922ea2384d3Sbellard { 923ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 924ea2384d3Sbellard return 1; 925ea2384d3Sbellard return bs->encrypted; 926ea2384d3Sbellard } 927ea2384d3Sbellard 928ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 929ea2384d3Sbellard { 930ea2384d3Sbellard int ret; 931ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 932ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 933ea2384d3Sbellard if (ret < 0) 934ea2384d3Sbellard return ret; 935ea2384d3Sbellard if (!bs->encrypted) 936ea2384d3Sbellard return 0; 937ea2384d3Sbellard } 938ea2384d3Sbellard if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) 939ea2384d3Sbellard return -1; 940ea2384d3Sbellard return bs->drv->bdrv_set_key(bs, key); 941ea2384d3Sbellard } 942ea2384d3Sbellard 943ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 944ea2384d3Sbellard { 94519cb3738Sbellard if (!bs->drv) { 946ea2384d3Sbellard buf[0] = '\0'; 947ea2384d3Sbellard } else { 948ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 949ea2384d3Sbellard } 950ea2384d3Sbellard } 951ea2384d3Sbellard 952ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 953ea2384d3Sbellard void *opaque) 954ea2384d3Sbellard { 955ea2384d3Sbellard BlockDriver *drv; 956ea2384d3Sbellard 957ea2384d3Sbellard for (drv = first_drv; drv != NULL; drv = drv->next) { 958ea2384d3Sbellard it(opaque, drv->format_name); 959ea2384d3Sbellard } 960ea2384d3Sbellard } 961ea2384d3Sbellard 962b338082bSbellard BlockDriverState *bdrv_find(const char *name) 963b338082bSbellard { 964b338082bSbellard BlockDriverState *bs; 965b338082bSbellard 966b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 967b338082bSbellard if (!strcmp(name, bs->device_name)) 968b338082bSbellard return bs; 969b338082bSbellard } 970b338082bSbellard return NULL; 971b338082bSbellard } 972b338082bSbellard 97381d0912dSbellard void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) 97481d0912dSbellard { 97581d0912dSbellard BlockDriverState *bs; 97681d0912dSbellard 97781d0912dSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 97881d0912dSbellard it(opaque, bs->device_name); 97981d0912dSbellard } 98081d0912dSbellard } 98181d0912dSbellard 982ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 983ea2384d3Sbellard { 984ea2384d3Sbellard return bs->device_name; 985ea2384d3Sbellard } 986ea2384d3Sbellard 9877a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 9887a6cba61Spbrook { 9897a6cba61Spbrook if (bs->drv->bdrv_flush) 9907a6cba61Spbrook bs->drv->bdrv_flush(bs); 9917a6cba61Spbrook if (bs->backing_hd) 9927a6cba61Spbrook bdrv_flush(bs->backing_hd); 9937a6cba61Spbrook } 9947a6cba61Spbrook 995c6ca28d6Saliguori void bdrv_flush_all(void) 996c6ca28d6Saliguori { 997c6ca28d6Saliguori BlockDriverState *bs; 998c6ca28d6Saliguori 999c6ca28d6Saliguori for (bs = bdrv_first; bs != NULL; bs = bs->next) 1000c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 1001c6ca28d6Saliguori (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) 1002c6ca28d6Saliguori bdrv_flush(bs); 1003c6ca28d6Saliguori } 1004c6ca28d6Saliguori 1005f58c7b35Sths /* 1006f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1007f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1008f58c7b35Sths * hence all their sectors are reported as allocated. 1009f58c7b35Sths * 1010f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1011f58c7b35Sths * the specified sector) that are known to be in the same 1012f58c7b35Sths * allocated/unallocated state. 1013f58c7b35Sths * 1014f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1015f58c7b35Sths */ 1016f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1017f58c7b35Sths int *pnum) 1018f58c7b35Sths { 1019f58c7b35Sths int64_t n; 1020f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1021f58c7b35Sths if (sector_num >= bs->total_sectors) { 1022f58c7b35Sths *pnum = 0; 1023f58c7b35Sths return 0; 1024f58c7b35Sths } 1025f58c7b35Sths n = bs->total_sectors - sector_num; 1026f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1027f58c7b35Sths return 1; 1028f58c7b35Sths } 1029f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1030f58c7b35Sths } 1031f58c7b35Sths 1032b338082bSbellard void bdrv_info(void) 1033b338082bSbellard { 1034b338082bSbellard BlockDriverState *bs; 1035b338082bSbellard 1036b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1037b338082bSbellard term_printf("%s:", bs->device_name); 1038b338082bSbellard term_printf(" type="); 1039b338082bSbellard switch(bs->type) { 1040b338082bSbellard case BDRV_TYPE_HD: 1041b338082bSbellard term_printf("hd"); 1042b338082bSbellard break; 1043b338082bSbellard case BDRV_TYPE_CDROM: 1044b338082bSbellard term_printf("cdrom"); 1045b338082bSbellard break; 1046b338082bSbellard case BDRV_TYPE_FLOPPY: 1047b338082bSbellard term_printf("floppy"); 1048b338082bSbellard break; 1049b338082bSbellard } 1050b338082bSbellard term_printf(" removable=%d", bs->removable); 1051b338082bSbellard if (bs->removable) { 1052b338082bSbellard term_printf(" locked=%d", bs->locked); 1053b338082bSbellard } 105419cb3738Sbellard if (bs->drv) { 1055fef30743Sths term_printf(" file="); 1056fef30743Sths term_print_filename(bs->filename); 1057fef30743Sths if (bs->backing_file[0] != '\0') { 1058fef30743Sths term_printf(" backing_file="); 1059fef30743Sths term_print_filename(bs->backing_file); 1060fef30743Sths } 1061b338082bSbellard term_printf(" ro=%d", bs->read_only); 1062ea2384d3Sbellard term_printf(" drv=%s", bs->drv->format_name); 1063ea2384d3Sbellard if (bs->encrypted) 1064ea2384d3Sbellard term_printf(" encrypted"); 1065b338082bSbellard } else { 1066b338082bSbellard term_printf(" [not inserted]"); 1067b338082bSbellard } 1068b338082bSbellard term_printf("\n"); 1069b338082bSbellard } 1070b338082bSbellard } 1071a36e69ddSths 1072a36e69ddSths /* The "info blockstats" command. */ 1073a36e69ddSths void bdrv_info_stats (void) 1074a36e69ddSths { 1075a36e69ddSths BlockDriverState *bs; 1076a7cbfae0Saliguori BlockDriverInfo bdi; 1077a36e69ddSths 1078a36e69ddSths for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1079a36e69ddSths term_printf ("%s:" 1080a36e69ddSths " rd_bytes=%" PRIu64 1081a36e69ddSths " wr_bytes=%" PRIu64 1082a36e69ddSths " rd_operations=%" PRIu64 1083a36e69ddSths " wr_operations=%" PRIu64 1084a7cbfae0Saliguori , 1085a36e69ddSths bs->device_name, 1086a36e69ddSths bs->rd_bytes, bs->wr_bytes, 1087a36e69ddSths bs->rd_ops, bs->wr_ops); 1088a7cbfae0Saliguori if (bdrv_get_info(bs, &bdi) == 0) 10891987530fSaliguori term_printf(" high=%" PRId64 10901987530fSaliguori " bytes_free=%" PRId64, 10911987530fSaliguori bdi.highest_alloc, bdi.num_free_bytes); 1092a7cbfae0Saliguori term_printf("\n"); 1093a36e69ddSths } 1094a36e69ddSths } 1095ea2384d3Sbellard 109683f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 109783f64091Sbellard char *filename, int filename_size) 109883f64091Sbellard { 109983f64091Sbellard if (!bs->backing_hd) { 110083f64091Sbellard pstrcpy(filename, filename_size, ""); 110183f64091Sbellard } else { 110283f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 110383f64091Sbellard } 110483f64091Sbellard } 110583f64091Sbellard 1106faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1107faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1108faea38e7Sbellard { 1109faea38e7Sbellard BlockDriver *drv = bs->drv; 1110faea38e7Sbellard if (!drv) 111119cb3738Sbellard return -ENOMEDIUM; 1112faea38e7Sbellard if (!drv->bdrv_write_compressed) 1113faea38e7Sbellard return -ENOTSUP; 1114faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1115faea38e7Sbellard } 1116faea38e7Sbellard 1117faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1118faea38e7Sbellard { 1119faea38e7Sbellard BlockDriver *drv = bs->drv; 1120faea38e7Sbellard if (!drv) 112119cb3738Sbellard return -ENOMEDIUM; 1122faea38e7Sbellard if (!drv->bdrv_get_info) 1123faea38e7Sbellard return -ENOTSUP; 1124faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1125faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1126faea38e7Sbellard } 1127faea38e7Sbellard 1128faea38e7Sbellard /**************************************************************/ 1129faea38e7Sbellard /* handling of snapshots */ 1130faea38e7Sbellard 1131faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1132faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1133faea38e7Sbellard { 1134faea38e7Sbellard BlockDriver *drv = bs->drv; 1135faea38e7Sbellard if (!drv) 113619cb3738Sbellard return -ENOMEDIUM; 1137faea38e7Sbellard if (!drv->bdrv_snapshot_create) 1138faea38e7Sbellard return -ENOTSUP; 1139faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 1140faea38e7Sbellard } 1141faea38e7Sbellard 1142faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1143faea38e7Sbellard const char *snapshot_id) 1144faea38e7Sbellard { 1145faea38e7Sbellard BlockDriver *drv = bs->drv; 1146faea38e7Sbellard if (!drv) 114719cb3738Sbellard return -ENOMEDIUM; 1148faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 1149faea38e7Sbellard return -ENOTSUP; 1150faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 1151faea38e7Sbellard } 1152faea38e7Sbellard 1153faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1154faea38e7Sbellard { 1155faea38e7Sbellard BlockDriver *drv = bs->drv; 1156faea38e7Sbellard if (!drv) 115719cb3738Sbellard return -ENOMEDIUM; 1158faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 1159faea38e7Sbellard return -ENOTSUP; 1160faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 1161faea38e7Sbellard } 1162faea38e7Sbellard 1163faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1164faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1165faea38e7Sbellard { 1166faea38e7Sbellard BlockDriver *drv = bs->drv; 1167faea38e7Sbellard if (!drv) 116819cb3738Sbellard return -ENOMEDIUM; 1169faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1170faea38e7Sbellard return -ENOTSUP; 1171faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1172faea38e7Sbellard } 1173faea38e7Sbellard 1174faea38e7Sbellard #define NB_SUFFIXES 4 1175faea38e7Sbellard 1176faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1177faea38e7Sbellard { 1178faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1179faea38e7Sbellard int64_t base; 1180faea38e7Sbellard int i; 1181faea38e7Sbellard 1182faea38e7Sbellard if (size <= 999) { 1183faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1184faea38e7Sbellard } else { 1185faea38e7Sbellard base = 1024; 1186faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1187faea38e7Sbellard if (size < (10 * base)) { 1188faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1189faea38e7Sbellard (double)size / base, 1190faea38e7Sbellard suffixes[i]); 1191faea38e7Sbellard break; 1192faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1193faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1194faea38e7Sbellard ((size + (base >> 1)) / base), 1195faea38e7Sbellard suffixes[i]); 1196faea38e7Sbellard break; 1197faea38e7Sbellard } 1198faea38e7Sbellard base = base * 1024; 1199faea38e7Sbellard } 1200faea38e7Sbellard } 1201faea38e7Sbellard return buf; 1202faea38e7Sbellard } 1203faea38e7Sbellard 1204faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1205faea38e7Sbellard { 1206faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 12073b9f94e1Sbellard #ifdef _WIN32 12083b9f94e1Sbellard struct tm *ptm; 12093b9f94e1Sbellard #else 1210faea38e7Sbellard struct tm tm; 12113b9f94e1Sbellard #endif 1212faea38e7Sbellard time_t ti; 1213faea38e7Sbellard int64_t secs; 1214faea38e7Sbellard 1215faea38e7Sbellard if (!sn) { 1216faea38e7Sbellard snprintf(buf, buf_size, 1217faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1218faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1219faea38e7Sbellard } else { 1220faea38e7Sbellard ti = sn->date_sec; 12213b9f94e1Sbellard #ifdef _WIN32 12223b9f94e1Sbellard ptm = localtime(&ti); 12233b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 12243b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 12253b9f94e1Sbellard #else 1226faea38e7Sbellard localtime_r(&ti, &tm); 1227faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1228faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 12293b9f94e1Sbellard #endif 1230faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1231faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1232faea38e7Sbellard "%02d:%02d:%02d.%03d", 1233faea38e7Sbellard (int)(secs / 3600), 1234faea38e7Sbellard (int)((secs / 60) % 60), 1235faea38e7Sbellard (int)(secs % 60), 1236faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1237faea38e7Sbellard snprintf(buf, buf_size, 1238faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1239faea38e7Sbellard sn->id_str, sn->name, 1240faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1241faea38e7Sbellard date_buf, 1242faea38e7Sbellard clock_buf); 1243faea38e7Sbellard } 1244faea38e7Sbellard return buf; 1245faea38e7Sbellard } 1246faea38e7Sbellard 124783f64091Sbellard 1248ea2384d3Sbellard /**************************************************************/ 124983f64091Sbellard /* async I/Os */ 1250ea2384d3Sbellard 12513b69e4b9Saliguori typedef struct VectorTranslationState { 12523b69e4b9Saliguori QEMUIOVector *iov; 12533b69e4b9Saliguori uint8_t *bounce; 12543b69e4b9Saliguori int is_write; 12553b69e4b9Saliguori BlockDriverAIOCB *aiocb; 12563b69e4b9Saliguori BlockDriverAIOCB *this_aiocb; 12573b69e4b9Saliguori } VectorTranslationState; 12583b69e4b9Saliguori 12593b69e4b9Saliguori static void bdrv_aio_rw_vector_cb(void *opaque, int ret) 12603b69e4b9Saliguori { 12613b69e4b9Saliguori VectorTranslationState *s = opaque; 12623b69e4b9Saliguori 12633b69e4b9Saliguori if (!s->is_write) { 1264249aa745Saliguori qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size); 12653b69e4b9Saliguori } 1266*d905dba4Saurel32 qemu_vfree(s->bounce); 12673b69e4b9Saliguori s->this_aiocb->cb(s->this_aiocb->opaque, ret); 12683b69e4b9Saliguori qemu_aio_release(s->this_aiocb); 12693b69e4b9Saliguori } 12703b69e4b9Saliguori 12713b69e4b9Saliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 12723b69e4b9Saliguori int64_t sector_num, 12733b69e4b9Saliguori QEMUIOVector *iov, 12743b69e4b9Saliguori int nb_sectors, 12753b69e4b9Saliguori BlockDriverCompletionFunc *cb, 12763b69e4b9Saliguori void *opaque, 12773b69e4b9Saliguori int is_write) 12783b69e4b9Saliguori 12793b69e4b9Saliguori { 12803b69e4b9Saliguori VectorTranslationState *s = qemu_mallocz(sizeof(*s)); 12813b69e4b9Saliguori BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque); 12823b69e4b9Saliguori 12833b69e4b9Saliguori s->this_aiocb = aiocb; 12843b69e4b9Saliguori s->iov = iov; 12853b69e4b9Saliguori s->bounce = qemu_memalign(512, nb_sectors * 512); 12863b69e4b9Saliguori s->is_write = is_write; 12873b69e4b9Saliguori if (is_write) { 12883b69e4b9Saliguori qemu_iovec_to_buffer(s->iov, s->bounce); 12893b69e4b9Saliguori s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors, 12903b69e4b9Saliguori bdrv_aio_rw_vector_cb, s); 12913b69e4b9Saliguori } else { 12923b69e4b9Saliguori s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors, 12933b69e4b9Saliguori bdrv_aio_rw_vector_cb, s); 12943b69e4b9Saliguori } 12953b69e4b9Saliguori return aiocb; 12963b69e4b9Saliguori } 12973b69e4b9Saliguori 12983b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 12993b69e4b9Saliguori QEMUIOVector *iov, int nb_sectors, 13003b69e4b9Saliguori BlockDriverCompletionFunc *cb, void *opaque) 13013b69e4b9Saliguori { 13023b69e4b9Saliguori return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, 13033b69e4b9Saliguori cb, opaque, 0); 13043b69e4b9Saliguori } 13053b69e4b9Saliguori 13063b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 13073b69e4b9Saliguori QEMUIOVector *iov, int nb_sectors, 13083b69e4b9Saliguori BlockDriverCompletionFunc *cb, void *opaque) 13093b69e4b9Saliguori { 13103b69e4b9Saliguori return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, 13113b69e4b9Saliguori cb, opaque, 1); 13123b69e4b9Saliguori } 13133b69e4b9Saliguori 1314ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, 131583f64091Sbellard uint8_t *buf, int nb_sectors, 131683f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1317ea2384d3Sbellard { 131883f64091Sbellard BlockDriver *drv = bs->drv; 1319a36e69ddSths BlockDriverAIOCB *ret; 1320ea2384d3Sbellard 132119cb3738Sbellard if (!drv) 1322ce1a14dcSpbrook return NULL; 132383f64091Sbellard 1324a36e69ddSths ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); 1325a36e69ddSths 1326a36e69ddSths if (ret) { 1327a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1328a36e69ddSths bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1329a36e69ddSths bs->rd_ops ++; 1330a36e69ddSths } 1331a36e69ddSths 1332a36e69ddSths return ret; 133383f64091Sbellard } 133483f64091Sbellard 1335ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, 133683f64091Sbellard const uint8_t *buf, int nb_sectors, 133783f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 13387674e7bfSbellard { 133983f64091Sbellard BlockDriver *drv = bs->drv; 1340a36e69ddSths BlockDriverAIOCB *ret; 134183f64091Sbellard 134219cb3738Sbellard if (!drv) 1343ce1a14dcSpbrook return NULL; 134483f64091Sbellard if (bs->read_only) 1345ce1a14dcSpbrook return NULL; 134683f64091Sbellard 1347a36e69ddSths ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); 1348a36e69ddSths 1349a36e69ddSths if (ret) { 1350a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1351a36e69ddSths bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1352a36e69ddSths bs->wr_ops ++; 1353a36e69ddSths } 1354a36e69ddSths 1355a36e69ddSths return ret; 135683f64091Sbellard } 135783f64091Sbellard 135883f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 135983f64091Sbellard { 1360ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 136183f64091Sbellard 13623b69e4b9Saliguori if (acb->cb == bdrv_aio_rw_vector_cb) { 13633b69e4b9Saliguori VectorTranslationState *s = acb->opaque; 13643b69e4b9Saliguori acb = s->aiocb; 13653b69e4b9Saliguori } 13663b69e4b9Saliguori 136783f64091Sbellard drv->bdrv_aio_cancel(acb); 136883f64091Sbellard } 136983f64091Sbellard 137083f64091Sbellard 137183f64091Sbellard /**************************************************************/ 137283f64091Sbellard /* async block device emulation */ 137383f64091Sbellard 137483f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 1375beac80cdSbellard { 1376ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 1377ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 1378ce1a14dcSpbrook qemu_aio_release(acb); 1379beac80cdSbellard } 1380beac80cdSbellard 1381ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 1382ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 1383ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 1384ea2384d3Sbellard { 1385ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 138683f64091Sbellard int ret; 138783f64091Sbellard 1388ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1389ce1a14dcSpbrook if (!acb->bh) 1390ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1391ce1a14dcSpbrook ret = bdrv_read(bs, sector_num, buf, nb_sectors); 1392ce1a14dcSpbrook acb->ret = ret; 1393ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1394ce1a14dcSpbrook return &acb->common; 13957a6cba61Spbrook } 13967a6cba61Spbrook 1397ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 1398ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 1399ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 140083f64091Sbellard { 1401ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 140283f64091Sbellard int ret; 140383f64091Sbellard 1404ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1405ce1a14dcSpbrook if (!acb->bh) 1406ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1407ce1a14dcSpbrook ret = bdrv_write(bs, sector_num, buf, nb_sectors); 1408ce1a14dcSpbrook acb->ret = ret; 1409ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1410ce1a14dcSpbrook return &acb->common; 141183f64091Sbellard } 141283f64091Sbellard 1413ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 141483f64091Sbellard { 1415ce1a14dcSpbrook BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; 1416ce1a14dcSpbrook qemu_bh_cancel(acb->bh); 1417ce1a14dcSpbrook qemu_aio_release(acb); 141883f64091Sbellard } 141983f64091Sbellard 142083f64091Sbellard /**************************************************************/ 142183f64091Sbellard /* sync block device emulation */ 142283f64091Sbellard 142383f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 142483f64091Sbellard { 142583f64091Sbellard *(int *)opaque = ret; 142683f64091Sbellard } 142783f64091Sbellard 142883f64091Sbellard #define NOT_DONE 0x7fffffff 142983f64091Sbellard 143083f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 143183f64091Sbellard uint8_t *buf, int nb_sectors) 143283f64091Sbellard { 1433ce1a14dcSpbrook int async_ret; 1434ce1a14dcSpbrook BlockDriverAIOCB *acb; 143583f64091Sbellard 143683f64091Sbellard async_ret = NOT_DONE; 1437ce1a14dcSpbrook acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, 143883f64091Sbellard bdrv_rw_em_cb, &async_ret); 1439baf35cb9Saliguori if (acb == NULL) 1440ce1a14dcSpbrook return -1; 1441baf35cb9Saliguori 144283f64091Sbellard while (async_ret == NOT_DONE) { 144383f64091Sbellard qemu_aio_wait(); 144483f64091Sbellard } 1445baf35cb9Saliguori 144683f64091Sbellard return async_ret; 144783f64091Sbellard } 144883f64091Sbellard 144983f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 145083f64091Sbellard const uint8_t *buf, int nb_sectors) 145183f64091Sbellard { 1452ce1a14dcSpbrook int async_ret; 1453ce1a14dcSpbrook BlockDriverAIOCB *acb; 145483f64091Sbellard 145583f64091Sbellard async_ret = NOT_DONE; 1456ce1a14dcSpbrook acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, 145783f64091Sbellard bdrv_rw_em_cb, &async_ret); 1458baf35cb9Saliguori if (acb == NULL) 1459ce1a14dcSpbrook return -1; 146083f64091Sbellard while (async_ret == NOT_DONE) { 146183f64091Sbellard qemu_aio_wait(); 146283f64091Sbellard } 146383f64091Sbellard return async_ret; 146483f64091Sbellard } 1465ea2384d3Sbellard 1466ea2384d3Sbellard void bdrv_init(void) 1467ea2384d3Sbellard { 1468ea2384d3Sbellard bdrv_register(&bdrv_raw); 146919cb3738Sbellard bdrv_register(&bdrv_host_device); 1470ea2384d3Sbellard #ifndef _WIN32 1471ea2384d3Sbellard bdrv_register(&bdrv_cow); 1472ea2384d3Sbellard #endif 1473ea2384d3Sbellard bdrv_register(&bdrv_qcow); 1474ea2384d3Sbellard bdrv_register(&bdrv_vmdk); 14753c56521bSbellard bdrv_register(&bdrv_cloop); 1476585d0ed9Sbellard bdrv_register(&bdrv_dmg); 1477a8753c34Sbellard bdrv_register(&bdrv_bochs); 14786a0f9e82Sbellard bdrv_register(&bdrv_vpc); 1479712e7874Sbellard bdrv_register(&bdrv_vvfat); 1480faea38e7Sbellard bdrv_register(&bdrv_qcow2); 14816ada7453Sths bdrv_register(&bdrv_parallels); 148275818250Sths bdrv_register(&bdrv_nbd); 1483ea2384d3Sbellard } 1484ce1a14dcSpbrook 1485ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, 1486ce1a14dcSpbrook void *opaque) 1487ce1a14dcSpbrook { 1488ce1a14dcSpbrook BlockDriver *drv; 1489ce1a14dcSpbrook BlockDriverAIOCB *acb; 1490ce1a14dcSpbrook 1491ce1a14dcSpbrook drv = bs->drv; 1492ce1a14dcSpbrook if (drv->free_aiocb) { 1493ce1a14dcSpbrook acb = drv->free_aiocb; 1494ce1a14dcSpbrook drv->free_aiocb = acb->next; 1495ce1a14dcSpbrook } else { 1496ce1a14dcSpbrook acb = qemu_mallocz(drv->aiocb_size); 1497ce1a14dcSpbrook } 1498ce1a14dcSpbrook acb->bs = bs; 1499ce1a14dcSpbrook acb->cb = cb; 1500ce1a14dcSpbrook acb->opaque = opaque; 1501ce1a14dcSpbrook return acb; 1502ce1a14dcSpbrook } 1503ce1a14dcSpbrook 1504ce1a14dcSpbrook void qemu_aio_release(void *p) 1505ce1a14dcSpbrook { 1506ce1a14dcSpbrook BlockDriverAIOCB *acb = p; 1507ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 1508ce1a14dcSpbrook acb->next = drv->free_aiocb; 1509ce1a14dcSpbrook drv->free_aiocb = acb; 1510ce1a14dcSpbrook } 151119cb3738Sbellard 151219cb3738Sbellard /**************************************************************/ 151319cb3738Sbellard /* removable device support */ 151419cb3738Sbellard 151519cb3738Sbellard /** 151619cb3738Sbellard * Return TRUE if the media is present 151719cb3738Sbellard */ 151819cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 151919cb3738Sbellard { 152019cb3738Sbellard BlockDriver *drv = bs->drv; 152119cb3738Sbellard int ret; 152219cb3738Sbellard if (!drv) 152319cb3738Sbellard return 0; 152419cb3738Sbellard if (!drv->bdrv_is_inserted) 152519cb3738Sbellard return 1; 152619cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 152719cb3738Sbellard return ret; 152819cb3738Sbellard } 152919cb3738Sbellard 153019cb3738Sbellard /** 153119cb3738Sbellard * Return TRUE if the media changed since the last call to this 153219cb3738Sbellard * function. It is currently only used for floppy disks 153319cb3738Sbellard */ 153419cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 153519cb3738Sbellard { 153619cb3738Sbellard BlockDriver *drv = bs->drv; 153719cb3738Sbellard int ret; 153819cb3738Sbellard 153919cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 154019cb3738Sbellard ret = -ENOTSUP; 154119cb3738Sbellard else 154219cb3738Sbellard ret = drv->bdrv_media_changed(bs); 154319cb3738Sbellard if (ret == -ENOTSUP) 154419cb3738Sbellard ret = bs->media_changed; 154519cb3738Sbellard bs->media_changed = 0; 154619cb3738Sbellard return ret; 154719cb3738Sbellard } 154819cb3738Sbellard 154919cb3738Sbellard /** 155019cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 155119cb3738Sbellard */ 155219cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag) 155319cb3738Sbellard { 155419cb3738Sbellard BlockDriver *drv = bs->drv; 155519cb3738Sbellard int ret; 155619cb3738Sbellard 155719cb3738Sbellard if (!drv || !drv->bdrv_eject) { 155819cb3738Sbellard ret = -ENOTSUP; 155919cb3738Sbellard } else { 156019cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 156119cb3738Sbellard } 156219cb3738Sbellard if (ret == -ENOTSUP) { 156319cb3738Sbellard if (eject_flag) 156419cb3738Sbellard bdrv_close(bs); 156519cb3738Sbellard } 156619cb3738Sbellard } 156719cb3738Sbellard 156819cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 156919cb3738Sbellard { 157019cb3738Sbellard return bs->locked; 157119cb3738Sbellard } 157219cb3738Sbellard 157319cb3738Sbellard /** 157419cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 157519cb3738Sbellard * to eject it manually). 157619cb3738Sbellard */ 157719cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 157819cb3738Sbellard { 157919cb3738Sbellard BlockDriver *drv = bs->drv; 158019cb3738Sbellard 158119cb3738Sbellard bs->locked = locked; 158219cb3738Sbellard if (drv && drv->bdrv_set_locked) { 158319cb3738Sbellard drv->bdrv_set_locked(bs, locked); 158419cb3738Sbellard } 158519cb3738Sbellard } 1586985a03b0Sths 1587985a03b0Sths /* needed for generic scsi interface */ 1588985a03b0Sths 1589985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 1590985a03b0Sths { 1591985a03b0Sths BlockDriver *drv = bs->drv; 1592985a03b0Sths 1593985a03b0Sths if (drv && drv->bdrv_ioctl) 1594985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 1595985a03b0Sths return -ENOTSUP; 1596985a03b0Sths } 1597