Statistics
| Branch: | Revision:

root / block.c @ 45024f09

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