Statistics
| Branch: | Revision:

root / block.c @ b3e5e3e6

History | View | Annotate | Download (65.8 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 d15e5465 Luiz Capitulino
#include "qemu-objects.h"
30 fc01f7e7 bellard
31 71e72a19 Juan Quintela
#ifdef CONFIG_BSD
32 7674e7bf bellard
#include <sys/types.h>
33 7674e7bf bellard
#include <sys/stat.h>
34 7674e7bf bellard
#include <sys/ioctl.h>
35 72cf2d4f Blue Swirl
#include <sys/queue.h>
36 c5e97233 blueswir1
#ifndef __DragonFly__
37 7674e7bf bellard
#include <sys/disk.h>
38 7674e7bf bellard
#endif
39 c5e97233 blueswir1
#endif
40 7674e7bf bellard
41 49dc768d aliguori
#ifdef _WIN32
42 49dc768d aliguori
#include <windows.h>
43 49dc768d aliguori
#endif
44 49dc768d aliguori
45 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
46 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
47 c87c0672 aliguori
        BlockDriverCompletionFunc *cb, void *opaque);
48 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
49 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
50 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
51 b2e12bc6 Christoph Hellwig
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
52 b2e12bc6 Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque);
53 5fafdf24 ths
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
54 83f64091 bellard
                        uint8_t *buf, int nb_sectors);
55 83f64091 bellard
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
56 83f64091 bellard
                         const uint8_t *buf, int nb_sectors);
57 84a12e66 Christoph Hellwig
static BlockDriver *find_protocol(const char *filename);
58 ec530c81 bellard
59 1b7bdbc1 Stefan Hajnoczi
static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
60 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_HEAD_INITIALIZER(bdrv_states);
61 7ee930d0 blueswir1
62 8a22f02a Stefan Hajnoczi
static QLIST_HEAD(, BlockDriver) bdrv_drivers =
63 8a22f02a Stefan Hajnoczi
    QLIST_HEAD_INITIALIZER(bdrv_drivers);
64 ea2384d3 bellard
65 eb852011 Markus Armbruster
/* If non-zero, use only whitelisted block drivers */
66 eb852011 Markus Armbruster
static int use_bdrv_whitelist;
67 eb852011 Markus Armbruster
68 83f64091 bellard
int path_is_absolute(const char *path)
69 3b0d4f61 bellard
{
70 83f64091 bellard
    const char *p;
71 21664424 bellard
#ifdef _WIN32
72 21664424 bellard
    /* specific case for names like: "\\.\d:" */
73 21664424 bellard
    if (*path == '/' || *path == '\\')
74 21664424 bellard
        return 1;
75 21664424 bellard
#endif
76 83f64091 bellard
    p = strchr(path, ':');
77 83f64091 bellard
    if (p)
78 83f64091 bellard
        p++;
79 83f64091 bellard
    else
80 83f64091 bellard
        p = path;
81 3b9f94e1 bellard
#ifdef _WIN32
82 3b9f94e1 bellard
    return (*p == '/' || *p == '\\');
83 3b9f94e1 bellard
#else
84 3b9f94e1 bellard
    return (*p == '/');
85 3b9f94e1 bellard
#endif
86 3b0d4f61 bellard
}
87 3b0d4f61 bellard
88 83f64091 bellard
/* if filename is absolute, just copy it to dest. Otherwise, build a
89 83f64091 bellard
   path to it by considering it is relative to base_path. URL are
90 83f64091 bellard
   supported. */
91 83f64091 bellard
void path_combine(char *dest, int dest_size,
92 83f64091 bellard
                  const char *base_path,
93 83f64091 bellard
                  const char *filename)
94 3b0d4f61 bellard
{
95 83f64091 bellard
    const char *p, *p1;
96 83f64091 bellard
    int len;
97 83f64091 bellard
98 83f64091 bellard
    if (dest_size <= 0)
99 83f64091 bellard
        return;
100 83f64091 bellard
    if (path_is_absolute(filename)) {
101 83f64091 bellard
        pstrcpy(dest, dest_size, filename);
102 83f64091 bellard
    } else {
103 83f64091 bellard
        p = strchr(base_path, ':');
104 83f64091 bellard
        if (p)
105 83f64091 bellard
            p++;
106 83f64091 bellard
        else
107 83f64091 bellard
            p = base_path;
108 3b9f94e1 bellard
        p1 = strrchr(base_path, '/');
109 3b9f94e1 bellard
#ifdef _WIN32
110 3b9f94e1 bellard
        {
111 3b9f94e1 bellard
            const char *p2;
112 3b9f94e1 bellard
            p2 = strrchr(base_path, '\\');
113 3b9f94e1 bellard
            if (!p1 || p2 > p1)
114 3b9f94e1 bellard
                p1 = p2;
115 3b9f94e1 bellard
        }
116 3b9f94e1 bellard
#endif
117 83f64091 bellard
        if (p1)
118 83f64091 bellard
            p1++;
119 83f64091 bellard
        else
120 83f64091 bellard
            p1 = base_path;
121 83f64091 bellard
        if (p1 > p)
122 83f64091 bellard
            p = p1;
123 83f64091 bellard
        len = p - base_path;
124 83f64091 bellard
        if (len > dest_size - 1)
125 83f64091 bellard
            len = dest_size - 1;
126 83f64091 bellard
        memcpy(dest, base_path, len);
127 83f64091 bellard
        dest[len] = '\0';
128 83f64091 bellard
        pstrcat(dest, dest_size, filename);
129 3b0d4f61 bellard
    }
130 3b0d4f61 bellard
}
131 3b0d4f61 bellard
132 5efa9d5a Anthony Liguori
void bdrv_register(BlockDriver *bdrv)
133 ea2384d3 bellard
{
134 f141eafe aliguori
    if (!bdrv->bdrv_aio_readv) {
135 83f64091 bellard
        /* add AIO emulation layer */
136 f141eafe aliguori
        bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
137 f141eafe aliguori
        bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
138 eda578e5 aliguori
    } else if (!bdrv->bdrv_read) {
139 83f64091 bellard
        /* add synchronous IO emulation layer */
140 83f64091 bellard
        bdrv->bdrv_read = bdrv_read_em;
141 83f64091 bellard
        bdrv->bdrv_write = bdrv_write_em;
142 83f64091 bellard
    }
143 b2e12bc6 Christoph Hellwig
144 b2e12bc6 Christoph Hellwig
    if (!bdrv->bdrv_aio_flush)
145 b2e12bc6 Christoph Hellwig
        bdrv->bdrv_aio_flush = bdrv_aio_flush_em;
146 b2e12bc6 Christoph Hellwig
147 8a22f02a Stefan Hajnoczi
    QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
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 1b7bdbc1 Stefan Hajnoczi
    BlockDriverState *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 1b7bdbc1 Stefan Hajnoczi
        QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
159 ea2384d3 bellard
    }
160 b338082b bellard
    return bs;
161 b338082b bellard
}
162 b338082b bellard
163 ea2384d3 bellard
BlockDriver *bdrv_find_format(const char *format_name)
164 ea2384d3 bellard
{
165 ea2384d3 bellard
    BlockDriver *drv1;
166 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
167 8a22f02a Stefan Hajnoczi
        if (!strcmp(drv1->format_name, format_name)) {
168 ea2384d3 bellard
            return drv1;
169 8a22f02a Stefan Hajnoczi
        }
170 ea2384d3 bellard
    }
171 ea2384d3 bellard
    return NULL;
172 ea2384d3 bellard
}
173 ea2384d3 bellard
174 eb852011 Markus Armbruster
static int bdrv_is_whitelisted(BlockDriver *drv)
175 eb852011 Markus Armbruster
{
176 eb852011 Markus Armbruster
    static const char *whitelist[] = {
177 eb852011 Markus Armbruster
        CONFIG_BDRV_WHITELIST
178 eb852011 Markus Armbruster
    };
179 eb852011 Markus Armbruster
    const char **p;
180 eb852011 Markus Armbruster
181 eb852011 Markus Armbruster
    if (!whitelist[0])
182 eb852011 Markus Armbruster
        return 1;               /* no whitelist, anything goes */
183 eb852011 Markus Armbruster
184 eb852011 Markus Armbruster
    for (p = whitelist; *p; p++) {
185 eb852011 Markus Armbruster
        if (!strcmp(drv->format_name, *p)) {
186 eb852011 Markus Armbruster
            return 1;
187 eb852011 Markus Armbruster
        }
188 eb852011 Markus Armbruster
    }
189 eb852011 Markus Armbruster
    return 0;
190 eb852011 Markus Armbruster
}
191 eb852011 Markus Armbruster
192 eb852011 Markus Armbruster
BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
193 eb852011 Markus Armbruster
{
194 eb852011 Markus Armbruster
    BlockDriver *drv = bdrv_find_format(format_name);
195 eb852011 Markus Armbruster
    return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
196 eb852011 Markus Armbruster
}
197 eb852011 Markus Armbruster
198 0e7e1989 Kevin Wolf
int bdrv_create(BlockDriver *drv, const char* filename,
199 0e7e1989 Kevin Wolf
    QEMUOptionParameter *options)
200 ea2384d3 bellard
{
201 ea2384d3 bellard
    if (!drv->bdrv_create)
202 ea2384d3 bellard
        return -ENOTSUP;
203 0e7e1989 Kevin Wolf
204 0e7e1989 Kevin Wolf
    return drv->bdrv_create(filename, options);
205 ea2384d3 bellard
}
206 ea2384d3 bellard
207 84a12e66 Christoph Hellwig
int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
208 84a12e66 Christoph Hellwig
{
209 84a12e66 Christoph Hellwig
    BlockDriver *drv;
210 84a12e66 Christoph Hellwig
211 84a12e66 Christoph Hellwig
    drv = find_protocol(filename);
212 84a12e66 Christoph Hellwig
    if (drv == NULL) {
213 84a12e66 Christoph Hellwig
        drv = bdrv_find_format("file");
214 84a12e66 Christoph Hellwig
    }
215 84a12e66 Christoph Hellwig
216 84a12e66 Christoph Hellwig
    return bdrv_create(drv, filename, options);
217 84a12e66 Christoph Hellwig
}
218 84a12e66 Christoph Hellwig
219 d5249393 bellard
#ifdef _WIN32
220 95389c86 bellard
void get_tmp_filename(char *filename, int size)
221 d5249393 bellard
{
222 3b9f94e1 bellard
    char temp_dir[MAX_PATH];
223 3b46e624 ths
224 3b9f94e1 bellard
    GetTempPath(MAX_PATH, temp_dir);
225 3b9f94e1 bellard
    GetTempFileName(temp_dir, "qem", 0, filename);
226 d5249393 bellard
}
227 d5249393 bellard
#else
228 95389c86 bellard
void get_tmp_filename(char *filename, int size)
229 fc01f7e7 bellard
{
230 67b915a5 bellard
    int fd;
231 7ccfb2eb blueswir1
    const char *tmpdir;
232 d5249393 bellard
    /* XXX: race condition possible */
233 0badc1ee aurel32
    tmpdir = getenv("TMPDIR");
234 0badc1ee aurel32
    if (!tmpdir)
235 0badc1ee aurel32
        tmpdir = "/tmp";
236 0badc1ee aurel32
    snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
237 ea2384d3 bellard
    fd = mkstemp(filename);
238 ea2384d3 bellard
    close(fd);
239 ea2384d3 bellard
}
240 d5249393 bellard
#endif
241 fc01f7e7 bellard
242 19cb3738 bellard
#ifdef _WIN32
243 f45512fe bellard
static int is_windows_drive_prefix(const char *filename)
244 f45512fe bellard
{
245 f45512fe bellard
    return (((filename[0] >= 'a' && filename[0] <= 'z') ||
246 f45512fe bellard
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
247 f45512fe bellard
            filename[1] == ':');
248 f45512fe bellard
}
249 3b46e624 ths
250 508c7cb3 Christoph Hellwig
int is_windows_drive(const char *filename)
251 19cb3738 bellard
{
252 5fafdf24 ths
    if (is_windows_drive_prefix(filename) &&
253 f45512fe bellard
        filename[2] == '\0')
254 19cb3738 bellard
        return 1;
255 19cb3738 bellard
    if (strstart(filename, "\\\\.\\", NULL) ||
256 19cb3738 bellard
        strstart(filename, "//./", NULL))
257 19cb3738 bellard
        return 1;
258 19cb3738 bellard
    return 0;
259 19cb3738 bellard
}
260 19cb3738 bellard
#endif
261 19cb3738 bellard
262 84a12e66 Christoph Hellwig
/*
263 84a12e66 Christoph Hellwig
 * Detect host devices. By convention, /dev/cdrom[N] is always
264 84a12e66 Christoph Hellwig
 * recognized as a host CDROM.
265 84a12e66 Christoph Hellwig
 */
266 84a12e66 Christoph Hellwig
static BlockDriver *find_hdev_driver(const char *filename)
267 84a12e66 Christoph Hellwig
{
268 84a12e66 Christoph Hellwig
    int score_max = 0, score;
269 84a12e66 Christoph Hellwig
    BlockDriver *drv = NULL, *d;
270 84a12e66 Christoph Hellwig
271 84a12e66 Christoph Hellwig
    QLIST_FOREACH(d, &bdrv_drivers, list) {
272 84a12e66 Christoph Hellwig
        if (d->bdrv_probe_device) {
273 84a12e66 Christoph Hellwig
            score = d->bdrv_probe_device(filename);
274 84a12e66 Christoph Hellwig
            if (score > score_max) {
275 84a12e66 Christoph Hellwig
                score_max = score;
276 84a12e66 Christoph Hellwig
                drv = d;
277 84a12e66 Christoph Hellwig
            }
278 84a12e66 Christoph Hellwig
        }
279 84a12e66 Christoph Hellwig
    }
280 84a12e66 Christoph Hellwig
281 84a12e66 Christoph Hellwig
    return drv;
282 84a12e66 Christoph Hellwig
}
283 84a12e66 Christoph Hellwig
284 83f64091 bellard
static BlockDriver *find_protocol(const char *filename)
285 83f64091 bellard
{
286 83f64091 bellard
    BlockDriver *drv1;
287 83f64091 bellard
    char protocol[128];
288 1cec71e3 Anthony Liguori
    int len;
289 83f64091 bellard
    const char *p;
290 20993081 Kevin Wolf
    int is_drive;
291 19cb3738 bellard
292 66f82cee Kevin Wolf
    /* TODO Drivers without bdrv_file_open must be specified explicitly */
293 66f82cee Kevin Wolf
294 19cb3738 bellard
#ifdef _WIN32
295 20993081 Kevin Wolf
    is_drive = is_windows_drive(filename) ||
296 20993081 Kevin Wolf
        is_windows_drive_prefix(filename);
297 20993081 Kevin Wolf
#else
298 20993081 Kevin Wolf
    is_drive = 0;
299 19cb3738 bellard
#endif
300 1cec71e3 Anthony Liguori
    p = strchr(filename, ':');
301 20993081 Kevin Wolf
    if (!p || is_drive) {
302 84a12e66 Christoph Hellwig
        drv1 = find_hdev_driver(filename);
303 84a12e66 Christoph Hellwig
        if (!drv1) {
304 84a12e66 Christoph Hellwig
            drv1 = bdrv_find_format("file");
305 84a12e66 Christoph Hellwig
        }
306 84a12e66 Christoph Hellwig
        return drv1;
307 84a12e66 Christoph Hellwig
    }
308 1cec71e3 Anthony Liguori
    len = p - filename;
309 1cec71e3 Anthony Liguori
    if (len > sizeof(protocol) - 1)
310 1cec71e3 Anthony Liguori
        len = sizeof(protocol) - 1;
311 1cec71e3 Anthony Liguori
    memcpy(protocol, filename, len);
312 1cec71e3 Anthony Liguori
    protocol[len] = '\0';
313 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
314 5fafdf24 ths
        if (drv1->protocol_name &&
315 8a22f02a Stefan Hajnoczi
            !strcmp(drv1->protocol_name, protocol)) {
316 83f64091 bellard
            return drv1;
317 8a22f02a Stefan Hajnoczi
        }
318 83f64091 bellard
    }
319 83f64091 bellard
    return NULL;
320 83f64091 bellard
}
321 83f64091 bellard
322 f3a5d3f8 Christoph Hellwig
static BlockDriver *find_image_format(const char *filename)
323 f3a5d3f8 Christoph Hellwig
{
324 f3a5d3f8 Christoph Hellwig
    int ret, score, score_max;
325 f3a5d3f8 Christoph Hellwig
    BlockDriver *drv1, *drv;
326 f3a5d3f8 Christoph Hellwig
    uint8_t buf[2048];
327 f3a5d3f8 Christoph Hellwig
    BlockDriverState *bs;
328 f3a5d3f8 Christoph Hellwig
329 f5edb014 Naphtali Sprei
    ret = bdrv_file_open(&bs, filename, 0);
330 83f64091 bellard
    if (ret < 0)
331 83f64091 bellard
        return NULL;
332 83f64091 bellard
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
333 83f64091 bellard
    bdrv_delete(bs);
334 83f64091 bellard
    if (ret < 0) {
335 83f64091 bellard
        return NULL;
336 83f64091 bellard
    }
337 83f64091 bellard
338 ea2384d3 bellard
    score_max = 0;
339 84a12e66 Christoph Hellwig
    drv = NULL;
340 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
341 83f64091 bellard
        if (drv1->bdrv_probe) {
342 83f64091 bellard
            score = drv1->bdrv_probe(buf, ret, filename);
343 83f64091 bellard
            if (score > score_max) {
344 83f64091 bellard
                score_max = score;
345 83f64091 bellard
                drv = drv1;
346 83f64091 bellard
            }
347 0849bf08 bellard
        }
348 fc01f7e7 bellard
    }
349 ea2384d3 bellard
    return drv;
350 ea2384d3 bellard
}
351 ea2384d3 bellard
352 51762288 Stefan Hajnoczi
/**
353 51762288 Stefan Hajnoczi
 * Set the current 'total_sectors' value
354 51762288 Stefan Hajnoczi
 */
