xref: /openbmc/qemu/block.c (revision eb5c851f2dadab59c9de4132e00268219edb5a44)
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  */
24fc01f7e7Sbellard #include "vl.h"
25ea2384d3Sbellard #include "block_int.h"
26fc01f7e7Sbellard 
277674e7bfSbellard #ifdef _BSD
287674e7bfSbellard #include <sys/types.h>
297674e7bfSbellard #include <sys/stat.h>
307674e7bfSbellard #include <sys/ioctl.h>
317674e7bfSbellard #include <sys/queue.h>
327674e7bfSbellard #include <sys/disk.h>
337674e7bfSbellard #endif
347674e7bfSbellard 
3583f64091Sbellard #define SECTOR_BITS 9
3683f64091Sbellard #define SECTOR_SIZE (1 << SECTOR_BITS)
373b0d4f61Sbellard 
3890765429Sbellard typedef struct BlockDriverAIOCBSync {
3990765429Sbellard     BlockDriverAIOCB common;
4090765429Sbellard     QEMUBH *bh;
4190765429Sbellard     int ret;
4290765429Sbellard } BlockDriverAIOCBSync;
4390765429Sbellard 
44ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
45ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
46ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
47ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
48ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
49ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque);
5083f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
5183f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
5283f64091Sbellard                         uint8_t *buf, int nb_sectors);
5383f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
5483f64091Sbellard                          const uint8_t *buf, int nb_sectors);
55ec530c81Sbellard 
56b338082bSbellard static BlockDriverState *bdrv_first;
57ea2384d3Sbellard static BlockDriver *first_drv;
58ea2384d3Sbellard 
5983f64091Sbellard int path_is_absolute(const char *path)
6083f64091Sbellard {
6183f64091Sbellard     const char *p;
6221664424Sbellard #ifdef _WIN32
6321664424Sbellard     /* specific case for names like: "\\.\d:" */
6421664424Sbellard     if (*path == '/' || *path == '\\')
6521664424Sbellard         return 1;
6621664424Sbellard #endif
6783f64091Sbellard     p = strchr(path, ':');
6883f64091Sbellard     if (p)
6983f64091Sbellard         p++;
7083f64091Sbellard     else
7183f64091Sbellard         p = path;
723b9f94e1Sbellard #ifdef _WIN32
733b9f94e1Sbellard     return (*p == '/' || *p == '\\');
743b9f94e1Sbellard #else
753b9f94e1Sbellard     return (*p == '/');
763b9f94e1Sbellard #endif
7783f64091Sbellard }
7883f64091Sbellard 
7983f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
8083f64091Sbellard    path to it by considering it is relative to base_path. URL are
8183f64091Sbellard    supported. */
8283f64091Sbellard void path_combine(char *dest, int dest_size,
8383f64091Sbellard                   const char *base_path,
8483f64091Sbellard                   const char *filename)
8583f64091Sbellard {
8683f64091Sbellard     const char *p, *p1;
8783f64091Sbellard     int len;
8883f64091Sbellard 
8983f64091Sbellard     if (dest_size <= 0)
9083f64091Sbellard         return;
9183f64091Sbellard     if (path_is_absolute(filename)) {
9283f64091Sbellard         pstrcpy(dest, dest_size, filename);
9383f64091Sbellard     } else {
9483f64091Sbellard         p = strchr(base_path, ':');
9583f64091Sbellard         if (p)
9683f64091Sbellard             p++;
9783f64091Sbellard         else
9883f64091Sbellard             p = base_path;
993b9f94e1Sbellard         p1 = strrchr(base_path, '/');
1003b9f94e1Sbellard #ifdef _WIN32
1013b9f94e1Sbellard         {
1023b9f94e1Sbellard             const char *p2;
1033b9f94e1Sbellard             p2 = strrchr(base_path, '\\');
1043b9f94e1Sbellard             if (!p1 || p2 > p1)
1053b9f94e1Sbellard                 p1 = p2;
1063b9f94e1Sbellard         }
1073b9f94e1Sbellard #endif
10883f64091Sbellard         if (p1)
10983f64091Sbellard             p1++;
11083f64091Sbellard         else
11183f64091Sbellard             p1 = base_path;
11283f64091Sbellard         if (p1 > p)
11383f64091Sbellard             p = p1;
11483f64091Sbellard         len = p - base_path;
11583f64091Sbellard         if (len > dest_size - 1)
11683f64091Sbellard             len = dest_size - 1;
11783f64091Sbellard         memcpy(dest, base_path, len);
11883f64091Sbellard         dest[len] = '\0';
11983f64091Sbellard         pstrcat(dest, dest_size, filename);
12083f64091Sbellard     }
12183f64091Sbellard }
12283f64091Sbellard 
12383f64091Sbellard 
124ea2384d3Sbellard void bdrv_register(BlockDriver *bdrv)
125ea2384d3Sbellard {
126ce1a14dcSpbrook     if (!bdrv->bdrv_aio_read) {
12783f64091Sbellard         /* add AIO emulation layer */
12883f64091Sbellard         bdrv->bdrv_aio_read = bdrv_aio_read_em;
12983f64091Sbellard         bdrv->bdrv_aio_write = bdrv_aio_write_em;
13083f64091Sbellard         bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
13190765429Sbellard         bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
13283f64091Sbellard     } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
13383f64091Sbellard         /* add synchronous IO emulation layer */
13483f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
13583f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
13683f64091Sbellard     }
137ea2384d3Sbellard     bdrv->next = first_drv;
138ea2384d3Sbellard     first_drv = bdrv;
139ea2384d3Sbellard }
140b338082bSbellard 
141b338082bSbellard /* create a new block device (by default it is empty) */
142b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
143fc01f7e7Sbellard {
144b338082bSbellard     BlockDriverState **pbs, *bs;
145b338082bSbellard 
146b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
147b338082bSbellard     if(!bs)
148b338082bSbellard         return NULL;
149b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
150ea2384d3Sbellard     if (device_name[0] != '\0') {
151b338082bSbellard         /* insert at the end */
152b338082bSbellard         pbs = &bdrv_first;
153b338082bSbellard         while (*pbs != NULL)
154b338082bSbellard             pbs = &(*pbs)->next;
155b338082bSbellard         *pbs = bs;
156ea2384d3Sbellard     }
157b338082bSbellard     return bs;
158b338082bSbellard }
159b338082bSbellard 
160ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
161ea2384d3Sbellard {
162ea2384d3Sbellard     BlockDriver *drv1;
163ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
164ea2384d3Sbellard         if (!strcmp(drv1->format_name, format_name))
165ea2384d3Sbellard             return drv1;
166ea2384d3Sbellard     }
167ea2384d3Sbellard     return NULL;
168ea2384d3Sbellard }
169ea2384d3Sbellard 
170ea2384d3Sbellard int bdrv_create(BlockDriver *drv,
171ea2384d3Sbellard                 const char *filename, int64_t size_in_sectors,
172ea2384d3Sbellard                 const char *backing_file, int flags)
173ea2384d3Sbellard {
174ea2384d3Sbellard     if (!drv->bdrv_create)
175ea2384d3Sbellard         return -ENOTSUP;
176ea2384d3Sbellard     return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
177ea2384d3Sbellard }
178ea2384d3Sbellard 
179d5249393Sbellard #ifdef _WIN32
18095389c86Sbellard void get_tmp_filename(char *filename, int size)
181d5249393Sbellard {
1823b9f94e1Sbellard     char temp_dir[MAX_PATH];
1833b9f94e1Sbellard 
1843b9f94e1Sbellard     GetTempPath(MAX_PATH, temp_dir);
1853b9f94e1Sbellard     GetTempFileName(temp_dir, "qem", 0, filename);
186d5249393Sbellard }
187d5249393Sbellard #else
18895389c86Sbellard void get_tmp_filename(char *filename, int size)
189ea2384d3Sbellard {
190ea2384d3Sbellard     int fd;
191d5249393Sbellard     /* XXX: race condition possible */
192ea2384d3Sbellard     pstrcpy(filename, size, "/tmp/vl.XXXXXX");
193ea2384d3Sbellard     fd = mkstemp(filename);
194ea2384d3Sbellard     close(fd);
195ea2384d3Sbellard }
196d5249393Sbellard #endif
197ea2384d3Sbellard 
19819cb3738Sbellard #ifdef _WIN32
199f45512feSbellard static int is_windows_drive_prefix(const char *filename)
200f45512feSbellard {
201f45512feSbellard     return (((filename[0] >= 'a' && filename[0] <= 'z') ||
202f45512feSbellard              (filename[0] >= 'A' && filename[0] <= 'Z')) &&
203f45512feSbellard             filename[1] == ':');
204f45512feSbellard }
205f45512feSbellard 
20619cb3738Sbellard static int is_windows_drive(const char *filename)
20719cb3738Sbellard {
208f45512feSbellard     if (is_windows_drive_prefix(filename) &&
209f45512feSbellard         filename[2] == '\0')
21019cb3738Sbellard         return 1;
21119cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
21219cb3738Sbellard         strstart(filename, "//./", NULL))
21319cb3738Sbellard         return 1;
21419cb3738Sbellard     return 0;
21519cb3738Sbellard }
21619cb3738Sbellard #endif
21719cb3738Sbellard 
21883f64091Sbellard static BlockDriver *find_protocol(const char *filename)
21983f64091Sbellard {
22083f64091Sbellard     BlockDriver *drv1;
22183f64091Sbellard     char protocol[128];
22283f64091Sbellard     int len;
22383f64091Sbellard     const char *p;
22419cb3738Sbellard 
22519cb3738Sbellard #ifdef _WIN32
226f45512feSbellard     if (is_windows_drive(filename) ||
227f45512feSbellard         is_windows_drive_prefix(filename))
22819cb3738Sbellard         return &bdrv_raw;
22919cb3738Sbellard #endif
23083f64091Sbellard     p = strchr(filename, ':');
23183f64091Sbellard     if (!p)
23283f64091Sbellard         return &bdrv_raw;
23383f64091Sbellard     len = p - filename;
23483f64091Sbellard     if (len > sizeof(protocol) - 1)
23583f64091Sbellard         len = sizeof(protocol) - 1;
23683f64091Sbellard     memcpy(protocol, filename, len);
23783f64091Sbellard     protocol[len] = '\0';
23883f64091Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
23983f64091Sbellard         if (drv1->protocol_name &&
24083f64091Sbellard             !strcmp(drv1->protocol_name, protocol))
24183f64091Sbellard             return drv1;
24283f64091Sbellard     }
24383f64091Sbellard     return NULL;
24483f64091Sbellard }
24583f64091Sbellard 
2467674e7bfSbellard /* XXX: force raw format if block or character device ? It would
2477674e7bfSbellard    simplify the BSD case */
248ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
249ea2384d3Sbellard {
25083f64091Sbellard     int ret, score, score_max;
251ea2384d3Sbellard     BlockDriver *drv1, *drv;
25283f64091Sbellard     uint8_t buf[2048];
25383f64091Sbellard     BlockDriverState *bs;
254ea2384d3Sbellard 
25519cb3738Sbellard     /* detect host devices. By convention, /dev/cdrom[N] is always
25619cb3738Sbellard        recognized as a host CDROM */
25719cb3738Sbellard     if (strstart(filename, "/dev/cdrom", NULL))
25819cb3738Sbellard         return &bdrv_host_device;
25919cb3738Sbellard #ifdef _WIN32
26019cb3738Sbellard     if (is_windows_drive(filename))
26119cb3738Sbellard         return &bdrv_host_device;
26219cb3738Sbellard #else
26319cb3738Sbellard     {
26419cb3738Sbellard         struct stat st;
26519cb3738Sbellard         if (stat(filename, &st) >= 0 &&
26619cb3738Sbellard             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
26719cb3738Sbellard             return &bdrv_host_device;
26819cb3738Sbellard         }
26919cb3738Sbellard     }
27019cb3738Sbellard #endif
27119cb3738Sbellard 
27283f64091Sbellard     drv = find_protocol(filename);
27319cb3738Sbellard     /* no need to test disk image formats for vvfat */
27483f64091Sbellard     if (drv == &bdrv_vvfat)
27583f64091Sbellard         return drv;
27683f64091Sbellard 
27783f64091Sbellard     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
27883f64091Sbellard     if (ret < 0)
2797674e7bfSbellard         return NULL;
28083f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
28183f64091Sbellard     bdrv_delete(bs);
282ea2384d3Sbellard     if (ret < 0) {
283ea2384d3Sbellard         return NULL;
284ea2384d3Sbellard     }
285ea2384d3Sbellard 
286ea2384d3Sbellard     score_max = 0;
287ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
28883f64091Sbellard         if (drv1->bdrv_probe) {
289ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
290ea2384d3Sbellard             if (score > score_max) {
291ea2384d3Sbellard                 score_max = score;
292ea2384d3Sbellard                 drv = drv1;
293ea2384d3Sbellard             }
294ea2384d3Sbellard         }
29583f64091Sbellard     }
296ea2384d3Sbellard     return drv;
297ea2384d3Sbellard }
298ea2384d3Sbellard 
29983f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
300b338082bSbellard {
30183f64091Sbellard     BlockDriverState *bs;
30283f64091Sbellard     int ret;
3033b0d4f61Sbellard 
30483f64091Sbellard     bs = bdrv_new("");
30583f64091Sbellard     if (!bs)
30683f64091Sbellard         return -ENOMEM;
30783f64091Sbellard     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
30883f64091Sbellard     if (ret < 0) {
30983f64091Sbellard         bdrv_delete(bs);
31083f64091Sbellard         return ret;
3113b0d4f61Sbellard     }
31283f64091Sbellard     *pbs = bs;
31383f64091Sbellard     return 0;
3143b0d4f61Sbellard }
3153b0d4f61Sbellard 
31683f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
31783f64091Sbellard {
31883f64091Sbellard     return bdrv_open2(bs, filename, flags, NULL);
319ea2384d3Sbellard }
320ea2384d3Sbellard 
32183f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
322ea2384d3Sbellard                BlockDriver *drv)
323ea2384d3Sbellard {
32483f64091Sbellard     int ret, open_flags;
325*eb5c851fSths     char tmp_filename[PATH_MAX];
326*eb5c851fSths     char backing_filename[PATH_MAX];
327fc01f7e7Sbellard 
3280849bf08Sbellard     bs->read_only = 0;
329ea2384d3Sbellard     bs->is_temporary = 0;
330ea2384d3Sbellard     bs->encrypted = 0;
33133e3963eSbellard 
33283f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
333ea2384d3Sbellard         BlockDriverState *bs1;
334ea2384d3Sbellard         int64_t total_size;
33533e3963eSbellard 
336ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
337ea2384d3Sbellard            instead of opening 'filename' directly */
338ea2384d3Sbellard 
339ea2384d3Sbellard         /* if there is a backing file, use it */
340ea2384d3Sbellard         bs1 = bdrv_new("");
341ea2384d3Sbellard         if (!bs1) {
34283f64091Sbellard             return -ENOMEM;
343ea2384d3Sbellard         }
344ea2384d3Sbellard         if (bdrv_open(bs1, filename, 0) < 0) {
345ea2384d3Sbellard             bdrv_delete(bs1);
346ea2384d3Sbellard             return -1;
347ea2384d3Sbellard         }
34883f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
349ea2384d3Sbellard         bdrv_delete(bs1);
350ea2384d3Sbellard 
351ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
352a817d936Sbellard         realpath(filename, backing_filename);
353d15a771dSbellard         if (bdrv_create(&bdrv_qcow2, tmp_filename,
354a817d936Sbellard                         total_size, backing_filename, 0) < 0) {
355ea2384d3Sbellard             return -1;
356ea2384d3Sbellard         }
357ea2384d3Sbellard         filename = tmp_filename;
358ea2384d3Sbellard         bs->is_temporary = 1;
359ea2384d3Sbellard     }
360ea2384d3Sbellard 
361ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
36283f64091Sbellard     if (flags & BDRV_O_FILE) {
36383f64091Sbellard         drv = find_protocol(filename);
36483f64091Sbellard         if (!drv)
36583f64091Sbellard             return -ENOENT;
36683f64091Sbellard     } else {
367ea2384d3Sbellard         if (!drv) {
368ea2384d3Sbellard             drv = find_image_format(filename);
369ea2384d3Sbellard             if (!drv)
370ea2384d3Sbellard                 return -1;
371ea2384d3Sbellard         }
37283f64091Sbellard     }
373ea2384d3Sbellard     bs->drv = drv;
374ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
375ea2384d3Sbellard     if (bs->opaque == NULL && drv->instance_size > 0)
376ea2384d3Sbellard         return -1;
37783f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
37883f64091Sbellard        RDONLY as fallback */
37983f64091Sbellard     if (!(flags & BDRV_O_FILE))
38083f64091Sbellard         open_flags = BDRV_O_RDWR;
38183f64091Sbellard     else
38283f64091Sbellard         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
38383f64091Sbellard     ret = drv->bdrv_open(bs, filename, open_flags);
38483f64091Sbellard     if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
38583f64091Sbellard         ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
38683f64091Sbellard         bs->read_only = 1;
38783f64091Sbellard     }
388ea2384d3Sbellard     if (ret < 0) {
389ea2384d3Sbellard         qemu_free(bs->opaque);
3906b21b973Sbellard         bs->opaque = NULL;
3916b21b973Sbellard         bs->drv = NULL;
39283f64091Sbellard         return ret;
393ea2384d3Sbellard     }
394d15a771dSbellard     if (drv->bdrv_getlength) {
395d15a771dSbellard         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
396d15a771dSbellard     }
397ea2384d3Sbellard #ifndef _WIN32
398ea2384d3Sbellard     if (bs->is_temporary) {
399ea2384d3Sbellard         unlink(filename);
40033e3963eSbellard     }
40167b915a5Sbellard #endif
40283f64091Sbellard     if (bs->backing_file[0] != '\0') {
403ea2384d3Sbellard         /* if there is a backing file, use it */
404ea2384d3Sbellard         bs->backing_hd = bdrv_new("");
405ea2384d3Sbellard         if (!bs->backing_hd) {
406ea2384d3Sbellard         fail:
407ea2384d3Sbellard             bdrv_close(bs);
4086b21b973Sbellard             return -ENOMEM;
409ea2384d3Sbellard         }
41083f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
41183f64091Sbellard                      filename, bs->backing_file);
41283f64091Sbellard         if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
413ea2384d3Sbellard             goto fail;
414ea2384d3Sbellard     }
41533e3963eSbellard 
416b338082bSbellard     /* call the change callback */
41719cb3738Sbellard     bs->media_changed = 1;
418b338082bSbellard     if (bs->change_cb)
419b338082bSbellard         bs->change_cb(bs->change_opaque);
420b338082bSbellard 
421b338082bSbellard     return 0;
422fc01f7e7Sbellard }
423fc01f7e7Sbellard 
424fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
425fc01f7e7Sbellard {
42619cb3738Sbellard     if (bs->drv) {
427ea2384d3Sbellard         if (bs->backing_hd)
428ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
429ea2384d3Sbellard         bs->drv->bdrv_close(bs);
430ea2384d3Sbellard         qemu_free(bs->opaque);
431ea2384d3Sbellard #ifdef _WIN32
432ea2384d3Sbellard         if (bs->is_temporary) {
433ea2384d3Sbellard             unlink(bs->filename);
434ea2384d3Sbellard         }
43567b915a5Sbellard #endif
436ea2384d3Sbellard         bs->opaque = NULL;
437ea2384d3Sbellard         bs->drv = NULL;
438b338082bSbellard 
439b338082bSbellard         /* call the change callback */
44019cb3738Sbellard         bs->media_changed = 1;
441b338082bSbellard         if (bs->change_cb)
442b338082bSbellard             bs->change_cb(bs->change_opaque);
443b338082bSbellard     }
444b338082bSbellard }
445b338082bSbellard 
446b338082bSbellard void bdrv_delete(BlockDriverState *bs)
447b338082bSbellard {
448ea2384d3Sbellard     /* XXX: remove the driver list */
449b338082bSbellard     bdrv_close(bs);
450b338082bSbellard     qemu_free(bs);
451fc01f7e7Sbellard }
452fc01f7e7Sbellard 
45333e3963eSbellard /* commit COW file into the raw image */
45433e3963eSbellard int bdrv_commit(BlockDriverState *bs)
45533e3963eSbellard {
45619cb3738Sbellard     BlockDriver *drv = bs->drv;
45783f64091Sbellard     int64_t i, total_sectors;
458ea2384d3Sbellard     int n, j;
459ea2384d3Sbellard     unsigned char sector[512];
46033e3963eSbellard 
46119cb3738Sbellard     if (!drv)
46219cb3738Sbellard         return -ENOMEDIUM;
46333e3963eSbellard 
46433e3963eSbellard     if (bs->read_only) {
465ea2384d3Sbellard 	return -EACCES;
46633e3963eSbellard     }
46733e3963eSbellard 
468ea2384d3Sbellard     if (!bs->backing_hd) {
469ea2384d3Sbellard 	return -ENOTSUP;
470ea2384d3Sbellard     }
471ea2384d3Sbellard 
47283f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
47383f64091Sbellard     for (i = 0; i < total_sectors;) {
47419cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
475ea2384d3Sbellard             for(j = 0; j < n; j++) {
47633e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
477ea2384d3Sbellard                     return -EIO;
47833e3963eSbellard                 }
47933e3963eSbellard 
480ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
481ea2384d3Sbellard                     return -EIO;
48233e3963eSbellard                 }
483ea2384d3Sbellard                 i++;
484ea2384d3Sbellard 	    }
485ea2384d3Sbellard 	} else {
486ea2384d3Sbellard             i += n;
48733e3963eSbellard         }
48833e3963eSbellard     }
48995389c86Sbellard 
49019cb3738Sbellard     if (drv->bdrv_make_empty)
49119cb3738Sbellard 	return drv->bdrv_make_empty(bs);
49295389c86Sbellard 
49333e3963eSbellard     return 0;
49433e3963eSbellard }
49533e3963eSbellard 
49619cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
497fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
498fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
499fc01f7e7Sbellard {
500ea2384d3Sbellard     BlockDriver *drv = bs->drv;
501fc01f7e7Sbellard 
50219cb3738Sbellard     if (!drv)
50319cb3738Sbellard         return -ENOMEDIUM;
504b338082bSbellard 
50583f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
506cf98951bSbellard             memcpy(buf, bs->boot_sector_data, 512);
50783f64091Sbellard         sector_num++;
50883f64091Sbellard         nb_sectors--;
50983f64091Sbellard         buf += 512;
51083f64091Sbellard         if (nb_sectors == 0)
511fc01f7e7Sbellard             return 0;
512fc01f7e7Sbellard     }
51383f64091Sbellard     if (drv->bdrv_pread) {
51483f64091Sbellard         int ret, len;
51583f64091Sbellard         len = nb_sectors * 512;
51683f64091Sbellard         ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
51783f64091Sbellard         if (ret < 0)
51883f64091Sbellard             return ret;
51983f64091Sbellard         else if (ret != len)
52019cb3738Sbellard             return -EINVAL;
52183f64091Sbellard         else
52283f64091Sbellard             return 0;
52383f64091Sbellard     } else {
52483f64091Sbellard         return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
52583f64091Sbellard     }
52683f64091Sbellard }
527fc01f7e7Sbellard 
52819cb3738Sbellard /* Return < 0 if error. Important errors are:
52919cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
53019cb3738Sbellard   -ENOMEDIUM   No media inserted.
53119cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
53219cb3738Sbellard   -EACCES      Trying to write a read-only device
53319cb3738Sbellard */
534fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
535fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
536fc01f7e7Sbellard {
53783f64091Sbellard     BlockDriver *drv = bs->drv;
53819cb3738Sbellard     if (!bs->drv)
53919cb3738Sbellard         return -ENOMEDIUM;
5400849bf08Sbellard     if (bs->read_only)
54119cb3738Sbellard         return -EACCES;
54279639d42Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
54379639d42Sbellard         memcpy(bs->boot_sector_data, buf, 512);
54479639d42Sbellard     }
54583f64091Sbellard     if (drv->bdrv_pwrite) {
54683f64091Sbellard         int ret, len;
54783f64091Sbellard         len = nb_sectors * 512;
54883f64091Sbellard         ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
54983f64091Sbellard         if (ret < 0)
55083f64091Sbellard             return ret;
55183f64091Sbellard         else if (ret != len)
55283f64091Sbellard             return -EIO;
55383f64091Sbellard         else
55483f64091Sbellard             return 0;
55583f64091Sbellard     } else {
55683f64091Sbellard         return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
55783f64091Sbellard     }
55883f64091Sbellard }
55983f64091Sbellard 
56083f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
561faea38e7Sbellard                          uint8_t *buf, int count1)
56283f64091Sbellard {
56383f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
56483f64091Sbellard     int len, nb_sectors, count;
56583f64091Sbellard     int64_t sector_num;
56683f64091Sbellard 
56783f64091Sbellard     count = count1;
56883f64091Sbellard     /* first read to align to sector start */
56983f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
57083f64091Sbellard     if (len > count)
57183f64091Sbellard         len = count;
57283f64091Sbellard     sector_num = offset >> SECTOR_BITS;
57383f64091Sbellard     if (len > 0) {
57483f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
57583f64091Sbellard             return -EIO;
57683f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
57783f64091Sbellard         count -= len;
57883f64091Sbellard         if (count == 0)
57983f64091Sbellard             return count1;
58083f64091Sbellard         sector_num++;
58183f64091Sbellard         buf += len;
58283f64091Sbellard     }
58383f64091Sbellard 
58483f64091Sbellard     /* read the sectors "in place" */
58583f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
58683f64091Sbellard     if (nb_sectors > 0) {
58783f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
58883f64091Sbellard             return -EIO;
58983f64091Sbellard         sector_num += nb_sectors;
59083f64091Sbellard         len = nb_sectors << SECTOR_BITS;
59183f64091Sbellard         buf += len;
59283f64091Sbellard         count -= len;
59383f64091Sbellard     }
59483f64091Sbellard 
59583f64091Sbellard     /* add data from the last sector */
59683f64091Sbellard     if (count > 0) {
59783f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
59883f64091Sbellard             return -EIO;
59983f64091Sbellard         memcpy(buf, tmp_buf, count);
60083f64091Sbellard     }
60183f64091Sbellard     return count1;
60283f64091Sbellard }
60383f64091Sbellard 
60483f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
605faea38e7Sbellard                           const uint8_t *buf, int count1)
60683f64091Sbellard {
60783f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
60883f64091Sbellard     int len, nb_sectors, count;
60983f64091Sbellard     int64_t sector_num;
61083f64091Sbellard 
61183f64091Sbellard     count = count1;
61283f64091Sbellard     /* first write to align to sector start */
61383f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
61483f64091Sbellard     if (len > count)
61583f64091Sbellard         len = count;
61683f64091Sbellard     sector_num = offset >> SECTOR_BITS;
61783f64091Sbellard     if (len > 0) {
61883f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
61983f64091Sbellard             return -EIO;
62083f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
62183f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
62283f64091Sbellard             return -EIO;
62383f64091Sbellard         count -= len;
62483f64091Sbellard         if (count == 0)
62583f64091Sbellard             return count1;
62683f64091Sbellard         sector_num++;
62783f64091Sbellard         buf += len;
62883f64091Sbellard     }
62983f64091Sbellard 
63083f64091Sbellard     /* write the sectors "in place" */
63183f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
63283f64091Sbellard     if (nb_sectors > 0) {
63383f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
63483f64091Sbellard             return -EIO;
63583f64091Sbellard         sector_num += nb_sectors;
63683f64091Sbellard         len = nb_sectors << SECTOR_BITS;
63783f64091Sbellard         buf += len;
63883f64091Sbellard         count -= len;
63983f64091Sbellard     }
64083f64091Sbellard 
64183f64091Sbellard     /* add data from the last sector */
64283f64091Sbellard     if (count > 0) {
64383f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
64483f64091Sbellard             return -EIO;
64583f64091Sbellard         memcpy(tmp_buf, buf, count);
64683f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
64783f64091Sbellard             return -EIO;
64883f64091Sbellard     }
64983f64091Sbellard     return count1;
65083f64091Sbellard }
65183f64091Sbellard 
65283f64091Sbellard /**
65383f64091Sbellard  * Read with byte offsets (needed only for file protocols)
65483f64091Sbellard  */
65583f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset,
65683f64091Sbellard                void *buf1, int count1)
65783f64091Sbellard {
65883f64091Sbellard     BlockDriver *drv = bs->drv;
65983f64091Sbellard 
66083f64091Sbellard     if (!drv)
66119cb3738Sbellard         return -ENOMEDIUM;
66283f64091Sbellard     if (!drv->bdrv_pread)
663faea38e7Sbellard         return bdrv_pread_em(bs, offset, buf1, count1);
66483f64091Sbellard     return drv->bdrv_pread(bs, offset, buf1, count1);
66583f64091Sbellard }
66683f64091Sbellard 
66783f64091Sbellard /**
66883f64091Sbellard  * Write with byte offsets (needed only for file protocols)
66983f64091Sbellard  */
67083f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
67183f64091Sbellard                 const void *buf1, int count1)
67283f64091Sbellard {
67383f64091Sbellard     BlockDriver *drv = bs->drv;
67483f64091Sbellard 
67583f64091Sbellard     if (!drv)
67619cb3738Sbellard         return -ENOMEDIUM;
67783f64091Sbellard     if (!drv->bdrv_pwrite)
678faea38e7Sbellard         return bdrv_pwrite_em(bs, offset, buf1, count1);
67983f64091Sbellard     return drv->bdrv_pwrite(bs, offset, buf1, count1);
68083f64091Sbellard }
68183f64091Sbellard 
68283f64091Sbellard /**
68383f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
68483f64091Sbellard  */
68583f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
68683f64091Sbellard {
68783f64091Sbellard     BlockDriver *drv = bs->drv;
68883f64091Sbellard     if (!drv)
68919cb3738Sbellard         return -ENOMEDIUM;
69083f64091Sbellard     if (!drv->bdrv_truncate)
69183f64091Sbellard         return -ENOTSUP;
69283f64091Sbellard     return drv->bdrv_truncate(bs, offset);
69383f64091Sbellard }
69483f64091Sbellard 
69583f64091Sbellard /**
69683f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
69783f64091Sbellard  */
69883f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
69983f64091Sbellard {
70083f64091Sbellard     BlockDriver *drv = bs->drv;
70183f64091Sbellard     if (!drv)
70219cb3738Sbellard         return -ENOMEDIUM;
70383f64091Sbellard     if (!drv->bdrv_getlength) {
70483f64091Sbellard         /* legacy mode */
70583f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
70683f64091Sbellard     }
70783f64091Sbellard     return drv->bdrv_getlength(bs);
708fc01f7e7Sbellard }
709fc01f7e7Sbellard 
71019cb3738Sbellard /* return 0 as number of sectors if no device present or error */
711fc01f7e7Sbellard void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
712fc01f7e7Sbellard {
71319cb3738Sbellard     int64_t length;
71419cb3738Sbellard     length = bdrv_getlength(bs);
71519cb3738Sbellard     if (length < 0)
71619cb3738Sbellard         length = 0;
71719cb3738Sbellard     else
71819cb3738Sbellard         length = length >> SECTOR_BITS;
71919cb3738Sbellard     *nb_sectors_ptr = length;
720fc01f7e7Sbellard }
721cf98951bSbellard 
722cf98951bSbellard /* force a given boot sector. */
723cf98951bSbellard void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
724cf98951bSbellard {
725cf98951bSbellard     bs->boot_sector_enabled = 1;
726cf98951bSbellard     if (size > 512)
727cf98951bSbellard         size = 512;
728cf98951bSbellard     memcpy(bs->boot_sector_data, data, size);
729cf98951bSbellard     memset(bs->boot_sector_data + size, 0, 512 - size);
730cf98951bSbellard }
731b338082bSbellard 
732b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
733b338082bSbellard                             int cyls, int heads, int secs)
734b338082bSbellard {
735b338082bSbellard     bs->cyls = cyls;
736b338082bSbellard     bs->heads = heads;
737b338082bSbellard     bs->secs = secs;
738b338082bSbellard }
739b338082bSbellard 
740b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
741b338082bSbellard {
742b338082bSbellard     bs->type = type;
743b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
744b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
745b338082bSbellard }
746b338082bSbellard 
74746d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
74846d4767dSbellard {
74946d4767dSbellard     bs->translation = translation;
75046d4767dSbellard }
75146d4767dSbellard 
752b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
753b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
754b338082bSbellard {
755b338082bSbellard     *pcyls = bs->cyls;
756b338082bSbellard     *pheads = bs->heads;
757b338082bSbellard     *psecs = bs->secs;
758b338082bSbellard }
759b338082bSbellard 
760b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
761b338082bSbellard {
762b338082bSbellard     return bs->type;
763b338082bSbellard }
764b338082bSbellard 
76546d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
76646d4767dSbellard {
76746d4767dSbellard     return bs->translation;
76846d4767dSbellard }
76946d4767dSbellard 
770b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
771b338082bSbellard {
772b338082bSbellard     return bs->removable;
773b338082bSbellard }
774b338082bSbellard 
775b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
776b338082bSbellard {
777b338082bSbellard     return bs->read_only;
778b338082bSbellard }
779b338082bSbellard 
78019cb3738Sbellard /* XXX: no longer used */
781b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
782b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
783b338082bSbellard {
784b338082bSbellard     bs->change_cb = change_cb;
785b338082bSbellard     bs->change_opaque = opaque;
786b338082bSbellard }
787b338082bSbellard 
788ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
789ea2384d3Sbellard {
790ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
791ea2384d3Sbellard         return 1;
792ea2384d3Sbellard     return bs->encrypted;
793ea2384d3Sbellard }
794ea2384d3Sbellard 
795ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
796ea2384d3Sbellard {
797ea2384d3Sbellard     int ret;
798ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
799ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
800ea2384d3Sbellard         if (ret < 0)
801ea2384d3Sbellard             return ret;
802ea2384d3Sbellard         if (!bs->encrypted)
803ea2384d3Sbellard             return 0;
804ea2384d3Sbellard     }
805ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
806ea2384d3Sbellard         return -1;
807ea2384d3Sbellard     return bs->drv->bdrv_set_key(bs, key);
808ea2384d3Sbellard }
809ea2384d3Sbellard 
810ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
811ea2384d3Sbellard {
81219cb3738Sbellard     if (!bs->drv) {
813ea2384d3Sbellard         buf[0] = '\0';
814ea2384d3Sbellard     } else {
815ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
816ea2384d3Sbellard     }
817ea2384d3Sbellard }
818ea2384d3Sbellard 
819ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
820ea2384d3Sbellard                          void *opaque)
821ea2384d3Sbellard {
822ea2384d3Sbellard     BlockDriver *drv;
823ea2384d3Sbellard 
824ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
825ea2384d3Sbellard         it(opaque, drv->format_name);
826ea2384d3Sbellard     }
827ea2384d3Sbellard }
828ea2384d3Sbellard 
829b338082bSbellard BlockDriverState *bdrv_find(const char *name)
830b338082bSbellard {
831b338082bSbellard     BlockDriverState *bs;
832b338082bSbellard 
833b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
834b338082bSbellard         if (!strcmp(name, bs->device_name))
835b338082bSbellard             return bs;
836b338082bSbellard     }
837b338082bSbellard     return NULL;
838b338082bSbellard }
839b338082bSbellard 
84081d0912dSbellard void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
84181d0912dSbellard {
84281d0912dSbellard     BlockDriverState *bs;
84381d0912dSbellard 
84481d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
84581d0912dSbellard         it(opaque, bs->device_name);
84681d0912dSbellard     }
84781d0912dSbellard }
84881d0912dSbellard 
849ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
850ea2384d3Sbellard {
851ea2384d3Sbellard     return bs->device_name;
852ea2384d3Sbellard }
853ea2384d3Sbellard 
8547a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
8557a6cba61Spbrook {
8567a6cba61Spbrook     if (bs->drv->bdrv_flush)
8577a6cba61Spbrook         bs->drv->bdrv_flush(bs);
8587a6cba61Spbrook     if (bs->backing_hd)
8597a6cba61Spbrook         bdrv_flush(bs->backing_hd);
8607a6cba61Spbrook }
8617a6cba61Spbrook 
862b338082bSbellard void bdrv_info(void)
863b338082bSbellard {
864b338082bSbellard     BlockDriverState *bs;
865b338082bSbellard 
866b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
867b338082bSbellard         term_printf("%s:", bs->device_name);
868b338082bSbellard         term_printf(" type=");
869b338082bSbellard         switch(bs->type) {
870b338082bSbellard         case BDRV_TYPE_HD:
871b338082bSbellard             term_printf("hd");
872b338082bSbellard             break;
873b338082bSbellard         case BDRV_TYPE_CDROM:
874b338082bSbellard             term_printf("cdrom");
875b338082bSbellard             break;
876b338082bSbellard         case BDRV_TYPE_FLOPPY:
877b338082bSbellard             term_printf("floppy");
878b338082bSbellard             break;
879b338082bSbellard         }
880b338082bSbellard         term_printf(" removable=%d", bs->removable);
881b338082bSbellard         if (bs->removable) {
882b338082bSbellard             term_printf(" locked=%d", bs->locked);
883b338082bSbellard         }
88419cb3738Sbellard         if (bs->drv) {
885fef30743Sths             term_printf(" file=");
886fef30743Sths 	    term_print_filename(bs->filename);
887fef30743Sths             if (bs->backing_file[0] != '\0') {
888fef30743Sths                 term_printf(" backing_file=");
889fef30743Sths 		term_print_filename(bs->backing_file);
890fef30743Sths 	    }
891b338082bSbellard             term_printf(" ro=%d", bs->read_only);
892ea2384d3Sbellard             term_printf(" drv=%s", bs->drv->format_name);
893ea2384d3Sbellard             if (bs->encrypted)
894ea2384d3Sbellard                 term_printf(" encrypted");
895b338082bSbellard         } else {
896b338082bSbellard             term_printf(" [not inserted]");
897b338082bSbellard         }
898b338082bSbellard         term_printf("\n");
899b338082bSbellard     }
900b338082bSbellard }
901ea2384d3Sbellard 
90283f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
90383f64091Sbellard                                char *filename, int filename_size)
90483f64091Sbellard {
90583f64091Sbellard     if (!bs->backing_hd) {
90683f64091Sbellard         pstrcpy(filename, filename_size, "");
90783f64091Sbellard     } else {
90883f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
90983f64091Sbellard     }
91083f64091Sbellard }
91183f64091Sbellard 
912faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
913faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
914faea38e7Sbellard {
915faea38e7Sbellard     BlockDriver *drv = bs->drv;
916faea38e7Sbellard     if (!drv)
91719cb3738Sbellard         return -ENOMEDIUM;
918faea38e7Sbellard     if (!drv->bdrv_write_compressed)
919faea38e7Sbellard         return -ENOTSUP;
920faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
921faea38e7Sbellard }
922faea38e7Sbellard 
923faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
924faea38e7Sbellard {
925faea38e7Sbellard     BlockDriver *drv = bs->drv;
926faea38e7Sbellard     if (!drv)
92719cb3738Sbellard         return -ENOMEDIUM;
928faea38e7Sbellard     if (!drv->bdrv_get_info)
929faea38e7Sbellard         return -ENOTSUP;
930faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
931faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
932faea38e7Sbellard }
933faea38e7Sbellard 
934faea38e7Sbellard /**************************************************************/
935faea38e7Sbellard /* handling of snapshots */
936faea38e7Sbellard 
937faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
938faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
939faea38e7Sbellard {
940faea38e7Sbellard     BlockDriver *drv = bs->drv;
941faea38e7Sbellard     if (!drv)
94219cb3738Sbellard         return -ENOMEDIUM;
943faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
944faea38e7Sbellard         return -ENOTSUP;
945faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
946faea38e7Sbellard }
947faea38e7Sbellard 
948faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
949faea38e7Sbellard                        const char *snapshot_id)
950faea38e7Sbellard {
951faea38e7Sbellard     BlockDriver *drv = bs->drv;
952faea38e7Sbellard     if (!drv)
95319cb3738Sbellard         return -ENOMEDIUM;
954faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
955faea38e7Sbellard         return -ENOTSUP;
956faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
957faea38e7Sbellard }
958faea38e7Sbellard 
959faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
960faea38e7Sbellard {
961faea38e7Sbellard     BlockDriver *drv = bs->drv;
962faea38e7Sbellard     if (!drv)
96319cb3738Sbellard         return -ENOMEDIUM;
964faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
965faea38e7Sbellard         return -ENOTSUP;
966faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
967faea38e7Sbellard }
968faea38e7Sbellard 
969faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
970faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
971faea38e7Sbellard {
972faea38e7Sbellard     BlockDriver *drv = bs->drv;
973faea38e7Sbellard     if (!drv)
97419cb3738Sbellard         return -ENOMEDIUM;
975faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
976faea38e7Sbellard         return -ENOTSUP;
977faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
978faea38e7Sbellard }
979faea38e7Sbellard 
980faea38e7Sbellard #define NB_SUFFIXES 4
981faea38e7Sbellard 
982faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
983faea38e7Sbellard {
984faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
985faea38e7Sbellard     int64_t base;
986faea38e7Sbellard     int i;
987faea38e7Sbellard 
988faea38e7Sbellard     if (size <= 999) {
989faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
990faea38e7Sbellard     } else {
991faea38e7Sbellard         base = 1024;
992faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
993faea38e7Sbellard             if (size < (10 * base)) {
994faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
995faea38e7Sbellard                          (double)size / base,
996faea38e7Sbellard                          suffixes[i]);
997faea38e7Sbellard                 break;
998faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
999faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
1000faea38e7Sbellard                          ((size + (base >> 1)) / base),
1001faea38e7Sbellard                          suffixes[i]);
1002faea38e7Sbellard                 break;
1003faea38e7Sbellard             }
1004faea38e7Sbellard             base = base * 1024;
1005faea38e7Sbellard         }
1006faea38e7Sbellard     }
1007faea38e7Sbellard     return buf;
1008faea38e7Sbellard }
1009faea38e7Sbellard 
1010faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1011faea38e7Sbellard {
1012faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
10133b9f94e1Sbellard #ifdef _WIN32
10143b9f94e1Sbellard     struct tm *ptm;
10153b9f94e1Sbellard #else
1016faea38e7Sbellard     struct tm tm;
10173b9f94e1Sbellard #endif
1018faea38e7Sbellard     time_t ti;
1019faea38e7Sbellard     int64_t secs;
1020faea38e7Sbellard 
1021faea38e7Sbellard     if (!sn) {
1022faea38e7Sbellard         snprintf(buf, buf_size,
1023faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1024faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1025faea38e7Sbellard     } else {
1026faea38e7Sbellard         ti = sn->date_sec;
10273b9f94e1Sbellard #ifdef _WIN32
10283b9f94e1Sbellard         ptm = localtime(&ti);
10293b9f94e1Sbellard         strftime(date_buf, sizeof(date_buf),
10303b9f94e1Sbellard                  "%Y-%m-%d %H:%M:%S", ptm);
10313b9f94e1Sbellard #else
1032faea38e7Sbellard         localtime_r(&ti, &tm);
1033faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1034faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
10353b9f94e1Sbellard #endif
1036faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1037faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1038faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1039faea38e7Sbellard                  (int)(secs / 3600),
1040faea38e7Sbellard                  (int)((secs / 60) % 60),
1041faea38e7Sbellard                  (int)(secs % 60),
1042faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1043faea38e7Sbellard         snprintf(buf, buf_size,
1044faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1045faea38e7Sbellard                  sn->id_str, sn->name,
1046faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1047faea38e7Sbellard                  date_buf,
1048faea38e7Sbellard                  clock_buf);
1049faea38e7Sbellard     }
1050faea38e7Sbellard     return buf;
1051faea38e7Sbellard }
1052faea38e7Sbellard 
105383f64091Sbellard 
1054ea2384d3Sbellard /**************************************************************/
105583f64091Sbellard /* async I/Os */
1056ea2384d3Sbellard 
1057ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
105883f64091Sbellard                                 uint8_t *buf, int nb_sectors,
105983f64091Sbellard                                 BlockDriverCompletionFunc *cb, void *opaque)
1060ea2384d3Sbellard {
106183f64091Sbellard     BlockDriver *drv = bs->drv;
1062ea2384d3Sbellard 
106319cb3738Sbellard     if (!drv)
1064ce1a14dcSpbrook         return NULL;
106583f64091Sbellard 
106683f64091Sbellard     /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
106783f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
106883f64091Sbellard         memcpy(buf, bs->boot_sector_data, 512);
106983f64091Sbellard         sector_num++;
107083f64091Sbellard         nb_sectors--;
107183f64091Sbellard         buf += 512;
1072ea2384d3Sbellard     }
107383f64091Sbellard 
1074ce1a14dcSpbrook     return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
107583f64091Sbellard }
107683f64091Sbellard 
1077ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
107883f64091Sbellard                                  const uint8_t *buf, int nb_sectors,
107983f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
10807674e7bfSbellard {
108183f64091Sbellard     BlockDriver *drv = bs->drv;
108283f64091Sbellard 
108319cb3738Sbellard     if (!drv)
1084ce1a14dcSpbrook         return NULL;
108583f64091Sbellard     if (bs->read_only)
1086ce1a14dcSpbrook         return NULL;
108783f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
108883f64091Sbellard         memcpy(bs->boot_sector_data, buf, 512);
10897674e7bfSbellard     }
109083f64091Sbellard 
1091ce1a14dcSpbrook     return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
109283f64091Sbellard }
109383f64091Sbellard 
109483f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
109583f64091Sbellard {
1096ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
109783f64091Sbellard 
109883f64091Sbellard     drv->bdrv_aio_cancel(acb);
109983f64091Sbellard }
110083f64091Sbellard 
110183f64091Sbellard 
110283f64091Sbellard /**************************************************************/
110383f64091Sbellard /* async block device emulation */
110483f64091Sbellard 
110583f64091Sbellard #ifdef QEMU_TOOL
1106ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1107ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1108ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1109ea2384d3Sbellard {
1110ea2384d3Sbellard     int ret;
1111ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1112ce1a14dcSpbrook     cb(opaque, ret);
1113ce1a14dcSpbrook     return NULL;
1114ea2384d3Sbellard }
1115ea2384d3Sbellard 
1116ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1117ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1118ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1119ea2384d3Sbellard {
1120ea2384d3Sbellard     int ret;
1121ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1122ce1a14dcSpbrook     cb(opaque, ret);
1123ce1a14dcSpbrook     return NULL;
1124ea2384d3Sbellard }
1125ea2384d3Sbellard 
112683f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1127ea2384d3Sbellard {
1128ea2384d3Sbellard }
1129beac80cdSbellard #else
113083f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1131beac80cdSbellard {
1132ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1133ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
1134ce1a14dcSpbrook     qemu_aio_release(acb);
1135beac80cdSbellard }
1136beac80cdSbellard 
1137ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1138ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1139ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1140ea2384d3Sbellard {
1141ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
114283f64091Sbellard     int ret;
114383f64091Sbellard 
1144ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1145ce1a14dcSpbrook     if (!acb->bh)
1146ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1147ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1148ce1a14dcSpbrook     acb->ret = ret;
1149ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1150ce1a14dcSpbrook     return &acb->common;
11517a6cba61Spbrook }
11527a6cba61Spbrook 
1153ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1154ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1155ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
115683f64091Sbellard {
1157ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
115883f64091Sbellard     int ret;
115983f64091Sbellard 
1160ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1161ce1a14dcSpbrook     if (!acb->bh)
1162ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1163ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1164ce1a14dcSpbrook     acb->ret = ret;
1165ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1166ce1a14dcSpbrook     return &acb->common;
116783f64091Sbellard }
116883f64091Sbellard 
1169ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
117083f64091Sbellard {
1171ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1172ce1a14dcSpbrook     qemu_bh_cancel(acb->bh);
1173ce1a14dcSpbrook     qemu_aio_release(acb);
117483f64091Sbellard }
117583f64091Sbellard #endif /* !QEMU_TOOL */
117683f64091Sbellard 
117783f64091Sbellard /**************************************************************/
117883f64091Sbellard /* sync block device emulation */
117983f64091Sbellard 
118083f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
118183f64091Sbellard {
118283f64091Sbellard     *(int *)opaque = ret;
118383f64091Sbellard }
118483f64091Sbellard 
118583f64091Sbellard #define NOT_DONE 0x7fffffff
118683f64091Sbellard 
118783f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
118883f64091Sbellard                         uint8_t *buf, int nb_sectors)
118983f64091Sbellard {
1190ce1a14dcSpbrook     int async_ret;
1191ce1a14dcSpbrook     BlockDriverAIOCB *acb;
119283f64091Sbellard 
119383f64091Sbellard     async_ret = NOT_DONE;
119483f64091Sbellard     qemu_aio_wait_start();
1195ce1a14dcSpbrook     acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
119683f64091Sbellard                         bdrv_rw_em_cb, &async_ret);
1197ce1a14dcSpbrook     if (acb == NULL) {
119883f64091Sbellard         qemu_aio_wait_end();
1199ce1a14dcSpbrook         return -1;
120083f64091Sbellard     }
120183f64091Sbellard     while (async_ret == NOT_DONE) {
120283f64091Sbellard         qemu_aio_wait();
120383f64091Sbellard     }
120483f64091Sbellard     qemu_aio_wait_end();
120583f64091Sbellard     return async_ret;
120683f64091Sbellard }
120783f64091Sbellard 
120883f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
120983f64091Sbellard                          const uint8_t *buf, int nb_sectors)
121083f64091Sbellard {
1211ce1a14dcSpbrook     int async_ret;
1212ce1a14dcSpbrook     BlockDriverAIOCB *acb;
121383f64091Sbellard 
121483f64091Sbellard     async_ret = NOT_DONE;
121583f64091Sbellard     qemu_aio_wait_start();
1216ce1a14dcSpbrook     acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
121783f64091Sbellard                          bdrv_rw_em_cb, &async_ret);
1218ce1a14dcSpbrook     if (acb == NULL) {
121983f64091Sbellard         qemu_aio_wait_end();
1220ce1a14dcSpbrook         return -1;
122183f64091Sbellard     }
122283f64091Sbellard     while (async_ret == NOT_DONE) {
122383f64091Sbellard         qemu_aio_wait();
122483f64091Sbellard     }
122583f64091Sbellard     qemu_aio_wait_end();
122683f64091Sbellard     return async_ret;
122783f64091Sbellard }
1228ea2384d3Sbellard 
1229ea2384d3Sbellard void bdrv_init(void)
1230ea2384d3Sbellard {
1231ea2384d3Sbellard     bdrv_register(&bdrv_raw);
123219cb3738Sbellard     bdrv_register(&bdrv_host_device);
1233ea2384d3Sbellard #ifndef _WIN32
1234ea2384d3Sbellard     bdrv_register(&bdrv_cow);
1235ea2384d3Sbellard #endif
1236ea2384d3Sbellard     bdrv_register(&bdrv_qcow);
1237ea2384d3Sbellard     bdrv_register(&bdrv_vmdk);
12383c56521bSbellard     bdrv_register(&bdrv_cloop);
1239585d0ed9Sbellard     bdrv_register(&bdrv_dmg);
1240a8753c34Sbellard     bdrv_register(&bdrv_bochs);
12416a0f9e82Sbellard     bdrv_register(&bdrv_vpc);
1242712e7874Sbellard     bdrv_register(&bdrv_vvfat);
1243faea38e7Sbellard     bdrv_register(&bdrv_qcow2);
1244ea2384d3Sbellard }
1245ce1a14dcSpbrook 
1246ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1247ce1a14dcSpbrook                    void *opaque)
1248ce1a14dcSpbrook {
1249ce1a14dcSpbrook     BlockDriver *drv;
1250ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1251ce1a14dcSpbrook 
1252ce1a14dcSpbrook     drv = bs->drv;
1253ce1a14dcSpbrook     if (drv->free_aiocb) {
1254ce1a14dcSpbrook         acb = drv->free_aiocb;
1255ce1a14dcSpbrook         drv->free_aiocb = acb->next;
1256ce1a14dcSpbrook     } else {
1257ce1a14dcSpbrook         acb = qemu_mallocz(drv->aiocb_size);
1258ce1a14dcSpbrook         if (!acb)
1259ce1a14dcSpbrook             return NULL;
1260ce1a14dcSpbrook     }
1261ce1a14dcSpbrook     acb->bs = bs;
1262ce1a14dcSpbrook     acb->cb = cb;
1263ce1a14dcSpbrook     acb->opaque = opaque;
1264ce1a14dcSpbrook     return acb;
1265ce1a14dcSpbrook }
1266ce1a14dcSpbrook 
1267ce1a14dcSpbrook void qemu_aio_release(void *p)
1268ce1a14dcSpbrook {
1269ce1a14dcSpbrook     BlockDriverAIOCB *acb = p;
1270ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
1271ce1a14dcSpbrook     acb->next = drv->free_aiocb;
1272ce1a14dcSpbrook     drv->free_aiocb = acb;
1273ce1a14dcSpbrook }
127419cb3738Sbellard 
127519cb3738Sbellard /**************************************************************/
127619cb3738Sbellard /* removable device support */
127719cb3738Sbellard 
127819cb3738Sbellard /**
127919cb3738Sbellard  * Return TRUE if the media is present
128019cb3738Sbellard  */
128119cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
128219cb3738Sbellard {
128319cb3738Sbellard     BlockDriver *drv = bs->drv;
128419cb3738Sbellard     int ret;
128519cb3738Sbellard     if (!drv)
128619cb3738Sbellard         return 0;
128719cb3738Sbellard     if (!drv->bdrv_is_inserted)
128819cb3738Sbellard         return 1;
128919cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
129019cb3738Sbellard     return ret;
129119cb3738Sbellard }
129219cb3738Sbellard 
129319cb3738Sbellard /**
129419cb3738Sbellard  * Return TRUE if the media changed since the last call to this
129519cb3738Sbellard  * function. It is currently only used for floppy disks
129619cb3738Sbellard  */
129719cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
129819cb3738Sbellard {
129919cb3738Sbellard     BlockDriver *drv = bs->drv;
130019cb3738Sbellard     int ret;
130119cb3738Sbellard 
130219cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
130319cb3738Sbellard         ret = -ENOTSUP;
130419cb3738Sbellard     else
130519cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
130619cb3738Sbellard     if (ret == -ENOTSUP)
130719cb3738Sbellard         ret = bs->media_changed;
130819cb3738Sbellard     bs->media_changed = 0;
130919cb3738Sbellard     return ret;
131019cb3738Sbellard }
131119cb3738Sbellard 
131219cb3738Sbellard /**
131319cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
131419cb3738Sbellard  */
131519cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag)
131619cb3738Sbellard {
131719cb3738Sbellard     BlockDriver *drv = bs->drv;
131819cb3738Sbellard     int ret;
131919cb3738Sbellard 
132019cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
132119cb3738Sbellard         ret = -ENOTSUP;
132219cb3738Sbellard     } else {
132319cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
132419cb3738Sbellard     }
132519cb3738Sbellard     if (ret == -ENOTSUP) {
132619cb3738Sbellard         if (eject_flag)
132719cb3738Sbellard             bdrv_close(bs);
132819cb3738Sbellard     }
132919cb3738Sbellard }
133019cb3738Sbellard 
133119cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
133219cb3738Sbellard {
133319cb3738Sbellard     return bs->locked;
133419cb3738Sbellard }
133519cb3738Sbellard 
133619cb3738Sbellard /**
133719cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
133819cb3738Sbellard  * to eject it manually).
133919cb3738Sbellard  */
134019cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
134119cb3738Sbellard {
134219cb3738Sbellard     BlockDriver *drv = bs->drv;
134319cb3738Sbellard 
134419cb3738Sbellard     bs->locked = locked;
134519cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
134619cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
134719cb3738Sbellard     }
134819cb3738Sbellard }
1349