Statistics
| Branch: | Revision:

root / block.c @ 749bc4bf

History | View | Annotate | Download (34.4 kB)

1 fc01f7e7 bellard
/*
2 fc01f7e7 bellard
 * QEMU System Emulator block driver
3 5fafdf24 ths
 *
4 fc01f7e7 bellard
 * Copyright (c) 2003 Fabrice Bellard
5 5fafdf24 ths
 *
6 fc01f7e7 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 fc01f7e7 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 fc01f7e7 bellard
 * in the Software without restriction, including without limitation the rights
9 fc01f7e7 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 fc01f7e7 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 fc01f7e7 bellard
 * furnished to do so, subject to the following conditions:
12 fc01f7e7 bellard
 *
13 fc01f7e7 bellard
 * The above copyright notice and this permission notice shall be included in
14 fc01f7e7 bellard
 * all copies or substantial portions of the Software.
15 fc01f7e7 bellard
 *
16 fc01f7e7 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 fc01f7e7 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 fc01f7e7 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 fc01f7e7 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 fc01f7e7 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 fc01f7e7 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 fc01f7e7 bellard
 * THE SOFTWARE.
23 fc01f7e7 bellard
 */
24 faf07963 pbrook
#include "qemu-common.h"
25 87ecb68b pbrook
#ifndef QEMU_IMG
26 87ecb68b pbrook
#include "console.h"
27 faf07963 pbrook
#endif
28 ea2384d3 bellard
#include "block_int.h"
29 fc01f7e7 bellard
30 7674e7bf bellard
#ifdef _BSD
31 7674e7bf bellard
#include <sys/types.h>
32 7674e7bf bellard
#include <sys/stat.h>
33 7674e7bf bellard
#include <sys/ioctl.h>
34 7674e7bf bellard
#include <sys/queue.h>
35 7674e7bf bellard
#include <sys/disk.h>
36 7674e7bf bellard
#endif
37 7674e7bf bellard
38 83f64091 bellard
#define SECTOR_BITS 9
39 83f64091 bellard
#define SECTOR_SIZE (1 << SECTOR_BITS)
40 83f64091 bellard
41 90765429 bellard
typedef struct BlockDriverAIOCBSync {
42 90765429 bellard
    BlockDriverAIOCB common;
43 90765429 bellard
    QEMUBH *bh;
44 90765429 bellard
    int ret;
45 90765429 bellard
} BlockDriverAIOCBSync;
46 90765429 bellard
47 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
48 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
49 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
50 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
51 ce1a14dc pbrook
        int64_t sector_num, const uint8_t *buf, int nb_sectors,
52 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
53 83f64091 bellard
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb);
54 5fafdf24 ths
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
55 83f64091 bellard
                        uint8_t *buf, int nb_sectors);
56 83f64091 bellard
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
57 83f64091 bellard
                         const uint8_t *buf, int nb_sectors);
58 ec530c81 bellard
59 faf07963 pbrook
BlockDriverState *bdrv_first;
60 ea2384d3 bellard
static BlockDriver *first_drv;
61 ea2384d3 bellard
62 83f64091 bellard
int path_is_absolute(const char *path)
63 3b0d4f61 bellard
{
64 83f64091 bellard
    const char *p;
65 21664424 bellard
#ifdef _WIN32
66 21664424 bellard
    /* specific case for names like: "\\.\d:" */
67 21664424 bellard
    if (*path == '/' || *path == '\\')
68 21664424 bellard
        return 1;
69 21664424 bellard
#endif
70 83f64091 bellard
    p = strchr(path, ':');
71 83f64091 bellard
    if (p)
72 83f64091 bellard
        p++;
73 83f64091 bellard
    else
74 83f64091 bellard
        p = path;
75 3b9f94e1 bellard
#ifdef _WIN32
76 3b9f94e1 bellard
    return (*p == '/' || *p == '\\');
77 3b9f94e1 bellard
#else
78 3b9f94e1 bellard
    return (*p == '/');
79 3b9f94e1 bellard
#endif
80 3b0d4f61 bellard
}
81 3b0d4f61 bellard
82 83f64091 bellard
/* if filename is absolute, just copy it to dest. Otherwise, build a
83 83f64091 bellard
   path to it by considering it is relative to base_path. URL are
84 83f64091 bellard
   supported. */
85 83f64091 bellard
void path_combine(char *dest, int dest_size,
86 83f64091 bellard
                  const char *base_path,
87 83f64091 bellard
                  const char *filename)
88 3b0d4f61 bellard
{
89 83f64091 bellard
    const char *p, *p1;
90 83f64091 bellard
    int len;
91 83f64091 bellard
92 83f64091 bellard
    if (dest_size <= 0)
93 83f64091 bellard
        return;
94 83f64091 bellard
    if (path_is_absolute(filename)) {
95 83f64091 bellard
        pstrcpy(dest, dest_size, filename);
96 83f64091 bellard
    } else {
97 83f64091 bellard
        p = strchr(base_path, ':');
98 83f64091 bellard
        if (p)
99 83f64091 bellard
            p++;
100 83f64091 bellard
        else
101 83f64091 bellard
            p = base_path;
102 3b9f94e1 bellard
        p1 = strrchr(base_path, '/');
103 3b9f94e1 bellard
#ifdef _WIN32
104 3b9f94e1 bellard
        {
105 3b9f94e1 bellard
            const char *p2;
106 3b9f94e1 bellard
            p2 = strrchr(base_path, '\\');
107 3b9f94e1 bellard
            if (!p1 || p2 > p1)
108 3b9f94e1 bellard
                p1 = p2;
109 3b9f94e1 bellard
        }
110 3b9f94e1 bellard
#endif
111 83f64091 bellard
        if (p1)
112 83f64091 bellard
            p1++;
113 83f64091 bellard
        else
114 83f64091 bellard
            p1 = base_path;
115 83f64091 bellard
        if (p1 > p)
116 83f64091 bellard
            p = p1;
117 83f64091 bellard
        len = p - base_path;
118 83f64091 bellard
        if (len > dest_size - 1)
119 83f64091 bellard
            len = dest_size - 1;
120 83f64091 bellard
        memcpy(dest, base_path, len);
121 83f64091 bellard
        dest[len] = '\0';
122 83f64091 bellard
        pstrcat(dest, dest_size, filename);
123 3b0d4f61 bellard
    }
124 3b0d4f61 bellard
}
125 3b0d4f61 bellard
126 3b0d4f61 bellard
127 ea2384d3 bellard
void bdrv_register(BlockDriver *bdrv)
128 ea2384d3 bellard
{
129 ce1a14dc pbrook
    if (!bdrv->bdrv_aio_read) {
130 83f64091 bellard
        /* add AIO emulation layer */
131 83f64091 bellard
        bdrv->bdrv_aio_read = bdrv_aio_read_em;
132 83f64091 bellard
        bdrv->bdrv_aio_write = bdrv_aio_write_em;
133 83f64091 bellard
        bdrv->bdrv_aio_cancel = bdrv_aio_cancel_em;
134 90765429 bellard
        bdrv->aiocb_size = sizeof(BlockDriverAIOCBSync);
135 83f64091 bellard
    } else if (!bdrv->bdrv_read && !bdrv->bdrv_pread) {
136 83f64091 bellard
        /* add synchronous IO emulation layer */
137 83f64091 bellard
        bdrv->bdrv_read = bdrv_read_em;
138 83f64091 bellard
        bdrv->bdrv_write = bdrv_write_em;
139 83f64091 bellard
    }
140 ea2384d3 bellard
    bdrv->next = first_drv;
141 ea2384d3 bellard
    first_drv = bdrv;
142 ea2384d3 bellard
}
143 b338082b bellard
144 b338082b bellard
/* create a new block device (by default it is empty) */
145 b338082b bellard
BlockDriverState *bdrv_new(const char *device_name)
146 b338082b bellard
{
147 b338082b bellard
    BlockDriverState **pbs, *bs;
148 b338082b bellard
149 b338082b bellard
    bs = qemu_mallocz(sizeof(BlockDriverState));
150 b338082b bellard
    if(!bs)
151 b338082b bellard
        return NULL;
152 b338082b bellard
    pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
153 ea2384d3 bellard
    if (device_name[0] != '\0') {
154 ea2384d3 bellard
        /* insert at the end */
155 ea2384d3 bellard
        pbs = &bdrv_first;
156 ea2384d3 bellard
        while (*pbs != NULL)
157 ea2384d3 bellard
            pbs = &(*pbs)->next;
158 ea2384d3 bellard
        *pbs = bs;
159 ea2384d3 bellard
    }
160 b338082b bellard
    return bs;
161 b338082b bellard
}
162 b338082b bellard
163 ea2384d3 bellard
BlockDriver *bdrv_find_format(const char *format_name)
164 ea2384d3 bellard
{
165 ea2384d3 bellard
    BlockDriver *drv1;
166 ea2384d3 bellard
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
167 ea2384d3 bellard
        if (!strcmp(drv1->format_name, format_name))
168 ea2384d3 bellard
            return drv1;
169 ea2384d3 bellard
    }
