Statistics
| Branch: | Revision:

root / block.c @ c988bfad

History | View | Annotate | Download (51.5 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 faf07963 pbrook
#include "qemu-common.h"
26 376253ec aliguori
#include "monitor.h"
27 ea2384d3 bellard
#include "block_int.h"
28 5efa9d5a Anthony Liguori
#include "module.h"
29 fc01f7e7 bellard
30 71e72a19 Juan Quintela
#ifdef CONFIG_BSD
31 7674e7bf bellard
#include <sys/types.h>
32 7674e7bf bellard
#include <sys/stat.h>
33 7674e7bf bellard
#include <sys/ioctl.h>
34 72cf2d4f Blue Swirl
#include <sys/queue.h>
35 c5e97233 blueswir1
#ifndef __DragonFly__
36 7674e7bf bellard
#include <sys/disk.h>
37 7674e7bf bellard
#endif
38 c5e97233 blueswir1
#endif
39 7674e7bf bellard
40 49dc768d aliguori
#ifdef _WIN32
41 49dc768d aliguori
#include <windows.h>
42 49dc768d aliguori
#endif
43 49dc768d aliguori
44 83f64091 bellard
#define SECTOR_BITS 9
45 83f64091 bellard
#define SECTOR_SIZE (1 << SECTOR_BITS)
46 83f64091 bellard
47 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
48 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
49 c87c0672 aliguori
        BlockDriverCompletionFunc *cb, void *opaque);
50 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
51 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
52 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
53 b2e12bc6 Christoph Hellwig
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
54 b2e12bc6 Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque);
55 5fafdf24 ths
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
56 83f64091 bellard
                        uint8_t *buf, int nb_sectors);
57 83f64091 bellard
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
58 83f64091 bellard
                         const uint8_t *buf, int nb_sectors);
59 ec530c81 bellard
60 7ee930d0 blueswir1
BlockDriverState *bdrv_first;
61 7ee930d0 blueswir1
62 ea2384d3 bellard
static BlockDriver *first_drv;
63 ea2384d3 bellard
64 eb852011 Markus Armbruster
/* If non-zero, use only whitelisted block drivers */
65 eb852011 Markus Armbruster
static int use_bdrv_whitelist;
66 eb852011 Markus Armbruster
67 83f64091 bellard
int path_is_absolute(const char *path)
68 3b0d4f61 bellard
{
69 83f64091 bellard
    const char *p;
70 21664424 bellard
#ifdef _WIN32
71 21664424 bellard
    /* specific case for names like: "\\.\d:" */
72 21664424 bellard
    if (*path == '/' || *path == '\\')
73 21664424 bellard
        return 1;
74 21664424 bellard
#endif
75 83f64091 bellard
    p = strchr(path, ':');
76 83f64091 bellard
    if (p)
77 83f64091 bellard
        p++;
78 83f64091 bellard
    else
79 83f64091 bellard
        p = path;
80 3b9f94e1 bellard
#ifdef _WIN32
81 3b9f94e1 bellard
    return (*p == '/' || *p == '\\');
82 3b9f94e1 bellard
#else
83 3b9f94e1 bellard
    return (*p == '/');
84 3b9f94e1 bellard
#endif
85 3b0d4f61 bellard
}
86 3b0d4f61 bellard
87 83f64091 bellard
/* if filename is absolute, just copy it to dest. Otherwise, build a
88 83f64091 bellard
   path to it by considering it is relative to base_path. URL are
89 83f64091 bellard
   supported. */
90 83f64091 bellard
void path_combine(char *dest, int dest_size,
91 83f64091 bellard
                  const char *base_path,
92 83f64091 bellard
                  const char *filename)
93 3b0d4f61 bellard
{
94 83f64091 bellard
    const char *p, *p1;
95 83f64091 bellard
    int len;
96 83f64091 bellard
97 83f64091 bellard
    if (dest_size <= 0)
98 83f64091 bellard
        return;
99 83f64091 bellard
    if (path_is_absolute(filename)) {
100 83f64091 bellard
        pstrcpy(dest, dest_size, filename);
101 83f64091 bellard
    } else {
102 83f64091 bellard
        p = strchr(base_path, ':');
103 83f64091 bellard
        if (p)
104 83f64091 bellard
            p++;
105 83f64091 bellard
        else
106 83f64091 bellard
            p = base_path;
107 3b9f94e1 bellard
        p1 = strrchr(base_path, '/');
108 3b9f94e1 bellard
#ifdef _WIN32
109 3b9f94e1 bellard
        {
110 3b9f94e1 bellard
            const char *p2;
111 3b9f94e1 bellard
            p2 = strrchr(base_path, '\\');
112 3b9f94e1 bellard
            if (!p1 || p2 > p1)
113 3b9f94e1 bellard
                p1 = p2;
114 3b9f94e1 bellard
        }
115 3b9f94e1 bellard
#endif
116 83f64091 bellard
        if (p1)
117 83f64091 bellard
            p1++;
118 83f64091 bellard
        else
119 83f64091 bellard
            p1 = base_path;
120 83f64091 bellard
        if (p1 > p)
121 83f64091 bellard
            p = p1;
122 83f64091 bellard
        len = p - base_path;
123 83f64091 bellard
        if (len > dest_size - 1)
124 83f64091 bellard
            len = dest_size - 1;
125 83f64091 bellard
        memcpy(dest, base_path, len);
126 83f64091 bellard
        dest[len] = '\0';
127 83f64091 bellard
        pstrcat(dest, dest_size, filename);
128 3b0d4f61 bellard
    }
129 3b0d4f61 bellard
}
130 3b0d4f61 bellard
131 5efa9d5a Anthony Liguori
void bdrv_register(BlockDriver *bdrv)
132 ea2384d3 bellard
{
133 f141eafe aliguori
    if (!bdrv->bdrv_aio_readv) {
134 83f64091 bellard
        /* add AIO emulation layer */
135 f141eafe aliguori
        bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
136 f141eafe aliguori
        bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
137 eda578e5 aliguori
    } else if (!bdrv->bdrv_read) {
138 83f64091 bellard
        /* add synchronous IO emulation layer */
139 83f64091 bellard
        bdrv->bdrv_read = bdrv_read_em;
140 83f64091 bellard
        bdrv->bdrv_write = bdrv_write_em;
141 83f64091 bellard
    }
142 b2e12bc6 Christoph Hellwig
143 b2e12bc6 Christoph Hellwig
    if (!bdrv->bdrv_aio_flush)
144 b2e12bc6 Christoph Hellwig
        bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
145 b2e12bc6 Christoph Hellwig
146 ea2384d3 bellard
    bdrv->next = first_drv;
147 ea2384d3 bellard
    first_drv = bdrv;
148 ea2384d3 bellard
}
149 b338082b bellard
150 b338082b bellard
/* create a new block device (by default it is empty) */
151 b338082b bellard
BlockDriverState *bdrv_new(const char *device_name)
152 b338082b bellard
{
153 b338082b bellard
    BlockDriverState **pbs, *bs;
154 b338082b bellard
155 b338082b bellard
    bs = qemu_mallocz(sizeof(BlockDriverState));
156 b338082b bellard
    pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
157 ea2384d3 bellard
    if (device_name[0] != '\0') {
158 ea2384d3 bellard
        /* insert at the end */
159 ea2384d3 bellard
        pbs = &bdrv_first;
160 ea2384d3 bellard
        while (*pbs != NULL)
161 ea2384d3 bellard
            pbs = &(*pbs)->next;
162 ea2384d3 bellard
        *pbs = bs;
163 ea2384d3 bellard
    }
164 b338082b bellard
    return bs;
165 b338082b bellard
}
166 b338082b bellard
167 ea2384d3 bellard
BlockDriver *bdrv_find_format(const char *format_name)
168 ea2384d3 bellard
{
169 ea2384d3 bellard
    BlockDriver *drv1;
170 ea2384d3 bellard
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
171 ea2384d3 bellard
        if (!strcmp(drv1->format_name, format_name))
172 ea2384d3 bellard
            return drv1;
173 ea2384d3 bellard
    }
174 ea2384d3 bellard
    return NULL;
175 ea2384d3 bellard
}
176 ea2384d3 bellard
177 eb852011 Markus Armbruster
static int bdrv_is_whitelisted(BlockDriver *drv)
178 eb852011 Markus Armbruster
{
179 eb852011 Markus Armbruster
    static const char *whitelist[] = {
180 eb852011 Markus Armbruster
        CONFIG_BDRV_WHITELIST
181 eb852011 Markus Armbruster
    };
182 eb852011 Markus Armbruster
    const char **p;
183 eb852011 Markus Armbruster
184 eb852011 Markus Armbruster
    if (!whitelist[0])
185 eb852011 Markus Armbruster
        return 1;               /* no whitelist, anything goes */
186 eb852011 Markus Armbruster
187 eb852011 Markus Armbruster
    for (p = whitelist; *p; p++) {
188 eb852011 Markus Armbruster
        if (!strcmp(drv->format_name, *p)) {
189 eb852011 Markus Armbruster
            return 1;
190 eb852011 Markus Armbruster
        }
191 eb852011 Markus Armbruster
    }
192 eb852011 Markus Armbruster
    return 0;
193 eb852011 Markus Armbruster
}
194 eb852011 Markus Armbruster
195 eb852011 Markus Armbruster
BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
196 eb852011 Markus Armbruster
{
197 eb852011 Markus Armbruster
    BlockDriver *drv = bdrv_find_format(format_name);
198 eb852011 Markus Armbruster
    return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
199 eb852011 Markus Armbruster
}
200 eb852011 Markus Armbruster
201 0e7e1989 Kevin Wolf
int bdrv_create(BlockDriver *drv, const char* filename,
202 0e7e1989 Kevin Wolf
    QEMUOptionParameter *options)
203 ea2384d3 bellard
{
204 ea2384d3 bellard
    if (!drv->bdrv_create)
205 ea2384d3 bellard
        return -ENOTSUP;
206 0e7e1989 Kevin Wolf
207 0e7e1989 Kevin Wolf
    return drv->bdrv_create(filename, options);
208 ea2384d3 bellard
}
209 ea2384d3 bellard
210 d5249393 bellard
#ifdef _WIN32
211 95389c86 bellard
void get_tmp_filename(char *filename, int size)
212 d5249393 bellard
{
213 3b9f94e1 bellard
    char temp_dir[MAX_PATH];
214 3b46e624 ths
215 3b9f94e1 bellard
    GetTempPath(MAX_PATH, temp_dir);
216 3b9f94e1 bellard
    GetTempFileName(temp_dir, "qem", 0, filename);
217 d5249393 bellard
}
218 d5249393 bellard
#else
219 95389c86 bellard
void get_tmp_filename(char *filename, int size)
220 fc01f7e7 bellard
{
221 67b915a5 bellard
    int fd;
222 7ccfb2eb blueswir1
    const char *tmpdir;
223 d5249393 bellard
    /* XXX: race condition possible */
224 0badc1ee aurel32
    tmpdir = getenv("TMPDIR");
225 0badc1ee aurel32
    if (!tmpdir)
226 0badc1ee aurel32
        tmpdir = "/tmp";
227 0badc1ee aurel32
    snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
228 ea2384d3 bellard
    fd = mkstemp(filename);
229 ea2384d3 bellard
    close(fd);
230 ea2384d3 bellard
}
231 d5249393 bellard
#endif
232 fc01f7e7 bellard
233 19cb3738 bellard
#ifdef _WIN32
234 f45512fe bellard
static int is_windows_drive_prefix(const char *filename)
235 f45512fe bellard
{
236 f45512fe bellard
    return (((filename[0] >= 'a' && filename[0] <= 'z') ||
237 f45512fe bellard
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
238 f45512fe bellard
            filename[1] == ':');
239 f45512fe bellard
}
240 3b46e624 ths
241 508c7cb3 Christoph Hellwig
int is_windows_drive(const char *filename)
242 19cb3738 bellard
{
243 5fafdf24 ths
    if (is_windows_drive_prefix(filename) &&
244 f45512fe bellard
        filename[2] == '\0')
245 19cb3738 bellard
        return 1;
246 19cb3738 bellard
    if (strstart(filename, "\\\\.\\", NULL) ||
247 19cb3738 bellard
        strstart(filename, "//./", NULL))
248 19cb3738 bellard
        return 1;
249 19cb3738 bellard
    return 0;
250 19cb3738 bellard
}
251 19cb3738 bellard
#endif
252 19cb3738 bellard
253 83f64091 bellard
static BlockDriver *find_protocol(const char *filename)
254 83f64091 bellard
{
255 83f64091 bellard
    BlockDriver *drv1;
256 83f64091 bellard
    char protocol[128];
257 1cec71e3 Anthony Liguori
    int len;
258 83f64091 bellard
    const char *p;
259 19cb3738 bellard
260 19cb3738 bellard
#ifdef _WIN32
261 f45512fe bellard
    if (is_windows_drive(filename) ||
262 f45512fe bellard
        is_windows_drive_prefix(filename))
263 5efa9d5a Anthony Liguori
        return bdrv_find_format("raw");
264 19cb3738 bellard
#endif
265 1cec71e3 Anthony Liguori
    p = strchr(filename, ':');
266 1cec71e3 Anthony Liguori
    if (!p)
267 5efa9d5a Anthony Liguori
        return bdrv_find_format("raw");
268 1cec71e3 Anthony Liguori
    len = p - filename;
269 1cec71e3 Anthony Liguori
    if (len > sizeof(protocol) - 1)
270 1cec71e3 Anthony Liguori
        len = sizeof(protocol) - 1;
271 1cec71e3 Anthony Liguori
    memcpy(protocol, filename, len);
272 1cec71e3 Anthony Liguori
    protocol[len] = '\0';
273 83f64091 bellard
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
274 5fafdf24 ths
        if (drv1->protocol_name &&
275 83f64091 bellard
            !strcmp(drv1->protocol_name, protocol))
276 83f64091 bellard
            return drv1;
277 83f64091 bellard
    }
278 83f64091 bellard
    return NULL;
279 83f64091 bellard
}
280 83f64091 bellard
281 f3a5d3f8 Christoph Hellwig
/*
282 f3a5d3f8 Christoph Hellwig
 * Detect host devices. By convention, /dev/cdrom[N] is always
283 f3a5d3f8 Christoph Hellwig
 * recognized as a host CDROM.
284 f3a5d3f8 Christoph Hellwig
 */