355 51762288 Stefan Hajnoczi
static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
356 51762288 Stefan Hajnoczi
{
357 51762288 Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
358 51762288 Stefan Hajnoczi
359 51762288 Stefan Hajnoczi
    /* query actual device if possible, otherwise just trust the hint */
360 51762288 Stefan Hajnoczi
    if (drv->bdrv_getlength) {
361 51762288 Stefan Hajnoczi
        int64_t length = drv->bdrv_getlength(bs);
362 51762288 Stefan Hajnoczi
        if (length < 0) {
363 51762288 Stefan Hajnoczi
            return length;
364 51762288 Stefan Hajnoczi
        }
365 51762288 Stefan Hajnoczi
        hint = length >> BDRV_SECTOR_BITS;
366 51762288 Stefan Hajnoczi
    }
367 51762288 Stefan Hajnoczi
368 51762288 Stefan Hajnoczi
    bs->total_sectors = hint;
369 51762288 Stefan Hajnoczi
    return 0;
370 51762288 Stefan Hajnoczi
}
371 51762288 Stefan Hajnoczi
372 b6ce07aa Kevin Wolf
/*
373 57915332 Kevin Wolf
 * Common part for opening disk images and files
374 57915332 Kevin Wolf
 */
375 57915332 Kevin Wolf
static int bdrv_open_common(BlockDriverState *bs, const char *filename,
376 57915332 Kevin Wolf
    int flags, BlockDriver *drv)
377 57915332 Kevin Wolf
{
378 57915332 Kevin Wolf
    int ret, open_flags;
379 57915332 Kevin Wolf
380 57915332 Kevin Wolf
    assert(drv != NULL);
381 57915332 Kevin Wolf
382 66f82cee Kevin Wolf
    bs->file = NULL;
383 51762288 Stefan Hajnoczi
    bs->total_sectors = 0;
384 57915332 Kevin Wolf
    bs->is_temporary = 0;
385 57915332 Kevin Wolf
    bs->encrypted = 0;
386 57915332 Kevin Wolf
    bs->valid_key = 0;
387 57915332 Kevin Wolf
    bs->open_flags = flags;
388 57915332 Kevin Wolf
    /* buffer_alignment defaulted to 512, drivers can change this value */
389 57915332 Kevin Wolf
    bs->buffer_alignment = 512;
390 57915332 Kevin Wolf
391 57915332 Kevin Wolf
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
392 57915332 Kevin Wolf
393 57915332 Kevin Wolf
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
394 57915332 Kevin Wolf
        return -ENOTSUP;
395 57915332 Kevin Wolf
    }
396 57915332 Kevin Wolf
397 57915332 Kevin Wolf
    bs->drv = drv;
398 57915332 Kevin Wolf
    bs->opaque = qemu_mallocz(drv->instance_size);
399 57915332 Kevin Wolf
400 57915332 Kevin Wolf
    /*
401 57915332 Kevin Wolf
     * Yes, BDRV_O_NOCACHE aka O_DIRECT means we have to present a
402 57915332 Kevin Wolf
     * write cache to the guest.  We do need the fdatasync to flush
403 57915332 Kevin Wolf
     * out transactions for block allocations, and we maybe have a
404 57915332 Kevin Wolf
     * volatile write cache in our backing device to deal with.
405 57915332 Kevin Wolf
     */
406 57915332 Kevin Wolf
    if (flags & (BDRV_O_CACHE_WB|BDRV_O_NOCACHE))
407 57915332 Kevin Wolf
        bs->enable_write_cache = 1;
408 57915332 Kevin Wolf
409 57915332 Kevin Wolf
    /*
410 57915332 Kevin Wolf
     * Clear flags that are internal to the block layer before opening the
411 57915332 Kevin Wolf
     * image.
412 57915332 Kevin Wolf
     */
413 57915332 Kevin Wolf
    open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
414 57915332 Kevin Wolf
415 57915332 Kevin Wolf
    /*
416 57915332 Kevin Wolf
     * Snapshots should be writeable.
417 57915332 Kevin Wolf
     */
418 57915332 Kevin Wolf
    if (bs->is_temporary) {
419 57915332 Kevin Wolf
        open_flags |= BDRV_O_RDWR;
420 57915332 Kevin Wolf
    }
421 57915332 Kevin Wolf
422 66f82cee Kevin Wolf
    /* Open the image, either directly or using a protocol */
423 66f82cee Kevin Wolf
    if (drv->bdrv_file_open) {
424 66f82cee Kevin Wolf
        ret = drv->bdrv_file_open(bs, filename, open_flags);
425 66f82cee Kevin Wolf
    } else {
426 66f82cee Kevin Wolf
        ret = bdrv_file_open(&bs->file, filename, open_flags);
427 66f82cee Kevin Wolf
        if (ret >= 0) {
428 66f82cee Kevin Wolf
            ret = drv->bdrv_open(bs, open_flags);
429 66f82cee Kevin Wolf
        }
430 66f82cee Kevin Wolf
    }
431 66f82cee Kevin Wolf
432 57915332 Kevin Wolf
    if (ret < 0) {
433 57915332 Kevin Wolf
        goto free_and_fail;
434 57915332 Kevin Wolf
    }
435 57915332 Kevin Wolf
436 57915332 Kevin Wolf
    bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
437 51762288 Stefan Hajnoczi
438 51762288 Stefan Hajnoczi
    ret = refresh_total_sectors(bs, bs->total_sectors);
439 51762288 Stefan Hajnoczi
    if (ret < 0) {
440 51762288 Stefan Hajnoczi
        goto free_and_fail;
441 57915332 Kevin Wolf
    }
442 51762288 Stefan Hajnoczi
443 57915332 Kevin Wolf
#ifndef _WIN32
444 57915332 Kevin Wolf
    if (bs->is_temporary) {
445 57915332 Kevin Wolf
        unlink(filename);
446 57915332 Kevin Wolf
    }
447 57915332 Kevin Wolf
#endif
448 57915332 Kevin Wolf
    return 0;
449 57915332 Kevin Wolf
450 57915332 Kevin Wolf
free_and_fail:
451 66f82cee Kevin Wolf
    if (bs->file) {
452 66f82cee Kevin Wolf
        bdrv_delete(bs->file);
453 66f82cee Kevin Wolf
        bs->file = NULL;
454 66f82cee Kevin Wolf
    }
455 57915332 Kevin Wolf
    qemu_free(bs->opaque);
456 57915332 Kevin Wolf
    bs->opaque = NULL;
457 57915332 Kevin Wolf
    bs->drv = NULL;
458 57915332 Kevin Wolf
    return ret;
459 57915332 Kevin Wolf
}
460 57915332 Kevin Wolf
461 57915332 Kevin Wolf
/*
462 b6ce07aa Kevin Wolf
 * Opens a file using a protocol (file, host_device, nbd, ...)
463 b6ce07aa Kevin Wolf
 */
464 83f64091 bellard
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
465 ea2384d3 bellard
{
466 83f64091 bellard
    BlockDriverState *bs;
467 6db95603 Christoph Hellwig
    BlockDriver *drv;
468 83f64091 bellard
    int ret;
469 83f64091 bellard
470 6db95603 Christoph Hellwig
    drv = find_protocol(filename);
471 6db95603 Christoph Hellwig
    if (!drv) {
472 6db95603 Christoph Hellwig
        return -ENOENT;
473 6db95603 Christoph Hellwig
    }
474 6db95603 Christoph Hellwig
475 83f64091 bellard
    bs = bdrv_new("");
476 b6ce07aa Kevin Wolf
    ret = bdrv_open_common(bs, filename, flags, drv);
477 83f64091 bellard
    if (ret < 0) {
478 83f64091 bellard
        bdrv_delete(bs);
479 83f64091 bellard
        return ret;
480 3b0d4f61 bellard
    }
481 71d0770c aliguori
    bs->growable = 1;
482 83f64091 bellard
    *pbs = bs;
483 83f64091 bellard
    return 0;
484 83f64091 bellard
}
485 83f64091 bellard
486 b6ce07aa Kevin Wolf
/*
487 b6ce07aa Kevin Wolf
 * Opens a disk image (raw, qcow2, vmdk, ...)
488 b6ce07aa Kevin Wolf
 */
489 d6e9098e Kevin Wolf
int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
490 d6e9098e Kevin Wolf
              BlockDriver *drv)
491 ea2384d3 bellard
{
492 b6ce07aa Kevin Wolf
    int ret;
493 712e7874 bellard
494 83f64091 bellard
    if (flags & BDRV_O_SNAPSHOT) {
495 ea2384d3 bellard
        BlockDriverState *bs1;
496 ea2384d3 bellard
        int64_t total_size;
497 7c96d46e aliguori
        int is_protocol = 0;
498 91a073a9 Kevin Wolf
        BlockDriver *bdrv_qcow2;
499 91a073a9 Kevin Wolf
        QEMUOptionParameter *options;
500 b6ce07aa Kevin Wolf
        char tmp_filename[PATH_MAX];
501 b6ce07aa Kevin Wolf
        char backing_filename[PATH_MAX];
502 3b46e624 ths
503 ea2384d3 bellard
        /* if snapshot, we create a temporary backing file and open it
504 ea2384d3 bellard
           instead of opening 'filename' directly */
505 33e3963e bellard
506 ea2384d3 bellard
        /* if there is a backing file, use it */
507 ea2384d3 bellard
        bs1 = bdrv_new("");
508 d6e9098e Kevin Wolf
        ret = bdrv_open(bs1, filename, 0, drv);
509 51d7c00c aliguori
        if (ret < 0) {
510 ea2384d3 bellard
            bdrv_delete(bs1);
511 51d7c00c aliguori
            return ret;
512 ea2384d3 bellard
        }
513 6ea44308 Jan Kiszka
        total_size = bdrv_getlength(bs1) >> BDRV_SECTOR_BITS;
514 7c96d46e aliguori
515 7c96d46e aliguori
        if (bs1->drv && bs1->drv->protocol_name)
516 7c96d46e aliguori
            is_protocol = 1;
517 7c96d46e aliguori
518 ea2384d3 bellard
        bdrv_delete(bs1);
519 3b46e624 ths
520 ea2384d3 bellard
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
521 7c96d46e aliguori
522 7c96d46e aliguori
        /* Real path is meaningless for protocols */
523 7c96d46e aliguori
        if (is_protocol)
524 7c96d46e aliguori
            snprintf(backing_filename, sizeof(backing_filename),
525 7c96d46e aliguori
                     "%s", filename);
526 114cdfa9 Kirill A. Shutemov
        else if (!realpath(filename, backing_filename))
527 114cdfa9 Kirill A. Shutemov
            return -errno;
528 7c96d46e aliguori
529 91a073a9 Kevin Wolf
        bdrv_qcow2 = bdrv_find_format("qcow2");
530 91a073a9 Kevin Wolf
        options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
531 91a073a9 Kevin Wolf
532 91a073a9 Kevin Wolf
        set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size * 512);
533 91a073a9 Kevin Wolf
        set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
534 91a073a9 Kevin Wolf
        if (drv) {
535 91a073a9 Kevin Wolf
            set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
536 91a073a9 Kevin Wolf
                drv->format_name);
537 91a073a9 Kevin Wolf
        }
538 91a073a9 Kevin Wolf
539 91a073a9 Kevin Wolf
        ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
540 d748768c Jan Kiszka
        free_option_parameters(options);
541 51d7c00c aliguori
        if (ret < 0) {
542 51d7c00c aliguori
            return ret;
543 ea2384d3 bellard
        }
544 91a073a9 Kevin Wolf
545 ea2384d3 bellard
        filename = tmp_filename;
546 91a073a9 Kevin Wolf
        drv = bdrv_qcow2;
547 ea2384d3 bellard
        bs->is_temporary = 1;
548 ea2384d3 bellard
    }
549 712e7874 bellard
550 b6ce07aa Kevin Wolf
    /* Find the right image format driver */
551 6db95603 Christoph Hellwig
    if (!drv) {
552 84a12e66 Christoph Hellwig
        drv = find_image_format(filename);
553 51d7c00c aliguori
    }
554 6987307c Christoph Hellwig
555 51d7c00c aliguori
    if (!drv) {
556 51d7c00c aliguori
        ret = -ENOENT;
557 51d7c00c aliguori
        goto unlink_and_fail;
558 ea2384d3 bellard
    }
559 b6ce07aa Kevin Wolf
560 b6ce07aa Kevin Wolf
    /* Open the image */
561 b6ce07aa Kevin Wolf
    ret = bdrv_open_common(bs, filename, flags, drv);
562 b6ce07aa Kevin Wolf
    if (ret < 0) {
563 6987307c Christoph Hellwig
        goto unlink_and_fail;
564 6987307c Christoph Hellwig
    }
565 6987307c Christoph Hellwig
566 b6ce07aa Kevin Wolf
    /* If there is a backing file, use it */
567 b6ce07aa Kevin Wolf
    if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
568 b6ce07aa Kevin Wolf
        char backing_filename[PATH_MAX];
569 b6ce07aa Kevin Wolf
        int back_flags;
570 b6ce07aa Kevin Wolf
        BlockDriver *back_drv = NULL;
571 b6ce07aa Kevin Wolf
572 b6ce07aa Kevin Wolf
        bs->backing_hd = bdrv_new("");
573 b6ce07aa Kevin Wolf
        path_combine(backing_filename, sizeof(backing_filename),
574 b6ce07aa Kevin Wolf
                     filename, bs->backing_file);
575 b6ce07aa Kevin Wolf
        if (bs->backing_format[0] != '\0')
576 b6ce07aa Kevin Wolf
            back_drv = bdrv_find_format(bs->backing_format);
577 b6ce07aa Kevin Wolf
578 b6ce07aa Kevin Wolf
        /* backing files always opened read-only */
579 b6ce07aa Kevin Wolf
        back_flags =
580 b6ce07aa Kevin Wolf
            flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
581 b6ce07aa Kevin Wolf
582 b6ce07aa Kevin Wolf
        ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
583 b6ce07aa Kevin Wolf
        if (ret < 0) {
584 b6ce07aa Kevin Wolf
            bdrv_close(bs);
585 b6ce07aa Kevin Wolf
            return ret;
586 b6ce07aa Kevin Wolf
        }
587 b6ce07aa Kevin Wolf
        if (bs->is_temporary) {
588 b6ce07aa Kevin Wolf
            bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
589 b6ce07aa Kevin Wolf
        } else {
590 b6ce07aa Kevin Wolf
            /* base image inherits from "parent" */
591 b6ce07aa Kevin Wolf
            bs->backing_hd->keep_read_only = bs->keep_read_only;
592 b6ce07aa Kevin Wolf
        }
593 b6ce07aa Kevin Wolf
    }
594 b6ce07aa Kevin Wolf
595 b6ce07aa Kevin Wolf
    if (!bdrv_key_required(bs)) {
596 b6ce07aa Kevin Wolf
        /* call the change callback */
597 b6ce07aa Kevin Wolf
        bs->media_changed = 1;
598 b6ce07aa Kevin Wolf
        if (bs->change_cb)
599 b6ce07aa Kevin Wolf
            bs->change_cb(bs->change_opaque);
600 b6ce07aa Kevin Wolf
    }
601 b6ce07aa Kevin Wolf
602 b6ce07aa Kevin Wolf
    return 0;
603 b6ce07aa Kevin Wolf
604 b6ce07aa Kevin Wolf
unlink_and_fail:
605 b6ce07aa Kevin Wolf
    if (bs->is_temporary) {
606 b6ce07aa Kevin Wolf
        unlink(filename);
607 b6ce07aa Kevin Wolf
    }
608 b6ce07aa Kevin Wolf
    return ret;
609 b6ce07aa Kevin Wolf
}
610 b6ce07aa Kevin Wolf
611 fc01f7e7 bellard
void bdrv_close(BlockDriverState *bs)
612 fc01f7e7 bellard
{
613 19cb3738 bellard
    if (bs->drv) {
614 557df6ac Stefan Hajnoczi
        if (bs->backing_hd) {
615 ea2384d3 bellard
            bdrv_delete(bs->backing_hd);
616 557df6ac Stefan Hajnoczi
            bs->backing_hd = NULL;
617 557df6ac Stefan Hajnoczi
        }
618 ea2384d3 bellard
        bs->drv->bdrv_close(bs);
619 ea2384d3 bellard
        qemu_free(bs->opaque);
620 ea2384d3 bellard
#ifdef _WIN32
621 ea2384d3 bellard
        if (bs->is_temporary) {
622 ea2384d3 bellard
            unlink(bs->filename);
623 ea2384d3 bellard
        }
624 67b915a5 bellard
#endif
625 ea2384d3 bellard
        bs->opaque = NULL;
626 ea2384d3 bellard
        bs->drv = NULL;
627 b338082b bellard
628 66f82cee Kevin Wolf
        if (bs->file != NULL) {
629 66f82cee Kevin Wolf
            bdrv_close(bs->file);
630 66f82cee Kevin Wolf
        }
631 66f82cee Kevin Wolf
632 b338082b bellard
        /* call the change callback */
633 19cb3738 bellard
        bs->media_changed = 1;
634 b338082b bellard
        if (bs->change_cb)
635 b338082b bellard
            bs->change_cb(bs->change_opaque);
636 b338082b bellard
    }
637 b338082b bellard
}
638 b338082b bellard
639 b338082b bellard
void bdrv_delete(BlockDriverState *bs)
640 b338082b bellard
{
641 1b7bdbc1 Stefan Hajnoczi
    /* remove from list, if necessary */
642 1b7bdbc1 Stefan Hajnoczi
    if (bs->device_name[0] != '\0') {
643 1b7bdbc1 Stefan Hajnoczi
        QTAILQ_REMOVE(&bdrv_states, bs, list);
644 1b7bdbc1 Stefan Hajnoczi
    }
645 34c6f050 aurel32
646 b338082b bellard
    bdrv_close(bs);
647 66f82cee Kevin Wolf
    if (bs->file != NULL) {
648 66f82cee Kevin Wolf
        bdrv_delete(bs->file);
649 66f82cee Kevin Wolf
    }
650 66f82cee Kevin Wolf
651 b338082b bellard
    qemu_free(bs);
652 fc01f7e7 bellard
}
653 fc01f7e7 bellard
654 e97fc193 aliguori
/*
655 e97fc193 aliguori
 * Run consistency checks on an image
656 e97fc193 aliguori
 *
657 e97fc193 aliguori
 * Returns the number of errors or -errno when an internal error occurs
658 e97fc193 aliguori
 */
