Statistics
| Branch: | Revision:

root / block.c @ 64adab3f

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