1fc01f7e7Sbellard /* 2fc01f7e7Sbellard * QEMU System Emulator block driver 3fc01f7e7Sbellard * 4fc01f7e7Sbellard * Copyright (c) 2003 Fabrice Bellard 5fc01f7e7Sbellard * 6fc01f7e7Sbellard * Permission is hereby granted, free of charge, to any person obtaining a copy 7fc01f7e7Sbellard * of this software and associated documentation files (the "Software"), to deal 8fc01f7e7Sbellard * in the Software without restriction, including without limitation the rights 9fc01f7e7Sbellard * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 10fc01f7e7Sbellard * copies of the Software, and to permit persons to whom the Software is 11fc01f7e7Sbellard * furnished to do so, subject to the following conditions: 12fc01f7e7Sbellard * 13fc01f7e7Sbellard * The above copyright notice and this permission notice shall be included in 14fc01f7e7Sbellard * all copies or substantial portions of the Software. 15fc01f7e7Sbellard * 16fc01f7e7Sbellard * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17fc01f7e7Sbellard * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18fc01f7e7Sbellard * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19fc01f7e7Sbellard * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20fc01f7e7Sbellard * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 21fc01f7e7Sbellard * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 22fc01f7e7Sbellard * THE SOFTWARE. 23fc01f7e7Sbellard */ 243990d09aSblueswir1 #include "config-host.h" 25179a2c19Sblueswir1 #ifdef HOST_BSD 263990d09aSblueswir1 /* include native header before sys-queue.h */ 273990d09aSblueswir1 #include <sys/queue.h> 283990d09aSblueswir1 #endif 293990d09aSblueswir1 30faf07963Spbrook #include "qemu-common.h" 31376253ecSaliguori #include "monitor.h" 32ea2384d3Sbellard #include "block_int.h" 33fc01f7e7Sbellard 34179a2c19Sblueswir1 #ifdef HOST_BSD 357674e7bfSbellard #include <sys/types.h> 367674e7bfSbellard #include <sys/stat.h> 377674e7bfSbellard #include <sys/ioctl.h> 38c5e97233Sblueswir1 #ifndef __DragonFly__ 397674e7bfSbellard #include <sys/disk.h> 407674e7bfSbellard #endif 41c5e97233Sblueswir1 #endif 427674e7bfSbellard 4349dc768dSaliguori #ifdef _WIN32 4449dc768dSaliguori #include <windows.h> 4549dc768dSaliguori #endif 4649dc768dSaliguori 4783f64091Sbellard #define SECTOR_BITS 9 4883f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS) 493b0d4f61Sbellard 5090765429Sbellard typedef struct BlockDriverAIOCBSync { 5190765429Sbellard BlockDriverAIOCB common; 5290765429Sbellard QEMUBH *bh; 5390765429Sbellard int ret; 5490765429Sbellard } BlockDriverAIOCBSync; 5590765429Sbellard 56ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 57ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 58ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 59ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 60ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 61ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 6283f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); 6383f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 6483f64091Sbellard uint8_t *buf, int nb_sectors); 6583f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 6683f64091Sbellard const uint8_t *buf, int nb_sectors); 67ec530c81Sbellard 687ee930d0Sblueswir1 BlockDriverState *bdrv_first; 697ee930d0Sblueswir1 70ea2384d3Sbellard static BlockDriver *first_drv; 71ea2384d3Sbellard 7283f64091Sbellard int path_is_absolute(const char *path) 7383f64091Sbellard { 7483f64091Sbellard const char *p; 7521664424Sbellard #ifdef _WIN32 7621664424Sbellard /* specific case for names like: "\\.\d:" */ 7721664424Sbellard if (*path == '/' || *path == '\\') 7821664424Sbellard return 1; 7921664424Sbellard #endif 8083f64091Sbellard p = strchr(path, ':'); 8183f64091Sbellard if (p) 8283f64091Sbellard p++; 8383f64091Sbellard else 8483f64091Sbellard p = path; 853b9f94e1Sbellard #ifdef _WIN32 863b9f94e1Sbellard return (*p == '/' || *p == '\\'); 873b9f94e1Sbellard #else 883b9f94e1Sbellard return (*p == '/'); 893b9f94e1Sbellard #endif 9083f64091Sbellard } 9183f64091Sbellard 9283f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 9383f64091Sbellard path to it by considering it is relative to base_path. URL are 9483f64091Sbellard supported. */ 9583f64091Sbellard void path_combine(char *dest, int dest_size, 9683f64091Sbellard const char *base_path, 9783f64091Sbellard const char *filename) 9883f64091Sbellard { 9983f64091Sbellard const char *p, *p1; 10083f64091Sbellard int len; 10183f64091Sbellard 10283f64091Sbellard if (dest_size <= 0) 10383f64091Sbellard return; 10483f64091Sbellard if (path_is_absolute(filename)) { 10583f64091Sbellard pstrcpy(dest, dest_size, filename); 10683f64091Sbellard } else { 10783f64091Sbellard p = strchr(base_path, ':'); 10883f64091Sbellard if (p) 10983f64091Sbellard p++; 11083f64091Sbellard else 11183f64091Sbellard p = base_path; 1123b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1133b9f94e1Sbellard #ifdef _WIN32 1143b9f94e1Sbellard { 1153b9f94e1Sbellard const char *p2; 1163b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1173b9f94e1Sbellard if (!p1 || p2 > p1) 1183b9f94e1Sbellard p1 = p2; 1193b9f94e1Sbellard } 1203b9f94e1Sbellard #endif 12183f64091Sbellard if (p1) 12283f64091Sbellard p1++; 12383f64091Sbellard else 12483f64091Sbellard p1 = base_path; 12583f64091Sbellard if (p1 > p) 12683f64091Sbellard p = p1; 12783f64091Sbellard len = p - base_path; 12883f64091Sbellard if (len > dest_size - 1) 12983f64091Sbellard len = dest_size - 1; 13083f64091Sbellard memcpy(dest, base_path, len); 13183f64091Sbellard dest[len] = '\0'; 13283f64091Sbellard pstrcat(dest, dest_size, filename); 13383f64091Sbellard } 13483f64091Sbellard } 13583f64091Sbellard 13683f64091Sbellard 1379596ebb7Spbrook static void bdrv_register(BlockDriver *bdrv) 138ea2384d3Sbellard { 139ce1a14dcSpbrook if (!bdrv->bdrv_aio_read) { 14083f64091Sbellard /* add AIO emulation layer */ 14183f64091Sbellard bdrv->bdrv_aio_read = bdrv_aio_read_em; 14283f64091Sbellard bdrv->bdrv_aio_write = bdrv_aio_write_em; 14383f64091Sbellard bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; 14490765429Sbellard bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync); 14583f64091Sbellard } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) { 14683f64091Sbellard /* add synchronous IO emulation layer */ 14783f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 14883f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 14983f64091Sbellard } 150ea2384d3Sbellard bdrv->next = first_drv; 151ea2384d3Sbellard first_drv = bdrv; 152ea2384d3Sbellard } 153b338082bSbellard 154b338082bSbellard /* create a new block device (by default it is empty) */ 155b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 156fc01f7e7Sbellard { 157b338082bSbellard BlockDriverState **pbs, *bs; 158b338082bSbellard 159b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 160b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 161ea2384d3Sbellard if (device_name[0] != '\0') { 162b338082bSbellard /* insert at the end */ 163b338082bSbellard pbs = &bdrv_first; 164b338082bSbellard while (*pbs != NULL) 165b338082bSbellard pbs = &(*pbs)->next; 166b338082bSbellard *pbs = bs; 167ea2384d3Sbellard } 168b338082bSbellard return bs; 169b338082bSbellard } 170b338082bSbellard 171ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 172ea2384d3Sbellard { 173ea2384d3Sbellard BlockDriver *drv1; 174ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 175ea2384d3Sbellard if (!strcmp(drv1->format_name, format_name)) 176ea2384d3Sbellard return drv1; 177ea2384d3Sbellard } 178ea2384d3Sbellard return NULL; 179ea2384d3Sbellard } 180ea2384d3Sbellard 181ea2384d3Sbellard int bdrv_create(BlockDriver *drv, 182ea2384d3Sbellard const char *filename, int64_t size_in_sectors, 183ea2384d3Sbellard const char *backing_file, int flags) 184ea2384d3Sbellard { 185ea2384d3Sbellard if (!drv->bdrv_create) 186ea2384d3Sbellard return -ENOTSUP; 187ea2384d3Sbellard return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); 188ea2384d3Sbellard } 189ea2384d3Sbellard 190d5249393Sbellard #ifdef _WIN32 19195389c86Sbellard void get_tmp_filename(char *filename, int size) 192d5249393Sbellard { 1933b9f94e1Sbellard char temp_dir[MAX_PATH]; 1943b9f94e1Sbellard 1953b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 1963b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 197d5249393Sbellard } 198d5249393Sbellard #else 19995389c86Sbellard void get_tmp_filename(char *filename, int size) 200ea2384d3Sbellard { 201ea2384d3Sbellard int fd; 2027ccfb2ebSblueswir1 const char *tmpdir; 203d5249393Sbellard /* XXX: race condition possible */ 2040badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 2050badc1eeSaurel32 if (!tmpdir) 2060badc1eeSaurel32 tmpdir = "/tmp"; 2070badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 208ea2384d3Sbellard fd = mkstemp(filename); 209ea2384d3Sbellard close(fd); 210ea2384d3Sbellard } 211d5249393Sbellard #endif 212ea2384d3Sbellard 21319cb3738Sbellard #ifdef _WIN32 214f45512feSbellard static int is_windows_drive_prefix(const char *filename) 215f45512feSbellard { 216f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 217f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 218f45512feSbellard filename[1] == ':'); 219f45512feSbellard } 220f45512feSbellard 22119cb3738Sbellard static int is_windows_drive(const char *filename) 22219cb3738Sbellard { 223f45512feSbellard if (is_windows_drive_prefix(filename) && 224f45512feSbellard filename[2] == '\0') 22519cb3738Sbellard return 1; 22619cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 22719cb3738Sbellard strstart(filename, "//./", NULL)) 22819cb3738Sbellard return 1; 22919cb3738Sbellard return 0; 23019cb3738Sbellard } 23119cb3738Sbellard #endif 23219cb3738Sbellard 23383f64091Sbellard static BlockDriver *find_protocol(const char *filename) 23483f64091Sbellard { 23583f64091Sbellard BlockDriver *drv1; 23683f64091Sbellard char protocol[128]; 23783f64091Sbellard int len; 23883f64091Sbellard const char *p; 23919cb3738Sbellard 24019cb3738Sbellard #ifdef _WIN32 241f45512feSbellard if (is_windows_drive(filename) || 242f45512feSbellard is_windows_drive_prefix(filename)) 24319cb3738Sbellard return &bdrv_raw; 24419cb3738Sbellard #endif 24583f64091Sbellard p = strchr(filename, ':'); 24683f64091Sbellard if (!p) 24783f64091Sbellard return &bdrv_raw; 24883f64091Sbellard len = p - filename; 24983f64091Sbellard if (len > sizeof(protocol) - 1) 25083f64091Sbellard len = sizeof(protocol) - 1; 25183f64091Sbellard memcpy(protocol, filename, len); 25283f64091Sbellard protocol[len] = '\0'; 25383f64091Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 25483f64091Sbellard if (drv1->protocol_name && 25583f64091Sbellard !strcmp(drv1->protocol_name, protocol)) 25683f64091Sbellard return drv1; 25783f64091Sbellard } 25883f64091Sbellard return NULL; 25983f64091Sbellard } 26083f64091Sbellard 2617674e7bfSbellard /* XXX: force raw format if block or character device ? It would 2627674e7bfSbellard simplify the BSD case */ 263ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 264ea2384d3Sbellard { 26583f64091Sbellard int ret, score, score_max; 266ea2384d3Sbellard BlockDriver *drv1, *drv; 26783f64091Sbellard uint8_t buf[2048]; 26883f64091Sbellard BlockDriverState *bs; 269ea2384d3Sbellard 27019cb3738Sbellard /* detect host devices. By convention, /dev/cdrom[N] is always 27119cb3738Sbellard recognized as a host CDROM */ 27219cb3738Sbellard if (strstart(filename, "/dev/cdrom", NULL)) 27319cb3738Sbellard return &bdrv_host_device; 27419cb3738Sbellard #ifdef _WIN32 27519cb3738Sbellard if (is_windows_drive(filename)) 27619cb3738Sbellard return &bdrv_host_device; 27719cb3738Sbellard #else 27819cb3738Sbellard { 27919cb3738Sbellard struct stat st; 28019cb3738Sbellard if (stat(filename, &st) >= 0 && 28119cb3738Sbellard (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 28219cb3738Sbellard return &bdrv_host_device; 28319cb3738Sbellard } 28419cb3738Sbellard } 28519cb3738Sbellard #endif 28619cb3738Sbellard 28783f64091Sbellard drv = find_protocol(filename); 28819cb3738Sbellard /* no need to test disk image formats for vvfat */ 28983f64091Sbellard if (drv == &bdrv_vvfat) 29083f64091Sbellard return drv; 29183f64091Sbellard 29283f64091Sbellard ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); 29383f64091Sbellard if (ret < 0) 2947674e7bfSbellard return NULL; 29583f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 29683f64091Sbellard bdrv_delete(bs); 297ea2384d3Sbellard if (ret < 0) { 298ea2384d3Sbellard return NULL; 299ea2384d3Sbellard } 300ea2384d3Sbellard 301ea2384d3Sbellard score_max = 0; 302ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 30383f64091Sbellard if (drv1->bdrv_probe) { 304ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 305ea2384d3Sbellard if (score > score_max) { 306ea2384d3Sbellard score_max = score; 307ea2384d3Sbellard drv = drv1; 308ea2384d3Sbellard } 309ea2384d3Sbellard } 31083f64091Sbellard } 311ea2384d3Sbellard return drv; 312ea2384d3Sbellard } 313ea2384d3Sbellard 31483f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 315b338082bSbellard { 31683f64091Sbellard BlockDriverState *bs; 31783f64091Sbellard int ret; 3183b0d4f61Sbellard 31983f64091Sbellard bs = bdrv_new(""); 32083f64091Sbellard ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); 32183f64091Sbellard if (ret < 0) { 32283f64091Sbellard bdrv_delete(bs); 32383f64091Sbellard return ret; 3243b0d4f61Sbellard } 32571d0770cSaliguori bs->growable = 1; 32683f64091Sbellard *pbs = bs; 32783f64091Sbellard return 0; 3283b0d4f61Sbellard } 3293b0d4f61Sbellard 33083f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags) 33183f64091Sbellard { 33283f64091Sbellard return bdrv_open2(bs, filename, flags, NULL); 333ea2384d3Sbellard } 334ea2384d3Sbellard 33583f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 336ea2384d3Sbellard BlockDriver *drv) 337ea2384d3Sbellard { 33883f64091Sbellard int ret, open_flags; 339eb5c851fSths char tmp_filename[PATH_MAX]; 340eb5c851fSths char backing_filename[PATH_MAX]; 341fc01f7e7Sbellard 3420849bf08Sbellard bs->read_only = 0; 343ea2384d3Sbellard bs->is_temporary = 0; 344ea2384d3Sbellard bs->encrypted = 0; 345c0f4ce77Saliguori bs->valid_key = 0; 34633e3963eSbellard 34783f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 348ea2384d3Sbellard BlockDriverState *bs1; 349ea2384d3Sbellard int64_t total_size; 3507c96d46eSaliguori int is_protocol = 0; 35133e3963eSbellard 352ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 353ea2384d3Sbellard instead of opening 'filename' directly */ 354ea2384d3Sbellard 355ea2384d3Sbellard /* if there is a backing file, use it */ 356ea2384d3Sbellard bs1 = bdrv_new(""); 35751d7c00cSaliguori ret = bdrv_open(bs1, filename, 0); 35851d7c00cSaliguori if (ret < 0) { 359ea2384d3Sbellard bdrv_delete(bs1); 36051d7c00cSaliguori return ret; 361ea2384d3Sbellard } 36283f64091Sbellard total_size = bdrv_getlength(bs1) >> SECTOR_BITS; 3637c96d46eSaliguori 3647c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 3657c96d46eSaliguori is_protocol = 1; 3667c96d46eSaliguori 367ea2384d3Sbellard bdrv_delete(bs1); 368ea2384d3Sbellard 369ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 3707c96d46eSaliguori 3717c96d46eSaliguori /* Real path is meaningless for protocols */ 3727c96d46eSaliguori if (is_protocol) 3737c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 3747c96d46eSaliguori "%s", filename); 3757c96d46eSaliguori else 376a817d936Sbellard realpath(filename, backing_filename); 3777c96d46eSaliguori 37851d7c00cSaliguori ret = bdrv_create(&bdrv_qcow2, tmp_filename, 37951d7c00cSaliguori total_size, backing_filename, 0); 38051d7c00cSaliguori if (ret < 0) { 38151d7c00cSaliguori return ret; 382ea2384d3Sbellard } 383ea2384d3Sbellard filename = tmp_filename; 384ea2384d3Sbellard bs->is_temporary = 1; 385ea2384d3Sbellard } 386ea2384d3Sbellard 387ea2384d3Sbellard pstrcpy(bs->filename, sizeof(bs->filename), filename); 38883f64091Sbellard if (flags & BDRV_O_FILE) { 38983f64091Sbellard drv = find_protocol(filename); 39051d7c00cSaliguori } else if (!drv) { 391ea2384d3Sbellard drv = find_image_format(filename); 392ea2384d3Sbellard } 39351d7c00cSaliguori if (!drv) { 39451d7c00cSaliguori ret = -ENOENT; 39551d7c00cSaliguori goto unlink_and_fail; 39683f64091Sbellard } 397ea2384d3Sbellard bs->drv = drv; 398ea2384d3Sbellard bs->opaque = qemu_mallocz(drv->instance_size); 39983f64091Sbellard /* Note: for compatibility, we open disk image files as RDWR, and 40083f64091Sbellard RDONLY as fallback */ 40183f64091Sbellard if (!(flags & BDRV_O_FILE)) 4029f7965c7Saliguori open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); 40383f64091Sbellard else 40483f64091Sbellard open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); 40583f64091Sbellard ret = drv->bdrv_open(bs, filename, open_flags); 406a0a83536Saurel32 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { 4079f7965c7Saliguori ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); 40883f64091Sbellard bs->read_only = 1; 40983f64091Sbellard } 410ea2384d3Sbellard if (ret < 0) { 411ea2384d3Sbellard qemu_free(bs->opaque); 4126b21b973Sbellard bs->opaque = NULL; 4136b21b973Sbellard bs->drv = NULL; 41451d7c00cSaliguori unlink_and_fail: 41551d7c00cSaliguori if (bs->is_temporary) 41651d7c00cSaliguori unlink(filename); 41783f64091Sbellard return ret; 418ea2384d3Sbellard } 419d15a771dSbellard if (drv->bdrv_getlength) { 420d15a771dSbellard bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 421d15a771dSbellard } 422ea2384d3Sbellard #ifndef _WIN32 423ea2384d3Sbellard if (bs->is_temporary) { 424ea2384d3Sbellard unlink(filename); 42533e3963eSbellard } 42667b915a5Sbellard #endif 42783f64091Sbellard if (bs->backing_file[0] != '\0') { 428ea2384d3Sbellard /* if there is a backing file, use it */ 429ea2384d3Sbellard bs->backing_hd = bdrv_new(""); 43083f64091Sbellard path_combine(backing_filename, sizeof(backing_filename), 43183f64091Sbellard filename, bs->backing_file); 43251d7c00cSaliguori ret = bdrv_open(bs->backing_hd, backing_filename, open_flags); 43351d7c00cSaliguori if (ret < 0) { 43451d7c00cSaliguori bdrv_close(bs); 43551d7c00cSaliguori return ret; 43651d7c00cSaliguori } 437ea2384d3Sbellard } 43833e3963eSbellard 439bb5fc20fSaliguori if (!bdrv_key_required(bs)) { 440b338082bSbellard /* call the change callback */ 44119cb3738Sbellard bs->media_changed = 1; 442b338082bSbellard if (bs->change_cb) 443b338082bSbellard bs->change_cb(bs->change_opaque); 444bb5fc20fSaliguori } 445b338082bSbellard return 0; 446fc01f7e7Sbellard } 447fc01f7e7Sbellard 448fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 449fc01f7e7Sbellard { 45019cb3738Sbellard if (bs->drv) { 451ea2384d3Sbellard if (bs->backing_hd) 452ea2384d3Sbellard bdrv_delete(bs->backing_hd); 453ea2384d3Sbellard bs->drv->bdrv_close(bs); 454ea2384d3Sbellard qemu_free(bs->opaque); 455ea2384d3Sbellard #ifdef _WIN32 456ea2384d3Sbellard if (bs->is_temporary) { 457ea2384d3Sbellard unlink(bs->filename); 458ea2384d3Sbellard } 45967b915a5Sbellard #endif 460ea2384d3Sbellard bs->opaque = NULL; 461ea2384d3Sbellard bs->drv = NULL; 462b338082bSbellard 463b338082bSbellard /* call the change callback */ 46419cb3738Sbellard bs->media_changed = 1; 465b338082bSbellard if (bs->change_cb) 466b338082bSbellard bs->change_cb(bs->change_opaque); 467b338082bSbellard } 468b338082bSbellard } 469b338082bSbellard 470b338082bSbellard void bdrv_delete(BlockDriverState *bs) 471b338082bSbellard { 47234c6f050Saurel32 BlockDriverState **pbs; 47334c6f050Saurel32 47434c6f050Saurel32 pbs = &bdrv_first; 47534c6f050Saurel32 while (*pbs != bs && *pbs != NULL) 47634c6f050Saurel32 pbs = &(*pbs)->next; 47734c6f050Saurel32 if (*pbs == bs) 47834c6f050Saurel32 *pbs = bs->next; 47934c6f050Saurel32 480b338082bSbellard bdrv_close(bs); 481b338082bSbellard qemu_free(bs); 482fc01f7e7Sbellard } 483fc01f7e7Sbellard 48433e3963eSbellard /* commit COW file into the raw image */ 48533e3963eSbellard int bdrv_commit(BlockDriverState *bs) 48633e3963eSbellard { 48719cb3738Sbellard BlockDriver *drv = bs->drv; 48883f64091Sbellard int64_t i, total_sectors; 489ea2384d3Sbellard int n, j; 490ea2384d3Sbellard unsigned char sector[512]; 49133e3963eSbellard 49219cb3738Sbellard if (!drv) 49319cb3738Sbellard return -ENOMEDIUM; 49433e3963eSbellard 49533e3963eSbellard if (bs->read_only) { 496ea2384d3Sbellard return -EACCES; 49733e3963eSbellard } 49833e3963eSbellard 499ea2384d3Sbellard if (!bs->backing_hd) { 500ea2384d3Sbellard return -ENOTSUP; 501ea2384d3Sbellard } 502ea2384d3Sbellard 50383f64091Sbellard total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 50483f64091Sbellard for (i = 0; i < total_sectors;) { 50519cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 506ea2384d3Sbellard for(j = 0; j < n; j++) { 50733e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 508ea2384d3Sbellard return -EIO; 50933e3963eSbellard } 51033e3963eSbellard 511ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 512ea2384d3Sbellard return -EIO; 51333e3963eSbellard } 514ea2384d3Sbellard i++; 515ea2384d3Sbellard } 516ea2384d3Sbellard } else { 517ea2384d3Sbellard i += n; 51833e3963eSbellard } 51933e3963eSbellard } 52095389c86Sbellard 52119cb3738Sbellard if (drv->bdrv_make_empty) 52219cb3738Sbellard return drv->bdrv_make_empty(bs); 52395389c86Sbellard 52433e3963eSbellard return 0; 52533e3963eSbellard } 52633e3963eSbellard 52771d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 52871d0770cSaliguori size_t size) 52971d0770cSaliguori { 53071d0770cSaliguori int64_t len; 53171d0770cSaliguori 53271d0770cSaliguori if (!bdrv_is_inserted(bs)) 53371d0770cSaliguori return -ENOMEDIUM; 53471d0770cSaliguori 53571d0770cSaliguori if (bs->growable) 53671d0770cSaliguori return 0; 53771d0770cSaliguori 53871d0770cSaliguori len = bdrv_getlength(bs); 53971d0770cSaliguori 54071d0770cSaliguori if ((offset + size) > len) 54171d0770cSaliguori return -EIO; 54271d0770cSaliguori 54371d0770cSaliguori return 0; 54471d0770cSaliguori } 54571d0770cSaliguori 54671d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 54771d0770cSaliguori int nb_sectors) 54871d0770cSaliguori { 54971d0770cSaliguori int64_t offset; 55071d0770cSaliguori 55171d0770cSaliguori /* Deal with byte accesses */ 55271d0770cSaliguori if (sector_num < 0) 55371d0770cSaliguori offset = -sector_num; 55471d0770cSaliguori else 55571d0770cSaliguori offset = sector_num * 512; 55671d0770cSaliguori 55771d0770cSaliguori return bdrv_check_byte_request(bs, offset, nb_sectors * 512); 55871d0770cSaliguori } 55971d0770cSaliguori 56019cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 561fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 562fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 563fc01f7e7Sbellard { 564ea2384d3Sbellard BlockDriver *drv = bs->drv; 565fc01f7e7Sbellard 56619cb3738Sbellard if (!drv) 56719cb3738Sbellard return -ENOMEDIUM; 56871d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 56971d0770cSaliguori return -EIO; 570b338082bSbellard 57183f64091Sbellard if (drv->bdrv_pread) { 57283f64091Sbellard int ret, len; 57383f64091Sbellard len = nb_sectors * 512; 57483f64091Sbellard ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); 57583f64091Sbellard if (ret < 0) 57683f64091Sbellard return ret; 57783f64091Sbellard else if (ret != len) 57819cb3738Sbellard return -EINVAL; 579a36e69ddSths else { 580a36e69ddSths bs->rd_bytes += (unsigned) len; 581a36e69ddSths bs->rd_ops ++; 58283f64091Sbellard return 0; 583a36e69ddSths } 58483f64091Sbellard } else { 58583f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 58683f64091Sbellard } 58783f64091Sbellard } 588fc01f7e7Sbellard 58919cb3738Sbellard /* Return < 0 if error. Important errors are: 59019cb3738Sbellard -EIO generic I/O error (may happen for all errors) 59119cb3738Sbellard -ENOMEDIUM No media inserted. 59219cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 59319cb3738Sbellard -EACCES Trying to write a read-only device 59419cb3738Sbellard */ 595fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 596fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 597fc01f7e7Sbellard { 59883f64091Sbellard BlockDriver *drv = bs->drv; 59919cb3738Sbellard if (!bs->drv) 60019cb3738Sbellard return -ENOMEDIUM; 6010849bf08Sbellard if (bs->read_only) 60219cb3738Sbellard return -EACCES; 60371d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 60471d0770cSaliguori return -EIO; 60571d0770cSaliguori 60683f64091Sbellard if (drv->bdrv_pwrite) { 60742fb2807Saliguori int ret, len, count = 0; 60883f64091Sbellard len = nb_sectors * 512; 60942fb2807Saliguori do { 61042fb2807Saliguori ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len - count); 61142fb2807Saliguori if (ret < 0) { 61242fb2807Saliguori printf("bdrv_write ret=%d\n", ret); 61383f64091Sbellard return ret; 61442fb2807Saliguori } 61542fb2807Saliguori count += ret; 61642fb2807Saliguori buf += ret; 61742fb2807Saliguori } while (count != len); 618a36e69ddSths bs->wr_bytes += (unsigned) len; 619a36e69ddSths bs->wr_ops ++; 62083f64091Sbellard return 0; 621a36e69ddSths } 62283f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 62383f64091Sbellard } 62483f64091Sbellard 62583f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, 626faea38e7Sbellard uint8_t *buf, int count1) 62783f64091Sbellard { 62883f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 62983f64091Sbellard int len, nb_sectors, count; 63083f64091Sbellard int64_t sector_num; 63183f64091Sbellard 63283f64091Sbellard count = count1; 63383f64091Sbellard /* first read to align to sector start */ 63483f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 63583f64091Sbellard if (len > count) 63683f64091Sbellard len = count; 63783f64091Sbellard sector_num = offset >> SECTOR_BITS; 63883f64091Sbellard if (len > 0) { 63983f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 64083f64091Sbellard return -EIO; 64183f64091Sbellard memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); 64283f64091Sbellard count -= len; 64383f64091Sbellard if (count == 0) 64483f64091Sbellard return count1; 64583f64091Sbellard sector_num++; 64683f64091Sbellard buf += len; 64783f64091Sbellard } 64883f64091Sbellard 64983f64091Sbellard /* read the sectors "in place" */ 65083f64091Sbellard nb_sectors = count >> SECTOR_BITS; 65183f64091Sbellard if (nb_sectors > 0) { 65283f64091Sbellard if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) 65383f64091Sbellard return -EIO; 65483f64091Sbellard sector_num += nb_sectors; 65583f64091Sbellard len = nb_sectors << SECTOR_BITS; 65683f64091Sbellard buf += len; 65783f64091Sbellard count -= len; 65883f64091Sbellard } 65983f64091Sbellard 66083f64091Sbellard /* add data from the last sector */ 66183f64091Sbellard if (count > 0) { 66283f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 66383f64091Sbellard return -EIO; 66483f64091Sbellard memcpy(buf, tmp_buf, count); 66583f64091Sbellard } 66683f64091Sbellard return count1; 66783f64091Sbellard } 66883f64091Sbellard 66983f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, 670faea38e7Sbellard const uint8_t *buf, int count1) 67183f64091Sbellard { 67283f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 67383f64091Sbellard int len, nb_sectors, count; 67483f64091Sbellard int64_t sector_num; 67583f64091Sbellard 67683f64091Sbellard count = count1; 67783f64091Sbellard /* first write to align to sector start */ 67883f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 67983f64091Sbellard if (len > count) 68083f64091Sbellard len = count; 68183f64091Sbellard sector_num = offset >> SECTOR_BITS; 68283f64091Sbellard if (len > 0) { 68383f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 68483f64091Sbellard return -EIO; 68583f64091Sbellard memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); 68683f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 68783f64091Sbellard return -EIO; 68883f64091Sbellard count -= len; 68983f64091Sbellard if (count == 0) 69083f64091Sbellard return count1; 69183f64091Sbellard sector_num++; 69283f64091Sbellard buf += len; 69383f64091Sbellard } 69483f64091Sbellard 69583f64091Sbellard /* write the sectors "in place" */ 69683f64091Sbellard nb_sectors = count >> SECTOR_BITS; 69783f64091Sbellard if (nb_sectors > 0) { 69883f64091Sbellard if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) 69983f64091Sbellard return -EIO; 70083f64091Sbellard sector_num += nb_sectors; 70183f64091Sbellard len = nb_sectors << SECTOR_BITS; 70283f64091Sbellard buf += len; 70383f64091Sbellard count -= len; 70483f64091Sbellard } 70583f64091Sbellard 70683f64091Sbellard /* add data from the last sector */ 70783f64091Sbellard if (count > 0) { 70883f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 70983f64091Sbellard return -EIO; 71083f64091Sbellard memcpy(tmp_buf, buf, count); 71183f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 71283f64091Sbellard return -EIO; 71383f64091Sbellard } 71483f64091Sbellard return count1; 71583f64091Sbellard } 71683f64091Sbellard 71783f64091Sbellard /** 71883f64091Sbellard * Read with byte offsets (needed only for file protocols) 71983f64091Sbellard */ 72083f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset, 72183f64091Sbellard void *buf1, int count1) 72283f64091Sbellard { 72383f64091Sbellard BlockDriver *drv = bs->drv; 72483f64091Sbellard 72583f64091Sbellard if (!drv) 72619cb3738Sbellard return -ENOMEDIUM; 72771d0770cSaliguori if (bdrv_check_byte_request(bs, offset, count1)) 72871d0770cSaliguori return -EIO; 72971d0770cSaliguori 73083f64091Sbellard if (!drv->bdrv_pread) 731faea38e7Sbellard return bdrv_pread_em(bs, offset, buf1, count1); 73283f64091Sbellard return drv->bdrv_pread(bs, offset, buf1, count1); 73383f64091Sbellard } 73483f64091Sbellard 73583f64091Sbellard /** 73683f64091Sbellard * Write with byte offsets (needed only for file protocols) 73783f64091Sbellard */ 73883f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 73983f64091Sbellard const void *buf1, int count1) 74083f64091Sbellard { 74183f64091Sbellard BlockDriver *drv = bs->drv; 74283f64091Sbellard 74383f64091Sbellard if (!drv) 74419cb3738Sbellard return -ENOMEDIUM; 74571d0770cSaliguori if (bdrv_check_byte_request(bs, offset, count1)) 74671d0770cSaliguori return -EIO; 74771d0770cSaliguori 74883f64091Sbellard if (!drv->bdrv_pwrite) 749faea38e7Sbellard return bdrv_pwrite_em(bs, offset, buf1, count1); 75083f64091Sbellard return drv->bdrv_pwrite(bs, offset, buf1, count1); 75183f64091Sbellard } 75283f64091Sbellard 75383f64091Sbellard /** 75483f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 75583f64091Sbellard */ 75683f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 75783f64091Sbellard { 75883f64091Sbellard BlockDriver *drv = bs->drv; 75983f64091Sbellard if (!drv) 76019cb3738Sbellard return -ENOMEDIUM; 76183f64091Sbellard if (!drv->bdrv_truncate) 76283f64091Sbellard return -ENOTSUP; 76383f64091Sbellard return drv->bdrv_truncate(bs, offset); 76483f64091Sbellard } 76583f64091Sbellard 76683f64091Sbellard /** 76783f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 76883f64091Sbellard */ 76983f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 77083f64091Sbellard { 77183f64091Sbellard BlockDriver *drv = bs->drv; 77283f64091Sbellard if (!drv) 77319cb3738Sbellard return -ENOMEDIUM; 77483f64091Sbellard if (!drv->bdrv_getlength) { 77583f64091Sbellard /* legacy mode */ 77683f64091Sbellard return bs->total_sectors * SECTOR_SIZE; 77783f64091Sbellard } 77883f64091Sbellard return drv->bdrv_getlength(bs); 779fc01f7e7Sbellard } 780fc01f7e7Sbellard 78119cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 78296b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 783fc01f7e7Sbellard { 78419cb3738Sbellard int64_t length; 78519cb3738Sbellard length = bdrv_getlength(bs); 78619cb3738Sbellard if (length < 0) 78719cb3738Sbellard length = 0; 78819cb3738Sbellard else 78919cb3738Sbellard length = length >> SECTOR_BITS; 79019cb3738Sbellard *nb_sectors_ptr = length; 791fc01f7e7Sbellard } 792cf98951bSbellard 793f3d54fc4Saliguori struct partition { 794f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 795f3d54fc4Saliguori uint8_t head; /* starting head */ 796f3d54fc4Saliguori uint8_t sector; /* starting sector */ 797f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 798f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 799f3d54fc4Saliguori uint8_t end_head; /* end head */ 800f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 801f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 802f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 803f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 804f3d54fc4Saliguori } __attribute__((packed)); 805f3d54fc4Saliguori 806f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 807f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 808f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 809f3d54fc4Saliguori { 810f3d54fc4Saliguori uint8_t buf[512]; 811f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 812f3d54fc4Saliguori struct partition *p; 813f3d54fc4Saliguori uint32_t nr_sects; 814a38131b6Sblueswir1 uint64_t nb_sectors; 815f3d54fc4Saliguori 816f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 817f3d54fc4Saliguori 818f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 819f3d54fc4Saliguori if (ret < 0) 820f3d54fc4Saliguori return -1; 821f3d54fc4Saliguori /* test msdos magic */ 822f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 823f3d54fc4Saliguori return -1; 824f3d54fc4Saliguori for(i = 0; i < 4; i++) { 825f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 826f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 827f3d54fc4Saliguori if (nr_sects && p->end_head) { 828f3d54fc4Saliguori /* We make the assumption that the partition terminates on 829f3d54fc4Saliguori a cylinder boundary */ 830f3d54fc4Saliguori heads = p->end_head + 1; 831f3d54fc4Saliguori sectors = p->end_sector & 63; 832f3d54fc4Saliguori if (sectors == 0) 833f3d54fc4Saliguori continue; 834f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 835f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 836f3d54fc4Saliguori continue; 837f3d54fc4Saliguori *pheads = heads; 838f3d54fc4Saliguori *psectors = sectors; 839f3d54fc4Saliguori *pcylinders = cylinders; 840f3d54fc4Saliguori #if 0 841f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 842f3d54fc4Saliguori cylinders, heads, sectors); 843f3d54fc4Saliguori #endif 844f3d54fc4Saliguori return 0; 845f3d54fc4Saliguori } 846f3d54fc4Saliguori } 847f3d54fc4Saliguori return -1; 848f3d54fc4Saliguori } 849f3d54fc4Saliguori 850f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 851f3d54fc4Saliguori { 852f3d54fc4Saliguori int translation, lba_detected = 0; 853f3d54fc4Saliguori int cylinders, heads, secs; 854a38131b6Sblueswir1 uint64_t nb_sectors; 855f3d54fc4Saliguori 856f3d54fc4Saliguori /* if a geometry hint is available, use it */ 857f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 858f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 859f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 860f3d54fc4Saliguori if (cylinders != 0) { 861f3d54fc4Saliguori *pcyls = cylinders; 862f3d54fc4Saliguori *pheads = heads; 863f3d54fc4Saliguori *psecs = secs; 864f3d54fc4Saliguori } else { 865f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 866f3d54fc4Saliguori if (heads > 16) { 867f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 868f3d54fc4Saliguori translation was active, so the default 869f3d54fc4Saliguori hardware geometry is OK */ 870f3d54fc4Saliguori lba_detected = 1; 871f3d54fc4Saliguori goto default_geometry; 872f3d54fc4Saliguori } else { 873f3d54fc4Saliguori *pcyls = cylinders; 874f3d54fc4Saliguori *pheads = heads; 875f3d54fc4Saliguori *psecs = secs; 876f3d54fc4Saliguori /* disable any translation to be in sync with 877f3d54fc4Saliguori the logical geometry */ 878f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 879f3d54fc4Saliguori bdrv_set_translation_hint(bs, 880f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 881f3d54fc4Saliguori } 882f3d54fc4Saliguori } 883f3d54fc4Saliguori } else { 884f3d54fc4Saliguori default_geometry: 885f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 886f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 887f3d54fc4Saliguori 888f3d54fc4Saliguori if (cylinders > 16383) 889f3d54fc4Saliguori cylinders = 16383; 890f3d54fc4Saliguori else if (cylinders < 2) 891f3d54fc4Saliguori cylinders = 2; 892f3d54fc4Saliguori *pcyls = cylinders; 893f3d54fc4Saliguori *pheads = 16; 894f3d54fc4Saliguori *psecs = 63; 895f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 896f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 897f3d54fc4Saliguori bdrv_set_translation_hint(bs, 898f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 899f3d54fc4Saliguori } else { 900f3d54fc4Saliguori bdrv_set_translation_hint(bs, 901f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 902f3d54fc4Saliguori } 903f3d54fc4Saliguori } 904f3d54fc4Saliguori } 905f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 906f3d54fc4Saliguori } 907f3d54fc4Saliguori } 908f3d54fc4Saliguori 909b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 910b338082bSbellard int cyls, int heads, int secs) 911b338082bSbellard { 912b338082bSbellard bs->cyls = cyls; 913b338082bSbellard bs->heads = heads; 914b338082bSbellard bs->secs = secs; 915b338082bSbellard } 916b338082bSbellard 917b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 918b338082bSbellard { 919b338082bSbellard bs->type = type; 920b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 921b338082bSbellard type == BDRV_TYPE_FLOPPY)); 922b338082bSbellard } 923b338082bSbellard 92446d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 92546d4767dSbellard { 92646d4767dSbellard bs->translation = translation; 92746d4767dSbellard } 92846d4767dSbellard 929b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 930b338082bSbellard int *pcyls, int *pheads, int *psecs) 931b338082bSbellard { 932b338082bSbellard *pcyls = bs->cyls; 933b338082bSbellard *pheads = bs->heads; 934b338082bSbellard *psecs = bs->secs; 935b338082bSbellard } 936b338082bSbellard 937b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 938b338082bSbellard { 939b338082bSbellard return bs->type; 940b338082bSbellard } 941b338082bSbellard 94246d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 94346d4767dSbellard { 94446d4767dSbellard return bs->translation; 94546d4767dSbellard } 94646d4767dSbellard 947b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 948b338082bSbellard { 949b338082bSbellard return bs->removable; 950b338082bSbellard } 951b338082bSbellard 952b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 953b338082bSbellard { 954b338082bSbellard return bs->read_only; 955b338082bSbellard } 956b338082bSbellard 957985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 958985a03b0Sths { 959985a03b0Sths return bs->sg; 960985a03b0Sths } 961985a03b0Sths 96219cb3738Sbellard /* XXX: no longer used */ 963b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 964b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 965b338082bSbellard { 966b338082bSbellard bs->change_cb = change_cb; 967b338082bSbellard bs->change_opaque = opaque; 968b338082bSbellard } 969b338082bSbellard 970ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 971ea2384d3Sbellard { 972ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 973ea2384d3Sbellard return 1; 974ea2384d3Sbellard return bs->encrypted; 975ea2384d3Sbellard } 976ea2384d3Sbellard 977c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 978c0f4ce77Saliguori { 979c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 980c0f4ce77Saliguori 981c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 982c0f4ce77Saliguori return 1; 983c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 984c0f4ce77Saliguori } 985c0f4ce77Saliguori 986ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 987ea2384d3Sbellard { 988ea2384d3Sbellard int ret; 989ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 990ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 991ea2384d3Sbellard if (ret < 0) 992ea2384d3Sbellard return ret; 993ea2384d3Sbellard if (!bs->encrypted) 994ea2384d3Sbellard return 0; 995ea2384d3Sbellard } 996ea2384d3Sbellard if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) 997ea2384d3Sbellard return -1; 998c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 999bb5fc20fSaliguori if (ret < 0) { 1000bb5fc20fSaliguori bs->valid_key = 0; 1001bb5fc20fSaliguori } else if (!bs->valid_key) { 1002bb5fc20fSaliguori bs->valid_key = 1; 1003bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 1004bb5fc20fSaliguori bs->media_changed = 1; 1005bb5fc20fSaliguori if (bs->change_cb) 1006bb5fc20fSaliguori bs->change_cb(bs->change_opaque); 1007bb5fc20fSaliguori } 1008c0f4ce77Saliguori return ret; 1009ea2384d3Sbellard } 1010ea2384d3Sbellard 1011ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 1012ea2384d3Sbellard { 101319cb3738Sbellard if (!bs->drv) { 1014ea2384d3Sbellard buf[0] = '\0'; 1015ea2384d3Sbellard } else { 1016ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 1017ea2384d3Sbellard } 1018ea2384d3Sbellard } 1019ea2384d3Sbellard 1020ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 1021ea2384d3Sbellard void *opaque) 1022ea2384d3Sbellard { 1023ea2384d3Sbellard BlockDriver *drv; 1024ea2384d3Sbellard 1025ea2384d3Sbellard for (drv = first_drv; drv != NULL; drv = drv->next) { 1026ea2384d3Sbellard it(opaque, drv->format_name); 1027ea2384d3Sbellard } 1028ea2384d3Sbellard } 1029ea2384d3Sbellard 1030b338082bSbellard BlockDriverState *bdrv_find(const char *name) 1031b338082bSbellard { 1032b338082bSbellard BlockDriverState *bs; 1033b338082bSbellard 1034b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1035b338082bSbellard if (!strcmp(name, bs->device_name)) 1036b338082bSbellard return bs; 1037b338082bSbellard } 1038b338082bSbellard return NULL; 1039b338082bSbellard } 1040b338082bSbellard 104151de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 104281d0912dSbellard { 104381d0912dSbellard BlockDriverState *bs; 104481d0912dSbellard 104581d0912dSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 104651de9760Saliguori it(opaque, bs); 104781d0912dSbellard } 104881d0912dSbellard } 104981d0912dSbellard 1050ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1051ea2384d3Sbellard { 1052ea2384d3Sbellard return bs->device_name; 1053ea2384d3Sbellard } 1054ea2384d3Sbellard 10557a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 10567a6cba61Spbrook { 10577a6cba61Spbrook if (bs->drv->bdrv_flush) 10587a6cba61Spbrook bs->drv->bdrv_flush(bs); 10597a6cba61Spbrook if (bs->backing_hd) 10607a6cba61Spbrook bdrv_flush(bs->backing_hd); 10617a6cba61Spbrook } 10627a6cba61Spbrook 1063c6ca28d6Saliguori void bdrv_flush_all(void) 1064c6ca28d6Saliguori { 1065c6ca28d6Saliguori BlockDriverState *bs; 1066c6ca28d6Saliguori 1067c6ca28d6Saliguori for (bs = bdrv_first; bs != NULL; bs = bs->next) 1068c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 1069c6ca28d6Saliguori (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) 1070c6ca28d6Saliguori bdrv_flush(bs); 1071c6ca28d6Saliguori } 1072c6ca28d6Saliguori 1073f58c7b35Sths /* 1074f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1075f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1076f58c7b35Sths * hence all their sectors are reported as allocated. 1077f58c7b35Sths * 1078f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1079f58c7b35Sths * the specified sector) that are known to be in the same 1080f58c7b35Sths * allocated/unallocated state. 1081f58c7b35Sths * 1082f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1083f58c7b35Sths */ 1084f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1085f58c7b35Sths int *pnum) 1086f58c7b35Sths { 1087f58c7b35Sths int64_t n; 1088f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1089f58c7b35Sths if (sector_num >= bs->total_sectors) { 1090f58c7b35Sths *pnum = 0; 1091f58c7b35Sths return 0; 1092f58c7b35Sths } 1093f58c7b35Sths n = bs->total_sectors - sector_num; 1094f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1095f58c7b35Sths return 1; 1096f58c7b35Sths } 1097f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1098f58c7b35Sths } 1099f58c7b35Sths 1100376253ecSaliguori void bdrv_info(Monitor *mon) 1101b338082bSbellard { 1102b338082bSbellard BlockDriverState *bs; 1103b338082bSbellard 1104b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1105376253ecSaliguori monitor_printf(mon, "%s:", bs->device_name); 1106376253ecSaliguori monitor_printf(mon, " type="); 1107b338082bSbellard switch(bs->type) { 1108b338082bSbellard case BDRV_TYPE_HD: 1109376253ecSaliguori monitor_printf(mon, "hd"); 1110b338082bSbellard break; 1111b338082bSbellard case BDRV_TYPE_CDROM: 1112376253ecSaliguori monitor_printf(mon, "cdrom"); 1113b338082bSbellard break; 1114b338082bSbellard case BDRV_TYPE_FLOPPY: 1115376253ecSaliguori monitor_printf(mon, "floppy"); 1116b338082bSbellard break; 1117b338082bSbellard } 1118376253ecSaliguori monitor_printf(mon, " removable=%d", bs->removable); 1119b338082bSbellard if (bs->removable) { 1120376253ecSaliguori monitor_printf(mon, " locked=%d", bs->locked); 1121b338082bSbellard } 112219cb3738Sbellard if (bs->drv) { 1123376253ecSaliguori monitor_printf(mon, " file="); 1124376253ecSaliguori monitor_print_filename(mon, bs->filename); 1125fef30743Sths if (bs->backing_file[0] != '\0') { 1126376253ecSaliguori monitor_printf(mon, " backing_file="); 1127376253ecSaliguori monitor_print_filename(mon, bs->backing_file); 1128fef30743Sths } 1129376253ecSaliguori monitor_printf(mon, " ro=%d", bs->read_only); 1130376253ecSaliguori monitor_printf(mon, " drv=%s", bs->drv->format_name); 1131376253ecSaliguori monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs)); 1132b338082bSbellard } else { 1133376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1134b338082bSbellard } 1135376253ecSaliguori monitor_printf(mon, "\n"); 1136b338082bSbellard } 1137b338082bSbellard } 1138a36e69ddSths 1139a36e69ddSths /* The "info blockstats" command. */ 1140376253ecSaliguori void bdrv_info_stats(Monitor *mon) 1141a36e69ddSths { 1142a36e69ddSths BlockDriverState *bs; 1143a36e69ddSths 1144a36e69ddSths for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1145376253ecSaliguori monitor_printf(mon, "%s:" 1146a36e69ddSths " rd_bytes=%" PRIu64 1147a36e69ddSths " wr_bytes=%" PRIu64 1148a36e69ddSths " rd_operations=%" PRIu64 1149a36e69ddSths " wr_operations=%" PRIu64 1150*ebf53fcdSaliguori "\n", 1151a36e69ddSths bs->device_name, 1152a36e69ddSths bs->rd_bytes, bs->wr_bytes, 1153a36e69ddSths bs->rd_ops, bs->wr_ops); 1154a36e69ddSths } 1155a36e69ddSths } 1156ea2384d3Sbellard 1157045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1158045df330Saliguori { 1159045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1160045df330Saliguori return bs->backing_file; 1161045df330Saliguori else if (bs->encrypted) 1162045df330Saliguori return bs->filename; 1163045df330Saliguori else 1164045df330Saliguori return NULL; 1165045df330Saliguori } 1166045df330Saliguori 116783f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 116883f64091Sbellard char *filename, int filename_size) 116983f64091Sbellard { 117083f64091Sbellard if (!bs->backing_hd) { 117183f64091Sbellard pstrcpy(filename, filename_size, ""); 117283f64091Sbellard } else { 117383f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 117483f64091Sbellard } 117583f64091Sbellard } 117683f64091Sbellard 1177faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1178faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1179faea38e7Sbellard { 1180faea38e7Sbellard BlockDriver *drv = bs->drv; 1181faea38e7Sbellard if (!drv) 118219cb3738Sbellard return -ENOMEDIUM; 1183faea38e7Sbellard if (!drv->bdrv_write_compressed) 1184faea38e7Sbellard return -ENOTSUP; 1185faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1186faea38e7Sbellard } 1187faea38e7Sbellard 1188faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1189faea38e7Sbellard { 1190faea38e7Sbellard BlockDriver *drv = bs->drv; 1191faea38e7Sbellard if (!drv) 119219cb3738Sbellard return -ENOMEDIUM; 1193faea38e7Sbellard if (!drv->bdrv_get_info) 1194faea38e7Sbellard return -ENOTSUP; 1195faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1196faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1197faea38e7Sbellard } 1198faea38e7Sbellard 1199faea38e7Sbellard /**************************************************************/ 1200faea38e7Sbellard /* handling of snapshots */ 1201faea38e7Sbellard 1202faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1203faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1204faea38e7Sbellard { 1205faea38e7Sbellard BlockDriver *drv = bs->drv; 1206faea38e7Sbellard if (!drv) 120719cb3738Sbellard return -ENOMEDIUM; 1208faea38e7Sbellard if (!drv->bdrv_snapshot_create) 1209faea38e7Sbellard return -ENOTSUP; 1210faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 1211faea38e7Sbellard } 1212faea38e7Sbellard 1213faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1214faea38e7Sbellard const char *snapshot_id) 1215faea38e7Sbellard { 1216faea38e7Sbellard BlockDriver *drv = bs->drv; 1217faea38e7Sbellard if (!drv) 121819cb3738Sbellard return -ENOMEDIUM; 1219faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 1220faea38e7Sbellard return -ENOTSUP; 1221faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 1222faea38e7Sbellard } 1223faea38e7Sbellard 1224faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1225faea38e7Sbellard { 1226faea38e7Sbellard BlockDriver *drv = bs->drv; 1227faea38e7Sbellard if (!drv) 122819cb3738Sbellard return -ENOMEDIUM; 1229faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 1230faea38e7Sbellard return -ENOTSUP; 1231faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 1232faea38e7Sbellard } 1233faea38e7Sbellard 1234faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1235faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1236faea38e7Sbellard { 1237faea38e7Sbellard BlockDriver *drv = bs->drv; 1238faea38e7Sbellard if (!drv) 123919cb3738Sbellard return -ENOMEDIUM; 1240faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1241faea38e7Sbellard return -ENOTSUP; 1242faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1243faea38e7Sbellard } 1244faea38e7Sbellard 1245faea38e7Sbellard #define NB_SUFFIXES 4 1246faea38e7Sbellard 1247faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1248faea38e7Sbellard { 1249faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1250faea38e7Sbellard int64_t base; 1251faea38e7Sbellard int i; 1252faea38e7Sbellard 1253faea38e7Sbellard if (size <= 999) { 1254faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1255faea38e7Sbellard } else { 1256faea38e7Sbellard base = 1024; 1257faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1258faea38e7Sbellard if (size < (10 * base)) { 1259faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1260faea38e7Sbellard (double)size / base, 1261faea38e7Sbellard suffixes[i]); 1262faea38e7Sbellard break; 1263faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1264faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1265faea38e7Sbellard ((size + (base >> 1)) / base), 1266faea38e7Sbellard suffixes[i]); 1267faea38e7Sbellard break; 1268faea38e7Sbellard } 1269faea38e7Sbellard base = base * 1024; 1270faea38e7Sbellard } 1271faea38e7Sbellard } 1272faea38e7Sbellard return buf; 1273faea38e7Sbellard } 1274faea38e7Sbellard 1275faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1276faea38e7Sbellard { 1277faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 12783b9f94e1Sbellard #ifdef _WIN32 12793b9f94e1Sbellard struct tm *ptm; 12803b9f94e1Sbellard #else 1281faea38e7Sbellard struct tm tm; 12823b9f94e1Sbellard #endif 1283faea38e7Sbellard time_t ti; 1284faea38e7Sbellard int64_t secs; 1285faea38e7Sbellard 1286faea38e7Sbellard if (!sn) { 1287faea38e7Sbellard snprintf(buf, buf_size, 1288faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1289faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1290faea38e7Sbellard } else { 1291faea38e7Sbellard ti = sn->date_sec; 12923b9f94e1Sbellard #ifdef _WIN32 12933b9f94e1Sbellard ptm = localtime(&ti); 12943b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 12953b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 12963b9f94e1Sbellard #else 1297faea38e7Sbellard localtime_r(&ti, &tm); 1298faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1299faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 13003b9f94e1Sbellard #endif 1301faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1302faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1303faea38e7Sbellard "%02d:%02d:%02d.%03d", 1304faea38e7Sbellard (int)(secs / 3600), 1305faea38e7Sbellard (int)((secs / 60) % 60), 1306faea38e7Sbellard (int)(secs % 60), 1307faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1308faea38e7Sbellard snprintf(buf, buf_size, 1309faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1310faea38e7Sbellard sn->id_str, sn->name, 1311faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1312faea38e7Sbellard date_buf, 1313faea38e7Sbellard clock_buf); 1314faea38e7Sbellard } 1315faea38e7Sbellard return buf; 1316faea38e7Sbellard } 1317faea38e7Sbellard 131883f64091Sbellard 1319ea2384d3Sbellard /**************************************************************/ 132083f64091Sbellard /* async I/Os */ 1321ea2384d3Sbellard 13223b69e4b9Saliguori typedef struct VectorTranslationState { 13233b69e4b9Saliguori QEMUIOVector *iov; 13243b69e4b9Saliguori uint8_t *bounce; 13253b69e4b9Saliguori int is_write; 13263b69e4b9Saliguori BlockDriverAIOCB *aiocb; 13273b69e4b9Saliguori BlockDriverAIOCB *this_aiocb; 13283b69e4b9Saliguori } VectorTranslationState; 13293b69e4b9Saliguori 13303b69e4b9Saliguori static void bdrv_aio_rw_vector_cb(void *opaque, int ret) 13313b69e4b9Saliguori { 13323b69e4b9Saliguori VectorTranslationState *s = opaque; 13333b69e4b9Saliguori 13343b69e4b9Saliguori if (!s->is_write) { 1335249aa745Saliguori qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size); 13363b69e4b9Saliguori } 1337d905dba4Saurel32 qemu_vfree(s->bounce); 13383b69e4b9Saliguori s->this_aiocb->cb(s->this_aiocb->opaque, ret); 13393b69e4b9Saliguori qemu_aio_release(s->this_aiocb); 13403b69e4b9Saliguori } 13413b69e4b9Saliguori 13423b69e4b9Saliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 13433b69e4b9Saliguori int64_t sector_num, 13443b69e4b9Saliguori QEMUIOVector *iov, 13453b69e4b9Saliguori int nb_sectors, 13463b69e4b9Saliguori BlockDriverCompletionFunc *cb, 13473b69e4b9Saliguori void *opaque, 13483b69e4b9Saliguori int is_write) 13493b69e4b9Saliguori 13503b69e4b9Saliguori { 13513b69e4b9Saliguori VectorTranslationState *s = qemu_mallocz(sizeof(*s)); 13523b69e4b9Saliguori BlockDriverAIOCB *aiocb = qemu_aio_get(bs, cb, opaque); 13533b69e4b9Saliguori 13543b69e4b9Saliguori s->this_aiocb = aiocb; 13553b69e4b9Saliguori s->iov = iov; 13563b69e4b9Saliguori s->bounce = qemu_memalign(512, nb_sectors * 512); 13573b69e4b9Saliguori s->is_write = is_write; 13583b69e4b9Saliguori if (is_write) { 13593b69e4b9Saliguori qemu_iovec_to_buffer(s->iov, s->bounce); 13603b69e4b9Saliguori s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors, 13613b69e4b9Saliguori bdrv_aio_rw_vector_cb, s); 13623b69e4b9Saliguori } else { 13633b69e4b9Saliguori s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors, 13643b69e4b9Saliguori bdrv_aio_rw_vector_cb, s); 13653b69e4b9Saliguori } 13663b69e4b9Saliguori return aiocb; 13673b69e4b9Saliguori } 13683b69e4b9Saliguori 13693b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 13703b69e4b9Saliguori QEMUIOVector *iov, int nb_sectors, 13713b69e4b9Saliguori BlockDriverCompletionFunc *cb, void *opaque) 13723b69e4b9Saliguori { 137371d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 137471d0770cSaliguori return NULL; 137571d0770cSaliguori 13763b69e4b9Saliguori return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, 13773b69e4b9Saliguori cb, opaque, 0); 13783b69e4b9Saliguori } 13793b69e4b9Saliguori 13803b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 13813b69e4b9Saliguori QEMUIOVector *iov, int nb_sectors, 13823b69e4b9Saliguori BlockDriverCompletionFunc *cb, void *opaque) 13833b69e4b9Saliguori { 138471d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 138571d0770cSaliguori return NULL; 138671d0770cSaliguori 13873b69e4b9Saliguori return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, 13883b69e4b9Saliguori cb, opaque, 1); 13893b69e4b9Saliguori } 13903b69e4b9Saliguori 1391ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, 139283f64091Sbellard uint8_t *buf, int nb_sectors, 139383f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1394ea2384d3Sbellard { 139583f64091Sbellard BlockDriver *drv = bs->drv; 1396a36e69ddSths BlockDriverAIOCB *ret; 1397ea2384d3Sbellard 139819cb3738Sbellard if (!drv) 1399ce1a14dcSpbrook return NULL; 140071d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 140171d0770cSaliguori return NULL; 140283f64091Sbellard 1403a36e69ddSths ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); 1404a36e69ddSths 1405a36e69ddSths if (ret) { 1406a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1407a36e69ddSths bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1408a36e69ddSths bs->rd_ops ++; 1409a36e69ddSths } 1410a36e69ddSths 1411a36e69ddSths return ret; 141283f64091Sbellard } 141383f64091Sbellard 1414ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, 141583f64091Sbellard const uint8_t *buf, int nb_sectors, 141683f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 14177674e7bfSbellard { 141883f64091Sbellard BlockDriver *drv = bs->drv; 1419a36e69ddSths BlockDriverAIOCB *ret; 142083f64091Sbellard 142119cb3738Sbellard if (!drv) 1422ce1a14dcSpbrook return NULL; 142383f64091Sbellard if (bs->read_only) 1424ce1a14dcSpbrook return NULL; 142571d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 142671d0770cSaliguori return NULL; 142783f64091Sbellard 1428a36e69ddSths ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); 1429a36e69ddSths 1430a36e69ddSths if (ret) { 1431a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1432a36e69ddSths bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1433a36e69ddSths bs->wr_ops ++; 1434a36e69ddSths } 1435a36e69ddSths 1436a36e69ddSths return ret; 143783f64091Sbellard } 143883f64091Sbellard 143983f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 144083f64091Sbellard { 1441ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 144283f64091Sbellard 14433b69e4b9Saliguori if (acb->cb == bdrv_aio_rw_vector_cb) { 14443b69e4b9Saliguori VectorTranslationState *s = acb->opaque; 14453b69e4b9Saliguori acb = s->aiocb; 14463b69e4b9Saliguori } 14473b69e4b9Saliguori 144883f64091Sbellard drv->bdrv_aio_cancel(acb); 144983f64091Sbellard } 145083f64091Sbellard 145183f64091Sbellard 145283f64091Sbellard /**************************************************************/ 145383f64091Sbellard /* async block device emulation */ 145483f64091Sbellard 145583f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 1456beac80cdSbellard { 1457ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 1458ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 1459ce1a14dcSpbrook qemu_aio_release(acb); 1460beac80cdSbellard } 1461beac80cdSbellard 1462ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 1463ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 1464ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 1465ea2384d3Sbellard { 1466ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 146783f64091Sbellard int ret; 146883f64091Sbellard 1469ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1470ce1a14dcSpbrook if (!acb->bh) 1471ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1472ce1a14dcSpbrook ret = bdrv_read(bs, sector_num, buf, nb_sectors); 1473ce1a14dcSpbrook acb->ret = ret; 1474ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1475ce1a14dcSpbrook return &acb->common; 14767a6cba61Spbrook } 14777a6cba61Spbrook 1478ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 1479ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 1480ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 148183f64091Sbellard { 1482ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 148383f64091Sbellard int ret; 148483f64091Sbellard 1485ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1486ce1a14dcSpbrook if (!acb->bh) 1487ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1488ce1a14dcSpbrook ret = bdrv_write(bs, sector_num, buf, nb_sectors); 1489ce1a14dcSpbrook acb->ret = ret; 1490ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1491ce1a14dcSpbrook return &acb->common; 149283f64091Sbellard } 149383f64091Sbellard 1494ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 149583f64091Sbellard { 1496ce1a14dcSpbrook BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; 1497ce1a14dcSpbrook qemu_bh_cancel(acb->bh); 1498ce1a14dcSpbrook qemu_aio_release(acb); 149983f64091Sbellard } 150083f64091Sbellard 150183f64091Sbellard /**************************************************************/ 150283f64091Sbellard /* sync block device emulation */ 150383f64091Sbellard 150483f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 150583f64091Sbellard { 150683f64091Sbellard *(int *)opaque = ret; 150783f64091Sbellard } 150883f64091Sbellard 150983f64091Sbellard #define NOT_DONE 0x7fffffff 151083f64091Sbellard 151183f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 151283f64091Sbellard uint8_t *buf, int nb_sectors) 151383f64091Sbellard { 1514ce1a14dcSpbrook int async_ret; 1515ce1a14dcSpbrook BlockDriverAIOCB *acb; 151683f64091Sbellard 151783f64091Sbellard async_ret = NOT_DONE; 1518ce1a14dcSpbrook acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, 151983f64091Sbellard bdrv_rw_em_cb, &async_ret); 1520baf35cb9Saliguori if (acb == NULL) 1521ce1a14dcSpbrook return -1; 1522baf35cb9Saliguori 152383f64091Sbellard while (async_ret == NOT_DONE) { 152483f64091Sbellard qemu_aio_wait(); 152583f64091Sbellard } 1526baf35cb9Saliguori 152783f64091Sbellard return async_ret; 152883f64091Sbellard } 152983f64091Sbellard 153083f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 153183f64091Sbellard const uint8_t *buf, int nb_sectors) 153283f64091Sbellard { 1533ce1a14dcSpbrook int async_ret; 1534ce1a14dcSpbrook BlockDriverAIOCB *acb; 153583f64091Sbellard 153683f64091Sbellard async_ret = NOT_DONE; 1537ce1a14dcSpbrook acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, 153883f64091Sbellard bdrv_rw_em_cb, &async_ret); 1539baf35cb9Saliguori if (acb == NULL) 1540ce1a14dcSpbrook return -1; 154183f64091Sbellard while (async_ret == NOT_DONE) { 154283f64091Sbellard qemu_aio_wait(); 154383f64091Sbellard } 154483f64091Sbellard return async_ret; 154583f64091Sbellard } 1546ea2384d3Sbellard 1547ea2384d3Sbellard void bdrv_init(void) 1548ea2384d3Sbellard { 1549ea2384d3Sbellard bdrv_register(&bdrv_raw); 155019cb3738Sbellard bdrv_register(&bdrv_host_device); 1551ea2384d3Sbellard #ifndef _WIN32 1552ea2384d3Sbellard bdrv_register(&bdrv_cow); 1553ea2384d3Sbellard #endif 1554ea2384d3Sbellard bdrv_register(&bdrv_qcow); 1555ea2384d3Sbellard bdrv_register(&bdrv_vmdk); 15563c56521bSbellard bdrv_register(&bdrv_cloop); 1557585d0ed9Sbellard bdrv_register(&bdrv_dmg); 1558a8753c34Sbellard bdrv_register(&bdrv_bochs); 15596a0f9e82Sbellard bdrv_register(&bdrv_vpc); 1560712e7874Sbellard bdrv_register(&bdrv_vvfat); 1561faea38e7Sbellard bdrv_register(&bdrv_qcow2); 15626ada7453Sths bdrv_register(&bdrv_parallels); 156375818250Sths bdrv_register(&bdrv_nbd); 1564ea2384d3Sbellard } 1565ce1a14dcSpbrook 1566ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, 1567ce1a14dcSpbrook void *opaque) 1568ce1a14dcSpbrook { 1569ce1a14dcSpbrook BlockDriver *drv; 1570ce1a14dcSpbrook BlockDriverAIOCB *acb; 1571ce1a14dcSpbrook 1572ce1a14dcSpbrook drv = bs->drv; 1573ce1a14dcSpbrook if (drv->free_aiocb) { 1574ce1a14dcSpbrook acb = drv->free_aiocb; 1575ce1a14dcSpbrook drv->free_aiocb = acb->next; 1576ce1a14dcSpbrook } else { 1577ce1a14dcSpbrook acb = qemu_mallocz(drv->aiocb_size); 1578ce1a14dcSpbrook } 1579ce1a14dcSpbrook acb->bs = bs; 1580ce1a14dcSpbrook acb->cb = cb; 1581ce1a14dcSpbrook acb->opaque = opaque; 1582ce1a14dcSpbrook return acb; 1583ce1a14dcSpbrook } 1584ce1a14dcSpbrook 1585ce1a14dcSpbrook void qemu_aio_release(void *p) 1586ce1a14dcSpbrook { 1587ce1a14dcSpbrook BlockDriverAIOCB *acb = p; 1588ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 1589ce1a14dcSpbrook acb->next = drv->free_aiocb; 1590ce1a14dcSpbrook drv->free_aiocb = acb; 1591ce1a14dcSpbrook } 159219cb3738Sbellard 159319cb3738Sbellard /**************************************************************/ 159419cb3738Sbellard /* removable device support */ 159519cb3738Sbellard 159619cb3738Sbellard /** 159719cb3738Sbellard * Return TRUE if the media is present 159819cb3738Sbellard */ 159919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 160019cb3738Sbellard { 160119cb3738Sbellard BlockDriver *drv = bs->drv; 160219cb3738Sbellard int ret; 160319cb3738Sbellard if (!drv) 160419cb3738Sbellard return 0; 160519cb3738Sbellard if (!drv->bdrv_is_inserted) 160619cb3738Sbellard return 1; 160719cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 160819cb3738Sbellard return ret; 160919cb3738Sbellard } 161019cb3738Sbellard 161119cb3738Sbellard /** 161219cb3738Sbellard * Return TRUE if the media changed since the last call to this 161319cb3738Sbellard * function. It is currently only used for floppy disks 161419cb3738Sbellard */ 161519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 161619cb3738Sbellard { 161719cb3738Sbellard BlockDriver *drv = bs->drv; 161819cb3738Sbellard int ret; 161919cb3738Sbellard 162019cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 162119cb3738Sbellard ret = -ENOTSUP; 162219cb3738Sbellard else 162319cb3738Sbellard ret = drv->bdrv_media_changed(bs); 162419cb3738Sbellard if (ret == -ENOTSUP) 162519cb3738Sbellard ret = bs->media_changed; 162619cb3738Sbellard bs->media_changed = 0; 162719cb3738Sbellard return ret; 162819cb3738Sbellard } 162919cb3738Sbellard 163019cb3738Sbellard /** 163119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 163219cb3738Sbellard */ 163319cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag) 163419cb3738Sbellard { 163519cb3738Sbellard BlockDriver *drv = bs->drv; 163619cb3738Sbellard int ret; 163719cb3738Sbellard 163819cb3738Sbellard if (!drv || !drv->bdrv_eject) { 163919cb3738Sbellard ret = -ENOTSUP; 164019cb3738Sbellard } else { 164119cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 164219cb3738Sbellard } 164319cb3738Sbellard if (ret == -ENOTSUP) { 164419cb3738Sbellard if (eject_flag) 164519cb3738Sbellard bdrv_close(bs); 164619cb3738Sbellard } 164719cb3738Sbellard } 164819cb3738Sbellard 164919cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 165019cb3738Sbellard { 165119cb3738Sbellard return bs->locked; 165219cb3738Sbellard } 165319cb3738Sbellard 165419cb3738Sbellard /** 165519cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 165619cb3738Sbellard * to eject it manually). 165719cb3738Sbellard */ 165819cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 165919cb3738Sbellard { 166019cb3738Sbellard BlockDriver *drv = bs->drv; 166119cb3738Sbellard 166219cb3738Sbellard bs->locked = locked; 166319cb3738Sbellard if (drv && drv->bdrv_set_locked) { 166419cb3738Sbellard drv->bdrv_set_locked(bs, locked); 166519cb3738Sbellard } 166619cb3738Sbellard } 1667985a03b0Sths 1668985a03b0Sths /* needed for generic scsi interface */ 1669985a03b0Sths 1670985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 1671985a03b0Sths { 1672985a03b0Sths BlockDriver *drv = bs->drv; 1673985a03b0Sths 1674985a03b0Sths if (drv && drv->bdrv_ioctl) 1675985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 1676985a03b0Sths return -ENOTSUP; 1677985a03b0Sths } 1678