659 e97fc193 aliguori
int bdrv_check(BlockDriverState *bs)
660 e97fc193 aliguori
{
661 e97fc193 aliguori
    if (bs->drv->bdrv_check == NULL) {
662 e97fc193 aliguori
        return -ENOTSUP;
663 e97fc193 aliguori
    }
664 e97fc193 aliguori
665 e97fc193 aliguori
    return bs->drv->bdrv_check(bs);
666 e97fc193 aliguori
}
667 e97fc193 aliguori
668 33e3963e bellard
/* commit COW file into the raw image */
669 33e3963e bellard
int bdrv_commit(BlockDriverState *bs)
670 33e3963e bellard
{
671 19cb3738 bellard
    BlockDriver *drv = bs->drv;
672 83f64091 bellard
    int64_t i, total_sectors;
673 4dca4b63 Naphtali Sprei
    int n, j, ro, open_flags;
674 4dca4b63 Naphtali Sprei
    int ret = 0, rw_ret = 0;
675 ea2384d3 bellard
    unsigned char sector[512];
676 4dca4b63 Naphtali Sprei
    char filename[1024];
677 4dca4b63 Naphtali Sprei
    BlockDriverState *bs_rw, *bs_ro;
678 33e3963e bellard
679 19cb3738 bellard
    if (!drv)
680 19cb3738 bellard
        return -ENOMEDIUM;
681 4dca4b63 Naphtali Sprei
    
682 4dca4b63 Naphtali Sprei
    if (!bs->backing_hd) {
683 4dca4b63 Naphtali Sprei
        return -ENOTSUP;
684 33e3963e bellard
    }
685 33e3963e bellard
686 4dca4b63 Naphtali Sprei
    if (bs->backing_hd->keep_read_only) {
687 4dca4b63 Naphtali Sprei
        return -EACCES;
688 4dca4b63 Naphtali Sprei
    }
689 4dca4b63 Naphtali Sprei
    
690 4dca4b63 Naphtali Sprei
    ro = bs->backing_hd->read_only;
691 4dca4b63 Naphtali Sprei
    strncpy(filename, bs->backing_hd->filename, sizeof(filename));
692 4dca4b63 Naphtali Sprei
    open_flags =  bs->backing_hd->open_flags;
693 4dca4b63 Naphtali Sprei
694 4dca4b63 Naphtali Sprei
    if (ro) {
695 4dca4b63 Naphtali Sprei
        /* re-open as RW */
696 4dca4b63 Naphtali Sprei
        bdrv_delete(bs->backing_hd);
697 4dca4b63 Naphtali Sprei
        bs->backing_hd = NULL;
698 4dca4b63 Naphtali Sprei
        bs_rw = bdrv_new("");
699 c3349197 Kevin Wolf
        rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR, drv);
700 4dca4b63 Naphtali Sprei
        if (rw_ret < 0) {
701 4dca4b63 Naphtali Sprei
            bdrv_delete(bs_rw);
702 4dca4b63 Naphtali Sprei
            /* try to re-open read-only */
703 4dca4b63 Naphtali Sprei
            bs_ro = bdrv_new("");
704 c3349197 Kevin Wolf
            ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
705 4dca4b63 Naphtali Sprei
            if (ret < 0) {
706 4dca4b63 Naphtali Sprei
                bdrv_delete(bs_ro);
707 4dca4b63 Naphtali Sprei
                /* drive not functional anymore */
708 4dca4b63 Naphtali Sprei
                bs->drv = NULL;
709 4dca4b63 Naphtali Sprei
                return ret;
710 4dca4b63 Naphtali Sprei
            }
711 4dca4b63 Naphtali Sprei
            bs->backing_hd = bs_ro;
712 4dca4b63 Naphtali Sprei
            return rw_ret;
713 4dca4b63 Naphtali Sprei
        }
714 4dca4b63 Naphtali Sprei
        bs->backing_hd = bs_rw;
715 ea2384d3 bellard
    }
716 33e3963e bellard
717 6ea44308 Jan Kiszka
    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
718 83f64091 bellard
    for (i = 0; i < total_sectors;) {
719 19cb3738 bellard
        if (drv->bdrv_is_allocated(bs, i, 65536, &n)) {
720 ea2384d3 bellard
            for(j = 0; j < n; j++) {
721 ea2384d3 bellard
                if (bdrv_read(bs, i, sector, 1) != 0) {
722 4dca4b63 Naphtali Sprei
                    ret = -EIO;
723 4dca4b63 Naphtali Sprei
                    goto ro_cleanup;
724 ea2384d3 bellard
                }
725 ea2384d3 bellard
726 ea2384d3 bellard
                if (bdrv_write(bs->backing_hd, i, sector, 1) != 0) {
727 4dca4b63 Naphtali Sprei
                    ret = -EIO;
728 4dca4b63 Naphtali Sprei
                    goto ro_cleanup;
729 ea2384d3 bellard
                }
730 ea2384d3 bellard
                i++;
731 33e3963e bellard
            }
732 ea2384d3 bellard
        } else {
733 ea2384d3 bellard
            i += n;
734 ea2384d3 bellard
        }
735 33e3963e bellard
    }
736 95389c86 bellard
737 1d44952f Christoph Hellwig
    if (drv->bdrv_make_empty) {
738 1d44952f Christoph Hellwig
        ret = drv->bdrv_make_empty(bs);
739 1d44952f Christoph Hellwig
        bdrv_flush(bs);
740 1d44952f Christoph Hellwig
    }
741 95389c86 bellard
742 3f5075ae Christoph Hellwig
    /*
743 3f5075ae Christoph Hellwig
     * Make sure all data we wrote to the backing device is actually
744 3f5075ae Christoph Hellwig
     * stable on disk.
745 3f5075ae Christoph Hellwig
     */
746 3f5075ae Christoph Hellwig
    if (bs->backing_hd)
747 3f5075ae Christoph Hellwig
        bdrv_flush(bs->backing_hd);
748 4dca4b63 Naphtali Sprei
749 4dca4b63 Naphtali Sprei
ro_cleanup:
750 4dca4b63 Naphtali Sprei
751 4dca4b63 Naphtali Sprei
    if (ro) {
752 4dca4b63 Naphtali Sprei
        /* re-open as RO */
753 4dca4b63 Naphtali Sprei
        bdrv_delete(bs->backing_hd);
754 4dca4b63 Naphtali Sprei
        bs->backing_hd = NULL;
755 4dca4b63 Naphtali Sprei
        bs_ro = bdrv_new("");
756 c3349197 Kevin Wolf
        ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR, drv);
757 4dca4b63 Naphtali Sprei
        if (ret < 0) {
758 4dca4b63 Naphtali Sprei
            bdrv_delete(bs_ro);
759 4dca4b63 Naphtali Sprei
            /* drive not functional anymore */
760 4dca4b63 Naphtali Sprei
            bs->drv = NULL;
761 4dca4b63 Naphtali Sprei
            return ret;
762 4dca4b63 Naphtali Sprei
        }
763 4dca4b63 Naphtali Sprei
        bs->backing_hd = bs_ro;
764 4dca4b63 Naphtali Sprei
        bs->backing_hd->keep_read_only = 0;
765 4dca4b63 Naphtali Sprei
    }
766 4dca4b63 Naphtali Sprei
767 1d44952f Christoph Hellwig
    return ret;
768 33e3963e bellard
}
769 33e3963e bellard
770 756e6736 Kevin Wolf
/*
771 756e6736 Kevin Wolf
 * Return values:
772 756e6736 Kevin Wolf
 * 0        - success
773 756e6736 Kevin Wolf
 * -EINVAL  - backing format specified, but no file
774 756e6736 Kevin Wolf
 * -ENOSPC  - can't update the backing file because no space is left in the
775 756e6736 Kevin Wolf
 *            image file header
776 756e6736 Kevin Wolf
 * -ENOTSUP - format driver doesn't support changing the backing file
777 756e6736 Kevin Wolf
 */
778 756e6736 Kevin Wolf
int bdrv_change_backing_file(BlockDriverState *bs,
779 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
780 756e6736 Kevin Wolf
{
781 756e6736 Kevin Wolf
    BlockDriver *drv = bs->drv;
782 756e6736 Kevin Wolf
783 756e6736 Kevin Wolf
    if (drv->bdrv_change_backing_file != NULL) {
784 756e6736 Kevin Wolf
        return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
785 756e6736 Kevin Wolf
    } else {
786 756e6736 Kevin Wolf
        return -ENOTSUP;
787 756e6736 Kevin Wolf
    }
788 756e6736 Kevin Wolf
}
789 756e6736 Kevin Wolf
790 71d0770c aliguori
static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
791 71d0770c aliguori
                                   size_t size)
792 71d0770c aliguori
{
793 71d0770c aliguori
    int64_t len;
794 71d0770c aliguori
795 71d0770c aliguori
    if (!bdrv_is_inserted(bs))
796 71d0770c aliguori
        return -ENOMEDIUM;
797 71d0770c aliguori
798 71d0770c aliguori
    if (bs->growable)
799 71d0770c aliguori
        return 0;
800 71d0770c aliguori
801 71d0770c aliguori
    len = bdrv_getlength(bs);
802 71d0770c aliguori
803 fbb7b4e0 Kevin Wolf
    if (offset < 0)
804 fbb7b4e0 Kevin Wolf
        return -EIO;
805 fbb7b4e0 Kevin Wolf
806 fbb7b4e0 Kevin Wolf
    if ((offset > len) || (len - offset < size))
807 71d0770c aliguori
        return -EIO;
808 71d0770c aliguori
809 71d0770c aliguori
    return 0;
810 71d0770c aliguori
}
811 71d0770c aliguori
812 71d0770c aliguori
static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
813 71d0770c aliguori
                              int nb_sectors)
814 71d0770c aliguori
{
815 999dec57 aliguori
    return bdrv_check_byte_request(bs, sector_num * 512, nb_sectors * 512);
816 71d0770c aliguori
}
817 71d0770c aliguori
818 19cb3738 bellard
/* return < 0 if error. See bdrv_write() for the return codes */
819 5fafdf24 ths
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
820 fc01f7e7 bellard
              uint8_t *buf, int nb_sectors)
821 fc01f7e7 bellard
{
822 ea2384d3 bellard
    BlockDriver *drv = bs->drv;
823 ea2384d3 bellard
824 19cb3738 bellard
    if (!drv)
825 19cb3738 bellard
        return -ENOMEDIUM;
826 71d0770c aliguori
    if (bdrv_check_request(bs, sector_num, nb_sectors))
827 71d0770c aliguori
        return -EIO;
828 b338082b bellard
829 eda578e5 aliguori
    return drv->bdrv_read(bs, sector_num, buf, nb_sectors);
830 fc01f7e7 bellard
}
831 fc01f7e7 bellard
832 7cd1e32a lirans@il.ibm.com
static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
833 a55eb92c Jan Kiszka
                             int nb_sectors, int dirty)
834 7cd1e32a lirans@il.ibm.com
{
835 7cd1e32a lirans@il.ibm.com
    int64_t start, end;
836 c6d22830 Jan Kiszka
    unsigned long val, idx, bit;
837 a55eb92c Jan Kiszka
838 6ea44308 Jan Kiszka
    start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
839 c6d22830 Jan Kiszka
    end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
840 a55eb92c Jan Kiszka
841 a55eb92c Jan Kiszka
    for (; start <= end; start++) {
842 c6d22830 Jan Kiszka
        idx = start / (sizeof(unsigned long) * 8);
843 c6d22830 Jan Kiszka
        bit = start % (sizeof(unsigned long) * 8);
844 c6d22830 Jan Kiszka
        val = bs->dirty_bitmap[idx];
845 c6d22830 Jan Kiszka
        if (dirty) {
846 aaa0eb75 Liran Schour
            if (!(val & (1 << bit))) {
847 aaa0eb75 Liran Schour
                bs->dirty_count++;
848 aaa0eb75 Liran Schour
                val |= 1 << bit;
849 aaa0eb75 Liran Schour
            }
850 c6d22830 Jan Kiszka
        } else {
851 aaa0eb75 Liran Schour
            if (val & (1 << bit)) {
852 aaa0eb75 Liran Schour
                bs->dirty_count--;
853 aaa0eb75 Liran Schour
                val &= ~(1 << bit);
854 aaa0eb75 Liran Schour
            }
855 c6d22830 Jan Kiszka
        }
856 c6d22830 Jan Kiszka
        bs->dirty_bitmap[idx] = val;
857 7cd1e32a lirans@il.ibm.com
    }
858 7cd1e32a lirans@il.ibm.com
}
859 7cd1e32a lirans@il.ibm.com
860 5fafdf24 ths
/* Return < 0 if error. Important errors are:
861 19cb3738 bellard
  -EIO         generic I/O error (may happen for all errors)
862 19cb3738 bellard
  -ENOMEDIUM   No media inserted.
863 19cb3738 bellard
  -EINVAL      Invalid sector number or nb_sectors
864 19cb3738 bellard
  -EACCES      Trying to write a read-only device
865 19cb3738 bellard
*/
866 5fafdf24 ths
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
867 fc01f7e7 bellard
               const uint8_t *buf, int nb_sectors)
868 fc01f7e7 bellard
{
869 83f64091 bellard
    BlockDriver *drv = bs->drv;
870 19cb3738 bellard
    if (!bs->drv)
871 19cb3738 bellard
        return -ENOMEDIUM;
872 0849bf08 bellard
    if (bs->read_only)
873 19cb3738 bellard
        return -EACCES;
874 71d0770c aliguori
    if (bdrv_check_request(bs, sector_num, nb_sectors))
875 71d0770c aliguori
        return -EIO;
876 a55eb92c Jan Kiszka
877 c6d22830 Jan Kiszka
    if (bs->dirty_bitmap) {
878 7cd1e32a lirans@il.ibm.com
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
879 7cd1e32a lirans@il.ibm.com
    }
880 a55eb92c Jan Kiszka
881 294cc35f Kevin Wolf
    if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
882 294cc35f Kevin Wolf
        bs->wr_highest_sector = sector_num + nb_sectors - 1;
883 294cc35f Kevin Wolf
    }
884 294cc35f Kevin Wolf
885 42fb2807 aliguori
    return drv->bdrv_write(bs, sector_num, buf, nb_sectors);
886 83f64091 bellard
}
887 83f64091 bellard
888 eda578e5 aliguori
int bdrv_pread(BlockDriverState *bs, int64_t offset,
889 eda578e5 aliguori
               void *buf, int count1)
890 83f64091 bellard
{
891 6ea44308 Jan Kiszka
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
892 83f64091 bellard
    int len, nb_sectors, count;
893 83f64091 bellard
    int64_t sector_num;
894 9a8c4cce Kevin Wolf
    int ret;
895 83f64091 bellard
896 83f64091 bellard
    count = count1;
897 83f64091 bellard
    /* first read to align to sector start */
898 6ea44308 Jan Kiszka
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
899 83f64091 bellard
    if (len > count)
900 83f64091 bellard
        len = count;
901 6ea44308 Jan Kiszka
    sector_num = offset >> BDRV_SECTOR_BITS;
902 83f64091 bellard
    if (len > 0) {
903 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
904 9a8c4cce Kevin Wolf
            return ret;
905 6ea44308 Jan Kiszka
        memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
906 83f64091 bellard
        count -= len;
907 83f64091 bellard
        if (count == 0)
908 83f64091 bellard
            return count1;
909 83f64091 bellard
        sector_num++;
910 83f64091 bellard
        buf += len;
911 83f64091 bellard
    }
912 83f64091 bellard
913 83f64091 bellard
    /* read the sectors "in place" */
914 6ea44308 Jan Kiszka
    nb_sectors = count >> BDRV_SECTOR_BITS;
915 83f64091 bellard
    if (nb_sectors > 0) {
916 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
917 9a8c4cce Kevin Wolf
            return ret;
918 83f64091 bellard
        sector_num += nb_sectors;
919 6ea44308 Jan Kiszka
        len = nb_sectors << BDRV_SECTOR_BITS;
920 83f64091 bellard
        buf += len;
921 83f64091 bellard
        count -= len;
922 83f64091 bellard
    }
923 83f64091 bellard
924 83f64091 bellard
    /* add data from the last sector */
925 83f64091 bellard
    if (count > 0) {
926 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
927 9a8c4cce Kevin Wolf
            return ret;
928 83f64091 bellard
        memcpy(buf, tmp_buf, count);
929 83f64091 bellard
    }
930 83f64091 bellard
    return count1;
931 83f64091 bellard
}
932 83f64091 bellard
933 eda578e5 aliguori
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
934 eda578e5 aliguori
                const void *buf, int count1)
935 83f64091 bellard
{
936 6ea44308 Jan Kiszka
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
937 83f64091 bellard
    int len, nb_sectors, count;
938 83f64091 bellard
    int64_t sector_num;
939 9a8c4cce Kevin Wolf
    int ret;
940 83f64091 bellard
941 83f64091 bellard
    count = count1;
942 83f64091 bellard
    /* first write to align to sector start */
943 6ea44308 Jan Kiszka
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
944 83f64091 bellard
    if (len > count)
945 83f64091 bellard
        len = count;
946 6ea44308 Jan Kiszka
    sector_num = offset >> BDRV_SECTOR_BITS;
947 83f64091 bellard
    if (len > 0) {
948 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
949 9a8c4cce Kevin Wolf
            return ret;
950 6ea44308 Jan Kiszka
        memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
951 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
952 9a8c4cce Kevin Wolf
            return ret;
953 83f64091 bellard
        count -= len;
954 83f64091 bellard
        if (count == 0)
955 83f64091 bellard
            return count1;
956 83f64091 bellard
        sector_num++;
957 83f64091 bellard
        buf += len;
958 83f64091 bellard
    }
959 83f64091 bellard
960 83f64091 bellard
    /* write the sectors "in place" */
961 6ea44308 Jan Kiszka
    nb_sectors = count >> BDRV_SECTOR_BITS;
962 83f64091 bellard
    if (nb_sectors > 0) {
963 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
964 9a8c4cce Kevin Wolf
            return ret;
965 83f64091 bellard
        sector_num += nb_sectors;
966 6ea44308 Jan Kiszka
        len = nb_sectors << BDRV_SECTOR_BITS;
967 83f64091 bellard
        buf += len;
968 83f64091 bellard
        count -= len;
969 83f64091 bellard
    }
970 83f64091 bellard
971 83f64091 bellard
    /* add data from the last sector */
972 83f64091 bellard
    if (count > 0) {
973 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
974 9a8c4cce Kevin Wolf
            return ret;
975 83f64091 bellard
        memcpy(tmp_buf, buf, count);
976 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
977 9a8c4cce Kevin Wolf
            return ret;
978 83f64091 bellard
    }
979 83f64091 bellard
    return count1;
980 83f64091 bellard
}
981 83f64091 bellard
982 83f64091 bellard
/**
983 83f64091 bellard
 * Truncate file to 'offset' bytes (needed only for file protocols)
984 83f64091 bellard
 */
