Statistics
| Branch: | Revision:

root / block.c @ da3d9c5b

History | View | Annotate | Download (36.6 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 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 0badc1ee aurel32
    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 3b46e624 ths
342 ea2384d3 bellard
        /* if snapshot, we create a temporary backing file and open it
343 ea2384d3 bellard
           instead of opening 'filename' directly */
344 33e3963e bellard
345 ea2384d3 bellard
        /* if there is a backing file, use it */
346 ea2384d3 bellard
        bs1 = bdrv_new("");
347 ea2384d3 bellard
        if (!bs1) {
348 83f64091 bellard
            return -ENOMEM;
349 ea2384d3 bellard
        }
350 ea2384d3 bellard
        if (bdrv_open(bs1, filename, 0) < 0) {
351 ea2384d3 bellard
            bdrv_delete(bs1);
352 ea2384d3 bellard
            return -1;
353 ea2384d3 bellard
        }
354 83f64091 bellard
        total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
355 ea2384d3 bellard
        bdrv_delete(bs1);
356 3b46e624 ths
357 ea2384d3 bellard
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
358 a817d936 bellard
        realpath(filename, backing_filename);
359 5fafdf24 ths
        if (bdrv_create(&bdrv_qcow2, tmp_filename,
360 a817d936 bellard
                        total_size, backing_filename, 0) < 0) {
361 ea2384d3 bellard
            return -1;
362 ea2384d3 bellard
        }
363 ea2384d3 bellard
        filename = tmp_filename;
364 ea2384d3 bellard
        bs->is_temporary = 1;
365 ea2384d3 bellard
    }
366 712e7874 bellard
367 ea2384d3 bellard
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
368 83f64091 bellard
    if (flags & BDRV_O_FILE) {
369 83f64091 bellard
        drv = find_protocol(filename);
370 ea2384d3 bellard
        if (!drv)
371 83f64091 bellard
            return -ENOENT;
372 83f64091 bellard
    } else {
373 83f64091 bellard
        if (!drv) {
374 83f64091 bellard
            drv = find_image_format(filename);
375 83f64091 bellard
            if (!drv)
376 83f64091 bellard
                return -1;
377 83f64091 bellard
        }
378 ea2384d3 bellard
    }
379 ea2384d3 bellard
    bs->drv = drv;
380 ea2384d3 bellard
    bs->opaque = qemu_mallocz(drv->instance_size);
381 ea2384d3 bellard
    if (bs->opaque == NULL && drv->instance_size > 0)
382 ea2384d3 bellard
        return -1;
383 83f64091 bellard
    /* Note: for compatibility, we open disk image files as RDWR, and
384 83f64091 bellard
       RDONLY as fallback */
385 83f64091 bellard
    if (!(flags & BDRV_O_FILE))
386 33f00271 balrog
        open_flags = BDRV_O_RDWR | (flags & BDRV_O_DIRECT);
387 83f64091 bellard
    else
388 83f64091 bellard
        open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
389 83f64091 bellard
    ret = drv->bdrv_open(bs, filename, open_flags);
390 83f64091 bellard
    if (ret == -EACCES && !(flags & BDRV_O_FILE)) {
391 83f64091 bellard
        ret = drv->bdrv_open(bs, filename, BDRV_O_RDONLY);
392 83f64091 bellard
        bs->read_only = 1;
393 83f64091 bellard
    }
394 ea2384d3 bellard
    if (ret < 0) {
395 ea2384d3 bellard
        qemu_free(bs->opaque);
396 6b21b973 bellard
        bs->opaque = NULL;
397 6b21b973 bellard
        bs->drv = NULL;
398 83f64091 bellard
        return ret;
399 33e3963e bellard
    }
400 d15a771d bellard
    if (drv->bdrv_getlength) {
401 d15a771d bellard
        bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
402 d15a771d bellard
    }
403 67b915a5 bellard
#ifndef _WIN32
404 ea2384d3 bellard
    if (bs->is_temporary) {
405 ea2384d3 bellard
        unlink(filename);
406 ea2384d3 bellard
    }
407 ea2384d3 bellard
#endif
408 83f64091 bellard
    if (bs->backing_file[0] != '\0') {
409 ea2384d3 bellard
        /* if there is a backing file, use it */
410 ea2384d3 bellard
        bs->backing_hd = bdrv_new("");
411 ea2384d3 bellard
        if (!bs->backing_hd) {
412 ea2384d3 bellard
        fail:
413 ea2384d3 bellard
            bdrv_close(bs);
414 6b21b973 bellard
            return -ENOMEM;
415 33e3963e bellard
        }
416 83f64091 bellard
        path_combine(backing_filename, sizeof(backing_filename),
417 83f64091 bellard
                     filename, bs->backing_file);
418 83f64091 bellard
        if (bdrv_open(bs->backing_hd, backing_filename, 0) < 0)
419 33e3963e bellard
            goto fail;
420 33e3963e bellard
    }
421 33e3963e bellard
422 b338082b bellard
    /* call the change callback */
423 19cb3738 bellard
    bs->media_changed = 1;
424 b338082b bellard
    if (bs->change_cb)
425 b338082b bellard
        bs->change_cb(bs->change_opaque);
426 b338082b bellard
427 b338082b bellard
    return 0;