285 f3a5d3f8 Christoph Hellwig
static BlockDriver *find_hdev_driver(const char *filename)
286 f3a5d3f8 Christoph Hellwig
{
287 508c7cb3 Christoph Hellwig
    int score_max = 0, score;
288 508c7cb3 Christoph Hellwig
    BlockDriver *drv = NULL, *d;
289 f3a5d3f8 Christoph Hellwig
290 508c7cb3 Christoph Hellwig
    for (d = first_drv; d; d = d->next) {
291 508c7cb3 Christoph Hellwig
        if (d->bdrv_probe_device) {
292 508c7cb3 Christoph Hellwig
            score = d->bdrv_probe_device(filename);
293 508c7cb3 Christoph Hellwig
            if (score > score_max) {
294 508c7cb3 Christoph Hellwig
                score_max = score;
295 508c7cb3 Christoph Hellwig
                drv = d;
296 508c7cb3 Christoph Hellwig
            }
297 508c7cb3 Christoph Hellwig
        }
298 19cb3738 bellard
    }
299 f3a5d3f8 Christoph Hellwig
300 508c7cb3 Christoph Hellwig
    return drv;
301 f3a5d3f8 Christoph Hellwig
}
302 3b46e624 ths
303 f3a5d3f8 Christoph Hellwig
static BlockDriver *find_image_format(const char *filename)
304 f3a5d3f8 Christoph Hellwig
{
305 f3a5d3f8 Christoph Hellwig
    int ret, score, score_max;
306 f3a5d3f8 Christoph Hellwig
    BlockDriver *drv1, *drv;
307 f3a5d3f8 Christoph Hellwig
    uint8_t buf[2048];
308 f3a5d3f8 Christoph Hellwig
    BlockDriverState *bs;
309 f3a5d3f8 Christoph Hellwig
310 83f64091 bellard
    drv = find_protocol(filename);
311 19cb3738 bellard
    /* no need to test disk image formats for vvfat */
312 c833ab73 Anthony Liguori
    if (drv && strcmp(drv->format_name, "vvfat") == 0)
313 83f64091 bellard
        return drv;
314 19cb3738 bellard
315 83f64091 bellard
    ret = bdrv_file_open(&bs, filename, BDRV_O_RDONLY);
316 83f64091 bellard
    if (ret < 0)
317 83f64091 bellard
        return NULL;
318 83f64091 bellard
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
319 83f64091 bellard
    bdrv_delete(bs);
320 83f64091 bellard
    if (ret < 0) {
321 83f64091 bellard
        return NULL;
322 83f64091 bellard
    }
323 83f64091 bellard
324 ea2384d3 bellard
    score_max = 0;
325 ea2384d3 bellard
    for(drv1 = first_drv; drv1 != NULL; drv1 = drv1->next) {
326 83f64091 bellard
        if (drv1->bdrv_probe) {
327 83f64091 bellard
            score = drv1->bdrv_probe(buf, ret, filename);
328 83f64091 bellard
            if (score > score_max) {
329 83f64091 bellard
                score_max = score;
330 83f64091 bellard
                drv = drv1;
331 83f64091 bellard
            }
332 0849bf08 bellard
        }
333 fc01f7e7 bellard
    }
334 ea2384d3 bellard
    return drv;
335 ea2384d3 bellard
}
336 ea2384d3 bellard
337 83f64091 bellard
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
338 ea2384d3 bellard
{
339 83f64091 bellard
    BlockDriverState *bs;
340 83f64091 bellard
    int ret;
341 83f64091 bellard
342 83f64091 bellard
    bs = bdrv_new("");
343 83f64091 bellard
    ret = bdrv_open2(bs, filename, flags | BDRV_O_FILE, NULL);
344 83f64091 bellard
    if (ret < 0) {
345 83f64091 bellard
        bdrv_delete(bs);
346 83f64091 bellard
        return ret;
347 3b0d4f61 bellard
    }
348 71d0770c aliguori
    bs->growable = 1;
349 83f64091 bellard
    *pbs = bs;
350 83f64091 bellard
    return 0;
351 83f64091 bellard
}
352 83f64091 bellard
353 83f64091 bellard
int bdrv_open(BlockDriverState *bs, const char *filename, int flags)
354 83f64091 bellard
{
355 83f64091 bellard
    return bdrv_open2(bs, filename, flags, NULL);
356 ea2384d3 bellard
}
357 ea2384d3 bellard
358 83f64091 bellard
int bdrv_open2(BlockDriverState *bs, const char *filename, int flags,
359 ea2384d3 bellard
               BlockDriver *drv)
360 ea2384d3 bellard
{
361 59f2689d Naphtali Sprei
    int ret, open_flags, try_rw;
362 eb5c851f ths
    char tmp_filename[PATH_MAX];
363 eb5c851f ths
    char backing_filename[PATH_MAX];
364 3b46e624 ths
365 ea2384d3 bellard
    bs->is_temporary = 0;
366 ea2384d3 bellard
    bs->encrypted = 0;
367 c0f4ce77 aliguori
    bs->valid_key = 0;
368 e268ca52 aliguori
    /* buffer_alignment defaulted to 512, drivers can change this value */
369 e268ca52 aliguori
    bs->buffer_alignment = 512;
370 712e7874 bellard
371 83f64091 bellard
    if (flags & BDRV_O_SNAPSHOT) {
372 ea2384d3 bellard
        BlockDriverState *bs1;
373 ea2384d3 bellard
        int64_t total_size;
374 7c96d46e aliguori
        int is_protocol = 0;
375 91a073a9 Kevin Wolf
        BlockDriver *bdrv_qcow2;
376 91a073a9 Kevin Wolf
        QEMUOptionParameter *options;
377 3b46e624 ths
378 ea2384d3 bellard
        /* if snapshot, we create a temporary backing file and open it
379 ea2384d3 bellard
           instead of opening 'filename' directly */
380 33e3963e bellard
381 ea2384d3 bellard
        /* if there is a backing file, use it */
382 ea2384d3 bellard
        bs1 = bdrv_new("");
383 5eb45639 aliguori
        ret = bdrv_open2(bs1, filename, 0, drv);
384 51d7c00c aliguori
        if (ret < 0) {
385 ea2384d3 bellard
            bdrv_delete(bs1);
386 51d7c00c aliguori
            return ret;
387 ea2384d3 bellard
        }
388 83f64091 bellard
        total_size = bdrv_getlength(bs1) >> SECTOR_BITS;
389 7c96d46e aliguori
390 7c96d46e aliguori
        if (bs1->drv && bs1->drv->protocol_name)
391 7c96d46e aliguori
            is_protocol = 1;
392 7c96d46e aliguori
393 ea2384d3 bellard
        bdrv_delete(bs1);
394 3b46e624 ths
395 ea2384d3 bellard
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
396 7c96d46e aliguori
397 7c96d46e aliguori
        /* Real path is meaningless for protocols */
398 7c96d46e aliguori
        if (is_protocol)
399 7c96d46e aliguori
            snprintf(backing_filename, sizeof(backing_filename),
400 7c96d46e aliguori
                     "%s", filename);
401 7c96d46e aliguori
        else
402 7c96d46e aliguori
            realpath(filename, backing_filename);
403 7c96d46e aliguori
404 91a073a9 Kevin Wolf
        bdrv_qcow2 = bdrv_find_format("qcow2");
405 91a073a9 Kevin Wolf
        options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
406 91a073a9 Kevin Wolf
407 91a073a9 Kevin Wolf
        set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
408 91a073a9 Kevin Wolf
        set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
409 91a073a9 Kevin Wolf
        if (drv) {
410 91a073a9 Kevin Wolf
            set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
411 91a073a9 Kevin Wolf
                drv->format_name);
412 91a073a9 Kevin Wolf
        }
413 91a073a9 Kevin Wolf
414 91a073a9 Kevin Wolf
        ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
415 51d7c00c aliguori
        if (ret < 0) {
416 51d7c00c aliguori
            return ret;
417 ea2384d3 bellard
        }
418 91a073a9 Kevin Wolf
419 ea2384d3 bellard
        filename = tmp_filename;
420 91a073a9 Kevin Wolf
        drv = bdrv_qcow2;
421 ea2384d3 bellard
        bs->is_temporary = 1;
422 ea2384d3 bellard
    }
423 712e7874 bellard
424 ea2384d3 bellard
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
425 83f64091 bellard
    if (flags & BDRV_O_FILE) {
426 83f64091 bellard
        drv = find_protocol(filename);
427 51d7c00c aliguori
    } else if (!drv) {
428 f3a5d3f8 Christoph Hellwig
        drv = find_hdev_driver(filename);
429 f3a5d3f8 Christoph Hellwig
        if (!drv) {
430 f3a5d3f8 Christoph Hellwig
            drv = find_image_format(filename);
431 f3a5d3f8 Christoph Hellwig
        }
432 51d7c00c aliguori
    }
433 51d7c00c aliguori
    if (!drv) {
434 51d7c00c aliguori
        ret = -ENOENT;
435 51d7c00c aliguori
        goto unlink_and_fail;
436 ea2384d3 bellard
    }
437 ea2384d3 bellard
    bs->drv = drv;
438 ea2384d3 bellard
    bs->opaque = qemu_mallocz(drv->instance_size);
439 e900a7b7 Christoph Hellwig
440 e900a7b7 Christoph Hellwig
    /*
441 e900a7b7 Christoph Hellwig
     * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
442 e900a7b7 Christoph Hellwig
     * write cache to the guest.  We do need the fdatasync to flush
443 e900a7b7 Christoph Hellwig
     * out transactions for block allocations, and we maybe have a
444 e900a7b7 Christoph Hellwig
     * volatile write cache in our backing device to deal with.
445 e900a7b7 Christoph Hellwig
     */
446 e900a7b7 Christoph Hellwig
    if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
447 e900a7b7 Christoph Hellwig
        bs->enable_write_cache = 1;
448 e900a7b7 Christoph Hellwig
449 83f64091 bellard
    /* Note: for compatibility, we open disk image files as RDWR, and
450 83f64091 bellard
       RDONLY as fallback */
451 59f2689d Naphtali Sprei
    try_rw = !bs->read_only || bs->is_temporary;
452 83f64091 bellard
    if (!(flags & BDRV_O_FILE))
453 59f2689d Naphtali Sprei
        open_flags = (try_rw ? BDRV_O_RDWR : 0) |
454 59f2689d Naphtali Sprei
            (flags & (BDRV_O_CACHE_MASK|BDRV_O_NATIVE_AIO));
455 83f64091 bellard
    else
456 83f64091 bellard
        open_flags = flags & ~(BDRV_O_FILE | BDRV_O_SNAPSHOT);
457 eb852011 Markus Armbruster
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv))
458 eb852011 Markus Armbruster
        ret = -ENOTSUP;
459 eb852011 Markus Armbruster
    else
460 eb852011 Markus Armbruster
        ret = drv->bdrv_open(bs, filename, open_flags);
461 a0a83536 aurel32
    if ((ret == -EACCES || ret == -EPERM) && !(flags & BDRV_O_FILE)) {
462 1cec71e3 Anthony Liguori
        ret = drv->bdrv_open(bs, filename, open_flags & ~BDRV_O_RDWR);
463 83f64091 bellard
        bs->read_only = 1;
464 83f64091 bellard
    }
465 ea2384d3 bellard
    if (ret < 0) {
466 ea2384d3 bellard
        qemu_free(bs->opaque);
467 6b21b973 bellard
        bs->opaque = NULL;
468 6b21b973 bellard
        bs->drv = NULL;
469 51d7c00c aliguori
    unlink_and_fail:
470 51d7c00c aliguori
        if (bs->is_temporary)
471 51d7c00c aliguori
            unlink(filename);
472 83f64091 bellard
        return ret;
473 33e3963e bellard
    }
474 d15a771d bellard
    if (drv->bdrv_getlength) {
475 d15a771d bellard
        bs->total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
476 d15a771d bellard
    }
477 67b915a5 bellard
#ifndef _WIN32
478 ea2384d3 bellard
    if (bs->is_temporary) {
479 ea2384d3 bellard
        unlink(filename);
480 ea2384d3 bellard
    }
481 ea2384d3 bellard
#endif
482 83f64091 bellard
    if (bs->backing_file[0] != '\0') {
483 ea2384d3 bellard
        /* if there is a backing file, use it */
484 5eb45639 aliguori
        BlockDriver *back_drv = NULL;
485 ea2384d3 bellard
        bs->backing_hd = bdrv_new("");
486 59f2689d Naphtali Sprei
        /* pass on read_only property to the backing_hd */
487 59f2689d Naphtali Sprei
        bs->backing_hd->read_only = bs->read_only;
488 83f64091 bellard
        path_combine(backing_filename, sizeof(backing_filename),
489 83f64091 bellard
                     filename, bs->backing_file);
490 5eb45639 aliguori
        if (bs->backing_format[0] != '\0')
491 5eb45639 aliguori
            back_drv = bdrv_find_format(bs->backing_format);
492 5eb45639 aliguori
        ret = bdrv_open2(bs->backing_hd, backing_filename, open_flags,
493 5eb45639 aliguori
                         back_drv);
494 51d7c00c aliguori
        if (ret < 0) {
495 51d7c00c aliguori
            bdrv_close(bs);
496 51d7c00c aliguori
            return ret;
497 51d7c00c aliguori
        }
498 33e3963e bellard
    }
