xref: /openbmc/qemu/block.c (revision 985a03b0ce38275c2ea355bf29b6d6b5779dbb56)
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))
38333f00271Sbalrog         open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT);
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;
524a36e69ddSths         else {
525a36e69ddSths 	    bs->rd_bytes += (unsigned) len;
526a36e69ddSths 	    bs->rd_ops ++;
52783f64091Sbellard             return 0;
528a36e69ddSths 	}
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;
559a36e69ddSths         else {
560a36e69ddSths 	    bs->wr_bytes += (unsigned) len;
561a36e69ddSths 	    bs->wr_ops ++;
56283f64091Sbellard             return 0;
563a36e69ddSths 	}
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 */
72096b8f136Sths void bdrv_get_geometry(BlockDriverState *bs, uint64_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 
789*985a03b0Sths int bdrv_is_sg(BlockDriverState *bs)
790*985a03b0Sths {
791*985a03b0Sths     return bs->sg;
792*985a03b0Sths }
793*985a03b0Sths 
79419cb3738Sbellard /* XXX: no longer used */
795b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
796b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
797b338082bSbellard {
798b338082bSbellard     bs->change_cb = change_cb;
799b338082bSbellard     bs->change_opaque = opaque;
800b338082bSbellard }
801b338082bSbellard 
802ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
803ea2384d3Sbellard {
804ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
805ea2384d3Sbellard         return 1;
806ea2384d3Sbellard     return bs->encrypted;
807ea2384d3Sbellard }
808ea2384d3Sbellard 
809ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
810ea2384d3Sbellard {
811ea2384d3Sbellard     int ret;
812ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
813ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
814ea2384d3Sbellard         if (ret < 0)
815ea2384d3Sbellard             return ret;
816ea2384d3Sbellard         if (!bs->encrypted)
817ea2384d3Sbellard             return 0;
818ea2384d3Sbellard     }
819ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
820ea2384d3Sbellard         return -1;
821ea2384d3Sbellard     return bs->drv->bdrv_set_key(bs, key);
822ea2384d3Sbellard }
823ea2384d3Sbellard 
824ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
825ea2384d3Sbellard {
82619cb3738Sbellard     if (!bs->drv) {
827ea2384d3Sbellard         buf[0] = '\0';
828ea2384d3Sbellard     } else {
829ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
830ea2384d3Sbellard     }
831ea2384d3Sbellard }
832ea2384d3Sbellard 
833ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
834ea2384d3Sbellard                          void *opaque)
835ea2384d3Sbellard {
836ea2384d3Sbellard     BlockDriver *drv;
837ea2384d3Sbellard 
838ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
839ea2384d3Sbellard         it(opaque, drv->format_name);
840ea2384d3Sbellard     }
841ea2384d3Sbellard }
842ea2384d3Sbellard 
843b338082bSbellard BlockDriverState *bdrv_find(const char *name)
844b338082bSbellard {
845b338082bSbellard     BlockDriverState *bs;
846b338082bSbellard 
847b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
848b338082bSbellard         if (!strcmp(name, bs->device_name))
849b338082bSbellard             return bs;
850b338082bSbellard     }
851b338082bSbellard     return NULL;
852b338082bSbellard }
853b338082bSbellard 
85481d0912dSbellard void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
85581d0912dSbellard {
85681d0912dSbellard     BlockDriverState *bs;
85781d0912dSbellard 
85881d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
85981d0912dSbellard         it(opaque, bs->device_name);
86081d0912dSbellard     }
86181d0912dSbellard }
86281d0912dSbellard 
863ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
864ea2384d3Sbellard {
865ea2384d3Sbellard     return bs->device_name;
866ea2384d3Sbellard }
867ea2384d3Sbellard 
8687a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
8697a6cba61Spbrook {
8707a6cba61Spbrook     if (bs->drv->bdrv_flush)
8717a6cba61Spbrook         bs->drv->bdrv_flush(bs);
8727a6cba61Spbrook     if (bs->backing_hd)
8737a6cba61Spbrook         bdrv_flush(bs->backing_hd);
8747a6cba61Spbrook }
8757a6cba61Spbrook 
876faf07963Spbrook #ifndef QEMU_IMG
877b338082bSbellard void bdrv_info(void)
878b338082bSbellard {
879b338082bSbellard     BlockDriverState *bs;
880b338082bSbellard 
881b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
882b338082bSbellard         term_printf("%s:", bs->device_name);
883b338082bSbellard         term_printf(" type=");
884b338082bSbellard         switch(bs->type) {
885b338082bSbellard         case BDRV_TYPE_HD:
886b338082bSbellard             term_printf("hd");
887b338082bSbellard             break;
888b338082bSbellard         case BDRV_TYPE_CDROM:
889b338082bSbellard             term_printf("cdrom");
890b338082bSbellard             break;
891b338082bSbellard         case BDRV_TYPE_FLOPPY:
892b338082bSbellard             term_printf("floppy");
893b338082bSbellard             break;
894b338082bSbellard         }
895b338082bSbellard         term_printf(" removable=%d", bs->removable);
896b338082bSbellard         if (bs->removable) {
897b338082bSbellard             term_printf(" locked=%d", bs->locked);
898b338082bSbellard         }
89919cb3738Sbellard         if (bs->drv) {
900fef30743Sths             term_printf(" file=");
901fef30743Sths 	    term_print_filename(bs->filename);
902fef30743Sths             if (bs->backing_file[0] != '\0') {
903fef30743Sths                 term_printf(" backing_file=");
904fef30743Sths 		term_print_filename(bs->backing_file);
905fef30743Sths 	    }
906b338082bSbellard             term_printf(" ro=%d", bs->read_only);
907ea2384d3Sbellard             term_printf(" drv=%s", bs->drv->format_name);
908ea2384d3Sbellard             if (bs->encrypted)
909ea2384d3Sbellard                 term_printf(" encrypted");
910b338082bSbellard         } else {
911b338082bSbellard             term_printf(" [not inserted]");
912b338082bSbellard         }
913b338082bSbellard         term_printf("\n");
914b338082bSbellard     }
915b338082bSbellard }
916a36e69ddSths 
917a36e69ddSths /* The "info blockstats" command. */
918a36e69ddSths void bdrv_info_stats (void)
919a36e69ddSths {
920a36e69ddSths     BlockDriverState *bs;
921a36e69ddSths 
922a36e69ddSths     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
923a36e69ddSths 	term_printf ("%s:"
924a36e69ddSths 		     " rd_bytes=%" PRIu64
925a36e69ddSths 		     " wr_bytes=%" PRIu64
926a36e69ddSths 		     " rd_operations=%" PRIu64
927a36e69ddSths 		     " wr_operations=%" PRIu64
928a36e69ddSths 		     "\n",
929a36e69ddSths 		     bs->device_name,
930a36e69ddSths 		     bs->rd_bytes, bs->wr_bytes,
931a36e69ddSths 		     bs->rd_ops, bs->wr_ops);
932a36e69ddSths     }
933a36e69ddSths }
934faf07963Spbrook #endif
935ea2384d3Sbellard 
93683f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
93783f64091Sbellard                                char *filename, int filename_size)
93883f64091Sbellard {
93983f64091Sbellard     if (!bs->backing_hd) {
94083f64091Sbellard         pstrcpy(filename, filename_size, "");
94183f64091Sbellard     } else {
94283f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
94383f64091Sbellard     }
94483f64091Sbellard }
94583f64091Sbellard 
946faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
947faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
948faea38e7Sbellard {
949faea38e7Sbellard     BlockDriver *drv = bs->drv;
950faea38e7Sbellard     if (!drv)
95119cb3738Sbellard         return -ENOMEDIUM;
952faea38e7Sbellard     if (!drv->bdrv_write_compressed)
953faea38e7Sbellard         return -ENOTSUP;
954faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
955faea38e7Sbellard }
956faea38e7Sbellard 
957faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
958faea38e7Sbellard {
959faea38e7Sbellard     BlockDriver *drv = bs->drv;
960faea38e7Sbellard     if (!drv)
96119cb3738Sbellard         return -ENOMEDIUM;
962faea38e7Sbellard     if (!drv->bdrv_get_info)
963faea38e7Sbellard         return -ENOTSUP;
964faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
965faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
966faea38e7Sbellard }
967faea38e7Sbellard 
968faea38e7Sbellard /**************************************************************/
969faea38e7Sbellard /* handling of snapshots */
970faea38e7Sbellard 
971faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
972faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
973faea38e7Sbellard {
974faea38e7Sbellard     BlockDriver *drv = bs->drv;
975faea38e7Sbellard     if (!drv)
97619cb3738Sbellard         return -ENOMEDIUM;
977faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
978faea38e7Sbellard         return -ENOTSUP;
979faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
980faea38e7Sbellard }
981faea38e7Sbellard 
982faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
983faea38e7Sbellard                        const char *snapshot_id)
984faea38e7Sbellard {
985faea38e7Sbellard     BlockDriver *drv = bs->drv;
986faea38e7Sbellard     if (!drv)
98719cb3738Sbellard         return -ENOMEDIUM;
988faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
989faea38e7Sbellard         return -ENOTSUP;
990faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
991faea38e7Sbellard }
992faea38e7Sbellard 
993faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
994faea38e7Sbellard {
995faea38e7Sbellard     BlockDriver *drv = bs->drv;
996faea38e7Sbellard     if (!drv)
99719cb3738Sbellard         return -ENOMEDIUM;
998faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
999faea38e7Sbellard         return -ENOTSUP;
1000faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
1001faea38e7Sbellard }
1002faea38e7Sbellard 
1003faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
1004faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
1005faea38e7Sbellard {
1006faea38e7Sbellard     BlockDriver *drv = bs->drv;
1007faea38e7Sbellard     if (!drv)
100819cb3738Sbellard         return -ENOMEDIUM;
1009faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
1010faea38e7Sbellard         return -ENOTSUP;
1011faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
1012faea38e7Sbellard }
1013faea38e7Sbellard 
1014faea38e7Sbellard #define NB_SUFFIXES 4
1015faea38e7Sbellard 
1016faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1017faea38e7Sbellard {
1018faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
1019faea38e7Sbellard     int64_t base;
1020faea38e7Sbellard     int i;
1021faea38e7Sbellard 
1022faea38e7Sbellard     if (size <= 999) {
1023faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
1024faea38e7Sbellard     } else {
1025faea38e7Sbellard         base = 1024;
1026faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
1027faea38e7Sbellard             if (size < (10 * base)) {
1028faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
1029faea38e7Sbellard                          (double)size / base,
1030faea38e7Sbellard                          suffixes[i]);
1031faea38e7Sbellard                 break;
1032faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1033faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1034faea38e7Sbellard                          ((size + (base >> 1)) / base),
1035faea38e7Sbellard                          suffixes[i]);
1036faea38e7Sbellard                 break;
1037faea38e7Sbellard             }
1038faea38e7Sbellard             base = base * 1024;
1039faea38e7Sbellard         }
1040faea38e7Sbellard     }
1041faea38e7Sbellard     return buf;
1042faea38e7Sbellard }
1043faea38e7Sbellard 
1044faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1045faea38e7Sbellard {
1046faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
10473b9f94e1Sbellard #ifdef _WIN32
10483b9f94e1Sbellard     struct tm *ptm;
10493b9f94e1Sbellard #else
1050faea38e7Sbellard     struct tm tm;
10513b9f94e1Sbellard #endif
1052faea38e7Sbellard     time_t ti;
1053faea38e7Sbellard     int64_t secs;
1054faea38e7Sbellard 
1055faea38e7Sbellard     if (!sn) {
1056faea38e7Sbellard         snprintf(buf, buf_size,
1057faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1058faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1059faea38e7Sbellard     } else {
1060faea38e7Sbellard         ti = sn->date_sec;
10613b9f94e1Sbellard #ifdef _WIN32
10623b9f94e1Sbellard         ptm = localtime(&ti);
10633b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
10643b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
10653b9f94e1Sbellard #else
1066faea38e7Sbellard         localtime_r(&ti, &tm);
1067faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1068faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
10693b9f94e1Sbellard #endif
1070faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1071faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1072faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1073faea38e7Sbellard                  (int)(secs / 3600),
1074faea38e7Sbellard                  (int)((secs / 60) % 60),
1075faea38e7Sbellard                  (int)(secs % 60),
1076faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1077faea38e7Sbellard         snprintf(buf, buf_size,
1078faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1079faea38e7Sbellard                  sn->id_str, sn->name,
1080faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1081faea38e7Sbellard                  date_buf,
1082faea38e7Sbellard                  clock_buf);
1083faea38e7Sbellard     }
1084faea38e7Sbellard     return buf;
1085faea38e7Sbellard }
1086faea38e7Sbellard 
108783f64091Sbellard 
1088ea2384d3Sbellard /**************************************************************/
108983f64091Sbellard /* async I/Os */
1090ea2384d3Sbellard 
1091ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
109283f64091Sbellard                                 uint8_t *buf, int nb_sectors,
109383f64091Sbellard                                 BlockDriverCompletionFunc *cb, void *opaque)
1094ea2384d3Sbellard {
109583f64091Sbellard     BlockDriver *drv = bs->drv;
1096a36e69ddSths     BlockDriverAIOCB *ret;
1097ea2384d3Sbellard 
109819cb3738Sbellard     if (!drv)
1099ce1a14dcSpbrook         return NULL;
110083f64091Sbellard 
110183f64091Sbellard     /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
110283f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
110383f64091Sbellard         memcpy(buf, bs->boot_sector_data, 512);
110483f64091Sbellard         sector_num++;
110583f64091Sbellard         nb_sectors--;
110683f64091Sbellard         buf += 512;
1107ea2384d3Sbellard     }
110883f64091Sbellard 
1109a36e69ddSths     ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1110a36e69ddSths 
1111a36e69ddSths     if (ret) {
1112a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1113a36e69ddSths 	bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1114a36e69ddSths 	bs->rd_ops ++;
1115a36e69ddSths     }
1116a36e69ddSths 
1117a36e69ddSths     return ret;
111883f64091Sbellard }
111983f64091Sbellard 
1120ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
112183f64091Sbellard                                  const uint8_t *buf, int nb_sectors,
112283f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
11237674e7bfSbellard {
112483f64091Sbellard     BlockDriver *drv = bs->drv;
1125a36e69ddSths     BlockDriverAIOCB *ret;
112683f64091Sbellard 
112719cb3738Sbellard     if (!drv)
1128ce1a14dcSpbrook         return NULL;
112983f64091Sbellard     if (bs->read_only)
1130ce1a14dcSpbrook         return NULL;
113183f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
113283f64091Sbellard         memcpy(bs->boot_sector_data, buf, 512);
11337674e7bfSbellard     }
113483f64091Sbellard 
1135a36e69ddSths     ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1136a36e69ddSths 
1137a36e69ddSths     if (ret) {
1138a36e69ddSths 	/* Update stats even though technically transfer has not happened. */
1139a36e69ddSths 	bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1140a36e69ddSths 	bs->wr_ops ++;
1141a36e69ddSths     }
1142a36e69ddSths 
1143a36e69ddSths     return ret;
114483f64091Sbellard }
114583f64091Sbellard 
114683f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
114783f64091Sbellard {
1148ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
114983f64091Sbellard 
115083f64091Sbellard     drv->bdrv_aio_cancel(acb);
115183f64091Sbellard }
115283f64091Sbellard 
115383f64091Sbellard 
115483f64091Sbellard /**************************************************************/
115583f64091Sbellard /* async block device emulation */
115683f64091Sbellard 
1157faf07963Spbrook #ifdef QEMU_IMG
1158ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1159ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1160ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1161ea2384d3Sbellard {
1162ea2384d3Sbellard     int ret;
1163ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1164ce1a14dcSpbrook     cb(opaque, ret);
1165ce1a14dcSpbrook     return NULL;
1166ea2384d3Sbellard }
1167ea2384d3Sbellard 
1168ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1169ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1170ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1171ea2384d3Sbellard {
1172ea2384d3Sbellard     int ret;
1173ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1174ce1a14dcSpbrook     cb(opaque, ret);
1175ce1a14dcSpbrook     return NULL;
1176ea2384d3Sbellard }
1177ea2384d3Sbellard 
117883f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1179ea2384d3Sbellard {
1180ea2384d3Sbellard }
1181beac80cdSbellard #else
118283f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1183beac80cdSbellard {
1184ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1185ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
1186ce1a14dcSpbrook     qemu_aio_release(acb);
1187beac80cdSbellard }
1188beac80cdSbellard 
1189ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1190ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1191ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1192ea2384d3Sbellard {
1193ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
119483f64091Sbellard     int ret;
119583f64091Sbellard 
1196ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1197ce1a14dcSpbrook     if (!acb->bh)
1198ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1199ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1200ce1a14dcSpbrook     acb->ret = ret;
1201ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1202ce1a14dcSpbrook     return &acb->common;
12037a6cba61Spbrook }
12047a6cba61Spbrook 
1205ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1206ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1207ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
120883f64091Sbellard {
1209ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
121083f64091Sbellard     int ret;
121183f64091Sbellard 
1212ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1213ce1a14dcSpbrook     if (!acb->bh)
1214ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1215ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1216ce1a14dcSpbrook     acb->ret = ret;
1217ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1218ce1a14dcSpbrook     return &acb->common;
121983f64091Sbellard }
122083f64091Sbellard 
1221ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
122283f64091Sbellard {
1223ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1224ce1a14dcSpbrook     qemu_bh_cancel(acb->bh);
1225ce1a14dcSpbrook     qemu_aio_release(acb);
122683f64091Sbellard }
1227faf07963Spbrook #endif /* !QEMU_IMG */
122883f64091Sbellard 
122983f64091Sbellard /**************************************************************/
123083f64091Sbellard /* sync block device emulation */
123183f64091Sbellard 
123283f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
123383f64091Sbellard {
123483f64091Sbellard     *(int *)opaque = ret;
123583f64091Sbellard }
123683f64091Sbellard 
123783f64091Sbellard #define NOT_DONE 0x7fffffff
123883f64091Sbellard 
123983f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
124083f64091Sbellard                         uint8_t *buf, int nb_sectors)
124183f64091Sbellard {
1242ce1a14dcSpbrook     int async_ret;
1243ce1a14dcSpbrook     BlockDriverAIOCB *acb;
124483f64091Sbellard 
124583f64091Sbellard     async_ret = NOT_DONE;
124683f64091Sbellard     qemu_aio_wait_start();
1247ce1a14dcSpbrook     acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
124883f64091Sbellard                         bdrv_rw_em_cb, &async_ret);
1249ce1a14dcSpbrook     if (acb == NULL) {
125083f64091Sbellard         qemu_aio_wait_end();
1251ce1a14dcSpbrook         return -1;
125283f64091Sbellard     }
125383f64091Sbellard     while (async_ret == NOT_DONE) {
125483f64091Sbellard         qemu_aio_wait();
125583f64091Sbellard     }
125683f64091Sbellard     qemu_aio_wait_end();
125783f64091Sbellard     return async_ret;
125883f64091Sbellard }
125983f64091Sbellard 
126083f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
126183f64091Sbellard                          const uint8_t *buf, int nb_sectors)
126283f64091Sbellard {
1263ce1a14dcSpbrook     int async_ret;
1264ce1a14dcSpbrook     BlockDriverAIOCB *acb;
126583f64091Sbellard 
126683f64091Sbellard     async_ret = NOT_DONE;
126783f64091Sbellard     qemu_aio_wait_start();
1268ce1a14dcSpbrook     acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
126983f64091Sbellard                          bdrv_rw_em_cb, &async_ret);
1270ce1a14dcSpbrook     if (acb == NULL) {
127183f64091Sbellard         qemu_aio_wait_end();
1272ce1a14dcSpbrook         return -1;
127383f64091Sbellard     }
127483f64091Sbellard     while (async_ret == NOT_DONE) {
127583f64091Sbellard         qemu_aio_wait();
127683f64091Sbellard     }
127783f64091Sbellard     qemu_aio_wait_end();
127883f64091Sbellard     return async_ret;
127983f64091Sbellard }
1280ea2384d3Sbellard 
1281ea2384d3Sbellard void bdrv_init(void)
1282ea2384d3Sbellard {
1283ea2384d3Sbellard     bdrv_register(&bdrv_raw);
128419cb3738Sbellard     bdrv_register(&bdrv_host_device);
1285ea2384d3Sbellard #ifndef _WIN32
1286ea2384d3Sbellard     bdrv_register(&bdrv_cow);
1287ea2384d3Sbellard #endif
1288ea2384d3Sbellard     bdrv_register(&bdrv_qcow);
1289ea2384d3Sbellard     bdrv_register(&bdrv_vmdk);
12903c56521bSbellard     bdrv_register(&bdrv_cloop);
1291585d0ed9Sbellard     bdrv_register(&bdrv_dmg);
1292a8753c34Sbellard     bdrv_register(&bdrv_bochs);
12936a0f9e82Sbellard     bdrv_register(&bdrv_vpc);
1294712e7874Sbellard     bdrv_register(&bdrv_vvfat);
1295faea38e7Sbellard     bdrv_register(&bdrv_qcow2);
12966ada7453Sths     bdrv_register(&bdrv_parallels);
1297ea2384d3Sbellard }
1298ce1a14dcSpbrook 
1299ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1300ce1a14dcSpbrook                    void *opaque)
1301ce1a14dcSpbrook {
1302ce1a14dcSpbrook     BlockDriver *drv;
1303ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1304ce1a14dcSpbrook 
1305ce1a14dcSpbrook     drv = bs->drv;
1306ce1a14dcSpbrook     if (drv->free_aiocb) {
1307ce1a14dcSpbrook         acb = drv->free_aiocb;
1308ce1a14dcSpbrook         drv->free_aiocb = acb->next;
1309ce1a14dcSpbrook     } else {
1310ce1a14dcSpbrook         acb = qemu_mallocz(drv->aiocb_size);
1311ce1a14dcSpbrook         if (!acb)
1312ce1a14dcSpbrook             return NULL;
1313ce1a14dcSpbrook     }
1314ce1a14dcSpbrook     acb->bs = bs;
1315ce1a14dcSpbrook     acb->cb = cb;
1316ce1a14dcSpbrook     acb->opaque = opaque;
1317ce1a14dcSpbrook     return acb;
1318ce1a14dcSpbrook }
1319ce1a14dcSpbrook 
1320ce1a14dcSpbrook void qemu_aio_release(void *p)
1321ce1a14dcSpbrook {
1322ce1a14dcSpbrook     BlockDriverAIOCB *acb = p;
1323ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
1324ce1a14dcSpbrook     acb->next = drv->free_aiocb;
1325ce1a14dcSpbrook     drv->free_aiocb = acb;
1326ce1a14dcSpbrook }
132719cb3738Sbellard 
132819cb3738Sbellard /**************************************************************/
132919cb3738Sbellard /* removable device support */
133019cb3738Sbellard 
133119cb3738Sbellard /**
133219cb3738Sbellard  * Return TRUE if the media is present
133319cb3738Sbellard  */
133419cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
133519cb3738Sbellard {
133619cb3738Sbellard     BlockDriver *drv = bs->drv;
133719cb3738Sbellard     int ret;
133819cb3738Sbellard     if (!drv)
133919cb3738Sbellard         return 0;
134019cb3738Sbellard     if (!drv->bdrv_is_inserted)
134119cb3738Sbellard         return 1;
134219cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
134319cb3738Sbellard     return ret;
134419cb3738Sbellard }
134519cb3738Sbellard 
134619cb3738Sbellard /**
134719cb3738Sbellard  * Return TRUE if the media changed since the last call to this
134819cb3738Sbellard  * function. It is currently only used for floppy disks
134919cb3738Sbellard  */
135019cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
135119cb3738Sbellard {
135219cb3738Sbellard     BlockDriver *drv = bs->drv;
135319cb3738Sbellard     int ret;
135419cb3738Sbellard 
135519cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
135619cb3738Sbellard         ret = -ENOTSUP;
135719cb3738Sbellard     else
135819cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
135919cb3738Sbellard     if (ret == -ENOTSUP)
136019cb3738Sbellard         ret = bs->media_changed;
136119cb3738Sbellard     bs->media_changed = 0;
136219cb3738Sbellard     return ret;
136319cb3738Sbellard }
136419cb3738Sbellard 
136519cb3738Sbellard /**
136619cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
136719cb3738Sbellard  */
136819cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag)
136919cb3738Sbellard {
137019cb3738Sbellard     BlockDriver *drv = bs->drv;
137119cb3738Sbellard     int ret;
137219cb3738Sbellard 
137319cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
137419cb3738Sbellard         ret = -ENOTSUP;
137519cb3738Sbellard     } else {
137619cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
137719cb3738Sbellard     }
137819cb3738Sbellard     if (ret == -ENOTSUP) {
137919cb3738Sbellard         if (eject_flag)
138019cb3738Sbellard             bdrv_close(bs);
138119cb3738Sbellard     }
138219cb3738Sbellard }
138319cb3738Sbellard 
138419cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
138519cb3738Sbellard {
138619cb3738Sbellard     return bs->locked;
138719cb3738Sbellard }
138819cb3738Sbellard 
138919cb3738Sbellard /**
139019cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
139119cb3738Sbellard  * to eject it manually).
139219cb3738Sbellard  */
139319cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
139419cb3738Sbellard {
139519cb3738Sbellard     BlockDriver *drv = bs->drv;
139619cb3738Sbellard 
139719cb3738Sbellard     bs->locked = locked;
139819cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
139919cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
140019cb3738Sbellard     }
140119cb3738Sbellard }
1402*985a03b0Sths 
1403*985a03b0Sths /* needed for generic scsi interface */
1404*985a03b0Sths 
1405*985a03b0Sths int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1406*985a03b0Sths {
1407*985a03b0Sths     BlockDriver *drv = bs->drv;
1408*985a03b0Sths 
1409*985a03b0Sths     if (drv && drv->bdrv_ioctl)
1410*985a03b0Sths         return drv->bdrv_ioctl(bs, req, buf);
1411*985a03b0Sths     return -ENOTSUP;
1412*985a03b0Sths }
1413