985 83f64091 bellard
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
986 83f64091 bellard
{
987 83f64091 bellard
    BlockDriver *drv = bs->drv;
988 51762288 Stefan Hajnoczi
    int ret;
989 83f64091 bellard
    if (!drv)
990 19cb3738 bellard
        return -ENOMEDIUM;
991 83f64091 bellard
    if (!drv->bdrv_truncate)
992 83f64091 bellard
        return -ENOTSUP;
993 59f2689d Naphtali Sprei
    if (bs->read_only)
994 59f2689d Naphtali Sprei
        return -EACCES;
995 51762288 Stefan Hajnoczi
    ret = drv->bdrv_truncate(bs, offset);
996 51762288 Stefan Hajnoczi
    if (ret == 0) {
997 51762288 Stefan Hajnoczi
        ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
998 51762288 Stefan Hajnoczi
    }
999 51762288 Stefan Hajnoczi
    return ret;
1000 83f64091 bellard
}
1001 83f64091 bellard
1002 83f64091 bellard
/**
1003 83f64091 bellard
 * Length of a file in bytes. Return < 0 if error or unknown.
1004 83f64091 bellard
 */
1005 83f64091 bellard
int64_t bdrv_getlength(BlockDriverState *bs)
1006 83f64091 bellard
{
1007 83f64091 bellard
    BlockDriver *drv = bs->drv;
1008 83f64091 bellard
    if (!drv)
1009 19cb3738 bellard
        return -ENOMEDIUM;
1010 51762288 Stefan Hajnoczi
1011 51762288 Stefan Hajnoczi
    /* Fixed size devices use the total_sectors value for speed instead of
1012 51762288 Stefan Hajnoczi
       issuing a length query (like lseek) on each call.  Also, legacy block
1013 51762288 Stefan Hajnoczi
       drivers don't provide a bdrv_getlength function and must use
1014 51762288 Stefan Hajnoczi
       total_sectors. */
1015 51762288 Stefan Hajnoczi
    if (!bs->growable || !drv->bdrv_getlength) {
1016 6ea44308 Jan Kiszka
        return bs->total_sectors * BDRV_SECTOR_SIZE;
1017 83f64091 bellard
    }
1018 83f64091 bellard
    return drv->bdrv_getlength(bs);
1019 fc01f7e7 bellard
}
1020 fc01f7e7 bellard
1021 19cb3738 bellard
/* return 0 as number of sectors if no device present or error */
1022 96b8f136 ths
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1023 fc01f7e7 bellard
{
1024 19cb3738 bellard
    int64_t length;
1025 19cb3738 bellard
    length = bdrv_getlength(bs);
1026 19cb3738 bellard
    if (length < 0)
1027 19cb3738 bellard
        length = 0;
1028 19cb3738 bellard
    else
1029 6ea44308 Jan Kiszka
        length = length >> BDRV_SECTOR_BITS;
1030 19cb3738 bellard
    *nb_sectors_ptr = length;
1031 fc01f7e7 bellard
}
1032 cf98951b bellard
1033 f3d54fc4 aliguori
struct partition {
1034 f3d54fc4 aliguori
        uint8_t boot_ind;           /* 0x80 - active */
1035 f3d54fc4 aliguori
        uint8_t head;               /* starting head */
1036 f3d54fc4 aliguori
        uint8_t sector;             /* starting sector */
1037 f3d54fc4 aliguori
        uint8_t cyl;                /* starting cylinder */
1038 f3d54fc4 aliguori
        uint8_t sys_ind;            /* What partition type */
1039 f3d54fc4 aliguori
        uint8_t end_head;           /* end head */
1040 f3d54fc4 aliguori
        uint8_t end_sector;         /* end sector */
1041 f3d54fc4 aliguori
        uint8_t end_cyl;            /* end cylinder */
1042 f3d54fc4 aliguori
        uint32_t start_sect;        /* starting sector counting from 0 */
1043 f3d54fc4 aliguori
        uint32_t nr_sects;          /* nr of sectors in partition */
1044 f3d54fc4 aliguori
} __attribute__((packed));
1045 f3d54fc4 aliguori
1046 f3d54fc4 aliguori
/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1047 f3d54fc4 aliguori
static int guess_disk_lchs(BlockDriverState *bs,
1048 f3d54fc4 aliguori
                           int *pcylinders, int *pheads, int *psectors)
1049 f3d54fc4 aliguori
{
1050 f3d54fc4 aliguori
    uint8_t buf[512];
1051 f3d54fc4 aliguori
    int ret, i, heads, sectors, cylinders;
1052 f3d54fc4 aliguori
    struct partition *p;
1053 f3d54fc4 aliguori
    uint32_t nr_sects;
1054 a38131b6 blueswir1
    uint64_t nb_sectors;
1055 f3d54fc4 aliguori
1056 f3d54fc4 aliguori
    bdrv_get_geometry(bs, &nb_sectors);
1057 f3d54fc4 aliguori
1058 f3d54fc4 aliguori
    ret = bdrv_read(bs, 0, buf, 1);
1059 f3d54fc4 aliguori
    if (ret < 0)
1060 f3d54fc4 aliguori
        return -1;
1061 f3d54fc4 aliguori
    /* test msdos magic */
1062 f3d54fc4 aliguori
    if (buf[510] != 0x55 || buf[511] != 0xaa)
1063 f3d54fc4 aliguori
        return -1;
1064 f3d54fc4 aliguori
    for(i = 0; i < 4; i++) {
1065 f3d54fc4 aliguori
        p = ((struct partition *)(buf + 0x1be)) + i;
1066 f3d54fc4 aliguori
        nr_sects = le32_to_cpu(p->nr_sects);
1067 f3d54fc4 aliguori
        if (nr_sects && p->end_head) {
1068 f3d54fc4 aliguori
            /* We make the assumption that the partition terminates on
1069 f3d54fc4 aliguori
               a cylinder boundary */
1070 f3d54fc4 aliguori
            heads = p->end_head + 1;
1071 f3d54fc4 aliguori
            sectors = p->end_sector & 63;
1072 f3d54fc4 aliguori
            if (sectors == 0)
1073 f3d54fc4 aliguori
                continue;
1074 f3d54fc4 aliguori
            cylinders = nb_sectors / (heads * sectors);
1075 f3d54fc4 aliguori
            if (cylinders < 1 || cylinders > 16383)
1076 f3d54fc4 aliguori
                continue;
1077 f3d54fc4 aliguori
            *pheads = heads;
1078 f3d54fc4 aliguori
            *psectors = sectors;
1079 f3d54fc4 aliguori
            *pcylinders = cylinders;
1080 f3d54fc4 aliguori
#if 0
1081 f3d54fc4 aliguori
            printf("guessed geometry: LCHS=%d %d %d\n",
1082 f3d54fc4 aliguori
                   cylinders, heads, sectors);
1083 f3d54fc4 aliguori
#endif
1084 f3d54fc4 aliguori
            return 0;
1085 f3d54fc4 aliguori
        }
1086 f3d54fc4 aliguori
    }
1087 f3d54fc4 aliguori
    return -1;
1088 f3d54fc4 aliguori
}
1089 f3d54fc4 aliguori
1090 f3d54fc4 aliguori
void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
1091 f3d54fc4 aliguori
{
1092 f3d54fc4 aliguori
    int translation, lba_detected = 0;
1093 f3d54fc4 aliguori
    int cylinders, heads, secs;
1094 a38131b6 blueswir1
    uint64_t nb_sectors;
1095 f3d54fc4 aliguori
1096 f3d54fc4 aliguori
    /* if a geometry hint is available, use it */
1097 f3d54fc4 aliguori
    bdrv_get_geometry(bs, &nb_sectors);
1098 f3d54fc4 aliguori
    bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
1099 f3d54fc4 aliguori
    translation = bdrv_get_translation_hint(bs);
1100 f3d54fc4 aliguori
    if (cylinders != 0) {
1101 f3d54fc4 aliguori
        *pcyls = cylinders;
1102 f3d54fc4 aliguori
        *pheads = heads;
1103 f3d54fc4 aliguori
        *psecs = secs;
1104 f3d54fc4 aliguori
    } else {
1105 f3d54fc4 aliguori
        if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
1106 f3d54fc4 aliguori
            if (heads > 16) {
1107 f3d54fc4 aliguori
                /* if heads > 16, it means that a BIOS LBA
1108 f3d54fc4 aliguori
                   translation was active, so the default
1109 f3d54fc4 aliguori
                   hardware geometry is OK */
1110 f3d54fc4 aliguori
                lba_detected = 1;
1111 f3d54fc4 aliguori
                goto default_geometry;
1112 f3d54fc4 aliguori
            } else {
1113 f3d54fc4 aliguori
                *pcyls = cylinders;
1114 f3d54fc4 aliguori
                *pheads = heads;
1115 f3d54fc4 aliguori
                *psecs = secs;
1116 f3d54fc4 aliguori
                /* disable any translation to be in sync with
1117 f3d54fc4 aliguori
                   the logical geometry */
1118 f3d54fc4 aliguori
                if (translation == BIOS_ATA_TRANSLATION_AUTO) {
1119 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
1120 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_NONE);
1121 f3d54fc4 aliguori
                }
1122 f3d54fc4 aliguori
            }
1123 f3d54fc4 aliguori
        } else {
1124 f3d54fc4 aliguori
        default_geometry:
1125 f3d54fc4 aliguori
            /* if no geometry, use a standard physical disk geometry */
1126 f3d54fc4 aliguori
            cylinders = nb_sectors / (16 * 63);
1127 f3d54fc4 aliguori
1128 f3d54fc4 aliguori
            if (cylinders > 16383)
1129 f3d54fc4 aliguori
                cylinders = 16383;
1130 f3d54fc4 aliguori
            else if (cylinders < 2)
1131 f3d54fc4 aliguori
                cylinders = 2;
1132 f3d54fc4 aliguori
            *pcyls = cylinders;
1133 f3d54fc4 aliguori
            *pheads = 16;
1134 f3d54fc4 aliguori
            *psecs = 63;
1135 f3d54fc4 aliguori
            if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
1136 f3d54fc4 aliguori
                if ((*pcyls * *pheads) <= 131072) {
1137 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
1138 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_LARGE);
1139 f3d54fc4 aliguori
                } else {
1140 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
1141 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_LBA);
1142 f3d54fc4 aliguori
                }
1143 f3d54fc4 aliguori
            }
1144 f3d54fc4 aliguori
        }
1145 f3d54fc4 aliguori
        bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
1146 f3d54fc4 aliguori
    }
1147 f3d54fc4 aliguori
}
1148 f3d54fc4 aliguori
1149 5fafdf24 ths
void bdrv_set_geometry_hint(BlockDriverState *bs,
1150 b338082b bellard
                            int cyls, int heads, int secs)
1151 b338082b bellard
{
1152 b338082b bellard
    bs->cyls = cyls;
1153 b338082b bellard
    bs->heads = heads;
1154 b338082b bellard
    bs->secs = secs;
1155 b338082b bellard
}
1156 b338082b bellard
1157 b338082b bellard
void bdrv_set_type_hint(BlockDriverState *bs, int type)
1158 b338082b bellard
{
1159 b338082b bellard
    bs->type = type;
1160 b338082b bellard
    bs->removable = ((type == BDRV_TYPE_CDROM ||
1161 b338082b bellard
                      type == BDRV_TYPE_FLOPPY));
1162 b338082b bellard
}
1163 b338082b bellard
1164 46d4767d bellard
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
1165 46d4767d bellard
{
1166 46d4767d bellard
    bs->translation = translation;
1167 46d4767d bellard
}
1168 46d4767d bellard
1169 5fafdf24 ths
void bdrv_get_geometry_hint(BlockDriverState *bs,
1170 b338082b bellard
                            int *pcyls, int *pheads, int *psecs)
1171 b338082b bellard
{
1172 b338082b bellard
    *pcyls = bs->cyls;
1173 b338082b bellard
    *pheads = bs->heads;
1174 b338082b bellard
    *psecs = bs->secs;
1175 b338082b bellard
}
1176 b338082b bellard
1177 b338082b bellard
int bdrv_get_type_hint(BlockDriverState *bs)
1178 b338082b bellard
{
1179 b338082b bellard
    return bs->type;
1180 b338082b bellard
}
1181 b338082b bellard
1182 46d4767d bellard
int bdrv_get_translation_hint(BlockDriverState *bs)
1183 46d4767d bellard
{
1184 46d4767d bellard
    return bs->translation;
1185 46d4767d bellard
}
1186 46d4767d bellard
1187 b338082b bellard
int bdrv_is_removable(BlockDriverState *bs)
1188 b338082b bellard
{
1189 b338082b bellard
    return bs->removable;
1190 b338082b bellard
}
1191 b338082b bellard
1192 b338082b bellard
int bdrv_is_read_only(BlockDriverState *bs)
1193 b338082b bellard
{
1194 b338082b bellard
    return bs->read_only;
1195 b338082b bellard
}
1196 b338082b bellard
1197 985a03b0 ths
int bdrv_is_sg(BlockDriverState *bs)
1198 985a03b0 ths
{
1199 985a03b0 ths
    return bs->sg;
1200 985a03b0 ths
}
1201 985a03b0 ths
1202 e900a7b7 Christoph Hellwig
int bdrv_enable_write_cache(BlockDriverState *bs)
1203 e900a7b7 Christoph Hellwig
{
1204 e900a7b7 Christoph Hellwig
    return bs->enable_write_cache;
1205 e900a7b7 Christoph Hellwig
}
1206 e900a7b7 Christoph Hellwig
1207 19cb3738 bellard
/* XXX: no longer used */
1208 5fafdf24 ths
void bdrv_set_change_cb(BlockDriverState *bs,
1209 b338082b bellard
                        void (*change_cb)(void *opaque), void *opaque)
1210 b338082b bellard
{
1211 b338082b bellard
    bs->change_cb = change_cb;
1212 b338082b bellard
    bs->change_opaque = opaque;
1213 b338082b bellard
}
1214 b338082b bellard
1215 ea2384d3 bellard
int bdrv_is_encrypted(BlockDriverState *bs)
1216 ea2384d3 bellard
{
1217 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted)
1218 ea2384d3 bellard
        return 1;
1219 ea2384d3 bellard
    return bs->encrypted;
1220 ea2384d3 bellard
}
1221 ea2384d3 bellard
1222 c0f4ce77 aliguori
int bdrv_key_required(BlockDriverState *bs)
1223 c0f4ce77 aliguori
{
1224 c0f4ce77 aliguori
    BlockDriverState *backing_hd = bs->backing_hd;
1225 c0f4ce77 aliguori
1226 c0f4ce77 aliguori
    if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
1227 c0f4ce77 aliguori
        return 1;
1228 c0f4ce77 aliguori
    return (bs->encrypted && !bs->valid_key);
1229 c0f4ce77 aliguori
}
1230 c0f4ce77 aliguori
1231 ea2384d3 bellard
int bdrv_set_key(BlockDriverState *bs, const char *key)
1232 ea2384d3 bellard
{
1233 ea2384d3 bellard
    int ret;
1234 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted) {
1235 ea2384d3 bellard
        ret = bdrv_set_key(bs->backing_hd, key);
1236 ea2384d3 bellard
        if (ret < 0)
1237 ea2384d3 bellard
            return ret;
1238 ea2384d3 bellard
        if (!bs->encrypted)
1239 ea2384d3 bellard
            return 0;
1240 ea2384d3 bellard
    }
1241 fd04a2ae Shahar Havivi
    if (!bs->encrypted) {
1242 fd04a2ae Shahar Havivi
        return -EINVAL;
1243 fd04a2ae Shahar Havivi
    } else if (!bs->drv || !bs->drv->bdrv_set_key) {
1244 fd04a2ae Shahar Havivi
        return -ENOMEDIUM;
1245 fd04a2ae Shahar Havivi
    }
1246 c0f4ce77 aliguori
    ret = bs->drv->bdrv_set_key(bs, key);
1247 bb5fc20f aliguori
    if (ret < 0) {
1248 bb5fc20f aliguori
        bs->valid_key = 0;
1249 bb5fc20f aliguori
    } else if (!bs->valid_key) {
1250 bb5fc20f aliguori
        bs->valid_key = 1;
1251 bb5fc20f aliguori
        /* call the change callback now, we skipped it on open */
1252 bb5fc20f aliguori
        bs->media_changed = 1;
1253 bb5fc20f aliguori
        if (bs->change_cb)
1254 bb5fc20f aliguori
            bs->change_cb(bs->change_opaque);
1255 bb5fc20f aliguori
    }
1256 c0f4ce77 aliguori
    return ret;
1257 ea2384d3 bellard
}
1258 ea2384d3 bellard
1259 ea2384d3 bellard
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
1260 ea2384d3 bellard
{
1261 19cb3738 bellard
    if (!bs->drv) {
1262 ea2384d3 bellard
        buf[0] = '\0';
1263 ea2384d3 bellard
    } else {
1264 ea2384d3 bellard
        pstrcpy(buf, buf_size, bs->drv->format_name);
1265 ea2384d3 bellard
    }
1266 ea2384d3 bellard
}
1267 ea2384d3 bellard
1268 5fafdf24 ths
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
1269 ea2384d3 bellard
                         void *opaque)
1270 ea2384d3 bellard
{
1271 ea2384d3 bellard
    BlockDriver *drv;
1272 ea2384d3 bellard
1273 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv, &bdrv_drivers, list) {
1274 ea2384d3 bellard
        it(opaque, drv->format_name);
1275 ea2384d3 bellard
    }
1276 ea2384d3 bellard
}
1277 ea2384d3 bellard
1278 b338082b bellard
BlockDriverState *bdrv_find(const char *name)
1279 b338082b bellard
{
1280 b338082b bellard
    BlockDriverState *bs;
1281 b338082b bellard
1282 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1283 1b7bdbc1 Stefan Hajnoczi
        if (!strcmp(name, bs->device_name)) {
1284 b338082b bellard
            return bs;
1285 1b7bdbc1 Stefan Hajnoczi
        }
1286 b338082b bellard
    }
1287 b338082b bellard
    return NULL;
1288 b338082b bellard
}
1289 b338082b bellard
1290 51de9760 aliguori
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
1291 81d0912d bellard
{
1292 81d0912d bellard
    BlockDriverState *bs;
1293 81d0912d bellard
1294 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1295 51de9760 aliguori
        it(opaque, bs);
1296 81d0912d bellard
    }
