Statistics
| Branch: | Revision:

root / block.c @ 153859be

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