170 ea2384d3 bellard
    return NULL;
171 ea2384d3 bellard
}
172 ea2384d3 bellard
173 5fafdf24 ths
int bdrv_create(BlockDriver *drv,
174 ea2384d3 bellard
                const char *filename, int64_t size_in_sectors,
175 ea2384d3 bellard
                const char *backing_file, int flags)
176 ea2384d3 bellard
{
177 ea2384d3 bellard
    if (!drv->bdrv_create)
178 ea2384d3 bellard
        return -ENOTSUP;
179 ea2384d3 bellard
    return drv->bdrv_create(filename, size_in_sectors, backing_file, flags);
180 ea2384d3 bellard
}
181 ea2384d3 bellard
182 d5249393 bellard
#ifdef _WIN32
183 95389c86 bellard
void get_tmp_filename(char *filename, int size)
184 d5249393 bellard
{
185 3b9f94e1 bellard
    char temp_dir[MAX_PATH];
186 3b46e624 ths
187 3b9f94e1 bellard
    GetTempPath(MAX_PATH, temp_dir);
188 3b9f94e1 bellard
    GetTempFileName(temp_dir, "qem", 0, filename);
189 d5249393 bellard
}
190 d5249393 bellard
#else
191 95389c86 bellard
void get_tmp_filename(char *filename, int size)
192 fc01f7e7 bellard
{
193 67b915a5 bellard
    int fd;
194 d5249393 bellard
    /* XXX: race condition possible */
195 ea2384d3 bellard
    pstrcpy(filename, size, "/tmp/vl.XXXXXX");
196 ea2384d3 bellard
    fd = mkstemp(filename);
197 ea2384d3 bellard
    close(fd);
198 ea2384d3 bellard
}
199 d5249393 bellard
#endif
200 fc01f7e7 bellard
201 19cb3738 bellard
#ifdef _WIN32
202 f45512fe bellard
static int is_windows_drive_prefix(const char *filename)
203 f45512fe bellard
{
204 f45512fe bellard
    return (((filename[0] >= 'a' && filename[0] <= 'z') ||
205 f45512fe bellard
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
206 f45512fe bellard
            filename[1] == ':');
207 f45512fe bellard
}
208 3b46e624 ths
209 19cb3738 bellard
static int is_windows_drive(const char *filename)
210 19cb3738 bellard
{
211 5fafdf24 ths
    if (is_windows_drive_prefix(filename) &&
212 f45512fe bellard
        filename[2] == '\0')
213 19cb3738 bellard
        return 1;
214 19cb3738 bellard
    if (strstart(filename, "\\\\.\\", NULL) ||
215 19cb3738 bellard
        strstart(filename, "//./", NULL))
216 19cb3738 bellard
        return 1;
217 19cb3738 bellard
    return 0;
218 19cb3738 bellard
}
219 19cb3738 bellard
#endif
220 19cb3738 bellard
221 83f64091 bellard
static BlockDriver *find_protocol(const char *filename)
222 83f64091 bellard
{
223 83f64091 bellard
    BlockDriver *drv1;
224 83f64091 bellard
    char protocol[128];
225 83f64091 bellard
    int len;
226 83f64091 bellard
    const char *p;
227 19cb3738 bellard
228 19cb3738 bellard
#ifdef _WIN32
229 f45512fe bellard
    if (is_windows_drive(filename) ||
230 f45512fe bellard
        is_windows_drive_prefix(filename))
231 19cb3738 bellard
        return &bdrv_raw;
232 19cb3738 bellard
#endif
233 83f64091 bellard
    p = strchr(filename, ':');
234 83f64091 bellard
    if (!p)
235 83f64091 bellard
        return &bdrv_raw;
236 83f64091 bellard
    len = p - filename;
237 83f64091 bellard
    if (len > sizeof(protocol) - 1)
238 83f64091 bellard
        len = sizeof(protocol) - 1;
239 83f64091 bellard
    memcpy(protocol, filename, len);
240 83f64091 bellard
    protocol[len] = '\0';
241 83f64091 bellard
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
242 5fafdf24 ths
        if (drv1->protocol_name &&
243 83f64091 bellard
            !strcmp(drv1->protocol_name, protocol))
244 83f64091 bellard
            return drv1;
245 83f64091 bellard
    }
246 83f64091 bellard
    return NULL;
247 83f64091 bellard
}
248 83f64091 bellard
249 7674e7bf bellard
/* XXX: force raw format if block or character device ? It would
250 7674e7bf bellard
   simplify the BSD case */
251 ea2384d3 bellard
static BlockDriver *find_image_format(const char *filename)
252 ea2384d3 bellard
{
253 83f64091 bellard
    int ret, score, score_max;
254 ea2384d3 bellard
    BlockDriver *drv1, *drv;
255 83f64091 bellard
    uint8_t buf[2048];
256 83f64091 bellard
    BlockDriverState *bs;
257 3b46e624 ths
258 19cb3738 bellard
    /* detect host devices. By convention, /dev/cdrom[N] is always
259 19cb3738 bellard
       recognized as a host CDROM */
260 19cb3738 bellard
    if (strstart(filename, "/dev/cdrom", NULL))
261 19cb3738 bellard
        return &bdrv_host_device;
262 19cb3738 bellard
#ifdef _WIN32
263 19cb3738 bellard
    if (is_windows_drive(filename))
264 19cb3738 bellard
        return &bdrv_host_device;
265 19cb3738 bellard
#else
266 19cb3738 bellard
    {
267 19cb3738 bellard
        struct stat st;
268 5fafdf24 ths
        if (stat(filename, &st) >= 0 &&
269 19cb3738 bellard
            (S_ISCHR(st.st_mode) || S_ISBLK(st.st_mode))) {
270 19cb3738 bellard
            return &bdrv_host_device;
271 19cb3738 bellard
        }
272 19cb3738 bellard
    }
273 19cb3738 bellard
#endif
274 3b46e624 ths
275 83f64091 bellard
    drv = find_protocol(filename);
276 19cb3738 bellard
    /* no need to test disk image formats for vvfat */
277 83f64091 bellard
    if (drv == &bdrv_vvfat)
278 83f64091 bellard
        return drv;
279 19cb3738 bellard
280 83f64091 bellard
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
281 83f64091 bellard
    if (ret < 0)
282 83f64091 bellard
        return NULL;
283 83f64091 bellard
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
284 83f64091 bellard
    bdrv_delete(bs);
285 83f64091 bellard
    if (ret < 0) {
286 83f64091 bellard
        return NULL;
287 83f64091 bellard
    }
288 83f64091 bellard
289 ea2384d3 bellard
    score_max = 0;
290 ea2384d3 bellard
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
291 83f64091 bellard
        if (drv1->bdrv_probe) {
292 83f64091 bellard
            score = drv1->bdrv_probe(buf, ret, filename);
293 83f64091 bellard
            if (score > score_max) {
294 83f64091 bellard
                score_max = score;
295 83f64091 bellard
                drv = drv1;
296 83f64091 bellard
            }
297 0849bf08 bellard
        }
298 fc01f7e7 bellard
    }
299 ea2384d3 bellard
    return drv;
300 ea2384d3 bellard
}
301 ea2384d3 bellard
302 83f64091 bellard
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
303 ea2384d3 bellard
{
304 83f64091 bellard
    BlockDriverState *bs;
305 83f64091 bellard
    int ret;
306 83f64091 bellard
307 83f64091 bellard
    bs = bdrv_new("");
308 83f64091 bellard
    if (!bs)
309 83f64091 bellard
        return -ENOMEM;
310 83f64091 bellard
    ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
311 83f64091 bellard
    if (ret < 0) {
312 83f64091 bellard
        bdrv_delete(bs);
313 83f64091 bellard
        return ret;
314 3b0d4f61 bellard
    }
315 83f64091 bellard
    *pbs = bs;
316 83f64091 bellard
    return 0;
317 83f64091 bellard
}
318 83f64091 bellard
319 83f64091 bellard
int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
320 83f64091 bellard
{
321 83f64091 bellard
    return bdrv_open2(bs, filename, flags, NULL);
322 ea2384d3 bellard
}
323 ea2384d3 bellard
324 83f64091 bellard
int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
325 ea2384d3 bellard
               BlockDriver *drv)
