Statistics
| Branch: | Revision:

root / block.c @ 8c5e95d8

History | View | Annotate | Download (36.8 kB)

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