1297 81d0912d bellard
}
1298 81d0912d bellard
1299 ea2384d3 bellard
const char *bdrv_get_device_name(BlockDriverState *bs)
1300 ea2384d3 bellard
{
1301 ea2384d3 bellard
    return bs->device_name;
1302 ea2384d3 bellard
}
1303 ea2384d3 bellard
1304 7a6cba61 pbrook
void bdrv_flush(BlockDriverState *bs)
1305 7a6cba61 pbrook
{
1306 3f5075ae Christoph Hellwig
    if (bs->drv && bs->drv->bdrv_flush)
1307 7a6cba61 pbrook
        bs->drv->bdrv_flush(bs);
1308 7a6cba61 pbrook
}
1309 7a6cba61 pbrook
1310 c6ca28d6 aliguori
void bdrv_flush_all(void)
1311 c6ca28d6 aliguori
{
1312 c6ca28d6 aliguori
    BlockDriverState *bs;
1313 c6ca28d6 aliguori
1314 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1315 1b7bdbc1 Stefan Hajnoczi
        if (bs->drv && !bdrv_is_read_only(bs) &&
1316 1b7bdbc1 Stefan Hajnoczi
            (!bdrv_is_removable(bs) || bdrv_is_inserted(bs))) {
1317 c6ca28d6 aliguori
            bdrv_flush(bs);
1318 1b7bdbc1 Stefan Hajnoczi
        }
1319 1b7bdbc1 Stefan Hajnoczi
    }
1320 c6ca28d6 aliguori
}
1321 c6ca28d6 aliguori
1322 f2feebbd Kevin Wolf
int bdrv_has_zero_init(BlockDriverState *bs)
1323 f2feebbd Kevin Wolf
{
1324 f2feebbd Kevin Wolf
    assert(bs->drv);
1325 f2feebbd Kevin Wolf
1326 f2feebbd Kevin Wolf
    if (bs->drv->no_zero_init) {
1327 f2feebbd Kevin Wolf
        return 0;
1328 f2feebbd Kevin Wolf
    } else if (bs->file) {
1329 f2feebbd Kevin Wolf
        return bdrv_has_zero_init(bs->file);
1330 f2feebbd Kevin Wolf
    }
1331 f2feebbd Kevin Wolf
1332 f2feebbd Kevin Wolf
    return 1;
1333 f2feebbd Kevin Wolf
}
1334 f2feebbd Kevin Wolf
1335 f58c7b35 ths
/*
1336 f58c7b35 ths
 * Returns true iff the specified sector is present in the disk image. Drivers
1337 f58c7b35 ths
 * not implementing the functionality are assumed to not support backing files,
1338 f58c7b35 ths
 * hence all their sectors are reported as allocated.
1339 f58c7b35 ths
 *
1340 f58c7b35 ths
 * 'pnum' is set to the number of sectors (including and immediately following
1341 f58c7b35 ths
 * the specified sector) that are known to be in the same
1342 f58c7b35 ths
 * allocated/unallocated state.
1343 f58c7b35 ths
 *
1344 f58c7b35 ths
 * 'nb_sectors' is the max value 'pnum' should be set to.
1345 f58c7b35 ths
 */
1346 f58c7b35 ths
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
1347 f58c7b35 ths
        int *pnum)
1348 f58c7b35 ths
{
1349 f58c7b35 ths
    int64_t n;
1350 f58c7b35 ths
    if (!bs->drv->bdrv_is_allocated) {
1351 f58c7b35 ths
        if (sector_num >= bs->total_sectors) {
1352 f58c7b35 ths
            *pnum = 0;
1353 f58c7b35 ths
            return 0;
1354 f58c7b35 ths
        }
1355 f58c7b35 ths
        n = bs->total_sectors - sector_num;
1356 f58c7b35 ths
        *pnum = (n < nb_sectors) ? (n) : (nb_sectors);
1357 f58c7b35 ths
        return 1;
1358 f58c7b35 ths
    }
1359 f58c7b35 ths
    return bs->drv->bdrv_is_allocated(bs, sector_num, nb_sectors, pnum);
1360 f58c7b35 ths
}
1361 f58c7b35 ths
1362 2582bfed Luiz Capitulino
void bdrv_mon_event(const BlockDriverState *bdrv,
1363 2582bfed Luiz Capitulino
                    BlockMonEventAction action, int is_read)
1364 2582bfed Luiz Capitulino
{
1365 2582bfed Luiz Capitulino
    QObject *data;
1366 2582bfed Luiz Capitulino
    const char *action_str;
1367 2582bfed Luiz Capitulino
1368 2582bfed Luiz Capitulino
    switch (action) {
1369 2582bfed Luiz Capitulino
    case BDRV_ACTION_REPORT:
1370 2582bfed Luiz Capitulino
        action_str = "report";
1371 2582bfed Luiz Capitulino
        break;
1372 2582bfed Luiz Capitulino
    case BDRV_ACTION_IGNORE:
1373 2582bfed Luiz Capitulino
        action_str = "ignore";
1374 2582bfed Luiz Capitulino
        break;
1375 2582bfed Luiz Capitulino
    case BDRV_ACTION_STOP:
1376 2582bfed Luiz Capitulino
        action_str = "stop";
1377 2582bfed Luiz Capitulino
        break;
1378 2582bfed Luiz Capitulino
    default:
1379 2582bfed Luiz Capitulino
        abort();
1380 2582bfed Luiz Capitulino
    }
1381 2582bfed Luiz Capitulino
1382 2582bfed Luiz Capitulino
    data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1383 2582bfed Luiz Capitulino
                              bdrv->device_name,
1384 2582bfed Luiz Capitulino
                              action_str,
1385 2582bfed Luiz Capitulino
                              is_read ? "read" : "write");
1386 2582bfed Luiz Capitulino
    monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1387 2582bfed Luiz Capitulino
1388 2582bfed Luiz Capitulino
    qobject_decref(data);
1389 2582bfed Luiz Capitulino
}
1390 2582bfed Luiz Capitulino
1391 d15e5465 Luiz Capitulino
static void bdrv_print_dict(QObject *obj, void *opaque)
1392 b338082b bellard
{
1393 d15e5465 Luiz Capitulino
    QDict *bs_dict;
1394 d15e5465 Luiz Capitulino
    Monitor *mon = opaque;
1395 d15e5465 Luiz Capitulino
1396 d15e5465 Luiz Capitulino
    bs_dict = qobject_to_qdict(obj);
1397 d15e5465 Luiz Capitulino
1398 d15e5465 Luiz Capitulino
    monitor_printf(mon, "%s: type=%s removable=%d",
1399 d15e5465 Luiz Capitulino
                        qdict_get_str(bs_dict, "device"),
1400 d15e5465 Luiz Capitulino
                        qdict_get_str(bs_dict, "type"),
1401 d15e5465 Luiz Capitulino
                        qdict_get_bool(bs_dict, "removable"));
1402 d15e5465 Luiz Capitulino
1403 d15e5465 Luiz Capitulino
    if (qdict_get_bool(bs_dict, "removable")) {
1404 d15e5465 Luiz Capitulino
        monitor_printf(mon, " locked=%d", qdict_get_bool(bs_dict, "locked"));
1405 d15e5465 Luiz Capitulino
    }
1406 d15e5465 Luiz Capitulino
1407 d15e5465 Luiz Capitulino
    if (qdict_haskey(bs_dict, "inserted")) {
1408 d15e5465 Luiz Capitulino
        QDict *qdict = qobject_to_qdict(qdict_get(bs_dict, "inserted"));
1409 d15e5465 Luiz Capitulino
1410 d15e5465 Luiz Capitulino
        monitor_printf(mon, " file=");
1411 d15e5465 Luiz Capitulino
        monitor_print_filename(mon, qdict_get_str(qdict, "file"));
1412 d15e5465 Luiz Capitulino
        if (qdict_haskey(qdict, "backing_file")) {
1413 d15e5465 Luiz Capitulino
            monitor_printf(mon, " backing_file=");
1414 d15e5465 Luiz Capitulino
            monitor_print_filename(mon, qdict_get_str(qdict, "backing_file"));
1415 d15e5465 Luiz Capitulino
        }
1416 d15e5465 Luiz Capitulino
        monitor_printf(mon, " ro=%d drv=%s encrypted=%d",
1417 d15e5465 Luiz Capitulino
                            qdict_get_bool(qdict, "ro"),
1418 d15e5465 Luiz Capitulino
                            qdict_get_str(qdict, "drv"),
1419 d15e5465 Luiz Capitulino
                            qdict_get_bool(qdict, "encrypted"));
1420 d15e5465 Luiz Capitulino
    } else {
1421 d15e5465 Luiz Capitulino
        monitor_printf(mon, " [not inserted]");
1422 d15e5465 Luiz Capitulino
    }
1423 d15e5465 Luiz Capitulino
1424 d15e5465 Luiz Capitulino
    monitor_printf(mon, "\n");
1425 d15e5465 Luiz Capitulino
}
1426 d15e5465 Luiz Capitulino
1427 d15e5465 Luiz Capitulino
void bdrv_info_print(Monitor *mon, const QObject *data)
1428 d15e5465 Luiz Capitulino
{
1429 d15e5465 Luiz Capitulino
    qlist_iter(qobject_to_qlist(data), bdrv_print_dict, mon);
1430 d15e5465 Luiz Capitulino
}
1431 d15e5465 Luiz Capitulino
1432 d15e5465 Luiz Capitulino
/**
1433 d15e5465 Luiz Capitulino
 * bdrv_info(): Block devices information
1434 d15e5465 Luiz Capitulino
 *
1435 d15e5465 Luiz Capitulino
 * Each block device information is stored in a QDict and the
1436 d15e5465 Luiz Capitulino
 * returned QObject is a QList of all devices.
1437 d15e5465 Luiz Capitulino
 *
1438 d15e5465 Luiz Capitulino
 * The QDict contains the following:
1439 d15e5465 Luiz Capitulino
 *
1440 d15e5465 Luiz Capitulino
 * - "device": device name
1441 d15e5465 Luiz Capitulino
 * - "type": device type
1442 d15e5465 Luiz Capitulino
 * - "removable": true if the device is removable, false otherwise
1443 d15e5465 Luiz Capitulino
 * - "locked": true if the device is locked, false otherwise
1444 d15e5465 Luiz Capitulino
 * - "inserted": only present if the device is inserted, it is a QDict
1445 d15e5465 Luiz Capitulino
 *    containing the following:
1446 d15e5465 Luiz Capitulino
 *          - "file": device file name
1447 d15e5465 Luiz Capitulino
 *          - "ro": true if read-only, false otherwise
1448 d15e5465 Luiz Capitulino
 *          - "drv": driver format name
1449 d15e5465 Luiz Capitulino
 *          - "backing_file": backing file name if one is used
1450 d15e5465 Luiz Capitulino
 *          - "encrypted": true if encrypted, false otherwise
1451 d15e5465 Luiz Capitulino
 *
1452 d15e5465 Luiz Capitulino
 * Example:
1453 d15e5465 Luiz Capitulino
 *
1454 d15e5465 Luiz Capitulino
 * [ { "device": "ide0-hd0", "type": "hd", "removable": false, "locked": false,
1455 d15e5465 Luiz Capitulino
 *     "inserted": { "file": "/tmp/foobar", "ro": false, "drv": "qcow2" } },
1456 d15e5465 Luiz Capitulino
 *   { "device": "floppy0", "type": "floppy", "removable": true,
1457 d15e5465 Luiz Capitulino
 *     "locked": false } ]
1458 d15e5465 Luiz Capitulino
 */
1459 d15e5465 Luiz Capitulino
void bdrv_info(Monitor *mon, QObject **ret_data)
1460 d15e5465 Luiz Capitulino
{
1461 d15e5465 Luiz Capitulino
    QList *bs_list;
1462 b338082b bellard
    BlockDriverState *bs;
1463 b338082b bellard
1464 d15e5465 Luiz Capitulino
    bs_list = qlist_new();
1465 d15e5465 Luiz Capitulino
1466 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1467 d15e5465 Luiz Capitulino
        QObject *bs_obj;
1468 d15e5465 Luiz Capitulino
        const char *type = "unknown";
1469 d15e5465 Luiz Capitulino
1470 b338082b bellard
        switch(bs->type) {
1471 b338082b bellard
        case BDRV_TYPE_HD:
1472 d15e5465 Luiz Capitulino
            type = "hd";
1473 b338082b bellard
            break;
1474 b338082b bellard
        case BDRV_TYPE_CDROM:
1475 d15e5465 Luiz Capitulino
            type = "cdrom";
1476 b338082b bellard
            break;
1477 b338082b bellard
        case BDRV_TYPE_FLOPPY:
1478 d15e5465 Luiz Capitulino
            type = "floppy";
1479 b338082b bellard
            break;
1480 b338082b bellard
        }
1481 d15e5465 Luiz Capitulino
1482 d15e5465 Luiz Capitulino
        bs_obj = qobject_from_jsonf("{ 'device': %s, 'type': %s, "
1483 d15e5465 Luiz Capitulino
                                    "'removable': %i, 'locked': %i }",
1484 d15e5465 Luiz Capitulino
                                    bs->device_name, type, bs->removable,
1485 d15e5465 Luiz Capitulino
                                    bs->locked);
1486 d15e5465 Luiz Capitulino
1487 19cb3738 bellard
        if (bs->drv) {
1488 d15e5465 Luiz Capitulino
            QObject *obj;
1489 d15e5465 Luiz Capitulino
            QDict *bs_dict = qobject_to_qdict(bs_obj);
1490 d15e5465 Luiz Capitulino
1491 d15e5465 Luiz Capitulino
            obj = qobject_from_jsonf("{ 'file': %s, 'ro': %i, 'drv': %s, "
1492 d15e5465 Luiz Capitulino
                                     "'encrypted': %i }",
1493 d15e5465 Luiz Capitulino
                                     bs->filename, bs->read_only,
1494 d15e5465 Luiz Capitulino
                                     bs->drv->format_name,
1495 d15e5465 Luiz Capitulino
                                     bdrv_is_encrypted(bs));
1496 fef30743 ths
            if (bs->backing_file[0] != '\0') {
1497 d15e5465 Luiz Capitulino
                QDict *qdict = qobject_to_qdict(obj);
1498 d15e5465 Luiz Capitulino
                qdict_put(qdict, "backing_file",
1499 d15e5465 Luiz Capitulino
                          qstring_from_str(bs->backing_file));
1500 376253ec aliguori
            }
1501 d15e5465 Luiz Capitulino
1502 d15e5465 Luiz Capitulino
            qdict_put_obj(bs_dict, "inserted", obj);
1503 b338082b bellard
        }
1504 d15e5465 Luiz Capitulino
        qlist_append_obj(bs_list, bs_obj);
1505 b338082b bellard
    }
1506 d15e5465 Luiz Capitulino
1507 d15e5465 Luiz Capitulino
    *ret_data = QOBJECT(bs_list);