326 ea2384d3 bellard
{
327 83f64091 bellard
    int ret, open_flags;
328 eb5c851f ths
    char tmp_filename[PATH_MAX];
329 eb5c851f ths
    char backing_filename[PATH_MAX];
330 3b46e624 ths
331 ea2384d3 bellard
    bs->read_only = 0;
332 ea2384d3 bellard
    bs->is_temporary = 0;
333 ea2384d3 bellard
    bs->encrypted = 0;
334 712e7874 bellard
335 83f64091 bellard
    if (flags & BDRV_O_SNAPSHOT) {
336 ea2384d3 bellard
        BlockDriverState *bs1;
337 ea2384d3 bellard
        int64_t total_size;
338 3b46e624 ths
339 ea2384d3 bellard
        /* if snapshot, we create a temporary backing file and open it
340 ea2384d3 bellard
           instead of opening 'filename' directly */
341 33e3963e bellard
342 ea2384d3 bellard
        /* if there is a backing file, use it */
343 ea2384d3 bellard
        bs1 = bdrv_new("");
344 ea2384d3 bellard
        if (!bs1) {
345 83f64091 bellard
            return -ENOMEM;
346 ea2384d3 bellard
        }
347 ea2384d3 bellard
        if (bdrv_open(bs1, filename, 0) < 0) {
348 ea2384d3 bellard
            bdrv_delete(bs1);
349 ea2384d3 bellard
            return -1;
350 ea2384d3 bellard
        }
351 83f64091 bellard
        total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
352 ea2384d3 bellard
        bdrv_delete(bs1);
353 3b46e624 ths
354 ea2384d3 bellard
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
355 a817d936 bellard
        realpath(filename, backing_filename);
356 5fafdf24 ths
        if (bdrv_create(&bdrv_qcow2, tmp_filename,
357 a817d936 bellard
                        total_size, backing_filename, 0) < 0) {
358 ea2384d3 bellard
            return -1;
359 ea2384d3 bellard
        }
360 ea2384d3 bellard
        filename = tmp_filename;
361 ea2384d3 bellard
        bs->is_temporary = 1;
362 ea2384d3 bellard
    }
363 712e7874 bellard
364 ea2384d3 bellard
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
365 83f64091 bellard
    if (flags & BDRV_O_FILE) {
366 83f64091 bellard
        drv = find_protocol(filename);
367 ea2384d3 bellard
        if (!drv)
368 83f64091 bellard
            return -ENOENT;
369 83f64091 bellard
    } else {
370 83f64091 bellard
        if (!drv) {
371 83f64091 bellard
            drv = find_image_format(filename);
372 83f64091 bellard
            if (!drv)
373 83f64091 bellard
                return -1;
374 83f64091 bellard
        }
375 ea2384d3 bellard
    }
376 ea2384d3 bellard
    bs->drv = drv;
377 ea2384d3 bellard
    bs->opaque = qemu_mallocz(drv->instance_size);
378 ea2384d3 bellard
    if (bs->opaque == NULL && drv->instance_size > 0)
379 ea2384d3 bellard
        return -1;
380 83f64091 bellard
    /* Note: for compatibility, we open disk image files as RDWR, and
381 83f64091 bellard
       RDONLY as fallback */
382 83f64091 bellard
    if (!(flags & BDRV_O_FILE))
383 83f64091 bellard
        open_flags = BDRV_O_RDWR;
384 83f64091 bellard
    else
385 83f64091 bellard
        open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
386 83f64091 bellard
    ret = drv->bdrv_open(bs, filename, open_flags);
387 83f64091 bellard
    if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
388 83f64091 bellard
        ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
389 83f64091 bellard
        bs->read_only = 1;
390 83f64091 bellard
    }
391 ea2384d3 bellard
    if (ret < 0) {
392 ea2384d3 bellard
        qemu_free(bs->opaque);
393 6b21b973 bellard
        bs->opaque = NULL;
394 6b21b973 bellard
        bs->drv = NULL;
395 83f64091 bellard
        return ret;
396 33e3963e bellard
    }
397 d15a771d bellard
    if (drv->bdrv_getlength) {
398 d15a771d bellard
        bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
399 d15a771d bellard
    }
400 67b915a5 bellard
#ifndef _WIN32
401 ea2384d3 bellard
    if (bs->is_temporary) {
402 ea2384d3 bellard
        unlink(filename);
403 ea2384d3 bellard
    }
404 ea2384d3 bellard
#endif
405 83f64091 bellard
    if (bs->backing_file[0] != '\0') {
406 ea2384d3 bellard
        /* if there is a backing file, use it */
407 ea2384d3 bellard
        bs->backing_hd = bdrv_new("");
408 ea2384d3 bellard
        if (!bs->backing_hd) {
409 ea2384d3 bellard
        fail:
410 ea2384d3 bellard
            bdrv_close(bs);
411 6b21b973 bellard
            return -ENOMEM;
412 33e3963e bellard
        }
413 83f64091 bellard
        path_combine(backing_filename, sizeof(backing_filename),
414 83f64091 bellard
                     filename, bs->backing_file);
415 83f64091 bellard
        if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
416 33e3963e bellard
            goto fail;
417 33e3963e bellard
    }
418 33e3963e bellard
419 b338082b bellard
    /* call the change callback */
420 19cb3738 bellard
    bs->media_changed = 1;
421 b338082b bellard
    if (bs->change_cb)
422 b338082b bellard
        bs->change_cb(bs->change_opaque);
423 b338082b bellard
424 b338082b bellard
    return 0;
425 fc01f7e7 bellard
}
426 fc01f7e7 bellard
427 fc01f7e7 bellard
void bdrv_close(BlockDriverState *bs)
428 fc01f7e7 bellard
{
429 19cb3738 bellard
    if (bs->drv) {
430 ea2384d3 bellard
        if (bs->backing_hd)
431 ea2384d3 bellard
            bdrv_delete(bs->backing_hd);
432 ea2384d3 bellard
        bs->drv->bdrv_close(bs);
433 ea2384d3 bellard
        qemu_free(bs->opaque);
434 ea2384d3 bellard
#ifdef _WIN32
435 ea2384d3 bellard
        if (bs->is_temporary) {
436 ea2384d3 bellard
            unlink(bs->filename);
437 ea2384d3 bellard
        }
438 67b915a5 bellard
#endif
439 ea2384d3 bellard
        bs->opaque = NULL;
440 ea2384d3 bellard
        bs->drv = NULL;
441 b338082b bellard
442 b338082b bellard
        /* call the change callback */
443 19cb3738 bellard
        bs->media_changed = 1;
444 b338082b bellard
        if (bs->change_cb)
445 b338082b bellard
            bs->change_cb(bs->change_opaque);
446 b338082b bellard
    }
447 b338082b bellard
}
448 b338082b bellard
449 b338082b bellard
void bdrv_delete(BlockDriverState *bs)
450 b338082b bellard
{
451 ea2384d3 bellard
    /* XXX: remove the driver list */
452 b338082b bellard
    bdrv_close(bs);
453 b338082b bellard
    qemu_free(bs);
454 fc01f7e7 bellard
}
455 fc01f7e7 bellard
456 33e3963e bellard
/* commit COW file into the raw image */
457 33e3963e bellard
int bdrv_commit(BlockDriverState *bs)
458 33e3963e bellard
{
459 19cb3738 bellard
    BlockDriver *drv = bs->drv;
460 83f64091 bellard
    int64_t i, total_sectors;
461 ea2384d3 bellard
    int n, j;
462 ea2384d3 bellard
    unsigned char sector[512];
463 33e3963e bellard
464 19cb3738 bellard
    if (!drv)
465 19cb3738 bellard
        return -ENOMEDIUM;
466 33e3963e bellard
467 33e3963e bellard
    if (bs->read_only) {
468 ea2384d3 bellard
        return -EACCES;
469 33e3963e bellard
    }
470 33e3963e bellard
471 ea2384d3 bellard
    if (!bs->backing_hd) {
472 ea2384d3 bellard
        return -ENOTSUP;
473 ea2384d3 bellard
    }
474 33e3963e bellard
475 83f64091 bellard
    total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
476 83f64091 bellard
    for (i = 0; i < total_sectors;) {
477 19cb3738 bellard
        if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
478 ea2384d3 bellard
            for(j = 0; j < n; j++) {
479 ea2384d3 bellard
                if (bdrv_read(bs, i, sector, 1) != 0) {
480 ea2384d3 bellard
                    return -EIO;
481 ea2384d3 bellard
                }
482 ea2384d3 bellard
483 ea2384d3 bellard
                if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
484 ea2384d3 bellard
                    return -EIO;
485 ea2384d3 bellard
                }
486 ea2384d3 bellard
                i++;
487 33e3963e bellard
            }
488 ea2384d3 bellard
        } else {
489 ea2384d3 bellard
            i += n;
490 ea2384d3 bellard
        }
491 33e3963e bellard
    }
492 95389c86 bellard
493 19cb3738 bellard
    if (drv->bdrv_make_empty)
494 19cb3738 bellard
        return drv->bdrv_make_empty(bs);
495 95389c86 bellard
496 33e3963e bellard
    return 0;
497 33e3963e bellard
}
498 33e3963e bellard
499 19cb3738 bellard
/* return < 0 if error. See bdrv_write() for the return codes */
500 5fafdf24 ths
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
501 fc01f7e7 bellard
              uint8_t *buf, int nb_sectors)