499 33e3963e bellard
500 bb5fc20f aliguori
    if (!bdrv_key_required(bs)) {
501 bb5fc20f aliguori
        /* call the change callback */
502 bb5fc20f aliguori
        bs->media_changed = 1;
503 bb5fc20f aliguori
        if (bs->change_cb)
504 bb5fc20f aliguori
            bs->change_cb(bs->change_opaque);
505 bb5fc20f aliguori
    }
506 b338082b bellard
    return 0;
507 fc01f7e7 bellard
}
508 fc01f7e7 bellard
509 fc01f7e7 bellard
void bdrv_close(BlockDriverState *bs)
510 fc01f7e7 bellard
{
511 19cb3738 bellard
    if (bs->drv) {
512 ea2384d3 bellard
        if (bs->backing_hd)
513 ea2384d3 bellard
            bdrv_delete(bs->backing_hd);
514 ea2384d3 bellard
        bs->drv->bdrv_close(bs);
515 ea2384d3 bellard
        qemu_free(bs->opaque);
516 ea2384d3 bellard
#ifdef _WIN32
517 ea2384d3 bellard
        if (bs->is_temporary) {
518 ea2384d3 bellard
            unlink(bs->filename);
519 ea2384d3 bellard
        }
520 67b915a5 bellard
#endif
521 ea2384d3 bellard
        bs->opaque = NULL;
522 ea2384d3 bellard
        bs->drv = NULL;
523 b338082b bellard
524 b338082b bellard
        /* call the change callback */
525 19cb3738 bellard
        bs->media_changed = 1;
526 b338082b bellard
        if (bs->change_cb)
527 b338082b bellard
            bs->change_cb(bs->change_opaque);
528 b338082b bellard
    }
529 b338082b bellard
}
530 b338082b bellard
531 b338082b bellard
void bdrv_delete(BlockDriverState *bs)
532 b338082b bellard
{
533 34c6f050 aurel32
    BlockDriverState **pbs;
534 34c6f050 aurel32
535 34c6f050 aurel32
    pbs = &bdrv_first;
536 34c6f050 aurel32
    while (*pbs != bs && *pbs != NULL)
537 34c6f050 aurel32
        pbs = &(*pbs)->next;
538 34c6f050 aurel32
    if (*pbs == bs)
539 34c6f050 aurel32
        *pbs = bs->next;
540 34c6f050 aurel32
541 b338082b bellard
    bdrv_close(bs);
542 b338082b bellard
    qemu_free(bs);
543 fc01f7e7 bellard
}
544 fc01f7e7 bellard
545 e97fc193 aliguori
/*
546 e97fc193 aliguori
 * Run consistency checks on an image
547 e97fc193 aliguori
 *
548 e97fc193 aliguori
 * Returns the number of errors or -errno when an internal error occurs
549 e97fc193 aliguori
 */
550 e97fc193 aliguori
int bdrv_check(BlockDriverState *bs)
551 e97fc193 aliguori
{
552 e97fc193 aliguori
    if (bs->drv->bdrv_check == NULL) {
553 e97fc193 aliguori
        return -ENOTSUP;
554 e97fc193 aliguori
    }
555 e97fc193 aliguori
556 e97fc193 aliguori
    return bs->drv->bdrv_check(bs);
557 e97fc193 aliguori
}
558 e97fc193 aliguori
559 33e3963e bellard
/* commit COW file into the raw image */
560 33e3963e bellard
int bdrv_commit(BlockDriverState *bs)
561 33e3963e bellard
{
562 19cb3738 bellard
    BlockDriver *drv = bs->drv;
563 83f64091 bellard
    int64_t i, total_sectors;
564 ea2384d3 bellard
    int n, j;
565 ea2384d3 bellard
    unsigned char sector[512];
566 33e3963e bellard
567 19cb3738 bellard
    if (!drv)
568 19cb3738 bellard
        return -ENOMEDIUM;
569 33e3963e bellard
570 33e3963e bellard
    if (bs->read_only) {
571 ea2384d3 bellard
        return -EACCES;
572 33e3963e bellard
    }
573 33e3963e bellard
574 ea2384d3 bellard
    if (!bs->backing_hd) {
575 ea2384d3 bellard
        return -ENOTSUP;
576 ea2384d3 bellard
    }
577 33e3963e bellard
578 83f64091 bellard
    total_sectors = bdrv_getlength(bs) >> SECTOR_BITS;
579 83f64091 bellard
    for (i = 0; i < total_sectors;) {
580 19cb3738 bellard
        if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
581 ea2384d3 bellard
            for(j = 0; j < n; j++) {
582 ea2384d3 bellard
                if (bdrv_read(bs, i, sector, 1) != 0) {
583 ea2384d3 bellard
                    return -EIO;
584 ea2384d3 bellard
                }
585 ea2384d3 bellard
586 ea2384d3 bellard
                if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
587 ea2384d3 bellard
                    return -EIO;
588 ea2384d3 bellard
                }
589 ea2384d3 bellard
                i++;
590 33e3963e bellard
            }
591 ea2384d3 bellard
        } else {
592 ea2384d3 bellard
            i += n;
593 ea2384d3 bellard
        }
594 33e3963e bellard
    }
595 95389c86 bellard
596 19cb3738 bellard
    if (drv->bdrv_make_empty)
597 19cb3738 bellard
        return drv->bdrv_make_empty(bs);
598 95389c86 bellard
599 33e3963e bellard
    return 0;
600 33e3963e bellard
}
601 33e3963e bellard
602 71d0770c aliguori
static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
603 71d0770c aliguori
                                   size_t size)
604 71d0770c aliguori
{
605 71d0770c aliguori
    int64_t len;
606 71d0770c aliguori
607 71d0770c aliguori
    if (!bdrv_is_inserted(bs))
608 71d0770c aliguori
        return -ENOMEDIUM;
609 71d0770c aliguori
610 71d0770c aliguori
    if (bs->growable)
611 71d0770c aliguori
        return 0;
612 71d0770c aliguori
613 71d0770c aliguori
    len = bdrv_getlength(bs);
614 71d0770c aliguori
615 fbb7b4e0 Kevin Wolf
    if (offset < 0)
616 fbb7b4e0 Kevin Wolf
        return -EIO;
617 fbb7b4e0 Kevin Wolf
618 fbb7b4e0 Kevin Wolf
    if ((offset > len) || (len - offset < size))
619 71d0770c aliguori
        return -EIO;
620 71d0770c aliguori
621 71d0770c aliguori
    return 0;
622 71d0770c aliguori
}
623 71d0770c aliguori
624 71d0770c aliguori
static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
625 71d0770c aliguori
                              int nb_sectors)
626 71d0770c aliguori
{
627 999dec57 aliguori
    return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
628 71d0770c aliguori
}
629 71d0770c aliguori
630 19cb3738 bellard
/* return < 0 if error. See bdrv_write() for the return codes */
631 5fafdf24 ths
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
632 fc01f7e7 bellard
              uint8_t *buf, int nb_sectors)
633 fc01f7e7 bellard
{
634 ea2384d3 bellard
    BlockDriver *drv = bs->drv;
635 ea2384d3 bellard
636 19cb3738 bellard
    if (!drv)
637 19cb3738 bellard
        return -ENOMEDIUM;
638 71d0770c aliguori
    if (bdrv_check_request(bs, sector_num, nb_sectors))
639 71d0770c aliguori
        return -EIO;
640 b338082b bellard
641 eda578e5 aliguori
    return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
642 fc01f7e7 bellard
}
643 fc01f7e7 bellard
644 5fafdf24 ths
/* Return < 0 if error. Important errors are:
645 19cb3738 bellard
  -EIO         generic I/O error (may happen for all errors)
646 19cb3738 bellard
  -ENOMEDIUM   No media inserted.
647 19cb3738 bellard
  -EINVAL      Invalid sector number or nb_sectors
648 19cb3738 bellard
  -EACCES      Trying to write a read-only device
649 19cb3738 bellard
*/
650 5fafdf24 ths
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
651 fc01f7e7 bellard
               const uint8_t *buf, int nb_sectors)
652 fc01f7e7 bellard
{
653 83f64091 bellard
    BlockDriver *drv = bs->drv;
654 19cb3738 bellard
    if (!bs->drv)
655 19cb3738 bellard
        return -ENOMEDIUM;
656 0849bf08 bellard
    if (bs->read_only)
657 19cb3738 bellard
        return -EACCES;
658 71d0770c aliguori
    if (bdrv_check_request(bs, sector_num, nb_sectors))
659 71d0770c aliguori
        return -EIO;
660 71d0770c aliguori
661 42fb2807 aliguori
    return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
662 83f64091 bellard
}
663 83f64091 bellard
664 eda578e5 aliguori
int bdrv_pread(BlockDriverState *bs, int64_t offset,
665 eda578e5 aliguori
               void *buf, int count1)
666 83f64091 bellard
{
667 83f64091 bellard
    uint8_t tmp_buf[SECTOR_SIZE];
668 83f64091 bellard
    int len, nb_sectors, count;
669 83f64091 bellard
    int64_t sector_num;
670 83f64091 bellard
671 83f64091 bellard
    count = count1;
672 83f64091 bellard
    /* first read to align to sector start */
673 83f64091 bellard
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
674 83f64091 bellard
    if (len > count)
675 83f64091 bellard
        len = count;
676 83f64091 bellard
    sector_num = offset >> SECTOR_BITS;
677 83f64091 bellard
    if (len > 0) {
678 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
679 83f64091 bellard
            return -EIO;
680 83f64091 bellard
        memcpy(buf, tmp_buf + (offset & (SECTOR_SIZE - 1)), len);
681 83f64091 bellard
        count -= len;
682 83f64091 bellard
        if (count == 0)
683 83f64091 bellard
            return count1;
684 83f64091 bellard
        sector_num++;
685 83f64091 bellard
        buf += len;
686 83f64091 bellard
    }
687 83f64091 bellard
688 83f64091 bellard
    /* read the sectors "in place" */
689 83f64091 bellard
    nb_sectors = count >> SECTOR_BITS;
690 83f64091 bellard
    if (nb_sectors > 0) {
691 83f64091 bellard
        if (bdrv_read(bs, sector_num, buf, nb_sectors) < 0)
692 83f64091 bellard
            return -EIO;
693 83f64091 bellard
        sector_num += nb_sectors;
694 83f64091 bellard
        len = nb_sectors << SECTOR_BITS;
695 83f64091 bellard
        buf += len;
696 83f64091 bellard
        count -= len;
697 83f64091 bellard
    }
698 83f64091 bellard
699 83f64091 bellard
    /* add data from the last sector */
700 83f64091 bellard
    if (count > 0) {
701 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
702 83f64091 bellard
            return -EIO;
703 83f64091 bellard
        memcpy(buf, tmp_buf, count);
704 83f64091 bellard
    }
705 83f64091 bellard
    return count1;
706 83f64091 bellard
}
707 83f64091 bellard
708 eda578e5 aliguori
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
709 eda578e5 aliguori
                const void *buf, int count1)
710 83f64091 bellard
{
711 83f64091 bellard
    uint8_t tmp_buf[SECTOR_SIZE];
712 83f64091 bellard
    int len, nb_sectors, count;
713 83f64091 bellard
    int64_t sector_num;
714 83f64091 bellard
715 83f64091 bellard
    count = count1;
716 83f64091 bellard
    /* first write to align to sector start */
717 83f64091 bellard
    len = (SECTOR_SIZE - offset) & (SECTOR_SIZE - 1);
718 83f64091 bellard
    if (len > count)
719 83f64091 bellard
        len = count;
720 83f64091 bellard
    sector_num = offset >> SECTOR_BITS;
721 83f64091 bellard
    if (len > 0) {
722 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
723 83f64091 bellard
            return -EIO;
724 83f64091 bellard
        memcpy(tmp_buf + (offset & (SECTOR_SIZE - 1)), buf, len);
725 83f64091 bellard
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
726 83f64091 bellard
            return -EIO;
727 83f64091 bellard
        count -= len;
728 83f64091 bellard
        if (count == 0)
729 83f64091 bellard
            return count1;
730 83f64091 bellard
        sector_num++;
731 83f64091 bellard
        buf += len;
732 83f64091 bellard
    }
733 83f64091 bellard
734 83f64091 bellard
    /* write the sectors "in place" */
735 83f64091 bellard
    nb_sectors = count >> SECTOR_BITS;
736 83f64091 bellard
    if (nb_sectors > 0) {
737 83f64091 bellard
        if (bdrv_write(bs, sector_num, buf, nb_sectors) < 0)
738 83f64091 bellard
            return -EIO;
739 83f64091 bellard
        sector_num += nb_sectors;
740 83f64091 bellard
        len = nb_sectors << SECTOR_BITS;
741 83f64091 bellard
        buf += len;
742 83f64091 bellard
        count -= len;
743 83f64091 bellard
    }
744 83f64091 bellard
745 83f64091 bellard
    /* add data from the last sector */
746 83f64091 bellard
    if (count > 0) {
747 83f64091 bellard
        if (bdrv_read(bs, sector_num, tmp_buf, 1) < 0)
748 83f64091 bellard
            return -EIO;
749 83f64091 bellard
        memcpy(tmp_buf, buf, count);
750 83f64091 bellard
        if (bdrv_write(bs, sector_num, tmp_buf, 1) < 0)
751 83f64091 bellard
            return -EIO;
752 83f64091 bellard
    }
753 83f64091 bellard
    return count1;
754 83f64091 bellard
}
755 83f64091 bellard
756 83f64091 bellard
/**
757 83f64091 bellard
 * Truncate file to 'offset' bytes (needed only for file protocols)
758 83f64091 bellard
 */