428 fc01f7e7 bellard
}
429 fc01f7e7 bellard
430 fc01f7e7 bellard
void bdrv_close(BlockDriverState *bs)
431 fc01f7e7 bellard
{
432 19cb3738 bellard
    if (bs->drv) {
433 ea2384d3 bellard
        if (bs->backing_hd)
434 ea2384d3 bellard
            bdrv_delete(bs->backing_hd);
435 ea2384d3 bellard
        bs->drv->bdrv_close(bs);
436 ea2384d3 bellard
        qemu_free(bs->opaque);
437 ea2384d3 bellard
#ifdef _WIN32
438 ea2384d3 bellard
        if (bs->is_temporary) {
439 ea2384d3 bellard
            unlink(bs->filename);
440 ea2384d3 bellard
        }
441 67b915a5 bellard
#endif
442 ea2384d3 bellard
        bs->opaque = NULL;
443 ea2384d3 bellard
        bs->drv = NULL;
444 b338082b bellard
445 b338082b bellard
        /* call the change callback */
446 19cb3738 bellard
        bs->media_changed = 1;
447 b338082b bellard
        if (bs->change_cb)
448 b338082b bellard
            bs->change_cb(bs->change_opaque);
449 b338082b bellard
    }
450 b338082b bellard
}
451 b338082b bellard
452 b338082b bellard
void bdrv_delete(BlockDriverState *bs)
453 b338082b bellard
{
454 34c6f050 aurel32
    BlockDriverState **pbs;
455 34c6f050 aurel32
456 34c6f050 aurel32
    pbs = &bdrv_first;
457 34c6f050 aurel32
    while (*pbs != bs && *pbs != NULL)
458 34c6f050 aurel32
        pbs = &(*pbs)->next;
459 34c6f050 aurel32
    if (*pbs == bs)
460 34c6f050 aurel32
        *pbs = bs->next;
461 34c6f050 aurel32
462 b338082b bellard
    bdrv_close(bs);
463 b338082b bellard
    qemu_free(bs);
464 fc01f7e7 bellard
}
465 fc01f7e7 bellard
466 33e3963e bellard
/* commit COW file into the raw image */
467 33e3963e bellard
int bdrv_commit(BlockDriverState *bs)
468 33e3963e bellard
{
469 19cb3738 bellard
    BlockDriver *drv = bs->drv;
470 83f64091 bellard
    int64_t i, total_sectors;
471 ea2384d3 bellard
    int n, j;
472 ea2384d3 bellard
    unsigned char sector[512];
473 33e3963e bellard
474 19cb3738 bellard
    if (!drv)
475 19cb3738 bellard
        return -ENOMEDIUM;
476 33e3963e bellard
477 33e3963e bellard
    if (bs->read_only) {
478 ea2384d3 bellard
        return -EACCES;
479 33e3963e bellard
    }
480 33e3963e bellard
481 ea2384d3 bellard
    if (!bs->backing_hd) {
482 ea2384d3 bellard
        return -ENOTSUP;
483 ea2384d3 bellard
    }
484 33e3963e bellard
485 83f64091 bellard
    total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
486 83f64091 bellard
    for (i = 0; i < total_sectors;) {
487 19cb3738 bellard
        if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
488 ea2384d3 bellard
            for(j = 0; j < n; j++) {
489 ea2384d3 bellard
                if (bdrv_read(bs, i, sector, 1) != 0) {
490 ea2384d3 bellard
                    return -EIO;
491 ea2384d3 bellard
                }
492 ea2384d3 bellard
493 ea2384d3 bellard
                if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
494 ea2384d3 bellard
                    return -EIO;
495 ea2384d3 bellard
                }
496 ea2384d3 bellard
                i++;
497 33e3963e bellard
            }
498 ea2384d3 bellard
        } else {
499 ea2384d3 bellard
            i += n;
500 ea2384d3 bellard
        }
501 33e3963e bellard
    }
502 95389c86 bellard
503 19cb3738 bellard
    if (drv->bdrv_make_empty)
504 19cb3738 bellard
        return drv->bdrv_make_empty(bs);
505 95389c86 bellard
506 33e3963e bellard
    return 0;
507 33e3963e bellard
}
508 33e3963e bellard
509 19cb3738 bellard
/* return < 0 if error. See bdrv_write() for the return codes */
510 5fafdf24 ths
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
511 fc01f7e7 bellard
              uint8_t *buf, int nb_sectors)
512 fc01f7e7 bellard
{
513 ea2384d3 bellard
    BlockDriver *drv = bs->drv;
514 ea2384d3 bellard
515 19cb3738 bellard
    if (!drv)
516 19cb3738 bellard
        return -ENOMEDIUM;
517 b338082b bellard
518 83f64091 bellard
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
519 cf98951b bellard
            memcpy(buf, bs->boot_sector_data, 512);
520 83f64091 bellard
        sector_num++;
521 83f64091 bellard
        nb_sectors--;
522 83f64091 bellard
        buf += 512;
523 83f64091 bellard
        if (nb_sectors == 0)
524 83f64091 bellard
            return 0;
525 83f64091 bellard
    }
526 83f64091 bellard
    if (drv->bdrv_pread) {
527 83f64091 bellard
        int ret, len;
528 83f64091 bellard
        len = nb_sectors * 512;
529 83f64091 bellard
        ret = drv->bdrv_pread(bs, sector_num * 512, buf, len);
530 83f64091 bellard
        if (ret < 0)
531 83f64091 bellard
            return ret;
532 83f64091 bellard
        else if (ret != len)
533 19cb3738 bellard
            return -EINVAL;
534 a36e69dd ths
        else {
535 a36e69dd ths
            bs->rd_bytes += (unsigned) len;
536 a36e69dd ths
            bs->rd_ops ++;
537 83f64091 bellard
            return 0;
538 a36e69dd ths
        }
539 83f64091 bellard
    } else {
540 83f64091 bellard
        return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
541 33e3963e bellard
    }
542 fc01f7e7 bellard
}
543 fc01f7e7 bellard
544 5fafdf24 ths
/* Return < 0 if error. Important errors are:
545 19cb3738 bellard
  -EIO         generic I/O error (may happen for all errors)
546 19cb3738 bellard
  -ENOMEDIUM   No media inserted.
547 19cb3738 bellard
  -EINVAL      Invalid sector number or nb_sectors
548 19cb3738 bellard
  -EACCES      Trying to write a read-only device
549 19cb3738 bellard
*/
550 5fafdf24 ths
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
551 fc01f7e7 bellard
               const uint8_t *buf, int nb_sectors)
552 fc01f7e7 bellard
{
553 83f64091 bellard
    BlockDriver *drv = bs->drv;
554 19cb3738 bellard
    if (!bs->drv)
555 19cb3738 bellard
        return -ENOMEDIUM;
556 0849bf08 bellard
    if (bs->read_only)
557 19cb3738 bellard
        return -EACCES;
558 79639d42 bellard
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
559 3b46e624 ths
        memcpy(bs->boot_sector_data, buf, 512);
560 79639d42 bellard
    }
561 83f64091 bellard
    if (drv->bdrv_pwrite) {
562 83f64091 bellard
        int ret, len;
563 83f64091 bellard
        len = nb_sectors * 512;
564 83f64091 bellard
        ret = drv->bdrv_pwrite(bs, sector_num * 512, buf, len);
565 83f64091 bellard
        if (ret < 0)
566 83f64091 bellard
            return ret;
567 83f64091 bellard
        else if (ret != len)
568 83f64091 bellard
            return -EIO;
569 a36e69dd ths
        else {
570 a36e69dd ths
            bs->wr_bytes += (unsigned) len;
571 a36e69dd ths
            bs->wr_ops ++;
572 83f64091 bellard
            return 0;
573 a36e69dd ths
        }
574 83f64091 bellard
    } else {
575 83f64091 bellard
        return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
576 83f64091 bellard
    }
577 83f64091 bellard
}
578 83f64091 bellard
579 5fafdf24 ths
static int bdrv_pread_em(BlockDriverState *bs, int64_t offset,
580 faea38e7 bellard
                         uint8_t *buf, int count1)
