Statistics
| Branch: | Revision:

root / block.c @ 67f0875e

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