759 83f64091 bellard
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
760 83f64091 bellard
{
761 83f64091 bellard
    BlockDriver *drv = bs->drv;
762 83f64091 bellard
    if (!drv)
763 19cb3738 bellard
        return -ENOMEDIUM;
764 83f64091 bellard
    if (!drv->bdrv_truncate)
765 83f64091 bellard
        return -ENOTSUP;
766 59f2689d Naphtali Sprei
    if (bs->read_only)
767 59f2689d Naphtali Sprei
        return -EACCES;
768 83f64091 bellard
    return drv->bdrv_truncate(bs, offset);
769 83f64091 bellard
}
770 83f64091 bellard
771 83f64091 bellard
/**
772 83f64091 bellard
 * Length of a file in bytes. Return < 0 if error or unknown.
773 83f64091 bellard
 */
774 83f64091 bellard
int64_t bdrv_getlength(BlockDriverState *bs)
775 83f64091 bellard
{
776 83f64091 bellard
    BlockDriver *drv = bs->drv;
777 83f64091 bellard
    if (!drv)
778 19cb3738 bellard
        return -ENOMEDIUM;
779 83f64091 bellard
    if (!drv->bdrv_getlength) {
780 83f64091 bellard
        /* legacy mode */
781 83f64091 bellard
        return bs->total_sectors * SECTOR_SIZE;
782 83f64091 bellard
    }
783 83f64091 bellard
    return drv->bdrv_getlength(bs);
784 fc01f7e7 bellard
}
785 fc01f7e7 bellard
786 19cb3738 bellard
/* return 0 as number of sectors if no device present or error */
787 96b8f136 ths
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
788 fc01f7e7 bellard
{
789 19cb3738 bellard
    int64_t length;
790 19cb3738 bellard
    length = bdrv_getlength(bs);
791 19cb3738 bellard
    if (length < 0)
792 19cb3738 bellard
        length = 0;
793 19cb3738 bellard
    else
794 19cb3738 bellard
        length = length >> SECTOR_BITS;
795 19cb3738 bellard
    *nb_sectors_ptr = length;
796 fc01f7e7 bellard
}
797 cf98951b bellard
798 f3d54fc4 aliguori
struct partition {
799 f3d54fc4 aliguori
        uint8_t boot_ind;           /* 0x80 - active */
800 f3d54fc4 aliguori
        uint8_t head;               /* starting head */
801 f3d54fc4 aliguori
        uint8_t sector;             /* starting sector */
802 f3d54fc4 aliguori
        uint8_t cyl;                /* starting cylinder */
803 f3d54fc4 aliguori
        uint8_t sys_ind;            /* What partition type */
804 f3d54fc4 aliguori
        uint8_t end_head;           /* end head */
805 f3d54fc4 aliguori
        uint8_t end_sector;         /* end sector */
806 f3d54fc4 aliguori
        uint8_t end_cyl;            /* end cylinder */
807 f3d54fc4 aliguori
        uint32_t start_sect;        /* starting sector counting from 0 */
808 f3d54fc4 aliguori
        uint32_t nr_sects;          /* nr of sectors in partition */
809 f3d54fc4 aliguori
} __attribute__((packed));
810 f3d54fc4 aliguori
811 f3d54fc4 aliguori
/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
812 f3d54fc4 aliguori
static int guess_disk_lchs(BlockDriverState *bs,
813 f3d54fc4 aliguori
                           int *pcylinders, int *pheads, int *psectors)
814 f3d54fc4 aliguori
{
815 f3d54fc4 aliguori
    uint8_t buf[512];
816 f3d54fc4 aliguori
    int ret, i, heads, sectors, cylinders;
817 f3d54fc4 aliguori
    struct partition *p;
818 f3d54fc4 aliguori
    uint32_t nr_sects;
819 a38131b6 blueswir1
    uint64_t nb_sectors;
820 f3d54fc4 aliguori
821 f3d54fc4 aliguori
    bdrv_get_geometry(bs, &nb_sectors);
822 f3d54fc4 aliguori
823 f3d54fc4 aliguori
    ret = bdrv_read(bs, 0, buf, 1);
824 f3d54fc4 aliguori
    if (ret < 0)
825 f3d54fc4 aliguori
        return -1;
826 f3d54fc4 aliguori
    /* test msdos magic */
827 f3d54fc4 aliguori
    if (buf[510] != 0x55 || buf[511] != 0xaa)
828 f3d54fc4 aliguori
        return -1;
829 f3d54fc4 aliguori
    for(i = 0; i < 4; i++) {
830 f3d54fc4 aliguori
        p = ((struct partition *)(buf + 0x1be)) + i;
831 f3d54fc4 aliguori
        nr_sects = le32_to_cpu(p->nr_sects);
832 f3d54fc4 aliguori
        if (nr_sects && p->end_head) {
833 f3d54fc4 aliguori
            /* We make the assumption that the partition terminates on
834 f3d54fc4 aliguori
               a cylinder boundary */
835 f3d54fc4 aliguori
            heads = p->end_head + 1;
836 f3d54fc4 aliguori
            sectors = p->end_sector & 63;
837 f3d54fc4 aliguori
            if (sectors == 0)
838 f3d54fc4 aliguori
                continue;
839 f3d54fc4 aliguori
            cylinders = nb_sectors / (heads * sectors);
840 f3d54fc4 aliguori
            if (cylinders < 1 || cylinders > 16383)
841 f3d54fc4 aliguori
                continue;
842 f3d54fc4 aliguori
            *pheads = heads;
843 f3d54fc4 aliguori
            *psectors = sectors;
844 f3d54fc4 aliguori
            *pcylinders = cylinders;
845 f3d54fc4 aliguori
#if 0
846 f3d54fc4 aliguori
            printf("guessed geometry: LCHS=%d %d %d\n",
847 f3d54fc4 aliguori
                   cylinders, heads, sectors);
848 f3d54fc4 aliguori
#endif
849 f3d54fc4 aliguori
            return 0;
850 f3d54fc4 aliguori
        }
851 f3d54fc4 aliguori
    }
852 f3d54fc4 aliguori
    return -1;
853 f3d54fc4 aliguori
}
854 f3d54fc4 aliguori
855 f3d54fc4 aliguori
void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
856 f3d54fc4 aliguori
{
857 f3d54fc4 aliguori
    int translation, lba_detected = 0;
858 f3d54fc4 aliguori
    int cylinders, heads, secs;
859 a38131b6 blueswir1
    uint64_t nb_sectors;
860 f3d54fc4 aliguori
861 f3d54fc4 aliguori
    /* if a geometry hint is available, use it */
862 f3d54fc4 aliguori
    bdrv_get_geometry(bs, &nb_sectors);
863 f3d54fc4 aliguori
    bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
864 f3d54fc4 aliguori
    translation = bdrv_get_translation_hint(bs);
865 f3d54fc4 aliguori
    if (cylinders != 0) {
866 f3d54fc4 aliguori
        *pcyls = cylinders;
867 f3d54fc4 aliguori
        *pheads = heads;
868 f3d54fc4 aliguori
        *psecs = secs;
869 f3d54fc4 aliguori
    } else {
870 f3d54fc4 aliguori
        if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
871 f3d54fc4 aliguori
            if (heads > 16) {
872 f3d54fc4 aliguori
                /* if heads > 16, it means that a BIOS LBA
873 f3d54fc4 aliguori
                   translation was active, so the default
874 f3d54fc4 aliguori
                   hardware geometry is OK */
875 f3d54fc4 aliguori
                lba_detected = 1;
876 f3d54fc4 aliguori
                goto default_geometry;
877 f3d54fc4 aliguori
            } else {
878 f3d54fc4 aliguori
                *pcyls = cylinders;
879 f3d54fc4 aliguori
                *pheads = heads;
880 f3d54fc4 aliguori
                *psecs = secs;
881 f3d54fc4 aliguori
                /* disable any translation to be in sync with
882 f3d54fc4 aliguori
                   the logical geometry */
883 f3d54fc4 aliguori
                if (translation == BIOS_ATA_TRANSLATION_AUTO) {
884 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
885 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_NONE);
886 f3d54fc4 aliguori
                }
887 f3d54fc4 aliguori
            }
888 f3d54fc4 aliguori
        } else {
889 f3d54fc4 aliguori
        default_geometry:
890 f3d54fc4 aliguori
            /* if no geometry, use a standard physical disk geometry */
891 f3d54fc4 aliguori
            cylinders = nb_sectors / (16 * 63);
892 f3d54fc4 aliguori
893 f3d54fc4 aliguori
            if (cylinders > 16383)
894 f3d54fc4 aliguori
                cylinders = 16383;
895 f3d54fc4 aliguori
            else if (cylinders < 2)
896 f3d54fc4 aliguori
                cylinders = 2;
897 f3d54fc4 aliguori
            *pcyls = cylinders;
898 f3d54fc4 aliguori
            *pheads = 16;
899 f3d54fc4 aliguori
            *psecs = 63;
900 f3d54fc4 aliguori
            if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
901 f3d54fc4 aliguori
                if ((*pcyls * *pheads) <= 131072) {
902 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
903 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_LARGE);
904 f3d54fc4 aliguori
                } else {
905 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
906 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_LBA);
907 f3d54fc4 aliguori
                }
908 f3d54fc4 aliguori
            }
909 f3d54fc4 aliguori
        }
910 f3d54fc4 aliguori
        bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
911 f3d54fc4 aliguori
    }
912 f3d54fc4 aliguori
}
913 f3d54fc4 aliguori
914 5fafdf24 ths
void bdrv_set_geometry_hint(BlockDriverState *bs,
915 b338082b bellard
                            int cyls, int heads, int secs)
916 b338082b bellard
{
917 b338082b bellard
    bs->cyls = cyls;
918 b338082b bellard
    bs->heads = heads;
919 b338082b bellard
    bs->secs = secs;
920 b338082b bellard
}
921 b338082b bellard
922 b338082b bellard
void bdrv_set_type_hint(BlockDriverState *bs, int type)
923 b338082b bellard
{
924 b338082b bellard
    bs->type = type;
925 b338082b bellard
    bs->removable = ((type == BDRV_TYPE_CDROM ||
926 b338082b bellard
                      type == BDRV_TYPE_FLOPPY));
927 b338082b bellard
}
928 b338082b bellard
929 46d4767d bellard
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
930 46d4767d bellard
{
931 46d4767d bellard
    bs->translation = translation;
932 46d4767d bellard
}
933 46d4767d bellard
934 5fafdf24 ths
void bdrv_get_geometry_hint(BlockDriverState *bs,
935 b338082b bellard
                            int *pcyls, int *pheads, int *psecs)
936 b338082b bellard
{
937 b338082b bellard
    *pcyls = bs->cyls;
938 b338082b bellard
    *pheads = bs->heads;
939 b338082b bellard
    *psecs = bs->secs;
940 b338082b bellard
}
941 b338082b bellard
942 b338082b bellard
int bdrv_get_type_hint(BlockDriverState *bs)
943 b338082b bellard
{
944 b338082b bellard
    return bs->type;
945 b338082b bellard
}
946 b338082b bellard
947 46d4767d bellard
int bdrv_get_translation_hint(BlockDriverState *bs)
948 46d4767d bellard
{
949 46d4767d bellard
    return bs->translation;
950 46d4767d bellard
}
951 46d4767d bellard
952 b338082b bellard
int bdrv_is_removable(BlockDriverState *bs)
953 b338082b bellard
{
954 b338082b bellard
    return bs->removable;
955 b338082b bellard
}
956 b338082b bellard
957 b338082b bellard
int bdrv_is_read_only(BlockDriverState *bs)
958 b338082b bellard
{
959 b338082b bellard
    return bs->read_only;
960 b338082b bellard
}
961 b338082b bellard
962 59f2689d Naphtali Sprei
int bdrv_set_read_only(BlockDriverState *bs, int read_only)
963 59f2689d Naphtali Sprei
{
964 59f2689d Naphtali Sprei
    int ret = bs->read_only;
965 59f2689d Naphtali Sprei
    bs->read_only = read_only;
966 59f2689d Naphtali Sprei
    return ret;
967 59f2689d Naphtali Sprei
}
968 59f2689d Naphtali Sprei
969 985a03b0 ths
int bdrv_is_sg(BlockDriverState *bs)
970 985a03b0 ths
{
971 985a03b0 ths
    return bs->sg;
972 985a03b0 ths
}
973 985a03b0 ths
974 e900a7b7 Christoph Hellwig
int bdrv_enable_write_cache(BlockDriverState *bs)
975 e900a7b7 Christoph Hellwig
{
976 e900a7b7 Christoph Hellwig
    return bs->enable_write_cache;
977 e900a7b7 Christoph Hellwig
}
978 e900a7b7 Christoph Hellwig
979 19cb3738 bellard
/* XXX: no longer used */
980 5fafdf24 ths
void bdrv_set_change_cb(BlockDriverState *bs,
981 b338082b bellard
                        void (*change_cb)(void *opaque), void *opaque)
982 b338082b bellard
{
983 b338082b bellard
    bs->change_cb = change_cb;
984 b338082b bellard
    bs->change_opaque = opaque;
985 b338082b bellard
}
986 b338082b bellard
987 ea2384d3 bellard
int bdrv_is_encrypted(BlockDriverState *bs)
988 ea2384d3 bellard
{
989 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted)
990 ea2384d3 bellard
        return 1;
991 ea2384d3 bellard
    return bs->encrypted;