581 83f64091 bellard
{
582 83f64091 bellard
    uint8_t tmp_buf[SECTOR_SIZE];
583 83f64091 bellard
    int len, nb_sectors, count;
584 83f64091 bellard
    int64_t sector_num;
585 83f64091 bellard
586 83f64091 bellard
    count = count1;
587 83f64091 bellard
    /* first read to align to sector start */
588 83f64091 bellard
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
589 83f64091 bellard
    if (len > count)
590 83f64091 bellard
        len = count;
591 83f64091 bellard
    sector_num = offset >> SECTOR_BITS;
592 83f64091 bellard
    if (len > 0) {
593 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
594 83f64091 bellard
            return -EIO;
595 83f64091 bellard
        memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
596 83f64091 bellard
        count -= len;
597 83f64091 bellard
        if (count == 0)
598 83f64091 bellard
            return count1;
599 83f64091 bellard
        sector_num++;
600 83f64091 bellard
        buf += len;
601 83f64091 bellard
    }
602 83f64091 bellard
603 83f64091 bellard
    /* read the sectors "in place" */
604 83f64091 bellard
    nb_sectors = count >> SECTOR_BITS;
605 83f64091 bellard
    if (nb_sectors > 0) {
606 83f64091 bellard
        if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
607 83f64091 bellard
            return -EIO;
608 83f64091 bellard
        sector_num += nb_sectors;
609 83f64091 bellard
        len = nb_sectors << SECTOR_BITS;
610 83f64091 bellard
        buf += len;
611 83f64091 bellard
        count -= len;
612 83f64091 bellard
    }
613 83f64091 bellard
614 83f64091 bellard
    /* add data from the last sector */
615 83f64091 bellard
    if (count > 0) {
616 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
617 83f64091 bellard
            return -EIO;
618 83f64091 bellard
        memcpy(buf, tmp_buf, count);
619 83f64091 bellard
    }
620 83f64091 bellard
    return count1;
621 83f64091 bellard
}
622 83f64091 bellard
623 5fafdf24 ths
static int bdrv_pwrite_em(BlockDriverState *bs, int64_t offset,
624 faea38e7 bellard
                          const uint8_t *buf, int count1)
625 83f64091 bellard
{
626 83f64091 bellard
    uint8_t tmp_buf[SECTOR_SIZE];
627 83f64091 bellard
    int len, nb_sectors, count;
628 83f64091 bellard
    int64_t sector_num;
629 83f64091 bellard
630 83f64091 bellard
    count = count1;
631 83f64091 bellard
    /* first write to align to sector start */
632 83f64091 bellard
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
633 83f64091 bellard
    if (len > count)
634 83f64091 bellard
        len = count;
635 83f64091 bellard
    sector_num = offset >> SECTOR_BITS;
636 83f64091 bellard
    if (len > 0) {
637 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
638 83f64091 bellard
            return -EIO;
639 83f64091 bellard
        memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
640 83f64091 bellard
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
641 83f64091 bellard
            return -EIO;
642 83f64091 bellard
        count -= len;
643 83f64091 bellard
        if (count == 0)
644 83f64091 bellard
            return count1;
645 83f64091 bellard
        sector_num++;
646 83f64091 bellard
        buf += len;
647 83f64091 bellard
    }
648 83f64091 bellard
649 83f64091 bellard
    /* write the sectors "in place" */
650 83f64091 bellard
    nb_sectors = count >> SECTOR_BITS;
651 83f64091 bellard
    if (nb_sectors > 0) {
652 83f64091 bellard
        if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
653 83f64091 bellard
            return -EIO;
654 83f64091 bellard
        sector_num += nb_sectors;
655 83f64091 bellard
        len = nb_sectors << SECTOR_BITS;
656 83f64091 bellard
        buf += len;
657 83f64091 bellard
        count -= len;
658 83f64091 bellard
    }
659 83f64091 bellard
660 83f64091 bellard
    /* add data from the last sector */
661 83f64091 bellard
    if (count > 0) {
662 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
663 83f64091 bellard
            return -EIO;
664 83f64091 bellard
        memcpy(tmp_buf, buf, count);
665 83f64091 bellard
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
666 83f64091 bellard
            return -EIO;
667 83f64091 bellard
    }
668 83f64091 bellard
    return count1;
669 83f64091 bellard
}
670 83f64091 bellard
671 83f64091 bellard
/**
672 5fafdf24 ths
 * Read with byte offsets (needed only for file protocols)
673 83f64091 bellard
 */
674 5fafdf24 ths
int bdrv_pread(BlockDriverState *bs, int64_t offset,
675 83f64091 bellard
               void *buf1, int count1)
676 83f64091 bellard
{
677 83f64091 bellard
    BlockDriver *drv = bs->drv;
678 83f64091 bellard
679 83f64091 bellard
    if (!drv)
680 19cb3738 bellard
        return -ENOMEDIUM;
681 83f64091 bellard
    if (!drv->bdrv_pread)
682 faea38e7 bellard
        return bdrv_pread_em(bs, offset, buf1, count1);
683 83f64091 bellard
    return drv->bdrv_pread(bs, offset, buf1, count1);
684 83f64091 bellard
}
685 83f64091 bellard
686 5fafdf24 ths
/**
687 5fafdf24 ths
 * Write with byte offsets (needed only for file protocols)
688 83f64091 bellard
 */
689 5fafdf24 ths
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
690 83f64091 bellard
                const void *buf1, int count1)
691 83f64091 bellard
{
692 83f64091 bellard
    BlockDriver *drv = bs->drv;
693 83f64091 bellard
694 83f64091 bellard
    if (!drv)
695 19cb3738 bellard
        return -ENOMEDIUM;
696 83f64091 bellard
    if (!drv->bdrv_pwrite)
697 faea38e7 bellard
        return bdrv_pwrite_em(bs, offset, buf1, count1);
698 83f64091 bellard
    return drv->bdrv_pwrite(bs, offset, buf1, count1);
699 83f64091 bellard
}
700 83f64091 bellard
701 83f64091 bellard
/**
702 83f64091 bellard
 * Truncate file to 'offset' bytes (needed only for file protocols)
703 83f64091 bellard
 */
704 83f64091 bellard
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
705 83f64091 bellard
{
706 83f64091 bellard
    BlockDriver *drv = bs->drv;
707 83f64091 bellard
    if (!drv)
708 19cb3738 bellard
        return -ENOMEDIUM;
709 83f64091 bellard
    if (!drv->bdrv_truncate)
710 83f64091 bellard
        return -ENOTSUP;
711 83f64091 bellard
    return drv->bdrv_truncate(bs, offset);
712 83f64091 bellard
}
713 83f64091 bellard
714 83f64091 bellard
/**
715 83f64091 bellard
 * Length of a file in bytes. Return < 0 if error or unknown.
716 83f64091 bellard
 */
717 83f64091 bellard
int64_t bdrv_getlength(BlockDriverState *bs)
718 83f64091 bellard
{
719 83f64091 bellard
    BlockDriver *drv = bs->drv;
720 83f64091 bellard
    if (!drv)
721 19cb3738 bellard
        return -ENOMEDIUM;
722 83f64091 bellard
    if (!drv->bdrv_getlength) {
723 83f64091 bellard
        /* legacy mode */
724 83f64091 bellard
        return bs->total_sectors * SECTOR_SIZE;
725 83f64091 bellard
    }
726 83f64091 bellard
    return drv->bdrv_getlength(bs);
727 fc01f7e7 bellard
}
728 fc01f7e7 bellard
729 19cb3738 bellard
/* return 0 as number of sectors if no device present or error */
730 96b8f136 ths
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
731 fc01f7e7 bellard
{
732 19cb3738 bellard
    int64_t length;
733 19cb3738 bellard
    length = bdrv_getlength(bs);
734 19cb3738 bellard
    if (length < 0)
735 19cb3738 bellard
        length = 0;
736 19cb3738 bellard
    else
737 19cb3738 bellard
        length = length >> SECTOR_BITS;
738 19cb3738 bellard
    *nb_sectors_ptr = length;
739 fc01f7e7 bellard
}
740 cf98951b bellard
741 cf98951b bellard
/* force a given boot sector. */
742 cf98951b bellard
void bdrv_set_boot_sector(BlockDriverState *bs, const uint8_t *data, int size)
743 cf98951b bellard
{
744 cf98951b bellard
    bs->boot_sector_enabled = 1;
745 cf98951b bellard
    if (size > 512)
746 cf98951b bellard
        size = 512;
747 cf98951b bellard
    memcpy(bs->boot_sector_data, data, size);
748 cf98951b bellard
    memset(bs->boot_sector_data + size, 0, 512 - size);
749 cf98951b bellard
}
750 b338082b bellard
751 5fafdf24 ths
void bdrv_set_geometry_hint(BlockDriverState *bs,
752 b338082b bellard
                            int cyls, int heads, int secs)