1508 b338082b bellard
}
1509 a36e69dd ths
1510 218a536a Luiz Capitulino
static void bdrv_stats_iter(QObject *data, void *opaque)
1511 a36e69dd ths
{
1512 218a536a Luiz Capitulino
    QDict *qdict;
1513 218a536a Luiz Capitulino
    Monitor *mon = opaque;
1514 218a536a Luiz Capitulino
1515 218a536a Luiz Capitulino
    qdict = qobject_to_qdict(data);
1516 218a536a Luiz Capitulino
    monitor_printf(mon, "%s:", qdict_get_str(qdict, "device"));
1517 218a536a Luiz Capitulino
1518 218a536a Luiz Capitulino
    qdict = qobject_to_qdict(qdict_get(qdict, "stats"));
1519 218a536a Luiz Capitulino
    monitor_printf(mon, " rd_bytes=%" PRId64
1520 218a536a Luiz Capitulino
                        " wr_bytes=%" PRId64
1521 218a536a Luiz Capitulino
                        " rd_operations=%" PRId64
1522 218a536a Luiz Capitulino
                        " wr_operations=%" PRId64
1523 218a536a Luiz Capitulino
                        "\n",
1524 218a536a Luiz Capitulino
                        qdict_get_int(qdict, "rd_bytes"),
1525 218a536a Luiz Capitulino
                        qdict_get_int(qdict, "wr_bytes"),
1526 218a536a Luiz Capitulino
                        qdict_get_int(qdict, "rd_operations"),
1527 218a536a Luiz Capitulino
                        qdict_get_int(qdict, "wr_operations"));
1528 218a536a Luiz Capitulino
}
1529 218a536a Luiz Capitulino
1530 218a536a Luiz Capitulino
void bdrv_stats_print(Monitor *mon, const QObject *data)
1531 218a536a Luiz Capitulino
{
1532 218a536a Luiz Capitulino
    qlist_iter(qobject_to_qlist(data), bdrv_stats_iter, mon);
1533 218a536a Luiz Capitulino
}
1534 218a536a Luiz Capitulino
1535 294cc35f Kevin Wolf
static QObject* bdrv_info_stats_bs(BlockDriverState *bs)
1536 294cc35f Kevin Wolf
{
1537 294cc35f Kevin Wolf
    QObject *res;
1538 294cc35f Kevin Wolf
    QDict *dict;
1539 294cc35f Kevin Wolf
1540 294cc35f Kevin Wolf
    res = qobject_from_jsonf("{ 'stats': {"
1541 294cc35f Kevin Wolf
                             "'rd_bytes': %" PRId64 ","
1542 294cc35f Kevin Wolf
                             "'wr_bytes': %" PRId64 ","
1543 294cc35f Kevin Wolf
                             "'rd_operations': %" PRId64 ","
1544 294cc35f Kevin Wolf
                             "'wr_operations': %" PRId64 ","
1545 294cc35f Kevin Wolf
                             "'wr_highest_offset': %" PRId64
1546 294cc35f Kevin Wolf
                             "} }",
1547 294cc35f Kevin Wolf
                             bs->rd_bytes, bs->wr_bytes,
1548 294cc35f Kevin Wolf
                             bs->rd_ops, bs->wr_ops,
1549 294cc35f Kevin Wolf
                             bs->wr_highest_sector * 512);
1550 294cc35f Kevin Wolf
    dict  = qobject_to_qdict(res);
1551 294cc35f Kevin Wolf
1552 294cc35f Kevin Wolf
    if (*bs->device_name) {
1553 294cc35f Kevin Wolf
        qdict_put(dict, "device", qstring_from_str(bs->device_name));
1554 294cc35f Kevin Wolf
    }
1555 294cc35f Kevin Wolf
1556 294cc35f Kevin Wolf
    if (bs->file) {
1557 294cc35f Kevin Wolf
        QObject *parent = bdrv_info_stats_bs(bs->file);
1558 294cc35f Kevin Wolf
        qdict_put_obj(dict, "parent", parent);
1559 294cc35f Kevin Wolf
    }
1560 294cc35f Kevin Wolf
1561 294cc35f Kevin Wolf
    return res;
1562 294cc35f Kevin Wolf
}
1563 294cc35f Kevin Wolf
1564 218a536a Luiz Capitulino
/**
1565 218a536a Luiz Capitulino
 * bdrv_info_stats(): show block device statistics
1566 218a536a Luiz Capitulino
 *
1567 218a536a Luiz Capitulino
 * Each device statistic information is stored in a QDict and
1568 218a536a Luiz Capitulino
 * the returned QObject is a QList of all devices.
1569 218a536a Luiz Capitulino
 *
1570 218a536a Luiz Capitulino
 * The QDict contains the following:
1571 218a536a Luiz Capitulino
 *
1572 218a536a Luiz Capitulino
 * - "device": device name
1573 218a536a Luiz Capitulino
 * - "stats": A QDict with the statistics information, it contains:
1574 218a536a Luiz Capitulino
 *     - "rd_bytes": bytes read
1575 218a536a Luiz Capitulino
 *     - "wr_bytes": bytes written
1576 218a536a Luiz Capitulino
 *     - "rd_operations": read operations
1577 218a536a Luiz Capitulino
 *     - "wr_operations": write operations
1578 294cc35f Kevin Wolf
 *     - "wr_highest_offset": Highest offset of a sector written since the
1579 294cc35f Kevin Wolf
 *       BlockDriverState has been opened
1580 21955137 Daniel P. Berrange
 * - "parent": A QDict recursively holding the statistics of the underlying
1581 21955137 Daniel P. Berrange
 *    protocol (e.g. the host file for a qcow2 image). If there is no
1582 21955137 Daniel P. Berrange
 *    underlying protocol, this field is omitted.
1583 294cc35f Kevin Wolf
 *
1584 218a536a Luiz Capitulino
 * Example:
1585 218a536a Luiz Capitulino
 *
1586 218a536a Luiz Capitulino
 * [ { "device": "ide0-hd0",
1587 218a536a Luiz Capitulino
 *               "stats": { "rd_bytes": 512,
1588 218a536a Luiz Capitulino
 *                          "wr_bytes": 0,
1589 218a536a Luiz Capitulino
 *                          "rd_operations": 1,
1590 294cc35f Kevin Wolf
 *                          "wr_operations": 0,
1591 21955137 Daniel P. Berrange
 *                          "wr_highest_offset": 0 },
1592 21955137 Daniel P. Berrange
 *               "parent": {
1593 21955137 Daniel P. Berrange
 *                      "stats": { "rd_bytes": 1024,
1594 21955137 Daniel P. Berrange
 *                                 "wr_bytes": 0,
1595 21955137 Daniel P. Berrange
 *                                 "rd_operations": 2,
1596 21955137 Daniel P. Berrange
 *                                 "wr_operations": 0,
1597 21955137 Daniel P. Berrange
 *                                 "wr_highest_offset": 0,
1598 21955137 Daniel P. Berrange
 *                      } } },
1599 218a536a Luiz Capitulino
 *   { "device": "ide1-cd0",
1600 218a536a Luiz Capitulino
 *               "stats": { "rd_bytes": 0,
1601 218a536a Luiz Capitulino
 *                          "wr_bytes": 0,
1602 218a536a Luiz Capitulino
 *                          "rd_operations": 0,
1603 294cc35f Kevin Wolf
 *                          "wr_operations": 0,
1604 294cc35f Kevin Wolf
 *                          "wr_highest_offset": 0 } },
1605 218a536a Luiz Capitulino
 */
1606 218a536a Luiz Capitulino
void bdrv_info_stats(Monitor *mon, QObject **ret_data)
1607 218a536a Luiz Capitulino
{
1608 218a536a Luiz Capitulino
    QObject *obj;
1609 218a536a Luiz Capitulino
    QList *devices;
1610 a36e69dd ths
    BlockDriverState *bs;
1611 a36e69dd ths
1612 218a536a Luiz Capitulino
    devices = qlist_new();
1613 218a536a Luiz Capitulino
1614 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1615 294cc35f Kevin Wolf
        obj = bdrv_info_stats_bs(bs);
1616 218a536a Luiz Capitulino
        qlist_append_obj(devices, obj);
1617 a36e69dd ths
    }
1618 218a536a Luiz Capitulino
1619 218a536a Luiz Capitulino
    *ret_data = QOBJECT(devices);
1620 a36e69dd ths
}
1621 ea2384d3 bellard
1622 045df330 aliguori
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
1623 045df330 aliguori
{
1624 045df330 aliguori
    if (bs->backing_hd && bs->backing_hd->encrypted)
1625 045df330 aliguori
        return bs->backing_file;
1626 045df330 aliguori
    else if (bs->encrypted)
1627 045df330 aliguori
        return bs->filename;
1628 045df330 aliguori
    else
1629 045df330 aliguori
        return NULL;
1630 045df330 aliguori
}
1631 045df330 aliguori
1632 5fafdf24 ths
void bdrv_get_backing_filename(BlockDriverState *bs,
1633 83f64091 bellard
                               char *filename, int filename_size)
1634 83f64091 bellard
{
1635 b783e409 Kevin Wolf
    if (!bs->backing_file) {
1636 83f64091 bellard
        pstrcpy(filename, filename_size, "");
1637 83f64091 bellard
    } else {
1638 83f64091 bellard
        pstrcpy(filename, filename_size, bs->backing_file);
1639 83f64091 bellard
    }
1640 83f64091 bellard
}
1641 83f64091 bellard
1642 5fafdf24 ths
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
1643 faea38e7 bellard
                          const uint8_t *buf, int nb_sectors)
1644 faea38e7 bellard
{
1645 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1646 faea38e7 bellard
    if (!drv)
1647 19cb3738 bellard
        return -ENOMEDIUM;
1648 faea38e7 bellard
    if (!drv->bdrv_write_compressed)
1649 faea38e7 bellard
        return -ENOTSUP;
1650 fbb7b4e0 Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1651 fbb7b4e0 Kevin Wolf
        return -EIO;
1652 a55eb92c Jan Kiszka
1653 c6d22830 Jan Kiszka
    if (bs->dirty_bitmap) {
1654 7cd1e32a lirans@il.ibm.com
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1655 7cd1e32a lirans@il.ibm.com
    }
1656 a55eb92c Jan Kiszka
1657 faea38e7 bellard
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
1658 faea38e7 bellard
}
1659 3b46e624 ths
1660 faea38e7 bellard
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
1661 faea38e7 bellard
{
1662 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1663 faea38e7 bellard
    if (!drv)
1664 19cb3738 bellard
        return -ENOMEDIUM;
1665 faea38e7 bellard
    if (!drv->bdrv_get_info)
1666 faea38e7 bellard
        return -ENOTSUP;
1667 faea38e7 bellard
    memset(bdi, 0, sizeof(*bdi));
1668 faea38e7 bellard
    return drv->bdrv_get_info(bs, bdi);
1669 faea38e7 bellard
}
1670 faea38e7 bellard
1671 45566e9c Christoph Hellwig
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
1672 45566e9c Christoph Hellwig
                      int64_t pos, int size)
1673 178e08a5 aliguori
{
1674 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
1675 178e08a5 aliguori
    if (!drv)
1676 178e08a5 aliguori
        return -ENOMEDIUM;
1677 45566e9c Christoph Hellwig
    if (!drv->bdrv_save_vmstate)
1678 178e08a5 aliguori
        return -ENOTSUP;
1679 45566e9c Christoph Hellwig
    return drv->bdrv_save_vmstate(bs, buf, pos, size);
1680 178e08a5 aliguori
}
1681 178e08a5 aliguori
1682 45566e9c Christoph Hellwig
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
1683 45566e9c Christoph Hellwig
                      int64_t pos, int size)
1684 178e08a5 aliguori
{
1685 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
1686 178e08a5 aliguori
    if (!drv)
1687 178e08a5 aliguori
        return -ENOMEDIUM;
1688 45566e9c Christoph Hellwig
    if (!drv->bdrv_load_vmstate)
1689 178e08a5 aliguori
        return -ENOTSUP;
1690 45566e9c Christoph Hellwig
    return drv->bdrv_load_vmstate(bs, buf, pos, size);
1691 178e08a5 aliguori
}
1692 178e08a5 aliguori
1693 8b9b0cc2 Kevin Wolf
void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
1694 8b9b0cc2 Kevin Wolf
{
1695 8b9b0cc2 Kevin Wolf
    BlockDriver *drv = bs->drv;
1696 8b9b0cc2 Kevin Wolf
1697 8b9b0cc2 Kevin Wolf
    if (!drv || !drv->bdrv_debug_event) {
1698 8b9b0cc2 Kevin Wolf
        return;
1699 8b9b0cc2 Kevin Wolf
    }
1700 8b9b0cc2 Kevin Wolf
1701 8b9b0cc2 Kevin Wolf
    return drv->bdrv_debug_event(bs, event);
1702 8b9b0cc2 Kevin Wolf
1703 8b9b0cc2 Kevin Wolf
}
1704 8b9b0cc2 Kevin Wolf
1705 faea38e7 bellard
/**************************************************************/
1706 faea38e7 bellard
/* handling of snapshots */
1707 faea38e7 bellard
1708 5fafdf24 ths
int bdrv_snapshot_create(BlockDriverState *bs,
1709 faea38e7 bellard
                         QEMUSnapshotInfo *sn_info)
1710 faea38e7 bellard
{
1711 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1712 faea38e7 bellard
    if (!drv)
1713 19cb3738 bellard
        return -ENOMEDIUM;
1714 faea38e7 bellard
    if (!drv->bdrv_snapshot_create)
1715 faea38e7 bellard
        return -ENOTSUP;
1716 faea38e7 bellard
    return drv->bdrv_snapshot_create(bs, sn_info);
1717 faea38e7 bellard
}
1718 faea38e7 bellard
1719 5fafdf24 ths
int bdrv_snapshot_goto(BlockDriverState *bs,
1720 faea38e7 bellard
                       const char *snapshot_id)
1721 faea38e7 bellard
{
1722 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1723 faea38e7 bellard
    if (!drv)
1724 19cb3738 bellard
        return -ENOMEDIUM;
1725 faea38e7 bellard
    if (!drv->bdrv_snapshot_goto)
1726 faea38e7 bellard
        return -ENOTSUP;
1727 faea38e7 bellard
    return drv->bdrv_snapshot_goto(bs, snapshot_id);
1728 faea38e7 bellard
}
1729 faea38e7 bellard
1730 faea38e7 bellard
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
1731 faea38e7 bellard
{
1732 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1733 faea38e7 bellard
    if (!drv)
1734 19cb3738 bellard
        return -ENOMEDIUM;
1735 faea38e7 bellard
    if (!drv->bdrv_snapshot_delete)
1736 faea38e7 bellard
        return -ENOTSUP;
1737 faea38e7 bellard
    return drv->bdrv_snapshot_delete(bs, snapshot_id);
1738 faea38e7 bellard
}
1739 faea38e7 bellard
1740 5fafdf24 ths
int bdrv_snapshot_list(BlockDriverState *bs,
1741 faea38e7 bellard
                       QEMUSnapshotInfo **psn_info)
1742 faea38e7 bellard
{
1743 faea38e7 bellard
    BlockDriver *drv = bs->drv;
1744 faea38e7 bellard
    if (!drv)
1745 19cb3738 bellard
        return -ENOMEDIUM;
1746 faea38e7 bellard
    if (!drv->bdrv_snapshot_list)
1747 faea38e7 bellard
        return -ENOTSUP;
1748 faea38e7 bellard
    return drv->bdrv_snapshot_list(bs, psn_info);
1749 faea38e7 bellard
}
1750 faea38e7 bellard
1751 faea38e7 bellard
#define NB_SUFFIXES 4
1752 faea38e7 bellard
1753 faea38e7 bellard
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
1754 faea38e7 bellard
{
1755 faea38e7 bellard
    static const char suffixes[NB_SUFFIXES] = "KMGT";
1756 faea38e7 bellard
    int64_t base;
1757 faea38e7 bellard
    int i;
1758 faea38e7 bellard
1759 faea38e7 bellard
    if (size <= 999) {
1760 faea38e7 bellard
        snprintf(buf, buf_size, "%" PRId64, size);
1761 faea38e7 bellard
    } else {
1762 faea38e7 bellard
        base = 1024;
1763 faea38e7 bellard
        for(i = 0; i < NB_SUFFIXES; i++) {
1764 faea38e7 bellard
            if (size < (10 * base)) {
1765 5fafdf24 ths
                snprintf(buf, buf_size, "%0.1f%c",
1766 faea38e7 bellard
                         (double)size / base,
1767 faea38e7 bellard
                         suffixes[i]);
1768 faea38e7 bellard
                break;
1769 faea38e7 bellard
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
1770 5fafdf24 ths
                snprintf(buf, buf_size, "%" PRId64 "%c",
1771 faea38e7 bellard
                         ((size + (base >> 1)) / base),
1772 faea38e7 bellard
                         suffixes[i]);
1773 faea38e7 bellard
                break;
1774 faea38e7 bellard
            }
1775 faea38e7 bellard
            base = base * 1024;
1776 faea38e7 bellard
        }
1777 faea38e7 bellard
    }
1778 faea38e7 bellard
    return buf;
1779 faea38e7 bellard
}
1780 faea38e7 bellard
1781 faea38e7 bellard
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
1782 faea38e7 bellard
{
1783 faea38e7 bellard
    char buf1[128], date_buf[128], clock_buf[128];
1784 3b9f94e1 bellard
#ifdef _WIN32
1785 3b9f94e1 bellard
    struct tm *ptm;
1786 3b9f94e1 bellard
#else
1787 faea38e7 bellard
    struct tm tm;
1788 3b9f94e1 bellard
#endif
1789 faea38e7 bellard
    time_t ti;
1790 faea38e7 bellard
    int64_t secs;
1791 faea38e7 bellard
1792 faea38e7 bellard
    if (!sn) {
1793 5fafdf24 ths
        snprintf(buf, buf_size,
1794 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
1795 faea38e7 bellard
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
1796 faea38e7 bellard
    } else {
1797 faea38e7 bellard
        ti = sn->date_sec;
1798 3b9f94e1 bellard
#ifdef _WIN32
1799 3b9f94e1 bellard
        ptm = localtime(&ti);
1800 3b9f94e1 bellard
        strftime(date_buf, sizeof(date_buf),
1801 3b9f94e1 bellard
                 "%Y-%m-%d %H:%M:%S", ptm);
1802 3b9f94e1 bellard
#else
1803 faea38e7 bellard
        localtime_r(&ti, &tm);
1804 faea38e7 bellard
        strftime(date_buf, sizeof(date_buf),
1805 faea38e7 bellard
                 "%Y-%m-%d %H:%M:%S", &tm);
1806 3b9f94e1 bellard
#endif
1807 faea38e7 bellard
        secs = sn->vm_clock_nsec / 1000000000;
1808 faea38e7 bellard
        snprintf(clock_buf, sizeof(clock_buf),
1809 faea38e7 bellard
                 "%02d:%02d:%02d.%03d",
1810 faea38e7 bellard
                 (int)(secs / 3600),
1811 faea38e7 bellard
                 (int)((secs / 60) % 60),
1812 5fafdf24 ths
                 (int)(secs % 60),
1813 faea38e7 bellard
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
1814 faea38e7 bellard
        snprintf(buf, buf_size,
1815 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
1816 faea38e7 bellard
                 sn->id_str, sn->name,
1817 faea38e7 bellard
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
1818 faea38e7 bellard
                 date_buf,
1819 faea38e7 bellard
                 clock_buf);
1820 faea38e7 bellard
    }
1821 faea38e7 bellard
    return buf;
1822 faea38e7 bellard
}
1823 faea38e7 bellard
1824 83f64091 bellard
1825 ea2384d3 bellard
/**************************************************************/
1826 83f64091 bellard
/* async I/Os */
1827 ea2384d3 bellard
1828 3b69e4b9 aliguori
BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
1829 f141eafe aliguori
                                 QEMUIOVector *qiov, int nb_sectors,
1830 3b69e4b9 aliguori
                                 BlockDriverCompletionFunc *cb, void *opaque)
1831 3b69e4b9 aliguori
{
1832 83f64091 bellard
    BlockDriver *drv = bs->drv;
1833 a36e69dd ths
    BlockDriverAIOCB *ret;
1834 83f64091 bellard
1835 19cb3738 bellard
    if (!drv)
1836 ce1a14dc pbrook
        return NULL;
1837 71d0770c aliguori
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1838 71d0770c aliguori
        return NULL;
1839 3b46e624 ths
1840 f141eafe aliguori
    ret = drv->bdrv_aio_readv(bs, sector_num, qiov, nb_sectors,
1841 f141eafe aliguori
                              cb, opaque);
1842 a36e69dd ths
1843 a36e69dd ths
    if (ret) {
1844 a36e69dd ths
        /* Update stats even though technically transfer has not happened. */
1845 6ea44308 Jan Kiszka
        bs->rd_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1846 a36e69dd ths
        bs->rd_ops ++;
1847 a36e69dd ths
    }
1848 a36e69dd ths
1849 a36e69dd ths
    return ret;
1850 ea2384d3 bellard
}
1851 ea2384d3 bellard
1852 f141eafe aliguori
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
1853 f141eafe aliguori
                                  QEMUIOVector *qiov, int nb_sectors,
1854 f141eafe aliguori
                                  BlockDriverCompletionFunc *cb, void *opaque)