992 ea2384d3 bellard
}
993 ea2384d3 bellard
994 c0f4ce77 aliguori
int bdrv_key_required(BlockDriverState *bs)
995 c0f4ce77 aliguori
{
996 c0f4ce77 aliguori
    BlockDriverState *backing_hd = bs->backing_hd;
997 c0f4ce77 aliguori
998 c0f4ce77 aliguori
    if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
999 c0f4ce77 aliguori
        return 1;
1000 c0f4ce77 aliguori
    return (bs->encrypted && !bs->valid_key);
1001 c0f4ce77 aliguori
}
1002 c0f4ce77 aliguori
1003 ea2384d3 bellard
int bdrv_set_key(BlockDriverState *bs, const char *key)
1004 ea2384d3 bellard
{
1005 ea2384d3 bellard
    int ret;
1006 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted) {
1007 ea2384d3 bellard
        ret = bdrv_set_key(bs->backing_hd, key);
1008 ea2384d3 bellard
        if (ret < 0)
1009 ea2384d3 bellard
            return ret;
1010 ea2384d3 bellard
        if (!bs->encrypted)
1011 ea2384d3 bellard
            return 0;
1012 ea2384d3 bellard
    }
1013 ea2384d3 bellard
    if (!bs->encrypted || !bs->drv || !bs->drv->bdrv_set_key)
1014 ea2384d3 bellard
        return -1;
1015 c0f4ce77 aliguori
    ret = bs->drv->bdrv_set_key(bs, key);
1016 bb5fc20f aliguori
    if (ret < 0) {
1017 bb5fc20f aliguori
        bs->valid_key = 0;
1018 bb5fc20f aliguori
    } else if (!bs->valid_key) {
1019 bb5fc20f aliguori
        bs->valid_key = 1;
1020 bb5fc20f aliguori
        /* call the change callback now, we skipped it on open */
1021 bb5fc20f aliguori
        bs->media_changed = 1;
1022 bb5fc20f aliguori
        if (bs->change_cb)
1023 bb5fc20f aliguori
            bs->change_cb(bs->change_opaque);
1024 bb5fc20f aliguori
    }
1025 c0f4ce77 aliguori
    return ret;
1026 ea2384d3 bellard
}
1027 ea2384d3 bellard
1028 ea2384d3 bellard
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1029 ea2384d3 bellard
{
1030 19cb3738 bellard
    if (!bs->drv) {
1031 ea2384d3 bellard
        buf[0] = '\0';
1032 ea2384d3 bellard
    } else {
1033 ea2384d3 bellard
        pstrcpy(buf, buf_size, bs->drv->format_name);
1034 ea2384d3 bellard
    }
1035 ea2384d3 bellard
}
1036 ea2384d3 bellard
1037 5fafdf24 ths
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1038 ea2384d3 bellard
                         void *opaque)
1039 ea2384d3 bellard
{
1040 ea2384d3 bellard
    BlockDriver *drv;
1041 ea2384d3 bellard
1042 ea2384d3 bellard
    for (drv = first_drv; drv != NULL; drv = drv->next) {
1043 ea2384d3 bellard
        it(opaque, drv->format_name);
1044 ea2384d3 bellard
    }
1045 ea2384d3 bellard
}
1046 ea2384d3 bellard
1047 b338082b bellard
BlockDriverState *bdrv_find(const char *name)
1048 b338082b bellard
{
1049 b338082b bellard
    BlockDriverState *bs;
1050 b338082b bellard
1051 b338082b bellard
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1052 b338082b bellard
        if (!strcmp(name, bs->device_name))
1053 b338082b bellard
            return bs;
1054 b338082b bellard
    }
1055 b338082b bellard
    return NULL;
1056 b338082b bellard
}
1057 b338082b bellard
1058 51de9760 aliguori
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1059 81d0912d bellard
{
1060 81d0912d bellard
    BlockDriverState *bs;
1061 81d0912d bellard
1062 81d0912d bellard
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1063 51de9760 aliguori
        it(opaque, bs);
1064 81d0912d bellard
    }
1065 81d0912d bellard
}
1066 81d0912d bellard
1067 ea2384d3 bellard
const char *bdrv_get_device_name(BlockDriverState *bs)
1068 ea2384d3 bellard
{
1069 ea2384d3 bellard
    return bs->device_name;
1070 ea2384d3 bellard
}
1071 ea2384d3 bellard
1072 7a6cba61 pbrook
void bdrv_flush(BlockDriverState *bs)
1073 7a6cba61 pbrook
{
1074 081501da aliguori
    if (!bs->drv)
1075 081501da aliguori
        return;
1076 7a6cba61 pbrook
    if (bs->drv->bdrv_flush)
1077 7a6cba61 pbrook
        bs->drv->bdrv_flush(bs);
1078 7a6cba61 pbrook
    if (bs->backing_hd)
1079 7a6cba61 pbrook
        bdrv_flush(bs->backing_hd);
1080 7a6cba61 pbrook
}
1081 7a6cba61 pbrook
1082 c6ca28d6 aliguori
void bdrv_flush_all(void)
1083 c6ca28d6 aliguori
{
1084 c6ca28d6 aliguori
    BlockDriverState *bs;
1085 c6ca28d6 aliguori
1086 c6ca28d6 aliguori
    for (bs = bdrv_first; bs != NULL; bs = bs->next)
1087 c6ca28d6 aliguori
        if (bs->drv && !bdrv_is_read_only(bs) && 
1088 c6ca28d6 aliguori
            (!bdrv_is_removable(bs) || bdrv_is_inserted(bs)))
1089 c6ca28d6 aliguori
            bdrv_flush(bs);
1090 c6ca28d6 aliguori
}
1091 c6ca28d6 aliguori
1092 f58c7b35 ths
/*
1093 f58c7b35 ths
 * Returns true iff the specified sector is present in the disk image. Drivers
1094 f58c7b35 ths
 * not implementing the functionality are assumed to not support backing files,
1095 f58c7b35 ths
 * hence all their sectors are reported as allocated.
1096 f58c7b35 ths
 *
1097 f58c7b35 ths
 * 'pnum' is set to the number of sectors (including and immediately following
1098 f58c7b35 ths
 * the specified sector) that are known to be in the same
1099 f58c7b35 ths
 * allocated/unallocated state.
1100 f58c7b35 ths
 *
1101 f58c7b35 ths
 * 'nb_sectors' is the max value 'pnum' should be set to.
1102 f58c7b35 ths
 */
1103 f58c7b35 ths
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1104 f58c7b35 ths
        int *pnum)
1105 f58c7b35 ths
{
1106 f58c7b35 ths
    int64_t n;
1107 f58c7b35 ths
    if (!bs->drv->bdrv_is_allocated) {
1108 f58c7b35 ths
        if (sector_num >= bs->total_sectors) {
1109 f58c7b35 ths
            *pnum = 0;
1110 f58c7b35 ths
            return 0;
1111 f58c7b35 ths
        }
1112 f58c7b35 ths
        n = bs->total_sectors - sector_num;
1113 f58c7b35 ths
        *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1114 f58c7b35 ths
        return 1;
1115 f58c7b35 ths
    }
1116 f58c7b35 ths
    return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1117 f58c7b35 ths
}
1118 f58c7b35 ths
1119 376253ec aliguori
void bdrv_info(Monitor *mon)
1120 b338082b bellard
{
1121 b338082b bellard
    BlockDriverState *bs;
1122 b338082b bellard
1123 b338082b bellard
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1124 376253ec aliguori
        monitor_printf(mon, "%s:", bs->device_name);
1125 376253ec aliguori
        monitor_printf(mon, " type=");
1126 b338082b bellard
        switch(bs->type) {
1127 b338082b bellard
        case BDRV_TYPE_HD:
1128 376253ec aliguori
            monitor_printf(mon, "hd");
1129 b338082b bellard
            break;
1130 b338082b bellard
        case BDRV_TYPE_CDROM:
1131 376253ec aliguori
            monitor_printf(mon, "cdrom");
1132 b338082b bellard
            break;
1133 b338082b bellard
        case BDRV_TYPE_FLOPPY:
1134 376253ec aliguori
            monitor_printf(mon, "floppy");
1135 b338082b bellard
            break;
1136 b338082b bellard
        }
1137 376253ec aliguori
        monitor_printf(mon, " removable=%d", bs->removable);
1138 b338082b bellard
        if (bs->removable) {
1139 376253ec aliguori
            monitor_printf(mon, " locked=%d", bs->locked);
1140 b338082b bellard
        }
1141 19cb3738 bellard
        if (bs->drv) {
1142 376253ec aliguori
            monitor_printf(mon, " file=");
1143 376253ec aliguori
            monitor_print_filename(mon, bs->filename);
1144 fef30743 ths
            if (bs->backing_file[0] != '\0') {
1145 376253ec aliguori
                monitor_printf(mon, " backing_file=");
1146 376253ec aliguori
                monitor_print_filename(mon, bs->backing_file);
1147 376253ec aliguori
            }
1148 376253ec aliguori
            monitor_printf(mon, " ro=%d", bs->read_only);
1149 376253ec aliguori
            monitor_printf(mon, " drv=%s", bs->drv->format_name);
1150 376253ec aliguori
            monitor_printf(mon, " encrypted=%d", bdrv_is_encrypted(bs));
1151 b338082b bellard
        } else {
1152 376253ec aliguori
            monitor_printf(mon, " [not inserted]");
1153 b338082b bellard
        }
1154 376253ec aliguori
        monitor_printf(mon, "\n");
1155 b338082b bellard
    }
1156 b338082b bellard
}
1157 a36e69dd ths
1158 a36e69dd ths
/* The "info blockstats" command. */
1159 376253ec aliguori
void bdrv_info_stats(Monitor *mon)
1160 a36e69dd ths
{
1161 a36e69dd ths
    BlockDriverState *bs;
1162 a36e69dd ths
1163 a36e69dd ths
    for (bs = bdrv_first; bs != NULL; bs = bs->next) {
1164 376253ec aliguori
        monitor_printf(mon, "%s:"
1165 376253ec aliguori
                       " rd_bytes=%" PRIu64
1166 376253ec aliguori
                       " wr_bytes=%" PRIu64
1167 376253ec aliguori
                       " rd_operations=%" PRIu64
1168 376253ec aliguori
                       " wr_operations=%" PRIu64
1169 ebf53fcd aliguori
                       "\n",
1170 376253ec aliguori
                       bs->device_name,
1171 376253ec aliguori
                       bs->rd_bytes, bs->wr_bytes,
1172 376253ec aliguori
                       bs->rd_ops, bs->wr_ops);
1173 a36e69dd ths
    }
1174 a36e69dd ths
}
1175 ea2384d3 bellard
1176 045df330 aliguori
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1177 045df330 aliguori
{
1178 045df330 aliguori
    if (bs->backing_hd && bs->backing_hd->encrypted)
1179 045df330 aliguori
        return bs->backing_file;
1180 045df330 aliguori
    else if (bs->encrypted)
1181 045df330 aliguori
        return bs->filename;
1182 045df330 aliguori
    else
1183 045df330 aliguori
        return NULL;
1184 045df330 aliguori
}
1185 045df330 aliguori
1186 5fafdf24 ths
void bdrv_get_backing_filename(BlockDriverState *bs,
1187 83f64091 bellard
                               char *filename, int filename_size)
1188 83f64091 bellard
{
1189 83f64091 bellard
    if (!bs->backing_hd) {
1190 83f64091 bellard
        pstrcpy(filename, filename_size, "");
1191 83f64091 bellard
    } else {
1192 83f64091 bellard
        pstrcpy(filename, filename_size, bs->backing_file);
1193 83f64091 bellard
    }
1194 83f64091 bellard
}
1195 83f64091 bellard
1196 5fafdf24 ths
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1197 faea38e7 bellard
                          const uint8_t *buf, int nb_sectors)
1198 faea38e7 bellard
{
1199 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1200 faea38e7 bellard
    if (!drv)
1201 19cb3738 bellard
        return -ENOMEDIUM;
1202 faea38e7 bellard
    if (!drv->bdrv_write_compressed)
1203 faea38e7 bellard
        return -ENOTSUP;
1204 fbb7b4e0 Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1205 fbb7b4e0 Kevin Wolf
        return -EIO;
1206 faea38e7 bellard
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1207 faea38e7 bellard
}
1208 3b46e624 ths
1209 faea38e7 bellard
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1210 faea38e7 bellard
{
1211 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1212 faea38e7 bellard
    if (!drv)
1213 19cb3738 bellard
        return -ENOMEDIUM;
1214 faea38e7 bellard
    if (!drv->bdrv_get_info)
1215 faea38e7 bellard
        return -ENOTSUP;
1216 faea38e7 bellard
    memset(bdi, 0, sizeof(*bdi));
1217 faea38e7 bellard
    return drv->bdrv_get_info(bs, bdi);
1218 faea38e7 bellard
}
1219 faea38e7 bellard
1220 45566e9c Christoph Hellwig
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1221 45566e9c Christoph Hellwig
                      int64_t pos, int size)
1222 178e08a5 aliguori
{
1223 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
1224 178e08a5 aliguori
    if (!drv)
1225 178e08a5 aliguori
        return -ENOMEDIUM;
1226 45566e9c Christoph Hellwig
    if (!drv->bdrv_save_vmstate)
1227 178e08a5 aliguori
        return -ENOTSUP;
1228 45566e9c Christoph Hellwig
    return drv->bdrv_save_vmstate(bs, buf, pos, size);
1229 178e08a5 aliguori
}
1230 178e08a5 aliguori
1231 45566e9c Christoph Hellwig
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1232 45566e9c Christoph Hellwig
                      int64_t pos, int size)
