xref: /openbmc/qemu/block.c (revision 0badc1ee0e6ab8622b2f9b4ebc3c5ab96f0b58d0)
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;
194*0badc1eeSaurel32     char *tmpdir;
195d5249393Sbellard     /* XXX: race condition possible */
196*0badc1eeSaurel32     tmpdir = getenv("TMPDIR");
197*0badc1eeSaurel32     if (!tmpdir)
198*0badc1eeSaurel32         tmpdir = "/tmp";
199*0badc1eeSaurel32     snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
200ea2384d3Sbellard     fd = mkstemp(filename);
201ea2384d3Sbellard     close(fd);
202ea2384d3Sbellard }
203d5249393Sbellard #endif
204ea2384d3Sbellard 
20519cb3738Sbellard #ifdef _WIN32
206f45512feSbellard static int is_windows_drive_prefix(const char *filename)
207f45512feSbellard {
208f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
209f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
210f45512feSbellard             filename[1] == ':');
211f45512feSbellard }
212f45512feSbellard 
21319cb3738Sbellard static int is_windows_drive(const char *filename)
21419cb3738Sbellard {
215f45512feSbellard     if (is_windows_drive_prefix(filename) &&
216f45512feSbellard         filename[2] == '\0')
21719cb3738Sbellard         return 1;
21819cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
21919cb3738Sbellard         strstart(filename, "//./", NULL))
22019cb3738Sbellard         return 1;
22119cb3738Sbellard     return 0;
22219cb3738Sbellard }
22319cb3738Sbellard #endif
22419cb3738Sbellard 
22583f64091Sbellard static BlockDriver *find_protocol(const char *filename)
22683f64091Sbellard {
22783f64091Sbellard     BlockDriver *drv1;
22883f64091Sbellard     char protocol[128];
22983f64091Sbellard     int len;
23083f64091Sbellard     const char *p;
23119cb3738Sbellard 
23219cb3738Sbellard #ifdef _WIN32
233f45512feSbellard     if (is_windows_drive(filename) ||
234f45512feSbellard         is_windows_drive_prefix(filename))
23519cb3738Sbellard         return &bdrv_raw;
23619cb3738Sbellard #endif
23783f64091Sbellard     p = strchr(filename, ':');
23883f64091Sbellard     if (!p)
23983f64091Sbellard         return &bdrv_raw;
24083f64091Sbellard     len = p - filename;
24183f64091Sbellard     if (len > sizeof(protocol) - 1)
24283f64091Sbellard         len = sizeof(protocol) - 1;
24383f64091Sbellard     memcpy(protocol, filename, len);
24483f64091Sbellard     protocol[len] = '\0';
24583f64091Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
24683f64091Sbellard         if (drv1->protocol_name &&
24783f64091Sbellard             !strcmp(drv1->protocol_name, protocol))
24883f64091Sbellard             return drv1;
24983f64091Sbellard     }
25083f64091Sbellard     return NULL;
25183f64091Sbellard }
25283f64091Sbellard 
2537674e7bfSbellard /* XXX: force raw format if block or character device ? It would
2547674e7bfSbellard    simplify the BSD case */
255ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
256ea2384d3Sbellard {
25783f64091Sbellard     int ret, score, score_max;
258ea2384d3Sbellard     BlockDriver *drv1, *drv;
25983f64091Sbellard     uint8_t buf[2048];
26083f64091Sbellard     BlockDriverState *bs;
261ea2384d3Sbellard 
26219cb3738Sbellard     /* detect host devices. By convention, /dev/cdrom[N] is always
26319cb3738Sbellard        recognized as a host CDROM */
26419cb3738Sbellard     if (strstart(filename, "/dev/cdrom", NULL))
26519cb3738Sbellard         return &bdrv_host_device;
26619cb3738Sbellard #ifdef _WIN32
26719cb3738Sbellard     if (is_windows_drive(filename))
26819cb3738Sbellard         return &bdrv_host_device;
26919cb3738Sbellard #else
27019cb3738Sbellard     {
27119cb3738Sbellard         struct stat st;
27219cb3738Sbellard         if (stat(filename, &st) >= 0 &&
27319cb3738Sbellard             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
27419cb3738Sbellard             return &bdrv_host_device;
27519cb3738Sbellard         }
27619cb3738Sbellard     }
27719cb3738Sbellard #endif
27819cb3738Sbellard 
27983f64091Sbellard     drv = find_protocol(filename);
28019cb3738Sbellard     /* no need to test disk image formats for vvfat */
28183f64091Sbellard     if (drv == &bdrv_vvfat)
28283f64091Sbellard         return drv;
28383f64091Sbellard 
28483f64091Sbellard     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
28583f64091Sbellard     if (ret < 0)
2867674e7bfSbellard         return NULL;
28783f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
28883f64091Sbellard     bdrv_delete(bs);
289ea2384d3Sbellard     if (ret < 0) {
290ea2384d3Sbellard         return NULL;
291ea2384d3Sbellard     }
292ea2384d3Sbellard 
293ea2384d3Sbellard     score_max = 0;
294ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
29583f64091Sbellard         if (drv1->bdrv_probe) {
296ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
297ea2384d3Sbellard             if (score > score_max) {
298ea2384d3Sbellard                 score_max = score;
299ea2384d3Sbellard                 drv = drv1;
300ea2384d3Sbellard             }
301ea2384d3Sbellard         }
30283f64091Sbellard     }
303ea2384d3Sbellard     return drv;
304ea2384d3Sbellard }
305ea2384d3Sbellard 
30683f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
307b338082bSbellard {
30883f64091Sbellard     BlockDriverState *bs;
30983f64091Sbellard     int ret;
3103b0d4f61Sbellard 
31183f64091Sbellard     bs = bdrv_new("");
31283f64091Sbellard     if (!bs)
31383f64091Sbellard         return -ENOMEM;
31483f64091Sbellard     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
31583f64091Sbellard     if (ret < 0) {
31683f64091Sbellard         bdrv_delete(bs);
31783f64091Sbellard         return ret;
3183b0d4f61Sbellard     }
31983f64091Sbellard     *pbs = bs;
32083f64091Sbellard     return 0;
3213b0d4f61Sbellard }
3223b0d4f61Sbellard 
32383f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
32483f64091Sbellard {
32583f64091Sbellard     return bdrv_open2(bs, filename, flags, NULL);
326ea2384d3Sbellard }
327ea2384d3Sbellard 
32883f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
329ea2384d3Sbellard                BlockDriver *drv)
330ea2384d3Sbellard {
33183f64091Sbellard     int ret, open_flags;
332eb5c851fSths     char tmp_filename[PATH_MAX];
333eb5c851fSths     char backing_filename[PATH_MAX];
334fc01f7e7Sbellard 
3350849bf08Sbellard     bs->read_only = 0;
336ea2384d3Sbellard     bs->is_temporary = 0;
337ea2384d3Sbellard     bs->encrypted = 0;
33833e3963eSbellard 
33983f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
340ea2384d3Sbellard         BlockDriverState *bs1;
341ea2384d3Sbellard         int64_t total_size;
34233e3963eSbellard 
343ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
344ea2384d3Sbellard            instead of opening 'filename' directly */
345ea2384d3Sbellard 
346ea2384d3Sbellard         /* if there is a backing file, use it */
347ea2384d3Sbellard         bs1 = bdrv_new("");
348ea2384d3Sbellard         if (!bs1) {
34983f64091Sbellard             return -ENOMEM;
350ea2384d3Sbellard         }
351ea2384d3Sbellard         if (bdrv_open(bs1, filename, 0) < 0) {
352ea2384d3Sbellard             bdrv_delete(bs1);
353ea2384d3Sbellard             return -1;
354ea2384d3Sbellard         }
35583f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
356ea2384d3Sbellard         bdrv_delete(bs1);
357ea2384d3Sbellard 
358ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
359a817d936Sbellard         realpath(filename, backing_filename);
360d15a771dSbellard         if (bdrv_create(&bdrv_qcow2, tmp_filename,
361a817d936Sbellard                         total_size, backing_filename, 0) < 0) {
362ea2384d3Sbellard             return -1;
363ea2384d3Sbellard         }
364ea2384d3Sbellard         filename = tmp_filename;
365ea2384d3Sbellard         bs->is_temporary = 1;
366ea2384d3Sbellard     }
367ea2384d3Sbellard 
368ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
36983f64091Sbellard     if (flags & BDRV_O_FILE) {
37083f64091Sbellard         drv = find_protocol(filename);
37183f64091Sbellard         if (!drv)
37283f64091Sbellard             return -ENOENT;
37383f64091Sbellard     } else {
374ea2384d3Sbellard         if (!drv) {
375ea2384d3Sbellard             drv = find_image_format(filename);
376ea2384d3Sbellard             if (!drv)
377ea2384d3Sbellard                 return -1;
378ea2384d3Sbellard         }
37983f64091Sbellard     }
380ea2384d3Sbellard     bs->drv = drv;
381ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
382ea2384d3Sbellard     if (bs->opaque == NULL && drv->instance_size > 0)
383ea2384d3Sbellard         return -1;
38483f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
38583f64091Sbellard        RDONLY as fallback */
38683f64091Sbellard     if (!(flags & BDRV_O_FILE))
38733f00271Sbalrog         open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT);
38883f64091Sbellard     else
38983f64091Sbellard         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
39083f64091Sbellard     ret = drv->bdrv_open(bs, filename, open_flags);
39183f64091Sbellard     if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
39283f64091Sbellard         ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
39383f64091Sbellard         bs->read_only = 1;
39483f64091Sbellard     }
395ea2384d3Sbellard     if (ret < 0) {
396ea2384d3Sbellard         qemu_free(bs->opaque);
3976b21b973Sbellard         bs->opaque = NULL;
3986b21b973Sbellard         bs->drv = NULL;
39983f64091Sbellard         return ret;
400ea2384d3Sbellard     }
401d15a771dSbellard     if (drv->bdrv_getlength) {
402d15a771dSbellard         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
403d15a771dSbellard     }
404ea2384d3Sbellard #ifndef _WIN32
405ea2384d3Sbellard     if (bs->is_temporary) {
406ea2384d3Sbellard         unlink(filename);
40733e3963eSbellard     }
40867b915a5Sbellard #endif
40983f64091Sbellard     if (bs->backing_file[0] != '\0') {
410ea2384d3Sbellard         /* if there is a backing file, use it */
411ea2384d3Sbellard         bs->backing_hd = bdrv_new("");
412ea2384d3Sbellard         if (!bs->backing_hd) {
413ea2384d3Sbellard         fail:
414ea2384d3Sbellard             bdrv_close(bs);
4156b21b973Sbellard             return -ENOMEM;
416ea2384d3Sbellard         }
41783f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
41883f64091Sbellard                      filename, bs->backing_file);
41983f64091Sbellard         if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
420ea2384d3Sbellard             goto fail;
421ea2384d3Sbellard     }
42233e3963eSbellard 
423b338082bSbellard     /* call the change callback */
42419cb3738Sbellard     bs->media_changed = 1;
425b338082bSbellard     if (bs->change_cb)
426b338082bSbellard         bs->change_cb(bs->change_opaque);
427b338082bSbellard 
428b338082bSbellard     return 0;
429fc01f7e7Sbellard }
430fc01f7e7Sbellard 
431fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
432fc01f7e7Sbellard {
43319cb3738Sbellard     if (bs->drv) {
434ea2384d3Sbellard         if (bs->backing_hd)
435ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
436ea2384d3Sbellard         bs->drv->bdrv_close(bs);
437ea2384d3Sbellard         qemu_free(bs->opaque);
438ea2384d3Sbellard #ifdef _WIN32
439ea2384d3Sbellard         if (bs->is_temporary) {
440ea2384d3Sbellard             unlink(bs->filename);
441ea2384d3Sbellard         }
44267b915a5Sbellard #endif
443ea2384d3Sbellard         bs->opaque = NULL;
444ea2384d3Sbellard         bs->drv = NULL;
445b338082bSbellard 
446b338082bSbellard         /* call the change callback */
44719cb3738Sbellard         bs->media_changed = 1;
448b338082bSbellard         if (bs->change_cb)
449b338082bSbellard             bs->change_cb(bs->change_opaque);
450b338082bSbellard     }
451b338082bSbellard }
452b338082bSbellard 
453b338082bSbellard void bdrv_delete(BlockDriverState *bs)
454b338082bSbellard {
455ea2384d3Sbellard     /* XXX: remove the driver list */
456b338082bSbellard     bdrv_close(bs);
457b338082bSbellard     qemu_free(bs);
458fc01f7e7Sbellard }
459fc01f7e7Sbellard 
46033e3963eSbellard /* commit COW file into the raw image */
46133e3963eSbellard int bdrv_commit(BlockDriverState *bs)
46233e3963eSbellard {
46319cb3738Sbellard     BlockDriver *drv = bs->drv;
46483f64091Sbellard     int64_t i, total_sectors;
465ea2384d3Sbellard     int n, j;
466ea2384d3Sbellard     unsigned char sector[512];
46733e3963eSbellard 
46819cb3738Sbellard     if (!drv)
46919cb3738Sbellard         return -ENOMEDIUM;
47033e3963eSbellard 
47133e3963eSbellard     if (bs->read_only) {
472ea2384d3Sbellard 	return -EACCES;
47333e3963eSbellard     }
47433e3963eSbellard 
475ea2384d3Sbellard     if (!bs->backing_hd) {
476ea2384d3Sbellard 	return -ENOTSUP;
477ea2384d3Sbellard     }
478ea2384d3Sbellard 
47983f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
48083f64091Sbellard     for (i = 0; i < total_sectors;) {
48119cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
482ea2384d3Sbellard             for(j = 0; j < n; j++) {
48333e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
484ea2384d3Sbellard                     return -EIO;
48533e3963eSbellard                 }
48633e3963eSbellard 
487ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
488ea2384d3Sbellard                     return -EIO;
48933e3963eSbellard                 }
490ea2384d3Sbellard                 i++;
491ea2384d3Sbellard 	    }
492ea2384d3Sbellard 	} else {
493ea2384d3Sbellard             i += n;
49433e3963eSbellard         }
49533e3963eSbellard     }
49695389c86Sbellard 
49719cb3738Sbellard     if (drv->bdrv_make_empty)
49819cb3738Sbellard 	return drv->bdrv_make_empty(bs);
49995389c86Sbellard 
50033e3963eSbellard     return 0;
50133e3963eSbellard }
50233e3963eSbellard 
50319cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
504fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
505fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
506fc01f7e7Sbellard {
507ea2384d3Sbellard     BlockDriver *drv = bs->drv;
508fc01f7e7Sbellard 
50919cb3738Sbellard     if (!drv)
51019cb3738Sbellard         return -ENOMEDIUM;
511b338082bSbellard 
51283f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
513cf98951bSbellard             memcpy(buf, bs->boot_sector_data, 512);
51483f64091Sbellard         sector_num++;
51583f64091Sbellard         nb_sectors--;
51683f64091Sbellard         buf += 512;
51783f64091Sbellard         if (nb_sectors == 0)
518fc01f7e7Sbellard             return 0;
519fc01f7e7Sbellard     }
52083f64091Sbellard     if (drv->bdrv_pread) {
52183f64091Sbellard         int ret, len;
52283f64091Sbellard         len = nb_sectors * 512;
52383f64091Sbellard         ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
52483f64091Sbellard         if (ret < 0)
52583f64091Sbellard             return ret;
52683f64091Sbellard         else if (ret != len)
52719cb3738Sbellard             return -EINVAL;
528a36e69ddSths         else {
529a36e69ddSths 	    bs->rd_bytes += (unsigned) len;
530a36e69ddSths 	    bs->rd_ops ++;
53183f64091Sbellard             return 0;
532a36e69ddSths 	}
53383f64091Sbellard     } else {
53483f64091Sbellard         return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
53583f64091Sbellard     }
53683f64091Sbellard }
537fc01f7e7Sbellard 
53819cb3738Sbellard /* Return < 0 if error. Important errors are:
53919cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
54019cb3738Sbellard   -ENOMEDIUM   No media inserted.
54119cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
54219cb3738Sbellard   -EACCES      Trying to write a read-only device
54319cb3738Sbellard */
544fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
545fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
546fc01f7e7Sbellard {
54783f64091Sbellard     BlockDriver *drv = bs->drv;
54819cb3738Sbellard     if (!bs->drv)
54919cb3738Sbellard         return -ENOMEDIUM;
5500849bf08Sbellard     if (bs->read_only)
55119cb3738Sbellard         return -EACCES;
55279639d42Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
55379639d42Sbellard         memcpy(bs->boot_sector_data, buf, 512);
55479639d42Sbellard     }
55583f64091Sbellard     if (drv->bdrv_pwrite) {
55683f64091Sbellard         int ret, len;
55783f64091Sbellard         len = nb_sectors * 512;
55883f64091Sbellard         ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
55983f64091Sbellard         if (ret < 0)
56083f64091Sbellard             return ret;
56183f64091Sbellard         else if (ret != len)
56283f64091Sbellard             return -EIO;
563a36e69ddSths         else {
564a36e69ddSths 	    bs->wr_bytes += (unsigned) len;
565a36e69ddSths 	    bs->wr_ops ++;
56683f64091Sbellard             return 0;
567a36e69ddSths 	}
56883f64091Sbellard     } else {
56983f64091Sbellard         return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
57083f64091Sbellard     }
57183f64091Sbellard }
57283f64091Sbellard 
57383f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
574faea38e7Sbellard                          uint8_t *buf, int count1)
57583f64091Sbellard {
57683f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
57783f64091Sbellard     int len, nb_sectors, count;
57883f64091Sbellard     int64_t sector_num;
57983f64091Sbellard 
58083f64091Sbellard     count = count1;
58183f64091Sbellard     /* first read to align to sector start */
58283f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
58383f64091Sbellard     if (len > count)
58483f64091Sbellard         len = count;
58583f64091Sbellard     sector_num = offset >> SECTOR_BITS;
58683f64091Sbellard     if (len > 0) {
58783f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
58883f64091Sbellard             return -EIO;
58983f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
59083f64091Sbellard         count -= len;
59183f64091Sbellard         if (count == 0)
59283f64091Sbellard             return count1;
59383f64091Sbellard         sector_num++;
59483f64091Sbellard         buf += len;
59583f64091Sbellard     }
59683f64091Sbellard 
59783f64091Sbellard     /* read the sectors "in place" */
59883f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
59983f64091Sbellard     if (nb_sectors > 0) {
60083f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
60183f64091Sbellard             return -EIO;
60283f64091Sbellard         sector_num += nb_sectors;
60383f64091Sbellard         len = nb_sectors << SECTOR_BITS;
60483f64091Sbellard         buf += len;
60583f64091Sbellard         count -= len;
60683f64091Sbellard     }
60783f64091Sbellard 
60883f64091Sbellard     /* add data from the last sector */
60983f64091Sbellard     if (count > 0) {
61083f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
61183f64091Sbellard             return -EIO;
61283f64091Sbellard         memcpy(buf, tmp_buf, count);
61383f64091Sbellard     }
61483f64091Sbellard     return count1;
61583f64091Sbellard }
61683f64091Sbellard 
61783f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
618faea38e7Sbellard                           const uint8_t *buf, int count1)
61983f64091Sbellard {
62083f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
62183f64091Sbellard     int len, nb_sectors, count;
62283f64091Sbellard     int64_t sector_num;
62383f64091Sbellard 
62483f64091Sbellard     count = count1;
62583f64091Sbellard     /* first write to align to sector start */
62683f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
62783f64091Sbellard     if (len > count)
62883f64091Sbellard         len = count;
62983f64091Sbellard     sector_num = offset >> SECTOR_BITS;
63083f64091Sbellard     if (len > 0) {
63183f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
63283f64091Sbellard             return -EIO;
63383f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
63483f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
63583f64091Sbellard             return -EIO;
63683f64091Sbellard         count -= len;
63783f64091Sbellard         if (count == 0)
63883f64091Sbellard             return count1;
63983f64091Sbellard         sector_num++;
64083f64091Sbellard         buf += len;
64183f64091Sbellard     }
64283f64091Sbellard 
64383f64091Sbellard     /* write the sectors "in place" */
64483f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
64583f64091Sbellard     if (nb_sectors > 0) {
64683f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
64783f64091Sbellard             return -EIO;
64883f64091Sbellard         sector_num += nb_sectors;
64983f64091Sbellard         len = nb_sectors << SECTOR_BITS;
65083f64091Sbellard         buf += len;
65183f64091Sbellard         count -= len;
65283f64091Sbellard     }
65383f64091Sbellard 
65483f64091Sbellard     /* add data from the last sector */
65583f64091Sbellard     if (count > 0) {
65683f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
65783f64091Sbellard             return -EIO;
65883f64091Sbellard         memcpy(tmp_buf, buf, count);
65983f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
66083f64091Sbellard             return -EIO;
66183f64091Sbellard     }
66283f64091Sbellard     return count1;
66383f64091Sbellard }
66483f64091Sbellard 
66583f64091Sbellard /**
66683f64091Sbellard  * Read with byte offsets (needed only for file protocols)
66783f64091Sbellard  */
66883f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset,
66983f64091Sbellard                void *buf1, int count1)
67083f64091Sbellard {
67183f64091Sbellard     BlockDriver *drv = bs->drv;
67283f64091Sbellard 
67383f64091Sbellard     if (!drv)
67419cb3738Sbellard         return -ENOMEDIUM;
67583f64091Sbellard     if (!drv->bdrv_pread)
676faea38e7Sbellard         return bdrv_pread_em(bs, offset, buf1, count1);
67783f64091Sbellard     return drv->bdrv_pread(bs, offset, buf1, count1);
67883f64091Sbellard }
67983f64091Sbellard 
68083f64091Sbellard /**
68183f64091Sbellard  * Write with byte offsets (needed only for file protocols)
68283f64091Sbellard  */
68383f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
68483f64091Sbellard                 const void *buf1, int count1)
68583f64091Sbellard {
68683f64091Sbellard     BlockDriver *drv = bs->drv;
68783f64091Sbellard 
68883f64091Sbellard     if (!drv)
68919cb3738Sbellard         return -ENOMEDIUM;
69083f64091Sbellard     if (!drv->bdrv_pwrite)
691faea38e7Sbellard         return bdrv_pwrite_em(bs, offset, buf1, count1);
69283f64091Sbellard     return drv->bdrv_pwrite(bs, offset, buf1, count1);
69383f64091Sbellard }
69483f64091Sbellard 
69583f64091Sbellard /**
69683f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
69783f64091Sbellard  */
69883f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
69983f64091Sbellard {
70083f64091Sbellard     BlockDriver *drv = bs->drv;
70183f64091Sbellard     if (!drv)
70219cb3738Sbellard         return -ENOMEDIUM;
70383f64091Sbellard     if (!drv->bdrv_truncate)
70483f64091Sbellard         return -ENOTSUP;
70583f64091Sbellard     return drv->bdrv_truncate(bs, offset);
70683f64091Sbellard }
70783f64091Sbellard 
70883f64091Sbellard /**
70983f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
71083f64091Sbellard  */
71183f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
71283f64091Sbellard {
71383f64091Sbellard     BlockDriver *drv = bs->drv;
71483f64091Sbellard     if (!drv)
71519cb3738Sbellard         return -ENOMEDIUM;
71683f64091Sbellard     if (!drv->bdrv_getlength) {
71783f64091Sbellard         /* legacy mode */
71883f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
71983f64091Sbellard     }
72083f64091Sbellard     return drv->bdrv_getlength(bs);
721fc01f7e7Sbellard }
722fc01f7e7Sbellard 
72319cb3738Sbellard /* return 0 as number of sectors if no device present or error */
72496b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
725fc01f7e7Sbellard {
72619cb3738Sbellard     int64_t length;
72719cb3738Sbellard     length = bdrv_getlength(bs);
72819cb3738Sbellard     if (length < 0)
72919cb3738Sbellard         length = 0;
73019cb3738Sbellard     else
73119cb3738Sbellard         length = length >> SECTOR_BITS;
73219cb3738Sbellard     *nb_sectors_ptr = length;
733fc01f7e7Sbellard }
734cf98951bSbellard 
735cf98951bSbellard /* force a given boot sector. */
736cf98951bSbellard void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
737cf98951bSbellard {
738cf98951bSbellard     bs->boot_sector_enabled = 1;
739cf98951bSbellard     if (size > 512)
740cf98951bSbellard         size = 512;
741cf98951bSbellard     memcpy(bs->boot_sector_data, data, size);
742cf98951bSbellard     memset(bs->boot_sector_data + size, 0, 512 - size);
743cf98951bSbellard }
744b338082bSbellard 
745b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
746b338082bSbellard                             int cyls, int heads, int secs)
747b338082bSbellard {
748b338082bSbellard     bs->cyls = cyls;
749b338082bSbellard     bs->heads = heads;
750b338082bSbellard     bs->secs = secs;
751b338082bSbellard }
752b338082bSbellard 
753b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
754b338082bSbellard {
755b338082bSbellard     bs->type = type;
756b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
757b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
758b338082bSbellard }
759b338082bSbellard 
76046d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
76146d4767dSbellard {
76246d4767dSbellard     bs->translation = translation;
76346d4767dSbellard }
76446d4767dSbellard 
765b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
766b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
767b338082bSbellard {
768b338082bSbellard     *pcyls = bs->cyls;
769b338082bSbellard     *pheads = bs->heads;
770b338082bSbellard     *psecs = bs->secs;
771b338082bSbellard }
772b338082bSbellard 
773b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
774b338082bSbellard {
775b338082bSbellard     return bs->type;
776b338082bSbellard }
777b338082bSbellard 
77846d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
77946d4767dSbellard {
78046d4767dSbellard     return bs->translation;
78146d4767dSbellard }
78246d4767dSbellard 
783b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
784b338082bSbellard {
785b338082bSbellard     return bs->removable;
786b338082bSbellard }
787b338082bSbellard 
788b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
789b338082bSbellard {
790b338082bSbellard     return bs->read_only;
791b338082bSbellard }
792b338082bSbellard 
793985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
794985a03b0Sths {
795985a03b0Sths     return bs->sg;
796985a03b0Sths }
797985a03b0Sths 
79819cb3738Sbellard /* XXX: no longer used */
799b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
800b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
801b338082bSbellard {
802b338082bSbellard     bs->change_cb = change_cb;
803b338082bSbellard     bs->change_opaque = opaque;
804b338082bSbellard }
805b338082bSbellard 
806ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
807ea2384d3Sbellard {
808ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
809ea2384d3Sbellard         return 1;
810ea2384d3Sbellard     return bs->encrypted;
811ea2384d3Sbellard }
812ea2384d3Sbellard 
813ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
814ea2384d3Sbellard {
815ea2384d3Sbellard     int ret;
816ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
817ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
818ea2384d3Sbellard         if (ret < 0)
819ea2384d3Sbellard             return ret;
820ea2384d3Sbellard         if (!bs->encrypted)
821ea2384d3Sbellard             return 0;
822ea2384d3Sbellard     }
823ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
824ea2384d3Sbellard         return -1;
825ea2384d3Sbellard     return bs->drv->bdrv_set_key(bs, key);
826ea2384d3Sbellard }
827ea2384d3Sbellard 
828ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
829ea2384d3Sbellard {
83019cb3738Sbellard     if (!bs->drv) {
831ea2384d3Sbellard         buf[0] = '\0';
832ea2384d3Sbellard     } else {
833ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
834ea2384d3Sbellard     }
835ea2384d3Sbellard }
836ea2384d3Sbellard 
837ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
838ea2384d3Sbellard                          void *opaque)
839ea2384d3Sbellard {
840ea2384d3Sbellard     BlockDriver *drv;
841ea2384d3Sbellard 
842ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
843ea2384d3Sbellard         it(opaque, drv->format_name);
844ea2384d3Sbellard     }
845ea2384d3Sbellard }
846ea2384d3Sbellard 
847b338082bSbellard BlockDriverState *bdrv_find(const char *name)
848b338082bSbellard {
849b338082bSbellard     BlockDriverState *bs;
850b338082bSbellard 
851b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
852b338082bSbellard         if (!strcmp(name, bs->device_name))
853b338082bSbellard             return bs;
854b338082bSbellard     }
855b338082bSbellard     return NULL;
856b338082bSbellard }
857b338082bSbellard 
85881d0912dSbellard void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
85981d0912dSbellard {
86081d0912dSbellard     BlockDriverState *bs;
86181d0912dSbellard 
86281d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
86381d0912dSbellard         it(opaque, bs->device_name);
86481d0912dSbellard     }
86581d0912dSbellard }
86681d0912dSbellard 
867ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
868ea2384d3Sbellard {
869ea2384d3Sbellard     return bs->device_name;
870ea2384d3Sbellard }
871ea2384d3Sbellard 
8727a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
8737a6cba61Spbrook {
8747a6cba61Spbrook     if (bs->drv->bdrv_flush)
8757a6cba61Spbrook         bs->drv->bdrv_flush(bs);
8767a6cba61Spbrook     if (bs->backing_hd)
8777a6cba61Spbrook         bdrv_flush(bs->backing_hd);
8787a6cba61Spbrook }
8797a6cba61Spbrook 
880faf07963Spbrook #ifndef QEMU_IMG
881b338082bSbellard void bdrv_info(void)
882b338082bSbellard {
883b338082bSbellard     BlockDriverState *bs;
884b338082bSbellard 
885b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
886b338082bSbellard         term_printf("%s:", bs->device_name);
887b338082bSbellard         term_printf(" type=");
888b338082bSbellard         switch(bs->type) {
889b338082bSbellard         case BDRV_TYPE_HD:
890b338082bSbellard             term_printf("hd");
891b338082bSbellard             break;
892b338082bSbellard         case BDRV_TYPE_CDROM:
893b338082bSbellard             term_printf("cdrom");
894b338082bSbellard             break;
895b338082bSbellard         case BDRV_TYPE_FLOPPY:
896b338082bSbellard             term_printf("floppy");
897b338082bSbellard             break;
898b338082bSbellard         }
899b338082bSbellard         term_printf(" removable=%d", bs->removable);
900b338082bSbellard         if (bs->removable) {
901b338082bSbellard             term_printf(" locked=%d", bs->locked);
902b338082bSbellard         }
90319cb3738Sbellard         if (bs->drv) {
904fef30743Sths             term_printf(" file=");
905fef30743Sths 	    term_print_filename(bs->filename);
906fef30743Sths             if (bs->backing_file[0] != '\0') {
907fef30743Sths                 term_printf(" backing_file=");
908fef30743Sths 		term_print_filename(bs->backing_file);
909fef30743Sths 	    }
910b338082bSbellard             term_printf(" ro=%d", bs->read_only);
911ea2384d3Sbellard             term_printf(" drv=%s", bs->drv->format_name);
912ea2384d3Sbellard             if (bs->encrypted)
913ea2384d3Sbellard                 term_printf(" encrypted");
914b338082bSbellard         } else {
915b338082bSbellard             term_printf(" [not inserted]");
916b338082bSbellard         }
917b338082bSbellard         term_printf("\n");
918b338082bSbellard     }
919b338082bSbellard }
920a36e69ddSths 
921a36e69ddSths /* The "info blockstats" command. */
922a36e69ddSths void bdrv_info_stats (void)
923a36e69ddSths {
924a36e69ddSths     BlockDriverState *bs;
925a36e69ddSths 
926a36e69ddSths     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
927a36e69ddSths 	term_printf ("%s:"
928a36e69ddSths 		     " rd_bytes=%" PRIu64
929a36e69ddSths 		     " wr_bytes=%" PRIu64
930a36e69ddSths 		     " rd_operations=%" PRIu64
931a36e69ddSths 		     " wr_operations=%" PRIu64
932a36e69ddSths 		     "\n",
933a36e69ddSths 		     bs->device_name,
934a36e69ddSths 		     bs->rd_bytes, bs->wr_bytes,
935a36e69ddSths 		     bs->rd_ops, bs->wr_ops);
936a36e69ddSths     }
937a36e69ddSths }
938faf07963Spbrook #endif
939ea2384d3Sbellard 
94083f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
94183f64091Sbellard                                char *filename, int filename_size)
94283f64091Sbellard {
94383f64091Sbellard     if (!bs->backing_hd) {
94483f64091Sbellard         pstrcpy(filename, filename_size, "");
94583f64091Sbellard     } else {
94683f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
94783f64091Sbellard     }
94883f64091Sbellard }
94983f64091Sbellard 
950faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
951faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
952faea38e7Sbellard {
953faea38e7Sbellard     BlockDriver *drv = bs->drv;
954faea38e7Sbellard     if (!drv)
95519cb3738Sbellard         return -ENOMEDIUM;
956faea38e7Sbellard     if (!drv->bdrv_write_compressed)
957faea38e7Sbellard         return -ENOTSUP;
958faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
959faea38e7Sbellard }
960faea38e7Sbellard 
961faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
962faea38e7Sbellard {
963faea38e7Sbellard     BlockDriver *drv = bs->drv;
964faea38e7Sbellard     if (!drv)
96519cb3738Sbellard         return -ENOMEDIUM;
966faea38e7Sbellard     if (!drv->bdrv_get_info)
967faea38e7Sbellard         return -ENOTSUP;
968faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
969faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
970faea38e7Sbellard }
971faea38e7Sbellard 
972faea38e7Sbellard /**************************************************************/
973faea38e7Sbellard /* handling of snapshots */
974faea38e7Sbellard 
975faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
976faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
977faea38e7Sbellard {
978faea38e7Sbellard     BlockDriver *drv = bs->drv;
979faea38e7Sbellard     if (!drv)
98019cb3738Sbellard         return -ENOMEDIUM;
981faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
982faea38e7Sbellard         return -ENOTSUP;
983faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
984faea38e7Sbellard }
985faea38e7Sbellard 
986faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
987faea38e7Sbellard                        const char *snapshot_id)
988faea38e7Sbellard {
989faea38e7Sbellard     BlockDriver *drv = bs->drv;
990faea38e7Sbellard     if (!drv)
99119cb3738Sbellard         return -ENOMEDIUM;
992faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
993faea38e7Sbellard         return -ENOTSUP;
994faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
995faea38e7Sbellard }
996faea38e7Sbellard 
997faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
998faea38e7Sbellard {
999faea38e7Sbellard     BlockDriver *drv = bs->drv;
1000faea38e7Sbellard     if (!drv)
100119cb3738Sbellard         return -ENOMEDIUM;
1002faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
1003faea38e7Sbellard         return -ENOTSUP;
1004faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1005faea38e7Sbellard }
1006faea38e7Sbellard 
1007faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1008faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1009faea38e7Sbellard {
1010faea38e7Sbellard     BlockDriver *drv = bs->drv;
1011faea38e7Sbellard     if (!drv)
101219cb3738Sbellard         return -ENOMEDIUM;
1013faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
1014faea38e7Sbellard         return -ENOTSUP;
1015faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
1016faea38e7Sbellard }
1017faea38e7Sbellard 
1018faea38e7Sbellard #define NB_SUFFIXES 4
1019faea38e7Sbellard 
1020faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1021faea38e7Sbellard {
1022faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1023faea38e7Sbellard     int64_t base;
1024faea38e7Sbellard     int i;
1025faea38e7Sbellard 
1026faea38e7Sbellard     if (size <= 999) {
1027faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1028faea38e7Sbellard     } else {
1029faea38e7Sbellard         base = 1024;
1030faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1031faea38e7Sbellard             if (size < (10 * base)) {
1032faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1033faea38e7Sbellard                          (double)size / base,
1034faea38e7Sbellard                          suffixes[i]);
1035faea38e7Sbellard                 break;
1036faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1037faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1038faea38e7Sbellard                          ((size + (base >> 1)) / base),
1039faea38e7Sbellard                          suffixes[i]);
1040faea38e7Sbellard                 break;
1041faea38e7Sbellard             }
1042faea38e7Sbellard             base = base * 1024;
1043faea38e7Sbellard         }
1044faea38e7Sbellard     }
1045faea38e7Sbellard     return buf;
1046faea38e7Sbellard }
1047faea38e7Sbellard 
1048faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1049faea38e7Sbellard {
1050faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
10513b9f94e1Sbellard #ifdef _WIN32
10523b9f94e1Sbellard     struct tm *ptm;
10533b9f94e1Sbellard #else
1054faea38e7Sbellard     struct tm tm;
10553b9f94e1Sbellard #endif
1056faea38e7Sbellard     time_t ti;
1057faea38e7Sbellard     int64_t secs;
1058faea38e7Sbellard 
1059faea38e7Sbellard     if (!sn) {
1060faea38e7Sbellard         snprintf(buf, buf_size,
1061faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1062faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1063faea38e7Sbellard     } else {
1064faea38e7Sbellard         ti = sn->date_sec;
10653b9f94e1Sbellard #ifdef _WIN32
10663b9f94e1Sbellard         ptm = localtime(&ti);
10673b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
10683b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
10693b9f94e1Sbellard #else
1070faea38e7Sbellard         localtime_r(&ti, &tm);
1071faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1072faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
10733b9f94e1Sbellard #endif
1074faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1075faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1076faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1077faea38e7Sbellard                  (int)(secs / 3600),
1078faea38e7Sbellard                  (int)((secs / 60) % 60),
1079faea38e7Sbellard                  (int)(secs % 60),
1080faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1081faea38e7Sbellard         snprintf(buf, buf_size,
1082faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1083faea38e7Sbellard                  sn->id_str, sn->name,
1084faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1085faea38e7Sbellard                  date_buf,
1086faea38e7Sbellard                  clock_buf);
1087faea38e7Sbellard     }
1088faea38e7Sbellard     return buf;
1089faea38e7Sbellard }
1090faea38e7Sbellard 
109183f64091Sbellard 
1092ea2384d3Sbellard /**************************************************************/
109383f64091Sbellard /* async I/Os */
1094ea2384d3Sbellard 
1095ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
109683f64091Sbellard                                 uint8_t *buf, int nb_sectors,
109783f64091Sbellard                                 BlockDriverCompletionFunc *cb, void *opaque)
1098ea2384d3Sbellard {
109983f64091Sbellard     BlockDriver *drv = bs->drv;
1100a36e69ddSths     BlockDriverAIOCB *ret;
1101ea2384d3Sbellard 
110219cb3738Sbellard     if (!drv)
1103ce1a14dcSpbrook         return NULL;
110483f64091Sbellard 
110583f64091Sbellard     /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
110683f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
110783f64091Sbellard         memcpy(buf, bs->boot_sector_data, 512);
110883f64091Sbellard         sector_num++;
110983f64091Sbellard         nb_sectors--;
111083f64091Sbellard         buf += 512;
1111ea2384d3Sbellard     }
111283f64091Sbellard 
1113a36e69ddSths     ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1114a36e69ddSths 
1115a36e69ddSths     if (ret) {
1116a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1117a36e69ddSths 	bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1118a36e69ddSths 	bs->rd_ops ++;
1119a36e69ddSths     }
1120a36e69ddSths 
1121a36e69ddSths     return ret;
112283f64091Sbellard }
112383f64091Sbellard 
1124ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
112583f64091Sbellard                                  const uint8_t *buf, int nb_sectors,
112683f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
11277674e7bfSbellard {
112883f64091Sbellard     BlockDriver *drv = bs->drv;
1129a36e69ddSths     BlockDriverAIOCB *ret;
113083f64091Sbellard 
113119cb3738Sbellard     if (!drv)
1132ce1a14dcSpbrook         return NULL;
113383f64091Sbellard     if (bs->read_only)
1134ce1a14dcSpbrook         return NULL;
113583f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
113683f64091Sbellard         memcpy(bs->boot_sector_data, buf, 512);
11377674e7bfSbellard     }
113883f64091Sbellard 
1139a36e69ddSths     ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1140a36e69ddSths 
1141a36e69ddSths     if (ret) {
1142a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1143a36e69ddSths 	bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1144a36e69ddSths 	bs->wr_ops ++;
1145a36e69ddSths     }
1146a36e69ddSths 
1147a36e69ddSths     return ret;
114883f64091Sbellard }
114983f64091Sbellard 
115083f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
115183f64091Sbellard {
1152ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
115383f64091Sbellard 
115483f64091Sbellard     drv->bdrv_aio_cancel(acb);
115583f64091Sbellard }
115683f64091Sbellard 
115783f64091Sbellard 
115883f64091Sbellard /**************************************************************/
115983f64091Sbellard /* async block device emulation */
116083f64091Sbellard 
1161faf07963Spbrook #ifdef QEMU_IMG
1162ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1163ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1164ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1165ea2384d3Sbellard {
1166ea2384d3Sbellard     int ret;
1167ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1168ce1a14dcSpbrook     cb(opaque, ret);
1169ce1a14dcSpbrook     return NULL;
1170ea2384d3Sbellard }
1171ea2384d3Sbellard 
1172ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1173ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1174ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1175ea2384d3Sbellard {
1176ea2384d3Sbellard     int ret;
1177ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1178ce1a14dcSpbrook     cb(opaque, ret);
1179ce1a14dcSpbrook     return NULL;
1180ea2384d3Sbellard }
1181ea2384d3Sbellard 
118283f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1183ea2384d3Sbellard {
1184ea2384d3Sbellard }
1185beac80cdSbellard #else
118683f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1187beac80cdSbellard {
1188ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1189ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
1190ce1a14dcSpbrook     qemu_aio_release(acb);
1191beac80cdSbellard }
1192beac80cdSbellard 
1193ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1194ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1195ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1196ea2384d3Sbellard {
1197ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
119883f64091Sbellard     int ret;
119983f64091Sbellard 
1200ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1201ce1a14dcSpbrook     if (!acb->bh)
1202ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1203ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1204ce1a14dcSpbrook     acb->ret = ret;
1205ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1206ce1a14dcSpbrook     return &acb->common;
12077a6cba61Spbrook }
12087a6cba61Spbrook 
1209ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1210ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1211ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
121283f64091Sbellard {
1213ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
121483f64091Sbellard     int ret;
121583f64091Sbellard 
1216ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1217ce1a14dcSpbrook     if (!acb->bh)
1218ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1219ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1220ce1a14dcSpbrook     acb->ret = ret;
1221ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1222ce1a14dcSpbrook     return &acb->common;
122383f64091Sbellard }
122483f64091Sbellard 
1225ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
122683f64091Sbellard {
1227ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1228ce1a14dcSpbrook     qemu_bh_cancel(acb->bh);
1229ce1a14dcSpbrook     qemu_aio_release(acb);
123083f64091Sbellard }
1231faf07963Spbrook #endif /* !QEMU_IMG */
123283f64091Sbellard 
123383f64091Sbellard /**************************************************************/
123483f64091Sbellard /* sync block device emulation */
123583f64091Sbellard 
123683f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
123783f64091Sbellard {
123883f64091Sbellard     *(int *)opaque = ret;
123983f64091Sbellard }
124083f64091Sbellard 
124183f64091Sbellard #define NOT_DONE 0x7fffffff
124283f64091Sbellard 
124383f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
124483f64091Sbellard                         uint8_t *buf, int nb_sectors)
124583f64091Sbellard {
1246ce1a14dcSpbrook     int async_ret;
1247ce1a14dcSpbrook     BlockDriverAIOCB *acb;
124883f64091Sbellard 
124983f64091Sbellard     async_ret = NOT_DONE;
125083f64091Sbellard     qemu_aio_wait_start();
1251ce1a14dcSpbrook     acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
125283f64091Sbellard                         bdrv_rw_em_cb, &async_ret);
1253ce1a14dcSpbrook     if (acb == NULL) {
125483f64091Sbellard         qemu_aio_wait_end();
1255ce1a14dcSpbrook         return -1;
125683f64091Sbellard     }
125783f64091Sbellard     while (async_ret == NOT_DONE) {
125883f64091Sbellard         qemu_aio_wait();
125983f64091Sbellard     }
126083f64091Sbellard     qemu_aio_wait_end();
126183f64091Sbellard     return async_ret;
126283f64091Sbellard }
126383f64091Sbellard 
126483f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
126583f64091Sbellard                          const uint8_t *buf, int nb_sectors)
126683f64091Sbellard {
1267ce1a14dcSpbrook     int async_ret;
1268ce1a14dcSpbrook     BlockDriverAIOCB *acb;
126983f64091Sbellard 
127083f64091Sbellard     async_ret = NOT_DONE;
127183f64091Sbellard     qemu_aio_wait_start();
1272ce1a14dcSpbrook     acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
127383f64091Sbellard                          bdrv_rw_em_cb, &async_ret);
1274ce1a14dcSpbrook     if (acb == NULL) {
127583f64091Sbellard         qemu_aio_wait_end();
1276ce1a14dcSpbrook         return -1;
127783f64091Sbellard     }
127883f64091Sbellard     while (async_ret == NOT_DONE) {
127983f64091Sbellard         qemu_aio_wait();
128083f64091Sbellard     }
128183f64091Sbellard     qemu_aio_wait_end();
128283f64091Sbellard     return async_ret;
128383f64091Sbellard }
1284ea2384d3Sbellard 
1285ea2384d3Sbellard void bdrv_init(void)
1286ea2384d3Sbellard {
1287ea2384d3Sbellard     bdrv_register(&bdrv_raw);
128819cb3738Sbellard     bdrv_register(&bdrv_host_device);
1289ea2384d3Sbellard #ifndef _WIN32
1290ea2384d3Sbellard     bdrv_register(&bdrv_cow);
1291ea2384d3Sbellard #endif
1292ea2384d3Sbellard     bdrv_register(&bdrv_qcow);
1293ea2384d3Sbellard     bdrv_register(&bdrv_vmdk);
12943c56521bSbellard     bdrv_register(&bdrv_cloop);
1295585d0ed9Sbellard     bdrv_register(&bdrv_dmg);
1296a8753c34Sbellard     bdrv_register(&bdrv_bochs);
12976a0f9e82Sbellard     bdrv_register(&bdrv_vpc);
1298712e7874Sbellard     bdrv_register(&bdrv_vvfat);
1299faea38e7Sbellard     bdrv_register(&bdrv_qcow2);
13006ada7453Sths     bdrv_register(&bdrv_parallels);
1301ea2384d3Sbellard }
1302ce1a14dcSpbrook 
1303ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1304ce1a14dcSpbrook                    void *opaque)
1305ce1a14dcSpbrook {
1306ce1a14dcSpbrook     BlockDriver *drv;
1307ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1308ce1a14dcSpbrook 
1309ce1a14dcSpbrook     drv = bs->drv;
1310ce1a14dcSpbrook     if (drv->free_aiocb) {
1311ce1a14dcSpbrook         acb = drv->free_aiocb;
1312ce1a14dcSpbrook         drv->free_aiocb = acb->next;
1313ce1a14dcSpbrook     } else {
1314ce1a14dcSpbrook         acb = qemu_mallocz(drv->aiocb_size);
1315ce1a14dcSpbrook         if (!acb)
1316ce1a14dcSpbrook             return NULL;
1317ce1a14dcSpbrook     }
1318ce1a14dcSpbrook     acb->bs = bs;
1319ce1a14dcSpbrook     acb->cb = cb;
1320ce1a14dcSpbrook     acb->opaque = opaque;
1321ce1a14dcSpbrook     return acb;
1322ce1a14dcSpbrook }
1323ce1a14dcSpbrook 
1324ce1a14dcSpbrook void qemu_aio_release(void *p)
1325ce1a14dcSpbrook {
1326ce1a14dcSpbrook     BlockDriverAIOCB *acb = p;
1327ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
1328ce1a14dcSpbrook     acb->next = drv->free_aiocb;
1329ce1a14dcSpbrook     drv->free_aiocb = acb;
1330ce1a14dcSpbrook }
133119cb3738Sbellard 
133219cb3738Sbellard /**************************************************************/
133319cb3738Sbellard /* removable device support */
133419cb3738Sbellard 
133519cb3738Sbellard /**
133619cb3738Sbellard  * Return TRUE if the media is present
133719cb3738Sbellard  */
133819cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
133919cb3738Sbellard {
134019cb3738Sbellard     BlockDriver *drv = bs->drv;
134119cb3738Sbellard     int ret;
134219cb3738Sbellard     if (!drv)
134319cb3738Sbellard         return 0;
134419cb3738Sbellard     if (!drv->bdrv_is_inserted)
134519cb3738Sbellard         return 1;
134619cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
134719cb3738Sbellard     return ret;
134819cb3738Sbellard }
134919cb3738Sbellard 
135019cb3738Sbellard /**
135119cb3738Sbellard  * Return TRUE if the media changed since the last call to this
135219cb3738Sbellard  * function. It is currently only used for floppy disks
135319cb3738Sbellard  */
135419cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
135519cb3738Sbellard {
135619cb3738Sbellard     BlockDriver *drv = bs->drv;
135719cb3738Sbellard     int ret;
135819cb3738Sbellard 
135919cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
136019cb3738Sbellard         ret = -ENOTSUP;
136119cb3738Sbellard     else
136219cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
136319cb3738Sbellard     if (ret == -ENOTSUP)
136419cb3738Sbellard         ret = bs->media_changed;
136519cb3738Sbellard     bs->media_changed = 0;
136619cb3738Sbellard     return ret;
136719cb3738Sbellard }
136819cb3738Sbellard 
136919cb3738Sbellard /**
137019cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
137119cb3738Sbellard  */
137219cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag)
137319cb3738Sbellard {
137419cb3738Sbellard     BlockDriver *drv = bs->drv;
137519cb3738Sbellard     int ret;
137619cb3738Sbellard 
137719cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
137819cb3738Sbellard         ret = -ENOTSUP;
137919cb3738Sbellard     } else {
138019cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
138119cb3738Sbellard     }
138219cb3738Sbellard     if (ret == -ENOTSUP) {
138319cb3738Sbellard         if (eject_flag)
138419cb3738Sbellard             bdrv_close(bs);
138519cb3738Sbellard     }
138619cb3738Sbellard }
138719cb3738Sbellard 
138819cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
138919cb3738Sbellard {
139019cb3738Sbellard     return bs->locked;
139119cb3738Sbellard }
139219cb3738Sbellard 
139319cb3738Sbellard /**
139419cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
139519cb3738Sbellard  * to eject it manually).
139619cb3738Sbellard  */
139719cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
139819cb3738Sbellard {
139919cb3738Sbellard     BlockDriver *drv = bs->drv;
140019cb3738Sbellard 
140119cb3738Sbellard     bs->locked = locked;
140219cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
140319cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
140419cb3738Sbellard     }
140519cb3738Sbellard }
1406985a03b0Sths 
1407985a03b0Sths /* needed for generic scsi interface */
1408985a03b0Sths 
1409985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1410985a03b0Sths {
1411985a03b0Sths     BlockDriver *drv = bs->drv;
1412985a03b0Sths 
1413985a03b0Sths     if (drv && drv->bdrv_ioctl)
1414985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
1415985a03b0Sths     return -ENOTSUP;
1416985a03b0Sths }
1417