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 */ 24faf07963Spbrook #include "qemu-common.h" 2587ecb68bSpbrook #ifndef QEMU_IMG 2687ecb68bSpbrook #include "console.h" 27faf07963Spbrook #endif 28ea2384d3Sbellard #include "block_int.h" 29fc01f7e7Sbellard 307674e7bfSbellard #ifdef _BSD 317674e7bfSbellard #include <sys/types.h> 327674e7bfSbellard #include <sys/stat.h> 337674e7bfSbellard #include <sys/ioctl.h> 347674e7bfSbellard #include <sys/queue.h> 357674e7bfSbellard #include <sys/disk.h> 367674e7bfSbellard #endif 377674e7bfSbellard 3883f64091Sbellard #define SECTOR_BITS 9 3983f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS) 403b0d4f61Sbellard 4190765429Sbellard typedef struct BlockDriverAIOCBSync { 4290765429Sbellard BlockDriverAIOCB common; 4390765429Sbellard QEMUBH *bh; 4490765429Sbellard int ret; 4590765429Sbellard } BlockDriverAIOCBSync; 4690765429Sbellard 47ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 48ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 49ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 50ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 51ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 52ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque); 5383f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb); 5483f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 5583f64091Sbellard uint8_t *buf, int nb_sectors); 5683f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 5783f64091Sbellard const uint8_t *buf, int nb_sectors); 58ec530c81Sbellard 59faf07963Spbrook BlockDriverState *bdrv_first; 60ea2384d3Sbellard static BlockDriver *first_drv; 61ea2384d3Sbellard 6283f64091Sbellard int path_is_absolute(const char *path) 6383f64091Sbellard { 6483f64091Sbellard const char *p; 6521664424Sbellard #ifdef _WIN32 6621664424Sbellard /* specific case for names like: "\\.\d:" */ 6721664424Sbellard if (*path == '/' || *path == '\\') 6821664424Sbellard return 1; 6921664424Sbellard #endif 7083f64091Sbellard p = strchr(path, ':'); 7183f64091Sbellard if (p) 7283f64091Sbellard p++; 7383f64091Sbellard else 7483f64091Sbellard p = path; 753b9f94e1Sbellard #ifdef _WIN32 763b9f94e1Sbellard return (*p == '/' || *p == '\\'); 773b9f94e1Sbellard #else 783b9f94e1Sbellard return (*p == '/'); 793b9f94e1Sbellard #endif 8083f64091Sbellard } 8183f64091Sbellard 8283f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a 8383f64091Sbellard path to it by considering it is relative to base_path. URL are 8483f64091Sbellard supported. */ 8583f64091Sbellard void path_combine(char *dest, int dest_size, 8683f64091Sbellard const char *base_path, 8783f64091Sbellard const char *filename) 8883f64091Sbellard { 8983f64091Sbellard const char *p, *p1; 9083f64091Sbellard int len; 9183f64091Sbellard 9283f64091Sbellard if (dest_size <= 0) 9383f64091Sbellard return; 9483f64091Sbellard if (path_is_absolute(filename)) { 9583f64091Sbellard pstrcpy(dest, dest_size, filename); 9683f64091Sbellard } else { 9783f64091Sbellard p = strchr(base_path, ':'); 9883f64091Sbellard if (p) 9983f64091Sbellard p++; 10083f64091Sbellard else 10183f64091Sbellard p = base_path; 1023b9f94e1Sbellard p1 = strrchr(base_path, '/'); 1033b9f94e1Sbellard #ifdef _WIN32 1043b9f94e1Sbellard { 1053b9f94e1Sbellard const char *p2; 1063b9f94e1Sbellard p2 = strrchr(base_path, '\\'); 1073b9f94e1Sbellard if (!p1 || p2 > p1) 1083b9f94e1Sbellard p1 = p2; 1093b9f94e1Sbellard } 1103b9f94e1Sbellard #endif 11183f64091Sbellard if (p1) 11283f64091Sbellard p1++; 11383f64091Sbellard else 11483f64091Sbellard p1 = base_path; 11583f64091Sbellard if (p1 > p) 11683f64091Sbellard p = p1; 11783f64091Sbellard len = p - base_path; 11883f64091Sbellard if (len > dest_size - 1) 11983f64091Sbellard len = dest_size - 1; 12083f64091Sbellard memcpy(dest, base_path, len); 12183f64091Sbellard dest[len] = '\0'; 12283f64091Sbellard pstrcat(dest, dest_size, filename); 12383f64091Sbellard } 12483f64091Sbellard } 12583f64091Sbellard 12683f64091Sbellard 1279596ebb7Spbrook static void bdrv_register(BlockDriver *bdrv) 128ea2384d3Sbellard { 129ce1a14dcSpbrook if (!bdrv->bdrv_aio_read) { 13083f64091Sbellard /* add AIO emulation layer */ 13183f64091Sbellard bdrv->bdrv_aio_read = bdrv_aio_read_em; 13283f64091Sbellard bdrv->bdrv_aio_write = bdrv_aio_write_em; 13383f64091Sbellard bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em; 13490765429Sbellard bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync); 13583f64091Sbellard } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) { 13683f64091Sbellard /* add synchronous IO emulation layer */ 13783f64091Sbellard bdrv->bdrv_read = bdrv_read_em; 13883f64091Sbellard bdrv->bdrv_write = bdrv_write_em; 13983f64091Sbellard } 140ea2384d3Sbellard bdrv->next = first_drv; 141ea2384d3Sbellard first_drv = bdrv; 142ea2384d3Sbellard } 143b338082bSbellard 144b338082bSbellard /* create a new block device (by default it is empty) */ 145b338082bSbellard BlockDriverState *bdrv_new(const char *device_name) 146fc01f7e7Sbellard { 147b338082bSbellard BlockDriverState **pbs, *bs; 148b338082bSbellard 149b338082bSbellard bs = qemu_mallocz(sizeof(BlockDriverState)); 150b338082bSbellard if(!bs) 151b338082bSbellard return NULL; 152b338082bSbellard pstrcpy(bs->device_name, sizeof(bs->device_name), device_name); 153ea2384d3Sbellard if (device_name[0] != '\0') { 154b338082bSbellard /* insert at the end */ 155b338082bSbellard pbs = &bdrv_first; 156b338082bSbellard while (*pbs != NULL) 157b338082bSbellard pbs = &(*pbs)->next; 158b338082bSbellard *pbs = bs; 159ea2384d3Sbellard } 160b338082bSbellard return bs; 161b338082bSbellard } 162b338082bSbellard 163ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name) 164ea2384d3Sbellard { 165ea2384d3Sbellard BlockDriver *drv1; 166ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 167ea2384d3Sbellard if (!strcmp(drv1->format_name, format_name)) 168ea2384d3Sbellard return drv1; 169ea2384d3Sbellard } 170ea2384d3Sbellard return NULL; 171ea2384d3Sbellard } 172ea2384d3Sbellard 173ea2384d3Sbellard int bdrv_create(BlockDriver *drv, 174ea2384d3Sbellard const char *filename, int64_t size_in_sectors, 175ea2384d3Sbellard const char *backing_file, int flags) 176ea2384d3Sbellard { 177ea2384d3Sbellard if (!drv->bdrv_create) 178ea2384d3Sbellard return -ENOTSUP; 179ea2384d3Sbellard return drv->bdrv_create(filename, size_in_sectors, backing_file, flags); 180ea2384d3Sbellard } 181ea2384d3Sbellard 182d5249393Sbellard #ifdef _WIN32 18395389c86Sbellard void get_tmp_filename(char *filename, int size) 184d5249393Sbellard { 1853b9f94e1Sbellard char temp_dir[MAX_PATH]; 1863b9f94e1Sbellard 1873b9f94e1Sbellard GetTempPath(MAX_PATH, temp_dir); 1883b9f94e1Sbellard GetTempFileName(temp_dir, "qem", 0, filename); 189d5249393Sbellard } 190d5249393Sbellard #else 19195389c86Sbellard void get_tmp_filename(char *filename, int size) 192ea2384d3Sbellard { 193ea2384d3Sbellard int fd; 194d5249393Sbellard /* XXX: race condition possible */ 195ea2384d3Sbellard pstrcpy(filename, size, "/tmp/vl.XXXXXX"); 196ea2384d3Sbellard fd = mkstemp(filename); 197ea2384d3Sbellard close(fd); 198ea2384d3Sbellard } 199d5249393Sbellard #endif 200ea2384d3Sbellard 20119cb3738Sbellard #ifdef _WIN32 202f45512feSbellard static int is_windows_drive_prefix(const char *filename) 203f45512feSbellard { 204f45512feSbellard return (((filename[0] >= 'a' && filename[0] <= 'z') || 205f45512feSbellard (filename[0] >= 'A' && filename[0] <= 'Z')) && 206f45512feSbellard filename[1] == ':'); 207f45512feSbellard } 208f45512feSbellard 20919cb3738Sbellard static int is_windows_drive(const char *filename) 21019cb3738Sbellard { 211f45512feSbellard if (is_windows_drive_prefix(filename) && 212f45512feSbellard filename[2] == '\0') 21319cb3738Sbellard return 1; 21419cb3738Sbellard if (strstart(filename, "\\\\.\\", NULL) || 21519cb3738Sbellard strstart(filename, "//./", NULL)) 21619cb3738Sbellard return 1; 21719cb3738Sbellard return 0; 21819cb3738Sbellard } 21919cb3738Sbellard #endif 22019cb3738Sbellard 22183f64091Sbellard static BlockDriver *find_protocol(const char *filename) 22283f64091Sbellard { 22383f64091Sbellard BlockDriver *drv1; 22483f64091Sbellard char protocol[128]; 22583f64091Sbellard int len; 22683f64091Sbellard const char *p; 22719cb3738Sbellard 22819cb3738Sbellard #ifdef _WIN32 229f45512feSbellard if (is_windows_drive(filename) || 230f45512feSbellard is_windows_drive_prefix(filename)) 23119cb3738Sbellard return &bdrv_raw; 23219cb3738Sbellard #endif 23383f64091Sbellard p = strchr(filename, ':'); 23483f64091Sbellard if (!p) 23583f64091Sbellard return &bdrv_raw; 23683f64091Sbellard len = p - filename; 23783f64091Sbellard if (len > sizeof(protocol) - 1) 23883f64091Sbellard len = sizeof(protocol) - 1; 23983f64091Sbellard memcpy(protocol, filename, len); 24083f64091Sbellard protocol[len] = '\0'; 24183f64091Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 24283f64091Sbellard if (drv1->protocol_name && 24383f64091Sbellard !strcmp(drv1->protocol_name, protocol)) 24483f64091Sbellard return drv1; 24583f64091Sbellard } 24683f64091Sbellard return NULL; 24783f64091Sbellard } 24883f64091Sbellard 2497674e7bfSbellard /* XXX: force raw format if block or character device ? It would 2507674e7bfSbellard simplify the BSD case */ 251ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename) 252ea2384d3Sbellard { 25383f64091Sbellard int ret, score, score_max; 254ea2384d3Sbellard BlockDriver *drv1, *drv; 25583f64091Sbellard uint8_t buf[2048]; 25683f64091Sbellard BlockDriverState *bs; 257ea2384d3Sbellard 25819cb3738Sbellard /* detect host devices. By convention, /dev/cdrom[N] is always 25919cb3738Sbellard recognized as a host CDROM */ 26019cb3738Sbellard if (strstart(filename, "/dev/cdrom", NULL)) 26119cb3738Sbellard return &bdrv_host_device; 26219cb3738Sbellard #ifdef _WIN32 26319cb3738Sbellard if (is_windows_drive(filename)) 26419cb3738Sbellard return &bdrv_host_device; 26519cb3738Sbellard #else 26619cb3738Sbellard { 26719cb3738Sbellard struct stat st; 26819cb3738Sbellard if (stat(filename, &st) >= 0 && 26919cb3738Sbellard (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) { 27019cb3738Sbellard return &bdrv_host_device; 27119cb3738Sbellard } 27219cb3738Sbellard } 27319cb3738Sbellard #endif 27419cb3738Sbellard 27583f64091Sbellard drv = find_protocol(filename); 27619cb3738Sbellard /* no need to test disk image formats for vvfat */ 27783f64091Sbellard if (drv == &bdrv_vvfat) 27883f64091Sbellard return drv; 27983f64091Sbellard 28083f64091Sbellard ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY); 28183f64091Sbellard if (ret < 0) 2827674e7bfSbellard return NULL; 28383f64091Sbellard ret = bdrv_pread(bs, 0, buf, sizeof(buf)); 28483f64091Sbellard bdrv_delete(bs); 285ea2384d3Sbellard if (ret < 0) { 286ea2384d3Sbellard return NULL; 287ea2384d3Sbellard } 288ea2384d3Sbellard 289ea2384d3Sbellard score_max = 0; 290ea2384d3Sbellard for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) { 29183f64091Sbellard if (drv1->bdrv_probe) { 292ea2384d3Sbellard score = drv1->bdrv_probe(buf, ret, filename); 293ea2384d3Sbellard if (score > score_max) { 294ea2384d3Sbellard score_max = score; 295ea2384d3Sbellard drv = drv1; 296ea2384d3Sbellard } 297ea2384d3Sbellard } 29883f64091Sbellard } 299ea2384d3Sbellard return drv; 300ea2384d3Sbellard } 301ea2384d3Sbellard 30283f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags) 303b338082bSbellard { 30483f64091Sbellard BlockDriverState *bs; 30583f64091Sbellard int ret; 3063b0d4f61Sbellard 30783f64091Sbellard bs = bdrv_new(""); 30883f64091Sbellard if (!bs) 30983f64091Sbellard return -ENOMEM; 31083f64091Sbellard ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL); 31183f64091Sbellard if (ret < 0) { 31283f64091Sbellard bdrv_delete(bs); 31383f64091Sbellard return ret; 3143b0d4f61Sbellard } 31583f64091Sbellard *pbs = bs; 31683f64091Sbellard return 0; 3173b0d4f61Sbellard } 3183b0d4f61Sbellard 31983f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags) 32083f64091Sbellard { 32183f64091Sbellard return bdrv_open2(bs, filename, flags, NULL); 322ea2384d3Sbellard } 323ea2384d3Sbellard 32483f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags, 325ea2384d3Sbellard BlockDriver *drv) 326ea2384d3Sbellard { 32783f64091Sbellard int ret, open_flags; 328eb5c851fSths char tmp_filename[PATH_MAX]; 329eb5c851fSths char backing_filename[PATH_MAX]; 330fc01f7e7Sbellard 3310849bf08Sbellard bs->read_only = 0; 332ea2384d3Sbellard bs->is_temporary = 0; 333ea2384d3Sbellard bs->encrypted = 0; 33433e3963eSbellard 33583f64091Sbellard if (flags & BDRV_O_SNAPSHOT) { 336ea2384d3Sbellard BlockDriverState *bs1; 337ea2384d3Sbellard int64_t total_size; 33833e3963eSbellard 339ea2384d3Sbellard /* if snapshot, we create a temporary backing file and open it 340ea2384d3Sbellard instead of opening 'filename' directly */ 341ea2384d3Sbellard 342ea2384d3Sbellard /* if there is a backing file, use it */ 343ea2384d3Sbellard bs1 = bdrv_new(""); 344ea2384d3Sbellard if (!bs1) { 34583f64091Sbellard return -ENOMEM; 346ea2384d3Sbellard } 347ea2384d3Sbellard if (bdrv_open(bs1, filename, 0) < 0) { 348ea2384d3Sbellard bdrv_delete(bs1); 349ea2384d3Sbellard return -1; 350ea2384d3Sbellard } 35183f64091Sbellard total_size = bdrv_getlength(bs1) >> SECTOR_BITS; 352ea2384d3Sbellard bdrv_delete(bs1); 353ea2384d3Sbellard 354ea2384d3Sbellard get_tmp_filename(tmp_filename, sizeof(tmp_filename)); 355a817d936Sbellard realpath(filename, backing_filename); 356d15a771dSbellard if (bdrv_create(&bdrv_qcow2, tmp_filename, 357a817d936Sbellard total_size, backing_filename, 0) < 0) { 358ea2384d3Sbellard return -1; 359ea2384d3Sbellard } 360ea2384d3Sbellard filename = tmp_filename; 361ea2384d3Sbellard bs->is_temporary = 1; 362ea2384d3Sbellard } 363ea2384d3Sbellard 364ea2384d3Sbellard pstrcpy(bs->filename, sizeof(bs->filename), filename); 36583f64091Sbellard if (flags & BDRV_O_FILE) { 36683f64091Sbellard drv = find_protocol(filename); 36783f64091Sbellard if (!drv) 36883f64091Sbellard return -ENOENT; 36983f64091Sbellard } else { 370ea2384d3Sbellard if (!drv) { 371ea2384d3Sbellard drv = find_image_format(filename); 372ea2384d3Sbellard if (!drv) 373ea2384d3Sbellard return -1; 374ea2384d3Sbellard } 37583f64091Sbellard } 376ea2384d3Sbellard bs->drv = drv; 377ea2384d3Sbellard bs->opaque = qemu_mallocz(drv->instance_size); 378ea2384d3Sbellard if (bs->opaque == NULL && drv->instance_size > 0) 379ea2384d3Sbellard return -1; 38083f64091Sbellard /* Note: for compatibility, we open disk image files as RDWR, and 38183f64091Sbellard RDONLY as fallback */ 38283f64091Sbellard if (!(flags & BDRV_O_FILE)) 38383f64091Sbellard open_flags = BDRV_O_RDWR; 38483f64091Sbellard else 38583f64091Sbellard open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT); 38683f64091Sbellard ret = drv->bdrv_open(bs, filename, open_flags); 38783f64091Sbellard if (ret == -EACCES && !(flags & BDRV_O_FILE)) { 38883f64091Sbellard ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY); 38983f64091Sbellard bs->read_only = 1; 39083f64091Sbellard } 391ea2384d3Sbellard if (ret < 0) { 392ea2384d3Sbellard qemu_free(bs->opaque); 3936b21b973Sbellard bs->opaque = NULL; 3946b21b973Sbellard bs->drv = NULL; 39583f64091Sbellard return ret; 396ea2384d3Sbellard } 397d15a771dSbellard if (drv->bdrv_getlength) { 398d15a771dSbellard bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 399d15a771dSbellard } 400ea2384d3Sbellard #ifndef _WIN32 401ea2384d3Sbellard if (bs->is_temporary) { 402ea2384d3Sbellard unlink(filename); 40333e3963eSbellard } 40467b915a5Sbellard #endif 40583f64091Sbellard if (bs->backing_file[0] != '\0') { 406ea2384d3Sbellard /* if there is a backing file, use it */ 407ea2384d3Sbellard bs->backing_hd = bdrv_new(""); 408ea2384d3Sbellard if (!bs->backing_hd) { 409ea2384d3Sbellard fail: 410ea2384d3Sbellard bdrv_close(bs); 4116b21b973Sbellard return -ENOMEM; 412ea2384d3Sbellard } 41383f64091Sbellard path_combine(backing_filename, sizeof(backing_filename), 41483f64091Sbellard filename, bs->backing_file); 41583f64091Sbellard if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0) 416ea2384d3Sbellard goto fail; 417ea2384d3Sbellard } 41833e3963eSbellard 419b338082bSbellard /* call the change callback */ 42019cb3738Sbellard bs->media_changed = 1; 421b338082bSbellard if (bs->change_cb) 422b338082bSbellard bs->change_cb(bs->change_opaque); 423b338082bSbellard 424b338082bSbellard return 0; 425fc01f7e7Sbellard } 426fc01f7e7Sbellard 427fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs) 428fc01f7e7Sbellard { 42919cb3738Sbellard if (bs->drv) { 430ea2384d3Sbellard if (bs->backing_hd) 431ea2384d3Sbellard bdrv_delete(bs->backing_hd); 432ea2384d3Sbellard bs->drv->bdrv_close(bs); 433ea2384d3Sbellard qemu_free(bs->opaque); 434ea2384d3Sbellard #ifdef _WIN32 435ea2384d3Sbellard if (bs->is_temporary) { 436ea2384d3Sbellard unlink(bs->filename); 437ea2384d3Sbellard } 43867b915a5Sbellard #endif 439ea2384d3Sbellard bs->opaque = NULL; 440ea2384d3Sbellard bs->drv = NULL; 441b338082bSbellard 442b338082bSbellard /* call the change callback */ 44319cb3738Sbellard bs->media_changed = 1; 444b338082bSbellard if (bs->change_cb) 445b338082bSbellard bs->change_cb(bs->change_opaque); 446b338082bSbellard } 447b338082bSbellard } 448b338082bSbellard 449b338082bSbellard void bdrv_delete(BlockDriverState *bs) 450b338082bSbellard { 451ea2384d3Sbellard /* XXX: remove the driver list */ 452b338082bSbellard bdrv_close(bs); 453b338082bSbellard qemu_free(bs); 454fc01f7e7Sbellard } 455fc01f7e7Sbellard 45633e3963eSbellard /* commit COW file into the raw image */ 45733e3963eSbellard int bdrv_commit(BlockDriverState *bs) 45833e3963eSbellard { 45919cb3738Sbellard BlockDriver *drv = bs->drv; 46083f64091Sbellard int64_t i, total_sectors; 461ea2384d3Sbellard int n, j; 462ea2384d3Sbellard unsigned char sector[512]; 46333e3963eSbellard 46419cb3738Sbellard if (!drv) 46519cb3738Sbellard return -ENOMEDIUM; 46633e3963eSbellard 46733e3963eSbellard if (bs->read_only) { 468ea2384d3Sbellard return -EACCES; 46933e3963eSbellard } 47033e3963eSbellard 471ea2384d3Sbellard if (!bs->backing_hd) { 472ea2384d3Sbellard return -ENOTSUP; 473ea2384d3Sbellard } 474ea2384d3Sbellard 47583f64091Sbellard total_sectors = bdrv_getlength(bs) >> SECTOR_BITS; 47683f64091Sbellard for (i = 0; i < total_sectors;) { 47719cb3738Sbellard if (drv->bdrv_is_allocated(bs, i, 65536, &n)) { 478ea2384d3Sbellard for(j = 0; j < n; j++) { 47933e3963eSbellard if (bdrv_read(bs, i, sector, 1) != 0) { 480ea2384d3Sbellard return -EIO; 48133e3963eSbellard } 48233e3963eSbellard 483ea2384d3Sbellard if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) { 484ea2384d3Sbellard return -EIO; 48533e3963eSbellard } 486ea2384d3Sbellard i++; 487ea2384d3Sbellard } 488ea2384d3Sbellard } else { 489ea2384d3Sbellard i += n; 49033e3963eSbellard } 49133e3963eSbellard } 49295389c86Sbellard 49319cb3738Sbellard if (drv->bdrv_make_empty) 49419cb3738Sbellard return drv->bdrv_make_empty(bs); 49595389c86Sbellard 49633e3963eSbellard return 0; 49733e3963eSbellard } 49833e3963eSbellard 49919cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */ 500fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num, 501fc01f7e7Sbellard uint8_t *buf, int nb_sectors) 502fc01f7e7Sbellard { 503ea2384d3Sbellard BlockDriver *drv = bs->drv; 504fc01f7e7Sbellard 50519cb3738Sbellard if (!drv) 50619cb3738Sbellard return -ENOMEDIUM; 507b338082bSbellard 50883f64091Sbellard if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { 509cf98951bSbellard memcpy(buf, bs->boot_sector_data, 512); 51083f64091Sbellard sector_num++; 51183f64091Sbellard nb_sectors--; 51283f64091Sbellard buf += 512; 51383f64091Sbellard if (nb_sectors == 0) 514fc01f7e7Sbellard return 0; 515fc01f7e7Sbellard } 51683f64091Sbellard if (drv->bdrv_pread) { 51783f64091Sbellard int ret, len; 51883f64091Sbellard len = nb_sectors * 512; 51983f64091Sbellard ret = drv->bdrv_pread(bs, sector_num * 512, buf, len); 52083f64091Sbellard if (ret < 0) 52183f64091Sbellard return ret; 52283f64091Sbellard else if (ret != len) 52319cb3738Sbellard return -EINVAL; 524*a36e69ddSths else { 525*a36e69ddSths bs->rd_bytes += (unsigned) len; 526*a36e69ddSths bs->rd_ops ++; 52783f64091Sbellard return 0; 528*a36e69ddSths } 52983f64091Sbellard } else { 53083f64091Sbellard return drv->bdrv_read(bs, sector_num, buf, nb_sectors); 53183f64091Sbellard } 53283f64091Sbellard } 533fc01f7e7Sbellard 53419cb3738Sbellard /* Return < 0 if error. Important errors are: 53519cb3738Sbellard -EIO generic I/O error (may happen for all errors) 53619cb3738Sbellard -ENOMEDIUM No media inserted. 53719cb3738Sbellard -EINVAL Invalid sector number or nb_sectors 53819cb3738Sbellard -EACCES Trying to write a read-only device 53919cb3738Sbellard */ 540fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num, 541fc01f7e7Sbellard const uint8_t *buf, int nb_sectors) 542fc01f7e7Sbellard { 54383f64091Sbellard BlockDriver *drv = bs->drv; 54419cb3738Sbellard if (!bs->drv) 54519cb3738Sbellard return -ENOMEDIUM; 5460849bf08Sbellard if (bs->read_only) 54719cb3738Sbellard return -EACCES; 54879639d42Sbellard if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { 54979639d42Sbellard memcpy(bs->boot_sector_data, buf, 512); 55079639d42Sbellard } 55183f64091Sbellard if (drv->bdrv_pwrite) { 55283f64091Sbellard int ret, len; 55383f64091Sbellard len = nb_sectors * 512; 55483f64091Sbellard ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len); 55583f64091Sbellard if (ret < 0) 55683f64091Sbellard return ret; 55783f64091Sbellard else if (ret != len) 55883f64091Sbellard return -EIO; 559*a36e69ddSths else { 560*a36e69ddSths bs->wr_bytes += (unsigned) len; 561*a36e69ddSths bs->wr_ops ++; 56283f64091Sbellard return 0; 563*a36e69ddSths } 56483f64091Sbellard } else { 56583f64091Sbellard return drv->bdrv_write(bs, sector_num, buf, nb_sectors); 56683f64091Sbellard } 56783f64091Sbellard } 56883f64091Sbellard 56983f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset, 570faea38e7Sbellard uint8_t *buf, int count1) 57183f64091Sbellard { 57283f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 57383f64091Sbellard int len, nb_sectors, count; 57483f64091Sbellard int64_t sector_num; 57583f64091Sbellard 57683f64091Sbellard count = count1; 57783f64091Sbellard /* first read to align to sector start */ 57883f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 57983f64091Sbellard if (len > count) 58083f64091Sbellard len = count; 58183f64091Sbellard sector_num = offset >> SECTOR_BITS; 58283f64091Sbellard if (len > 0) { 58383f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 58483f64091Sbellard return -EIO; 58583f64091Sbellard memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len); 58683f64091Sbellard count -= len; 58783f64091Sbellard if (count == 0) 58883f64091Sbellard return count1; 58983f64091Sbellard sector_num++; 59083f64091Sbellard buf += len; 59183f64091Sbellard } 59283f64091Sbellard 59383f64091Sbellard /* read the sectors "in place" */ 59483f64091Sbellard nb_sectors = count >> SECTOR_BITS; 59583f64091Sbellard if (nb_sectors > 0) { 59683f64091Sbellard if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0) 59783f64091Sbellard return -EIO; 59883f64091Sbellard sector_num += nb_sectors; 59983f64091Sbellard len = nb_sectors << SECTOR_BITS; 60083f64091Sbellard buf += len; 60183f64091Sbellard count -= len; 60283f64091Sbellard } 60383f64091Sbellard 60483f64091Sbellard /* add data from the last sector */ 60583f64091Sbellard if (count > 0) { 60683f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 60783f64091Sbellard return -EIO; 60883f64091Sbellard memcpy(buf, tmp_buf, count); 60983f64091Sbellard } 61083f64091Sbellard return count1; 61183f64091Sbellard } 61283f64091Sbellard 61383f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset, 614faea38e7Sbellard const uint8_t *buf, int count1) 61583f64091Sbellard { 61683f64091Sbellard uint8_t tmp_buf[SECTOR_SIZE]; 61783f64091Sbellard int len, nb_sectors, count; 61883f64091Sbellard int64_t sector_num; 61983f64091Sbellard 62083f64091Sbellard count = count1; 62183f64091Sbellard /* first write to align to sector start */ 62283f64091Sbellard len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1); 62383f64091Sbellard if (len > count) 62483f64091Sbellard len = count; 62583f64091Sbellard sector_num = offset >> SECTOR_BITS; 62683f64091Sbellard if (len > 0) { 62783f64091Sbellard if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0) 62883f64091Sbellard return -EIO; 62983f64091Sbellard memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len); 63083f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 63183f64091Sbellard return -EIO; 63283f64091Sbellard count -= len; 63383f64091Sbellard if (count == 0) 63483f64091Sbellard return count1; 63583f64091Sbellard sector_num++; 63683f64091Sbellard buf += len; 63783f64091Sbellard } 63883f64091Sbellard 63983f64091Sbellard /* write the sectors "in place" */ 64083f64091Sbellard nb_sectors = count >> SECTOR_BITS; 64183f64091Sbellard if (nb_sectors > 0) { 64283f64091Sbellard if (bdrv_write(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(tmp_buf, buf, count); 65583f64091Sbellard if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0) 65683f64091Sbellard return -EIO; 65783f64091Sbellard } 65883f64091Sbellard return count1; 65983f64091Sbellard } 66083f64091Sbellard 66183f64091Sbellard /** 66283f64091Sbellard * Read with byte offsets (needed only for file protocols) 66383f64091Sbellard */ 66483f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset, 66583f64091Sbellard void *buf1, int count1) 66683f64091Sbellard { 66783f64091Sbellard BlockDriver *drv = bs->drv; 66883f64091Sbellard 66983f64091Sbellard if (!drv) 67019cb3738Sbellard return -ENOMEDIUM; 67183f64091Sbellard if (!drv->bdrv_pread) 672faea38e7Sbellard return bdrv_pread_em(bs, offset, buf1, count1); 67383f64091Sbellard return drv->bdrv_pread(bs, offset, buf1, count1); 67483f64091Sbellard } 67583f64091Sbellard 67683f64091Sbellard /** 67783f64091Sbellard * Write with byte offsets (needed only for file protocols) 67883f64091Sbellard */ 67983f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset, 68083f64091Sbellard const void *buf1, int count1) 68183f64091Sbellard { 68283f64091Sbellard BlockDriver *drv = bs->drv; 68383f64091Sbellard 68483f64091Sbellard if (!drv) 68519cb3738Sbellard return -ENOMEDIUM; 68683f64091Sbellard if (!drv->bdrv_pwrite) 687faea38e7Sbellard return bdrv_pwrite_em(bs, offset, buf1, count1); 68883f64091Sbellard return drv->bdrv_pwrite(bs, offset, buf1, count1); 68983f64091Sbellard } 69083f64091Sbellard 69183f64091Sbellard /** 69283f64091Sbellard * Truncate file to 'offset' bytes (needed only for file protocols) 69383f64091Sbellard */ 69483f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset) 69583f64091Sbellard { 69683f64091Sbellard BlockDriver *drv = bs->drv; 69783f64091Sbellard if (!drv) 69819cb3738Sbellard return -ENOMEDIUM; 69983f64091Sbellard if (!drv->bdrv_truncate) 70083f64091Sbellard return -ENOTSUP; 70183f64091Sbellard return drv->bdrv_truncate(bs, offset); 70283f64091Sbellard } 70383f64091Sbellard 70483f64091Sbellard /** 70583f64091Sbellard * Length of a file in bytes. Return < 0 if error or unknown. 70683f64091Sbellard */ 70783f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs) 70883f64091Sbellard { 70983f64091Sbellard BlockDriver *drv = bs->drv; 71083f64091Sbellard if (!drv) 71119cb3738Sbellard return -ENOMEDIUM; 71283f64091Sbellard if (!drv->bdrv_getlength) { 71383f64091Sbellard /* legacy mode */ 71483f64091Sbellard return bs->total_sectors * SECTOR_SIZE; 71583f64091Sbellard } 71683f64091Sbellard return drv->bdrv_getlength(bs); 717fc01f7e7Sbellard } 718fc01f7e7Sbellard 71919cb3738Sbellard /* return 0 as number of sectors if no device present or error */ 720fc01f7e7Sbellard void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr) 721fc01f7e7Sbellard { 72219cb3738Sbellard int64_t length; 72319cb3738Sbellard length = bdrv_getlength(bs); 72419cb3738Sbellard if (length < 0) 72519cb3738Sbellard length = 0; 72619cb3738Sbellard else 72719cb3738Sbellard length = length >> SECTOR_BITS; 72819cb3738Sbellard *nb_sectors_ptr = length; 729fc01f7e7Sbellard } 730cf98951bSbellard 731cf98951bSbellard /* force a given boot sector. */ 732cf98951bSbellard void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size) 733cf98951bSbellard { 734cf98951bSbellard bs->boot_sector_enabled = 1; 735cf98951bSbellard if (size > 512) 736cf98951bSbellard size = 512; 737cf98951bSbellard memcpy(bs->boot_sector_data, data, size); 738cf98951bSbellard memset(bs->boot_sector_data + size, 0, 512 - size); 739cf98951bSbellard } 740b338082bSbellard 741b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs, 742b338082bSbellard int cyls, int heads, int secs) 743b338082bSbellard { 744b338082bSbellard bs->cyls = cyls; 745b338082bSbellard bs->heads = heads; 746b338082bSbellard bs->secs = secs; 747b338082bSbellard } 748b338082bSbellard 749b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type) 750b338082bSbellard { 751b338082bSbellard bs->type = type; 752b338082bSbellard bs->removable = ((type == BDRV_TYPE_CDROM || 753b338082bSbellard type == BDRV_TYPE_FLOPPY)); 754b338082bSbellard } 755b338082bSbellard 75646d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation) 75746d4767dSbellard { 75846d4767dSbellard bs->translation = translation; 75946d4767dSbellard } 76046d4767dSbellard 761b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs, 762b338082bSbellard int *pcyls, int *pheads, int *psecs) 763b338082bSbellard { 764b338082bSbellard *pcyls = bs->cyls; 765b338082bSbellard *pheads = bs->heads; 766b338082bSbellard *psecs = bs->secs; 767b338082bSbellard } 768b338082bSbellard 769b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs) 770b338082bSbellard { 771b338082bSbellard return bs->type; 772b338082bSbellard } 773b338082bSbellard 77446d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs) 77546d4767dSbellard { 77646d4767dSbellard return bs->translation; 77746d4767dSbellard } 77846d4767dSbellard 779b338082bSbellard int bdrv_is_removable(BlockDriverState *bs) 780b338082bSbellard { 781b338082bSbellard return bs->removable; 782b338082bSbellard } 783b338082bSbellard 784b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs) 785b338082bSbellard { 786b338082bSbellard return bs->read_only; 787b338082bSbellard } 788b338082bSbellard 78919cb3738Sbellard /* XXX: no longer used */ 790b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs, 791b338082bSbellard void (*change_cb)(void *opaque), void *opaque) 792b338082bSbellard { 793b338082bSbellard bs->change_cb = change_cb; 794b338082bSbellard bs->change_opaque = opaque; 795b338082bSbellard } 796b338082bSbellard 797ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs) 798ea2384d3Sbellard { 799ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) 800ea2384d3Sbellard return 1; 801ea2384d3Sbellard return bs->encrypted; 802ea2384d3Sbellard } 803ea2384d3Sbellard 804ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key) 805ea2384d3Sbellard { 806ea2384d3Sbellard int ret; 807ea2384d3Sbellard if (bs->backing_hd && bs->backing_hd->encrypted) { 808ea2384d3Sbellard ret = bdrv_set_key(bs->backing_hd, key); 809ea2384d3Sbellard if (ret < 0) 810ea2384d3Sbellard return ret; 811ea2384d3Sbellard if (!bs->encrypted) 812ea2384d3Sbellard return 0; 813ea2384d3Sbellard } 814ea2384d3Sbellard if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key) 815ea2384d3Sbellard return -1; 816ea2384d3Sbellard return bs->drv->bdrv_set_key(bs, key); 817ea2384d3Sbellard } 818ea2384d3Sbellard 819ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size) 820ea2384d3Sbellard { 82119cb3738Sbellard if (!bs->drv) { 822ea2384d3Sbellard buf[0] = '\0'; 823ea2384d3Sbellard } else { 824ea2384d3Sbellard pstrcpy(buf, buf_size, bs->drv->format_name); 825ea2384d3Sbellard } 826ea2384d3Sbellard } 827ea2384d3Sbellard 828ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name), 829ea2384d3Sbellard void *opaque) 830ea2384d3Sbellard { 831ea2384d3Sbellard BlockDriver *drv; 832ea2384d3Sbellard 833ea2384d3Sbellard for (drv = first_drv; drv != NULL; drv = drv->next) { 834ea2384d3Sbellard it(opaque, drv->format_name); 835ea2384d3Sbellard } 836ea2384d3Sbellard } 837ea2384d3Sbellard 838b338082bSbellard BlockDriverState *bdrv_find(const char *name) 839b338082bSbellard { 840b338082bSbellard BlockDriverState *bs; 841b338082bSbellard 842b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 843b338082bSbellard if (!strcmp(name, bs->device_name)) 844b338082bSbellard return bs; 845b338082bSbellard } 846b338082bSbellard return NULL; 847b338082bSbellard } 848b338082bSbellard 84981d0912dSbellard void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque) 85081d0912dSbellard { 85181d0912dSbellard BlockDriverState *bs; 85281d0912dSbellard 85381d0912dSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 85481d0912dSbellard it(opaque, bs->device_name); 85581d0912dSbellard } 85681d0912dSbellard } 85781d0912dSbellard 858ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs) 859ea2384d3Sbellard { 860ea2384d3Sbellard return bs->device_name; 861ea2384d3Sbellard } 862ea2384d3Sbellard 8637a6cba61Spbrook void bdrv_flush(BlockDriverState *bs) 8647a6cba61Spbrook { 8657a6cba61Spbrook if (bs->drv->bdrv_flush) 8667a6cba61Spbrook bs->drv->bdrv_flush(bs); 8677a6cba61Spbrook if (bs->backing_hd) 8687a6cba61Spbrook bdrv_flush(bs->backing_hd); 8697a6cba61Spbrook } 8707a6cba61Spbrook 871faf07963Spbrook #ifndef QEMU_IMG 872b338082bSbellard void bdrv_info(void) 873b338082bSbellard { 874b338082bSbellard BlockDriverState *bs; 875b338082bSbellard 876b338082bSbellard for (bs = bdrv_first; bs != NULL; bs = bs->next) { 877b338082bSbellard term_printf("%s:", bs->device_name); 878b338082bSbellard term_printf(" type="); 879b338082bSbellard switch(bs->type) { 880b338082bSbellard case BDRV_TYPE_HD: 881b338082bSbellard term_printf("hd"); 882b338082bSbellard break; 883b338082bSbellard case BDRV_TYPE_CDROM: 884b338082bSbellard term_printf("cdrom"); 885b338082bSbellard break; 886b338082bSbellard case BDRV_TYPE_FLOPPY: 887b338082bSbellard term_printf("floppy"); 888b338082bSbellard break; 889b338082bSbellard } 890b338082bSbellard term_printf(" removable=%d", bs->removable); 891b338082bSbellard if (bs->removable) { 892b338082bSbellard term_printf(" locked=%d", bs->locked); 893b338082bSbellard } 89419cb3738Sbellard if (bs->drv) { 895fef30743Sths term_printf(" file="); 896fef30743Sths term_print_filename(bs->filename); 897fef30743Sths if (bs->backing_file[0] != '\0') { 898fef30743Sths term_printf(" backing_file="); 899fef30743Sths term_print_filename(bs->backing_file); 900fef30743Sths } 901b338082bSbellard term_printf(" ro=%d", bs->read_only); 902ea2384d3Sbellard term_printf(" drv=%s", bs->drv->format_name); 903ea2384d3Sbellard if (bs->encrypted) 904ea2384d3Sbellard term_printf(" encrypted"); 905b338082bSbellard } else { 906b338082bSbellard term_printf(" [not inserted]"); 907b338082bSbellard } 908b338082bSbellard term_printf("\n"); 909b338082bSbellard } 910b338082bSbellard } 911*a36e69ddSths 912*a36e69ddSths /* The "info blockstats" command. */ 913*a36e69ddSths void bdrv_info_stats (void) 914*a36e69ddSths { 915*a36e69ddSths BlockDriverState *bs; 916*a36e69ddSths 917*a36e69ddSths for (bs = bdrv_first; bs != NULL; bs = bs->next) { 918*a36e69ddSths term_printf ("%s:" 919*a36e69ddSths " rd_bytes=%" PRIu64 920*a36e69ddSths " wr_bytes=%" PRIu64 921*a36e69ddSths " rd_operations=%" PRIu64 922*a36e69ddSths " wr_operations=%" PRIu64 923*a36e69ddSths "\n", 924*a36e69ddSths bs->device_name, 925*a36e69ddSths bs->rd_bytes, bs->wr_bytes, 926*a36e69ddSths bs->rd_ops, bs->wr_ops); 927*a36e69ddSths } 928*a36e69ddSths } 929faf07963Spbrook #endif 930ea2384d3Sbellard 93183f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs, 93283f64091Sbellard char *filename, int filename_size) 93383f64091Sbellard { 93483f64091Sbellard if (!bs->backing_hd) { 93583f64091Sbellard pstrcpy(filename, filename_size, ""); 93683f64091Sbellard } else { 93783f64091Sbellard pstrcpy(filename, filename_size, bs->backing_file); 93883f64091Sbellard } 93983f64091Sbellard } 94083f64091Sbellard 941faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num, 942faea38e7Sbellard const uint8_t *buf, int nb_sectors) 943faea38e7Sbellard { 944faea38e7Sbellard BlockDriver *drv = bs->drv; 945faea38e7Sbellard if (!drv) 94619cb3738Sbellard return -ENOMEDIUM; 947faea38e7Sbellard if (!drv->bdrv_write_compressed) 948faea38e7Sbellard return -ENOTSUP; 949faea38e7Sbellard return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors); 950faea38e7Sbellard } 951faea38e7Sbellard 952faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi) 953faea38e7Sbellard { 954faea38e7Sbellard BlockDriver *drv = bs->drv; 955faea38e7Sbellard if (!drv) 95619cb3738Sbellard return -ENOMEDIUM; 957faea38e7Sbellard if (!drv->bdrv_get_info) 958faea38e7Sbellard return -ENOTSUP; 959faea38e7Sbellard memset(bdi, 0, sizeof(*bdi)); 960faea38e7Sbellard return drv->bdrv_get_info(bs, bdi); 961faea38e7Sbellard } 962faea38e7Sbellard 963faea38e7Sbellard /**************************************************************/ 964faea38e7Sbellard /* handling of snapshots */ 965faea38e7Sbellard 966faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs, 967faea38e7Sbellard QEMUSnapshotInfo *sn_info) 968faea38e7Sbellard { 969faea38e7Sbellard BlockDriver *drv = bs->drv; 970faea38e7Sbellard if (!drv) 97119cb3738Sbellard return -ENOMEDIUM; 972faea38e7Sbellard if (!drv->bdrv_snapshot_create) 973faea38e7Sbellard return -ENOTSUP; 974faea38e7Sbellard return drv->bdrv_snapshot_create(bs, sn_info); 975faea38e7Sbellard } 976faea38e7Sbellard 977faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs, 978faea38e7Sbellard const char *snapshot_id) 979faea38e7Sbellard { 980faea38e7Sbellard BlockDriver *drv = bs->drv; 981faea38e7Sbellard if (!drv) 98219cb3738Sbellard return -ENOMEDIUM; 983faea38e7Sbellard if (!drv->bdrv_snapshot_goto) 984faea38e7Sbellard return -ENOTSUP; 985faea38e7Sbellard return drv->bdrv_snapshot_goto(bs, snapshot_id); 986faea38e7Sbellard } 987faea38e7Sbellard 988faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id) 989faea38e7Sbellard { 990faea38e7Sbellard BlockDriver *drv = bs->drv; 991faea38e7Sbellard if (!drv) 99219cb3738Sbellard return -ENOMEDIUM; 993faea38e7Sbellard if (!drv->bdrv_snapshot_delete) 994faea38e7Sbellard return -ENOTSUP; 995faea38e7Sbellard return drv->bdrv_snapshot_delete(bs, snapshot_id); 996faea38e7Sbellard } 997faea38e7Sbellard 998faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs, 999faea38e7Sbellard QEMUSnapshotInfo **psn_info) 1000faea38e7Sbellard { 1001faea38e7Sbellard BlockDriver *drv = bs->drv; 1002faea38e7Sbellard if (!drv) 100319cb3738Sbellard return -ENOMEDIUM; 1004faea38e7Sbellard if (!drv->bdrv_snapshot_list) 1005faea38e7Sbellard return -ENOTSUP; 1006faea38e7Sbellard return drv->bdrv_snapshot_list(bs, psn_info); 1007faea38e7Sbellard } 1008faea38e7Sbellard 1009faea38e7Sbellard #define NB_SUFFIXES 4 1010faea38e7Sbellard 1011faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size) 1012faea38e7Sbellard { 1013faea38e7Sbellard static const char suffixes[NB_SUFFIXES] = "KMGT"; 1014faea38e7Sbellard int64_t base; 1015faea38e7Sbellard int i; 1016faea38e7Sbellard 1017faea38e7Sbellard if (size <= 999) { 1018faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64, size); 1019faea38e7Sbellard } else { 1020faea38e7Sbellard base = 1024; 1021faea38e7Sbellard for(i = 0; i < NB_SUFFIXES; i++) { 1022faea38e7Sbellard if (size < (10 * base)) { 1023faea38e7Sbellard snprintf(buf, buf_size, "%0.1f%c", 1024faea38e7Sbellard (double)size / base, 1025faea38e7Sbellard suffixes[i]); 1026faea38e7Sbellard break; 1027faea38e7Sbellard } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { 1028faea38e7Sbellard snprintf(buf, buf_size, "%" PRId64 "%c", 1029faea38e7Sbellard ((size + (base >> 1)) / base), 1030faea38e7Sbellard suffixes[i]); 1031faea38e7Sbellard break; 1032faea38e7Sbellard } 1033faea38e7Sbellard base = base * 1024; 1034faea38e7Sbellard } 1035faea38e7Sbellard } 1036faea38e7Sbellard return buf; 1037faea38e7Sbellard } 1038faea38e7Sbellard 1039faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn) 1040faea38e7Sbellard { 1041faea38e7Sbellard char buf1[128], date_buf[128], clock_buf[128]; 10423b9f94e1Sbellard #ifdef _WIN32 10433b9f94e1Sbellard struct tm *ptm; 10443b9f94e1Sbellard #else 1045faea38e7Sbellard struct tm tm; 10463b9f94e1Sbellard #endif 1047faea38e7Sbellard time_t ti; 1048faea38e7Sbellard int64_t secs; 1049faea38e7Sbellard 1050faea38e7Sbellard if (!sn) { 1051faea38e7Sbellard snprintf(buf, buf_size, 1052faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1053faea38e7Sbellard "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK"); 1054faea38e7Sbellard } else { 1055faea38e7Sbellard ti = sn->date_sec; 10563b9f94e1Sbellard #ifdef _WIN32 10573b9f94e1Sbellard ptm = localtime(&ti); 10583b9f94e1Sbellard strftime(date_buf, sizeof(date_buf), 10593b9f94e1Sbellard "%Y-%m-%d %H:%M:%S", ptm); 10603b9f94e1Sbellard #else 1061faea38e7Sbellard localtime_r(&ti, &tm); 1062faea38e7Sbellard strftime(date_buf, sizeof(date_buf), 1063faea38e7Sbellard "%Y-%m-%d %H:%M:%S", &tm); 10643b9f94e1Sbellard #endif 1065faea38e7Sbellard secs = sn->vm_clock_nsec / 1000000000; 1066faea38e7Sbellard snprintf(clock_buf, sizeof(clock_buf), 1067faea38e7Sbellard "%02d:%02d:%02d.%03d", 1068faea38e7Sbellard (int)(secs / 3600), 1069faea38e7Sbellard (int)((secs / 60) % 60), 1070faea38e7Sbellard (int)(secs % 60), 1071faea38e7Sbellard (int)((sn->vm_clock_nsec / 1000000) % 1000)); 1072faea38e7Sbellard snprintf(buf, buf_size, 1073faea38e7Sbellard "%-10s%-20s%7s%20s%15s", 1074faea38e7Sbellard sn->id_str, sn->name, 1075faea38e7Sbellard get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size), 1076faea38e7Sbellard date_buf, 1077faea38e7Sbellard clock_buf); 1078faea38e7Sbellard } 1079faea38e7Sbellard return buf; 1080faea38e7Sbellard } 1081faea38e7Sbellard 108283f64091Sbellard 1083ea2384d3Sbellard /**************************************************************/ 108483f64091Sbellard /* async I/Os */ 1085ea2384d3Sbellard 1086ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num, 108783f64091Sbellard uint8_t *buf, int nb_sectors, 108883f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 1089ea2384d3Sbellard { 109083f64091Sbellard BlockDriver *drv = bs->drv; 1091*a36e69ddSths BlockDriverAIOCB *ret; 1092ea2384d3Sbellard 109319cb3738Sbellard if (!drv) 1094ce1a14dcSpbrook return NULL; 109583f64091Sbellard 109683f64091Sbellard /* XXX: we assume that nb_sectors == 0 is suppored by the async read */ 109783f64091Sbellard if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { 109883f64091Sbellard memcpy(buf, bs->boot_sector_data, 512); 109983f64091Sbellard sector_num++; 110083f64091Sbellard nb_sectors--; 110183f64091Sbellard buf += 512; 1102ea2384d3Sbellard } 110383f64091Sbellard 1104*a36e69ddSths ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque); 1105*a36e69ddSths 1106*a36e69ddSths if (ret) { 1107*a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1108*a36e69ddSths bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1109*a36e69ddSths bs->rd_ops ++; 1110*a36e69ddSths } 1111*a36e69ddSths 1112*a36e69ddSths return ret; 111383f64091Sbellard } 111483f64091Sbellard 1115ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num, 111683f64091Sbellard const uint8_t *buf, int nb_sectors, 111783f64091Sbellard BlockDriverCompletionFunc *cb, void *opaque) 11187674e7bfSbellard { 111983f64091Sbellard BlockDriver *drv = bs->drv; 1120*a36e69ddSths BlockDriverAIOCB *ret; 112183f64091Sbellard 112219cb3738Sbellard if (!drv) 1123ce1a14dcSpbrook return NULL; 112483f64091Sbellard if (bs->read_only) 1125ce1a14dcSpbrook return NULL; 112683f64091Sbellard if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) { 112783f64091Sbellard memcpy(bs->boot_sector_data, buf, 512); 11287674e7bfSbellard } 112983f64091Sbellard 1130*a36e69ddSths ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque); 1131*a36e69ddSths 1132*a36e69ddSths if (ret) { 1133*a36e69ddSths /* Update stats even though technically transfer has not happened. */ 1134*a36e69ddSths bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE; 1135*a36e69ddSths bs->wr_ops ++; 1136*a36e69ddSths } 1137*a36e69ddSths 1138*a36e69ddSths return ret; 113983f64091Sbellard } 114083f64091Sbellard 114183f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb) 114283f64091Sbellard { 1143ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 114483f64091Sbellard 114583f64091Sbellard drv->bdrv_aio_cancel(acb); 114683f64091Sbellard } 114783f64091Sbellard 114883f64091Sbellard 114983f64091Sbellard /**************************************************************/ 115083f64091Sbellard /* async block device emulation */ 115183f64091Sbellard 1152faf07963Spbrook #ifdef QEMU_IMG 1153ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 1154ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 1155ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 1156ea2384d3Sbellard { 1157ea2384d3Sbellard int ret; 1158ce1a14dcSpbrook ret = bdrv_read(bs, sector_num, buf, nb_sectors); 1159ce1a14dcSpbrook cb(opaque, ret); 1160ce1a14dcSpbrook return NULL; 1161ea2384d3Sbellard } 1162ea2384d3Sbellard 1163ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 1164ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 1165ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 1166ea2384d3Sbellard { 1167ea2384d3Sbellard int ret; 1168ce1a14dcSpbrook ret = bdrv_write(bs, sector_num, buf, nb_sectors); 1169ce1a14dcSpbrook cb(opaque, ret); 1170ce1a14dcSpbrook return NULL; 1171ea2384d3Sbellard } 1172ea2384d3Sbellard 117383f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb) 1174ea2384d3Sbellard { 1175ea2384d3Sbellard } 1176beac80cdSbellard #else 117783f64091Sbellard static void bdrv_aio_bh_cb(void *opaque) 1178beac80cdSbellard { 1179ce1a14dcSpbrook BlockDriverAIOCBSync *acb = opaque; 1180ce1a14dcSpbrook acb->common.cb(acb->common.opaque, acb->ret); 1181ce1a14dcSpbrook qemu_aio_release(acb); 1182beac80cdSbellard } 1183beac80cdSbellard 1184ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs, 1185ce1a14dcSpbrook int64_t sector_num, uint8_t *buf, int nb_sectors, 1186ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 1187ea2384d3Sbellard { 1188ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 118983f64091Sbellard int ret; 119083f64091Sbellard 1191ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1192ce1a14dcSpbrook if (!acb->bh) 1193ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1194ce1a14dcSpbrook ret = bdrv_read(bs, sector_num, buf, nb_sectors); 1195ce1a14dcSpbrook acb->ret = ret; 1196ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1197ce1a14dcSpbrook return &acb->common; 11987a6cba61Spbrook } 11997a6cba61Spbrook 1200ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs, 1201ce1a14dcSpbrook int64_t sector_num, const uint8_t *buf, int nb_sectors, 1202ce1a14dcSpbrook BlockDriverCompletionFunc *cb, void *opaque) 120383f64091Sbellard { 1204ce1a14dcSpbrook BlockDriverAIOCBSync *acb; 120583f64091Sbellard int ret; 120683f64091Sbellard 1207ce1a14dcSpbrook acb = qemu_aio_get(bs, cb, opaque); 1208ce1a14dcSpbrook if (!acb->bh) 1209ce1a14dcSpbrook acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb); 1210ce1a14dcSpbrook ret = bdrv_write(bs, sector_num, buf, nb_sectors); 1211ce1a14dcSpbrook acb->ret = ret; 1212ce1a14dcSpbrook qemu_bh_schedule(acb->bh); 1213ce1a14dcSpbrook return &acb->common; 121483f64091Sbellard } 121583f64091Sbellard 1216ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb) 121783f64091Sbellard { 1218ce1a14dcSpbrook BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb; 1219ce1a14dcSpbrook qemu_bh_cancel(acb->bh); 1220ce1a14dcSpbrook qemu_aio_release(acb); 122183f64091Sbellard } 1222faf07963Spbrook #endif /* !QEMU_IMG */ 122383f64091Sbellard 122483f64091Sbellard /**************************************************************/ 122583f64091Sbellard /* sync block device emulation */ 122683f64091Sbellard 122783f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret) 122883f64091Sbellard { 122983f64091Sbellard *(int *)opaque = ret; 123083f64091Sbellard } 123183f64091Sbellard 123283f64091Sbellard #define NOT_DONE 0x7fffffff 123383f64091Sbellard 123483f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num, 123583f64091Sbellard uint8_t *buf, int nb_sectors) 123683f64091Sbellard { 1237ce1a14dcSpbrook int async_ret; 1238ce1a14dcSpbrook BlockDriverAIOCB *acb; 123983f64091Sbellard 124083f64091Sbellard async_ret = NOT_DONE; 124183f64091Sbellard qemu_aio_wait_start(); 1242ce1a14dcSpbrook acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors, 124383f64091Sbellard bdrv_rw_em_cb, &async_ret); 1244ce1a14dcSpbrook if (acb == NULL) { 124583f64091Sbellard qemu_aio_wait_end(); 1246ce1a14dcSpbrook return -1; 124783f64091Sbellard } 124883f64091Sbellard while (async_ret == NOT_DONE) { 124983f64091Sbellard qemu_aio_wait(); 125083f64091Sbellard } 125183f64091Sbellard qemu_aio_wait_end(); 125283f64091Sbellard return async_ret; 125383f64091Sbellard } 125483f64091Sbellard 125583f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num, 125683f64091Sbellard const uint8_t *buf, int nb_sectors) 125783f64091Sbellard { 1258ce1a14dcSpbrook int async_ret; 1259ce1a14dcSpbrook BlockDriverAIOCB *acb; 126083f64091Sbellard 126183f64091Sbellard async_ret = NOT_DONE; 126283f64091Sbellard qemu_aio_wait_start(); 1263ce1a14dcSpbrook acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors, 126483f64091Sbellard bdrv_rw_em_cb, &async_ret); 1265ce1a14dcSpbrook if (acb == NULL) { 126683f64091Sbellard qemu_aio_wait_end(); 1267ce1a14dcSpbrook return -1; 126883f64091Sbellard } 126983f64091Sbellard while (async_ret == NOT_DONE) { 127083f64091Sbellard qemu_aio_wait(); 127183f64091Sbellard } 127283f64091Sbellard qemu_aio_wait_end(); 127383f64091Sbellard return async_ret; 127483f64091Sbellard } 1275ea2384d3Sbellard 1276ea2384d3Sbellard void bdrv_init(void) 1277ea2384d3Sbellard { 1278ea2384d3Sbellard bdrv_register(&bdrv_raw); 127919cb3738Sbellard bdrv_register(&bdrv_host_device); 1280ea2384d3Sbellard #ifndef _WIN32 1281ea2384d3Sbellard bdrv_register(&bdrv_cow); 1282ea2384d3Sbellard #endif 1283ea2384d3Sbellard bdrv_register(&bdrv_qcow); 1284ea2384d3Sbellard bdrv_register(&bdrv_vmdk); 12853c56521bSbellard bdrv_register(&bdrv_cloop); 1286585d0ed9Sbellard bdrv_register(&bdrv_dmg); 1287a8753c34Sbellard bdrv_register(&bdrv_bochs); 12886a0f9e82Sbellard bdrv_register(&bdrv_vpc); 1289712e7874Sbellard bdrv_register(&bdrv_vvfat); 1290faea38e7Sbellard bdrv_register(&bdrv_qcow2); 12916ada7453Sths bdrv_register(&bdrv_parallels); 1292ea2384d3Sbellard } 1293ce1a14dcSpbrook 1294ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb, 1295ce1a14dcSpbrook void *opaque) 1296ce1a14dcSpbrook { 1297ce1a14dcSpbrook BlockDriver *drv; 1298ce1a14dcSpbrook BlockDriverAIOCB *acb; 1299ce1a14dcSpbrook 1300ce1a14dcSpbrook drv = bs->drv; 1301ce1a14dcSpbrook if (drv->free_aiocb) { 1302ce1a14dcSpbrook acb = drv->free_aiocb; 1303ce1a14dcSpbrook drv->free_aiocb = acb->next; 1304ce1a14dcSpbrook } else { 1305ce1a14dcSpbrook acb = qemu_mallocz(drv->aiocb_size); 1306ce1a14dcSpbrook if (!acb) 1307ce1a14dcSpbrook return NULL; 1308ce1a14dcSpbrook } 1309ce1a14dcSpbrook acb->bs = bs; 1310ce1a14dcSpbrook acb->cb = cb; 1311ce1a14dcSpbrook acb->opaque = opaque; 1312ce1a14dcSpbrook return acb; 1313ce1a14dcSpbrook } 1314ce1a14dcSpbrook 1315ce1a14dcSpbrook void qemu_aio_release(void *p) 1316ce1a14dcSpbrook { 1317ce1a14dcSpbrook BlockDriverAIOCB *acb = p; 1318ce1a14dcSpbrook BlockDriver *drv = acb->bs->drv; 1319ce1a14dcSpbrook acb->next = drv->free_aiocb; 1320ce1a14dcSpbrook drv->free_aiocb = acb; 1321ce1a14dcSpbrook } 132219cb3738Sbellard 132319cb3738Sbellard /**************************************************************/ 132419cb3738Sbellard /* removable device support */ 132519cb3738Sbellard 132619cb3738Sbellard /** 132719cb3738Sbellard * Return TRUE if the media is present 132819cb3738Sbellard */ 132919cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs) 133019cb3738Sbellard { 133119cb3738Sbellard BlockDriver *drv = bs->drv; 133219cb3738Sbellard int ret; 133319cb3738Sbellard if (!drv) 133419cb3738Sbellard return 0; 133519cb3738Sbellard if (!drv->bdrv_is_inserted) 133619cb3738Sbellard return 1; 133719cb3738Sbellard ret = drv->bdrv_is_inserted(bs); 133819cb3738Sbellard return ret; 133919cb3738Sbellard } 134019cb3738Sbellard 134119cb3738Sbellard /** 134219cb3738Sbellard * Return TRUE if the media changed since the last call to this 134319cb3738Sbellard * function. It is currently only used for floppy disks 134419cb3738Sbellard */ 134519cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs) 134619cb3738Sbellard { 134719cb3738Sbellard BlockDriver *drv = bs->drv; 134819cb3738Sbellard int ret; 134919cb3738Sbellard 135019cb3738Sbellard if (!drv || !drv->bdrv_media_changed) 135119cb3738Sbellard ret = -ENOTSUP; 135219cb3738Sbellard else 135319cb3738Sbellard ret = drv->bdrv_media_changed(bs); 135419cb3738Sbellard if (ret == -ENOTSUP) 135519cb3738Sbellard ret = bs->media_changed; 135619cb3738Sbellard bs->media_changed = 0; 135719cb3738Sbellard return ret; 135819cb3738Sbellard } 135919cb3738Sbellard 136019cb3738Sbellard /** 136119cb3738Sbellard * If eject_flag is TRUE, eject the media. Otherwise, close the tray 136219cb3738Sbellard */ 136319cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag) 136419cb3738Sbellard { 136519cb3738Sbellard BlockDriver *drv = bs->drv; 136619cb3738Sbellard int ret; 136719cb3738Sbellard 136819cb3738Sbellard if (!drv || !drv->bdrv_eject) { 136919cb3738Sbellard ret = -ENOTSUP; 137019cb3738Sbellard } else { 137119cb3738Sbellard ret = drv->bdrv_eject(bs, eject_flag); 137219cb3738Sbellard } 137319cb3738Sbellard if (ret == -ENOTSUP) { 137419cb3738Sbellard if (eject_flag) 137519cb3738Sbellard bdrv_close(bs); 137619cb3738Sbellard } 137719cb3738Sbellard } 137819cb3738Sbellard 137919cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs) 138019cb3738Sbellard { 138119cb3738Sbellard return bs->locked; 138219cb3738Sbellard } 138319cb3738Sbellard 138419cb3738Sbellard /** 138519cb3738Sbellard * Lock or unlock the media (if it is locked, the user won't be able 138619cb3738Sbellard * to eject it manually). 138719cb3738Sbellard */ 138819cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked) 138919cb3738Sbellard { 139019cb3738Sbellard BlockDriver *drv = bs->drv; 139119cb3738Sbellard 139219cb3738Sbellard bs->locked = locked; 139319cb3738Sbellard if (drv && drv->bdrv_set_locked) { 139419cb3738Sbellard drv->bdrv_set_locked(bs, locked); 139519cb3738Sbellard } 139619cb3738Sbellard } 1397