1233 178e08a5 aliguori
{
1234 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
1235 178e08a5 aliguori
    if (!drv)
1236 178e08a5 aliguori
        return -ENOMEDIUM;
1237 45566e9c Christoph Hellwig
    if (!drv->bdrv_load_vmstate)
1238 178e08a5 aliguori
        return -ENOTSUP;
1239 45566e9c Christoph Hellwig
    return drv->bdrv_load_vmstate(bs, buf, pos, size);
1240 178e08a5 aliguori
}
1241 178e08a5 aliguori
1242 faea38e7 bellard
/**************************************************************/
1243 faea38e7 bellard
/* handling of snapshots */
1244 faea38e7 bellard
1245 5fafdf24 ths
int bdrv_snapshot_create(BlockDriverState *bs,
1246 faea38e7 bellard
                         QEMUSnapshotInfo *sn_info)
1247 faea38e7 bellard
{
1248 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1249 faea38e7 bellard
    if (!drv)
1250 19cb3738 bellard
        return -ENOMEDIUM;
1251 faea38e7 bellard
    if (!drv->bdrv_snapshot_create)
1252 faea38e7 bellard
        return -ENOTSUP;
1253 faea38e7 bellard
    return drv->bdrv_snapshot_create(bs, sn_info);
1254 faea38e7 bellard
}
1255 faea38e7 bellard
1256 5fafdf24 ths
int bdrv_snapshot_goto(BlockDriverState *bs,
1257 faea38e7 bellard
                       const char *snapshot_id)
1258 faea38e7 bellard
{
1259 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1260 faea38e7 bellard
    if (!drv)
1261 19cb3738 bellard
        return -ENOMEDIUM;
1262 faea38e7 bellard
    if (!drv->bdrv_snapshot_goto)
1263 faea38e7 bellard
        return -ENOTSUP;
1264 faea38e7 bellard
    return drv->bdrv_snapshot_goto(bs, snapshot_id);
1265 faea38e7 bellard
}
1266 faea38e7 bellard
1267 faea38e7 bellard
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1268 faea38e7 bellard
{
1269 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1270 faea38e7 bellard
    if (!drv)
1271 19cb3738 bellard
        return -ENOMEDIUM;
1272 faea38e7 bellard
    if (!drv->bdrv_snapshot_delete)
1273 faea38e7 bellard
        return -ENOTSUP;
1274 faea38e7 bellard
    return drv->bdrv_snapshot_delete(bs, snapshot_id);
1275 faea38e7 bellard
}
1276 faea38e7 bellard
1277 5fafdf24 ths
int bdrv_snapshot_list(BlockDriverState *bs,
1278 faea38e7 bellard
                       QEMUSnapshotInfo **psn_info)
1279 faea38e7 bellard
{
1280 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1281 faea38e7 bellard
    if (!drv)
1282 19cb3738 bellard
        return -ENOMEDIUM;
1283 faea38e7 bellard
    if (!drv->bdrv_snapshot_list)
1284 faea38e7 bellard
        return -ENOTSUP;
1285 faea38e7 bellard
    return drv->bdrv_snapshot_list(bs, psn_info);
1286 faea38e7 bellard
}
1287 faea38e7 bellard
1288 faea38e7 bellard
#define NB_SUFFIXES 4
1289 faea38e7 bellard
1290 faea38e7 bellard
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1291 faea38e7 bellard
{
1292 faea38e7 bellard
    static const char suffixes[NB_SUFFIXES] = "KMGT";
1293 faea38e7 bellard
    int64_t base;
1294 faea38e7 bellard
    int i;
1295 faea38e7 bellard
1296 faea38e7 bellard
    if (size <= 999) {
1297 faea38e7 bellard
        snprintf(buf, buf_size, "%" PRId64, size);
1298 faea38e7 bellard
    } else {
1299 faea38e7 bellard
        base = 1024;
1300 faea38e7 bellard
        for(i = 0; i < NB_SUFFIXES; i++) {
1301 faea38e7 bellard
            if (size < (10 * base)) {
1302 5fafdf24 ths
                snprintf(buf, buf_size, "%0.1f%c",
1303 faea38e7 bellard
                         (double)size / base,
1304 faea38e7 bellard
                         suffixes[i]);
1305 faea38e7 bellard
                break;
1306 faea38e7 bellard
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1307 5fafdf24 ths
                snprintf(buf, buf_size, "%" PRId64 "%c",
1308 faea38e7 bellard
                         ((size + (base >> 1)) / base),
1309 faea38e7 bellard
                         suffixes[i]);
1310 faea38e7 bellard
                break;
1311 faea38e7 bellard
            }
1312 faea38e7 bellard
            base = base * 1024;
1313 faea38e7 bellard
        }
1314 faea38e7 bellard
    }
1315 faea38e7 bellard
    return buf;
1316 faea38e7 bellard
}
1317 faea38e7 bellard
1318 faea38e7 bellard
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1319 faea38e7 bellard
{
1320 faea38e7 bellard
    char buf1[128], date_buf[128], clock_buf[128];
1321 3b9f94e1 bellard
#ifdef _WIN32
1322 3b9f94e1 bellard
    struct tm *ptm;
1323 3b9f94e1 bellard
#else
1324 faea38e7 bellard
    struct tm tm;
1325 3b9f94e1 bellard
#endif
1326 faea38e7 bellard
    time_t ti;
1327 faea38e7 bellard
    int64_t secs;
1328 faea38e7 bellard
1329 faea38e7 bellard
    if (!sn) {
1330 5fafdf24 ths
        snprintf(buf, buf_size,
1331 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
1332 faea38e7 bellard
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1333 faea38e7 bellard
    } else {
1334 faea38e7 bellard
        ti = sn->date_sec;
1335 3b9f94e1 bellard
#ifdef _WIN32
1336 3b9f94e1 bellard
        ptm = localtime(&ti);
1337 3b9f94e1 bellard
        strftime(date_buf, sizeof(date_buf),
1338 3b9f94e1 bellard
                 "%Y-%m-%d %H:%M:%S", ptm);
1339 3b9f94e1 bellard
#else
1340 faea38e7 bellard
        localtime_r(&ti, &tm);
1341 faea38e7 bellard
        strftime(date_buf, sizeof(date_buf),
1342 faea38e7 bellard
                 "%Y-%m-%d %H:%M:%S", &tm);
1343 3b9f94e1 bellard
#endif
1344 faea38e7 bellard
        secs = sn->vm_clock_nsec / 1000000000;
1345 faea38e7 bellard
        snprintf(clock_buf, sizeof(clock_buf),
1346 faea38e7 bellard
                 "%02d:%02d:%02d.%03d",
1347 faea38e7 bellard
                 (int)(secs / 3600),
1348 faea38e7 bellard
                 (int)((secs / 60) % 60),
1349 5fafdf24 ths
                 (int)(secs % 60),
1350 faea38e7 bellard
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1351 faea38e7 bellard
        snprintf(buf, buf_size,
1352 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
1353 faea38e7 bellard
                 sn->id_str, sn->name,
1354 faea38e7 bellard
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1355 faea38e7 bellard
                 date_buf,
1356 faea38e7 bellard
                 clock_buf);
1357 faea38e7 bellard
    }
1358 faea38e7 bellard
    return buf;
1359 faea38e7 bellard
}
1360 faea38e7 bellard
1361 83f64091 bellard
1362 ea2384d3 bellard
/**************************************************************/
1363 83f64091 bellard
/* async I/Os */
1364 ea2384d3 bellard
1365 3b69e4b9 aliguori
BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1366 f141eafe aliguori
                                 QEMUIOVector *qiov, int nb_sectors,
1367 3b69e4b9 aliguori
                                 BlockDriverCompletionFunc *cb, void *opaque)
1368 3b69e4b9 aliguori
{
1369 83f64091 bellard
    BlockDriver *drv = bs->drv;
1370 a36e69dd ths
    BlockDriverAIOCB *ret;
1371 83f64091 bellard
1372 19cb3738 bellard
    if (!drv)
1373 ce1a14dc pbrook
        return NULL;
1374 71d0770c aliguori
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1375 71d0770c aliguori
        return NULL;
1376 3b46e624 ths
1377 f141eafe aliguori
    ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1378 f141eafe aliguori
                              cb, opaque);
1379 a36e69dd ths
1380 a36e69dd ths
    if (ret) {
1381 a36e69dd ths
        /* Update stats even though technically transfer has not happened. */
1382 a36e69dd ths
        bs->rd_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1383 a36e69dd ths
        bs->rd_ops ++;
1384 a36e69dd ths
    }
1385 a36e69dd ths
1386 a36e69dd ths
    return ret;
1387 ea2384d3 bellard
}
1388 ea2384d3 bellard
1389 f141eafe aliguori
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1390 f141eafe aliguori
                                  QEMUIOVector *qiov, int nb_sectors,
1391 f141eafe aliguori
                                  BlockDriverCompletionFunc *cb, void *opaque)
1392 ea2384d3 bellard
{
1393 83f64091 bellard
    BlockDriver *drv = bs->drv;
1394 a36e69dd ths
    BlockDriverAIOCB *ret;
1395 ea2384d3 bellard
1396 19cb3738 bellard
    if (!drv)
1397 ce1a14dc pbrook
        return NULL;
1398 83f64091 bellard
    if (bs->read_only)
1399 ce1a14dc pbrook
        return NULL;
1400 71d0770c aliguori
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1401 71d0770c aliguori
        return NULL;
1402 83f64091 bellard
1403 f141eafe aliguori
    ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1404 f141eafe aliguori
                               cb, opaque);
1405 a36e69dd ths
1406 a36e69dd ths
    if (ret) {
1407 a36e69dd ths
        /* Update stats even though technically transfer has not happened. */
1408 a36e69dd ths
        bs->wr_bytes += (unsigned) nb_sectors * SECTOR_SIZE;
1409 a36e69dd ths
        bs->wr_ops ++;
1410 a36e69dd ths
    }
1411 a36e69dd ths
1412 a36e69dd ths
    return ret;
1413 83f64091 bellard
}
1414 83f64091 bellard
1415 40b4f539 Kevin Wolf
1416 40b4f539 Kevin Wolf
typedef struct MultiwriteCB {
1417 40b4f539 Kevin Wolf
    int error;
1418 40b4f539 Kevin Wolf
    int num_requests;
1419 40b4f539 Kevin Wolf
    int num_callbacks;
1420 40b4f539 Kevin Wolf
    struct {
1421 40b4f539 Kevin Wolf
        BlockDriverCompletionFunc *cb;
1422 40b4f539 Kevin Wolf
        void *opaque;
1423 40b4f539 Kevin Wolf
        QEMUIOVector *free_qiov;
1424 40b4f539 Kevin Wolf
        void *free_buf;
1425 40b4f539 Kevin Wolf
    } callbacks[];
1426 40b4f539 Kevin Wolf
} MultiwriteCB;
1427 40b4f539 Kevin Wolf
1428 40b4f539 Kevin Wolf
static void multiwrite_user_cb(MultiwriteCB *mcb)
1429 40b4f539 Kevin Wolf
{
1430 40b4f539 Kevin Wolf
    int i;
1431 40b4f539 Kevin Wolf
1432 40b4f539 Kevin Wolf
    for (i = 0; i < mcb->num_callbacks; i++) {
1433 40b4f539 Kevin Wolf
        mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1434 40b4f539 Kevin Wolf
        qemu_free(mcb->callbacks[i].free_qiov);
1435 40b4f539 Kevin Wolf
        qemu_free(mcb->callbacks[i].free_buf);
1436 40b4f539 Kevin Wolf
    }
1437 40b4f539 Kevin Wolf
}
1438 40b4f539 Kevin Wolf
1439 40b4f539 Kevin Wolf
static void multiwrite_cb(void *opaque, int ret)
1440 40b4f539 Kevin Wolf
{
1441 40b4f539 Kevin Wolf
    MultiwriteCB *mcb = opaque;
1442 40b4f539 Kevin Wolf
1443 40b4f539 Kevin Wolf
    if (ret < 0) {
1444 40b4f539 Kevin Wolf
        mcb->error = ret;
1445 40b4f539 Kevin Wolf
        multiwrite_user_cb(mcb);
1446 40b4f539 Kevin Wolf
    }
1447 40b4f539 Kevin Wolf
1448 40b4f539 Kevin Wolf
    mcb->num_requests--;
1449 40b4f539 Kevin Wolf
    if (mcb->num_requests == 0) {
1450 40b4f539 Kevin Wolf
        if (mcb->error == 0) {
1451 40b4f539 Kevin Wolf
            multiwrite_user_cb(mcb);
1452 40b4f539 Kevin Wolf
        }
1453 40b4f539 Kevin Wolf
        qemu_free(mcb);
1454 40b4f539 Kevin Wolf
    }
1455 40b4f539 Kevin Wolf
}
1456 40b4f539 Kevin Wolf
1457 40b4f539 Kevin Wolf
static int multiwrite_req_compare(const void *a, const void *b)
1458 40b4f539 Kevin Wolf
{
1459 40b4f539 Kevin Wolf
    return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
1460 40b4f539 Kevin Wolf
}
1461 40b4f539 Kevin Wolf
1462 40b4f539 Kevin Wolf
/*
1463 40b4f539 Kevin Wolf
 * Takes a bunch of requests and tries to merge them. Returns the number of
1464 40b4f539 Kevin Wolf
 * requests that remain after merging.
1465 40b4f539 Kevin Wolf
 */
1466 40b4f539 Kevin Wolf
static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1467 40b4f539 Kevin Wolf
    int num_reqs, MultiwriteCB *mcb)