753 b338082b bellard
{
754 b338082b bellard
    bs->cyls = cyls;
755 b338082b bellard
    bs->heads = heads;
756 b338082b bellard
    bs->secs = secs;
757 b338082b bellard
}
758 b338082b bellard
759 b338082b bellard
void bdrv_set_type_hint(BlockDriverState *bs, int type)
760 b338082b bellard
{
761 b338082b bellard
    bs->type = type;
762 b338082b bellard
    bs->removable = ((type == BDRV_TYPE_CDROM ||
763 b338082b bellard
                      type == BDRV_TYPE_FLOPPY));
764 b338082b bellard
}
765 b338082b bellard
766 46d4767d bellard
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
767 46d4767d bellard
{
768 46d4767d bellard
    bs->translation = translation;
769 46d4767d bellard
}
770 46d4767d bellard
771 5fafdf24 ths
void bdrv_get_geometry_hint(BlockDriverState *bs,
772 b338082b bellard
                            int *pcyls, int *pheads, int *psecs)
773 b338082b bellard
{
774 b338082b bellard
    *pcyls = bs->cyls;
775 b338082b bellard
    *pheads = bs->heads;
776 b338082b bellard
    *psecs = bs->secs;
777 b338082b bellard
}
778 b338082b bellard
779 b338082b bellard
int bdrv_get_type_hint(BlockDriverState *bs)
780 b338082b bellard
{
781 b338082b bellard
    return bs->type;
782 b338082b bellard
}
783 b338082b bellard
784 46d4767d bellard
int bdrv_get_translation_hint(BlockDriverState *bs)
785 46d4767d bellard
{
786 46d4767d bellard
    return bs->translation;
787 46d4767d bellard
}
788 46d4767d bellard
789 b338082b bellard
int bdrv_is_removable(BlockDriverState *bs)
790 b338082b bellard
{
791 b338082b bellard
    return bs->removable;
792 b338082b bellard
}
793 b338082b bellard
794 b338082b bellard
int bdrv_is_read_only(BlockDriverState *bs)
795 b338082b bellard
{
796 b338082b bellard
    return bs->read_only;
797 b338082b bellard
}
798 b338082b bellard
799 985a03b0 ths
int bdrv_is_sg(BlockDriverState *bs)
800 985a03b0 ths
{
801 985a03b0 ths
    return bs->sg;
802 985a03b0 ths
}
803 985a03b0 ths
804 19cb3738 bellard
/* XXX: no longer used */
805 5fafdf24 ths
void bdrv_set_change_cb(BlockDriverState *bs,
806 b338082b bellard
                        void (*change_cb)(void *opaque), void *opaque)
807 b338082b bellard
{
808 b338082b bellard
    bs->change_cb = change_cb;
809 b338082b bellard
    bs->change_opaque = opaque;
810 b338082b bellard
}
811 b338082b bellard
812 ea2384d3 bellard
int bdrv_is_encrypted(BlockDriverState *bs)
813 ea2384d3 bellard
{
814 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted)
815 ea2384d3 bellard
        return 1;
816 ea2384d3 bellard
    return bs->encrypted;
817 ea2384d3 bellard
}
818 ea2384d3 bellard
819 ea2384d3 bellard
int bdrv_set_key(BlockDriverState *bs, const char *key)
820 ea2384d3 bellard
{
821 ea2384d3 bellard
    int ret;
822 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted) {
823 ea2384d3 bellard
        ret = bdrv_set_key(bs->backing_hd, key);
824 ea2384d3 bellard
        if (ret < 0)
825 ea2384d3 bellard
            return ret;
826 ea2384d3 bellard
        if (!bs->encrypted)
827 ea2384d3 bellard
            return 0;
828 ea2384d3 bellard
    }
829 ea2384d3 bellard
    if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
830 ea2384d3 bellard
        return -1;
831 ea2384d3 bellard
    return bs->drv->bdrv_set_key(bs, key);
832 ea2384d3 bellard
}
833 ea2384d3 bellard
834 ea2384d3 bellard
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
835 ea2384d3 bellard
{
836 19cb3738 bellard
    if (!bs->drv) {
837 ea2384d3 bellard
        buf[0] = '\0';
838 ea2384d3 bellard
    } else {
839 ea2384d3 bellard
        pstrcpy(buf, buf_size, bs->drv->format_name);
840 ea2384d3 bellard
    }
841 ea2384d3 bellard
}
842 ea2384d3 bellard
843 5fafdf24 ths
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
844 ea2384d3 bellard
                         void *opaque)
845 ea2384d3 bellard
{
846 ea2384d3 bellard
    BlockDriver *drv;
847 ea2384d3 bellard
848 ea2384d3 bellard
    for (drv = first_drv; drv != NULL; drv = drv->next) {
849 ea2384d3 bellard
        it(opaque, drv->format_name);
850 ea2384d3 bellard
    }
851 ea2384d3 bellard
}
852 ea2384d3 bellard
853 b338082b bellard
BlockDriverState *bdrv_find(const char *name)
854 b338082b bellard
{
855 b338082b bellard
    BlockDriverState *bs;
856 b338082b bellard
857 b338082b bellard
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
858 b338082b bellard
        if (!strcmp(name, bs->device_name))
859 b338082b bellard
            return bs;
860 b338082b bellard
    }
861 b338082b bellard
    return NULL;
862 b338082b bellard
}
863 b338082b bellard
864 81d0912d bellard
void bdrv_iterate(void (*it)(void *opaque, const char *name), void *opaque)
865 81d0912d bellard
{
866 81d0912d bellard
    BlockDriverState *bs;
867 81d0912d bellard
868 81d0912d bellard
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
869 81d0912d bellard
        it(opaque, bs->device_name);
870 81d0912d bellard
    }