502 fc01f7e7 bellard
{
503 ea2384d3 bellard
    BlockDriver *drv = bs->drv;
504 ea2384d3 bellard
505 19cb3738 bellard
    if (!drv)
506 19cb3738 bellard
        return -ENOMEDIUM;
507 b338082b bellard
508 83f64091 bellard
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
509 cf98951b bellard
            memcpy(buf, bs->boot_sector_data, 512);
510 83f64091 bellard
        sector_num++;
511 83f64091 bellard
        nb_sectors--;
512 83f64091 bellard
        buf += 512;
513 83f64091 bellard
        if (nb_sectors == 0)
514 83f64091 bellard
            return 0;
515 83f64091 bellard
    }
516 83f64091 bellard
    if (drv->bdrv_pread) {
517 83f64091 bellard
        int ret, len;
518 83f64091 bellard
        len = nb_sectors * 512;
519 83f64091 bellard
        ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
520 83f64091 bellard
        if (ret < 0)
521 83f64091 bellard
            return ret;
522 83f64091 bellard
        else if (ret != len)
523 19cb3738 bellard
            return -EINVAL;
524 83f64091 bellard
        else
525 83f64091 bellard
            return 0;
526 83f64091 bellard
    } else {
527 83f64091 bellard
        return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
528 33e3963e bellard
    }
529 fc01f7e7 bellard
}
530 fc01f7e7 bellard
531 5fafdf24 ths
/* Return < 0 if error. Important errors are:
532 19cb3738 bellard
  -EIO         generic I/O error (may happen for all errors)
533 19cb3738 bellard
  -ENOMEDIUM   No media inserted.
534 19cb3738 bellard
  -EINVAL      Invalid sector number or nb_sectors
535 19cb3738 bellard
  -EACCES      Trying to write a read-only device
536 19cb3738 bellard
*/
537 5fafdf24 ths
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
538 fc01f7e7 bellard
               const uint8_t *buf, int nb_sectors)
539 fc01f7e7 bellard
{
540 83f64091 bellard
    BlockDriver *drv = bs->drv;
541 19cb3738 bellard
    if (!bs->drv)
542 19cb3738 bellard
        return -ENOMEDIUM;
543 0849bf08 bellard
    if (bs->read_only)
544 19cb3738 bellard
        return -EACCES;
545 79639d42 bellard
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
546 3b46e624 ths
        memcpy(bs->boot_sector_data, buf, 512);
547 79639d42 bellard
    }
548 83f64091 bellard
    if (drv->bdrv_pwrite) {
549 83f64091 bellard
        int ret, len;
550 83f64091 bellard
        len = nb_sectors * 512;
551 83f64091 bellard
        ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
552 83f64091 bellard
        if (ret < 0)
553 83f64091 bellard
            return ret;
554 83f64091 bellard
        else if (ret != len)
555 83f64091 bellard
            return -EIO;
556 83f64091 bellard
        else
557 83f64091 bellard
            return 0;
558 83f64091 bellard
    } else {
559 83f64091 bellard
        return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
560 83f64091 bellard
    }
561 83f64091 bellard
}
562 83f64091 bellard
563 5fafdf24 ths
static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
564 faea38e7 bellard
                         uint8_t *buf, int count1)
565 83f64091 bellard
{
566 83f64091 bellard
    uint8_t tmp_buf[SECTOR_SIZE];
567 83f64091 bellard
    int len, nb_sectors, count;
568 83f64091 bellard
    int64_t sector_num;
569 83f64091 bellard
570 83f64091 bellard
    count = count1;
571 83f64091 bellard
    /* first read to align to sector start */
572 83f64091 bellard
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
573 83f64091 bellard
    if (len > count)
574 83f64091 bellard
        len = count;
575 83f64091 bellard
    sector_num = offset >> SECTOR_BITS;
576 83f64091 bellard
    if (len > 0) {
577 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
578 83f64091 bellard
            return -EIO;
579 83f64091 bellard
        memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
580 83f64091 bellard
        count -= len;
581 83f64091 bellard
        if (count == 0)
582 83f64091 bellard
            return count1;
583 83f64091 bellard
        sector_num++;
584 83f64091 bellard
        buf += len;
585 83f64091 bellard
    }
586 83f64091 bellard
587 83f64091 bellard
    /* read the sectors "in place" */
588 83f64091 bellard
    nb_sectors = count >> SECTOR_BITS;
589 83f64091 bellard
    if (nb_sectors > 0) {
590 83f64091 bellard
        if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
591 83f64091 bellard
            return -EIO;
592 83f64091 bellard
        sector_num += nb_sectors;
593 83f64091 bellard
        len = nb_sectors << SECTOR_BITS;
594 83f64091 bellard
        buf += len;
595 83f64091 bellard
        count -= len;
596 83f64091 bellard
    }
597 83f64091 bellard
598 83f64091 bellard
    /* add data from the last sector */
599 83f64091 bellard
    if (count > 0) {
600 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
601 83f64091 bellard
            return -EIO;
602 83f64091 bellard
        memcpy(buf, tmp_buf, count);
603 83f64091 bellard
    }
604 83f64091 bellard
    return count1;
605 83f64091 bellard
}
606 83f64091 bellard
607 5fafdf24 ths
static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
608 faea38e7 bellard
                          const uint8_t *buf, int count1)
609 83f64091 bellard
{
610 83f64091 bellard
    uint8_t tmp_buf[SECTOR_SIZE];
611 83f64091 bellard
    int len, nb_sectors, count;
612 83f64091 bellard
    int64_t sector_num;
613 83f64091 bellard
614 83f64091 bellard
    count = count1;
615 83f64091 bellard
    /* first write to align to sector start */
616 83f64091 bellard
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
617 83f64091 bellard
    if (len > count)
618 83f64091 bellard
        len = count;
619 83f64091 bellard
    sector_num = offset >> SECTOR_BITS;
620 83f64091 bellard
    if (len > 0) {
621 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
622 83f64091 bellard
            return -EIO;
623 83f64091 bellard
        memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
624 83f64091 bellard
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
625 83f64091 bellard
            return -EIO;
626 83f64091 bellard
        count -= len;
627 83f64091 bellard
        if (count == 0)
628 83f64091 bellard
            return count1;
629 83f64091 bellard
        sector_num++;
630 83f64091 bellard
        buf += len;
631 83f64091 bellard
    }
632 83f64091 bellard
633 83f64091 bellard
    /* write the sectors "in place" */
634 83f64091 bellard
    nb_sectors = count >> SECTOR_BITS;
635 83f64091 bellard
    if (nb_sectors > 0) {
636 83f64091 bellard
        if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
637 83f64091 bellard
            return -EIO;
638 83f64091 bellard
        sector_num += nb_sectors;
639 83f64091 bellard
        len = nb_sectors << SECTOR_BITS;
640 83f64091 bellard
        buf += len;
641 83f64091 bellard
        count -= len;
642 83f64091 bellard
    }
643 83f64091 bellard
644 83f64091 bellard
    /* add data from the last sector */
645 83f64091 bellard
    if (count > 0) {
646 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
647 83f64091 bellard
            return -EIO;
648 83f64091 bellard
        memcpy(tmp_buf, buf, count);
649 83f64091 bellard
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
650 83f64091 bellard
            return -EIO;
651 83f64091 bellard
    }
652 83f64091 bellard
    return count1;
653 83f64091 bellard
}
654 83f64091 bellard
655 83f64091 bellard
/**
656 5fafdf24 ths
 * Read with byte offsets (needed only for file protocols)
657 83f64091 bellard
 */
658 5fafdf24 ths
int bdrv_pread(BlockDriverState *bs, int64_t offset,
659 83f64091 bellard
               void *buf1, int count1)
660 83f64091 bellard
{
661 83f64091 bellard
    BlockDriver *drv = bs->drv;
662 83f64091 bellard
663 83f64091 bellard
    if (!drv)
664 19cb3738 bellard
        return -ENOMEDIUM;
665 83f64091 bellard
    if (!drv->bdrv_pread)
666 faea38e7 bellard
        return bdrv_pread_em(bs, offset, buf1, count1);
667 83f64091 bellard
    return drv->bdrv_pread(bs, offset, buf1, count1);
668 83f64091 bellard
}
669 83f64091 bellard
670 5fafdf24 ths
/**
671 5fafdf24 ths
 * Write with byte offsets (needed only for file protocols)
672 83f64091 bellard
 */
673 5fafdf24 ths
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
674 83f64091 bellard
                const void *buf1, int count1)
