Statistics
| Branch: | Revision:

root / block.c @ c3e36823

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