1468 40b4f539 Kevin Wolf
{
1469 40b4f539 Kevin Wolf
    int i, outidx;
1470 40b4f539 Kevin Wolf
1471 40b4f539 Kevin Wolf
    // Sort requests by start sector
1472 40b4f539 Kevin Wolf
    qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1473 40b4f539 Kevin Wolf
1474 40b4f539 Kevin Wolf
    // Check if adjacent requests touch the same clusters. If so, combine them,
1475 40b4f539 Kevin Wolf
    // filling up gaps with zero sectors.
1476 40b4f539 Kevin Wolf
    outidx = 0;
1477 40b4f539 Kevin Wolf
    for (i = 1; i < num_reqs; i++) {
1478 40b4f539 Kevin Wolf
        int merge = 0;
1479 40b4f539 Kevin Wolf
        int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1480 40b4f539 Kevin Wolf
1481 40b4f539 Kevin Wolf
        // This handles the cases that are valid for all block drivers, namely
1482 40b4f539 Kevin Wolf
        // exactly sequential writes and overlapping writes.
1483 40b4f539 Kevin Wolf
        if (reqs[i].sector <= oldreq_last) {
1484 40b4f539 Kevin Wolf
            merge = 1;
1485 40b4f539 Kevin Wolf
        }
1486 40b4f539 Kevin Wolf
1487 40b4f539 Kevin Wolf
        // The block driver may decide that it makes sense to combine requests
1488 40b4f539 Kevin Wolf
        // even if there is a gap of some sectors between them. In this case,
1489 40b4f539 Kevin Wolf
        // the gap is filled with zeros (therefore only applicable for yet
1490 40b4f539 Kevin Wolf
        // unused space in format like qcow2).
1491 40b4f539 Kevin Wolf
        if (!merge && bs->drv->bdrv_merge_requests) {
1492 40b4f539 Kevin Wolf
            merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1493 40b4f539 Kevin Wolf
        }
1494 40b4f539 Kevin Wolf
1495 40b4f539 Kevin Wolf
        if (merge) {
1496 40b4f539 Kevin Wolf
            size_t size;
1497 40b4f539 Kevin Wolf
            QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1498 40b4f539 Kevin Wolf
            qemu_iovec_init(qiov,
1499 40b4f539 Kevin Wolf
                reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1500 40b4f539 Kevin Wolf
1501 40b4f539 Kevin Wolf
            // Add the first request to the merged one. If the requests are
1502 40b4f539 Kevin Wolf
            // overlapping, drop the last sectors of the first request.
1503 40b4f539 Kevin Wolf
            size = (reqs[i].sector - reqs[outidx].sector) << 9;
1504 40b4f539 Kevin Wolf
            qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1505 40b4f539 Kevin Wolf
1506 40b4f539 Kevin Wolf
            // We might need to add some zeros between the two requests
1507 40b4f539 Kevin Wolf
            if (reqs[i].sector > oldreq_last) {
1508 40b4f539 Kevin Wolf
                size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1509 40b4f539 Kevin Wolf
                uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1510 40b4f539 Kevin Wolf
                memset(buf, 0, zero_bytes);
1511 40b4f539 Kevin Wolf
                qemu_iovec_add(qiov, buf, zero_bytes);
1512 40b4f539 Kevin Wolf
                mcb->callbacks[i].free_buf = buf;
1513 40b4f539 Kevin Wolf
            }
1514 40b4f539 Kevin Wolf
1515 40b4f539 Kevin Wolf
            // Add the second request
1516 40b4f539 Kevin Wolf
            qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1517 40b4f539 Kevin Wolf
1518 40b4f539 Kevin Wolf
            reqs[outidx].nb_sectors += reqs[i].nb_sectors;
1519 40b4f539 Kevin Wolf
            reqs[outidx].qiov = qiov;
1520 40b4f539 Kevin Wolf
1521 40b4f539 Kevin Wolf
            mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1522 40b4f539 Kevin Wolf
        } else {
1523 40b4f539 Kevin Wolf
            outidx++;
1524 40b4f539 Kevin Wolf
            reqs[outidx].sector     = reqs[i].sector;
1525 40b4f539 Kevin Wolf
            reqs[outidx].nb_sectors = reqs[i].nb_sectors;
1526 40b4f539 Kevin Wolf
            reqs[outidx].qiov       = reqs[i].qiov;
1527 40b4f539 Kevin Wolf
        }
1528 40b4f539 Kevin Wolf
    }
1529 40b4f539 Kevin Wolf
1530 40b4f539 Kevin Wolf
    return outidx + 1;
1531 40b4f539 Kevin Wolf
}
1532 40b4f539 Kevin Wolf
1533 40b4f539 Kevin Wolf
/*
1534 40b4f539 Kevin Wolf
 * Submit multiple AIO write requests at once.
1535 40b4f539 Kevin Wolf
 *
1536 40b4f539 Kevin Wolf
 * On success, the function returns 0 and all requests in the reqs array have
1537 40b4f539 Kevin Wolf
 * been submitted. In error case this function returns -1, and any of the
1538 40b4f539 Kevin Wolf
 * requests may or may not be submitted yet. In particular, this means that the
1539 40b4f539 Kevin Wolf
 * callback will be called for some of the requests, for others it won't. The
1540 40b4f539 Kevin Wolf
 * caller must check the error field of the BlockRequest to wait for the right
1541 40b4f539 Kevin Wolf
 * callbacks (if error != 0, no callback will be called).
1542 40b4f539 Kevin Wolf
 *
1543 40b4f539 Kevin Wolf
 * The implementation may modify the contents of the reqs array, e.g. to merge
1544 40b4f539 Kevin Wolf
 * requests. However, the fields opaque and error are left unmodified as they
1545 40b4f539 Kevin Wolf
 * are used to signal failure for a single request to the caller.
1546 40b4f539 Kevin Wolf
 */
1547 40b4f539 Kevin Wolf
int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
1548 40b4f539 Kevin Wolf
{
1549 40b4f539 Kevin Wolf
    BlockDriverAIOCB *acb;
1550 40b4f539 Kevin Wolf
    MultiwriteCB *mcb;
1551 40b4f539 Kevin Wolf
    int i;
1552 40b4f539 Kevin Wolf
1553 40b4f539 Kevin Wolf
    if (num_reqs == 0) {
1554 40b4f539 Kevin Wolf
        return 0;
1555 40b4f539 Kevin Wolf
    }
1556 40b4f539 Kevin Wolf
1557 40b4f539 Kevin Wolf
    // Create MultiwriteCB structure
1558 40b4f539 Kevin Wolf
    mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
1559 40b4f539 Kevin Wolf
    mcb->num_requests = 0;
1560 40b4f539 Kevin Wolf
    mcb->num_callbacks = num_reqs;
1561 40b4f539 Kevin Wolf
1562 40b4f539 Kevin Wolf
    for (i = 0; i < num_reqs; i++) {
1563 40b4f539 Kevin Wolf
        mcb->callbacks[i].cb = reqs[i].cb;
1564 40b4f539 Kevin Wolf
        mcb->callbacks[i].opaque = reqs[i].opaque;
1565 40b4f539 Kevin Wolf
    }
1566 40b4f539 Kevin Wolf
1567 40b4f539 Kevin Wolf
    // Check for mergable requests
1568 40b4f539 Kevin Wolf
    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
1569 40b4f539 Kevin Wolf
1570 40b4f539 Kevin Wolf
    // Run the aio requests
1571 40b4f539 Kevin Wolf
    for (i = 0; i < num_reqs; i++) {
1572 40b4f539 Kevin Wolf
        acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
1573 40b4f539 Kevin Wolf
            reqs[i].nb_sectors, multiwrite_cb, mcb);
1574 40b4f539 Kevin Wolf
1575 40b4f539 Kevin Wolf
        if (acb == NULL) {
1576 40b4f539 Kevin Wolf
            // We can only fail the whole thing if no request has been
1577 40b4f539 Kevin Wolf
            // submitted yet. Otherwise we'll wait for the submitted AIOs to
1578 40b4f539 Kevin Wolf
            // complete and report the error in the callback.
1579 40b4f539 Kevin Wolf
            if (mcb->num_requests == 0) {
1580 40b4f539 Kevin Wolf
                reqs[i].error = EIO;
1581 40b4f539 Kevin Wolf
                goto fail;
1582 40b4f539 Kevin Wolf
            } else {
1583 40b4f539 Kevin Wolf
                mcb->error = EIO;
1584 40b4f539 Kevin Wolf
                break;
1585 40b4f539 Kevin Wolf
            }
1586 40b4f539 Kevin Wolf
        } else {
1587 40b4f539 Kevin Wolf
            mcb->num_requests++;
1588 40b4f539 Kevin Wolf
        }
1589 40b4f539 Kevin Wolf
    }
1590 40b4f539 Kevin Wolf
1591 40b4f539 Kevin Wolf
    return 0;
1592 40b4f539 Kevin Wolf
1593 40b4f539 Kevin Wolf
fail:
1594 40b4f539 Kevin Wolf
    free(mcb);
1595 40b4f539 Kevin Wolf
    return -1;
1596 40b4f539 Kevin Wolf
}
1597 40b4f539 Kevin Wolf
1598 b2e12bc6 Christoph Hellwig
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
1599 b2e12bc6 Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque)
1600 b2e12bc6 Christoph Hellwig
{
1601 b2e12bc6 Christoph Hellwig
    BlockDriver *drv = bs->drv;
1602 b2e12bc6 Christoph Hellwig
1603 b2e12bc6 Christoph Hellwig
    if (!drv)
1604 b2e12bc6 Christoph Hellwig
        return NULL;
1605 b2e12bc6 Christoph Hellwig
1606 b2e12bc6 Christoph Hellwig
    /*
1607 b2e12bc6 Christoph Hellwig
     * Note that unlike bdrv_flush the driver is reponsible for flushing a
1608 b2e12bc6 Christoph Hellwig
     * backing image if it exists.
1609 b2e12bc6 Christoph Hellwig
     */
1610 b2e12bc6 Christoph Hellwig
    return drv->bdrv_aio_flush(bs, cb, opaque);
1611 b2e12bc6 Christoph Hellwig
}
1612 b2e12bc6 Christoph Hellwig
1613 83f64091 bellard
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
1614 83f64091 bellard
{
1615 6bbff9a0 aliguori
    acb->pool->cancel(acb);
1616 83f64091 bellard
}
1617 83f64091 bellard
1618 ce1a14dc pbrook
1619 83f64091 bellard
/**************************************************************/
1620 83f64091 bellard
/* async block device emulation */
1621 83f64091 bellard
1622 c16b5a2c Christoph Hellwig
typedef struct BlockDriverAIOCBSync {
1623 c16b5a2c Christoph Hellwig
    BlockDriverAIOCB common;
1624 c16b5a2c Christoph Hellwig
    QEMUBH *bh;
1625 c16b5a2c Christoph Hellwig
    int ret;
1626 c16b5a2c Christoph Hellwig
    /* vector translation state */
1627 c16b5a2c Christoph Hellwig
    QEMUIOVector *qiov;
1628 c16b5a2c Christoph Hellwig
    uint8_t *bounce;
1629 c16b5a2c Christoph Hellwig
    int is_write;
1630 c16b5a2c Christoph Hellwig
} BlockDriverAIOCBSync;
1631 c16b5a2c Christoph Hellwig
1632 c16b5a2c Christoph Hellwig
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
1633 c16b5a2c Christoph Hellwig
{
1634 c16b5a2c Christoph Hellwig
    BlockDriverAIOCBSync *acb = (BlockDriverAIOCBSync *)blockacb;
1635 6a7ad299 Dor Laor
    qemu_bh_delete(acb->bh);
1636 36afc451 Avi Kivity
    acb->bh = NULL;
1637 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
1638 c16b5a2c Christoph Hellwig
}
1639 c16b5a2c Christoph Hellwig
1640 c16b5a2c Christoph Hellwig
static AIOPool bdrv_em_aio_pool = {
1641 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(BlockDriverAIOCBSync),
1642 c16b5a2c Christoph Hellwig
    .cancel             = bdrv_aio_cancel_em,
1643 c16b5a2c Christoph Hellwig
};
1644 c16b5a2c Christoph Hellwig
1645 ce1a14dc pbrook
static void bdrv_aio_bh_cb(void *opaque)
1646 83f64091 bellard
{
1647 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb = opaque;
1648 f141eafe aliguori
1649 f141eafe aliguori
    if (!acb->is_write)
1650 f141eafe aliguori
        qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
1651 ceb42de8 aliguori
    qemu_vfree(acb->bounce);
1652 ce1a14dc pbrook
    acb->common.cb(acb->common.opaque, acb->ret);
1653 6a7ad299 Dor Laor
    qemu_bh_delete(acb->bh);
1654 36afc451 Avi Kivity
    acb->bh = NULL;
1655 ce1a14dc pbrook
    qemu_aio_release(acb);
1656 83f64091 bellard
}
1657 beac80cd bellard
1658 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
1659 f141eafe aliguori
                                            int64_t sector_num,
1660 f141eafe aliguori
                                            QEMUIOVector *qiov,
1661 f141eafe aliguori
                                            int nb_sectors,
1662 f141eafe aliguori
                                            BlockDriverCompletionFunc *cb,
1663 f141eafe aliguori
                                            void *opaque,
1664 f141eafe aliguori
                                            int is_write)