675 83f64091 bellard
{
676 83f64091 bellard
    BlockDriver *drv = bs->drv;
677 83f64091 bellard
678 83f64091 bellard
    if (!drv)
679 19cb3738 bellard
        return -ENOMEDIUM;
680 83f64091 bellard
    if (!drv->bdrv_pwrite)
681 faea38e7 bellard
        return bdrv_pwrite_em(bs, offset, buf1, count1);
682 83f64091 bellard
    return drv->bdrv_pwrite(bs, offset, buf1, count1);
683 83f64091 bellard
}
684 83f64091 bellard
685 83f64091 bellard
/**
686 83f64091 bellard
 * Truncate file to 'offset' bytes (needed only for file protocols)
687 83f64091 bellard
 */
688 83f64091 bellard
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
689 83f64091 bellard
{
690 83f64091 bellard
    BlockDriver *drv = bs->drv;
691 83f64091 bellard
    if (!drv)
692 19cb3738 bellard
        return -ENOMEDIUM;
693 83f64091 bellard
    if (!drv->bdrv_truncate)
694 83f64091 bellard
        return -ENOTSUP;
695 83f64091 bellard
    return drv->bdrv_truncate(bs, offset);
696 83f64091 bellard
}
697 83f64091 bellard
698 83f64091 bellard
/**
699 83f64091 bellard
 * Length of a file in bytes. Return < 0 if error or unknown.
700 83f64091 bellard
 */
701 83f64091 bellard
int64_t bdrv_getlength(BlockDriverState *bs)
702 83f64091 bellard
{
703 83f64091 bellard
    BlockDriver *drv = bs->drv;
704 83f64091 bellard
    if (!drv)
705 19cb3738 bellard
        return -ENOMEDIUM;
706 83f64091 bellard
    if (!drv->bdrv_getlength) {
707 83f64091 bellard
        /* legacy mode */
708 83f64091 bellard
        return bs->total_sectors * SECTOR_SIZE;
709 83f64091 bellard
    }
710 83f64091 bellard
    return drv->bdrv_getlength(bs);
711 fc01f7e7 bellard
}
712 fc01f7e7 bellard
713 19cb3738 bellard
/* return 0 as number of sectors if no device present or error */
714 fc01f7e7 bellard
void bdrv_get_geometry(BlockDriverState *bs, int64_t *nb_sectors_ptr)
715 fc01f7e7 bellard
{
716 19cb3738 bellard
    int64_t length;
717 19cb3738 bellard
    length = bdrv_getlength(bs);
718 19cb3738 bellard
    if (length < 0)
719 19cb3738 bellard
        length = 0;
720 19cb3738 bellard
    else
721 19cb3738 bellard
        length = length >> SECTOR_BITS;
722 19cb3738 bellard
    *nb_sectors_ptr = length;
723 fc01f7e7 bellard
}
724 cf98951b bellard
725 cf98951b bellard
/* force a given boot sector. */
726 cf98951b bellard
void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
727 cf98951b bellard
{
728 cf98951b bellard
    bs->boot_sector_enabled = 1;
729 cf98951b bellard
    if (size > 512)
730 cf98951b bellard
        size = 512;
731 cf98951b bellard
    memcpy(bs->boot_sector_data, data, size);
732 cf98951b bellard
    memset(bs->boot_sector_data + size, 0, 512 - size);
733 cf98951b bellard
}
734 b338082b bellard
735 5fafdf24 ths
void bdrv_set_geometry_hint(BlockDriverState *bs,
736 b338082b bellard
                            int cyls, int heads, int secs)
737 b338082b bellard
{
738 b338082b bellard
    bs->cyls = cyls;
739 b338082b bellard
    bs->heads = heads;
740 b338082b bellard
    bs->secs = secs;
741 b338082b bellard
}
742 b338082b bellard
743 b338082b bellard
void bdrv_set_type_hint(BlockDriverState *bs, int type)
744 b338082b bellard
{
745 b338082b bellard
    bs->type = type;
746 b338082b bellard
    bs->removable = ((type == BDRV_TYPE_CDROM ||
747 b338082b bellard
                      type == BDRV_TYPE_FLOPPY));
748 b338082b bellard
}
749 b338082b bellard
750 46d4767d bellard
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
751 46d4767d bellard
{
752 46d4767d bellard
    bs->translation = translation;
753 46d4767d bellard
}
754 46d4767d bellard
755 5fafdf24 ths
void bdrv_get_geometry_hint(BlockDriverState *bs,
756 b338082b bellard
                            int *pcyls, int *pheads, int *psecs)
757 b338082b bellard
{
758 b338082b bellard
    *pcyls = bs->cyls;
759 b338082b bellard
    *pheads = bs->heads;
760 b338082b bellard
    *psecs = bs->secs;
761 b338082b bellard
}
762 b338082b bellard
763 b338082b bellard
int bdrv_get_type_hint(BlockDriverState *bs)
764 b338082b bellard
{
765 b338082b bellard
    return bs->type;
766 b338082b bellard
}
767 b338082b bellard
768 46d4767d bellard
int bdrv_get_translation_hint(BlockDriverState *bs)
769 46d4767d bellard
{
770 46d4767d bellard
    return bs->translation;
771 46d4767d bellard
}
772 46d4767d bellard
773 b338082b bellard
int bdrv_is_removable(BlockDriverState *bs)
774 b338082b bellard
{
775 b338082b bellard
    return bs->removable;
776 b338082b bellard
}
777 b338082b bellard
778 b338082b bellard
int bdrv_is_read_only(BlockDriverState *bs)
779 b338082b bellard
{
780 b338082b bellard
    return bs->read_only;
781 b338082b bellard
}
782 b338082b bellard
783 19cb3738 bellard
/* XXX: no longer used */
784 5fafdf24 ths
void bdrv_set_change_cb(BlockDriverState *bs,
785 b338082b bellard
                        void (*change_cb)(void *opaque), void *opaque)
786 b338082b bellard
{
787 b338082b bellard
    bs->change_cb = change_cb;
788 b338082b bellard
    bs->change_opaque = opaque;
789 b338082b bellard
}
790 b338082b bellard
791 ea2384d3 bellard
int bdrv_is_encrypted(BlockDriverState *bs)
792 ea2384d3 bellard
{
793 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted)
794 ea2384d3 bellard
        return 1;
795 ea2384d3 bellard
    return bs->encrypted;
796 ea2384d3 bellard
}
797 ea2384d3 bellard
798 ea2384d3 bellard
int bdrv_set_key(BlockDriverState *bs, const char *key)
799 ea2384d3 bellard
{
800 ea2384d3 bellard
    int ret;
801 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted) {
802 ea2384d3 bellard
        ret = bdrv_set_key(bs->backing_hd, key);
803 ea2384d3 bellard
        if (ret < 0)
804 ea2384d3 bellard
            return ret;
805 ea2384d3 bellard
        if (!bs->encrypted)
806 ea2384d3 bellard
            return 0;
807 ea2384d3 bellard
    }
808 ea2384d3 bellard
    if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
809 ea2384d3 bellard
        return -1;
810 ea2384d3 bellard
    return bs->drv->bdrv_set_key(bs, key);
811 ea2384d3 bellard
}
812 ea2384d3 bellard
813 ea2384d3 bellard
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
814 ea2384d3 bellard
{
815 19cb3738 bellard
    if (!bs->drv) {
816 ea2384d3 bellard
        buf[0] = '\0';
817 ea2384d3 bellard
    } else {
818 ea2384d3 bellard
        pstrcpy(buf, buf_size, bs->drv->format_name);
819 ea2384d3 bellard
    }
820 ea2384d3 bellard
}
821 ea2384d3 bellard
822 5fafdf24 ths
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
823 ea2384d3 bellard
                         void *opaque)
824 ea2384d3 bellard
{
825 ea2384d3 bellard
    BlockDriver *drv;
826 ea2384d3 bellard
827 ea2384d3 bellard
    for (drv = first_drv; drv != NULL; drv = drv->next) {
828 ea2384d3 bellard
        it(opaque, drv->format_name);
829 ea2384d3 bellard
    }
830 ea2384d3 bellard
}
831 ea2384d3 bellard
832 b338082b bellard
BlockDriverState *bdrv_find(const char *name)
833 b338082b bellard
{
834 b338082b bellard
    BlockDriverState *bs;
835 b338082b bellard
836 b338082b bellard
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
837 b338082b bellard
        if (!strcmp(name, bs->device_name))
838 b338082b bellard
            return bs;
839 b338082b bellard
    }
840 b338082b bellard
    return NULL;
841 b338082b bellard
}
842 b338082b bellard
843 81d0912d bellard
void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
844 81d0912d bellard
{
845 81d0912d bellard
    BlockDriverState *bs;
846 81d0912d bellard
847 81d0912d bellard
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
848 81d0912d bellard
        it(opaque, bs->device_name);
849 81d0912d bellard
    }