1855 ea2384d3 bellard
{
1856 83f64091 bellard
    BlockDriver *drv = bs->drv;
1857 a36e69dd ths
    BlockDriverAIOCB *ret;
1858 ea2384d3 bellard
1859 19cb3738 bellard
    if (!drv)
1860 ce1a14dc pbrook
        return NULL;
1861 83f64091 bellard
    if (bs->read_only)
1862 ce1a14dc pbrook
        return NULL;
1863 71d0770c aliguori
    if (bdrv_check_request(bs, sector_num, nb_sectors))
1864 71d0770c aliguori
        return NULL;
1865 83f64091 bellard
1866 c6d22830 Jan Kiszka
    if (bs->dirty_bitmap) {
1867 7cd1e32a lirans@il.ibm.com
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1868 7cd1e32a lirans@il.ibm.com
    }
1869 a55eb92c Jan Kiszka
1870 f141eafe aliguori
    ret = drv->bdrv_aio_writev(bs, sector_num, qiov, nb_sectors,
1871 f141eafe aliguori
                               cb, opaque);
1872 a36e69dd ths
1873 a36e69dd ths
    if (ret) {
1874 294cc35f Kevin Wolf
        /* Update stats even though technically transfer has not happened. */
1875 294cc35f Kevin Wolf
        bs->wr_bytes += (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
1876 294cc35f Kevin Wolf
        bs->wr_ops ++;
1877 294cc35f Kevin Wolf
        if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1878 294cc35f Kevin Wolf
            bs->wr_highest_sector = sector_num + nb_sectors - 1;
1879 294cc35f Kevin Wolf
        }
1880 a36e69dd ths
    }
1881 a36e69dd ths
1882 a36e69dd ths
    return ret;
1883 83f64091 bellard
}
1884 83f64091 bellard
1885 40b4f539 Kevin Wolf
1886 40b4f539 Kevin Wolf
typedef struct MultiwriteCB {
1887 40b4f539 Kevin Wolf
    int error;
1888 40b4f539 Kevin Wolf
    int num_requests;
1889 40b4f539 Kevin Wolf
    int num_callbacks;
1890 40b4f539 Kevin Wolf
    struct {
1891 40b4f539 Kevin Wolf
        BlockDriverCompletionFunc *cb;
1892 40b4f539 Kevin Wolf
        void *opaque;
1893 40b4f539 Kevin Wolf
        QEMUIOVector *free_qiov;
1894 40b4f539 Kevin Wolf
        void *free_buf;
1895 40b4f539 Kevin Wolf
    } callbacks[];
1896 40b4f539 Kevin Wolf
} MultiwriteCB;
1897 40b4f539 Kevin Wolf
1898 40b4f539 Kevin Wolf
static void multiwrite_user_cb(MultiwriteCB *mcb)
1899 40b4f539 Kevin Wolf
{
1900 40b4f539 Kevin Wolf
    int i;
1901 40b4f539 Kevin Wolf
1902 40b4f539 Kevin Wolf
    for (i = 0; i < mcb->num_callbacks; i++) {
1903 40b4f539 Kevin Wolf
        mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
1904 1e1ea48d Stefan Hajnoczi
        if (mcb->callbacks[i].free_qiov) {
1905 1e1ea48d Stefan Hajnoczi
            qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
1906 1e1ea48d Stefan Hajnoczi
        }
1907 40b4f539 Kevin Wolf
        qemu_free(mcb->callbacks[i].free_qiov);
1908 f8a83245 Herve Poussineau
        qemu_vfree(mcb->callbacks[i].free_buf);
1909 40b4f539 Kevin Wolf
    }
1910 40b4f539 Kevin Wolf
}
1911 40b4f539 Kevin Wolf
1912 40b4f539 Kevin Wolf
static void multiwrite_cb(void *opaque, int ret)
1913 40b4f539 Kevin Wolf
{
1914 40b4f539 Kevin Wolf
    MultiwriteCB *mcb = opaque;
1915 40b4f539 Kevin Wolf
1916 cb6d3ca0 Kevin Wolf
    if (ret < 0 && !mcb->error) {
1917 40b4f539 Kevin Wolf
        mcb->error = ret;
1918 40b4f539 Kevin Wolf
        multiwrite_user_cb(mcb);
1919 40b4f539 Kevin Wolf
    }
1920 40b4f539 Kevin Wolf
1921 40b4f539 Kevin Wolf
    mcb->num_requests--;
1922 40b4f539 Kevin Wolf
    if (mcb->num_requests == 0) {
1923 40b4f539 Kevin Wolf
        if (mcb->error == 0) {
1924 40b4f539 Kevin Wolf
            multiwrite_user_cb(mcb);
1925 40b4f539 Kevin Wolf
        }
1926 40b4f539 Kevin Wolf
        qemu_free(mcb);
1927 40b4f539 Kevin Wolf
    }
1928 40b4f539 Kevin Wolf
}
1929 40b4f539 Kevin Wolf
1930 40b4f539 Kevin Wolf
static int multiwrite_req_compare(const void *a, const void *b)
1931 40b4f539 Kevin Wolf
{
1932 40b4f539 Kevin Wolf
    return (((BlockRequest*) a)->sector - ((BlockRequest*) b)->sector);
1933 40b4f539 Kevin Wolf
}
1934 40b4f539 Kevin Wolf
1935 40b4f539 Kevin Wolf
/*
1936 40b4f539 Kevin Wolf
 * Takes a bunch of requests and tries to merge them. Returns the number of
1937 40b4f539 Kevin Wolf
 * requests that remain after merging.
1938 40b4f539 Kevin Wolf
 */
1939 40b4f539 Kevin Wolf
static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
1940 40b4f539 Kevin Wolf
    int num_reqs, MultiwriteCB *mcb)
1941 40b4f539 Kevin Wolf
{
1942 40b4f539 Kevin Wolf
    int i, outidx;
1943 40b4f539 Kevin Wolf
1944 40b4f539 Kevin Wolf
    // Sort requests by start sector
1945 40b4f539 Kevin Wolf
    qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
1946 40b4f539 Kevin Wolf
1947 40b4f539 Kevin Wolf
    // Check if adjacent requests touch the same clusters. If so, combine them,
1948 40b4f539 Kevin Wolf
    // filling up gaps with zero sectors.
1949 40b4f539 Kevin Wolf
    outidx = 0;
1950 40b4f539 Kevin Wolf
    for (i = 1; i < num_reqs; i++) {
1951 40b4f539 Kevin Wolf
        int merge = 0;
1952 40b4f539 Kevin Wolf
        int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
1953 40b4f539 Kevin Wolf
1954 40b4f539 Kevin Wolf
        // This handles the cases that are valid for all block drivers, namely
1955 40b4f539 Kevin Wolf
        // exactly sequential writes and overlapping writes.
1956 40b4f539 Kevin Wolf
        if (reqs[i].sector <= oldreq_last) {
1957 40b4f539 Kevin Wolf
            merge = 1;
1958 40b4f539 Kevin Wolf
        }
1959 40b4f539 Kevin Wolf
1960 40b4f539 Kevin Wolf
        // The block driver may decide that it makes sense to combine requests
1961 40b4f539 Kevin Wolf
        // even if there is a gap of some sectors between them. In this case,
1962 40b4f539 Kevin Wolf
        // the gap is filled with zeros (therefore only applicable for yet
1963 40b4f539 Kevin Wolf
        // unused space in format like qcow2).
1964 40b4f539 Kevin Wolf
        if (!merge && bs->drv->bdrv_merge_requests) {
1965 40b4f539 Kevin Wolf
            merge = bs->drv->bdrv_merge_requests(bs, &reqs[outidx], &reqs[i]);
1966 40b4f539 Kevin Wolf
        }
1967 40b4f539 Kevin Wolf
1968 e2a305fb Christoph Hellwig
        if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
1969 e2a305fb Christoph Hellwig
            merge = 0;
1970 e2a305fb Christoph Hellwig
        }
1971 e2a305fb Christoph Hellwig
1972 40b4f539 Kevin Wolf
        if (merge) {
1973 40b4f539 Kevin Wolf
            size_t size;
1974 40b4f539 Kevin Wolf
            QEMUIOVector *qiov = qemu_mallocz(sizeof(*qiov));
1975 40b4f539 Kevin Wolf
            qemu_iovec_init(qiov,
1976 40b4f539 Kevin Wolf
                reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
1977 40b4f539 Kevin Wolf
1978 40b4f539 Kevin Wolf
            // Add the first request to the merged one. If the requests are
1979 40b4f539 Kevin Wolf
            // overlapping, drop the last sectors of the first request.
1980 40b4f539 Kevin Wolf
            size = (reqs[i].sector - reqs[outidx].sector) << 9;
1981 40b4f539 Kevin Wolf
            qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
1982 40b4f539 Kevin Wolf
1983 40b4f539 Kevin Wolf
            // We might need to add some zeros between the two requests
1984 40b4f539 Kevin Wolf
            if (reqs[i].sector > oldreq_last) {
1985 40b4f539 Kevin Wolf
                size_t zero_bytes = (reqs[i].sector - oldreq_last) << 9;
1986 40b4f539 Kevin Wolf
                uint8_t *buf = qemu_blockalign(bs, zero_bytes);
1987 40b4f539 Kevin Wolf
                memset(buf, 0, zero_bytes);
1988 40b4f539 Kevin Wolf
                qemu_iovec_add(qiov, buf, zero_bytes);
1989 40b4f539 Kevin Wolf
                mcb->callbacks[i].free_buf = buf;
1990 40b4f539 Kevin Wolf
            }
1991 40b4f539 Kevin Wolf
1992 40b4f539 Kevin Wolf
            // Add the second request
1993 40b4f539 Kevin Wolf
            qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
1994 40b4f539 Kevin Wolf
1995 40b4f539 Kevin Wolf
            reqs[outidx].nb_sectors += reqs[i].nb_sectors;
1996 40b4f539 Kevin Wolf
            reqs[outidx].qiov = qiov;
1997 40b4f539 Kevin Wolf
1998 40b4f539 Kevin Wolf
            mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
1999 40b4f539 Kevin Wolf
        } else {
2000 40b4f539 Kevin Wolf
            outidx++;
2001 40b4f539 Kevin Wolf
            reqs[outidx].sector     = reqs[i].sector;
2002 40b4f539 Kevin Wolf
            reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2003 40b4f539 Kevin Wolf
            reqs[outidx].qiov       = reqs[i].qiov;
2004 40b4f539 Kevin Wolf
        }
2005 40b4f539 Kevin Wolf
    }
2006 40b4f539 Kevin Wolf
2007 40b4f539 Kevin Wolf
    return outidx + 1;
2008 40b4f539 Kevin Wolf
}
2009 40b4f539 Kevin Wolf
2010 40b4f539 Kevin Wolf
/*
2011 40b4f539 Kevin Wolf
 * Submit multiple AIO write requests at once.
2012 40b4f539 Kevin Wolf
 *
2013 40b4f539 Kevin Wolf
 * On success, the function returns 0 and all requests in the reqs array have
2014 40b4f539 Kevin Wolf
 * been submitted. In error case this function returns -1, and any of the
2015 40b4f539 Kevin Wolf
 * requests may or may not be submitted yet. In particular, this means that the
2016 40b4f539 Kevin Wolf
 * callback will be called for some of the requests, for others it won't. The
2017 40b4f539 Kevin Wolf
 * caller must check the error field of the BlockRequest to wait for the right
2018 40b4f539 Kevin Wolf
 * callbacks (if error != 0, no callback will be called).
2019 40b4f539 Kevin Wolf
 *
2020 40b4f539 Kevin Wolf
 * The implementation may modify the contents of the reqs array, e.g. to merge
2021 40b4f539 Kevin Wolf
 * requests. However, the fields opaque and error are left unmodified as they
2022 40b4f539 Kevin Wolf
 * are used to signal failure for a single request to the caller.
2023 40b4f539 Kevin Wolf
 */
2024 40b4f539 Kevin Wolf
int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
2025 40b4f539 Kevin Wolf
{
2026 40b4f539 Kevin Wolf
    BlockDriverAIOCB *acb;
2027 40b4f539 Kevin Wolf
    MultiwriteCB *mcb;
2028 40b4f539 Kevin Wolf
    int i;
2029 40b4f539 Kevin Wolf
2030 40b4f539 Kevin Wolf
    if (num_reqs == 0) {
2031 40b4f539 Kevin Wolf
        return 0;
2032 40b4f539 Kevin Wolf
    }
2033 40b4f539 Kevin Wolf
2034 40b4f539 Kevin Wolf
    // Create MultiwriteCB structure
2035 40b4f539 Kevin Wolf
    mcb = qemu_mallocz(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
2036 40b4f539 Kevin Wolf
    mcb->num_requests = 0;
2037 40b4f539 Kevin Wolf
    mcb->num_callbacks = num_reqs;
2038 40b4f539 Kevin Wolf
2039 40b4f539 Kevin Wolf
    for (i = 0; i < num_reqs; i++) {
2040 40b4f539 Kevin Wolf
        mcb->callbacks[i].cb = reqs[i].cb;
2041 40b4f539 Kevin Wolf
        mcb->callbacks[i].opaque = reqs[i].opaque;
2042 40b4f539 Kevin Wolf
    }
2043 40b4f539 Kevin Wolf
2044 40b4f539 Kevin Wolf
    // Check for mergable requests
2045 40b4f539 Kevin Wolf
    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
2046 40b4f539 Kevin Wolf
2047 40b4f539 Kevin Wolf
    // Run the aio requests
2048 40b4f539 Kevin Wolf
    for (i = 0; i < num_reqs; i++) {
2049 40b4f539 Kevin Wolf
        acb = bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
2050 40b4f539 Kevin Wolf
            reqs[i].nb_sectors, multiwrite_cb, mcb);
2051 40b4f539 Kevin Wolf
2052 40b4f539 Kevin Wolf
        if (acb == NULL) {
2053 40b4f539 Kevin Wolf
            // We can only fail the whole thing if no request has been
2054 40b4f539 Kevin Wolf
            // submitted yet. Otherwise we'll wait for the submitted AIOs to
2055 40b4f539 Kevin Wolf
            // complete and report the error in the callback.
2056 40b4f539 Kevin Wolf
            if (mcb->num_requests == 0) {
2057 0f0b604b Kevin Wolf
                reqs[i].error = -EIO;
2058 40b4f539 Kevin Wolf
                goto fail;
2059 40b4f539 Kevin Wolf
            } else {
2060 7eb58a6c Kevin Wolf
                mcb->num_requests++;
2061 7eb58a6c Kevin Wolf
                multiwrite_cb(mcb, -EIO);
2062 40b4f539 Kevin Wolf
                break;
2063 40b4f539 Kevin Wolf
            }
2064 40b4f539 Kevin Wolf
        } else {
2065 40b4f539 Kevin Wolf
            mcb->num_requests++;
2066 40b4f539 Kevin Wolf
        }
2067 40b4f539 Kevin Wolf
    }
2068 40b4f539 Kevin Wolf
2069 40b4f539 Kevin Wolf
    return 0;
2070 40b4f539 Kevin Wolf
2071 40b4f539 Kevin Wolf
fail:
2072 af474591 Bruce Rogers
    qemu_free(mcb);
2073 40b4f539 Kevin Wolf
    return -1;
2074 40b4f539 Kevin Wolf
}
2075 40b4f539 Kevin Wolf
2076 b2e12bc6 Christoph Hellwig
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
2077 b2e12bc6 Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque)
2078 b2e12bc6 Christoph Hellwig
{
2079 b2e12bc6 Christoph Hellwig
    BlockDriver *drv = bs->drv;
2080 b2e12bc6 Christoph Hellwig
2081 b2e12bc6 Christoph Hellwig
    if (!drv)
2082 b2e12bc6 Christoph Hellwig
        return NULL;
2083 b2e12bc6 Christoph Hellwig
    return drv->bdrv_aio_flush(bs, cb, opaque);
2084 b2e12bc6 Christoph Hellwig
}
2085 b2e12bc6 Christoph Hellwig
2086 83f64091 bellard
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
2087 83f64091 bellard
{
2088 6bbff9a0 aliguori
    acb->pool->cancel(acb);
2089 83f64091 bellard
}
2090 83f64091 bellard
2091 ce1a14dc pbrook
2092 83f64091 bellard
/**************************************************************/
2093 83f64091 bellard
/* async block device emulation */
2094 83f64091 bellard
2095 c16b5a2c Christoph Hellwig
typedef struct BlockDriverAIOCBSync {
2096 c16b5a2c Christoph Hellwig
    BlockDriverAIOCB common;
2097 c16b5a2c Christoph Hellwig
    QEMUBH *bh;
2098 c16b5a2c Christoph Hellwig
    int ret;
2099 c16b5a2c Christoph Hellwig
    /* vector translation state */
2100 c16b5a2c Christoph Hellwig
    QEMUIOVector *qiov;
2101 c16b5a2c Christoph Hellwig
    uint8_t *bounce;
2102 c16b5a2c Christoph Hellwig
    int is_write;
2103 c16b5a2c Christoph Hellwig
} BlockDriverAIOCBSync;
2104 c16b5a2c Christoph Hellwig
2105 c16b5a2c Christoph Hellwig
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
2106 c16b5a2c Christoph Hellwig
{
2107 b666d239 Kevin Wolf
    BlockDriverAIOCBSync *acb =
2108 b666d239 Kevin Wolf
        container_of(blockacb, BlockDriverAIOCBSync, common);
2109 6a7ad299 Dor Laor
    qemu_bh_delete(acb->bh);
2110 36afc451 Avi Kivity
    acb->bh = NULL;
2111 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
2112 c16b5a2c Christoph Hellwig
}
2113 c16b5a2c Christoph Hellwig
2114 c16b5a2c Christoph Hellwig
static AIOPool bdrv_em_aio_pool = {
2115 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(BlockDriverAIOCBSync),
2116 c16b5a2c Christoph Hellwig
    .cancel             = bdrv_aio_cancel_em,
2117 c16b5a2c Christoph Hellwig
};
2118 c16b5a2c Christoph Hellwig
2119 ce1a14dc pbrook
static void bdrv_aio_bh_cb(void *opaque)
2120 83f64091 bellard
{
2121 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb = opaque;
2122 f141eafe aliguori
2123 f141eafe aliguori
    if (!acb->is_write)
2124 f141eafe aliguori
        qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
2125 ceb42de8 aliguori
    qemu_vfree(acb->bounce);
2126 ce1a14dc pbrook
    acb->common.cb(acb->common.opaque, acb->ret);
2127 6a7ad299 Dor Laor
    qemu_bh_delete(acb->bh);
2128 36afc451 Avi Kivity
    acb->bh = NULL;
2129 ce1a14dc pbrook
    qemu_aio_release(acb);
2130 83f64091 bellard
}
2131 beac80cd bellard
2132 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
2133 f141eafe aliguori
                                            int64_t sector_num,
2134 f141eafe aliguori
                                            QEMUIOVector *qiov,
2135 f141eafe aliguori
                                            int nb_sectors,
