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 50c07a9008Saliguori static AIOPool vectored_aio_pool; 51c07a9008Saliguori 5290765429Sbellard typedef struct BlockDriverAIOCBSync { 5390765429Sbellard BlockDriverAIOCB common; 5490765429Sbellard QEMUBH *bh; 5590765429Sbellard int ret; 5690765429Sbellard } BlockDriverAIOCBSync; 5790765429Sbellard 58*c87c0672Saliguori static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, 59*c87c0672Saliguori int64_t sector_num, uint8_t *buf, int nb_sectors, 60*c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 61*c87c0672Saliguori static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, 62*c87c0672Saliguori int64_t sector_num, const uint8_t *buf, int nb_sectors, 63*c87c0672Saliguori BlockDriverCompletionFunc *cb, void *opaque); 64ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 65ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 66ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 67ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 68ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 69ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 7083f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); 7183f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 7283f64091Sbellard uint8_t *buf, int nb_sectors); 7383f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 7483f64091Sbellard const uint8_t *buf, int nb_sectors); 75ec530c81Sbellard 767ee930d0Sblueswir1 BlockDriverState *bdrv_first; 777ee930d0Sblueswir1 78ea2384d3Sbellard static BlockDriver *first_drv; 79ea2384d3Sbellard 8083f64091Sbellard int path_is_absolute(const char *path) 8183f64091Sbellard { 8283f64091Sbellard const char *p; 8321664424Sbellard #ifdef _WIN32 8421664424Sbellard /* specific case for names like: "\\.\d:" */ 8521664424Sbellard if (*path == '/' || *path == '\\') 8621664424Sbellard return 1; 8721664424Sbellard #endif 8883f64091Sbellard p = strchr(path, ':'); 8983f64091Sbellard if (p) 9083f64091Sbellard p++; 9183f64091Sbellard else 9283f64091Sbellard p = path; 933b9f94e1Sbellard #ifdef _WIN32 943b9f94e1Sbellard return (*p == '/' || *p == '\\'); 953b9f94e1Sbellard #else 963b9f94e1Sbellard return (*p == '/'); 973b9f94e1Sbellard #endif 9883f64091Sbellard } 9983f64091Sbellard 10083f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 10183f64091Sbellard path to it by considering it is relative to base_path. URL are 10283f64091Sbellard supported. */ 10383f64091Sbellard void path_combine(char *dest, int dest_size, 10483f64091Sbellard const char *base_path, 10583f64091Sbellard const char *filename) 10683f64091Sbellard { 10783f64091Sbellard const char *p, *p1; 10883f64091Sbellard int len; 10983f64091Sbellard 11083f64091Sbellard if (dest_size <= 0) 11183f64091Sbellard return; 11283f64091Sbellard if (path_is_absolute(filename)) { 11383f64091Sbellard pstrcpy(dest, dest_size, filename); 11483f64091Sbellard } else { 11583f64091Sbellard p = strchr(base_path, ':'); 11683f64091Sbellard if (p) 11783f64091Sbellard p++; 11883f64091Sbellard else 11983f64091Sbellard p = base_path; 1203b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1213b9f94e1Sbellard #ifdef _WIN32 1223b9f94e1Sbellard { 1233b9f94e1Sbellard const char *p2; 1243b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1253b9f94e1Sbellard if (!p1 || p2 > p1) 1263b9f94e1Sbellard p1 = p2; 1273b9f94e1Sbellard } 1283b9f94e1Sbellard #endif 12983f64091Sbellard if (p1) 13083f64091Sbellard p1++; 13183f64091Sbellard else 13283f64091Sbellard p1 = base_path; 13383f64091Sbellard if (p1 > p) 13483f64091Sbellard p = p1; 13583f64091Sbellard len = p - base_path; 13683f64091Sbellard if (len > dest_size - 1) 13783f64091Sbellard len = dest_size - 1; 13883f64091Sbellard memcpy(dest, base_path, len); 13983f64091Sbellard dest[len] = '\0'; 14083f64091Sbellard pstrcat(dest, dest_size, filename); 14183f64091Sbellard } 14283f64091Sbellard } 14383f64091Sbellard 14483f64091Sbellard 1459596ebb7Spbrook static void bdrv_register(BlockDriver *bdrv) 146ea2384d3Sbellard { 147ce1a14dcSpbrook if (!bdrv->bdrv_aio_read) { 14883f64091Sbellard /* add AIO emulation layer */ 14983f64091Sbellard bdrv->bdrv_aio_read = bdrv_aio_read_em; 15083f64091Sbellard bdrv->bdrv_aio_write = bdrv_aio_write_em; 15183f64091Sbellard bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; 15290765429Sbellard bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync); 153eda578e5Saliguori } else if (!bdrv->bdrv_read) { 15483f64091Sbellard /* add synchronous IO emulation layer */ 15583f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 15683f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 15783f64091Sbellard } 1586bbff9a0Saliguori aio_pool_init(&bdrv->aio_pool, bdrv->aiocb_size, bdrv->bdrv_aio_cancel); 159ea2384d3Sbellard bdrv->next = first_drv; 160ea2384d3Sbellard first_drv = bdrv; 161ea2384d3Sbellard } 162b338082bSbellard 163b338082bSbellard /* create a new block device (by default it is empty) */ 164b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 165fc01f7e7Sbellard { 166b338082bSbellard BlockDriverState **pbs, *bs; 167b338082bSbellard 168b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 169b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 170ea2384d3Sbellard if (device_name[0] != '\0') { 171b338082bSbellard /* insert at the end */ 172b338082bSbellard pbs = &bdrv_first; 173b338082bSbellard while (*pbs != NULL) 174b338082bSbellard pbs = &(*pbs)->next; 175b338082bSbellard *pbs = bs; 176ea2384d3Sbellard } 177b338082bSbellard return bs; 178b338082bSbellard } 179b338082bSbellard 180ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 181ea2384d3Sbellard { 182ea2384d3Sbellard BlockDriver *drv1; 183ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 184ea2384d3Sbellard if (!strcmp(drv1->format_name, format_name)) 185ea2384d3Sbellard return drv1; 186ea2384d3Sbellard } 187ea2384d3Sbellard return NULL; 188ea2384d3Sbellard } 189ea2384d3Sbellard 1905eb45639Saliguori int bdrv_create2(BlockDriver *drv, 1915eb45639Saliguori const char *filename, int64_t size_in_sectors, 1925eb45639Saliguori const char *backing_file, const char *backing_format, 1935eb45639Saliguori int flags) 1945eb45639Saliguori { 1955eb45639Saliguori if (drv->bdrv_create2) 1965eb45639Saliguori return drv->bdrv_create2(filename, size_in_sectors, backing_file, 1975eb45639Saliguori backing_format, flags); 1985eb45639Saliguori if (drv->bdrv_create) 1995eb45639Saliguori return drv->bdrv_create(filename, size_in_sectors, backing_file, 2005eb45639Saliguori flags); 2015eb45639Saliguori return -ENOTSUP; 2025eb45639Saliguori } 2035eb45639Saliguori 204ea2384d3Sbellard int bdrv_create(BlockDriver *drv, 205ea2384d3Sbellard const char *filename, int64_t size_in_sectors, 206ea2384d3Sbellard const char *backing_file, int flags) 207ea2384d3Sbellard { 208ea2384d3Sbellard if (!drv->bdrv_create) 209ea2384d3Sbellard return -ENOTSUP; 210ea2384d3Sbellard return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); 211ea2384d3Sbellard } 212ea2384d3Sbellard 213d5249393Sbellard #ifdef _WIN32 21495389c86Sbellard void get_tmp_filename(char *filename, int size) 215d5249393Sbellard { 2163b9f94e1Sbellard char temp_dir[MAX_PATH]; 2173b9f94e1Sbellard 2183b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 2193b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 220d5249393Sbellard } 221d5249393Sbellard #else 22295389c86Sbellard void get_tmp_filename(char *filename, int size) 223ea2384d3Sbellard { 224ea2384d3Sbellard int fd; 2257ccfb2ebSblueswir1 const char *tmpdir; 226d5249393Sbellard /* XXX: race condition possible */ 2270badc1eeSaurel32 tmpdir = getenv("TMPDIR"); 2280badc1eeSaurel32 if (!tmpdir) 2290badc1eeSaurel32 tmpdir = "/tmp"; 2300badc1eeSaurel32 snprintf(filename, size, "%s/vl.XXXXXX", tmpdir); 231ea2384d3Sbellard fd = mkstemp(filename); 232ea2384d3Sbellard close(fd); 233ea2384d3Sbellard } 234d5249393Sbellard #endif 235ea2384d3Sbellard 23619cb3738Sbellard #ifdef _WIN32 237f45512feSbellard static int is_windows_drive_prefix(const char *filename) 238f45512feSbellard { 239f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 240f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 241f45512feSbellard filename[1] == ':'); 242f45512feSbellard } 243f45512feSbellard 24419cb3738Sbellard static int is_windows_drive(const char *filename) 24519cb3738Sbellard { 246f45512feSbellard if (is_windows_drive_prefix(filename) && 247f45512feSbellard filename[2] == '\0') 24819cb3738Sbellard return 1; 24919cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 25019cb3738Sbellard strstart(filename, "//./", NULL)) 25119cb3738Sbellard return 1; 25219cb3738Sbellard return 0; 25319cb3738Sbellard } 25419cb3738Sbellard #endif 25519cb3738Sbellard 25683f64091Sbellard static BlockDriver *find_protocol(const char *filename) 25783f64091Sbellard { 25883f64091Sbellard BlockDriver *drv1; 25983f64091Sbellard char protocol[128]; 26083f64091Sbellard int len; 26183f64091Sbellard const char *p; 26219cb3738Sbellard 26319cb3738Sbellard #ifdef _WIN32 264f45512feSbellard if (is_windows_drive(filename) || 265f45512feSbellard is_windows_drive_prefix(filename)) 26619cb3738Sbellard return &bdrv_raw; 26719cb3738Sbellard #endif 26883f64091Sbellard p = strchr(filename, ':'); 26983f64091Sbellard if (!p) 27083f64091Sbellard return &bdrv_raw; 27183f64091Sbellard len = p - filename; 27283f64091Sbellard if (len > sizeof(protocol) - 1) 27383f64091Sbellard len = sizeof(protocol) - 1; 27483f64091Sbellard memcpy(protocol, filename, len); 27583f64091Sbellard protocol[len] = '\0'; 27683f64091Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 27783f64091Sbellard if (drv1->protocol_name && 27883f64091Sbellard !strcmp(drv1->protocol_name, protocol)) 27983f64091Sbellard return drv1; 28083f64091Sbellard } 28183f64091Sbellard return NULL; 28283f64091Sbellard } 28383f64091Sbellard 2847674e7bfSbellard /* XXX: force raw format if block or character device ? It would 2857674e7bfSbellard simplify the BSD case */ 286ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 287ea2384d3Sbellard { 28883f64091Sbellard int ret, score, score_max; 289ea2384d3Sbellard BlockDriver *drv1, *drv; 29083f64091Sbellard uint8_t buf[2048]; 29183f64091Sbellard BlockDriverState *bs; 292ea2384d3Sbellard 29319cb3738Sbellard /* detect host devices. By convention, /dev/cdrom[N] is always 29419cb3738Sbellard recognized as a host CDROM */ 29519cb3738Sbellard if (strstart(filename, "/dev/cdrom", NULL)) 29619cb3738Sbellard return &bdrv_host_device; 29719cb3738Sbellard #ifdef _WIN32 29819cb3738Sbellard if (is_windows_drive(filename)) 29919cb3738Sbellard return &bdrv_host_device; 30019cb3738Sbellard #else 30119cb3738Sbellard { 30219cb3738Sbellard struct stat st; 30319cb3738Sbellard if (stat(filename, &st) >= 0 && 30419cb3738Sbellard (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 30519cb3738Sbellard return &bdrv_host_device; 30619cb3738Sbellard } 30719cb3738Sbellard } 30819cb3738Sbellard #endif 30919cb3738Sbellard 31083f64091Sbellard drv = find_protocol(filename); 31119cb3738Sbellard /* no need to test disk image formats for vvfat */ 31283f64091Sbellard if (drv == &bdrv_vvfat) 31383f64091Sbellard return drv; 31483f64091Sbellard 31583f64091Sbellard ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); 31683f64091Sbellard if (ret < 0) 3177674e7bfSbellard return NULL; 31883f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 31983f64091Sbellard bdrv_delete(bs); 320ea2384d3Sbellard if (ret < 0) { 321ea2384d3Sbellard return NULL; 322ea2384d3Sbellard } 323ea2384d3Sbellard 324ea2384d3Sbellard score_max = 0; 325ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 32683f64091Sbellard if (drv1->bdrv_probe) { 327ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 328ea2384d3Sbellard if (score > score_max) { 329ea2384d3Sbellard score_max = score; 330ea2384d3Sbellard drv = drv1; 331ea2384d3Sbellard } 332ea2384d3Sbellard } 33383f64091Sbellard } 334ea2384d3Sbellard return drv; 335ea2384d3Sbellard } 336ea2384d3Sbellard 33783f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 338b338082bSbellard { 33983f64091Sbellard BlockDriverState *bs; 34083f64091Sbellard int ret; 3413b0d4f61Sbellard 34283f64091Sbellard bs = bdrv_new(""); 34383f64091Sbellard ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); 34483f64091Sbellard if (ret < 0) { 34583f64091Sbellard bdrv_delete(bs); 34683f64091Sbellard return ret; 3473b0d4f61Sbellard } 34871d0770cSaliguori bs->growable = 1; 34983f64091Sbellard *pbs = bs; 35083f64091Sbellard return 0; 3513b0d4f61Sbellard } 3523b0d4f61Sbellard 35383f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags) 35483f64091Sbellard { 35583f64091Sbellard return bdrv_open2(bs, filename, flags, NULL); 356ea2384d3Sbellard } 357ea2384d3Sbellard 35883f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 359ea2384d3Sbellard BlockDriver *drv) 360ea2384d3Sbellard { 36183f64091Sbellard int ret, open_flags; 362eb5c851fSths char tmp_filename[PATH_MAX]; 363eb5c851fSths char backing_filename[PATH_MAX]; 364fc01f7e7Sbellard 3650849bf08Sbellard bs->read_only = 0; 366ea2384d3Sbellard bs->is_temporary = 0; 367ea2384d3Sbellard bs->encrypted = 0; 368c0f4ce77Saliguori bs->valid_key = 0; 36933e3963eSbellard 37083f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 371ea2384d3Sbellard BlockDriverState *bs1; 372ea2384d3Sbellard int64_t total_size; 3737c96d46eSaliguori int is_protocol = 0; 37433e3963eSbellard 375ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 376ea2384d3Sbellard instead of opening 'filename' directly */ 377ea2384d3Sbellard 378ea2384d3Sbellard /* if there is a backing file, use it */ 379ea2384d3Sbellard bs1 = bdrv_new(""); 3805eb45639Saliguori ret = bdrv_open2(bs1, filename, 0, drv); 38151d7c00cSaliguori if (ret < 0) { 382ea2384d3Sbellard bdrv_delete(bs1); 38351d7c00cSaliguori return ret; 384ea2384d3Sbellard } 38583f64091Sbellard total_size = bdrv_getlength(bs1) >> SECTOR_BITS; 3867c96d46eSaliguori 3877c96d46eSaliguori if (bs1->drv && bs1->drv->protocol_name) 3887c96d46eSaliguori is_protocol = 1; 3897c96d46eSaliguori 390ea2384d3Sbellard bdrv_delete(bs1); 391ea2384d3Sbellard 392ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 3937c96d46eSaliguori 3947c96d46eSaliguori /* Real path is meaningless for protocols */ 3957c96d46eSaliguori if (is_protocol) 3967c96d46eSaliguori snprintf(backing_filename, sizeof(backing_filename), 3977c96d46eSaliguori "%s", filename); 3987c96d46eSaliguori else 399a817d936Sbellard realpath(filename, backing_filename); 4007c96d46eSaliguori 4015eb45639Saliguori ret = bdrv_create2(&bdrv_qcow2, tmp_filename, 4025eb45639Saliguori total_size, backing_filename, 4035eb45639Saliguori (drv ? drv->format_name : NULL), 0); 40451d7c00cSaliguori if (ret < 0) { 40551d7c00cSaliguori return ret; 406ea2384d3Sbellard } 407ea2384d3Sbellard filename = tmp_filename; 4085eb45639Saliguori drv = &bdrv_qcow2; 409ea2384d3Sbellard bs->is_temporary = 1; 410ea2384d3Sbellard } 411ea2384d3Sbellard 412ea2384d3Sbellard pstrcpy(bs->filename, sizeof(bs->filename), filename); 41383f64091Sbellard if (flags & BDRV_O_FILE) { 41483f64091Sbellard drv = find_protocol(filename); 41551d7c00cSaliguori } else if (!drv) { 416ea2384d3Sbellard drv = find_image_format(filename); 417ea2384d3Sbellard } 41851d7c00cSaliguori if (!drv) { 41951d7c00cSaliguori ret = -ENOENT; 42051d7c00cSaliguori goto unlink_and_fail; 42183f64091Sbellard } 422ea2384d3Sbellard bs->drv = drv; 423ea2384d3Sbellard bs->opaque = qemu_mallocz(drv->instance_size); 42483f64091Sbellard /* Note: for compatibility, we open disk image files as RDWR, and 42583f64091Sbellard RDONLY as fallback */ 42683f64091Sbellard if (!(flags & BDRV_O_FILE)) 4279f7965c7Saliguori open_flags = BDRV_O_RDWR | (flags & BDRV_O_CACHE_MASK); 42883f64091Sbellard else 42983f64091Sbellard open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); 43083f64091Sbellard ret = drv->bdrv_open(bs, filename, open_flags); 431a0a83536Saurel32 if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) { 4329f7965c7Saliguori ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR); 43383f64091Sbellard bs->read_only = 1; 43483f64091Sbellard } 435ea2384d3Sbellard if (ret < 0) { 436ea2384d3Sbellard qemu_free(bs->opaque); 4376b21b973Sbellard bs->opaque = NULL; 4386b21b973Sbellard bs->drv = NULL; 43951d7c00cSaliguori unlink_and_fail: 44051d7c00cSaliguori if (bs->is_temporary) 44151d7c00cSaliguori unlink(filename); 44283f64091Sbellard return ret; 443ea2384d3Sbellard } 444d15a771dSbellard if (drv->bdrv_getlength) { 445d15a771dSbellard bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 446d15a771dSbellard } 447ea2384d3Sbellard #ifndef _WIN32 448ea2384d3Sbellard if (bs->is_temporary) { 449ea2384d3Sbellard unlink(filename); 45033e3963eSbellard } 45167b915a5Sbellard #endif 45283f64091Sbellard if (bs->backing_file[0] != '\0') { 453ea2384d3Sbellard /* if there is a backing file, use it */ 4545eb45639Saliguori BlockDriver *back_drv = NULL; 455ea2384d3Sbellard bs->backing_hd = bdrv_new(""); 45683f64091Sbellard path_combine(backing_filename, sizeof(backing_filename), 45783f64091Sbellard filename, bs->backing_file); 4585eb45639Saliguori if (bs->backing_format[0] != '\0') 4595eb45639Saliguori back_drv = bdrv_find_format(bs->backing_format); 4605eb45639Saliguori ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags, 4615eb45639Saliguori back_drv); 46251d7c00cSaliguori if (ret < 0) { 46351d7c00cSaliguori bdrv_close(bs); 46451d7c00cSaliguori return ret; 46551d7c00cSaliguori } 466ea2384d3Sbellard } 46733e3963eSbellard 468bb5fc20fSaliguori if (!bdrv_key_required(bs)) { 469b338082bSbellard /* call the change callback */ 47019cb3738Sbellard bs->media_changed = 1; 471b338082bSbellard if (bs->change_cb) 472b338082bSbellard bs->change_cb(bs->change_opaque); 473bb5fc20fSaliguori } 474b338082bSbellard return 0; 475fc01f7e7Sbellard } 476fc01f7e7Sbellard 477fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 478fc01f7e7Sbellard { 47919cb3738Sbellard if (bs->drv) { 480ea2384d3Sbellard if (bs->backing_hd) 481ea2384d3Sbellard bdrv_delete(bs->backing_hd); 482ea2384d3Sbellard bs->drv->bdrv_close(bs); 483ea2384d3Sbellard qemu_free(bs->opaque); 484ea2384d3Sbellard #ifdef _WIN32 485ea2384d3Sbellard if (bs->is_temporary) { 486ea2384d3Sbellard unlink(bs->filename); 487ea2384d3Sbellard } 48867b915a5Sbellard #endif 489ea2384d3Sbellard bs->opaque = NULL; 490ea2384d3Sbellard bs->drv = NULL; 491b338082bSbellard 492b338082bSbellard /* call the change callback */ 49319cb3738Sbellard bs->media_changed = 1; 494b338082bSbellard if (bs->change_cb) 495b338082bSbellard bs->change_cb(bs->change_opaque); 496b338082bSbellard } 497b338082bSbellard } 498b338082bSbellard 499b338082bSbellard void bdrv_delete(BlockDriverState *bs) 500b338082bSbellard { 50134c6f050Saurel32 BlockDriverState **pbs; 50234c6f050Saurel32 50334c6f050Saurel32 pbs = &bdrv_first; 50434c6f050Saurel32 while (*pbs != bs && *pbs != NULL) 50534c6f050Saurel32 pbs = &(*pbs)->next; 50634c6f050Saurel32 if (*pbs == bs) 50734c6f050Saurel32 *pbs = bs->next; 50834c6f050Saurel32 509b338082bSbellard bdrv_close(bs); 510b338082bSbellard qemu_free(bs); 511fc01f7e7Sbellard } 512fc01f7e7Sbellard 51333e3963eSbellard /* commit COW file into the raw image */ 51433e3963eSbellard int bdrv_commit(BlockDriverState *bs) 51533e3963eSbellard { 51619cb3738Sbellard BlockDriver *drv = bs->drv; 51783f64091Sbellard int64_t i, total_sectors; 518ea2384d3Sbellard int n, j; 519ea2384d3Sbellard unsigned char sector[512]; 52033e3963eSbellard 52119cb3738Sbellard if (!drv) 52219cb3738Sbellard return -ENOMEDIUM; 52333e3963eSbellard 52433e3963eSbellard if (bs->read_only) { 525ea2384d3Sbellard return -EACCES; 52633e3963eSbellard } 52733e3963eSbellard 528ea2384d3Sbellard if (!bs->backing_hd) { 529ea2384d3Sbellard return -ENOTSUP; 530ea2384d3Sbellard } 531ea2384d3Sbellard 53283f64091Sbellard total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 53383f64091Sbellard for (i = 0; i < total_sectors;) { 53419cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 535ea2384d3Sbellard for(j = 0; j < n; j++) { 53633e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 537ea2384d3Sbellard return -EIO; 53833e3963eSbellard } 53933e3963eSbellard 540ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 541ea2384d3Sbellard return -EIO; 54233e3963eSbellard } 543ea2384d3Sbellard i++; 544ea2384d3Sbellard } 545ea2384d3Sbellard } else { 546ea2384d3Sbellard i += n; 54733e3963eSbellard } 54833e3963eSbellard } 54995389c86Sbellard 55019cb3738Sbellard if (drv->bdrv_make_empty) 55119cb3738Sbellard return drv->bdrv_make_empty(bs); 55295389c86Sbellard 55333e3963eSbellard return 0; 55433e3963eSbellard } 55533e3963eSbellard 55671d0770cSaliguori static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset, 55771d0770cSaliguori size_t size) 55871d0770cSaliguori { 55971d0770cSaliguori int64_t len; 56071d0770cSaliguori 56171d0770cSaliguori if (!bdrv_is_inserted(bs)) 56271d0770cSaliguori return -ENOMEDIUM; 56371d0770cSaliguori 56471d0770cSaliguori if (bs->growable) 56571d0770cSaliguori return 0; 56671d0770cSaliguori 56771d0770cSaliguori len = bdrv_getlength(bs); 56871d0770cSaliguori 56971d0770cSaliguori if ((offset + size) > len) 57071d0770cSaliguori return -EIO; 57171d0770cSaliguori 57271d0770cSaliguori return 0; 57371d0770cSaliguori } 57471d0770cSaliguori 57571d0770cSaliguori static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num, 57671d0770cSaliguori int nb_sectors) 57771d0770cSaliguori { 578999dec57Saliguori return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512); 57971d0770cSaliguori } 58071d0770cSaliguori 58119cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 582fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 583fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 584fc01f7e7Sbellard { 585ea2384d3Sbellard BlockDriver *drv = bs->drv; 586fc01f7e7Sbellard 58719cb3738Sbellard if (!drv) 58819cb3738Sbellard return -ENOMEDIUM; 58971d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 59071d0770cSaliguori return -EIO; 591b338082bSbellard 59283f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 59383f64091Sbellard } 594fc01f7e7Sbellard 59519cb3738Sbellard /* Return < 0 if error. Important errors are: 59619cb3738Sbellard -EIO generic I/O error (may happen for all errors) 59719cb3738Sbellard -ENOMEDIUM No media inserted. 59819cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 59919cb3738Sbellard -EACCES Trying to write a read-only device 60019cb3738Sbellard */ 601fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 602fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 603fc01f7e7Sbellard { 60483f64091Sbellard BlockDriver *drv = bs->drv; 60519cb3738Sbellard if (!bs->drv) 60619cb3738Sbellard return -ENOMEDIUM; 6070849bf08Sbellard if (bs->read_only) 60819cb3738Sbellard return -EACCES; 60971d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 61071d0770cSaliguori return -EIO; 61171d0770cSaliguori 61283f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 61383f64091Sbellard } 61483f64091Sbellard 615eda578e5Saliguori int bdrv_pread(BlockDriverState *bs, int64_t offset, 616eda578e5Saliguori void *buf, int count1) 61783f64091Sbellard { 61883f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 61983f64091Sbellard int len, nb_sectors, count; 62083f64091Sbellard int64_t sector_num; 62183f64091Sbellard 62283f64091Sbellard count = count1; 62383f64091Sbellard /* first read to align to sector start */ 62483f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 62583f64091Sbellard if (len > count) 62683f64091Sbellard len = count; 62783f64091Sbellard sector_num = offset >> SECTOR_BITS; 62883f64091Sbellard if (len > 0) { 62983f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 63083f64091Sbellard return -EIO; 63183f64091Sbellard memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); 63283f64091Sbellard count -= len; 63383f64091Sbellard if (count == 0) 63483f64091Sbellard return count1; 63583f64091Sbellard sector_num++; 63683f64091Sbellard buf += len; 63783f64091Sbellard } 63883f64091Sbellard 63983f64091Sbellard /* read the sectors "in place" */ 64083f64091Sbellard nb_sectors = count >> SECTOR_BITS; 64183f64091Sbellard if (nb_sectors > 0) { 64283f64091Sbellard if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) 64383f64091Sbellard return -EIO; 64483f64091Sbellard sector_num += nb_sectors; 64583f64091Sbellard len = nb_sectors << SECTOR_BITS; 64683f64091Sbellard buf += len; 64783f64091Sbellard count -= len; 64883f64091Sbellard } 64983f64091Sbellard 65083f64091Sbellard /* add data from the last sector */ 65183f64091Sbellard if (count > 0) { 65283f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 65383f64091Sbellard return -EIO; 65483f64091Sbellard memcpy(buf, tmp_buf, count); 65583f64091Sbellard } 65683f64091Sbellard return count1; 65783f64091Sbellard } 65883f64091Sbellard 659eda578e5Saliguori int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 660eda578e5Saliguori const void *buf, int count1) 66183f64091Sbellard { 66283f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 66383f64091Sbellard int len, nb_sectors, count; 66483f64091Sbellard int64_t sector_num; 66583f64091Sbellard 66683f64091Sbellard count = count1; 66783f64091Sbellard /* first write to align to sector start */ 66883f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 66983f64091Sbellard if (len > count) 67083f64091Sbellard len = count; 67183f64091Sbellard sector_num = offset >> SECTOR_BITS; 67283f64091Sbellard if (len > 0) { 67383f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 67483f64091Sbellard return -EIO; 67583f64091Sbellard memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); 67683f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 67783f64091Sbellard return -EIO; 67883f64091Sbellard count -= len; 67983f64091Sbellard if (count == 0) 68083f64091Sbellard return count1; 68183f64091Sbellard sector_num++; 68283f64091Sbellard buf += len; 68383f64091Sbellard } 68483f64091Sbellard 68583f64091Sbellard /* write the sectors "in place" */ 68683f64091Sbellard nb_sectors = count >> SECTOR_BITS; 68783f64091Sbellard if (nb_sectors > 0) { 68883f64091Sbellard if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0) 68983f64091Sbellard return -EIO; 69083f64091Sbellard sector_num += nb_sectors; 69183f64091Sbellard len = nb_sectors << SECTOR_BITS; 69283f64091Sbellard buf += len; 69383f64091Sbellard count -= len; 69483f64091Sbellard } 69583f64091Sbellard 69683f64091Sbellard /* add data from the last sector */ 69783f64091Sbellard if (count > 0) { 69883f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 69983f64091Sbellard return -EIO; 70083f64091Sbellard memcpy(tmp_buf, buf, count); 70183f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 70283f64091Sbellard return -EIO; 70383f64091Sbellard } 70483f64091Sbellard return count1; 70583f64091Sbellard } 70683f64091Sbellard 70783f64091Sbellard /** 70883f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 70983f64091Sbellard */ 71083f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 71183f64091Sbellard { 71283f64091Sbellard BlockDriver *drv = bs->drv; 71383f64091Sbellard if (!drv) 71419cb3738Sbellard return -ENOMEDIUM; 71583f64091Sbellard if (!drv->bdrv_truncate) 71683f64091Sbellard return -ENOTSUP; 71783f64091Sbellard return drv->bdrv_truncate(bs, offset); 71883f64091Sbellard } 71983f64091Sbellard 72083f64091Sbellard /** 72183f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 72283f64091Sbellard */ 72383f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 72483f64091Sbellard { 72583f64091Sbellard BlockDriver *drv = bs->drv; 72683f64091Sbellard if (!drv) 72719cb3738Sbellard return -ENOMEDIUM; 72883f64091Sbellard if (!drv->bdrv_getlength) { 72983f64091Sbellard /* legacy mode */ 73083f64091Sbellard return bs->total_sectors * SECTOR_SIZE; 73183f64091Sbellard } 73283f64091Sbellard return drv->bdrv_getlength(bs); 733fc01f7e7Sbellard } 734fc01f7e7Sbellard 73519cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 73696b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr) 737fc01f7e7Sbellard { 73819cb3738Sbellard int64_t length; 73919cb3738Sbellard length = bdrv_getlength(bs); 74019cb3738Sbellard if (length < 0) 74119cb3738Sbellard length = 0; 74219cb3738Sbellard else 74319cb3738Sbellard length = length >> SECTOR_BITS; 74419cb3738Sbellard *nb_sectors_ptr = length; 745fc01f7e7Sbellard } 746cf98951bSbellard 747f3d54fc4Saliguori struct partition { 748f3d54fc4Saliguori uint8_t boot_ind; /* 0x80 - active */ 749f3d54fc4Saliguori uint8_t head; /* starting head */ 750f3d54fc4Saliguori uint8_t sector; /* starting sector */ 751f3d54fc4Saliguori uint8_t cyl; /* starting cylinder */ 752f3d54fc4Saliguori uint8_t sys_ind; /* What partition type */ 753f3d54fc4Saliguori uint8_t end_head; /* end head */ 754f3d54fc4Saliguori uint8_t end_sector; /* end sector */ 755f3d54fc4Saliguori uint8_t end_cyl; /* end cylinder */ 756f3d54fc4Saliguori uint32_t start_sect; /* starting sector counting from 0 */ 757f3d54fc4Saliguori uint32_t nr_sects; /* nr of sectors in partition */ 758f3d54fc4Saliguori } __attribute__((packed)); 759f3d54fc4Saliguori 760f3d54fc4Saliguori /* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */ 761f3d54fc4Saliguori static int guess_disk_lchs(BlockDriverState *bs, 762f3d54fc4Saliguori int *pcylinders, int *pheads, int *psectors) 763f3d54fc4Saliguori { 764f3d54fc4Saliguori uint8_t buf[512]; 765f3d54fc4Saliguori int ret, i, heads, sectors, cylinders; 766f3d54fc4Saliguori struct partition *p; 767f3d54fc4Saliguori uint32_t nr_sects; 768a38131b6Sblueswir1 uint64_t nb_sectors; 769f3d54fc4Saliguori 770f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 771f3d54fc4Saliguori 772f3d54fc4Saliguori ret = bdrv_read(bs, 0, buf, 1); 773f3d54fc4Saliguori if (ret < 0) 774f3d54fc4Saliguori return -1; 775f3d54fc4Saliguori /* test msdos magic */ 776f3d54fc4Saliguori if (buf[510] != 0x55 || buf[511] != 0xaa) 777f3d54fc4Saliguori return -1; 778f3d54fc4Saliguori for(i = 0; i < 4; i++) { 779f3d54fc4Saliguori p = ((struct partition *)(buf + 0x1be)) + i; 780f3d54fc4Saliguori nr_sects = le32_to_cpu(p->nr_sects); 781f3d54fc4Saliguori if (nr_sects && p->end_head) { 782f3d54fc4Saliguori /* We make the assumption that the partition terminates on 783f3d54fc4Saliguori a cylinder boundary */ 784f3d54fc4Saliguori heads = p->end_head + 1; 785f3d54fc4Saliguori sectors = p->end_sector & 63; 786f3d54fc4Saliguori if (sectors == 0) 787f3d54fc4Saliguori continue; 788f3d54fc4Saliguori cylinders = nb_sectors / (heads * sectors); 789f3d54fc4Saliguori if (cylinders < 1 || cylinders > 16383) 790f3d54fc4Saliguori continue; 791f3d54fc4Saliguori *pheads = heads; 792f3d54fc4Saliguori *psectors = sectors; 793f3d54fc4Saliguori *pcylinders = cylinders; 794f3d54fc4Saliguori #if 0 795f3d54fc4Saliguori printf("guessed geometry: LCHS=%d %d %d\n", 796f3d54fc4Saliguori cylinders, heads, sectors); 797f3d54fc4Saliguori #endif 798f3d54fc4Saliguori return 0; 799f3d54fc4Saliguori } 800f3d54fc4Saliguori } 801f3d54fc4Saliguori return -1; 802f3d54fc4Saliguori } 803f3d54fc4Saliguori 804f3d54fc4Saliguori void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs) 805f3d54fc4Saliguori { 806f3d54fc4Saliguori int translation, lba_detected = 0; 807f3d54fc4Saliguori int cylinders, heads, secs; 808a38131b6Sblueswir1 uint64_t nb_sectors; 809f3d54fc4Saliguori 810f3d54fc4Saliguori /* if a geometry hint is available, use it */ 811f3d54fc4Saliguori bdrv_get_geometry(bs, &nb_sectors); 812f3d54fc4Saliguori bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs); 813f3d54fc4Saliguori translation = bdrv_get_translation_hint(bs); 814f3d54fc4Saliguori if (cylinders != 0) { 815f3d54fc4Saliguori *pcyls = cylinders; 816f3d54fc4Saliguori *pheads = heads; 817f3d54fc4Saliguori *psecs = secs; 818f3d54fc4Saliguori } else { 819f3d54fc4Saliguori if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) { 820f3d54fc4Saliguori if (heads > 16) { 821f3d54fc4Saliguori /* if heads > 16, it means that a BIOS LBA 822f3d54fc4Saliguori translation was active, so the default 823f3d54fc4Saliguori hardware geometry is OK */ 824f3d54fc4Saliguori lba_detected = 1; 825f3d54fc4Saliguori goto default_geometry; 826f3d54fc4Saliguori } else { 827f3d54fc4Saliguori *pcyls = cylinders; 828f3d54fc4Saliguori *pheads = heads; 829f3d54fc4Saliguori *psecs = secs; 830f3d54fc4Saliguori /* disable any translation to be in sync with 831f3d54fc4Saliguori the logical geometry */ 832f3d54fc4Saliguori if (translation == BIOS_ATA_TRANSLATION_AUTO) { 833f3d54fc4Saliguori bdrv_set_translation_hint(bs, 834f3d54fc4Saliguori BIOS_ATA_TRANSLATION_NONE); 835f3d54fc4Saliguori } 836f3d54fc4Saliguori } 837f3d54fc4Saliguori } else { 838f3d54fc4Saliguori default_geometry: 839f3d54fc4Saliguori /* if no geometry, use a standard physical disk geometry */ 840f3d54fc4Saliguori cylinders = nb_sectors / (16 * 63); 841f3d54fc4Saliguori 842f3d54fc4Saliguori if (cylinders > 16383) 843f3d54fc4Saliguori cylinders = 16383; 844f3d54fc4Saliguori else if (cylinders < 2) 845f3d54fc4Saliguori cylinders = 2; 846f3d54fc4Saliguori *pcyls = cylinders; 847f3d54fc4Saliguori *pheads = 16; 848f3d54fc4Saliguori *psecs = 63; 849f3d54fc4Saliguori if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) { 850f3d54fc4Saliguori if ((*pcyls * *pheads) <= 131072) { 851f3d54fc4Saliguori bdrv_set_translation_hint(bs, 852f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LARGE); 853f3d54fc4Saliguori } else { 854f3d54fc4Saliguori bdrv_set_translation_hint(bs, 855f3d54fc4Saliguori BIOS_ATA_TRANSLATION_LBA); 856f3d54fc4Saliguori } 857f3d54fc4Saliguori } 858f3d54fc4Saliguori } 859f3d54fc4Saliguori bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs); 860f3d54fc4Saliguori } 861f3d54fc4Saliguori } 862f3d54fc4Saliguori 863b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 864b338082bSbellard int cyls, int heads, int secs) 865b338082bSbellard { 866b338082bSbellard bs->cyls = cyls; 867b338082bSbellard bs->heads = heads; 868b338082bSbellard bs->secs = secs; 869b338082bSbellard } 870b338082bSbellard 871b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 872b338082bSbellard { 873b338082bSbellard bs->type = type; 874b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 875b338082bSbellard type == BDRV_TYPE_FLOPPY)); 876b338082bSbellard } 877b338082bSbellard 87846d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 87946d4767dSbellard { 88046d4767dSbellard bs->translation = translation; 88146d4767dSbellard } 88246d4767dSbellard 883b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 884b338082bSbellard int *pcyls, int *pheads, int *psecs) 885b338082bSbellard { 886b338082bSbellard *pcyls = bs->cyls; 887b338082bSbellard *pheads = bs->heads; 888b338082bSbellard *psecs = bs->secs; 889b338082bSbellard } 890b338082bSbellard 891b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 892b338082bSbellard { 893b338082bSbellard return bs->type; 894b338082bSbellard } 895b338082bSbellard 89646d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 89746d4767dSbellard { 89846d4767dSbellard return bs->translation; 89946d4767dSbellard } 90046d4767dSbellard 901b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 902b338082bSbellard { 903b338082bSbellard return bs->removable; 904b338082bSbellard } 905b338082bSbellard 906b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 907b338082bSbellard { 908b338082bSbellard return bs->read_only; 909b338082bSbellard } 910b338082bSbellard 911985a03b0Sths int bdrv_is_sg(BlockDriverState *bs) 912985a03b0Sths { 913985a03b0Sths return bs->sg; 914985a03b0Sths } 915985a03b0Sths 91619cb3738Sbellard /* XXX: no longer used */ 917b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 918b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 919b338082bSbellard { 920b338082bSbellard bs->change_cb = change_cb; 921b338082bSbellard bs->change_opaque = opaque; 922b338082bSbellard } 923b338082bSbellard 924ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 925ea2384d3Sbellard { 926ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 927ea2384d3Sbellard return 1; 928ea2384d3Sbellard return bs->encrypted; 929ea2384d3Sbellard } 930ea2384d3Sbellard 931c0f4ce77Saliguori int bdrv_key_required(BlockDriverState *bs) 932c0f4ce77Saliguori { 933c0f4ce77Saliguori BlockDriverState *backing_hd = bs->backing_hd; 934c0f4ce77Saliguori 935c0f4ce77Saliguori if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key) 936c0f4ce77Saliguori return 1; 937c0f4ce77Saliguori return (bs->encrypted && !bs->valid_key); 938c0f4ce77Saliguori } 939c0f4ce77Saliguori 940ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 941ea2384d3Sbellard { 942ea2384d3Sbellard int ret; 943ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 944ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 945ea2384d3Sbellard if (ret < 0) 946ea2384d3Sbellard return ret; 947ea2384d3Sbellard if (!bs->encrypted) 948ea2384d3Sbellard return 0; 949ea2384d3Sbellard } 950ea2384d3Sbellard if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) 951ea2384d3Sbellard return -1; 952c0f4ce77Saliguori ret = bs->drv->bdrv_set_key(bs, key); 953bb5fc20fSaliguori if (ret < 0) { 954bb5fc20fSaliguori bs->valid_key = 0; 955bb5fc20fSaliguori } else if (!bs->valid_key) { 956bb5fc20fSaliguori bs->valid_key = 1; 957bb5fc20fSaliguori /* call the change callback now, we skipped it on open */ 958bb5fc20fSaliguori bs->media_changed = 1; 959bb5fc20fSaliguori if (bs->change_cb) 960bb5fc20fSaliguori bs->change_cb(bs->change_opaque); 961bb5fc20fSaliguori } 962c0f4ce77Saliguori return ret; 963ea2384d3Sbellard } 964ea2384d3Sbellard 965ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 966ea2384d3Sbellard { 96719cb3738Sbellard if (!bs->drv) { 968ea2384d3Sbellard buf[0] = '\0'; 969ea2384d3Sbellard } else { 970ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 971ea2384d3Sbellard } 972ea2384d3Sbellard } 973ea2384d3Sbellard 974ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 975ea2384d3Sbellard void *opaque) 976ea2384d3Sbellard { 977ea2384d3Sbellard BlockDriver *drv; 978ea2384d3Sbellard 979ea2384d3Sbellard for (drv = first_drv; drv != NULL; drv = drv->next) { 980ea2384d3Sbellard it(opaque, drv->format_name); 981ea2384d3Sbellard } 982ea2384d3Sbellard } 983ea2384d3Sbellard 984b338082bSbellard BlockDriverState *bdrv_find(const char *name) 985b338082bSbellard { 986b338082bSbellard BlockDriverState *bs; 987b338082bSbellard 988b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 989b338082bSbellard if (!strcmp(name, bs->device_name)) 990b338082bSbellard return bs; 991b338082bSbellard } 992b338082bSbellard return NULL; 993b338082bSbellard } 994b338082bSbellard 99551de9760Saliguori void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque) 99681d0912dSbellard { 99781d0912dSbellard BlockDriverState *bs; 99881d0912dSbellard 99981d0912dSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 100051de9760Saliguori it(opaque, bs); 100181d0912dSbellard } 100281d0912dSbellard } 100381d0912dSbellard 1004ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 1005ea2384d3Sbellard { 1006ea2384d3Sbellard return bs->device_name; 1007ea2384d3Sbellard } 1008ea2384d3Sbellard 10097a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 10107a6cba61Spbrook { 1011081501daSaliguori if (!bs->drv) 1012081501daSaliguori return; 10137a6cba61Spbrook if (bs->drv->bdrv_flush) 10147a6cba61Spbrook bs->drv->bdrv_flush(bs); 10157a6cba61Spbrook if (bs->backing_hd) 10167a6cba61Spbrook bdrv_flush(bs->backing_hd); 10177a6cba61Spbrook } 10187a6cba61Spbrook 1019c6ca28d6Saliguori void bdrv_flush_all(void) 1020c6ca28d6Saliguori { 1021c6ca28d6Saliguori BlockDriverState *bs; 1022c6ca28d6Saliguori 1023c6ca28d6Saliguori for (bs = bdrv_first; bs != NULL; bs = bs->next) 1024c6ca28d6Saliguori if (bs->drv && !bdrv_is_read_only(bs) && 1025c6ca28d6Saliguori (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) 1026c6ca28d6Saliguori bdrv_flush(bs); 1027c6ca28d6Saliguori } 1028c6ca28d6Saliguori 1029f58c7b35Sths /* 1030f58c7b35Sths * Returns true iff the specified sector is present in the disk image. Drivers 1031f58c7b35Sths * not implementing the functionality are assumed to not support backing files, 1032f58c7b35Sths * hence all their sectors are reported as allocated. 1033f58c7b35Sths * 1034f58c7b35Sths * 'pnum' is set to the number of sectors (including and immediately following 1035f58c7b35Sths * the specified sector) that are known to be in the same 1036f58c7b35Sths * allocated/unallocated state. 1037f58c7b35Sths * 1038f58c7b35Sths * 'nb_sectors' is the max value 'pnum' should be set to. 1039f58c7b35Sths */ 1040f58c7b35Sths int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors, 1041f58c7b35Sths int *pnum) 1042f58c7b35Sths { 1043f58c7b35Sths int64_t n; 1044f58c7b35Sths if (!bs->drv->bdrv_is_allocated) { 1045f58c7b35Sths if (sector_num >= bs->total_sectors) { 1046f58c7b35Sths *pnum = 0; 1047f58c7b35Sths return 0; 1048f58c7b35Sths } 1049f58c7b35Sths n = bs->total_sectors - sector_num; 1050f58c7b35Sths *pnum = (n < nb_sectors) ? (n) : (nb_sectors); 1051f58c7b35Sths return 1; 1052f58c7b35Sths } 1053f58c7b35Sths return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum); 1054f58c7b35Sths } 1055f58c7b35Sths 1056376253ecSaliguori void bdrv_info(Monitor *mon) 1057b338082bSbellard { 1058b338082bSbellard BlockDriverState *bs; 1059b338082bSbellard 1060b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1061376253ecSaliguori monitor_printf(mon, "%s:", bs->device_name); 1062376253ecSaliguori monitor_printf(mon, " type="); 1063b338082bSbellard switch(bs->type) { 1064b338082bSbellard case BDRV_TYPE_HD: 1065376253ecSaliguori monitor_printf(mon, "hd"); 1066b338082bSbellard break; 1067b338082bSbellard case BDRV_TYPE_CDROM: 1068376253ecSaliguori monitor_printf(mon, "cdrom"); 1069b338082bSbellard break; 1070b338082bSbellard case BDRV_TYPE_FLOPPY: 1071376253ecSaliguori monitor_printf(mon, "floppy"); 1072b338082bSbellard break; 1073b338082bSbellard } 1074376253ecSaliguori monitor_printf(mon, " removable=%d", bs->removable); 1075b338082bSbellard if (bs->removable) { 1076376253ecSaliguori monitor_printf(mon, " locked=%d", bs->locked); 1077b338082bSbellard } 107819cb3738Sbellard if (bs->drv) { 1079376253ecSaliguori monitor_printf(mon, " file="); 1080376253ecSaliguori monitor_print_filename(mon, bs->filename); 1081fef30743Sths if (bs->backing_file[0] != '\0') { 1082376253ecSaliguori monitor_printf(mon, " backing_file="); 1083376253ecSaliguori monitor_print_filename(mon, bs->backing_file); 1084fef30743Sths } 1085376253ecSaliguori monitor_printf(mon, " ro=%d", bs->read_only); 1086376253ecSaliguori monitor_printf(mon, " drv=%s", bs->drv->format_name); 1087376253ecSaliguori monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs)); 1088b338082bSbellard } else { 1089376253ecSaliguori monitor_printf(mon, " [not inserted]"); 1090b338082bSbellard } 1091376253ecSaliguori monitor_printf(mon, "\n"); 1092b338082bSbellard } 1093b338082bSbellard } 1094a36e69ddSths 1095a36e69ddSths /* The "info blockstats" command. */ 1096376253ecSaliguori void bdrv_info_stats(Monitor *mon) 1097a36e69ddSths { 1098a36e69ddSths BlockDriverState *bs; 1099a36e69ddSths 1100a36e69ddSths for (bs = bdrv_first; bs != NULL; bs = bs->next) { 1101376253ecSaliguori monitor_printf(mon, "%s:" 1102a36e69ddSths " rd_bytes=%" PRIu64 1103a36e69ddSths " wr_bytes=%" PRIu64 1104a36e69ddSths " rd_operations=%" PRIu64 1105a36e69ddSths " wr_operations=%" PRIu64 1106ebf53fcdSaliguori "\n", 1107a36e69ddSths bs->device_name, 1108a36e69ddSths bs->rd_bytes, bs->wr_bytes, 1109a36e69ddSths bs->rd_ops, bs->wr_ops); 1110a36e69ddSths } 1111a36e69ddSths } 1112ea2384d3Sbellard 1113045df330Saliguori const char *bdrv_get_encrypted_filename(BlockDriverState *bs) 1114045df330Saliguori { 1115045df330Saliguori if (bs->backing_hd && bs->backing_hd->encrypted) 1116045df330Saliguori return bs->backing_file; 1117045df330Saliguori else if (bs->encrypted) 1118045df330Saliguori return bs->filename; 1119045df330Saliguori else 1120045df330Saliguori return NULL; 1121045df330Saliguori } 1122045df330Saliguori 112383f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 112483f64091Sbellard char *filename, int filename_size) 112583f64091Sbellard { 112683f64091Sbellard if (!bs->backing_hd) { 112783f64091Sbellard pstrcpy(filename, filename_size, ""); 112883f64091Sbellard } else { 112983f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 113083f64091Sbellard } 113183f64091Sbellard } 113283f64091Sbellard 1133faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 1134faea38e7Sbellard const uint8_t *buf, int nb_sectors) 1135faea38e7Sbellard { 1136faea38e7Sbellard BlockDriver *drv = bs->drv; 1137faea38e7Sbellard if (!drv) 113819cb3738Sbellard return -ENOMEDIUM; 1139faea38e7Sbellard if (!drv->bdrv_write_compressed) 1140faea38e7Sbellard return -ENOTSUP; 1141faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 1142faea38e7Sbellard } 1143faea38e7Sbellard 1144faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 1145faea38e7Sbellard { 1146faea38e7Sbellard BlockDriver *drv = bs->drv; 1147faea38e7Sbellard if (!drv) 114819cb3738Sbellard return -ENOMEDIUM; 1149faea38e7Sbellard if (!drv->bdrv_get_info) 1150faea38e7Sbellard return -ENOTSUP; 1151faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 1152faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 1153faea38e7Sbellard } 1154faea38e7Sbellard 1155178e08a5Saliguori int bdrv_put_buffer(BlockDriverState *bs, const uint8_t *buf, int64_t pos, int size) 1156178e08a5Saliguori { 1157178e08a5Saliguori BlockDriver *drv = bs->drv; 1158178e08a5Saliguori if (!drv) 1159178e08a5Saliguori return -ENOMEDIUM; 1160178e08a5Saliguori if (!drv->bdrv_put_buffer) 1161178e08a5Saliguori return -ENOTSUP; 1162178e08a5Saliguori return drv->bdrv_put_buffer(bs, buf, pos, size); 1163178e08a5Saliguori } 1164178e08a5Saliguori 1165178e08a5Saliguori int bdrv_get_buffer(BlockDriverState *bs, uint8_t *buf, int64_t pos, int size) 1166178e08a5Saliguori { 1167178e08a5Saliguori BlockDriver *drv = bs->drv; 1168178e08a5Saliguori if (!drv) 1169178e08a5Saliguori return -ENOMEDIUM; 1170178e08a5Saliguori if (!drv->bdrv_get_buffer) 1171178e08a5Saliguori return -ENOTSUP; 1172178e08a5Saliguori return drv->bdrv_get_buffer(bs, buf, pos, size); 1173178e08a5Saliguori } 1174178e08a5Saliguori 1175faea38e7Sbellard /**************************************************************/ 1176faea38e7Sbellard /* handling of snapshots */ 1177faea38e7Sbellard 1178faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 1179faea38e7Sbellard QEMUSnapshotInfo *sn_info) 1180faea38e7Sbellard { 1181faea38e7Sbellard BlockDriver *drv = bs->drv; 1182faea38e7Sbellard if (!drv) 118319cb3738Sbellard return -ENOMEDIUM; 1184faea38e7Sbellard if (!drv->bdrv_snapshot_create) 1185faea38e7Sbellard return -ENOTSUP; 1186faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 1187faea38e7Sbellard } 1188faea38e7Sbellard 1189faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 1190faea38e7Sbellard const char *snapshot_id) 1191faea38e7Sbellard { 1192faea38e7Sbellard BlockDriver *drv = bs->drv; 1193faea38e7Sbellard if (!drv) 119419cb3738Sbellard return -ENOMEDIUM; 1195faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 1196faea38e7Sbellard return -ENOTSUP; 1197faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 1198faea38e7Sbellard } 1199faea38e7Sbellard 1200faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 1201faea38e7Sbellard { 1202faea38e7Sbellard BlockDriver *drv = bs->drv; 1203faea38e7Sbellard if (!drv) 120419cb3738Sbellard return -ENOMEDIUM; 1205faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 1206faea38e7Sbellard return -ENOTSUP; 1207faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 1208faea38e7Sbellard } 1209faea38e7Sbellard 1210faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 1211faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1212faea38e7Sbellard { 1213faea38e7Sbellard BlockDriver *drv = bs->drv; 1214faea38e7Sbellard if (!drv) 121519cb3738Sbellard return -ENOMEDIUM; 1216faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1217faea38e7Sbellard return -ENOTSUP; 1218faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1219faea38e7Sbellard } 1220faea38e7Sbellard 1221faea38e7Sbellard #define NB_SUFFIXES 4 1222faea38e7Sbellard 1223faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1224faea38e7Sbellard { 1225faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1226faea38e7Sbellard int64_t base; 1227faea38e7Sbellard int i; 1228faea38e7Sbellard 1229faea38e7Sbellard if (size <= 999) { 1230faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1231faea38e7Sbellard } else { 1232faea38e7Sbellard base = 1024; 1233faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1234faea38e7Sbellard if (size < (10 * base)) { 1235faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1236faea38e7Sbellard (double)size / base, 1237faea38e7Sbellard suffixes[i]); 1238faea38e7Sbellard break; 1239faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1240faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1241faea38e7Sbellard ((size + (base >> 1)) / base), 1242faea38e7Sbellard suffixes[i]); 1243faea38e7Sbellard break; 1244faea38e7Sbellard } 1245faea38e7Sbellard base = base * 1024; 1246faea38e7Sbellard } 1247faea38e7Sbellard } 1248faea38e7Sbellard return buf; 1249faea38e7Sbellard } 1250faea38e7Sbellard 1251faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1252faea38e7Sbellard { 1253faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 12543b9f94e1Sbellard #ifdef _WIN32 12553b9f94e1Sbellard struct tm *ptm; 12563b9f94e1Sbellard #else 1257faea38e7Sbellard struct tm tm; 12583b9f94e1Sbellard #endif 1259faea38e7Sbellard time_t ti; 1260faea38e7Sbellard int64_t secs; 1261faea38e7Sbellard 1262faea38e7Sbellard if (!sn) { 1263faea38e7Sbellard snprintf(buf, buf_size, 1264faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1265faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1266faea38e7Sbellard } else { 1267faea38e7Sbellard ti = sn->date_sec; 12683b9f94e1Sbellard #ifdef _WIN32 12693b9f94e1Sbellard ptm = localtime(&ti); 12703b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 12713b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 12723b9f94e1Sbellard #else 1273faea38e7Sbellard localtime_r(&ti, &tm); 1274faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1275faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 12763b9f94e1Sbellard #endif 1277faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1278faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1279faea38e7Sbellard "%02d:%02d:%02d.%03d", 1280faea38e7Sbellard (int)(secs / 3600), 1281faea38e7Sbellard (int)((secs / 60) % 60), 1282faea38e7Sbellard (int)(secs % 60), 1283faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1284faea38e7Sbellard snprintf(buf, buf_size, 1285faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1286faea38e7Sbellard sn->id_str, sn->name, 1287faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1288faea38e7Sbellard date_buf, 1289faea38e7Sbellard clock_buf); 1290faea38e7Sbellard } 1291faea38e7Sbellard return buf; 1292faea38e7Sbellard } 1293faea38e7Sbellard 129483f64091Sbellard 1295ea2384d3Sbellard /**************************************************************/ 129683f64091Sbellard /* async I/Os */ 1297ea2384d3Sbellard 12983fb94d56Saliguori typedef struct VectorTranslationAIOCB { 12993fb94d56Saliguori BlockDriverAIOCB common; 13003b69e4b9Saliguori QEMUIOVector *iov; 13013b69e4b9Saliguori uint8_t *bounce; 13023b69e4b9Saliguori int is_write; 13033b69e4b9Saliguori BlockDriverAIOCB *aiocb; 13043fb94d56Saliguori } VectorTranslationAIOCB; 13053b69e4b9Saliguori 13063fb94d56Saliguori static void bdrv_aio_cancel_vector(BlockDriverAIOCB *_acb) 1307c07a9008Saliguori { 13083fb94d56Saliguori VectorTranslationAIOCB *acb 13093fb94d56Saliguori = container_of(_acb, VectorTranslationAIOCB, common); 1310c07a9008Saliguori 13113fb94d56Saliguori bdrv_aio_cancel(acb->aiocb); 1312c07a9008Saliguori } 1313c07a9008Saliguori 13143b69e4b9Saliguori static void bdrv_aio_rw_vector_cb(void *opaque, int ret) 13153b69e4b9Saliguori { 13163fb94d56Saliguori VectorTranslationAIOCB *s = (VectorTranslationAIOCB *)opaque; 13173b69e4b9Saliguori 13183b69e4b9Saliguori if (!s->is_write) { 1319249aa745Saliguori qemu_iovec_from_buffer(s->iov, s->bounce, s->iov->size); 13203b69e4b9Saliguori } 1321d905dba4Saurel32 qemu_vfree(s->bounce); 13223fb94d56Saliguori s->common.cb(s->common.opaque, ret); 13233fb94d56Saliguori qemu_aio_release(s); 13243b69e4b9Saliguori } 13253b69e4b9Saliguori 13263b69e4b9Saliguori static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs, 13273b69e4b9Saliguori int64_t sector_num, 13283b69e4b9Saliguori QEMUIOVector *iov, 13293b69e4b9Saliguori int nb_sectors, 13303b69e4b9Saliguori BlockDriverCompletionFunc *cb, 13313b69e4b9Saliguori void *opaque, 13323b69e4b9Saliguori int is_write) 13333b69e4b9Saliguori 13343b69e4b9Saliguori { 13353fb94d56Saliguori VectorTranslationAIOCB *s = qemu_aio_get_pool(&vectored_aio_pool, bs, 1336c07a9008Saliguori cb, opaque); 13373b69e4b9Saliguori 13383b69e4b9Saliguori s->iov = iov; 13393b69e4b9Saliguori s->bounce = qemu_memalign(512, nb_sectors * 512); 13403b69e4b9Saliguori s->is_write = is_write; 13413b69e4b9Saliguori if (is_write) { 13423b69e4b9Saliguori qemu_iovec_to_buffer(s->iov, s->bounce); 13433b69e4b9Saliguori s->aiocb = bdrv_aio_write(bs, sector_num, s->bounce, nb_sectors, 13443b69e4b9Saliguori bdrv_aio_rw_vector_cb, s); 13453b69e4b9Saliguori } else { 13463b69e4b9Saliguori s->aiocb = bdrv_aio_read(bs, sector_num, s->bounce, nb_sectors, 13473b69e4b9Saliguori bdrv_aio_rw_vector_cb, s); 13483b69e4b9Saliguori } 1349c240b9afSaliguori if (!s->aiocb) { 1350c240b9afSaliguori qemu_vfree(s->bounce); 1351c240b9afSaliguori qemu_aio_release(s); 1352c240b9afSaliguori return NULL; 1353c240b9afSaliguori } 13543fb94d56Saliguori return &s->common; 13553b69e4b9Saliguori } 13563b69e4b9Saliguori 13573b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num, 13583b69e4b9Saliguori QEMUIOVector *iov, int nb_sectors, 13593b69e4b9Saliguori BlockDriverCompletionFunc *cb, void *opaque) 13603b69e4b9Saliguori { 136171d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 136271d0770cSaliguori return NULL; 136371d0770cSaliguori 13643b69e4b9Saliguori return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, 13653b69e4b9Saliguori cb, opaque, 0); 13663b69e4b9Saliguori } 13673b69e4b9Saliguori 13683b69e4b9Saliguori BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num, 13693b69e4b9Saliguori QEMUIOVector *iov, int nb_sectors, 13703b69e4b9Saliguori BlockDriverCompletionFunc *cb, void *opaque) 13713b69e4b9Saliguori { 137271d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 137371d0770cSaliguori return NULL; 137471d0770cSaliguori 13753b69e4b9Saliguori return bdrv_aio_rw_vector(bs, sector_num, iov, nb_sectors, 13763b69e4b9Saliguori cb, opaque, 1); 13773b69e4b9Saliguori } 13783b69e4b9Saliguori 1379*c87c0672Saliguori static BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, 138083f64091Sbellard uint8_t *buf, int nb_sectors, 138183f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1382ea2384d3Sbellard { 138383f64091Sbellard BlockDriver *drv = bs->drv; 1384a36e69ddSths BlockDriverAIOCB *ret; 1385ea2384d3Sbellard 138619cb3738Sbellard if (!drv) 1387ce1a14dcSpbrook return NULL; 138871d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 138971d0770cSaliguori return NULL; 139083f64091Sbellard 1391a36e69ddSths ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); 1392a36e69ddSths 1393a36e69ddSths if (ret) { 1394a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1395a36e69ddSths bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1396a36e69ddSths bs->rd_ops ++; 1397a36e69ddSths } 1398a36e69ddSths 1399a36e69ddSths return ret; 140083f64091Sbellard } 140183f64091Sbellard 1402*c87c0672Saliguori static BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, 140383f64091Sbellard const uint8_t *buf, int nb_sectors, 140483f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 14057674e7bfSbellard { 140683f64091Sbellard BlockDriver *drv = bs->drv; 1407a36e69ddSths BlockDriverAIOCB *ret; 140883f64091Sbellard 140919cb3738Sbellard if (!drv) 1410ce1a14dcSpbrook return NULL; 141183f64091Sbellard if (bs->read_only) 1412ce1a14dcSpbrook return NULL; 141371d0770cSaliguori if (bdrv_check_request(bs, sector_num, nb_sectors)) 141471d0770cSaliguori return NULL; 141583f64091Sbellard 1416a36e69ddSths ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); 1417a36e69ddSths 1418a36e69ddSths if (ret) { 1419a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1420a36e69ddSths bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1421a36e69ddSths bs->wr_ops ++; 1422a36e69ddSths } 1423a36e69ddSths 1424a36e69ddSths return ret; 142583f64091Sbellard } 142683f64091Sbellard 142783f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 142883f64091Sbellard { 14296bbff9a0Saliguori acb->pool->cancel(acb); 143083f64091Sbellard } 143183f64091Sbellard 143283f64091Sbellard 143383f64091Sbellard /**************************************************************/ 143483f64091Sbellard /* async block device emulation */ 143583f64091Sbellard 143683f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 1437beac80cdSbellard { 1438ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 1439ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 1440ce1a14dcSpbrook qemu_aio_release(acb); 1441beac80cdSbellard } 1442beac80cdSbellard 1443ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 1444ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 1445ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 1446ea2384d3Sbellard { 1447ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 144883f64091Sbellard int ret; 144983f64091Sbellard 1450ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1451ce1a14dcSpbrook if (!acb->bh) 1452ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1453ce1a14dcSpbrook ret = bdrv_read(bs, sector_num, buf, nb_sectors); 1454ce1a14dcSpbrook acb->ret = ret; 1455ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1456ce1a14dcSpbrook return &acb->common; 14577a6cba61Spbrook } 14587a6cba61Spbrook 1459ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 1460ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 1461ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 146283f64091Sbellard { 1463ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 146483f64091Sbellard int ret; 146583f64091Sbellard 1466ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1467ce1a14dcSpbrook if (!acb->bh) 1468ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1469ce1a14dcSpbrook ret = bdrv_write(bs, sector_num, buf, nb_sectors); 1470ce1a14dcSpbrook acb->ret = ret; 1471ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1472ce1a14dcSpbrook return &acb->common; 147383f64091Sbellard } 147483f64091Sbellard 1475ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 147683f64091Sbellard { 1477ce1a14dcSpbrook BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; 1478ce1a14dcSpbrook qemu_bh_cancel(acb->bh); 1479ce1a14dcSpbrook qemu_aio_release(acb); 148083f64091Sbellard } 148183f64091Sbellard 148283f64091Sbellard /**************************************************************/ 148383f64091Sbellard /* sync block device emulation */ 148483f64091Sbellard 148583f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 148683f64091Sbellard { 148783f64091Sbellard *(int *)opaque = ret; 148883f64091Sbellard } 148983f64091Sbellard 149083f64091Sbellard #define NOT_DONE 0x7fffffff 149183f64091Sbellard 149283f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 149383f64091Sbellard uint8_t *buf, int nb_sectors) 149483f64091Sbellard { 1495ce1a14dcSpbrook int async_ret; 1496ce1a14dcSpbrook BlockDriverAIOCB *acb; 149783f64091Sbellard 149883f64091Sbellard async_ret = NOT_DONE; 1499ce1a14dcSpbrook acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, 150083f64091Sbellard bdrv_rw_em_cb, &async_ret); 1501baf35cb9Saliguori if (acb == NULL) 1502ce1a14dcSpbrook return -1; 1503baf35cb9Saliguori 150483f64091Sbellard while (async_ret == NOT_DONE) { 150583f64091Sbellard qemu_aio_wait(); 150683f64091Sbellard } 1507baf35cb9Saliguori 150883f64091Sbellard return async_ret; 150983f64091Sbellard } 151083f64091Sbellard 151183f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 151283f64091Sbellard const uint8_t *buf, int nb_sectors) 151383f64091Sbellard { 1514ce1a14dcSpbrook int async_ret; 1515ce1a14dcSpbrook BlockDriverAIOCB *acb; 151683f64091Sbellard 151783f64091Sbellard async_ret = NOT_DONE; 1518ce1a14dcSpbrook acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, 151983f64091Sbellard bdrv_rw_em_cb, &async_ret); 1520baf35cb9Saliguori if (acb == NULL) 1521ce1a14dcSpbrook return -1; 152283f64091Sbellard while (async_ret == NOT_DONE) { 152383f64091Sbellard qemu_aio_wait(); 152483f64091Sbellard } 152583f64091Sbellard return async_ret; 152683f64091Sbellard } 1527ea2384d3Sbellard 1528ea2384d3Sbellard void bdrv_init(void) 1529ea2384d3Sbellard { 15303fb94d56Saliguori aio_pool_init(&vectored_aio_pool, sizeof(VectorTranslationAIOCB), 1531c07a9008Saliguori bdrv_aio_cancel_vector); 1532c07a9008Saliguori 1533ea2384d3Sbellard bdrv_register(&bdrv_raw); 153419cb3738Sbellard bdrv_register(&bdrv_host_device); 1535ea2384d3Sbellard #ifndef _WIN32 1536ea2384d3Sbellard bdrv_register(&bdrv_cow); 1537ea2384d3Sbellard #endif 1538ea2384d3Sbellard bdrv_register(&bdrv_qcow); 1539ea2384d3Sbellard bdrv_register(&bdrv_vmdk); 15403c56521bSbellard bdrv_register(&bdrv_cloop); 1541585d0ed9Sbellard bdrv_register(&bdrv_dmg); 1542a8753c34Sbellard bdrv_register(&bdrv_bochs); 15436a0f9e82Sbellard bdrv_register(&bdrv_vpc); 1544712e7874Sbellard bdrv_register(&bdrv_vvfat); 1545faea38e7Sbellard bdrv_register(&bdrv_qcow2); 15466ada7453Sths bdrv_register(&bdrv_parallels); 154775818250Sths bdrv_register(&bdrv_nbd); 1548ea2384d3Sbellard } 1549ce1a14dcSpbrook 15506bbff9a0Saliguori void aio_pool_init(AIOPool *pool, int aiocb_size, 15516bbff9a0Saliguori void (*cancel)(BlockDriverAIOCB *acb)) 1552ce1a14dcSpbrook { 15536bbff9a0Saliguori pool->aiocb_size = aiocb_size; 15546bbff9a0Saliguori pool->cancel = cancel; 15556bbff9a0Saliguori pool->free_aiocb = NULL; 15566bbff9a0Saliguori } 15576bbff9a0Saliguori 15586bbff9a0Saliguori void *qemu_aio_get_pool(AIOPool *pool, BlockDriverState *bs, 15596bbff9a0Saliguori BlockDriverCompletionFunc *cb, void *opaque) 15606bbff9a0Saliguori { 1561ce1a14dcSpbrook BlockDriverAIOCB *acb; 1562ce1a14dcSpbrook 15636bbff9a0Saliguori if (pool->free_aiocb) { 15646bbff9a0Saliguori acb = pool->free_aiocb; 15656bbff9a0Saliguori pool->free_aiocb = acb->next; 1566ce1a14dcSpbrook } else { 15676bbff9a0Saliguori acb = qemu_mallocz(pool->aiocb_size); 15686bbff9a0Saliguori acb->pool = pool; 1569ce1a14dcSpbrook } 1570ce1a14dcSpbrook acb->bs = bs; 1571ce1a14dcSpbrook acb->cb = cb; 1572ce1a14dcSpbrook acb->opaque = opaque; 1573ce1a14dcSpbrook return acb; 1574ce1a14dcSpbrook } 1575ce1a14dcSpbrook 15766bbff9a0Saliguori void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, 15776bbff9a0Saliguori void *opaque) 15786bbff9a0Saliguori { 15796bbff9a0Saliguori return qemu_aio_get_pool(&bs->drv->aio_pool, bs, cb, opaque); 15806bbff9a0Saliguori } 15816bbff9a0Saliguori 1582ce1a14dcSpbrook void qemu_aio_release(void *p) 1583ce1a14dcSpbrook { 15846bbff9a0Saliguori BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p; 15856bbff9a0Saliguori AIOPool *pool = acb->pool; 15866bbff9a0Saliguori acb->next = pool->free_aiocb; 15876bbff9a0Saliguori pool->free_aiocb = acb; 1588ce1a14dcSpbrook } 158919cb3738Sbellard 159019cb3738Sbellard /**************************************************************/ 159119cb3738Sbellard /* removable device support */ 159219cb3738Sbellard 159319cb3738Sbellard /** 159419cb3738Sbellard * Return TRUE if the media is present 159519cb3738Sbellard */ 159619cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 159719cb3738Sbellard { 159819cb3738Sbellard BlockDriver *drv = bs->drv; 159919cb3738Sbellard int ret; 160019cb3738Sbellard if (!drv) 160119cb3738Sbellard return 0; 160219cb3738Sbellard if (!drv->bdrv_is_inserted) 160319cb3738Sbellard return 1; 160419cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 160519cb3738Sbellard return ret; 160619cb3738Sbellard } 160719cb3738Sbellard 160819cb3738Sbellard /** 160919cb3738Sbellard * Return TRUE if the media changed since the last call to this 161019cb3738Sbellard * function. It is currently only used for floppy disks 161119cb3738Sbellard */ 161219cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 161319cb3738Sbellard { 161419cb3738Sbellard BlockDriver *drv = bs->drv; 161519cb3738Sbellard int ret; 161619cb3738Sbellard 161719cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 161819cb3738Sbellard ret = -ENOTSUP; 161919cb3738Sbellard else 162019cb3738Sbellard ret = drv->bdrv_media_changed(bs); 162119cb3738Sbellard if (ret == -ENOTSUP) 162219cb3738Sbellard ret = bs->media_changed; 162319cb3738Sbellard bs->media_changed = 0; 162419cb3738Sbellard return ret; 162519cb3738Sbellard } 162619cb3738Sbellard 162719cb3738Sbellard /** 162819cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 162919cb3738Sbellard */ 163019cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag) 163119cb3738Sbellard { 163219cb3738Sbellard BlockDriver *drv = bs->drv; 163319cb3738Sbellard int ret; 163419cb3738Sbellard 163519cb3738Sbellard if (!drv || !drv->bdrv_eject) { 163619cb3738Sbellard ret = -ENOTSUP; 163719cb3738Sbellard } else { 163819cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 163919cb3738Sbellard } 164019cb3738Sbellard if (ret == -ENOTSUP) { 164119cb3738Sbellard if (eject_flag) 164219cb3738Sbellard bdrv_close(bs); 164319cb3738Sbellard } 164419cb3738Sbellard } 164519cb3738Sbellard 164619cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 164719cb3738Sbellard { 164819cb3738Sbellard return bs->locked; 164919cb3738Sbellard } 165019cb3738Sbellard 165119cb3738Sbellard /** 165219cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 165319cb3738Sbellard * to eject it manually). 165419cb3738Sbellard */ 165519cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 165619cb3738Sbellard { 165719cb3738Sbellard BlockDriver *drv = bs->drv; 165819cb3738Sbellard 165919cb3738Sbellard bs->locked = locked; 166019cb3738Sbellard if (drv && drv->bdrv_set_locked) { 166119cb3738Sbellard drv->bdrv_set_locked(bs, locked); 166219cb3738Sbellard } 166319cb3738Sbellard } 1664985a03b0Sths 1665985a03b0Sths /* needed for generic scsi interface */ 1666985a03b0Sths 1667985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf) 1668985a03b0Sths { 1669985a03b0Sths BlockDriver *drv = bs->drv; 1670985a03b0Sths 1671985a03b0Sths if (drv && drv->bdrv_ioctl) 1672985a03b0Sths return drv->bdrv_ioctl(bs, req, buf); 1673985a03b0Sths return -ENOTSUP; 1674985a03b0Sths } 16757d780669Saliguori 1676221f715dSaliguori BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs, 1677221f715dSaliguori unsigned long int req, void *buf, 16787d780669Saliguori BlockDriverCompletionFunc *cb, void *opaque) 16797d780669Saliguori { 1680221f715dSaliguori BlockDriver *drv = bs->drv; 16817d780669Saliguori 1682221f715dSaliguori if (drv && drv->bdrv_aio_ioctl) 1683221f715dSaliguori return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque); 1684221f715dSaliguori return NULL; 16857d780669Saliguori } 1686