850 81d0912d bellard
}
851 81d0912d bellard
852 ea2384d3 bellard
const char *bdrv_get_device_name(BlockDriverState *bs)
853 ea2384d3 bellard
{
854 ea2384d3 bellard
    return bs->device_name;
855 ea2384d3 bellard
}
856 ea2384d3 bellard
857 7a6cba61 pbrook
void bdrv_flush(BlockDriverState *bs)
858 7a6cba61 pbrook
{
859 7a6cba61 pbrook
    if (bs->drv->bdrv_flush)
860 7a6cba61 pbrook
        bs->drv->bdrv_flush(bs);
861 7a6cba61 pbrook
    if (bs->backing_hd)
862 7a6cba61 pbrook
        bdrv_flush(bs->backing_hd);
863 7a6cba61 pbrook
}
864 7a6cba61 pbrook
865 faf07963 pbrook
#ifndef QEMU_IMG
866 b338082b bellard
void bdrv_info(void)
867 b338082b bellard
{
868 b338082b bellard
    BlockDriverState *bs;
869 b338082b bellard
870 b338082b bellard
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
871 b338082b bellard
        term_printf("%s:", bs->device_name);
872 b338082b bellard
        term_printf(" type=");
873 b338082b bellard
        switch(bs->type) {
874 b338082b bellard
        case BDRV_TYPE_HD:
875 b338082b bellard
            term_printf("hd");
876 b338082b bellard
            break;
877 b338082b bellard
        case BDRV_TYPE_CDROM:
878 b338082b bellard
            term_printf("cdrom");
879 b338082b bellard
            break;
880 b338082b bellard
        case BDRV_TYPE_FLOPPY:
881 b338082b bellard
            term_printf("floppy");
882 b338082b bellard
            break;
883 b338082b bellard
        }
884 b338082b bellard
        term_printf(" removable=%d", bs->removable);
885 b338082b bellard
        if (bs->removable) {
886 b338082b bellard
            term_printf(" locked=%d", bs->locked);
887 b338082b bellard
        }
888 19cb3738 bellard
        if (bs->drv) {
889 fef30743 ths
            term_printf(" file=");
890 fef30743 ths
            term_print_filename(bs->filename);
891 fef30743 ths
            if (bs->backing_file[0] != '\0') {
892 fef30743 ths
                term_printf(" backing_file=");
893 fef30743 ths
                term_print_filename(bs->backing_file);
894 fef30743 ths
            }
895 b338082b bellard
            term_printf(" ro=%d", bs->read_only);
896 ea2384d3 bellard
            term_printf(" drv=%s", bs->drv->format_name);
897 ea2384d3 bellard
            if (bs->encrypted)
898 ea2384d3 bellard
                term_printf(" encrypted");
899 b338082b bellard
        } else {
900 b338082b bellard
            term_printf(" [not inserted]");
901 b338082b bellard
        }
902 b338082b bellard
        term_printf("\n");
903 b338082b bellard
    }
904 b338082b bellard
}
905 faf07963 pbrook
#endif
906 ea2384d3 bellard
907 5fafdf24 ths
void bdrv_get_backing_filename(BlockDriverState *bs,
908 83f64091 bellard
                               char *filename, int filename_size)
909 83f64091 bellard
{
910 83f64091 bellard
    if (!bs->backing_hd) {
911 83f64091 bellard
        pstrcpy(filename, filename_size, "");
912 83f64091 bellard
    } else {
913 83f64091 bellard
        pstrcpy(filename, filename_size, bs->backing_file);
914 83f64091 bellard
    }
915 83f64091 bellard
}
916 83f64091 bellard
917 5fafdf24 ths
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
918 faea38e7 bellard
                          const uint8_t *buf, int nb_sectors)
919 faea38e7 bellard
{
920 faea38e7 bellard
    BlockDriver *drv = bs->drv;
921 faea38e7 bellard
    if (!drv)
922 19cb3738 bellard
        return -ENOMEDIUM;
923 faea38e7 bellard
    if (!drv->bdrv_write_compressed)
924 faea38e7 bellard
        return -ENOTSUP;
925 faea38e7 bellard
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
926 faea38e7 bellard
}
927 3b46e624 ths
928 faea38e7 bellard
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
929 faea38e7 bellard
{
930 faea38e7 bellard
    BlockDriver *drv = bs->drv;
931 faea38e7 bellard
    if (!drv)
932 19cb3738 bellard
        return -ENOMEDIUM;
933 faea38e7 bellard
    if (!drv->bdrv_get_info)
934 faea38e7 bellard
        return -ENOTSUP;
935 faea38e7 bellard
    memset(bdi, 0, sizeof(*bdi));
936 faea38e7 bellard
    return drv->bdrv_get_info(bs, bdi);
937 faea38e7 bellard
}
938 faea38e7 bellard
939 faea38e7 bellard
/**************************************************************/
940 faea38e7 bellard
/* handling of snapshots */
941 faea38e7 bellard
942 5fafdf24 ths
int bdrv_snapshot_create(BlockDriverState *bs,
943 faea38e7 bellard
                         QEMUSnapshotInfo *sn_info)
944 faea38e7 bellard
{
945 faea38e7 bellard
    BlockDriver *drv = bs->drv;
946 faea38e7 bellard
    if (!drv)
947 19cb3738 bellard
        return -ENOMEDIUM;
948 faea38e7 bellard
    if (!drv->bdrv_snapshot_create)
949 faea38e7 bellard
        return -ENOTSUP;
950 faea38e7 bellard
    return drv->bdrv_snapshot_create(bs, sn_info);
951 faea38e7 bellard
}
952 faea38e7 bellard
953 5fafdf24 ths
int bdrv_snapshot_goto(BlockDriverState *bs,
954 faea38e7 bellard
                       const char *snapshot_id)
955 faea38e7 bellard
{
956 faea38e7 bellard
    BlockDriver *drv = bs->drv;
957 faea38e7 bellard
    if (!drv)
958 19cb3738 bellard
        return -ENOMEDIUM;
959 faea38e7 bellard
    if (!drv->bdrv_snapshot_goto)
960 faea38e7 bellard
        return -ENOTSUP;
961 faea38e7 bellard
    return drv->bdrv_snapshot_goto(bs, snapshot_id);
962 faea38e7 bellard
}
963 faea38e7 bellard
964 faea38e7 bellard
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
965 faea38e7 bellard
{
966 faea38e7 bellard
    BlockDriver *drv = bs->drv;
967 faea38e7 bellard
    if (!drv)
968 19cb3738 bellard
        return -ENOMEDIUM;
969 faea38e7 bellard
    if (!drv->bdrv_snapshot_delete)
970 faea38e7 bellard
        return -ENOTSUP;
971 faea38e7 bellard
    return drv->bdrv_snapshot_delete(bs, snapshot_id);
972 faea38e7 bellard
}
973 faea38e7 bellard
974 5fafdf24 ths
int bdrv_snapshot_list(BlockDriverState *bs,
975 faea38e7 bellard
                       QEMUSnapshotInfo **psn_info)
