Statistics
| Branch: | Revision:

root / block.c @ bb5fc20f

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