871 81d0912d bellard
}
872 81d0912d bellard
873 ea2384d3 bellard
const char *bdrv_get_device_name(BlockDriverState *bs)
874 ea2384d3 bellard
{
875 ea2384d3 bellard
    return bs->device_name;
876 ea2384d3 bellard
}
877 ea2384d3 bellard
878 7a6cba61 pbrook
void bdrv_flush(BlockDriverState *bs)
879 7a6cba61 pbrook
{
880 7a6cba61 pbrook
    if (bs->drv->bdrv_flush)
881 7a6cba61 pbrook
        bs->drv->bdrv_flush(bs);
882 7a6cba61 pbrook
    if (bs->backing_hd)
883 7a6cba61 pbrook
        bdrv_flush(bs->backing_hd);
884 7a6cba61 pbrook
}
885 7a6cba61 pbrook
886 f58c7b35 ths
/*
887 f58c7b35 ths
 * Returns true iff the specified sector is present in the disk image. Drivers
888 f58c7b35 ths
 * not implementing the functionality are assumed to not support backing files,
889 f58c7b35 ths
 * hence all their sectors are reported as allocated.
890 f58c7b35 ths
 *
891 f58c7b35 ths
 * 'pnum' is set to the number of sectors (including and immediately following
892 f58c7b35 ths
 * the specified sector) that are known to be in the same
893 f58c7b35 ths
 * allocated/unallocated state.
894 f58c7b35 ths
 *
895 f58c7b35 ths
 * 'nb_sectors' is the max value 'pnum' should be set to.
896 f58c7b35 ths
 */
897 f58c7b35 ths
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
898 f58c7b35 ths
        int *pnum)
899 f58c7b35 ths
{
900 f58c7b35 ths
    int64_t n;
901 f58c7b35 ths
    if (!bs->drv->bdrv_is_allocated) {
902 f58c7b35 ths
        if (sector_num >= bs->total_sectors) {
903 f58c7b35 ths
            *pnum = 0;
904 f58c7b35 ths
            return 0;
905 f58c7b35 ths
        }
906 f58c7b35 ths
        n = bs->total_sectors - sector_num;
907 f58c7b35 ths
        *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
908 f58c7b35 ths
        return 1;
909 f58c7b35 ths
    }
910 f58c7b35 ths
    return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
911 f58c7b35 ths
}
912 f58c7b35 ths
913 faf07963 pbrook
#ifndef QEMU_IMG
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 faf07963 pbrook
#endif
972 ea2384d3 bellard
973 5fafdf24 ths
void bdrv_get_backing_filename(BlockDriverState *bs,
974 83f64091 bellard
                               char *filename, int filename_size)
975 83f64091 bellard
{
976 83f64091 bellard
    if (!bs->backing_hd) {
977 83f64091 bellard
        pstrcpy(filename, filename_size, "");
978 83f64091 bellard
    } else {
979 83f64091 bellard
        pstrcpy(filename, filename_size, bs->backing_file);
980 83f64091 bellard
    }
981 83f64091 bellard
}
982 83f64091 bellard
983 5fafdf24 ths
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
984 faea38e7 bellard
                          const uint8_t *buf, int nb_sectors)
985 faea38e7 bellard
{
986 faea38e7 bellard
    BlockDriver *drv = bs->drv;
987 faea38e7 bellard
    if (!drv)
988 19cb3738 bellard
        return -ENOMEDIUM;
989 faea38e7 bellard
    if (!drv->bdrv_write_compressed)
990 faea38e7 bellard
        return -ENOTSUP;
991 faea38e7 bellard
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
992 faea38e7 bellard
}
993 3b46e624 ths
994 faea38e7 bellard
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
995 faea38e7 bellard
{
996 faea38e7 bellard
    BlockDriver *drv = bs->drv;
997 faea38e7 bellard
    if (!drv)
998 19cb3738 bellard
        return -ENOMEDIUM;
999 faea38e7 bellard
    if (!drv->bdrv_get_info)
1000 faea38e7 bellard
        return -ENOTSUP;
1001 faea38e7 bellard
    memset(bdi, 0, sizeof(*bdi));
1002 faea38e7 bellard
    return drv->bdrv_get_info(bs, bdi);
1003 faea38e7 bellard
}
1004 faea38e7 bellard
1005 faea38e7 bellard
/**************************************************************/
1006 faea38e7 bellard
/* handling of snapshots */
1007 faea38e7 bellard
1008 5fafdf24 ths
int bdrv_snapshot_create(BlockDriverState *bs,
1009 faea38e7 bellard
                         QEMUSnapshotInfo *sn_info)
1010 faea38e7 bellard
{
1011 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1012 faea38e7 bellard
    if (!drv)
1013 19cb3738 bellard
        return -ENOMEDIUM;
1014 faea38e7 bellard
    if (!drv->bdrv_snapshot_create)
1015 faea38e7 bellard
        return -ENOTSUP;
1016 faea38e7 bellard
    return drv->bdrv_snapshot_create(bs, sn_info);
1017 faea38e7 bellard
}
1018 faea38e7 bellard
1019 5fafdf24 ths
int bdrv_snapshot_goto(BlockDriverState *bs,
1020 faea38e7 bellard
                       const char *snapshot_id)
1021 faea38e7 bellard
{
1022 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1023 faea38e7 bellard
    if (!drv)
1024 19cb3738 bellard
        return -ENOMEDIUM;
1025 faea38e7 bellard
    if (!drv->bdrv_snapshot_goto)
1026 faea38e7 bellard
        return -ENOTSUP;
1027 faea38e7 bellard
    return drv->bdrv_snapshot_goto(bs, snapshot_id);
1028 faea38e7 bellard
}
1029 faea38e7 bellard
1030 faea38e7 bellard
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1031 faea38e7 bellard
{
1032 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1033 faea38e7 bellard
    if (!drv)
1034 19cb3738 bellard
        return -ENOMEDIUM;
1035 faea38e7 bellard
    if (!drv->bdrv_snapshot_delete)
1036 faea38e7 bellard
        return -ENOTSUP;
1037 faea38e7 bellard
    return drv->bdrv_snapshot_delete(bs, snapshot_id);
1038 faea38e7 bellard
}
1039 faea38e7 bellard
1040 5fafdf24 ths
int bdrv_snapshot_list(BlockDriverState *bs,
1041 faea38e7 bellard
                       QEMUSnapshotInfo **psn_info)