2136 f141eafe aliguori
                                            BlockDriverCompletionFunc *cb,
2137 f141eafe aliguori
                                            void *opaque,
2138 f141eafe aliguori
                                            int is_write)
2139 f141eafe aliguori
2140 83f64091 bellard
{
2141 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb;
2142 ce1a14dc pbrook
2143 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2144 f141eafe aliguori
    acb->is_write = is_write;
2145 f141eafe aliguori
    acb->qiov = qiov;
2146 e268ca52 aliguori
    acb->bounce = qemu_blockalign(bs, qiov->size);
2147 f141eafe aliguori
2148 ce1a14dc pbrook
    if (!acb->bh)
2149 ce1a14dc pbrook
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2150 f141eafe aliguori
2151 f141eafe aliguori
    if (is_write) {
2152 f141eafe aliguori
        qemu_iovec_to_buffer(acb->qiov, acb->bounce);
2153 f141eafe aliguori
        acb->ret = bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
2154 f141eafe aliguori
    } else {
2155 f141eafe aliguori
        acb->ret = bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
2156 f141eafe aliguori
    }
2157 f141eafe aliguori
2158 ce1a14dc pbrook
    qemu_bh_schedule(acb->bh);
2159 f141eafe aliguori
2160 ce1a14dc pbrook
    return &acb->common;
2161 beac80cd bellard
}
2162 beac80cd bellard
2163 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
2164 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2165 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
2166 beac80cd bellard
{
2167 f141eafe aliguori
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
2168 f141eafe aliguori
}
2169 83f64091 bellard
2170 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
2171 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
2172 f141eafe aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
2173 f141eafe aliguori
{
2174 f141eafe aliguori
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
2175 beac80cd bellard
}
2176 beac80cd bellard
2177 b2e12bc6 Christoph Hellwig
static BlockDriverAIOCB *bdrv_aio_flush_em(BlockDriverState *bs,
2178 b2e12bc6 Christoph Hellwig
        BlockDriverCompletionFunc *cb, void *opaque)
2179 b2e12bc6 Christoph Hellwig
{
2180 b2e12bc6 Christoph Hellwig
    BlockDriverAIOCBSync *acb;
2181 b2e12bc6 Christoph Hellwig
2182 b2e12bc6 Christoph Hellwig
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
2183 b2e12bc6 Christoph Hellwig
    acb->is_write = 1; /* don't bounce in the completion hadler */
2184 b2e12bc6 Christoph Hellwig
    acb->qiov = NULL;
2185 b2e12bc6 Christoph Hellwig
    acb->bounce = NULL;
2186 b2e12bc6 Christoph Hellwig
    acb->ret = 0;
2187 b2e12bc6 Christoph Hellwig
2188 b2e12bc6 Christoph Hellwig
    if (!acb->bh)
2189 b2e12bc6 Christoph Hellwig
        acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
2190 b2e12bc6 Christoph Hellwig
2191 b2e12bc6 Christoph Hellwig
    bdrv_flush(bs);
2192 b2e12bc6 Christoph Hellwig
    qemu_bh_schedule(acb->bh);
2193 b2e12bc6 Christoph Hellwig
    return &acb->common;
2194 b2e12bc6 Christoph Hellwig
}
2195 b2e12bc6 Christoph Hellwig
2196 83f64091 bellard
/**************************************************************/
2197 83f64091 bellard
/* sync block device emulation */
2198 ea2384d3 bellard
2199 83f64091 bellard
static void bdrv_rw_em_cb(void *opaque, int ret)
2200 83f64091 bellard
{
2201 83f64091 bellard
    *(int *)opaque = ret;
2202 ea2384d3 bellard
}
2203 ea2384d3 bellard
2204 83f64091 bellard
#define NOT_DONE 0x7fffffff
2205 83f64091 bellard
2206 5fafdf24 ths
static int bdrv_read_em(BlockDriverState *bs, int64_t sector_num,
2207 83f64091 bellard
                        uint8_t *buf, int nb_sectors)
2208 7a6cba61 pbrook
{
2209 ce1a14dc pbrook
    int async_ret;
2210 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
2211 f141eafe aliguori
    struct iovec iov;
2212 f141eafe aliguori
    QEMUIOVector qiov;
2213 83f64091 bellard
2214 65d6b3d8 Kevin Wolf
    async_context_push();
2215 65d6b3d8 Kevin Wolf
2216 83f64091 bellard
    async_ret = NOT_DONE;
2217 3f4cb3d3 blueswir1
    iov.iov_base = (void *)buf;
2218 f141eafe aliguori
    iov.iov_len = nb_sectors * 512;
2219 f141eafe aliguori
    qemu_iovec_init_external(&qiov, &iov, 1);
2220 f141eafe aliguori
    acb = bdrv_aio_readv(bs, sector_num, &qiov, nb_sectors,
2221 f141eafe aliguori
        bdrv_rw_em_cb, &async_ret);
2222 65d6b3d8 Kevin Wolf
    if (acb == NULL) {
2223 65d6b3d8 Kevin Wolf
        async_ret = -1;
2224 65d6b3d8 Kevin Wolf
        goto fail;
2225 65d6b3d8 Kevin Wolf
    }
2226 baf35cb9 aliguori
2227 83f64091 bellard
    while (async_ret == NOT_DONE) {
2228 83f64091 bellard
        qemu_aio_wait();
2229 83f64091 bellard
    }
2230 baf35cb9 aliguori
2231 65d6b3d8 Kevin Wolf
2232 65d6b3d8 Kevin Wolf
fail:
2233 65d6b3d8 Kevin Wolf
    async_context_pop();
2234 83f64091 bellard
    return async_ret;
2235 7a6cba61 pbrook
}
2236 7a6cba61 pbrook
2237 83f64091 bellard
static int bdrv_write_em(BlockDriverState *bs, int64_t sector_num,
2238 83f64091 bellard
                         const uint8_t *buf, int nb_sectors)
2239 83f64091 bellard
{
2240 ce1a14dc pbrook
    int async_ret;
2241 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
2242 f141eafe aliguori
    struct iovec iov;
2243 f141eafe aliguori
    QEMUIOVector qiov;
2244 83f64091 bellard
2245 65d6b3d8 Kevin Wolf
    async_context_push();
2246 65d6b3d8 Kevin Wolf
2247 83f64091 bellard
    async_ret = NOT_DONE;
2248 f141eafe aliguori
    iov.iov_base = (void *)buf;
2249 f141eafe aliguori
    iov.iov_len = nb_sectors * 512;
2250 f141eafe aliguori
    qemu_iovec_init_external(&qiov, &iov, 1);
2251 f141eafe aliguori
    acb = bdrv_aio_writev(bs, sector_num, &qiov, nb_sectors,
2252 f141eafe aliguori
        bdrv_rw_em_cb, &async_ret);
2253 65d6b3d8 Kevin Wolf
    if (acb == NULL) {
2254 65d6b3d8 Kevin Wolf
        async_ret = -1;
2255 65d6b3d8 Kevin Wolf
        goto fail;
2256 65d6b3d8 Kevin Wolf
    }
2257 83f64091 bellard
    while (async_ret == NOT_DONE) {
2258 83f64091 bellard
        qemu_aio_wait();
2259 83f64091 bellard
    }
2260 65d6b3d8 Kevin Wolf
2261 65d6b3d8 Kevin Wolf
fail:
2262 65d6b3d8 Kevin Wolf
    async_context_pop();
2263 83f64091 bellard
    return async_ret;
2264 83f64091 bellard
}
2265 ea2384d3 bellard
2266 ea2384d3 bellard
void bdrv_init(void)
2267 ea2384d3 bellard
{
2268 5efa9d5a Anthony Liguori
    module_call_init(MODULE_INIT_BLOCK);
2269 ea2384d3 bellard
}
2270 ce1a14dc pbrook
2271 eb852011 Markus Armbruster
void bdrv_init_with_whitelist(void)
2272 eb852011 Markus Armbruster
{
2273 eb852011 Markus Armbruster
    use_bdrv_whitelist = 1;
2274 eb852011 Markus Armbruster
    bdrv_init();
2275 eb852011 Markus Armbruster
}
2276 eb852011 Markus Armbruster
2277 c16b5a2c Christoph Hellwig
void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
2278 c16b5a2c Christoph Hellwig
                   BlockDriverCompletionFunc *cb, void *opaque)
2279 ce1a14dc pbrook
{
2280 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
2281 ce1a14dc pbrook
2282 6bbff9a0 aliguori
    if (pool->free_aiocb) {
2283 6bbff9a0 aliguori
        acb = pool->free_aiocb;
2284 6bbff9a0 aliguori
        pool->free_aiocb = acb->next;
2285 ce1a14dc pbrook
    } else {
2286 6bbff9a0 aliguori
        acb = qemu_mallocz(pool->aiocb_size);
2287 6bbff9a0 aliguori
        acb->pool = pool;
2288 ce1a14dc pbrook
    }
2289 ce1a14dc pbrook
    acb->bs = bs;
2290 ce1a14dc pbrook
    acb->cb = cb;
2291 ce1a14dc pbrook
    acb->opaque = opaque;
2292 ce1a14dc pbrook
    return acb;
2293 ce1a14dc pbrook
}
2294 ce1a14dc pbrook
2295 ce1a14dc pbrook
void qemu_aio_release(void *p)
2296 ce1a14dc pbrook
{
2297 6bbff9a0 aliguori
    BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
2298 6bbff9a0 aliguori
    AIOPool *pool = acb->pool;
2299 6bbff9a0 aliguori
    acb->next = pool->free_aiocb;
2300 6bbff9a0 aliguori
    pool->free_aiocb = acb;
2301 ce1a14dc pbrook
}
2302 19cb3738 bellard
2303 19cb3738 bellard
/**************************************************************/
2304 19cb3738 bellard
/* removable device support */
2305 19cb3738 bellard
2306 19cb3738 bellard
/**
2307 19cb3738 bellard
 * Return TRUE if the media is present
2308 19cb3738 bellard
 */
2309 19cb3738 bellard
int bdrv_is_inserted(BlockDriverState *bs)
2310 19cb3738 bellard
{
2311 19cb3738 bellard
    BlockDriver *drv = bs->drv;
2312 19cb3738 bellard
    int ret;
2313 19cb3738 bellard
    if (!drv)
2314 19cb3738 bellard
        return 0;
2315 19cb3738 bellard
    if (!drv->bdrv_is_inserted)
2316 19cb3738 bellard
        return 1;
2317 19cb3738 bellard
    ret = drv->bdrv_is_inserted(bs);
2318 19cb3738 bellard
    return ret;
2319 19cb3738 bellard
}
2320 19cb3738 bellard
2321 19cb3738 bellard
/**
2322 19cb3738 bellard
 * Return TRUE if the media changed since the last call to this
2323 5fafdf24 ths
 * function. It is currently only used for floppy disks
2324 19cb3738 bellard
 */
2325 19cb3738 bellard
int bdrv_media_changed(BlockDriverState *bs)
2326 19cb3738 bellard
{
2327 19cb3738 bellard
    BlockDriver *drv = bs->drv;
2328 19cb3738 bellard
    int ret;
2329 19cb3738 bellard
2330 19cb3738 bellard
    if (!drv || !drv->bdrv_media_changed)
2331 19cb3738 bellard
        ret = -ENOTSUP;
2332 19cb3738 bellard
    else
2333 19cb3738 bellard
        ret = drv->bdrv_media_changed(bs);
2334 19cb3738 bellard
    if (ret == -ENOTSUP)
2335 19cb3738 bellard
        ret = bs->media_changed;
2336 19cb3738 bellard
    bs->media_changed = 0;
2337 19cb3738 bellard
    return ret;
2338 19cb3738 bellard
}
2339 19cb3738 bellard
2340 19cb3738 bellard
/**
2341 19cb3738 bellard
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
2342 19cb3738 bellard
 */
2343 aea2a33c Mark McLoughlin
int bdrv_eject(BlockDriverState *bs, int eject_flag)
2344 19cb3738 bellard
{
2345 19cb3738 bellard
    BlockDriver *drv = bs->drv;
2346 19cb3738 bellard
    int ret;
2347 19cb3738 bellard
2348 aea2a33c Mark McLoughlin
    if (bs->locked) {
2349 aea2a33c Mark McLoughlin
        return -EBUSY;
2350 aea2a33c Mark McLoughlin
    }
2351 aea2a33c Mark McLoughlin
2352 19cb3738 bellard
    if (!drv || !drv->bdrv_eject) {
2353 19cb3738 bellard
        ret = -ENOTSUP;
2354 19cb3738 bellard
    } else {
2355 19cb3738 bellard
        ret = drv->bdrv_eject(bs, eject_flag);
2356 19cb3738 bellard
    }
2357 19cb3738 bellard
    if (ret == -ENOTSUP) {
2358 19cb3738 bellard
        if (eject_flag)
2359 19cb3738 bellard
            bdrv_close(bs);
2360 aea2a33c Mark McLoughlin
        ret = 0;
2361 19cb3738 bellard
    }
2362 aea2a33c Mark McLoughlin
2363 aea2a33c Mark McLoughlin
    return ret;
2364 19cb3738 bellard
}
2365 19cb3738 bellard
2366 19cb3738 bellard
int bdrv_is_locked(BlockDriverState *bs)
2367 19cb3738 bellard
{
2368 19cb3738 bellard
    return bs->locked;
2369 19cb3738 bellard
}
2370 19cb3738 bellard
2371 19cb3738 bellard
/**
2372 19cb3738 bellard
 * Lock or unlock the media (if it is locked, the user won't be able
2373 19cb3738 bellard
 * to eject it manually).
2374 19cb3738 bellard
 */
2375 19cb3738 bellard
void bdrv_set_locked(BlockDriverState *bs, int locked)
2376 19cb3738 bellard
{
2377 19cb3738 bellard
    BlockDriver *drv = bs->drv;
2378 19cb3738 bellard
2379 19cb3738 bellard
    bs->locked = locked;
2380 19cb3738 bellard
    if (drv && drv->bdrv_set_locked) {
2381 19cb3738 bellard
        drv->bdrv_set_locked(bs, locked);
2382 19cb3738 bellard
    }
2383 19cb3738 bellard
}
2384 985a03b0 ths
2385 985a03b0 ths
/* needed for generic scsi interface */
2386 985a03b0 ths
2387 985a03b0 ths
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
2388 985a03b0 ths
{
2389 985a03b0 ths
    BlockDriver *drv = bs->drv;
2390 985a03b0 ths
2391 985a03b0 ths
    if (drv && drv->bdrv_ioctl)
2392 985a03b0 ths
        return drv->bdrv_ioctl(bs, req, buf);
2393 985a03b0 ths
    return -ENOTSUP;
2394 985a03b0 ths
}
2395 7d780669 aliguori
2396 221f715d aliguori
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
2397 221f715d aliguori
        unsigned long int req, void *buf,
2398 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
2399 7d780669 aliguori
{
2400 221f715d aliguori
    BlockDriver *drv = bs->drv;
2401 7d780669 aliguori
2402 221f715d aliguori
    if (drv && drv->bdrv_aio_ioctl)
2403 221f715d aliguori
        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
2404 221f715d aliguori
    return NULL;
2405 7d780669 aliguori
}
2406 e268ca52 aliguori
2407 7cd1e32a lirans@il.ibm.com
2408 7cd1e32a lirans@il.ibm.com
2409 e268ca52 aliguori
void *qemu_blockalign(BlockDriverState *bs, size_t size)
2410 e268ca52 aliguori
{
2411 e268ca52 aliguori
    return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
2412 e268ca52 aliguori
}
2413 7cd1e32a lirans@il.ibm.com
2414 7cd1e32a lirans@il.ibm.com
void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
2415 7cd1e32a lirans@il.ibm.com
{
2416 7cd1e32a lirans@il.ibm.com
    int64_t bitmap_size;
2417 a55eb92c Jan Kiszka
2418 aaa0eb75 Liran Schour
    bs->dirty_count = 0;
2419 a55eb92c Jan Kiszka
    if (enable) {
2420 c6d22830 Jan Kiszka
        if (!bs->dirty_bitmap) {
2421 c6d22830 Jan Kiszka
            bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
2422 c6d22830 Jan Kiszka
                    BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
2423 c6d22830 Jan Kiszka
            bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
2424 a55eb92c Jan Kiszka
2425 7cd1e32a lirans@il.ibm.com
            bs->dirty_bitmap = qemu_mallocz(bitmap_size);
2426 a55eb92c Jan Kiszka
        }
2427 7cd1e32a lirans@il.ibm.com
    } else {
2428 c6d22830 Jan Kiszka
        if (bs->dirty_bitmap) {
2429 7cd1e32a lirans@il.ibm.com
            qemu_free(bs->dirty_bitmap);
2430 c6d22830 Jan Kiszka
            bs->dirty_bitmap = NULL;
2431 a55eb92c Jan Kiszka
        }
2432 7cd1e32a lirans@il.ibm.com
    }
2433 7cd1e32a lirans@il.ibm.com
}
2434 7cd1e32a lirans@il.ibm.com
2435 7cd1e32a lirans@il.ibm.com
int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
2436 7cd1e32a lirans@il.ibm.com
{
2437 6ea44308 Jan Kiszka
    int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
2438 a55eb92c Jan Kiszka
2439 c6d22830 Jan Kiszka
    if (bs->dirty_bitmap &&
2440 c6d22830 Jan Kiszka
        (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
2441 c6d22830 Jan Kiszka
        return bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
2442 c6d22830 Jan Kiszka
            (1 << (chunk % (sizeof(unsigned long) * 8)));
2443 7cd1e32a lirans@il.ibm.com
    } else {
2444 7cd1e32a lirans@il.ibm.com
        return 0;
2445 7cd1e32a lirans@il.ibm.com
    }
2446 7cd1e32a lirans@il.ibm.com
}
2447 7cd1e32a lirans@il.ibm.com
2448 a55eb92c Jan Kiszka
void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
2449 a55eb92c Jan Kiszka
                      int nr_sectors)
2450 7cd1e32a lirans@il.ibm.com
{
2451 7cd1e32a lirans@il.ibm.com
    set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
2452 7cd1e32a lirans@il.ibm.com
}
2453 aaa0eb75 Liran Schour
2454 aaa0eb75 Liran Schour
int64_t bdrv_get_dirty_count(BlockDriverState *bs)
2455 aaa0eb75 Liran Schour
{
2456 aaa0eb75 Liran Schour
    return bs->dirty_count;
2457 aaa0eb75 Liran Schour
}