Statistics
| Branch: | Revision:

root / block.c @ e2832619

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