xref: /openbmc/qemu/block.c (revision 19cb37389f4641d48803f0c5d72708749cbcf318)
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 #ifdef _WIN32
6083f64091Sbellard #define PATH_SEP '\\'
6183f64091Sbellard #else
6283f64091Sbellard #define PATH_SEP '/'
633b0d4f61Sbellard #endif
643b0d4f61Sbellard 
6583f64091Sbellard int path_is_absolute(const char *path)
6683f64091Sbellard {
6783f64091Sbellard     const char *p;
6883f64091Sbellard     p = strchr(path, ':');
6983f64091Sbellard     if (p)
7083f64091Sbellard         p++;
7183f64091Sbellard     else
7283f64091Sbellard         p = path;
7383f64091Sbellard     return (*p == PATH_SEP);
7483f64091Sbellard }
7583f64091Sbellard 
7683f64091Sbellard /* if filename is absolute, just copy it to dest. Otherwise, build a
7783f64091Sbellard    path to it by considering it is relative to base_path. URL are
7883f64091Sbellard    supported. */
7983f64091Sbellard void path_combine(char *dest, int dest_size,
8083f64091Sbellard                   const char *base_path,
8183f64091Sbellard                   const char *filename)
8283f64091Sbellard {
8383f64091Sbellard     const char *p, *p1;
8483f64091Sbellard     int len;
8583f64091Sbellard 
8683f64091Sbellard     if (dest_size <= 0)
8783f64091Sbellard         return;
8883f64091Sbellard     if (path_is_absolute(filename)) {
8983f64091Sbellard         pstrcpy(dest, dest_size, filename);
9083f64091Sbellard     } else {
9183f64091Sbellard         p = strchr(base_path, ':');
9283f64091Sbellard         if (p)
9383f64091Sbellard             p++;
9483f64091Sbellard         else
9583f64091Sbellard             p = base_path;
9683f64091Sbellard         p1 = strrchr(base_path, PATH_SEP);
9783f64091Sbellard         if (p1)
9883f64091Sbellard             p1++;
9983f64091Sbellard         else
10083f64091Sbellard             p1 = base_path;
10183f64091Sbellard         if (p1 > p)
10283f64091Sbellard             p = p1;
10383f64091Sbellard         len = p - base_path;
10483f64091Sbellard         if (len > dest_size - 1)
10583f64091Sbellard             len = dest_size - 1;
10683f64091Sbellard         memcpy(dest, base_path, len);
10783f64091Sbellard         dest[len] = '\0';
10883f64091Sbellard         pstrcat(dest, dest_size, filename);
10983f64091Sbellard     }
11083f64091Sbellard }
11183f64091Sbellard 
11283f64091Sbellard 
113ea2384d3Sbellard void bdrv_register(BlockDriver *bdrv)
114ea2384d3Sbellard {
115ce1a14dcSpbrook     if (!bdrv->bdrv_aio_read) {
11683f64091Sbellard         /* add AIO emulation layer */
11783f64091Sbellard         bdrv->bdrv_aio_read = bdrv_aio_read_em;
11883f64091Sbellard         bdrv->bdrv_aio_write = bdrv_aio_write_em;
11983f64091Sbellard         bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
12090765429Sbellard         bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
12183f64091Sbellard     } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
12283f64091Sbellard         /* add synchronous IO emulation layer */
12383f64091Sbellard         bdrv->bdrv_read = bdrv_read_em;
12483f64091Sbellard         bdrv->bdrv_write = bdrv_write_em;
12583f64091Sbellard     }
126ea2384d3Sbellard     bdrv->next = first_drv;
127ea2384d3Sbellard     first_drv = bdrv;
128ea2384d3Sbellard }
129b338082bSbellard 
130b338082bSbellard /* create a new block device (by default it is empty) */
131b338082bSbellard BlockDriverState *bdrv_new(const char *device_name)
132fc01f7e7Sbellard {
133b338082bSbellard     BlockDriverState **pbs, *bs;
134b338082bSbellard 
135b338082bSbellard     bs = qemu_mallocz(sizeof(BlockDriverState));
136b338082bSbellard     if(!bs)
137b338082bSbellard         return NULL;
138b338082bSbellard     pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
139ea2384d3Sbellard     if (device_name[0] != '\0') {
140b338082bSbellard         /* insert at the end */
141b338082bSbellard         pbs = &bdrv_first;
142b338082bSbellard         while (*pbs != NULL)
143b338082bSbellard             pbs = &(*pbs)->next;
144b338082bSbellard         *pbs = bs;
145ea2384d3Sbellard     }
146b338082bSbellard     return bs;
147b338082bSbellard }
148b338082bSbellard 
149ea2384d3Sbellard BlockDriver *bdrv_find_format(const char *format_name)
150ea2384d3Sbellard {
151ea2384d3Sbellard     BlockDriver *drv1;
152ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
153ea2384d3Sbellard         if (!strcmp(drv1->format_name, format_name))
154ea2384d3Sbellard             return drv1;
155ea2384d3Sbellard     }
156ea2384d3Sbellard     return NULL;
157ea2384d3Sbellard }
158ea2384d3Sbellard 
159ea2384d3Sbellard int bdrv_create(BlockDriver *drv,
160ea2384d3Sbellard                 const char *filename, int64_t size_in_sectors,
161ea2384d3Sbellard                 const char *backing_file, int flags)
162ea2384d3Sbellard {
163ea2384d3Sbellard     if (!drv->bdrv_create)
164ea2384d3Sbellard         return -ENOTSUP;
165ea2384d3Sbellard     return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
166ea2384d3Sbellard }
167ea2384d3Sbellard 
168d5249393Sbellard #ifdef _WIN32
16995389c86Sbellard void get_tmp_filename(char *filename, int size)
170d5249393Sbellard {
17183f64091Sbellard     tmpnam(filename);
172d5249393Sbellard }
173d5249393Sbellard #else
17495389c86Sbellard void get_tmp_filename(char *filename, int size)
175ea2384d3Sbellard {
176ea2384d3Sbellard     int fd;
177d5249393Sbellard     /* XXX: race condition possible */
178ea2384d3Sbellard     pstrcpy(filename, size, "/tmp/vl.XXXXXX");
179ea2384d3Sbellard     fd = mkstemp(filename);
180ea2384d3Sbellard     close(fd);
181ea2384d3Sbellard }
182d5249393Sbellard #endif
183ea2384d3Sbellard 
184*19cb3738Sbellard #ifdef _WIN32
185*19cb3738Sbellard static int is_windows_drive(const char *filename)
186*19cb3738Sbellard {
187*19cb3738Sbellard     if (((filename[0] >= 'a' && filename[0] <= 'z') ||
188*19cb3738Sbellard          (filename[0] >= 'A' && filename[0] <= 'Z')) &&
189*19cb3738Sbellard         filename[1] == ':' && filename[2] == '\0')
190*19cb3738Sbellard         return 1;
191*19cb3738Sbellard     if (strstart(filename, "\\\\.\\", NULL) ||
192*19cb3738Sbellard         strstart(filename, "//./", NULL))
193*19cb3738Sbellard         return 1;
194*19cb3738Sbellard     return 0;
195*19cb3738Sbellard }
196*19cb3738Sbellard #endif
197*19cb3738Sbellard 
19883f64091Sbellard static BlockDriver *find_protocol(const char *filename)
19983f64091Sbellard {
20083f64091Sbellard     BlockDriver *drv1;
20183f64091Sbellard     char protocol[128];
20283f64091Sbellard     int len;
20383f64091Sbellard     const char *p;
204*19cb3738Sbellard 
205*19cb3738Sbellard #ifdef _WIN32
206*19cb3738Sbellard     if (is_windows_drive(filename))
207*19cb3738Sbellard         return &bdrv_raw;
208*19cb3738Sbellard #endif
20983f64091Sbellard     p = strchr(filename, ':');
21083f64091Sbellard     if (!p)
21183f64091Sbellard         return &bdrv_raw;
21283f64091Sbellard     len = p - filename;
21383f64091Sbellard     if (len > sizeof(protocol) - 1)
21483f64091Sbellard         len = sizeof(protocol) - 1;
21583f64091Sbellard     memcpy(protocol, filename, len);
21683f64091Sbellard     protocol[len] = '\0';
21783f64091Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
21883f64091Sbellard         if (drv1->protocol_name &&
21983f64091Sbellard             !strcmp(drv1->protocol_name, protocol))
22083f64091Sbellard             return drv1;
22183f64091Sbellard     }
22283f64091Sbellard     return NULL;
22383f64091Sbellard }
22483f64091Sbellard 
2257674e7bfSbellard /* XXX: force raw format if block or character device ? It would
2267674e7bfSbellard    simplify the BSD case */
227ea2384d3Sbellard static BlockDriver *find_image_format(const char *filename)
228ea2384d3Sbellard {
22983f64091Sbellard     int ret, score, score_max;
230ea2384d3Sbellard     BlockDriver *drv1, *drv;
23183f64091Sbellard     uint8_t buf[2048];
23283f64091Sbellard     BlockDriverState *bs;
233ea2384d3Sbellard 
234*19cb3738Sbellard     /* detect host devices. By convention, /dev/cdrom[N] is always
235*19cb3738Sbellard        recognized as a host CDROM */
236*19cb3738Sbellard     if (strstart(filename, "/dev/cdrom", NULL))
237*19cb3738Sbellard         return &bdrv_host_device;
238*19cb3738Sbellard #ifdef _WIN32
239*19cb3738Sbellard     if (is_windows_drive(filename))
240*19cb3738Sbellard         return &bdrv_host_device;
241*19cb3738Sbellard #else
242*19cb3738Sbellard     {
243*19cb3738Sbellard         struct stat st;
244*19cb3738Sbellard         if (stat(filename, &st) >= 0 &&
245*19cb3738Sbellard             (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
246*19cb3738Sbellard             return &bdrv_host_device;
247*19cb3738Sbellard         }
248*19cb3738Sbellard     }
249*19cb3738Sbellard #endif
250*19cb3738Sbellard 
25183f64091Sbellard     drv = find_protocol(filename);
252*19cb3738Sbellard     /* no need to test disk image formats for vvfat */
25383f64091Sbellard     if (drv == &bdrv_vvfat)
25483f64091Sbellard         return drv;
25583f64091Sbellard 
25683f64091Sbellard     ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
25783f64091Sbellard     if (ret < 0)
2587674e7bfSbellard         return NULL;
25983f64091Sbellard     ret = bdrv_pread(bs, 0, buf, sizeof(buf));
26083f64091Sbellard     bdrv_delete(bs);
261ea2384d3Sbellard     if (ret < 0) {
262ea2384d3Sbellard         return NULL;
263ea2384d3Sbellard     }
264ea2384d3Sbellard 
265ea2384d3Sbellard     score_max = 0;
266ea2384d3Sbellard     for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
26783f64091Sbellard         if (drv1->bdrv_probe) {
268ea2384d3Sbellard             score = drv1->bdrv_probe(buf, ret, filename);
269ea2384d3Sbellard             if (score > score_max) {
270ea2384d3Sbellard                 score_max = score;
271ea2384d3Sbellard                 drv = drv1;
272ea2384d3Sbellard             }
273ea2384d3Sbellard         }
27483f64091Sbellard     }
275ea2384d3Sbellard     return drv;
276ea2384d3Sbellard }
277ea2384d3Sbellard 
27883f64091Sbellard int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
279b338082bSbellard {
28083f64091Sbellard     BlockDriverState *bs;
28183f64091Sbellard     int ret;
2823b0d4f61Sbellard 
28383f64091Sbellard     bs = bdrv_new("");
28483f64091Sbellard     if (!bs)
28583f64091Sbellard         return -ENOMEM;
28683f64091Sbellard     ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
28783f64091Sbellard     if (ret < 0) {
28883f64091Sbellard         bdrv_delete(bs);
28983f64091Sbellard         return ret;
2903b0d4f61Sbellard     }
29183f64091Sbellard     *pbs = bs;
29283f64091Sbellard     return 0;
2933b0d4f61Sbellard }
2943b0d4f61Sbellard 
29583f64091Sbellard int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
29683f64091Sbellard {
29783f64091Sbellard     return bdrv_open2(bs, filename, flags, NULL);
298ea2384d3Sbellard }
299ea2384d3Sbellard 
30083f64091Sbellard int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
301ea2384d3Sbellard                BlockDriver *drv)
302ea2384d3Sbellard {
30383f64091Sbellard     int ret, open_flags;
304ea2384d3Sbellard     char tmp_filename[1024];
30583f64091Sbellard     char backing_filename[1024];
306fc01f7e7Sbellard 
3070849bf08Sbellard     bs->read_only = 0;
308ea2384d3Sbellard     bs->is_temporary = 0;
309ea2384d3Sbellard     bs->encrypted = 0;
31033e3963eSbellard 
31183f64091Sbellard     if (flags & BDRV_O_SNAPSHOT) {
312ea2384d3Sbellard         BlockDriverState *bs1;
313ea2384d3Sbellard         int64_t total_size;
31433e3963eSbellard 
315ea2384d3Sbellard         /* if snapshot, we create a temporary backing file and open it
316ea2384d3Sbellard            instead of opening 'filename' directly */
317ea2384d3Sbellard 
318ea2384d3Sbellard         /* if there is a backing file, use it */
319ea2384d3Sbellard         bs1 = bdrv_new("");
320ea2384d3Sbellard         if (!bs1) {
32183f64091Sbellard             return -ENOMEM;
322ea2384d3Sbellard         }
323ea2384d3Sbellard         if (bdrv_open(bs1, filename, 0) < 0) {
324ea2384d3Sbellard             bdrv_delete(bs1);
325ea2384d3Sbellard             return -1;
326ea2384d3Sbellard         }
32783f64091Sbellard         total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
328ea2384d3Sbellard         bdrv_delete(bs1);
329ea2384d3Sbellard 
330ea2384d3Sbellard         get_tmp_filename(tmp_filename, sizeof(tmp_filename));
331d15a771dSbellard         if (bdrv_create(&bdrv_qcow2, tmp_filename,
332ea2384d3Sbellard                         total_size, filename, 0) < 0) {
333ea2384d3Sbellard             return -1;
334ea2384d3Sbellard         }
335ea2384d3Sbellard         filename = tmp_filename;
336ea2384d3Sbellard         bs->is_temporary = 1;
337ea2384d3Sbellard     }
338ea2384d3Sbellard 
339ea2384d3Sbellard     pstrcpy(bs->filename, sizeof(bs->filename), filename);
34083f64091Sbellard     if (flags & BDRV_O_FILE) {
34183f64091Sbellard         drv = find_protocol(filename);
34283f64091Sbellard         if (!drv)
34383f64091Sbellard             return -ENOENT;
34483f64091Sbellard     } else {
345ea2384d3Sbellard         if (!drv) {
346ea2384d3Sbellard             drv = find_image_format(filename);
347ea2384d3Sbellard             if (!drv)
348ea2384d3Sbellard                 return -1;
349ea2384d3Sbellard         }
35083f64091Sbellard     }
351ea2384d3Sbellard     bs->drv = drv;
352ea2384d3Sbellard     bs->opaque = qemu_mallocz(drv->instance_size);
353ea2384d3Sbellard     if (bs->opaque == NULL && drv->instance_size > 0)
354ea2384d3Sbellard         return -1;
35583f64091Sbellard     /* Note: for compatibility, we open disk image files as RDWR, and
35683f64091Sbellard        RDONLY as fallback */
35783f64091Sbellard     if (!(flags & BDRV_O_FILE))
35883f64091Sbellard         open_flags = BDRV_O_RDWR;
35983f64091Sbellard     else
36083f64091Sbellard         open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
36183f64091Sbellard     ret = drv->bdrv_open(bs, filename, open_flags);
36283f64091Sbellard     if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
36383f64091Sbellard         ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
36483f64091Sbellard         bs->read_only = 1;
36583f64091Sbellard     }
366ea2384d3Sbellard     if (ret < 0) {
367ea2384d3Sbellard         qemu_free(bs->opaque);
36883f64091Sbellard         return ret;
369ea2384d3Sbellard     }
370d15a771dSbellard     if (drv->bdrv_getlength) {
371d15a771dSbellard         bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
372d15a771dSbellard     }
373ea2384d3Sbellard #ifndef _WIN32
374ea2384d3Sbellard     if (bs->is_temporary) {
375ea2384d3Sbellard         unlink(filename);
37633e3963eSbellard     }
37767b915a5Sbellard #endif
37883f64091Sbellard     if (bs->backing_file[0] != '\0') {
379ea2384d3Sbellard         /* if there is a backing file, use it */
380ea2384d3Sbellard         bs->backing_hd = bdrv_new("");
381ea2384d3Sbellard         if (!bs->backing_hd) {
382ea2384d3Sbellard         fail:
383ea2384d3Sbellard             bdrv_close(bs);
384ea2384d3Sbellard             return -1;
385ea2384d3Sbellard         }
38683f64091Sbellard         path_combine(backing_filename, sizeof(backing_filename),
38783f64091Sbellard                      filename, bs->backing_file);
38883f64091Sbellard         if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
389ea2384d3Sbellard             goto fail;
390ea2384d3Sbellard     }
39133e3963eSbellard 
392b338082bSbellard     /* call the change callback */
393*19cb3738Sbellard     bs->media_changed = 1;
394b338082bSbellard     if (bs->change_cb)
395b338082bSbellard         bs->change_cb(bs->change_opaque);
396b338082bSbellard 
397b338082bSbellard     return 0;
398fc01f7e7Sbellard }
399fc01f7e7Sbellard 
400fc01f7e7Sbellard void bdrv_close(BlockDriverState *bs)
401fc01f7e7Sbellard {
402*19cb3738Sbellard     if (bs->drv) {
403ea2384d3Sbellard         if (bs->backing_hd)
404ea2384d3Sbellard             bdrv_delete(bs->backing_hd);
405ea2384d3Sbellard         bs->drv->bdrv_close(bs);
406ea2384d3Sbellard         qemu_free(bs->opaque);
407ea2384d3Sbellard #ifdef _WIN32
408ea2384d3Sbellard         if (bs->is_temporary) {
409ea2384d3Sbellard             unlink(bs->filename);
410ea2384d3Sbellard         }
41167b915a5Sbellard #endif
412ea2384d3Sbellard         bs->opaque = NULL;
413ea2384d3Sbellard         bs->drv = NULL;
414b338082bSbellard 
415b338082bSbellard         /* call the change callback */
416*19cb3738Sbellard         bs->media_changed = 1;
417b338082bSbellard         if (bs->change_cb)
418b338082bSbellard             bs->change_cb(bs->change_opaque);
419b338082bSbellard     }
420b338082bSbellard }
421b338082bSbellard 
422b338082bSbellard void bdrv_delete(BlockDriverState *bs)
423b338082bSbellard {
424ea2384d3Sbellard     /* XXX: remove the driver list */
425b338082bSbellard     bdrv_close(bs);
426b338082bSbellard     qemu_free(bs);
427fc01f7e7Sbellard }
428fc01f7e7Sbellard 
42933e3963eSbellard /* commit COW file into the raw image */
43033e3963eSbellard int bdrv_commit(BlockDriverState *bs)
43133e3963eSbellard {
432*19cb3738Sbellard     BlockDriver *drv = bs->drv;
43383f64091Sbellard     int64_t i, total_sectors;
434ea2384d3Sbellard     int n, j;
435ea2384d3Sbellard     unsigned char sector[512];
43633e3963eSbellard 
437*19cb3738Sbellard     if (!drv)
438*19cb3738Sbellard         return -ENOMEDIUM;
43933e3963eSbellard 
44033e3963eSbellard     if (bs->read_only) {
441ea2384d3Sbellard 	return -EACCES;
44233e3963eSbellard     }
44333e3963eSbellard 
444ea2384d3Sbellard     if (!bs->backing_hd) {
445ea2384d3Sbellard 	return -ENOTSUP;
446ea2384d3Sbellard     }
447ea2384d3Sbellard 
44883f64091Sbellard     total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
44983f64091Sbellard     for (i = 0; i < total_sectors;) {
450*19cb3738Sbellard         if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
451ea2384d3Sbellard             for(j = 0; j < n; j++) {
45233e3963eSbellard                 if (bdrv_read(bs, i, sector, 1) != 0) {
453ea2384d3Sbellard                     return -EIO;
45433e3963eSbellard                 }
45533e3963eSbellard 
456ea2384d3Sbellard                 if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
457ea2384d3Sbellard                     return -EIO;
45833e3963eSbellard                 }
459ea2384d3Sbellard                 i++;
460ea2384d3Sbellard 	    }
461ea2384d3Sbellard 	} else {
462ea2384d3Sbellard             i += n;
46333e3963eSbellard         }
46433e3963eSbellard     }
46595389c86Sbellard 
466*19cb3738Sbellard     if (drv->bdrv_make_empty)
467*19cb3738Sbellard 	return drv->bdrv_make_empty(bs);
46895389c86Sbellard 
46933e3963eSbellard     return 0;
47033e3963eSbellard }
47133e3963eSbellard 
472*19cb3738Sbellard /* return < 0 if error. See bdrv_write() for the return codes */
473fc01f7e7Sbellard int bdrv_read(BlockDriverState *bs, int64_t sector_num,
474fc01f7e7Sbellard               uint8_t *buf, int nb_sectors)
475fc01f7e7Sbellard {
476ea2384d3Sbellard     BlockDriver *drv = bs->drv;
477fc01f7e7Sbellard 
478*19cb3738Sbellard     if (!drv)
479*19cb3738Sbellard         return -ENOMEDIUM;
480b338082bSbellard 
48183f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
482cf98951bSbellard             memcpy(buf, bs->boot_sector_data, 512);
48383f64091Sbellard         sector_num++;
48483f64091Sbellard         nb_sectors--;
48583f64091Sbellard         buf += 512;
48683f64091Sbellard         if (nb_sectors == 0)
487fc01f7e7Sbellard             return 0;
488fc01f7e7Sbellard     }
48983f64091Sbellard     if (drv->bdrv_pread) {
49083f64091Sbellard         int ret, len;
49183f64091Sbellard         len = nb_sectors * 512;
49283f64091Sbellard         ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
49383f64091Sbellard         if (ret < 0)
49483f64091Sbellard             return ret;
49583f64091Sbellard         else if (ret != len)
496*19cb3738Sbellard             return -EINVAL;
49783f64091Sbellard         else
49883f64091Sbellard             return 0;
49983f64091Sbellard     } else {
50083f64091Sbellard         return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
50183f64091Sbellard     }
50283f64091Sbellard }
503fc01f7e7Sbellard 
504*19cb3738Sbellard /* Return < 0 if error. Important errors are:
505*19cb3738Sbellard   -EIO         generic I/O error (may happen for all errors)
506*19cb3738Sbellard   -ENOMEDIUM   No media inserted.
507*19cb3738Sbellard   -EINVAL      Invalid sector number or nb_sectors
508*19cb3738Sbellard   -EACCES      Trying to write a read-only device
509*19cb3738Sbellard */
510fc01f7e7Sbellard int bdrv_write(BlockDriverState *bs, int64_t sector_num,
511fc01f7e7Sbellard                const uint8_t *buf, int nb_sectors)
512fc01f7e7Sbellard {
51383f64091Sbellard     BlockDriver *drv = bs->drv;
514*19cb3738Sbellard     if (!bs->drv)
515*19cb3738Sbellard         return -ENOMEDIUM;
5160849bf08Sbellard     if (bs->read_only)
517*19cb3738Sbellard         return -EACCES;
51879639d42Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
51979639d42Sbellard         memcpy(bs->boot_sector_data, buf, 512);
52079639d42Sbellard     }
52183f64091Sbellard     if (drv->bdrv_pwrite) {
52283f64091Sbellard         int ret, len;
52383f64091Sbellard         len = nb_sectors * 512;
52483f64091Sbellard         ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
52583f64091Sbellard         if (ret < 0)
52683f64091Sbellard             return ret;
52783f64091Sbellard         else if (ret != len)
52883f64091Sbellard             return -EIO;
52983f64091Sbellard         else
53083f64091Sbellard             return 0;
53183f64091Sbellard     } else {
53283f64091Sbellard         return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
53383f64091Sbellard     }
53483f64091Sbellard }
53583f64091Sbellard 
53683f64091Sbellard static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
537faea38e7Sbellard                          uint8_t *buf, int count1)
53883f64091Sbellard {
53983f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
54083f64091Sbellard     int len, nb_sectors, count;
54183f64091Sbellard     int64_t sector_num;
54283f64091Sbellard 
54383f64091Sbellard     count = count1;
54483f64091Sbellard     /* first read to align to sector start */
54583f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
54683f64091Sbellard     if (len > count)
54783f64091Sbellard         len = count;
54883f64091Sbellard     sector_num = offset >> SECTOR_BITS;
54983f64091Sbellard     if (len > 0) {
55083f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
55183f64091Sbellard             return -EIO;
55283f64091Sbellard         memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
55383f64091Sbellard         count -= len;
55483f64091Sbellard         if (count == 0)
55583f64091Sbellard             return count1;
55683f64091Sbellard         sector_num++;
55783f64091Sbellard         buf += len;
55883f64091Sbellard     }
55983f64091Sbellard 
56083f64091Sbellard     /* read the sectors "in place" */
56183f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
56283f64091Sbellard     if (nb_sectors > 0) {
56383f64091Sbellard         if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
56483f64091Sbellard             return -EIO;
56583f64091Sbellard         sector_num += nb_sectors;
56683f64091Sbellard         len = nb_sectors << SECTOR_BITS;
56783f64091Sbellard         buf += len;
56883f64091Sbellard         count -= len;
56983f64091Sbellard     }
57083f64091Sbellard 
57183f64091Sbellard     /* add data from the last sector */
57283f64091Sbellard     if (count > 0) {
57383f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
57483f64091Sbellard             return -EIO;
57583f64091Sbellard         memcpy(buf, tmp_buf, count);
57683f64091Sbellard     }
57783f64091Sbellard     return count1;
57883f64091Sbellard }
57983f64091Sbellard 
58083f64091Sbellard static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
581faea38e7Sbellard                           const uint8_t *buf, int count1)
58283f64091Sbellard {
58383f64091Sbellard     uint8_t tmp_buf[SECTOR_SIZE];
58483f64091Sbellard     int len, nb_sectors, count;
58583f64091Sbellard     int64_t sector_num;
58683f64091Sbellard 
58783f64091Sbellard     count = count1;
58883f64091Sbellard     /* first write to align to sector start */
58983f64091Sbellard     len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
59083f64091Sbellard     if (len > count)
59183f64091Sbellard         len = count;
59283f64091Sbellard     sector_num = offset >> SECTOR_BITS;
59383f64091Sbellard     if (len > 0) {
59483f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
59583f64091Sbellard             return -EIO;
59683f64091Sbellard         memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
59783f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
59883f64091Sbellard             return -EIO;
59983f64091Sbellard         count -= len;
60083f64091Sbellard         if (count == 0)
60183f64091Sbellard             return count1;
60283f64091Sbellard         sector_num++;
60383f64091Sbellard         buf += len;
60483f64091Sbellard     }
60583f64091Sbellard 
60683f64091Sbellard     /* write the sectors "in place" */
60783f64091Sbellard     nb_sectors = count >> SECTOR_BITS;
60883f64091Sbellard     if (nb_sectors > 0) {
60983f64091Sbellard         if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
61083f64091Sbellard             return -EIO;
61183f64091Sbellard         sector_num += nb_sectors;
61283f64091Sbellard         len = nb_sectors << SECTOR_BITS;
61383f64091Sbellard         buf += len;
61483f64091Sbellard         count -= len;
61583f64091Sbellard     }
61683f64091Sbellard 
61783f64091Sbellard     /* add data from the last sector */
61883f64091Sbellard     if (count > 0) {
61983f64091Sbellard         if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
62083f64091Sbellard             return -EIO;
62183f64091Sbellard         memcpy(tmp_buf, buf, count);
62283f64091Sbellard         if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
62383f64091Sbellard             return -EIO;
62483f64091Sbellard     }
62583f64091Sbellard     return count1;
62683f64091Sbellard }
62783f64091Sbellard 
62883f64091Sbellard /**
62983f64091Sbellard  * Read with byte offsets (needed only for file protocols)
63083f64091Sbellard  */
63183f64091Sbellard int bdrv_pread(BlockDriverState *bs, int64_t offset,
63283f64091Sbellard                void *buf1, int count1)
63383f64091Sbellard {
63483f64091Sbellard     BlockDriver *drv = bs->drv;
63583f64091Sbellard 
63683f64091Sbellard     if (!drv)
637*19cb3738Sbellard         return -ENOMEDIUM;
63883f64091Sbellard     if (!drv->bdrv_pread)
639faea38e7Sbellard         return bdrv_pread_em(bs, offset, buf1, count1);
64083f64091Sbellard     return drv->bdrv_pread(bs, offset, buf1, count1);
64183f64091Sbellard }
64283f64091Sbellard 
64383f64091Sbellard /**
64483f64091Sbellard  * Write with byte offsets (needed only for file protocols)
64583f64091Sbellard  */
64683f64091Sbellard int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
64783f64091Sbellard                 const void *buf1, int count1)
64883f64091Sbellard {
64983f64091Sbellard     BlockDriver *drv = bs->drv;
65083f64091Sbellard 
65183f64091Sbellard     if (!drv)
652*19cb3738Sbellard         return -ENOMEDIUM;
65383f64091Sbellard     if (!drv->bdrv_pwrite)
654faea38e7Sbellard         return bdrv_pwrite_em(bs, offset, buf1, count1);
65583f64091Sbellard     return drv->bdrv_pwrite(bs, offset, buf1, count1);
65683f64091Sbellard }
65783f64091Sbellard 
65883f64091Sbellard /**
65983f64091Sbellard  * Truncate file to 'offset' bytes (needed only for file protocols)
66083f64091Sbellard  */
66183f64091Sbellard int bdrv_truncate(BlockDriverState *bs, int64_t offset)
66283f64091Sbellard {
66383f64091Sbellard     BlockDriver *drv = bs->drv;
66483f64091Sbellard     if (!drv)
665*19cb3738Sbellard         return -ENOMEDIUM;
66683f64091Sbellard     if (!drv->bdrv_truncate)
66783f64091Sbellard         return -ENOTSUP;
66883f64091Sbellard     return drv->bdrv_truncate(bs, offset);
66983f64091Sbellard }
67083f64091Sbellard 
67183f64091Sbellard /**
67283f64091Sbellard  * Length of a file in bytes. Return < 0 if error or unknown.
67383f64091Sbellard  */
67483f64091Sbellard int64_t bdrv_getlength(BlockDriverState *bs)
67583f64091Sbellard {
67683f64091Sbellard     BlockDriver *drv = bs->drv;
67783f64091Sbellard     if (!drv)
678*19cb3738Sbellard         return -ENOMEDIUM;
67983f64091Sbellard     if (!drv->bdrv_getlength) {
68083f64091Sbellard         /* legacy mode */
68183f64091Sbellard         return bs->total_sectors * SECTOR_SIZE;
68283f64091Sbellard     }
68383f64091Sbellard     return drv->bdrv_getlength(bs);
684fc01f7e7Sbellard }
685fc01f7e7Sbellard 
686*19cb3738Sbellard /* return 0 as number of sectors if no device present or error */
687fc01f7e7Sbellard void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
688fc01f7e7Sbellard {
689*19cb3738Sbellard     int64_t length;
690*19cb3738Sbellard     length = bdrv_getlength(bs);
691*19cb3738Sbellard     if (length < 0)
692*19cb3738Sbellard         length = 0;
693*19cb3738Sbellard     else
694*19cb3738Sbellard         length = length >> SECTOR_BITS;
695*19cb3738Sbellard     *nb_sectors_ptr = length;
696fc01f7e7Sbellard }
697cf98951bSbellard 
698cf98951bSbellard /* force a given boot sector. */
699cf98951bSbellard void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
700cf98951bSbellard {
701cf98951bSbellard     bs->boot_sector_enabled = 1;
702cf98951bSbellard     if (size > 512)
703cf98951bSbellard         size = 512;
704cf98951bSbellard     memcpy(bs->boot_sector_data, data, size);
705cf98951bSbellard     memset(bs->boot_sector_data + size, 0, 512 - size);
706cf98951bSbellard }
707b338082bSbellard 
708b338082bSbellard void bdrv_set_geometry_hint(BlockDriverState *bs,
709b338082bSbellard                             int cyls, int heads, int secs)
710b338082bSbellard {
711b338082bSbellard     bs->cyls = cyls;
712b338082bSbellard     bs->heads = heads;
713b338082bSbellard     bs->secs = secs;
714b338082bSbellard }
715b338082bSbellard 
716b338082bSbellard void bdrv_set_type_hint(BlockDriverState *bs, int type)
717b338082bSbellard {
718b338082bSbellard     bs->type = type;
719b338082bSbellard     bs->removable = ((type == BDRV_TYPE_CDROM ||
720b338082bSbellard                       type == BDRV_TYPE_FLOPPY));
721b338082bSbellard }
722b338082bSbellard 
72346d4767dSbellard void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
72446d4767dSbellard {
72546d4767dSbellard     bs->translation = translation;
72646d4767dSbellard }
72746d4767dSbellard 
728b338082bSbellard void bdrv_get_geometry_hint(BlockDriverState *bs,
729b338082bSbellard                             int *pcyls, int *pheads, int *psecs)
730b338082bSbellard {
731b338082bSbellard     *pcyls = bs->cyls;
732b338082bSbellard     *pheads = bs->heads;
733b338082bSbellard     *psecs = bs->secs;
734b338082bSbellard }
735b338082bSbellard 
736b338082bSbellard int bdrv_get_type_hint(BlockDriverState *bs)
737b338082bSbellard {
738b338082bSbellard     return bs->type;
739b338082bSbellard }
740b338082bSbellard 
74146d4767dSbellard int bdrv_get_translation_hint(BlockDriverState *bs)
74246d4767dSbellard {
74346d4767dSbellard     return bs->translation;
74446d4767dSbellard }
74546d4767dSbellard 
746b338082bSbellard int bdrv_is_removable(BlockDriverState *bs)
747b338082bSbellard {
748b338082bSbellard     return bs->removable;
749b338082bSbellard }
750b338082bSbellard 
751b338082bSbellard int bdrv_is_read_only(BlockDriverState *bs)
752b338082bSbellard {
753b338082bSbellard     return bs->read_only;
754b338082bSbellard }
755b338082bSbellard 
756*19cb3738Sbellard /* XXX: no longer used */
757b338082bSbellard void bdrv_set_change_cb(BlockDriverState *bs,
758b338082bSbellard                         void (*change_cb)(void *opaque), void *opaque)
759b338082bSbellard {
760b338082bSbellard     bs->change_cb = change_cb;
761b338082bSbellard     bs->change_opaque = opaque;
762b338082bSbellard }
763b338082bSbellard 
764ea2384d3Sbellard int bdrv_is_encrypted(BlockDriverState *bs)
765ea2384d3Sbellard {
766ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted)
767ea2384d3Sbellard         return 1;
768ea2384d3Sbellard     return bs->encrypted;
769ea2384d3Sbellard }
770ea2384d3Sbellard 
771ea2384d3Sbellard int bdrv_set_key(BlockDriverState *bs, const char *key)
772ea2384d3Sbellard {
773ea2384d3Sbellard     int ret;
774ea2384d3Sbellard     if (bs->backing_hd && bs->backing_hd->encrypted) {
775ea2384d3Sbellard         ret = bdrv_set_key(bs->backing_hd, key);
776ea2384d3Sbellard         if (ret < 0)
777ea2384d3Sbellard             return ret;
778ea2384d3Sbellard         if (!bs->encrypted)
779ea2384d3Sbellard             return 0;
780ea2384d3Sbellard     }
781ea2384d3Sbellard     if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
782ea2384d3Sbellard         return -1;
783ea2384d3Sbellard     return bs->drv->bdrv_set_key(bs, key);
784ea2384d3Sbellard }
785ea2384d3Sbellard 
786ea2384d3Sbellard void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
787ea2384d3Sbellard {
788*19cb3738Sbellard     if (!bs->drv) {
789ea2384d3Sbellard         buf[0] = '\0';
790ea2384d3Sbellard     } else {
791ea2384d3Sbellard         pstrcpy(buf, buf_size, bs->drv->format_name);
792ea2384d3Sbellard     }
793ea2384d3Sbellard }
794ea2384d3Sbellard 
795ea2384d3Sbellard void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
796ea2384d3Sbellard                          void *opaque)
797ea2384d3Sbellard {
798ea2384d3Sbellard     BlockDriver *drv;
799ea2384d3Sbellard 
800ea2384d3Sbellard     for (drv = first_drv; drv != NULL; drv = drv->next) {
801ea2384d3Sbellard         it(opaque, drv->format_name);
802ea2384d3Sbellard     }
803ea2384d3Sbellard }
804ea2384d3Sbellard 
805b338082bSbellard BlockDriverState *bdrv_find(const char *name)
806b338082bSbellard {
807b338082bSbellard     BlockDriverState *bs;
808b338082bSbellard 
809b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
810b338082bSbellard         if (!strcmp(name, bs->device_name))
811b338082bSbellard             return bs;
812b338082bSbellard     }
813b338082bSbellard     return NULL;
814b338082bSbellard }
815b338082bSbellard 
81681d0912dSbellard void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
81781d0912dSbellard {
81881d0912dSbellard     BlockDriverState *bs;
81981d0912dSbellard 
82081d0912dSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
82181d0912dSbellard         it(opaque, bs->device_name);
82281d0912dSbellard     }
82381d0912dSbellard }
82481d0912dSbellard 
825ea2384d3Sbellard const char *bdrv_get_device_name(BlockDriverState *bs)
826ea2384d3Sbellard {
827ea2384d3Sbellard     return bs->device_name;
828ea2384d3Sbellard }
829ea2384d3Sbellard 
8307a6cba61Spbrook void bdrv_flush(BlockDriverState *bs)
8317a6cba61Spbrook {
8327a6cba61Spbrook     if (bs->drv->bdrv_flush)
8337a6cba61Spbrook         bs->drv->bdrv_flush(bs);
8347a6cba61Spbrook     if (bs->backing_hd)
8357a6cba61Spbrook         bdrv_flush(bs->backing_hd);
8367a6cba61Spbrook }
8377a6cba61Spbrook 
838b338082bSbellard void bdrv_info(void)
839b338082bSbellard {
840b338082bSbellard     BlockDriverState *bs;
841b338082bSbellard 
842b338082bSbellard     for (bs = bdrv_first; bs != NULL; bs = bs->next) {
843b338082bSbellard         term_printf("%s:", bs->device_name);
844b338082bSbellard         term_printf(" type=");
845b338082bSbellard         switch(bs->type) {
846b338082bSbellard         case BDRV_TYPE_HD:
847b338082bSbellard             term_printf("hd");
848b338082bSbellard             break;
849b338082bSbellard         case BDRV_TYPE_CDROM:
850b338082bSbellard             term_printf("cdrom");
851b338082bSbellard             break;
852b338082bSbellard         case BDRV_TYPE_FLOPPY:
853b338082bSbellard             term_printf("floppy");
854b338082bSbellard             break;
855b338082bSbellard         }
856b338082bSbellard         term_printf(" removable=%d", bs->removable);
857b338082bSbellard         if (bs->removable) {
858b338082bSbellard             term_printf(" locked=%d", bs->locked);
859b338082bSbellard         }
860*19cb3738Sbellard         if (bs->drv) {
861b338082bSbellard             term_printf(" file=%s", bs->filename);
862ea2384d3Sbellard             if (bs->backing_file[0] != '\0')
863ea2384d3Sbellard                 term_printf(" backing_file=%s", bs->backing_file);
864b338082bSbellard             term_printf(" ro=%d", bs->read_only);
865ea2384d3Sbellard             term_printf(" drv=%s", bs->drv->format_name);
866ea2384d3Sbellard             if (bs->encrypted)
867ea2384d3Sbellard                 term_printf(" encrypted");
868b338082bSbellard         } else {
869b338082bSbellard             term_printf(" [not inserted]");
870b338082bSbellard         }
871b338082bSbellard         term_printf("\n");
872b338082bSbellard     }
873b338082bSbellard }
874ea2384d3Sbellard 
87583f64091Sbellard void bdrv_get_backing_filename(BlockDriverState *bs,
87683f64091Sbellard                                char *filename, int filename_size)
87783f64091Sbellard {
87883f64091Sbellard     if (!bs->backing_hd) {
87983f64091Sbellard         pstrcpy(filename, filename_size, "");
88083f64091Sbellard     } else {
88183f64091Sbellard         pstrcpy(filename, filename_size, bs->backing_file);
88283f64091Sbellard     }
88383f64091Sbellard }
88483f64091Sbellard 
885faea38e7Sbellard int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
886faea38e7Sbellard                           const uint8_t *buf, int nb_sectors)
887faea38e7Sbellard {
888faea38e7Sbellard     BlockDriver *drv = bs->drv;
889faea38e7Sbellard     if (!drv)
890*19cb3738Sbellard         return -ENOMEDIUM;
891faea38e7Sbellard     if (!drv->bdrv_write_compressed)
892faea38e7Sbellard         return -ENOTSUP;
893faea38e7Sbellard     return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
894faea38e7Sbellard }
895faea38e7Sbellard 
896faea38e7Sbellard int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
897faea38e7Sbellard {
898faea38e7Sbellard     BlockDriver *drv = bs->drv;
899faea38e7Sbellard     if (!drv)
900*19cb3738Sbellard         return -ENOMEDIUM;
901faea38e7Sbellard     if (!drv->bdrv_get_info)
902faea38e7Sbellard         return -ENOTSUP;
903faea38e7Sbellard     memset(bdi, 0, sizeof(*bdi));
904faea38e7Sbellard     return drv->bdrv_get_info(bs, bdi);
905faea38e7Sbellard }
906faea38e7Sbellard 
907faea38e7Sbellard /**************************************************************/
908faea38e7Sbellard /* handling of snapshots */
909faea38e7Sbellard 
910faea38e7Sbellard int bdrv_snapshot_create(BlockDriverState *bs,
911faea38e7Sbellard                          QEMUSnapshotInfo *sn_info)
912faea38e7Sbellard {
913faea38e7Sbellard     BlockDriver *drv = bs->drv;
914faea38e7Sbellard     if (!drv)
915*19cb3738Sbellard         return -ENOMEDIUM;
916faea38e7Sbellard     if (!drv->bdrv_snapshot_create)
917faea38e7Sbellard         return -ENOTSUP;
918faea38e7Sbellard     return drv->bdrv_snapshot_create(bs, sn_info);
919faea38e7Sbellard }
920faea38e7Sbellard 
921faea38e7Sbellard int bdrv_snapshot_goto(BlockDriverState *bs,
922faea38e7Sbellard                        const char *snapshot_id)
923faea38e7Sbellard {
924faea38e7Sbellard     BlockDriver *drv = bs->drv;
925faea38e7Sbellard     if (!drv)
926*19cb3738Sbellard         return -ENOMEDIUM;
927faea38e7Sbellard     if (!drv->bdrv_snapshot_goto)
928faea38e7Sbellard         return -ENOTSUP;
929faea38e7Sbellard     return drv->bdrv_snapshot_goto(bs, snapshot_id);
930faea38e7Sbellard }
931faea38e7Sbellard 
932faea38e7Sbellard int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
933faea38e7Sbellard {
934faea38e7Sbellard     BlockDriver *drv = bs->drv;
935faea38e7Sbellard     if (!drv)
936*19cb3738Sbellard         return -ENOMEDIUM;
937faea38e7Sbellard     if (!drv->bdrv_snapshot_delete)
938faea38e7Sbellard         return -ENOTSUP;
939faea38e7Sbellard     return drv->bdrv_snapshot_delete(bs, snapshot_id);
940faea38e7Sbellard }
941faea38e7Sbellard 
942faea38e7Sbellard int bdrv_snapshot_list(BlockDriverState *bs,
943faea38e7Sbellard                        QEMUSnapshotInfo **psn_info)
944faea38e7Sbellard {
945faea38e7Sbellard     BlockDriver *drv = bs->drv;
946faea38e7Sbellard     if (!drv)
947*19cb3738Sbellard         return -ENOMEDIUM;
948faea38e7Sbellard     if (!drv->bdrv_snapshot_list)
949faea38e7Sbellard         return -ENOTSUP;
950faea38e7Sbellard     return drv->bdrv_snapshot_list(bs, psn_info);
951faea38e7Sbellard }
952faea38e7Sbellard 
953faea38e7Sbellard #define NB_SUFFIXES 4
954faea38e7Sbellard 
955faea38e7Sbellard char *get_human_readable_size(char *buf, int buf_size, int64_t size)
956faea38e7Sbellard {
957faea38e7Sbellard     static const char suffixes[NB_SUFFIXES] = "KMGT";
958faea38e7Sbellard     int64_t base;
959faea38e7Sbellard     int i;
960faea38e7Sbellard 
961faea38e7Sbellard     if (size <= 999) {
962faea38e7Sbellard         snprintf(buf, buf_size, "%" PRId64, size);
963faea38e7Sbellard     } else {
964faea38e7Sbellard         base = 1024;
965faea38e7Sbellard         for(i = 0; i < NB_SUFFIXES; i++) {
966faea38e7Sbellard             if (size < (10 * base)) {
967faea38e7Sbellard                 snprintf(buf, buf_size, "%0.1f%c",
968faea38e7Sbellard                          (double)size / base,
969faea38e7Sbellard                          suffixes[i]);
970faea38e7Sbellard                 break;
971faea38e7Sbellard             } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
972faea38e7Sbellard                 snprintf(buf, buf_size, "%" PRId64 "%c",
973faea38e7Sbellard                          ((size + (base >> 1)) / base),
974faea38e7Sbellard                          suffixes[i]);
975faea38e7Sbellard                 break;
976faea38e7Sbellard             }
977faea38e7Sbellard             base = base * 1024;
978faea38e7Sbellard         }
979faea38e7Sbellard     }
980faea38e7Sbellard     return buf;
981faea38e7Sbellard }
982faea38e7Sbellard 
983faea38e7Sbellard char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
984faea38e7Sbellard {
985faea38e7Sbellard     char buf1[128], date_buf[128], clock_buf[128];
986faea38e7Sbellard     struct tm tm;
987faea38e7Sbellard     time_t ti;
988faea38e7Sbellard     int64_t secs;
989faea38e7Sbellard 
990faea38e7Sbellard     if (!sn) {
991faea38e7Sbellard         snprintf(buf, buf_size,
992faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
993faea38e7Sbellard                  "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
994faea38e7Sbellard     } else {
995faea38e7Sbellard         ti = sn->date_sec;
996ce1a14dcSpbrook #ifndef _WIN32
997faea38e7Sbellard         localtime_r(&ti, &tm);
998ce1a14dcSpbrook #endif
999faea38e7Sbellard         strftime(date_buf, sizeof(date_buf),
1000faea38e7Sbellard                  "%Y-%m-%d %H:%M:%S", &tm);
1001faea38e7Sbellard         secs = sn->vm_clock_nsec / 1000000000;
1002faea38e7Sbellard         snprintf(clock_buf, sizeof(clock_buf),
1003faea38e7Sbellard                  "%02d:%02d:%02d.%03d",
1004faea38e7Sbellard                  (int)(secs / 3600),
1005faea38e7Sbellard                  (int)((secs / 60) % 60),
1006faea38e7Sbellard                  (int)(secs % 60),
1007faea38e7Sbellard                  (int)((sn->vm_clock_nsec / 1000000) % 1000));
1008faea38e7Sbellard         snprintf(buf, buf_size,
1009faea38e7Sbellard                  "%-10s%-20s%7s%20s%15s",
1010faea38e7Sbellard                  sn->id_str, sn->name,
1011faea38e7Sbellard                  get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1012faea38e7Sbellard                  date_buf,
1013faea38e7Sbellard                  clock_buf);
1014faea38e7Sbellard     }
1015faea38e7Sbellard     return buf;
1016faea38e7Sbellard }
1017faea38e7Sbellard 
101883f64091Sbellard 
1019ea2384d3Sbellard /**************************************************************/
102083f64091Sbellard /* async I/Os */
1021ea2384d3Sbellard 
1022ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
102383f64091Sbellard                                 uint8_t *buf, int nb_sectors,
102483f64091Sbellard                                 BlockDriverCompletionFunc *cb, void *opaque)
1025ea2384d3Sbellard {
102683f64091Sbellard     BlockDriver *drv = bs->drv;
1027ea2384d3Sbellard 
1028*19cb3738Sbellard     if (!drv)
1029ce1a14dcSpbrook         return NULL;
103083f64091Sbellard 
103183f64091Sbellard     /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
103283f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
103383f64091Sbellard         memcpy(buf, bs->boot_sector_data, 512);
103483f64091Sbellard         sector_num++;
103583f64091Sbellard         nb_sectors--;
103683f64091Sbellard         buf += 512;
1037ea2384d3Sbellard     }
103883f64091Sbellard 
1039ce1a14dcSpbrook     return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
104083f64091Sbellard }
104183f64091Sbellard 
1042ce1a14dcSpbrook BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
104383f64091Sbellard                                  const uint8_t *buf, int nb_sectors,
104483f64091Sbellard                                  BlockDriverCompletionFunc *cb, void *opaque)
10457674e7bfSbellard {
104683f64091Sbellard     BlockDriver *drv = bs->drv;
104783f64091Sbellard 
1048*19cb3738Sbellard     if (!drv)
1049ce1a14dcSpbrook         return NULL;
105083f64091Sbellard     if (bs->read_only)
1051ce1a14dcSpbrook         return NULL;
105283f64091Sbellard     if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
105383f64091Sbellard         memcpy(bs->boot_sector_data, buf, 512);
10547674e7bfSbellard     }
105583f64091Sbellard 
1056ce1a14dcSpbrook     return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
105783f64091Sbellard }
105883f64091Sbellard 
105983f64091Sbellard void bdrv_aio_cancel(BlockDriverAIOCB *acb)
106083f64091Sbellard {
1061ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
106283f64091Sbellard 
106383f64091Sbellard     drv->bdrv_aio_cancel(acb);
106483f64091Sbellard }
106583f64091Sbellard 
106683f64091Sbellard 
106783f64091Sbellard /**************************************************************/
106883f64091Sbellard /* async block device emulation */
106983f64091Sbellard 
107083f64091Sbellard #ifdef QEMU_TOOL
1071ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1072ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1073ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1074ea2384d3Sbellard {
1075ea2384d3Sbellard     int ret;
1076ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1077ce1a14dcSpbrook     cb(opaque, ret);
1078ce1a14dcSpbrook     return NULL;
1079ea2384d3Sbellard }
1080ea2384d3Sbellard 
1081ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1082ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1083ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1084ea2384d3Sbellard {
1085ea2384d3Sbellard     int ret;
1086ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1087ce1a14dcSpbrook     cb(opaque, ret);
1088ce1a14dcSpbrook     return NULL;
1089ea2384d3Sbellard }
1090ea2384d3Sbellard 
109183f64091Sbellard static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1092ea2384d3Sbellard {
1093ea2384d3Sbellard }
1094beac80cdSbellard #else
109583f64091Sbellard static void bdrv_aio_bh_cb(void *opaque)
1096beac80cdSbellard {
1097ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = opaque;
1098ce1a14dcSpbrook     acb->common.cb(acb->common.opaque, acb->ret);
1099ce1a14dcSpbrook     qemu_aio_release(acb);
1100beac80cdSbellard }
1101beac80cdSbellard 
1102ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1103ce1a14dcSpbrook         int64_t sector_num, uint8_t *buf, int nb_sectors,
1104ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
1105ea2384d3Sbellard {
1106ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
110783f64091Sbellard     int ret;
110883f64091Sbellard 
1109ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1110ce1a14dcSpbrook     if (!acb->bh)
1111ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1112ce1a14dcSpbrook     ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1113ce1a14dcSpbrook     acb->ret = ret;
1114ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1115ce1a14dcSpbrook     return &acb->common;
11167a6cba61Spbrook }
11177a6cba61Spbrook 
1118ce1a14dcSpbrook static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1119ce1a14dcSpbrook         int64_t sector_num, const uint8_t *buf, int nb_sectors,
1120ce1a14dcSpbrook         BlockDriverCompletionFunc *cb, void *opaque)
112183f64091Sbellard {
1122ce1a14dcSpbrook     BlockDriverAIOCBSync *acb;
112383f64091Sbellard     int ret;
112483f64091Sbellard 
1125ce1a14dcSpbrook     acb = qemu_aio_get(bs, cb, opaque);
1126ce1a14dcSpbrook     if (!acb->bh)
1127ce1a14dcSpbrook         acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1128ce1a14dcSpbrook     ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1129ce1a14dcSpbrook     acb->ret = ret;
1130ce1a14dcSpbrook     qemu_bh_schedule(acb->bh);
1131ce1a14dcSpbrook     return &acb->common;
113283f64091Sbellard }
113383f64091Sbellard 
1134ce1a14dcSpbrook static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
113583f64091Sbellard {
1136ce1a14dcSpbrook     BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1137ce1a14dcSpbrook     qemu_bh_cancel(acb->bh);
1138ce1a14dcSpbrook     qemu_aio_release(acb);
113983f64091Sbellard }
114083f64091Sbellard #endif /* !QEMU_TOOL */
114183f64091Sbellard 
114283f64091Sbellard /**************************************************************/
114383f64091Sbellard /* sync block device emulation */
114483f64091Sbellard 
114583f64091Sbellard static void bdrv_rw_em_cb(void *opaque, int ret)
114683f64091Sbellard {
114783f64091Sbellard     *(int *)opaque = ret;
114883f64091Sbellard }
114983f64091Sbellard 
115083f64091Sbellard #define NOT_DONE 0x7fffffff
115183f64091Sbellard 
115283f64091Sbellard static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
115383f64091Sbellard                         uint8_t *buf, int nb_sectors)
115483f64091Sbellard {
1155ce1a14dcSpbrook     int async_ret;
1156ce1a14dcSpbrook     BlockDriverAIOCB *acb;
115783f64091Sbellard 
115883f64091Sbellard     async_ret = NOT_DONE;
115983f64091Sbellard     qemu_aio_wait_start();
1160ce1a14dcSpbrook     acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
116183f64091Sbellard                         bdrv_rw_em_cb, &async_ret);
1162ce1a14dcSpbrook     if (acb == NULL) {
116383f64091Sbellard         qemu_aio_wait_end();
1164ce1a14dcSpbrook         return -1;
116583f64091Sbellard     }
116683f64091Sbellard     while (async_ret == NOT_DONE) {
116783f64091Sbellard         qemu_aio_wait();
116883f64091Sbellard     }
116983f64091Sbellard     qemu_aio_wait_end();
117083f64091Sbellard     return async_ret;
117183f64091Sbellard }
117283f64091Sbellard 
117383f64091Sbellard static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
117483f64091Sbellard                          const uint8_t *buf, int nb_sectors)
117583f64091Sbellard {
1176ce1a14dcSpbrook     int async_ret;
1177ce1a14dcSpbrook     BlockDriverAIOCB *acb;
117883f64091Sbellard 
117983f64091Sbellard     async_ret = NOT_DONE;
118083f64091Sbellard     qemu_aio_wait_start();
1181ce1a14dcSpbrook     acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
118283f64091Sbellard                          bdrv_rw_em_cb, &async_ret);
1183ce1a14dcSpbrook     if (acb == NULL) {
118483f64091Sbellard         qemu_aio_wait_end();
1185ce1a14dcSpbrook         return -1;
118683f64091Sbellard     }
118783f64091Sbellard     while (async_ret == NOT_DONE) {
118883f64091Sbellard         qemu_aio_wait();
118983f64091Sbellard     }
119083f64091Sbellard     qemu_aio_wait_end();
119183f64091Sbellard     return async_ret;
119283f64091Sbellard }
1193ea2384d3Sbellard 
1194ea2384d3Sbellard void bdrv_init(void)
1195ea2384d3Sbellard {
1196ea2384d3Sbellard     bdrv_register(&bdrv_raw);
1197*19cb3738Sbellard     bdrv_register(&bdrv_host_device);
1198ea2384d3Sbellard #ifndef _WIN32
1199ea2384d3Sbellard     bdrv_register(&bdrv_cow);
1200ea2384d3Sbellard #endif
1201ea2384d3Sbellard     bdrv_register(&bdrv_qcow);
1202ea2384d3Sbellard     bdrv_register(&bdrv_vmdk);
12033c56521bSbellard     bdrv_register(&bdrv_cloop);
1204585d0ed9Sbellard     bdrv_register(&bdrv_dmg);
1205a8753c34Sbellard     bdrv_register(&bdrv_bochs);
12066a0f9e82Sbellard     bdrv_register(&bdrv_vpc);
1207712e7874Sbellard     bdrv_register(&bdrv_vvfat);
1208faea38e7Sbellard     bdrv_register(&bdrv_qcow2);
1209ea2384d3Sbellard }
1210ce1a14dcSpbrook 
1211ce1a14dcSpbrook void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1212ce1a14dcSpbrook                    void *opaque)
1213ce1a14dcSpbrook {
1214ce1a14dcSpbrook     BlockDriver *drv;
1215ce1a14dcSpbrook     BlockDriverAIOCB *acb;
1216ce1a14dcSpbrook 
1217ce1a14dcSpbrook     drv = bs->drv;
1218ce1a14dcSpbrook     if (drv->free_aiocb) {
1219ce1a14dcSpbrook         acb = drv->free_aiocb;
1220ce1a14dcSpbrook         drv->free_aiocb = acb->next;
1221ce1a14dcSpbrook     } else {
1222ce1a14dcSpbrook         acb = qemu_mallocz(drv->aiocb_size);
1223ce1a14dcSpbrook         if (!acb)
1224ce1a14dcSpbrook             return NULL;
1225ce1a14dcSpbrook     }
1226ce1a14dcSpbrook     acb->bs = bs;
1227ce1a14dcSpbrook     acb->cb = cb;
1228ce1a14dcSpbrook     acb->opaque = opaque;
1229ce1a14dcSpbrook     return acb;
1230ce1a14dcSpbrook }
1231ce1a14dcSpbrook 
1232ce1a14dcSpbrook void qemu_aio_release(void *p)
1233ce1a14dcSpbrook {
1234ce1a14dcSpbrook     BlockDriverAIOCB *acb = p;
1235ce1a14dcSpbrook     BlockDriver *drv = acb->bs->drv;
1236ce1a14dcSpbrook     acb->next = drv->free_aiocb;
1237ce1a14dcSpbrook     drv->free_aiocb = acb;
1238ce1a14dcSpbrook }
1239*19cb3738Sbellard 
1240*19cb3738Sbellard /**************************************************************/
1241*19cb3738Sbellard /* removable device support */
1242*19cb3738Sbellard 
1243*19cb3738Sbellard /**
1244*19cb3738Sbellard  * Return TRUE if the media is present
1245*19cb3738Sbellard  */
1246*19cb3738Sbellard int bdrv_is_inserted(BlockDriverState *bs)
1247*19cb3738Sbellard {
1248*19cb3738Sbellard     BlockDriver *drv = bs->drv;
1249*19cb3738Sbellard     int ret;
1250*19cb3738Sbellard     if (!drv)
1251*19cb3738Sbellard         return 0;
1252*19cb3738Sbellard     if (!drv->bdrv_is_inserted)
1253*19cb3738Sbellard         return 1;
1254*19cb3738Sbellard     ret = drv->bdrv_is_inserted(bs);
1255*19cb3738Sbellard     return ret;
1256*19cb3738Sbellard }
1257*19cb3738Sbellard 
1258*19cb3738Sbellard /**
1259*19cb3738Sbellard  * Return TRUE if the media changed since the last call to this
1260*19cb3738Sbellard  * function. It is currently only used for floppy disks
1261*19cb3738Sbellard  */
1262*19cb3738Sbellard int bdrv_media_changed(BlockDriverState *bs)
1263*19cb3738Sbellard {
1264*19cb3738Sbellard     BlockDriver *drv = bs->drv;
1265*19cb3738Sbellard     int ret;
1266*19cb3738Sbellard 
1267*19cb3738Sbellard     if (!drv || !drv->bdrv_media_changed)
1268*19cb3738Sbellard         ret = -ENOTSUP;
1269*19cb3738Sbellard     else
1270*19cb3738Sbellard         ret = drv->bdrv_media_changed(bs);
1271*19cb3738Sbellard     if (ret == -ENOTSUP)
1272*19cb3738Sbellard         ret = bs->media_changed;
1273*19cb3738Sbellard     bs->media_changed = 0;
1274*19cb3738Sbellard     return ret;
1275*19cb3738Sbellard }
1276*19cb3738Sbellard 
1277*19cb3738Sbellard /**
1278*19cb3738Sbellard  * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1279*19cb3738Sbellard  */
1280*19cb3738Sbellard void bdrv_eject(BlockDriverState *bs, int eject_flag)
1281*19cb3738Sbellard {
1282*19cb3738Sbellard     BlockDriver *drv = bs->drv;
1283*19cb3738Sbellard     int ret;
1284*19cb3738Sbellard 
1285*19cb3738Sbellard     if (!drv || !drv->bdrv_eject) {
1286*19cb3738Sbellard         ret = -ENOTSUP;
1287*19cb3738Sbellard     } else {
1288*19cb3738Sbellard         ret = drv->bdrv_eject(bs, eject_flag);
1289*19cb3738Sbellard     }
1290*19cb3738Sbellard     if (ret == -ENOTSUP) {
1291*19cb3738Sbellard         if (eject_flag)
1292*19cb3738Sbellard             bdrv_close(bs);
1293*19cb3738Sbellard     }
1294*19cb3738Sbellard }
1295*19cb3738Sbellard 
1296*19cb3738Sbellard int bdrv_is_locked(BlockDriverState *bs)
1297*19cb3738Sbellard {
1298*19cb3738Sbellard     return bs->locked;
1299*19cb3738Sbellard }
1300*19cb3738Sbellard 
1301*19cb3738Sbellard /**
1302*19cb3738Sbellard  * Lock or unlock the media (if it is locked, the user won't be able
1303*19cb3738Sbellard  * to eject it manually).
1304*19cb3738Sbellard  */
1305*19cb3738Sbellard void bdrv_set_locked(BlockDriverState *bs, int locked)
1306*19cb3738Sbellard {
1307*19cb3738Sbellard     BlockDriver *drv = bs->drv;
1308*19cb3738Sbellard 
1309*19cb3738Sbellard     bs->locked = locked;
1310*19cb3738Sbellard     if (drv && drv->bdrv_set_locked) {
1311*19cb3738Sbellard         drv->bdrv_set_locked(bs, locked);
1312*19cb3738Sbellard     }
1313*19cb3738Sbellard }
1314