Statistics
| Branch: | Revision:

root / block.c @ 658788c5

History | View | Annotate | Download (53.4 kB)

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