Statistics
| Branch: | Revision:

root / block.c @ 636aa200

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