976 faea38e7 bellard
{
977 faea38e7 bellard
    BlockDriver *drv = bs->drv;
978 faea38e7 bellard
    if (!drv)
979 19cb3738 bellard
        return -ENOMEDIUM;
980 faea38e7 bellard
    if (!drv->bdrv_snapshot_list)
981 faea38e7 bellard
        return -ENOTSUP;
982 faea38e7 bellard
    return drv->bdrv_snapshot_list(bs, psn_info);
983 faea38e7 bellard
}
984 faea38e7 bellard
985 faea38e7 bellard
#define NB_SUFFIXES 4
986 faea38e7 bellard
987 faea38e7 bellard
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
988 faea38e7 bellard
{
989 faea38e7 bellard
    static const char suffixes[NB_SUFFIXES] = "KMGT";
990 faea38e7 bellard
    int64_t base;
991 faea38e7 bellard
    int i;
992 faea38e7 bellard
993 faea38e7 bellard
    if (size <= 999) {
994 faea38e7 bellard
        snprintf(buf, buf_size, "%" PRId64, size);
995 faea38e7 bellard
    } else {
996 faea38e7 bellard
        base = 1024;
997 faea38e7 bellard
        for(i = 0; i < NB_SUFFIXES; i++) {
998 faea38e7 bellard
            if (size < (10 * base)) {
999 5fafdf24 ths
                snprintf(buf, buf_size, "%0.1f%c",
1000 faea38e7 bellard
                         (double)size / base,
1001 faea38e7 bellard
                         suffixes[i]);
1002 faea38e7 bellard
                break;
1003 faea38e7 bellard
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1004 5fafdf24 ths
                snprintf(buf, buf_size, "%" PRId64 "%c",
1005 faea38e7 bellard
                         ((size + (base >> 1)) / base),
1006 faea38e7 bellard
                         suffixes[i]);
1007 faea38e7 bellard
                break;
1008 faea38e7 bellard
            }
1009 faea38e7 bellard
            base = base * 1024;
1010 faea38e7 bellard
        }
1011 faea38e7 bellard
    }
1012 faea38e7 bellard
    return buf;
1013 faea38e7 bellard
}
1014 faea38e7 bellard
1015 faea38e7 bellard
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1016 faea38e7 bellard
{
1017 faea38e7 bellard
    char buf1[128], date_buf[128], clock_buf[128];
1018 3b9f94e1 bellard
#ifdef _WIN32
1019 3b9f94e1 bellard
    struct tm *ptm;
1020 3b9f94e1 bellard
#else
1021 faea38e7 bellard
    struct tm tm;
1022 3b9f94e1 bellard
#endif
1023 faea38e7 bellard
    time_t ti;
1024 faea38e7 bellard
    int64_t secs;
1025 faea38e7 bellard
1026 faea38e7 bellard
    if (!sn) {
1027 5fafdf24 ths
        snprintf(buf, buf_size,
1028 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
1029 faea38e7 bellard
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1030 faea38e7 bellard
    } else {
1031 faea38e7 bellard
        ti = sn->date_sec;
1032 3b9f94e1 bellard
#ifdef _WIN32
1033 3b9f94e1 bellard
        ptm = localtime(&ti);
1034 3b9f94e1 bellard
        strftime(date_buf, sizeof(date_buf),
1035 3b9f94e1 bellard
                 "%Y-%m-%d %H:%M:%S", ptm);
1036 3b9f94e1 bellard
#else
1037 faea38e7 bellard
        localtime_r(&ti, &tm);
1038 faea38e7 bellard
        strftime(date_buf, sizeof(date_buf),
1039 faea38e7 bellard
                 "%Y-%m-%d %H:%M:%S", &tm);
1040 3b9f94e1 bellard
#endif
1041 faea38e7 bellard
        secs = sn->vm_clock_nsec / 1000000000;
1042 faea38e7 bellard
        snprintf(clock_buf, sizeof(clock_buf),
1043 faea38e7 bellard
                 "%02d:%02d:%02d.%03d",
1044 faea38e7 bellard
                 (int)(secs / 3600),
1045 faea38e7 bellard
                 (int)((secs / 60) % 60),
1046 5fafdf24 ths
                 (int)(secs % 60),
1047 faea38e7 bellard
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1048 faea38e7 bellard
        snprintf(buf, buf_size,
1049 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
1050 faea38e7 bellard
                 sn->id_str, sn->name,
1051 faea38e7 bellard
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1052 faea38e7 bellard
                 date_buf,
1053 faea38e7 bellard
                 clock_buf);
1054 faea38e7 bellard
    }
1055 faea38e7 bellard
    return buf;
1056 faea38e7 bellard
}
1057 faea38e7 bellard
1058 83f64091 bellard
1059 ea2384d3 bellard
/**************************************************************/
1060 83f64091 bellard
/* async I/Os */
1061 ea2384d3 bellard
1062 ce1a14dc pbrook
BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1063 ce1a14dc pbrook
                                uint8_t *buf, int nb_sectors,
1064 ce1a14dc pbrook
                                BlockDriverCompletionFunc *cb, void *opaque)
1065 83f64091 bellard
{
1066 83f64091 bellard
    BlockDriver *drv = bs->drv;
1067 83f64091 bellard
1068 19cb3738 bellard
    if (!drv)
1069 ce1a14dc pbrook
        return NULL;
1070 3b46e624 ths
1071 83f64091 bellard
    /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1072 83f64091 bellard
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1073 83f64091 bellard
        memcpy(buf, bs->boot_sector_data, 512);
1074 83f64091 bellard
        sector_num++;
1075 83f64091 bellard
        nb_sectors--;
1076 83f64091 bellard
        buf += 512;
1077 83f64091 bellard
    }
1078 83f64091 bellard
1079 ce1a14dc pbrook
    return drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1080 ea2384d3 bellard
}
1081 ea2384d3 bellard
1082 ce1a14dc pbrook
BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1083 ce1a14dc pbrook
                                 const uint8_t *buf, int nb_sectors,
1084 ce1a14dc pbrook
                                 BlockDriverCompletionFunc *cb, void *opaque)
1085 ea2384d3 bellard
{
1086 83f64091 bellard
    BlockDriver *drv = bs->drv;
1087 ea2384d3 bellard
1088 19cb3738 bellard
    if (!drv)
1089 ce1a14dc pbrook
        return NULL;
1090 83f64091 bellard
    if (bs->read_only)
1091 ce1a14dc pbrook
        return NULL;
1092 83f64091 bellard
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1093 3b46e624 ths
        memcpy(bs->boot_sector_data, buf, 512);
1094 ea2384d3 bellard
    }
1095 83f64091 bellard
1096 ce1a14dc pbrook
    return drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1097 83f64091 bellard
}
1098 83f64091 bellard
1099 83f64091 bellard
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1100 83f64091 bellard
{
1101 ce1a14dc pbrook
    BlockDriver *drv = acb->bs->drv;
1102 83f64091 bellard
1103 ce1a14dc pbrook
    drv->bdrv_aio_cancel(acb);
1104 83f64091 bellard
}
1105 83f64091 bellard
1106 ce1a14dc pbrook
1107 83f64091 bellard
/**************************************************************/
1108 83f64091 bellard
/* async block device emulation */
1109 83f64091 bellard
1110 faf07963 pbrook
#ifdef QEMU_IMG
1111 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1112 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1113 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1114 ea2384d3 bellard
{
1115 ea2384d3 bellard
    int ret;
1116 ce1a14dc pbrook
    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1117 ce1a14dc pbrook
    cb(opaque, ret);
1118 ce1a14dc pbrook
    return NULL;
1119 ea2384d3 bellard
}
1120 ea2384d3 bellard
1121 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1122 ce1a14dc pbrook
        int64_t sector_num, const uint8_t *buf, int nb_sectors,
1123 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1124 ea2384d3 bellard
{
1125 ea2384d3 bellard
    int ret;
1126 ce1a14dc pbrook
    ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1127 ce1a14dc pbrook
    cb(opaque, ret);
1128 ce1a14dc pbrook
    return NULL;
1129 ea2384d3 bellard
}
1130 ea2384d3 bellard
1131 83f64091 bellard
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1132 ea2384d3 bellard
{
1133 ea2384d3 bellard
}
1134 83f64091 bellard
#else
1135 ce1a14dc pbrook
static void bdrv_aio_bh_cb(void *opaque)
1136 83f64091 bellard
{
1137 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb = opaque;
1138 ce1a14dc pbrook
    acb->common.cb(acb->common.opaque, acb->ret);
1139 ce1a14dc pbrook
    qemu_aio_release(acb);
1140 83f64091 bellard
}
1141 beac80cd bellard
1142 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1143 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1144 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1145 83f64091 bellard
{
1146 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb;
1147 83f64091 bellard
    int ret;
1148 ce1a14dc pbrook
1149 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
1150 ce1a14dc pbrook
    if (!acb->bh)
1151 ce1a14dc pbrook
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1152 ce1a14dc pbrook
    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1153 ce1a14dc pbrook
    acb->ret = ret;
1154 ce1a14dc pbrook
    qemu_bh_schedule(acb->bh);
1155 ce1a14dc pbrook
    return &acb->common;
1156 beac80cd bellard
}
1157 beac80cd bellard
1158 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1159 ce1a14dc pbrook
        int64_t sector_num, const uint8_t *buf, int nb_sectors,
1160 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1161 beac80cd bellard
{
1162 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb;
1163 83f64091 bellard
    int ret;
1164 83f64091 bellard
1165 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
1166 ce1a14dc pbrook
    if (!acb->bh)
1167 ce1a14dc pbrook
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1168 ce1a14dc pbrook
    ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1169 ce1a14dc pbrook
    acb->ret = ret;
1170 ce1a14dc pbrook
    qemu_bh_schedule(acb->bh);
1171 ce1a14dc pbrook
    return &acb->common;
1172 beac80cd bellard
}
1173 beac80cd bellard
1174 ce1a14dc pbrook
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1175 ea2384d3 bellard
{
1176 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1177 ce1a14dc pbrook
    qemu_bh_cancel(acb->bh);
1178 ce1a14dc pbrook
    qemu_aio_release(acb);
1179 83f64091 bellard
}
1180 faf07963 pbrook
#endif /* !QEMU_IMG */
1181 ea2384d3 bellard
1182 83f64091 bellard
/**************************************************************/
1183 83f64091 bellard
/* sync block device emulation */
1184 ea2384d3 bellard
1185 83f64091 bellard
static void bdrv_rw_em_cb(void *opaque, int ret)
1186 83f64091 bellard
{
1187 83f64091 bellard
    *(int *)opaque = ret;
1188 ea2384d3 bellard
}
1189 ea2384d3 bellard
1190 83f64091 bellard
#define NOT_DONE 0x7fffffff
1191 83f64091 bellard
1192 5fafdf24 ths
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1193 83f64091 bellard
                        uint8_t *buf, int nb_sectors)
