Statistics
| Branch: | Revision:

root / block.c @ b40292e7

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