1042 faea38e7 bellard
{
1043 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1044 faea38e7 bellard
    if (!drv)
1045 19cb3738 bellard
        return -ENOMEDIUM;
1046 faea38e7 bellard
    if (!drv->bdrv_snapshot_list)
1047 faea38e7 bellard
        return -ENOTSUP;
1048 faea38e7 bellard
    return drv->bdrv_snapshot_list(bs, psn_info);
1049 faea38e7 bellard
}
1050 faea38e7 bellard
1051 faea38e7 bellard
#define NB_SUFFIXES 4
1052 faea38e7 bellard
1053 faea38e7 bellard
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1054 faea38e7 bellard
{
1055 faea38e7 bellard
    static const char suffixes[NB_SUFFIXES] = "KMGT";
1056 faea38e7 bellard
    int64_t base;
1057 faea38e7 bellard
    int i;
1058 faea38e7 bellard
1059 faea38e7 bellard
    if (size <= 999) {
1060 faea38e7 bellard
        snprintf(buf, buf_size, "%" PRId64, size);
1061 faea38e7 bellard
    } else {
1062 faea38e7 bellard
        base = 1024;
1063 faea38e7 bellard
        for(i = 0; i < NB_SUFFIXES; i++) {
1064 faea38e7 bellard
            if (size < (10 * base)) {
1065 5fafdf24 ths
                snprintf(buf, buf_size, "%0.1f%c",
1066 faea38e7 bellard
                         (double)size / base,
1067 faea38e7 bellard
                         suffixes[i]);
1068 faea38e7 bellard
                break;
1069 faea38e7 bellard
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1070 5fafdf24 ths
                snprintf(buf, buf_size, "%" PRId64 "%c",
1071 faea38e7 bellard
                         ((size + (base >> 1)) / base),
1072 faea38e7 bellard
                         suffixes[i]);
1073 faea38e7 bellard
                break;
1074 faea38e7 bellard
            }
1075 faea38e7 bellard
            base = base * 1024;
1076 faea38e7 bellard
        }
1077 faea38e7 bellard
    }
1078 faea38e7 bellard
    return buf;
1079 faea38e7 bellard
}
1080 faea38e7 bellard
1081 faea38e7 bellard
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1082 faea38e7 bellard
{
1083 faea38e7 bellard
    char buf1[128], date_buf[128], clock_buf[128];
1084 3b9f94e1 bellard
#ifdef _WIN32
1085 3b9f94e1 bellard
    struct tm *ptm;
1086 3b9f94e1 bellard
#else
1087 faea38e7 bellard
    struct tm tm;
1088 3b9f94e1 bellard
#endif
1089 faea38e7 bellard
    time_t ti;
1090 faea38e7 bellard
    int64_t secs;
1091 faea38e7 bellard
1092 faea38e7 bellard
    if (!sn) {
1093 5fafdf24 ths
        snprintf(buf, buf_size,
1094 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
1095 faea38e7 bellard
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1096 faea38e7 bellard
    } else {
1097 faea38e7 bellard
        ti = sn->date_sec;
1098 3b9f94e1 bellard
#ifdef _WIN32
1099 3b9f94e1 bellard
        ptm = localtime(&ti);
1100 3b9f94e1 bellard
        strftime(date_buf, sizeof(date_buf),
1101 3b9f94e1 bellard
                 "%Y-%m-%d %H:%M:%S", ptm);
1102 3b9f94e1 bellard
#else
1103 faea38e7 bellard
        localtime_r(&ti, &tm);
1104 faea38e7 bellard
        strftime(date_buf, sizeof(date_buf),
1105 faea38e7 bellard
                 "%Y-%m-%d %H:%M:%S", &tm);
1106 3b9f94e1 bellard
#endif
1107 faea38e7 bellard
        secs = sn->vm_clock_nsec / 1000000000;
1108 faea38e7 bellard
        snprintf(clock_buf, sizeof(clock_buf),
1109 faea38e7 bellard
                 "%02d:%02d:%02d.%03d",
1110 faea38e7 bellard
                 (int)(secs / 3600),
1111 faea38e7 bellard
                 (int)((secs / 60) % 60),
1112 5fafdf24 ths
                 (int)(secs % 60),
1113 faea38e7 bellard
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1114 faea38e7 bellard
        snprintf(buf, buf_size,
1115 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
1116 faea38e7 bellard
                 sn->id_str, sn->name,
1117 faea38e7 bellard
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1118 faea38e7 bellard
                 date_buf,
1119 faea38e7 bellard
                 clock_buf);
1120 faea38e7 bellard
    }
1121 faea38e7 bellard
    return buf;
1122 faea38e7 bellard
}
1123 faea38e7 bellard
1124 83f64091 bellard
1125 ea2384d3 bellard
/**************************************************************/
1126 83f64091 bellard
/* async I/Os */
1127 ea2384d3 bellard
1128 ce1a14dc pbrook
BlockDriverAIOCB *bdrv_aio_read(BlockDriverState *bs, int64_t sector_num,
1129 ce1a14dc pbrook
                                uint8_t *buf, int nb_sectors,
1130 ce1a14dc pbrook
                                BlockDriverCompletionFunc *cb, void *opaque)
1131 83f64091 bellard
{
1132 83f64091 bellard
    BlockDriver *drv = bs->drv;
1133 a36e69dd ths
    BlockDriverAIOCB *ret;
1134 83f64091 bellard
1135 19cb3738 bellard
    if (!drv)
1136 ce1a14dc pbrook
        return NULL;
1137 3b46e624 ths
1138 83f64091 bellard
    /* XXX: we assume that nb_sectors == 0 is suppored by the async read */
1139 83f64091 bellard
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1140 83f64091 bellard
        memcpy(buf, bs->boot_sector_data, 512);
1141 83f64091 bellard
        sector_num++;
1142 83f64091 bellard
        nb_sectors--;
1143 83f64091 bellard
        buf += 512;
1144 83f64091 bellard
    }
1145 83f64091 bellard
1146 a36e69dd ths
    ret = drv->bdrv_aio_read(bs, sector_num, buf, nb_sectors, cb, opaque);
1147 a36e69dd ths
1148 a36e69dd ths
    if (ret) {
1149 a36e69dd ths
        /* Update stats even though technically transfer has not happened. */
1150 a36e69dd ths
        bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1151 a36e69dd ths
        bs->rd_ops ++;
1152 a36e69dd ths
    }
1153 a36e69dd ths
1154 a36e69dd ths
    return ret;
1155 ea2384d3 bellard
}
1156 ea2384d3 bellard
1157 ce1a14dc pbrook
BlockDriverAIOCB *bdrv_aio_write(BlockDriverState *bs, int64_t sector_num,
1158 ce1a14dc pbrook
                                 const uint8_t *buf, int nb_sectors,
1159 ce1a14dc pbrook
                                 BlockDriverCompletionFunc *cb, void *opaque)
1160 ea2384d3 bellard
{
1161 83f64091 bellard
    BlockDriver *drv = bs->drv;
1162 a36e69dd ths
    BlockDriverAIOCB *ret;
1163 ea2384d3 bellard
1164 19cb3738 bellard
    if (!drv)
1165 ce1a14dc pbrook
        return NULL;
1166 83f64091 bellard
    if (bs->read_only)
1167 ce1a14dc pbrook
        return NULL;
1168 83f64091 bellard
    if (sector_num == 0 && bs->boot_sector_enabled && nb_sectors > 0) {
1169 3b46e624 ths
        memcpy(bs->boot_sector_data, buf, 512);
1170 ea2384d3 bellard
    }
1171 83f64091 bellard
1172 a36e69dd ths
    ret = drv->bdrv_aio_write(bs, sector_num, buf, nb_sectors, cb, opaque);
1173 a36e69dd ths
1174 a36e69dd ths
    if (ret) {
1175 a36e69dd ths
        /* Update stats even though technically transfer has not happened. */
1176 a36e69dd ths
        bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1177 a36e69dd ths
        bs->wr_ops ++;
1178 a36e69dd ths
    }
1179 a36e69dd ths
1180 a36e69dd ths
    return ret;
1181 83f64091 bellard
}
1182 83f64091 bellard
1183 83f64091 bellard
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1184 83f64091 bellard
{
1185 ce1a14dc pbrook
    BlockDriver *drv = acb->bs->drv;
1186 83f64091 bellard
1187 ce1a14dc pbrook
    drv->bdrv_aio_cancel(acb);
1188 83f64091 bellard
}
1189 83f64091 bellard
1190 ce1a14dc pbrook
1191 83f64091 bellard
/**************************************************************/
1192 83f64091 bellard
/* async block device emulation */
1193 83f64091 bellard
1194 faf07963 pbrook
#ifdef QEMU_IMG
1195 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1196 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1197 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1198 ea2384d3 bellard
{
1199 ea2384d3 bellard
    int ret;
1200 ce1a14dc pbrook
    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1201 ce1a14dc pbrook
    cb(opaque, ret);
1202 ce1a14dc pbrook
    return NULL;
1203 ea2384d3 bellard
}
1204 ea2384d3 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 ea2384d3 bellard
{
1209 ea2384d3 bellard
    int ret;
1210 ce1a14dc pbrook
    ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1211 ce1a14dc pbrook
    cb(opaque, ret);
1212 ce1a14dc pbrook
    return NULL;
1213 ea2384d3 bellard
}
1214 ea2384d3 bellard
1215 83f64091 bellard
static void bdrv_aio_cancel_em(BlockDriverAIOCB *acb)
1216 ea2384d3 bellard
{
1217 ea2384d3 bellard
}
1218 83f64091 bellard
#else
1219 ce1a14dc pbrook
static void bdrv_aio_bh_cb(void *opaque)
1220 83f64091 bellard
{
1221 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb = opaque;
1222 ce1a14dc pbrook
    acb->common.cb(acb->common.opaque, acb->ret);
1223 ce1a14dc pbrook
    qemu_aio_release(acb);
1224 83f64091 bellard
}
1225 beac80cd bellard
1226 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_read_em(BlockDriverState *bs,
1227 ce1a14dc pbrook
        int64_t sector_num, uint8_t *buf, int nb_sectors,
1228 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1229 83f64091 bellard
{
1230 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb;
1231 83f64091 bellard
    int ret;
1232 ce1a14dc pbrook
1233 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
1234 ce1a14dc pbrook
    if (!acb->bh)
1235 ce1a14dc pbrook
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1236 ce1a14dc pbrook
    ret = bdrv_read(bs, sector_num, buf, nb_sectors);
1237 ce1a14dc pbrook
    acb->ret = ret;
1238 ce1a14dc pbrook
    qemu_bh_schedule(acb->bh);
1239 ce1a14dc pbrook
    return &acb->common;
1240 beac80cd bellard
}
1241 beac80cd bellard
1242 ce1a14dc pbrook
static BlockDriverAIOCB *bdrv_aio_write_em(BlockDriverState *bs,
1243 ce1a14dc pbrook
        int64_t sector_num, const uint8_t *buf, int nb_sectors,
1244 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1245 beac80cd bellard
{
1246 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb;
1247 83f64091 bellard
    int ret;
1248 83f64091 bellard
1249 ce1a14dc pbrook
    acb = qemu_aio_get(bs, cb, opaque);
1250 ce1a14dc pbrook
    if (!acb->bh)
1251 ce1a14dc pbrook
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1252 ce1a14dc pbrook
    ret = bdrv_write(bs, sector_num, buf, nb_sectors);
1253 ce1a14dc pbrook
    acb->ret = ret;
1254 ce1a14dc pbrook
    qemu_bh_schedule(acb->bh);
1255 ce1a14dc pbrook
    return &acb->common;
1256 beac80cd bellard
}
1257 beac80cd bellard
1258 ce1a14dc pbrook
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1259 ea2384d3 bellard
{
1260 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1261 ce1a14dc pbrook
    qemu_bh_cancel(acb->bh);
1262 ce1a14dc pbrook
    qemu_aio_release(acb);
1263 83f64091 bellard
}
1264 faf07963 pbrook
#endif /* !QEMU_IMG */
1265 ea2384d3 bellard
1266 83f64091 bellard
/**************************************************************/
1267 83f64091 bellard
/* sync block device emulation */
1268 ea2384d3 bellard
1269 83f64091 bellard
static void bdrv_rw_em_cb(void *opaque, int ret)
1270 83f64091 bellard
{
1271 83f64091 bellard
    *(int *)opaque = ret;
1272 ea2384d3 bellard
}
1273 ea2384d3 bellard
1274 83f64091 bellard
#define NOT_DONE 0x7fffffff
1275 83f64091 bellard
1276 5fafdf24 ths
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1277 83f64091 bellard
                        uint8_t *buf, int nb_sectors)
1278 7a6cba61 pbrook
{
1279 ce1a14dc pbrook
    int async_ret;
1280 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1281 83f64091 bellard
1282 83f64091 bellard
    async_ret = NOT_DONE;
1283 5fafdf24 ths
    acb = bdrv_aio_read(bs, sector_num, buf, nb_sectors,
1284 83f64091 bellard
                        bdrv_rw_em_cb, &async_ret);
1285 baf35cb9 aliguori
    if (acb == NULL)
1286 ce1a14dc pbrook
        return -1;
1287 baf35cb9 aliguori
1288 83f64091 bellard
    while (async_ret == NOT_DONE) {
1289 83f64091 bellard
        qemu_aio_wait();
1290 83f64091 bellard
    }
1291 baf35cb9 aliguori
1292 83f64091 bellard
    return async_ret;
1293 7a6cba61 pbrook
}
1294 7a6cba61 pbrook
1295 83f64091 bellard
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1296 83f64091 bellard
                         const uint8_t *buf, int nb_sectors)
1297 83f64091 bellard
{
1298 ce1a14dc pbrook
    int async_ret;
1299 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1300 83f64091 bellard
1301 83f64091 bellard
    async_ret = NOT_DONE;
1302 5fafdf24 ths
    acb = bdrv_aio_write(bs, sector_num, buf, nb_sectors,
1303 83f64091 bellard
                         bdrv_rw_em_cb, &async_ret);
1304 baf35cb9 aliguori
    if (acb == NULL)
1305 ce1a14dc pbrook
        return -1;
1306 83f64091 bellard
    while (async_ret == NOT_DONE) {
1307 83f64091 bellard
        qemu_aio_wait();
1308 83f64091 bellard
    }
1309 83f64091 bellard
    return async_ret;
1310 83f64091 bellard
}
1311 ea2384d3 bellard
1312 ea2384d3 bellard
void bdrv_init(void)
1313 ea2384d3 bellard
{
1314 ea2384d3 bellard
    bdrv_register(&bdrv_raw);
1315 19cb3738 bellard
    bdrv_register(&bdrv_host_device);
1316 ea2384d3 bellard
#ifndef _WIN32
1317 ea2384d3 bellard
    bdrv_register(&bdrv_cow);
1318 ea2384d3 bellard
#endif
1319 ea2384d3 bellard
    bdrv_register(&bdrv_qcow);
1320 ea2384d3 bellard
    bdrv_register(&bdrv_vmdk);
1321 3c56521b bellard
    bdrv_register(&bdrv_cloop);
1322 585d0ed9 bellard
    bdrv_register(&bdrv_dmg);
1323 a8753c34 bellard
    bdrv_register(&bdrv_bochs);
1324 6a0f9e82 bellard
    bdrv_register(&bdrv_vpc);
1325 712e7874 bellard
    bdrv_register(&bdrv_vvfat);
1326 faea38e7 bellard
    bdrv_register(&bdrv_qcow2);
1327 6ada7453 ths
    bdrv_register(&bdrv_parallels);
1328 cd01b4a3 aliguori
#ifndef _WIN32
1329 75818250 ths
    bdrv_register(&bdrv_nbd);
1330 cd01b4a3 aliguori
#endif
1331 ea2384d3 bellard
}
1332 ce1a14dc pbrook
1333 ce1a14dc pbrook
void *qemu_aio_get(BlockDriverState *bs, BlockDriverCompletionFunc *cb,
1334 ce1a14dc pbrook
                   void *opaque)
1335 ce1a14dc pbrook
{
1336 ce1a14dc pbrook
    BlockDriver *drv;
1337 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1338 ce1a14dc pbrook
1339 ce1a14dc pbrook
    drv = bs->drv;
1340 ce1a14dc pbrook
    if (drv->free_aiocb) {
1341 ce1a14dc pbrook
        acb = drv->free_aiocb;
1342 ce1a14dc pbrook
        drv->free_aiocb = acb->next;
1343 ce1a14dc pbrook
    } else {
1344 ce1a14dc pbrook
        acb = qemu_mallocz(drv->aiocb_size);
1345 ce1a14dc pbrook
        if (!acb)
1346 ce1a14dc pbrook
            return NULL;
1347 ce1a14dc pbrook
    }
1348 ce1a14dc pbrook
    acb->bs = bs;
1349 ce1a14dc pbrook
    acb->cb = cb;
1350 ce1a14dc pbrook
    acb->opaque = opaque;
1351 ce1a14dc pbrook
    return acb;
1352 ce1a14dc pbrook
}
1353 ce1a14dc pbrook
1354 ce1a14dc pbrook
void qemu_aio_release(void *p)
1355 ce1a14dc pbrook
{
1356 ce1a14dc pbrook
    BlockDriverAIOCB *acb = p;
1357 ce1a14dc pbrook
    BlockDriver *drv = acb->bs->drv;
1358 ce1a14dc pbrook
    acb->next = drv->free_aiocb;
1359 ce1a14dc pbrook
    drv->free_aiocb = acb;
1360 ce1a14dc pbrook
}
1361 19cb3738 bellard
1362 19cb3738 bellard
/**************************************************************/
1363 19cb3738 bellard
/* removable device support */
1364 19cb3738 bellard
1365 19cb3738 bellard
/**
1366 19cb3738 bellard
 * Return TRUE if the media is present
1367 19cb3738 bellard
 */
1368 19cb3738 bellard
int bdrv_is_inserted(BlockDriverState *bs)
1369 19cb3738 bellard
{
1370 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1371 19cb3738 bellard
    int ret;
1372 19cb3738 bellard
    if (!drv)
1373 19cb3738 bellard
        return 0;
1374 19cb3738 bellard
    if (!drv->bdrv_is_inserted)
1375 19cb3738 bellard
        return 1;
1376 19cb3738 bellard
    ret = drv->bdrv_is_inserted(bs);
1377 19cb3738 bellard
    return ret;
1378 19cb3738 bellard
}
1379 19cb3738 bellard
1380 19cb3738 bellard
/**
1381 19cb3738 bellard
 * Return TRUE if the media changed since the last call to this
1382 5fafdf24 ths
 * function. It is currently only used for floppy disks
1383 19cb3738 bellard
 */
1384 19cb3738 bellard
int bdrv_media_changed(BlockDriverState *bs)
1385 19cb3738 bellard
{
1386 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1387 19cb3738 bellard
    int ret;
1388 19cb3738 bellard
1389 19cb3738 bellard
    if (!drv || !drv->bdrv_media_changed)
1390 19cb3738 bellard
        ret = -ENOTSUP;
1391 19cb3738 bellard
    else
1392 19cb3738 bellard
        ret = drv->bdrv_media_changed(bs);
1393 19cb3738 bellard
    if (ret == -ENOTSUP)
1394 19cb3738 bellard
        ret = bs->media_changed;
1395 19cb3738 bellard
    bs->media_changed = 0;
1396 19cb3738 bellard
    return ret;
1397 19cb3738 bellard
}
1398 19cb3738 bellard
1399 19cb3738 bellard
/**
1400 19cb3738 bellard
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1401 19cb3738 bellard
 */
1402 19cb3738 bellard
void bdrv_eject(BlockDriverState *bs, int eject_flag)
1403 19cb3738 bellard
{
1404 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1405 19cb3738 bellard
    int ret;
1406 19cb3738 bellard
1407 19cb3738 bellard
    if (!drv || !drv->bdrv_eject) {
1408 19cb3738 bellard
        ret = -ENOTSUP;
1409 19cb3738 bellard
    } else {
1410 19cb3738 bellard
        ret = drv->bdrv_eject(bs, eject_flag);
1411 19cb3738 bellard
    }
1412 19cb3738 bellard
    if (ret == -ENOTSUP) {
1413 19cb3738 bellard
        if (eject_flag)
1414 19cb3738 bellard
            bdrv_close(bs);
1415 19cb3738 bellard
    }
1416 19cb3738 bellard
}
1417 19cb3738 bellard
1418 19cb3738 bellard
int bdrv_is_locked(BlockDriverState *bs)
1419 19cb3738 bellard
{
1420 19cb3738 bellard
    return bs->locked;
1421 19cb3738 bellard
}
1422 19cb3738 bellard
1423 19cb3738 bellard
/**
1424 19cb3738 bellard
 * Lock or unlock the media (if it is locked, the user won't be able
1425 19cb3738 bellard
 * to eject it manually).
1426 19cb3738 bellard
 */
1427 19cb3738 bellard
void bdrv_set_locked(BlockDriverState *bs, int locked)
1428 19cb3738 bellard
{
1429 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1430 19cb3738 bellard
1431 19cb3738 bellard
    bs->locked = locked;
1432 19cb3738 bellard
    if (drv && drv->bdrv_set_locked) {
1433 19cb3738 bellard
        drv->bdrv_set_locked(bs, locked);
1434 19cb3738 bellard
    }
1435 19cb3738 bellard
}
1436 985a03b0 ths
1437 985a03b0 ths
/* needed for generic scsi interface */
1438 985a03b0 ths
1439 985a03b0 ths
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1440 985a03b0 ths
{
1441 985a03b0 ths
    BlockDriver *drv = bs->drv;
1442 985a03b0 ths
1443 985a03b0 ths
    if (drv && drv->bdrv_ioctl)
1444 985a03b0 ths
        return drv->bdrv_ioctl(bs, req, buf);
1445 985a03b0 ths
    return -ENOTSUP;
1446 985a03b0 ths
}