1194 7a6cba61 pbrook
{
1195 ce1a14dc pbrook
    int async_ret;
1196 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1197 83f64091 bellard
1198 83f64091 bellard
    async_ret = NOT_DONE;
1199 83f64091 bellard
    qemu_aio_wait_start();
1200 5fafdf24 ths
    acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1201 83f64091 bellard
                        bdrv_rw_em_cb, &async_ret);
1202 ce1a14dc pbrook
    if (acb == NULL) {
1203 83f64091 bellard
        qemu_aio_wait_end();
1204 ce1a14dc pbrook
        return -1;
1205 83f64091 bellard
    }
1206 83f64091 bellard
    while (async_ret == NOT_DONE) {
1207 83f64091 bellard
        qemu_aio_wait();
1208 83f64091 bellard
    }
1209 83f64091 bellard
    qemu_aio_wait_end();
1210 83f64091 bellard
    return async_ret;
1211 7a6cba61 pbrook
}
1212 7a6cba61 pbrook
1213 83f64091 bellard
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1214 83f64091 bellard
                         const uint8_t *buf, int nb_sectors)
1215 83f64091 bellard
{
1216 ce1a14dc pbrook
    int async_ret;
1217 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1218 83f64091 bellard
1219 83f64091 bellard
    async_ret = NOT_DONE;
1220 83f64091 bellard
    qemu_aio_wait_start();
1221 5fafdf24 ths
    acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1222 83f64091 bellard
                         bdrv_rw_em_cb, &async_ret);
1223 ce1a14dc pbrook
    if (acb == NULL) {
1224 83f64091 bellard
        qemu_aio_wait_end();
1225 ce1a14dc pbrook
        return -1;
1226 83f64091 bellard
    }
1227 83f64091 bellard
    while (async_ret == NOT_DONE) {
1228 83f64091 bellard
        qemu_aio_wait();
1229 83f64091 bellard
    }
1230 83f64091 bellard
    qemu_aio_wait_end();
1231 83f64091 bellard
    return async_ret;
1232 83f64091 bellard
}
1233 ea2384d3 bellard
1234 ea2384d3 bellard
void bdrv_init(void)
1235 ea2384d3 bellard
{
1236 ea2384d3 bellard
    bdrv_register(&bdrv_raw);
1237 19cb3738 bellard
    bdrv_register(&bdrv_host_device);
1238 ea2384d3 bellard
#ifndef _WIN32
1239 ea2384d3 bellard
    bdrv_register(&bdrv_cow);
1240 ea2384d3 bellard
#endif
1241 ea2384d3 bellard
    bdrv_register(&bdrv_qcow);
1242 ea2384d3 bellard
    bdrv_register(&bdrv_vmdk);
1243 3c56521b bellard
    bdrv_register(&bdrv_cloop);
1244 585d0ed9 bellard
    bdrv_register(&bdrv_dmg);
1245 a8753c34 bellard
    bdrv_register(&bdrv_bochs);
1246 6a0f9e82 bellard
    bdrv_register(&bdrv_vpc);
1247 712e7874 bellard
    bdrv_register(&bdrv_vvfat);
1248 faea38e7 bellard
    bdrv_register(&bdrv_qcow2);
1249 6ada7453 ths
    bdrv_register(&bdrv_parallels);
1250 ea2384d3 bellard
}
1251 ce1a14dc pbrook
1252 ce1a14dc pbrook
void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1253 ce1a14dc pbrook
                   void *opaque)
1254 ce1a14dc pbrook
{
1255 ce1a14dc pbrook
    BlockDriver *drv;
1256 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1257 ce1a14dc pbrook
1258 ce1a14dc pbrook
    drv = bs->drv;
1259 ce1a14dc pbrook
    if (drv->free_aiocb) {
1260 ce1a14dc pbrook
        acb = drv->free_aiocb;
1261 ce1a14dc pbrook
        drv->free_aiocb = acb->next;
1262 ce1a14dc pbrook
    } else {
1263 ce1a14dc pbrook
        acb = qemu_mallocz(drv->aiocb_size);
1264 ce1a14dc pbrook
        if (!acb)
1265 ce1a14dc pbrook
            return NULL;
1266 ce1a14dc pbrook
    }
1267 ce1a14dc pbrook
    acb->bs = bs;
1268 ce1a14dc pbrook
    acb->cb = cb;
1269 ce1a14dc pbrook
    acb->opaque = opaque;
1270 ce1a14dc pbrook
    return acb;
1271 ce1a14dc pbrook
}
1272 ce1a14dc pbrook
1273 ce1a14dc pbrook
void qemu_aio_release(void *p)
1274 ce1a14dc pbrook
{
1275 ce1a14dc pbrook
    BlockDriverAIOCB *acb = p;
1276 ce1a14dc pbrook
    BlockDriver *drv = acb->bs->drv;
1277 ce1a14dc pbrook
    acb->next = drv->free_aiocb;
1278 ce1a14dc pbrook
    drv->free_aiocb = acb;
1279 ce1a14dc pbrook
}
1280 19cb3738 bellard
1281 19cb3738 bellard
/**************************************************************/
1282 19cb3738 bellard
/* removable device support */
1283 19cb3738 bellard
1284 19cb3738 bellard
/**
1285 19cb3738 bellard
 * Return TRUE if the media is present
1286 19cb3738 bellard
 */
1287 19cb3738 bellard
int bdrv_is_inserted(BlockDriverState *bs)
1288 19cb3738 bellard
{
1289 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1290 19cb3738 bellard
    int ret;
1291 19cb3738 bellard
    if (!drv)
1292 19cb3738 bellard
        return 0;
1293 19cb3738 bellard
    if (!drv->bdrv_is_inserted)
1294 19cb3738 bellard
        return 1;
1295 19cb3738 bellard
    ret = drv->bdrv_is_inserted(bs);
1296 19cb3738 bellard
    return ret;
1297 19cb3738 bellard
}
1298 19cb3738 bellard
1299 19cb3738 bellard
/**
1300 19cb3738 bellard
 * Return TRUE if the media changed since the last call to this
1301 5fafdf24 ths
 * function. It is currently only used for floppy disks
1302 19cb3738 bellard
 */
1303 19cb3738 bellard
int bdrv_media_changed(BlockDriverState *bs)
1304 19cb3738 bellard
{
1305 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1306 19cb3738 bellard
    int ret;
1307 19cb3738 bellard
1308 19cb3738 bellard
    if (!drv || !drv->bdrv_media_changed)
1309 19cb3738 bellard
        ret = -ENOTSUP;
1310 19cb3738 bellard
    else
1311 19cb3738 bellard
        ret = drv->bdrv_media_changed(bs);
1312 19cb3738 bellard
    if (ret == -ENOTSUP)
1313 19cb3738 bellard
        ret = bs->media_changed;
1314 19cb3738 bellard
    bs->media_changed = 0;
1315 19cb3738 bellard
    return ret;
1316 19cb3738 bellard
}
1317 19cb3738 bellard
1318 19cb3738 bellard
/**
1319 19cb3738 bellard
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1320 19cb3738 bellard
 */
1321 19cb3738 bellard
void bdrv_eject(BlockDriverState *bs, int eject_flag)
1322 19cb3738 bellard
{
1323 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1324 19cb3738 bellard
    int ret;
1325 19cb3738 bellard
1326 19cb3738 bellard
    if (!drv || !drv->bdrv_eject) {
1327 19cb3738 bellard
        ret = -ENOTSUP;
1328 19cb3738 bellard
    } else {
1329 19cb3738 bellard
        ret = drv->bdrv_eject(bs, eject_flag);
1330 19cb3738 bellard
    }
1331 19cb3738 bellard
    if (ret == -ENOTSUP) {
1332 19cb3738 bellard
        if (eject_flag)
1333 19cb3738 bellard
            bdrv_close(bs);
1334 19cb3738 bellard
    }
1335 19cb3738 bellard
}
1336 19cb3738 bellard
1337 19cb3738 bellard
int bdrv_is_locked(BlockDriverState *bs)
1338 19cb3738 bellard
{
1339 19cb3738 bellard
    return bs->locked;
1340 19cb3738 bellard
}
1341 19cb3738 bellard
1342 19cb3738 bellard
/**
1343 19cb3738 bellard
 * Lock or unlock the media (if it is locked, the user won't be able
1344 19cb3738 bellard
 * to eject it manually).
1345 19cb3738 bellard
 */
1346 19cb3738 bellard
void bdrv_set_locked(BlockDriverState *bs, int locked)
1347 19cb3738 bellard
{
1348 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1349 19cb3738 bellard
1350 19cb3738 bellard
    bs->locked = locked;
1351 19cb3738 bellard
    if (drv && drv->bdrv_set_locked) {
1352 19cb3738 bellard
        drv->bdrv_set_locked(bs, locked);
1353 19cb3738 bellard
    }
1354 19cb3738 bellard
}