1665 f141eafe aliguori
1666 83f64091 bellard
{
1667 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb;
1668 ce1a14dc pbrook
1669 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1670 f141eafe aliguori
    acb->is_write = is_write;
1671 f141eafe aliguori
    acb->qiov = qiov;
1672 e268ca52 aliguori
    acb->bounce = qemu_blockalign(bs, qiov->size);
1673 f141eafe aliguori
1674 ce1a14dc pbrook
    if (!acb->bh)
1675 ce1a14dc pbrook
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1676 f141eafe aliguori
1677 f141eafe aliguori
    if (is_write) {
1678 f141eafe aliguori
        qemu_iovec_to_buffer(acb->qiov, acb->bounce);
1679 f141eafe aliguori
        acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
1680 f141eafe aliguori
    } else {
1681 f141eafe aliguori
        acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
1682 f141eafe aliguori
    }
1683 f141eafe aliguori
1684 ce1a14dc pbrook
    qemu_bh_schedule(acb->bh);
1685 f141eafe aliguori
1686 ce1a14dc pbrook
    return &acb->common;
1687 beac80cd bellard
}
1688 beac80cd bellard
1689 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
1690 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1691 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
1692 beac80cd bellard
{
1693 f141eafe aliguori
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
1694 f141eafe aliguori
}
1695 83f64091 bellard
1696 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
1697 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
1698 f141eafe aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
1699 f141eafe aliguori
{
1700 f141eafe aliguori
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
1701 beac80cd bellard
}
1702 beac80cd bellard
1703 b2e12bc6 Christoph Hellwig
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
1704 b2e12bc6 Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque)
1705 b2e12bc6 Christoph Hellwig
{
1706 b2e12bc6 Christoph Hellwig
    BlockDriverAIOCBSync *acb;
1707 b2e12bc6 Christoph Hellwig
1708 b2e12bc6 Christoph Hellwig
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
1709 b2e12bc6 Christoph Hellwig
    acb->is_write = 1; /* don't bounce in the completion hadler */
1710 b2e12bc6 Christoph Hellwig
    acb->qiov = NULL;
1711 b2e12bc6 Christoph Hellwig
    acb->bounce = NULL;
1712 b2e12bc6 Christoph Hellwig
    acb->ret = 0;
1713 b2e12bc6 Christoph Hellwig
1714 b2e12bc6 Christoph Hellwig
    if (!acb->bh)
1715 b2e12bc6 Christoph Hellwig
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
1716 b2e12bc6 Christoph Hellwig
1717 b2e12bc6 Christoph Hellwig
    bdrv_flush(bs);
1718 b2e12bc6 Christoph Hellwig
    qemu_bh_schedule(acb->bh);
1719 b2e12bc6 Christoph Hellwig
    return &acb->common;
1720 b2e12bc6 Christoph Hellwig
}
1721 b2e12bc6 Christoph Hellwig
1722 83f64091 bellard
/**************************************************************/
1723 83f64091 bellard
/* sync block device emulation */
1724 ea2384d3 bellard
1725 83f64091 bellard
static void bdrv_rw_em_cb(void *opaque, int ret)
1726 83f64091 bellard
{
1727 83f64091 bellard
    *(int *)opaque = ret;
1728 ea2384d3 bellard
}
1729 ea2384d3 bellard
1730 83f64091 bellard
#define NOT_DONE 0x7fffffff
1731 83f64091 bellard
1732 5fafdf24 ths
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
1733 83f64091 bellard
                        uint8_t *buf, int nb_sectors)
1734 7a6cba61 pbrook
{
1735 ce1a14dc pbrook
    int async_ret;
1736 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1737 f141eafe aliguori
    struct iovec iov;
1738 f141eafe aliguori
    QEMUIOVector qiov;
1739 83f64091 bellard
1740 65d6b3d8 Kevin Wolf
    async_context_push();
1741 65d6b3d8 Kevin Wolf
1742 83f64091 bellard
    async_ret = NOT_DONE;
1743 3f4cb3d3 blueswir1
    iov.iov_base = (void *)buf;
1744 f141eafe aliguori
    iov.iov_len = nb_sectors * 512;
1745 f141eafe aliguori
    qemu_iovec_init_external(&qiov, &iov, 1);
1746 f141eafe aliguori
    acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
1747 f141eafe aliguori
        bdrv_rw_em_cb, &async_ret);
1748 65d6b3d8 Kevin Wolf
    if (acb == NULL) {
1749 65d6b3d8 Kevin Wolf
        async_ret = -1;
1750 65d6b3d8 Kevin Wolf
        goto fail;
1751 65d6b3d8 Kevin Wolf
    }
1752 baf35cb9 aliguori
1753 83f64091 bellard
    while (async_ret == NOT_DONE) {
1754 83f64091 bellard
        qemu_aio_wait();
1755 83f64091 bellard
    }
1756 baf35cb9 aliguori
1757 65d6b3d8 Kevin Wolf
1758 65d6b3d8 Kevin Wolf
fail:
1759 65d6b3d8 Kevin Wolf
    async_context_pop();
1760 83f64091 bellard
    return async_ret;
1761 7a6cba61 pbrook
}
1762 7a6cba61 pbrook
1763 83f64091 bellard
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
1764 83f64091 bellard
                         const uint8_t *buf, int nb_sectors)
1765 83f64091 bellard
{
1766 ce1a14dc pbrook
    int async_ret;
1767 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1768 f141eafe aliguori
    struct iovec iov;
1769 f141eafe aliguori
    QEMUIOVector qiov;
1770 83f64091 bellard
1771 65d6b3d8 Kevin Wolf
    async_context_push();
1772 65d6b3d8 Kevin Wolf
1773 83f64091 bellard
    async_ret = NOT_DONE;
1774 f141eafe aliguori
    iov.iov_base = (void *)buf;
1775 f141eafe aliguori
    iov.iov_len = nb_sectors * 512;
1776 f141eafe aliguori
    qemu_iovec_init_external(&qiov, &iov, 1);
1777 f141eafe aliguori
    acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
1778 f141eafe aliguori
        bdrv_rw_em_cb, &async_ret);
1779 65d6b3d8 Kevin Wolf
    if (acb == NULL) {
1780 65d6b3d8 Kevin Wolf
        async_ret = -1;
1781 65d6b3d8 Kevin Wolf
        goto fail;
1782 65d6b3d8 Kevin Wolf
    }
1783 83f64091 bellard
    while (async_ret == NOT_DONE) {
1784 83f64091 bellard
        qemu_aio_wait();
1785 83f64091 bellard
    }
1786 65d6b3d8 Kevin Wolf
1787 65d6b3d8 Kevin Wolf
fail:
1788 65d6b3d8 Kevin Wolf
    async_context_pop();
1789 83f64091 bellard
    return async_ret;
1790 83f64091 bellard
}
1791 ea2384d3 bellard
1792 ea2384d3 bellard
void bdrv_init(void)
1793 ea2384d3 bellard
{
1794 5efa9d5a Anthony Liguori
    module_call_init(MODULE_INIT_BLOCK);
1795 ea2384d3 bellard
}
1796 ce1a14dc pbrook
1797 eb852011 Markus Armbruster
void bdrv_init_with_whitelist(void)
1798 eb852011 Markus Armbruster
{
1799 eb852011 Markus Armbruster
    use_bdrv_whitelist = 1;
1800 eb852011 Markus Armbruster
    bdrv_init();
1801 eb852011 Markus Armbruster
}
1802 eb852011 Markus Armbruster
1803 c16b5a2c Christoph Hellwig
void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
1804 c16b5a2c Christoph Hellwig
                   BlockDriverCompletionFunc *cb, void *opaque)
1805 ce1a14dc pbrook
{
1806 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
1807 ce1a14dc pbrook
1808 6bbff9a0 aliguori
    if (pool->free_aiocb) {
1809 6bbff9a0 aliguori
        acb = pool->free_aiocb;
1810 6bbff9a0 aliguori
        pool->free_aiocb = acb->next;
1811 ce1a14dc pbrook
    } else {
1812 6bbff9a0 aliguori
        acb = qemu_mallocz(pool->aiocb_size);
1813 6bbff9a0 aliguori
        acb->pool = pool;
1814 ce1a14dc pbrook
    }
1815 ce1a14dc pbrook
    acb->bs = bs;
1816 ce1a14dc pbrook
    acb->cb = cb;
1817 ce1a14dc pbrook
    acb->opaque = opaque;
1818 ce1a14dc pbrook
    return acb;
1819 ce1a14dc pbrook
}
1820 ce1a14dc pbrook
1821 ce1a14dc pbrook
void qemu_aio_release(void *p)
1822 ce1a14dc pbrook
{
1823 6bbff9a0 aliguori
    BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
1824 6bbff9a0 aliguori
    AIOPool *pool = acb->pool;
1825 6bbff9a0 aliguori
    acb->next = pool->free_aiocb;
1826 6bbff9a0 aliguori
    pool->free_aiocb = acb;
1827 ce1a14dc pbrook
}
1828 19cb3738 bellard
1829 19cb3738 bellard
/**************************************************************/
1830 19cb3738 bellard
/* removable device support */
1831 19cb3738 bellard
1832 19cb3738 bellard
/**
1833 19cb3738 bellard
 * Return TRUE if the media is present
1834 19cb3738 bellard
 */
1835 19cb3738 bellard
int bdrv_is_inserted(BlockDriverState *bs)
1836 19cb3738 bellard
{
1837 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1838 19cb3738 bellard
    int ret;
1839 19cb3738 bellard
    if (!drv)
1840 19cb3738 bellard
        return 0;
1841 19cb3738 bellard
    if (!drv->bdrv_is_inserted)
1842 19cb3738 bellard
        return 1;
1843 19cb3738 bellard
    ret = drv->bdrv_is_inserted(bs);
1844 19cb3738 bellard
    return ret;
1845 19cb3738 bellard
}
1846 19cb3738 bellard
1847 19cb3738 bellard
/**
1848 19cb3738 bellard
 * Return TRUE if the media changed since the last call to this
1849 5fafdf24 ths
 * function. It is currently only used for floppy disks
1850 19cb3738 bellard
 */
1851 19cb3738 bellard
int bdrv_media_changed(BlockDriverState *bs)
1852 19cb3738 bellard
{
1853 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1854 19cb3738 bellard
    int ret;
1855 19cb3738 bellard
1856 19cb3738 bellard
    if (!drv || !drv->bdrv_media_changed)
1857 19cb3738 bellard
        ret = -ENOTSUP;
1858 19cb3738 bellard
    else
1859 19cb3738 bellard
        ret = drv->bdrv_media_changed(bs);
1860 19cb3738 bellard
    if (ret == -ENOTSUP)
1861 19cb3738 bellard
        ret = bs->media_changed;
1862 19cb3738 bellard
    bs->media_changed = 0;
1863 19cb3738 bellard
    return ret;
1864 19cb3738 bellard
}
1865 19cb3738 bellard
1866 19cb3738 bellard
/**
1867 19cb3738 bellard
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
1868 19cb3738 bellard
 */
1869 aea2a33c Mark McLoughlin
int bdrv_eject(BlockDriverState *bs, int eject_flag)
1870 19cb3738 bellard
{
1871 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1872 19cb3738 bellard
    int ret;
1873 19cb3738 bellard
1874 aea2a33c Mark McLoughlin
    if (bs->locked) {
1875 aea2a33c Mark McLoughlin
        return -EBUSY;
1876 aea2a33c Mark McLoughlin
    }
1877 aea2a33c Mark McLoughlin
1878 19cb3738 bellard
    if (!drv || !drv->bdrv_eject) {
1879 19cb3738 bellard
        ret = -ENOTSUP;
1880 19cb3738 bellard
    } else {
1881 19cb3738 bellard
        ret = drv->bdrv_eject(bs, eject_flag);
1882 19cb3738 bellard
    }
1883 19cb3738 bellard
    if (ret == -ENOTSUP) {
1884 19cb3738 bellard
        if (eject_flag)
1885 19cb3738 bellard
            bdrv_close(bs);
1886 aea2a33c Mark McLoughlin
        ret = 0;
1887 19cb3738 bellard
    }
1888 aea2a33c Mark McLoughlin
1889 aea2a33c Mark McLoughlin
    return ret;
1890 19cb3738 bellard
}
1891 19cb3738 bellard
1892 19cb3738 bellard
int bdrv_is_locked(BlockDriverState *bs)
1893 19cb3738 bellard
{
1894 19cb3738 bellard
    return bs->locked;
1895 19cb3738 bellard
}
1896 19cb3738 bellard
1897 19cb3738 bellard
/**
1898 19cb3738 bellard
 * Lock or unlock the media (if it is locked, the user won't be able
1899 19cb3738 bellard
 * to eject it manually).
1900 19cb3738 bellard
 */
1901 19cb3738 bellard
void bdrv_set_locked(BlockDriverState *bs, int locked)
1902 19cb3738 bellard
{
1903 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1904 19cb3738 bellard
1905 19cb3738 bellard
    bs->locked = locked;
1906 19cb3738 bellard
    if (drv && drv->bdrv_set_locked) {
1907 19cb3738 bellard
        drv->bdrv_set_locked(bs, locked);
1908 19cb3738 bellard
    }
1909 19cb3738 bellard
}
1910 985a03b0 ths
1911 985a03b0 ths
/* needed for generic scsi interface */
1912 985a03b0 ths
1913 985a03b0 ths
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
1914 985a03b0 ths
{
1915 985a03b0 ths
    BlockDriver *drv = bs->drv;
1916 985a03b0 ths
1917 985a03b0 ths
    if (drv && drv->bdrv_ioctl)
1918 985a03b0 ths
        return drv->bdrv_ioctl(bs, req, buf);
1919 985a03b0 ths
    return -ENOTSUP;
1920 985a03b0 ths
}
1921 7d780669 aliguori
1922 221f715d aliguori
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
1923 221f715d aliguori
        unsigned long int req, void *buf,
1924 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
1925 7d780669 aliguori
{
1926 221f715d aliguori
    BlockDriver *drv = bs->drv;
1927 7d780669 aliguori
1928 221f715d aliguori
    if (drv && drv->bdrv_aio_ioctl)
1929 221f715d aliguori
        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
1930 221f715d aliguori
    return NULL;
1931 7d780669 aliguori
}
1932 e268ca52 aliguori
1933 e268ca52 aliguori
void *qemu_blockalign(BlockDriverState *bs, size_t size)
1934 e268ca52 aliguori
{
1935 e268ca52 aliguori
    return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
1936 e268ca52 aliguori
}