Statistics
| Branch: | Revision:

root / block.c @ 371c6489

History | View | Annotate | Download (112 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 6d519a5f Stefan Hajnoczi
#include "trace.h"
27 376253ec aliguori
#include "monitor.h"
28 ea2384d3 bellard
#include "block_int.h"
29 5efa9d5a Anthony Liguori
#include "module.h"
30 f795e743 Luiz Capitulino
#include "qjson.h"
31 68485420 Kevin Wolf
#include "qemu-coroutine.h"
32 b2023818 Luiz Capitulino
#include "qmp-commands.h"
33 0563e191 Zhi Yong Wu
#include "qemu-timer.h"
34 fc01f7e7 bellard
35 71e72a19 Juan Quintela
#ifdef CONFIG_BSD
36 7674e7bf bellard
#include <sys/types.h>
37 7674e7bf bellard
#include <sys/stat.h>
38 7674e7bf bellard
#include <sys/ioctl.h>
39 72cf2d4f Blue Swirl
#include <sys/queue.h>
40 c5e97233 blueswir1
#ifndef __DragonFly__
41 7674e7bf bellard
#include <sys/disk.h>
42 7674e7bf bellard
#endif
43 c5e97233 blueswir1
#endif
44 7674e7bf bellard
45 49dc768d aliguori
#ifdef _WIN32
46 49dc768d aliguori
#include <windows.h>
47 49dc768d aliguori
#endif
48 49dc768d aliguori
49 1c9805a3 Stefan Hajnoczi
#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
50 1c9805a3 Stefan Hajnoczi
51 470c0504 Stefan Hajnoczi
typedef enum {
52 470c0504 Stefan Hajnoczi
    BDRV_REQ_COPY_ON_READ = 0x1,
53 f08f2dda Stefan Hajnoczi
    BDRV_REQ_ZERO_WRITE   = 0x2,
54 470c0504 Stefan Hajnoczi
} BdrvRequestFlags;
55 470c0504 Stefan Hajnoczi
56 7d4b4ba5 Markus Armbruster
static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
57 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
58 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
59 c87c0672 aliguori
        BlockDriverCompletionFunc *cb, void *opaque);
60 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
61 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
62 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
63 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
64 f9f05dc5 Kevin Wolf
                                         int64_t sector_num, int nb_sectors,
65 f9f05dc5 Kevin Wolf
                                         QEMUIOVector *iov);
66 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
67 f9f05dc5 Kevin Wolf
                                         int64_t sector_num, int nb_sectors,
68 f9f05dc5 Kevin Wolf
                                         QEMUIOVector *iov);
69 c5fbe571 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
70 470c0504 Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
71 470c0504 Stefan Hajnoczi
    BdrvRequestFlags flags);
72 1c9805a3 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
73 f08f2dda Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
74 f08f2dda Stefan Hajnoczi
    BdrvRequestFlags flags);
75 b2a61371 Stefan Hajnoczi
static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
76 b2a61371 Stefan Hajnoczi
                                               int64_t sector_num,
77 b2a61371 Stefan Hajnoczi
                                               QEMUIOVector *qiov,
78 b2a61371 Stefan Hajnoczi
                                               int nb_sectors,
79 b2a61371 Stefan Hajnoczi
                                               BlockDriverCompletionFunc *cb,
80 b2a61371 Stefan Hajnoczi
                                               void *opaque,
81 8c5873d6 Stefan Hajnoczi
                                               bool is_write);
82 b2a61371 Stefan Hajnoczi
static void coroutine_fn bdrv_co_do_rw(void *opaque);
83 ec530c81 bellard
84 98f90dba Zhi Yong Wu
static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors,
85 98f90dba Zhi Yong Wu
        bool is_write, double elapsed_time, uint64_t *wait);
86 98f90dba Zhi Yong Wu
static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write,
87 98f90dba Zhi Yong Wu
        double elapsed_time, uint64_t *wait);
88 98f90dba Zhi Yong Wu
static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors,
89 98f90dba Zhi Yong Wu
        bool is_write, int64_t *wait);
90 98f90dba Zhi Yong Wu
91 1b7bdbc1 Stefan Hajnoczi
static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
92 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_HEAD_INITIALIZER(bdrv_states);
93 7ee930d0 blueswir1
94 8a22f02a Stefan Hajnoczi
static QLIST_HEAD(, BlockDriver) bdrv_drivers =
95 8a22f02a Stefan Hajnoczi
    QLIST_HEAD_INITIALIZER(bdrv_drivers);
96 ea2384d3 bellard
97 f9092b10 Markus Armbruster
/* The device to use for VM snapshots */
98 f9092b10 Markus Armbruster
static BlockDriverState *bs_snapshots;
99 f9092b10 Markus Armbruster
100 eb852011 Markus Armbruster
/* If non-zero, use only whitelisted block drivers */
101 eb852011 Markus Armbruster
static int use_bdrv_whitelist;
102 eb852011 Markus Armbruster
103 9e0b22f4 Stefan Hajnoczi
#ifdef _WIN32
104 9e0b22f4 Stefan Hajnoczi
static int is_windows_drive_prefix(const char *filename)
105 9e0b22f4 Stefan Hajnoczi
{
106 9e0b22f4 Stefan Hajnoczi
    return (((filename[0] >= 'a' && filename[0] <= 'z') ||
107 9e0b22f4 Stefan Hajnoczi
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
108 9e0b22f4 Stefan Hajnoczi
            filename[1] == ':');
109 9e0b22f4 Stefan Hajnoczi
}
110 9e0b22f4 Stefan Hajnoczi
111 9e0b22f4 Stefan Hajnoczi
int is_windows_drive(const char *filename)
112 9e0b22f4 Stefan Hajnoczi
{
113 9e0b22f4 Stefan Hajnoczi
    if (is_windows_drive_prefix(filename) &&
114 9e0b22f4 Stefan Hajnoczi
        filename[2] == '\0')
115 9e0b22f4 Stefan Hajnoczi
        return 1;
116 9e0b22f4 Stefan Hajnoczi
    if (strstart(filename, "\\\\.\\", NULL) ||
117 9e0b22f4 Stefan Hajnoczi
        strstart(filename, "//./", NULL))
118 9e0b22f4 Stefan Hajnoczi
        return 1;
119 9e0b22f4 Stefan Hajnoczi
    return 0;
120 9e0b22f4 Stefan Hajnoczi
}
121 9e0b22f4 Stefan Hajnoczi
#endif
122 9e0b22f4 Stefan Hajnoczi
123 0563e191 Zhi Yong Wu
/* throttling disk I/O limits */
124 98f90dba Zhi Yong Wu
void bdrv_io_limits_disable(BlockDriverState *bs)
125 98f90dba Zhi Yong Wu
{
126 98f90dba Zhi Yong Wu
    bs->io_limits_enabled = false;
127 98f90dba Zhi Yong Wu
128 98f90dba Zhi Yong Wu
    while (qemu_co_queue_next(&bs->throttled_reqs));
129 98f90dba Zhi Yong Wu
130 98f90dba Zhi Yong Wu
    if (bs->block_timer) {
131 98f90dba Zhi Yong Wu
        qemu_del_timer(bs->block_timer);
132 98f90dba Zhi Yong Wu
        qemu_free_timer(bs->block_timer);
133 98f90dba Zhi Yong Wu
        bs->block_timer = NULL;
134 98f90dba Zhi Yong Wu
    }
135 98f90dba Zhi Yong Wu
136 98f90dba Zhi Yong Wu
    bs->slice_start = 0;
137 98f90dba Zhi Yong Wu
    bs->slice_end   = 0;
138 98f90dba Zhi Yong Wu
    bs->slice_time  = 0;
139 98f90dba Zhi Yong Wu
    memset(&bs->io_base, 0, sizeof(bs->io_base));
140 98f90dba Zhi Yong Wu
}
141 98f90dba Zhi Yong Wu
142 0563e191 Zhi Yong Wu
static void bdrv_block_timer(void *opaque)
143 0563e191 Zhi Yong Wu
{
144 0563e191 Zhi Yong Wu
    BlockDriverState *bs = opaque;
145 0563e191 Zhi Yong Wu
146 0563e191 Zhi Yong Wu
    qemu_co_queue_next(&bs->throttled_reqs);
147 0563e191 Zhi Yong Wu
}
148 0563e191 Zhi Yong Wu
149 0563e191 Zhi Yong Wu
void bdrv_io_limits_enable(BlockDriverState *bs)
150 0563e191 Zhi Yong Wu
{
151 0563e191 Zhi Yong Wu
    qemu_co_queue_init(&bs->throttled_reqs);
152 0563e191 Zhi Yong Wu
    bs->block_timer = qemu_new_timer_ns(vm_clock, bdrv_block_timer, bs);
153 0563e191 Zhi Yong Wu
    bs->slice_time  = 5 * BLOCK_IO_SLICE_TIME;
154 0563e191 Zhi Yong Wu
    bs->slice_start = qemu_get_clock_ns(vm_clock);
155 0563e191 Zhi Yong Wu
    bs->slice_end   = bs->slice_start + bs->slice_time;
156 0563e191 Zhi Yong Wu
    memset(&bs->io_base, 0, sizeof(bs->io_base));
157 0563e191 Zhi Yong Wu
    bs->io_limits_enabled = true;
158 0563e191 Zhi Yong Wu
}
159 0563e191 Zhi Yong Wu
160 0563e191 Zhi Yong Wu
bool bdrv_io_limits_enabled(BlockDriverState *bs)
161 0563e191 Zhi Yong Wu
{
162 0563e191 Zhi Yong Wu
    BlockIOLimit *io_limits = &bs->io_limits;
163 0563e191 Zhi Yong Wu
    return io_limits->bps[BLOCK_IO_LIMIT_READ]
164 0563e191 Zhi Yong Wu
         || io_limits->bps[BLOCK_IO_LIMIT_WRITE]
165 0563e191 Zhi Yong Wu
         || io_limits->bps[BLOCK_IO_LIMIT_TOTAL]
166 0563e191 Zhi Yong Wu
         || io_limits->iops[BLOCK_IO_LIMIT_READ]
167 0563e191 Zhi Yong Wu
         || io_limits->iops[BLOCK_IO_LIMIT_WRITE]
168 0563e191 Zhi Yong Wu
         || io_limits->iops[BLOCK_IO_LIMIT_TOTAL];
169 0563e191 Zhi Yong Wu
}
170 0563e191 Zhi Yong Wu
171 98f90dba Zhi Yong Wu
static void bdrv_io_limits_intercept(BlockDriverState *bs,
172 98f90dba Zhi Yong Wu
                                     bool is_write, int nb_sectors)
173 98f90dba Zhi Yong Wu
{
174 98f90dba Zhi Yong Wu
    int64_t wait_time = -1;
175 98f90dba Zhi Yong Wu
176 98f90dba Zhi Yong Wu
    if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
177 98f90dba Zhi Yong Wu
        qemu_co_queue_wait(&bs->throttled_reqs);
178 98f90dba Zhi Yong Wu
    }
179 98f90dba Zhi Yong Wu
180 98f90dba Zhi Yong Wu
    /* In fact, we hope to keep each request's timing, in FIFO mode. The next
181 98f90dba Zhi Yong Wu
     * throttled requests will not be dequeued until the current request is
182 98f90dba Zhi Yong Wu
     * allowed to be serviced. So if the current request still exceeds the
183 98f90dba Zhi Yong Wu
     * limits, it will be inserted to the head. All requests followed it will
184 98f90dba Zhi Yong Wu
     * be still in throttled_reqs queue.
185 98f90dba Zhi Yong Wu
     */
186 98f90dba Zhi Yong Wu
187 98f90dba Zhi Yong Wu
    while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) {
188 98f90dba Zhi Yong Wu
        qemu_mod_timer(bs->block_timer,
189 98f90dba Zhi Yong Wu
                       wait_time + qemu_get_clock_ns(vm_clock));
190 98f90dba Zhi Yong Wu
        qemu_co_queue_wait_insert_head(&bs->throttled_reqs);
191 98f90dba Zhi Yong Wu
    }
192 98f90dba Zhi Yong Wu
193 98f90dba Zhi Yong Wu
    qemu_co_queue_next(&bs->throttled_reqs);
194 98f90dba Zhi Yong Wu
}
195 98f90dba Zhi Yong Wu
196 9e0b22f4 Stefan Hajnoczi
/* check if the path starts with "<protocol>:" */
197 9e0b22f4 Stefan Hajnoczi
static int path_has_protocol(const char *path)
198 9e0b22f4 Stefan Hajnoczi
{
199 9e0b22f4 Stefan Hajnoczi
#ifdef _WIN32
200 9e0b22f4 Stefan Hajnoczi
    if (is_windows_drive(path) ||
201 9e0b22f4 Stefan Hajnoczi
        is_windows_drive_prefix(path)) {
202 9e0b22f4 Stefan Hajnoczi
        return 0;
203 9e0b22f4 Stefan Hajnoczi
    }
204 9e0b22f4 Stefan Hajnoczi
#endif
205 9e0b22f4 Stefan Hajnoczi
206 9e0b22f4 Stefan Hajnoczi
    return strchr(path, ':') != NULL;
207 9e0b22f4 Stefan Hajnoczi
}
208 9e0b22f4 Stefan Hajnoczi
209 83f64091 bellard
int path_is_absolute(const char *path)
210 3b0d4f61 bellard
{
211 83f64091 bellard
    const char *p;
212 21664424 bellard
#ifdef _WIN32
213 21664424 bellard
    /* specific case for names like: "\\.\d:" */
214 21664424 bellard
    if (*path == '/' || *path == '\\')
215 21664424 bellard
        return 1;
216 21664424 bellard
#endif
217 83f64091 bellard
    p = strchr(path, ':');
218 83f64091 bellard
    if (p)
219 83f64091 bellard
        p++;
220 83f64091 bellard
    else
221 83f64091 bellard
        p = path;
222 3b9f94e1 bellard
#ifdef _WIN32
223 3b9f94e1 bellard
    return (*p == '/' || *p == '\\');
224 3b9f94e1 bellard
#else
225 3b9f94e1 bellard
    return (*p == '/');
226 3b9f94e1 bellard
#endif
227 3b0d4f61 bellard
}
228 3b0d4f61 bellard
229 83f64091 bellard
/* if filename is absolute, just copy it to dest. Otherwise, build a
230 83f64091 bellard
   path to it by considering it is relative to base_path. URL are
231 83f64091 bellard
   supported. */
232 83f64091 bellard
void path_combine(char *dest, int dest_size,
233 83f64091 bellard
                  const char *base_path,
234 83f64091 bellard
                  const char *filename)
235 3b0d4f61 bellard
{
236 83f64091 bellard
    const char *p, *p1;
237 83f64091 bellard
    int len;
238 83f64091 bellard
239 83f64091 bellard
    if (dest_size <= 0)
240 83f64091 bellard
        return;
241 83f64091 bellard
    if (path_is_absolute(filename)) {
242 83f64091 bellard
        pstrcpy(dest, dest_size, filename);
243 83f64091 bellard
    } else {
244 83f64091 bellard
        p = strchr(base_path, ':');
245 83f64091 bellard
        if (p)
246 83f64091 bellard
            p++;
247 83f64091 bellard
        else
248 83f64091 bellard
            p = base_path;
249 3b9f94e1 bellard
        p1 = strrchr(base_path, '/');
250 3b9f94e1 bellard
#ifdef _WIN32
251 3b9f94e1 bellard
        {
252 3b9f94e1 bellard
            const char *p2;
253 3b9f94e1 bellard
            p2 = strrchr(base_path, '\\');
254 3b9f94e1 bellard
            if (!p1 || p2 > p1)
255 3b9f94e1 bellard
                p1 = p2;
256 3b9f94e1 bellard
        }
257 3b9f94e1 bellard
#endif
258 83f64091 bellard
        if (p1)
259 83f64091 bellard
            p1++;
260 83f64091 bellard
        else
261 83f64091 bellard
            p1 = base_path;
262 83f64091 bellard
        if (p1 > p)
263 83f64091 bellard
            p = p1;
264 83f64091 bellard
        len = p - base_path;
265 83f64091 bellard
        if (len > dest_size - 1)
266 83f64091 bellard
            len = dest_size - 1;
267 83f64091 bellard
        memcpy(dest, base_path, len);
268 83f64091 bellard
        dest[len] = '\0';
269 83f64091 bellard
        pstrcat(dest, dest_size, filename);
270 3b0d4f61 bellard
    }
271 3b0d4f61 bellard
}
272 3b0d4f61 bellard
273 5efa9d5a Anthony Liguori
void bdrv_register(BlockDriver *bdrv)
274 ea2384d3 bellard
{
275 8c5873d6 Stefan Hajnoczi
    /* Block drivers without coroutine functions need emulation */
276 8c5873d6 Stefan Hajnoczi
    if (!bdrv->bdrv_co_readv) {
277 f9f05dc5 Kevin Wolf
        bdrv->bdrv_co_readv = bdrv_co_readv_em;
278 f9f05dc5 Kevin Wolf
        bdrv->bdrv_co_writev = bdrv_co_writev_em;
279 f9f05dc5 Kevin Wolf
280 f8c35c1d Stefan Hajnoczi
        /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
281 f8c35c1d Stefan Hajnoczi
         * the block driver lacks aio we need to emulate that too.
282 f8c35c1d Stefan Hajnoczi
         */
283 f9f05dc5 Kevin Wolf
        if (!bdrv->bdrv_aio_readv) {
284 f9f05dc5 Kevin Wolf
            /* add AIO emulation layer */
285 f9f05dc5 Kevin Wolf
            bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
286 f9f05dc5 Kevin Wolf
            bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
287 f9f05dc5 Kevin Wolf
        }
288 83f64091 bellard
    }
289 b2e12bc6 Christoph Hellwig
290 8a22f02a Stefan Hajnoczi
    QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
291 ea2384d3 bellard
}
292 b338082b bellard
293 b338082b bellard
/* create a new block device (by default it is empty) */
294 b338082b bellard
BlockDriverState *bdrv_new(const char *device_name)
295 b338082b bellard
{
296 1b7bdbc1 Stefan Hajnoczi
    BlockDriverState *bs;
297 b338082b bellard
298 7267c094 Anthony Liguori
    bs = g_malloc0(sizeof(BlockDriverState));
299 b338082b bellard
    pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
300 ea2384d3 bellard
    if (device_name[0] != '\0') {
301 1b7bdbc1 Stefan Hajnoczi
        QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
302 ea2384d3 bellard
    }
303 28a7282a Luiz Capitulino
    bdrv_iostatus_disable(bs);
304 b338082b bellard
    return bs;
305 b338082b bellard
}
306 b338082b bellard
307 ea2384d3 bellard
BlockDriver *bdrv_find_format(const char *format_name)
308 ea2384d3 bellard
{
309 ea2384d3 bellard
    BlockDriver *drv1;
310 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
311 8a22f02a Stefan Hajnoczi
        if (!strcmp(drv1->format_name, format_name)) {
312 ea2384d3 bellard
            return drv1;
313 8a22f02a Stefan Hajnoczi
        }
314 ea2384d3 bellard
    }
315 ea2384d3 bellard
    return NULL;
316 ea2384d3 bellard
}
317 ea2384d3 bellard
318 eb852011 Markus Armbruster
static int bdrv_is_whitelisted(BlockDriver *drv)
319 eb852011 Markus Armbruster
{
320 eb852011 Markus Armbruster
    static const char *whitelist[] = {
321 eb852011 Markus Armbruster
        CONFIG_BDRV_WHITELIST
322 eb852011 Markus Armbruster
    };
323 eb852011 Markus Armbruster
    const char **p;
324 eb852011 Markus Armbruster
325 eb852011 Markus Armbruster
    if (!whitelist[0])
326 eb852011 Markus Armbruster
        return 1;               /* no whitelist, anything goes */
327 eb852011 Markus Armbruster
328 eb852011 Markus Armbruster
    for (p = whitelist; *p; p++) {
329 eb852011 Markus Armbruster
        if (!strcmp(drv->format_name, *p)) {
330 eb852011 Markus Armbruster
            return 1;
331 eb852011 Markus Armbruster
        }
332 eb852011 Markus Armbruster
    }
333 eb852011 Markus Armbruster
    return 0;
334 eb852011 Markus Armbruster
}
335 eb852011 Markus Armbruster
336 eb852011 Markus Armbruster
BlockDriver *bdrv_find_whitelisted_format(const char *format_name)
337 eb852011 Markus Armbruster
{
338 eb852011 Markus Armbruster
    BlockDriver *drv = bdrv_find_format(format_name);
339 eb852011 Markus Armbruster
    return drv && bdrv_is_whitelisted(drv) ? drv : NULL;
340 eb852011 Markus Armbruster
}
341 eb852011 Markus Armbruster
342 0e7e1989 Kevin Wolf
int bdrv_create(BlockDriver *drv, const char* filename,
343 0e7e1989 Kevin Wolf
    QEMUOptionParameter *options)
344 ea2384d3 bellard
{
345 ea2384d3 bellard
    if (!drv->bdrv_create)
346 ea2384d3 bellard
        return -ENOTSUP;
347 0e7e1989 Kevin Wolf
348 0e7e1989 Kevin Wolf
    return drv->bdrv_create(filename, options);
349 ea2384d3 bellard
}
350 ea2384d3 bellard
351 84a12e66 Christoph Hellwig
int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
352 84a12e66 Christoph Hellwig
{
353 84a12e66 Christoph Hellwig
    BlockDriver *drv;
354 84a12e66 Christoph Hellwig
355 b50cbabc MORITA Kazutaka
    drv = bdrv_find_protocol(filename);
356 84a12e66 Christoph Hellwig
    if (drv == NULL) {
357 16905d71 Stefan Hajnoczi
        return -ENOENT;
358 84a12e66 Christoph Hellwig
    }
359 84a12e66 Christoph Hellwig
360 84a12e66 Christoph Hellwig
    return bdrv_create(drv, filename, options);
361 84a12e66 Christoph Hellwig
}
362 84a12e66 Christoph Hellwig
363 d5249393 bellard
#ifdef _WIN32
364 95389c86 bellard
void get_tmp_filename(char *filename, int size)
365 d5249393 bellard
{
366 3b9f94e1 bellard
    char temp_dir[MAX_PATH];
367 3b46e624 ths
368 3b9f94e1 bellard
    GetTempPath(MAX_PATH, temp_dir);
369 3b9f94e1 bellard
    GetTempFileName(temp_dir, "qem", 0, filename);
370 d5249393 bellard
}
371 d5249393 bellard
#else
372 95389c86 bellard
void get_tmp_filename(char *filename, int size)
373 fc01f7e7 bellard
{
374 67b915a5 bellard
    int fd;
375 7ccfb2eb blueswir1
    const char *tmpdir;
376 d5249393 bellard
    /* XXX: race condition possible */
377 0badc1ee aurel32
    tmpdir = getenv("TMPDIR");
378 0badc1ee aurel32
    if (!tmpdir)
379 0badc1ee aurel32
        tmpdir = "/tmp";
380 0badc1ee aurel32
    snprintf(filename, size, "%s/vl.XXXXXX", tmpdir);
381 ea2384d3 bellard
    fd = mkstemp(filename);
382 ea2384d3 bellard
    close(fd);
383 ea2384d3 bellard
}
384 d5249393 bellard
#endif
385 fc01f7e7 bellard
386 84a12e66 Christoph Hellwig
/*
387 84a12e66 Christoph Hellwig
 * Detect host devices. By convention, /dev/cdrom[N] is always
388 84a12e66 Christoph Hellwig
 * recognized as a host CDROM.
389 84a12e66 Christoph Hellwig
 */
390 84a12e66 Christoph Hellwig
static BlockDriver *find_hdev_driver(const char *filename)
391 84a12e66 Christoph Hellwig
{
392 84a12e66 Christoph Hellwig
    int score_max = 0, score;
393 84a12e66 Christoph Hellwig
    BlockDriver *drv = NULL, *d;
394 84a12e66 Christoph Hellwig
395 84a12e66 Christoph Hellwig
    QLIST_FOREACH(d, &bdrv_drivers, list) {
396 84a12e66 Christoph Hellwig
        if (d->bdrv_probe_device) {
397 84a12e66 Christoph Hellwig
            score = d->bdrv_probe_device(filename);
398 84a12e66 Christoph Hellwig
            if (score > score_max) {
399 84a12e66 Christoph Hellwig
                score_max = score;
400 84a12e66 Christoph Hellwig
                drv = d;
401 84a12e66 Christoph Hellwig
            }
402 84a12e66 Christoph Hellwig
        }
403 84a12e66 Christoph Hellwig
    }
404 84a12e66 Christoph Hellwig
405 84a12e66 Christoph Hellwig
    return drv;
406 84a12e66 Christoph Hellwig
}
407 84a12e66 Christoph Hellwig
408 b50cbabc MORITA Kazutaka
BlockDriver *bdrv_find_protocol(const char *filename)
409 83f64091 bellard
{
410 83f64091 bellard
    BlockDriver *drv1;
411 83f64091 bellard
    char protocol[128];
412 1cec71e3 Anthony Liguori
    int len;
413 83f64091 bellard
    const char *p;
414 19cb3738 bellard
415 66f82cee Kevin Wolf
    /* TODO Drivers without bdrv_file_open must be specified explicitly */
416 66f82cee Kevin Wolf
417 39508e7a Christoph Hellwig
    /*
418 39508e7a Christoph Hellwig
     * XXX(hch): we really should not let host device detection
419 39508e7a Christoph Hellwig
     * override an explicit protocol specification, but moving this
420 39508e7a Christoph Hellwig
     * later breaks access to device names with colons in them.
421 39508e7a Christoph Hellwig
     * Thanks to the brain-dead persistent naming schemes on udev-
422 39508e7a Christoph Hellwig
     * based Linux systems those actually are quite common.
423 39508e7a Christoph Hellwig
     */
424 39508e7a Christoph Hellwig
    drv1 = find_hdev_driver(filename);
425 39508e7a Christoph Hellwig
    if (drv1) {
426 39508e7a Christoph Hellwig
        return drv1;
427 39508e7a Christoph Hellwig
    }
428 39508e7a Christoph Hellwig
429 9e0b22f4 Stefan Hajnoczi
    if (!path_has_protocol(filename)) {
430 39508e7a Christoph Hellwig
        return bdrv_find_format("file");
431 84a12e66 Christoph Hellwig
    }
432 9e0b22f4 Stefan Hajnoczi
    p = strchr(filename, ':');
433 9e0b22f4 Stefan Hajnoczi
    assert(p != NULL);
434 1cec71e3 Anthony Liguori
    len = p - filename;
435 1cec71e3 Anthony Liguori
    if (len > sizeof(protocol) - 1)
436 1cec71e3 Anthony Liguori
        len = sizeof(protocol) - 1;
437 1cec71e3 Anthony Liguori
    memcpy(protocol, filename, len);
438 1cec71e3 Anthony Liguori
    protocol[len] = '\0';
439 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
440 5fafdf24 ths
        if (drv1->protocol_name &&
441 8a22f02a Stefan Hajnoczi
            !strcmp(drv1->protocol_name, protocol)) {
442 83f64091 bellard
            return drv1;
443 8a22f02a Stefan Hajnoczi
        }
444 83f64091 bellard
    }
445 83f64091 bellard
    return NULL;
446 83f64091 bellard
}
447 83f64091 bellard
448 c98ac35d Stefan Weil
static int find_image_format(const char *filename, BlockDriver **pdrv)
449 f3a5d3f8 Christoph Hellwig
{
450 f3a5d3f8 Christoph Hellwig
    int ret, score, score_max;
451 f3a5d3f8 Christoph Hellwig
    BlockDriver *drv1, *drv;
452 f3a5d3f8 Christoph Hellwig
    uint8_t buf[2048];
453 f3a5d3f8 Christoph Hellwig
    BlockDriverState *bs;
454 f3a5d3f8 Christoph Hellwig
455 f5edb014 Naphtali Sprei
    ret = bdrv_file_open(&bs, filename, 0);
456 c98ac35d Stefan Weil
    if (ret < 0) {
457 c98ac35d Stefan Weil
        *pdrv = NULL;
458 c98ac35d Stefan Weil
        return ret;
459 c98ac35d Stefan Weil
    }
460 f8ea0b00 Nicholas Bellinger
461 08a00559 Kevin Wolf
    /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
462 08a00559 Kevin Wolf
    if (bs->sg || !bdrv_is_inserted(bs)) {
463 1a396859 Nicholas A. Bellinger
        bdrv_delete(bs);
464 c98ac35d Stefan Weil
        drv = bdrv_find_format("raw");
465 c98ac35d Stefan Weil
        if (!drv) {
466 c98ac35d Stefan Weil
            ret = -ENOENT;
467 c98ac35d Stefan Weil
        }
468 c98ac35d Stefan Weil
        *pdrv = drv;
469 c98ac35d Stefan Weil
        return ret;
470 1a396859 Nicholas A. Bellinger
    }
471 f8ea0b00 Nicholas Bellinger
472 83f64091 bellard
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
473 83f64091 bellard
    bdrv_delete(bs);
474 83f64091 bellard
    if (ret < 0) {
475 c98ac35d Stefan Weil
        *pdrv = NULL;
476 c98ac35d Stefan Weil
        return ret;
477 83f64091 bellard
    }
478 83f64091 bellard
479 ea2384d3 bellard
    score_max = 0;
480 84a12e66 Christoph Hellwig
    drv = NULL;
481 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
482 83f64091 bellard
        if (drv1->bdrv_probe) {
483 83f64091 bellard
            score = drv1->bdrv_probe(buf, ret, filename);
484 83f64091 bellard
            if (score > score_max) {
485 83f64091 bellard
                score_max = score;
486 83f64091 bellard
                drv = drv1;
487 83f64091 bellard
            }
488 0849bf08 bellard
        }
489 fc01f7e7 bellard
    }
490 c98ac35d Stefan Weil
    if (!drv) {
491 c98ac35d Stefan Weil
        ret = -ENOENT;
492 c98ac35d Stefan Weil
    }
493 c98ac35d Stefan Weil
    *pdrv = drv;
494 c98ac35d Stefan Weil
    return ret;
495 ea2384d3 bellard
}
496 ea2384d3 bellard
497 51762288 Stefan Hajnoczi
/**
498 51762288 Stefan Hajnoczi
 * Set the current 'total_sectors' value
499 51762288 Stefan Hajnoczi
 */
500 51762288 Stefan Hajnoczi
static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
501 51762288 Stefan Hajnoczi
{
502 51762288 Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
503 51762288 Stefan Hajnoczi
504 396759ad Nicholas Bellinger
    /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
505 396759ad Nicholas Bellinger
    if (bs->sg)
506 396759ad Nicholas Bellinger
        return 0;
507 396759ad Nicholas Bellinger
508 51762288 Stefan Hajnoczi
    /* query actual device if possible, otherwise just trust the hint */
509 51762288 Stefan Hajnoczi
    if (drv->bdrv_getlength) {
510 51762288 Stefan Hajnoczi
        int64_t length = drv->bdrv_getlength(bs);
511 51762288 Stefan Hajnoczi
        if (length < 0) {
512 51762288 Stefan Hajnoczi
            return length;
513 51762288 Stefan Hajnoczi
        }
514 51762288 Stefan Hajnoczi
        hint = length >> BDRV_SECTOR_BITS;
515 51762288 Stefan Hajnoczi
    }
516 51762288 Stefan Hajnoczi
517 51762288 Stefan Hajnoczi
    bs->total_sectors = hint;
518 51762288 Stefan Hajnoczi
    return 0;
519 51762288 Stefan Hajnoczi
}
520 51762288 Stefan Hajnoczi
521 c3993cdc Stefan Hajnoczi
/**
522 c3993cdc Stefan Hajnoczi
 * Set open flags for a given cache mode
523 c3993cdc Stefan Hajnoczi
 *
524 c3993cdc Stefan Hajnoczi
 * Return 0 on success, -1 if the cache mode was invalid.
525 c3993cdc Stefan Hajnoczi
 */
526 c3993cdc Stefan Hajnoczi
int bdrv_parse_cache_flags(const char *mode, int *flags)
527 c3993cdc Stefan Hajnoczi
{
528 c3993cdc Stefan Hajnoczi
    *flags &= ~BDRV_O_CACHE_MASK;
529 c3993cdc Stefan Hajnoczi
530 c3993cdc Stefan Hajnoczi
    if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
531 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
532 92196b2f Stefan Hajnoczi
    } else if (!strcmp(mode, "directsync")) {
533 92196b2f Stefan Hajnoczi
        *flags |= BDRV_O_NOCACHE;
534 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "writeback")) {
535 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_CACHE_WB;
536 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "unsafe")) {
537 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_CACHE_WB;
538 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_NO_FLUSH;
539 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "writethrough")) {
540 c3993cdc Stefan Hajnoczi
        /* this is the default */
541 c3993cdc Stefan Hajnoczi
    } else {
542 c3993cdc Stefan Hajnoczi
        return -1;
543 c3993cdc Stefan Hajnoczi
    }
544 c3993cdc Stefan Hajnoczi
545 c3993cdc Stefan Hajnoczi
    return 0;
546 c3993cdc Stefan Hajnoczi
}
547 c3993cdc Stefan Hajnoczi
548 53fec9d3 Stefan Hajnoczi
/**
549 53fec9d3 Stefan Hajnoczi
 * The copy-on-read flag is actually a reference count so multiple users may
550 53fec9d3 Stefan Hajnoczi
 * use the feature without worrying about clobbering its previous state.
551 53fec9d3 Stefan Hajnoczi
 * Copy-on-read stays enabled until all users have called to disable it.
552 53fec9d3 Stefan Hajnoczi
 */
553 53fec9d3 Stefan Hajnoczi
void bdrv_enable_copy_on_read(BlockDriverState *bs)
554 53fec9d3 Stefan Hajnoczi
{
555 53fec9d3 Stefan Hajnoczi
    bs->copy_on_read++;
556 53fec9d3 Stefan Hajnoczi
}
557 53fec9d3 Stefan Hajnoczi
558 53fec9d3 Stefan Hajnoczi
void bdrv_disable_copy_on_read(BlockDriverState *bs)
559 53fec9d3 Stefan Hajnoczi
{
560 53fec9d3 Stefan Hajnoczi
    assert(bs->copy_on_read > 0);
561 53fec9d3 Stefan Hajnoczi
    bs->copy_on_read--;
562 53fec9d3 Stefan Hajnoczi
}
563 53fec9d3 Stefan Hajnoczi
564 b6ce07aa Kevin Wolf
/*
565 57915332 Kevin Wolf
 * Common part for opening disk images and files
566 57915332 Kevin Wolf
 */
567 57915332 Kevin Wolf
static int bdrv_open_common(BlockDriverState *bs, const char *filename,
568 57915332 Kevin Wolf
    int flags, BlockDriver *drv)
569 57915332 Kevin Wolf
{
570 57915332 Kevin Wolf
    int ret, open_flags;
571 57915332 Kevin Wolf
572 57915332 Kevin Wolf
    assert(drv != NULL);
573 57915332 Kevin Wolf
574 28dcee10 Stefan Hajnoczi
    trace_bdrv_open_common(bs, filename, flags, drv->format_name);
575 28dcee10 Stefan Hajnoczi
576 66f82cee Kevin Wolf
    bs->file = NULL;
577 51762288 Stefan Hajnoczi
    bs->total_sectors = 0;
578 57915332 Kevin Wolf
    bs->encrypted = 0;
579 57915332 Kevin Wolf
    bs->valid_key = 0;
580 03f541bd Stefan Hajnoczi
    bs->sg = 0;
581 57915332 Kevin Wolf
    bs->open_flags = flags;
582 03f541bd Stefan Hajnoczi
    bs->growable = 0;
583 57915332 Kevin Wolf
    bs->buffer_alignment = 512;
584 57915332 Kevin Wolf
585 53fec9d3 Stefan Hajnoczi
    assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
586 53fec9d3 Stefan Hajnoczi
    if ((flags & BDRV_O_RDWR) && (flags & BDRV_O_COPY_ON_READ)) {
587 53fec9d3 Stefan Hajnoczi
        bdrv_enable_copy_on_read(bs);
588 53fec9d3 Stefan Hajnoczi
    }
589 53fec9d3 Stefan Hajnoczi
590 57915332 Kevin Wolf
    pstrcpy(bs->filename, sizeof(bs->filename), filename);
591 03f541bd Stefan Hajnoczi
    bs->backing_file[0] = '\0';
592 57915332 Kevin Wolf
593 57915332 Kevin Wolf
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv)) {
594 57915332 Kevin Wolf
        return -ENOTSUP;
595 57915332 Kevin Wolf
    }
596 57915332 Kevin Wolf
597 57915332 Kevin Wolf
    bs->drv = drv;
598 7267c094 Anthony Liguori
    bs->opaque = g_malloc0(drv->instance_size);
599 57915332 Kevin Wolf
600 03f541bd Stefan Hajnoczi
    bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
601 57915332 Kevin Wolf
602 57915332 Kevin Wolf
    /*
603 57915332 Kevin Wolf
     * Clear flags that are internal to the block layer before opening the
604 57915332 Kevin Wolf
     * image.
605 57915332 Kevin Wolf
     */
606 57915332 Kevin Wolf
    open_flags = flags & ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
607 57915332 Kevin Wolf
608 57915332 Kevin Wolf
    /*
609 ebabb67a Stefan Weil
     * Snapshots should be writable.
610 57915332 Kevin Wolf
     */
611 57915332 Kevin Wolf
    if (bs->is_temporary) {
612 57915332 Kevin Wolf
        open_flags |= BDRV_O_RDWR;
613 57915332 Kevin Wolf
    }
614 57915332 Kevin Wolf
615 e7c63796 Stefan Hajnoczi
    bs->keep_read_only = bs->read_only = !(open_flags & BDRV_O_RDWR);
616 e7c63796 Stefan Hajnoczi
617 66f82cee Kevin Wolf
    /* Open the image, either directly or using a protocol */
618 66f82cee Kevin Wolf
    if (drv->bdrv_file_open) {
619 66f82cee Kevin Wolf
        ret = drv->bdrv_file_open(bs, filename, open_flags);
620 66f82cee Kevin Wolf
    } else {
621 66f82cee Kevin Wolf
        ret = bdrv_file_open(&bs->file, filename, open_flags);
622 66f82cee Kevin Wolf
        if (ret >= 0) {
623 66f82cee Kevin Wolf
            ret = drv->bdrv_open(bs, open_flags);
624 66f82cee Kevin Wolf
        }
625 66f82cee Kevin Wolf
    }
626 66f82cee Kevin Wolf
627 57915332 Kevin Wolf
    if (ret < 0) {
628 57915332 Kevin Wolf
        goto free_and_fail;
629 57915332 Kevin Wolf
    }
630 57915332 Kevin Wolf
631 51762288 Stefan Hajnoczi
    ret = refresh_total_sectors(bs, bs->total_sectors);
632 51762288 Stefan Hajnoczi
    if (ret < 0) {
633 51762288 Stefan Hajnoczi
        goto free_and_fail;
634 57915332 Kevin Wolf
    }
635 51762288 Stefan Hajnoczi
636 57915332 Kevin Wolf
#ifndef _WIN32
637 57915332 Kevin Wolf
    if (bs->is_temporary) {
638 57915332 Kevin Wolf
        unlink(filename);
639 57915332 Kevin Wolf
    }
640 57915332 Kevin Wolf
#endif
641 57915332 Kevin Wolf
    return 0;
642 57915332 Kevin Wolf
643 57915332 Kevin Wolf
free_and_fail:
644 66f82cee Kevin Wolf
    if (bs->file) {
645 66f82cee Kevin Wolf
        bdrv_delete(bs->file);
646 66f82cee Kevin Wolf
        bs->file = NULL;
647 66f82cee Kevin Wolf
    }
648 7267c094 Anthony Liguori
    g_free(bs->opaque);
649 57915332 Kevin Wolf
    bs->opaque = NULL;
650 57915332 Kevin Wolf
    bs->drv = NULL;
651 57915332 Kevin Wolf
    return ret;
652 57915332 Kevin Wolf
}
653 57915332 Kevin Wolf
654 57915332 Kevin Wolf
/*
655 b6ce07aa Kevin Wolf
 * Opens a file using a protocol (file, host_device, nbd, ...)
656 b6ce07aa Kevin Wolf
 */
657 83f64091 bellard
int bdrv_file_open(BlockDriverState **pbs, const char *filename, int flags)
658 ea2384d3 bellard
{
659 83f64091 bellard
    BlockDriverState *bs;
660 6db95603 Christoph Hellwig
    BlockDriver *drv;
661 83f64091 bellard
    int ret;
662 83f64091 bellard
663 b50cbabc MORITA Kazutaka
    drv = bdrv_find_protocol(filename);
664 6db95603 Christoph Hellwig
    if (!drv) {
665 6db95603 Christoph Hellwig
        return -ENOENT;
666 6db95603 Christoph Hellwig
    }
667 6db95603 Christoph Hellwig
668 83f64091 bellard
    bs = bdrv_new("");
669 b6ce07aa Kevin Wolf
    ret = bdrv_open_common(bs, filename, flags, drv);
670 83f64091 bellard
    if (ret < 0) {
671 83f64091 bellard
        bdrv_delete(bs);
672 83f64091 bellard
        return ret;
673 3b0d4f61 bellard
    }
674 71d0770c aliguori
    bs->growable = 1;
675 83f64091 bellard
    *pbs = bs;
676 83f64091 bellard
    return 0;
677 83f64091 bellard
}
678 83f64091 bellard
679 b6ce07aa Kevin Wolf
/*
680 b6ce07aa Kevin Wolf
 * Opens a disk image (raw, qcow2, vmdk, ...)
681 b6ce07aa Kevin Wolf
 */
682 d6e9098e Kevin Wolf
int bdrv_open(BlockDriverState *bs, const char *filename, int flags,
683 d6e9098e Kevin Wolf
              BlockDriver *drv)
684 ea2384d3 bellard
{
685 b6ce07aa Kevin Wolf
    int ret;
686 2b572816 Kevin Wolf
    char tmp_filename[PATH_MAX];
687 712e7874 bellard
688 83f64091 bellard
    if (flags & BDRV_O_SNAPSHOT) {
689 ea2384d3 bellard
        BlockDriverState *bs1;
690 ea2384d3 bellard
        int64_t total_size;
691 7c96d46e aliguori
        int is_protocol = 0;
692 91a073a9 Kevin Wolf
        BlockDriver *bdrv_qcow2;
693 91a073a9 Kevin Wolf
        QEMUOptionParameter *options;
694 b6ce07aa Kevin Wolf
        char backing_filename[PATH_MAX];
695 3b46e624 ths
696 ea2384d3 bellard
        /* if snapshot, we create a temporary backing file and open it
697 ea2384d3 bellard
           instead of opening 'filename' directly */
698 33e3963e bellard
699 ea2384d3 bellard
        /* if there is a backing file, use it */
700 ea2384d3 bellard
        bs1 = bdrv_new("");
701 d6e9098e Kevin Wolf
        ret = bdrv_open(bs1, filename, 0, drv);
702 51d7c00c aliguori
        if (ret < 0) {
703 ea2384d3 bellard
            bdrv_delete(bs1);
704 51d7c00c aliguori
            return ret;
705 ea2384d3 bellard
        }
706 3e82990b Jes Sorensen
        total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
707 7c96d46e aliguori
708 7c96d46e aliguori
        if (bs1->drv && bs1->drv->protocol_name)
709 7c96d46e aliguori
            is_protocol = 1;
710 7c96d46e aliguori
711 ea2384d3 bellard
        bdrv_delete(bs1);
712 3b46e624 ths
713 ea2384d3 bellard
        get_tmp_filename(tmp_filename, sizeof(tmp_filename));
714 7c96d46e aliguori
715 7c96d46e aliguori
        /* Real path is meaningless for protocols */
716 7c96d46e aliguori
        if (is_protocol)
717 7c96d46e aliguori
            snprintf(backing_filename, sizeof(backing_filename),
718 7c96d46e aliguori
                     "%s", filename);
719 114cdfa9 Kirill A. Shutemov
        else if (!realpath(filename, backing_filename))
720 114cdfa9 Kirill A. Shutemov
            return -errno;
721 7c96d46e aliguori
722 91a073a9 Kevin Wolf
        bdrv_qcow2 = bdrv_find_format("qcow2");
723 91a073a9 Kevin Wolf
        options = parse_option_parameters("", bdrv_qcow2->create_options, NULL);
724 91a073a9 Kevin Wolf
725 3e82990b Jes Sorensen
        set_option_parameter_int(options, BLOCK_OPT_SIZE, total_size);
726 91a073a9 Kevin Wolf
        set_option_parameter(options, BLOCK_OPT_BACKING_FILE, backing_filename);
727 91a073a9 Kevin Wolf
        if (drv) {
728 91a073a9 Kevin Wolf
            set_option_parameter(options, BLOCK_OPT_BACKING_FMT,
729 91a073a9 Kevin Wolf
                drv->format_name);
730 91a073a9 Kevin Wolf
        }
731 91a073a9 Kevin Wolf
732 91a073a9 Kevin Wolf
        ret = bdrv_create(bdrv_qcow2, tmp_filename, options);
733 d748768c Jan Kiszka
        free_option_parameters(options);
734 51d7c00c aliguori
        if (ret < 0) {
735 51d7c00c aliguori
            return ret;
736 ea2384d3 bellard
        }
737 91a073a9 Kevin Wolf
738 ea2384d3 bellard
        filename = tmp_filename;
739 91a073a9 Kevin Wolf
        drv = bdrv_qcow2;
740 ea2384d3 bellard
        bs->is_temporary = 1;
741 ea2384d3 bellard
    }
742 712e7874 bellard
743 b6ce07aa Kevin Wolf
    /* Find the right image format driver */
744 6db95603 Christoph Hellwig
    if (!drv) {
745 c98ac35d Stefan Weil
        ret = find_image_format(filename, &drv);
746 51d7c00c aliguori
    }
747 6987307c Christoph Hellwig
748 51d7c00c aliguori
    if (!drv) {
749 51d7c00c aliguori
        goto unlink_and_fail;
750 ea2384d3 bellard
    }
751 b6ce07aa Kevin Wolf
752 b6ce07aa Kevin Wolf
    /* Open the image */
753 b6ce07aa Kevin Wolf
    ret = bdrv_open_common(bs, filename, flags, drv);
754 b6ce07aa Kevin Wolf
    if (ret < 0) {
755 6987307c Christoph Hellwig
        goto unlink_and_fail;
756 6987307c Christoph Hellwig
    }
757 6987307c Christoph Hellwig
758 b6ce07aa Kevin Wolf
    /* If there is a backing file, use it */
759 b6ce07aa Kevin Wolf
    if ((flags & BDRV_O_NO_BACKING) == 0 && bs->backing_file[0] != '\0') {
760 b6ce07aa Kevin Wolf
        char backing_filename[PATH_MAX];
761 b6ce07aa Kevin Wolf
        int back_flags;
762 b6ce07aa Kevin Wolf
        BlockDriver *back_drv = NULL;
763 b6ce07aa Kevin Wolf
764 b6ce07aa Kevin Wolf
        bs->backing_hd = bdrv_new("");
765 df2dbb4a Stefan Hajnoczi
766 df2dbb4a Stefan Hajnoczi
        if (path_has_protocol(bs->backing_file)) {
767 df2dbb4a Stefan Hajnoczi
            pstrcpy(backing_filename, sizeof(backing_filename),
768 df2dbb4a Stefan Hajnoczi
                    bs->backing_file);
769 df2dbb4a Stefan Hajnoczi
        } else {
770 df2dbb4a Stefan Hajnoczi
            path_combine(backing_filename, sizeof(backing_filename),
771 df2dbb4a Stefan Hajnoczi
                         filename, bs->backing_file);
772 df2dbb4a Stefan Hajnoczi
        }
773 df2dbb4a Stefan Hajnoczi
774 df2dbb4a Stefan Hajnoczi
        if (bs->backing_format[0] != '\0') {
775 b6ce07aa Kevin Wolf
            back_drv = bdrv_find_format(bs->backing_format);
776 df2dbb4a Stefan Hajnoczi
        }
777 b6ce07aa Kevin Wolf
778 b6ce07aa Kevin Wolf
        /* backing files always opened read-only */
779 b6ce07aa Kevin Wolf
        back_flags =
780 b6ce07aa Kevin Wolf
            flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
781 b6ce07aa Kevin Wolf
782 b6ce07aa Kevin Wolf
        ret = bdrv_open(bs->backing_hd, backing_filename, back_flags, back_drv);
783 b6ce07aa Kevin Wolf
        if (ret < 0) {
784 b6ce07aa Kevin Wolf
            bdrv_close(bs);
785 b6ce07aa Kevin Wolf
            return ret;
786 b6ce07aa Kevin Wolf
        }
787 b6ce07aa Kevin Wolf
        if (bs->is_temporary) {
788 b6ce07aa Kevin Wolf
            bs->backing_hd->keep_read_only = !(flags & BDRV_O_RDWR);
789 b6ce07aa Kevin Wolf
        } else {
790 b6ce07aa Kevin Wolf
            /* base image inherits from "parent" */
791 b6ce07aa Kevin Wolf
            bs->backing_hd->keep_read_only = bs->keep_read_only;
792 b6ce07aa Kevin Wolf
        }
793 b6ce07aa Kevin Wolf
    }
794 b6ce07aa Kevin Wolf
795 b6ce07aa Kevin Wolf
    if (!bdrv_key_required(bs)) {
796 7d4b4ba5 Markus Armbruster
        bdrv_dev_change_media_cb(bs, true);
797 b6ce07aa Kevin Wolf
    }
798 b6ce07aa Kevin Wolf
799 98f90dba Zhi Yong Wu
    /* throttling disk I/O limits */
800 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
801 98f90dba Zhi Yong Wu
        bdrv_io_limits_enable(bs);
802 98f90dba Zhi Yong Wu
    }
803 98f90dba Zhi Yong Wu
804 b6ce07aa Kevin Wolf
    return 0;
805 b6ce07aa Kevin Wolf
806 b6ce07aa Kevin Wolf
unlink_and_fail:
807 b6ce07aa Kevin Wolf
    if (bs->is_temporary) {
808 b6ce07aa Kevin Wolf
        unlink(filename);
809 b6ce07aa Kevin Wolf
    }
810 b6ce07aa Kevin Wolf
    return ret;
811 b6ce07aa Kevin Wolf
}
812 b6ce07aa Kevin Wolf
813 fc01f7e7 bellard
void bdrv_close(BlockDriverState *bs)
814 fc01f7e7 bellard
{
815 19cb3738 bellard
    if (bs->drv) {
816 f9092b10 Markus Armbruster
        if (bs == bs_snapshots) {
817 f9092b10 Markus Armbruster
            bs_snapshots = NULL;
818 f9092b10 Markus Armbruster
        }
819 557df6ac Stefan Hajnoczi
        if (bs->backing_hd) {
820 ea2384d3 bellard
            bdrv_delete(bs->backing_hd);
821 557df6ac Stefan Hajnoczi
            bs->backing_hd = NULL;
822 557df6ac Stefan Hajnoczi
        }
823 ea2384d3 bellard
        bs->drv->bdrv_close(bs);
824 7267c094 Anthony Liguori
        g_free(bs->opaque);
825 ea2384d3 bellard
#ifdef _WIN32
826 ea2384d3 bellard
        if (bs->is_temporary) {
827 ea2384d3 bellard
            unlink(bs->filename);
828 ea2384d3 bellard
        }
829 67b915a5 bellard
#endif
830 ea2384d3 bellard
        bs->opaque = NULL;
831 ea2384d3 bellard
        bs->drv = NULL;
832 53fec9d3 Stefan Hajnoczi
        bs->copy_on_read = 0;
833 b338082b bellard
834 66f82cee Kevin Wolf
        if (bs->file != NULL) {
835 66f82cee Kevin Wolf
            bdrv_close(bs->file);
836 66f82cee Kevin Wolf
        }
837 66f82cee Kevin Wolf
838 7d4b4ba5 Markus Armbruster
        bdrv_dev_change_media_cb(bs, false);
839 b338082b bellard
    }
840 98f90dba Zhi Yong Wu
841 98f90dba Zhi Yong Wu
    /*throttling disk I/O limits*/
842 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
843 98f90dba Zhi Yong Wu
        bdrv_io_limits_disable(bs);
844 98f90dba Zhi Yong Wu
    }
845 b338082b bellard
}
846 b338082b bellard
847 2bc93fed MORITA Kazutaka
void bdrv_close_all(void)
848 2bc93fed MORITA Kazutaka
{
849 2bc93fed MORITA Kazutaka
    BlockDriverState *bs;
850 2bc93fed MORITA Kazutaka
851 2bc93fed MORITA Kazutaka
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
852 2bc93fed MORITA Kazutaka
        bdrv_close(bs);
853 2bc93fed MORITA Kazutaka
    }
854 2bc93fed MORITA Kazutaka
}
855 2bc93fed MORITA Kazutaka
856 922453bc Stefan Hajnoczi
/*
857 922453bc Stefan Hajnoczi
 * Wait for pending requests to complete across all BlockDriverStates
858 922453bc Stefan Hajnoczi
 *
859 922453bc Stefan Hajnoczi
 * This function does not flush data to disk, use bdrv_flush_all() for that
860 922453bc Stefan Hajnoczi
 * after calling this function.
861 922453bc Stefan Hajnoczi
 */
862 922453bc Stefan Hajnoczi
void bdrv_drain_all(void)
863 922453bc Stefan Hajnoczi
{
864 922453bc Stefan Hajnoczi
    BlockDriverState *bs;
865 922453bc Stefan Hajnoczi
866 922453bc Stefan Hajnoczi
    qemu_aio_flush();
867 922453bc Stefan Hajnoczi
868 922453bc Stefan Hajnoczi
    /* If requests are still pending there is a bug somewhere */
869 922453bc Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
870 922453bc Stefan Hajnoczi
        assert(QLIST_EMPTY(&bs->tracked_requests));
871 922453bc Stefan Hajnoczi
        assert(qemu_co_queue_empty(&bs->throttled_reqs));
872 922453bc Stefan Hajnoczi
    }
873 922453bc Stefan Hajnoczi
}
874 922453bc Stefan Hajnoczi
875 d22b2f41 Ryan Harper
/* make a BlockDriverState anonymous by removing from bdrv_state list.
876 d22b2f41 Ryan Harper
   Also, NULL terminate the device_name to prevent double remove */
877 d22b2f41 Ryan Harper
void bdrv_make_anon(BlockDriverState *bs)
878 d22b2f41 Ryan Harper
{
879 d22b2f41 Ryan Harper
    if (bs->device_name[0] != '\0') {
880 d22b2f41 Ryan Harper
        QTAILQ_REMOVE(&bdrv_states, bs, list);
881 d22b2f41 Ryan Harper
    }
882 d22b2f41 Ryan Harper
    bs->device_name[0] = '\0';
883 d22b2f41 Ryan Harper
}
884 d22b2f41 Ryan Harper
885 8802d1fd Jeff Cody
/*
886 8802d1fd Jeff Cody
 * Add new bs contents at the top of an image chain while the chain is
887 8802d1fd Jeff Cody
 * live, while keeping required fields on the top layer.
888 8802d1fd Jeff Cody
 *
889 8802d1fd Jeff Cody
 * This will modify the BlockDriverState fields, and swap contents
890 8802d1fd Jeff Cody
 * between bs_new and bs_top. Both bs_new and bs_top are modified.
891 8802d1fd Jeff Cody
 *
892 8802d1fd Jeff Cody
 * This function does not create any image files.
893 8802d1fd Jeff Cody
 */
894 8802d1fd Jeff Cody
void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
895 8802d1fd Jeff Cody
{
896 8802d1fd Jeff Cody
    BlockDriverState tmp;
897 8802d1fd Jeff Cody
898 8802d1fd Jeff Cody
    /* the new bs must not be in bdrv_states */
899 8802d1fd Jeff Cody
    bdrv_make_anon(bs_new);
900 8802d1fd Jeff Cody
901 8802d1fd Jeff Cody
    tmp = *bs_new;
902 8802d1fd Jeff Cody
903 8802d1fd Jeff Cody
    /* there are some fields that need to stay on the top layer: */
904 8802d1fd Jeff Cody
905 8802d1fd Jeff Cody
    /* dev info */
906 8802d1fd Jeff Cody
    tmp.dev_ops           = bs_top->dev_ops;
907 8802d1fd Jeff Cody
    tmp.dev_opaque        = bs_top->dev_opaque;
908 8802d1fd Jeff Cody
    tmp.dev               = bs_top->dev;
909 8802d1fd Jeff Cody
    tmp.buffer_alignment  = bs_top->buffer_alignment;
910 8802d1fd Jeff Cody
    tmp.copy_on_read      = bs_top->copy_on_read;
911 8802d1fd Jeff Cody
912 8802d1fd Jeff Cody
    /* i/o timing parameters */
913 8802d1fd Jeff Cody
    tmp.slice_time        = bs_top->slice_time;
914 8802d1fd Jeff Cody
    tmp.slice_start       = bs_top->slice_start;
915 8802d1fd Jeff Cody
    tmp.slice_end         = bs_top->slice_end;
916 8802d1fd Jeff Cody
    tmp.io_limits         = bs_top->io_limits;
917 8802d1fd Jeff Cody
    tmp.io_base           = bs_top->io_base;
918 8802d1fd Jeff Cody
    tmp.throttled_reqs    = bs_top->throttled_reqs;
919 8802d1fd Jeff Cody
    tmp.block_timer       = bs_top->block_timer;
920 8802d1fd Jeff Cody
    tmp.io_limits_enabled = bs_top->io_limits_enabled;
921 8802d1fd Jeff Cody
922 8802d1fd Jeff Cody
    /* geometry */
923 8802d1fd Jeff Cody
    tmp.cyls              = bs_top->cyls;
924 8802d1fd Jeff Cody
    tmp.heads             = bs_top->heads;
925 8802d1fd Jeff Cody
    tmp.secs              = bs_top->secs;
926 8802d1fd Jeff Cody
    tmp.translation       = bs_top->translation;
927 8802d1fd Jeff Cody
928 8802d1fd Jeff Cody
    /* r/w error */
929 8802d1fd Jeff Cody
    tmp.on_read_error     = bs_top->on_read_error;
930 8802d1fd Jeff Cody
    tmp.on_write_error    = bs_top->on_write_error;
931 8802d1fd Jeff Cody
932 8802d1fd Jeff Cody
    /* i/o status */
933 8802d1fd Jeff Cody
    tmp.iostatus_enabled  = bs_top->iostatus_enabled;
934 8802d1fd Jeff Cody
    tmp.iostatus          = bs_top->iostatus;
935 8802d1fd Jeff Cody
936 8802d1fd Jeff Cody
    /* keep the same entry in bdrv_states */
937 8802d1fd Jeff Cody
    pstrcpy(tmp.device_name, sizeof(tmp.device_name), bs_top->device_name);
938 8802d1fd Jeff Cody
    tmp.list = bs_top->list;
939 8802d1fd Jeff Cody
940 8802d1fd Jeff Cody
    /* The contents of 'tmp' will become bs_top, as we are
941 8802d1fd Jeff Cody
     * swapping bs_new and bs_top contents. */
942 8802d1fd Jeff Cody
    tmp.backing_hd = bs_new;
943 8802d1fd Jeff Cody
    pstrcpy(tmp.backing_file, sizeof(tmp.backing_file), bs_top->filename);
944 8802d1fd Jeff Cody
945 8802d1fd Jeff Cody
    /* swap contents of the fixed new bs and the current top */
946 8802d1fd Jeff Cody
    *bs_new = *bs_top;
947 8802d1fd Jeff Cody
    *bs_top = tmp;
948 8802d1fd Jeff Cody
949 8802d1fd Jeff Cody
    /* clear the copied fields in the new backing file */
950 8802d1fd Jeff Cody
    bdrv_detach_dev(bs_new, bs_new->dev);
951 8802d1fd Jeff Cody
952 8802d1fd Jeff Cody
    qemu_co_queue_init(&bs_new->throttled_reqs);
953 8802d1fd Jeff Cody
    memset(&bs_new->io_base,   0, sizeof(bs_new->io_base));
954 8802d1fd Jeff Cody
    memset(&bs_new->io_limits, 0, sizeof(bs_new->io_limits));
955 8802d1fd Jeff Cody
    bdrv_iostatus_disable(bs_new);
956 8802d1fd Jeff Cody
957 8802d1fd Jeff Cody
    /* we don't use bdrv_io_limits_disable() for this, because we don't want
958 8802d1fd Jeff Cody
     * to affect or delete the block_timer, as it has been moved to bs_top */
959 8802d1fd Jeff Cody
    bs_new->io_limits_enabled = false;
960 8802d1fd Jeff Cody
    bs_new->block_timer       = NULL;
961 8802d1fd Jeff Cody
    bs_new->slice_time        = 0;
962 8802d1fd Jeff Cody
    bs_new->slice_start       = 0;
963 8802d1fd Jeff Cody
    bs_new->slice_end         = 0;
964 8802d1fd Jeff Cody
}
965 8802d1fd Jeff Cody
966 b338082b bellard
void bdrv_delete(BlockDriverState *bs)
967 b338082b bellard
{
968 fa879d62 Markus Armbruster
    assert(!bs->dev);
969 18846dee Markus Armbruster
970 1b7bdbc1 Stefan Hajnoczi
    /* remove from list, if necessary */
971 d22b2f41 Ryan Harper
    bdrv_make_anon(bs);
972 34c6f050 aurel32
973 b338082b bellard
    bdrv_close(bs);
974 66f82cee Kevin Wolf
    if (bs->file != NULL) {
975 66f82cee Kevin Wolf
        bdrv_delete(bs->file);
976 66f82cee Kevin Wolf
    }
977 66f82cee Kevin Wolf
978 f9092b10 Markus Armbruster
    assert(bs != bs_snapshots);
979 7267c094 Anthony Liguori
    g_free(bs);
980 fc01f7e7 bellard
}
981 fc01f7e7 bellard
982 fa879d62 Markus Armbruster
int bdrv_attach_dev(BlockDriverState *bs, void *dev)
983 fa879d62 Markus Armbruster
/* TODO change to DeviceState *dev when all users are qdevified */
984 18846dee Markus Armbruster
{
985 fa879d62 Markus Armbruster
    if (bs->dev) {
986 18846dee Markus Armbruster
        return -EBUSY;
987 18846dee Markus Armbruster
    }
988 fa879d62 Markus Armbruster
    bs->dev = dev;
989 28a7282a Luiz Capitulino
    bdrv_iostatus_reset(bs);
990 18846dee Markus Armbruster
    return 0;
991 18846dee Markus Armbruster
}
992 18846dee Markus Armbruster
993 fa879d62 Markus Armbruster
/* TODO qdevified devices don't use this, remove when devices are qdevified */
994 fa879d62 Markus Armbruster
void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
995 18846dee Markus Armbruster
{
996 fa879d62 Markus Armbruster
    if (bdrv_attach_dev(bs, dev) < 0) {
997 fa879d62 Markus Armbruster
        abort();
998 fa879d62 Markus Armbruster
    }
999 fa879d62 Markus Armbruster
}
1000 fa879d62 Markus Armbruster
1001 fa879d62 Markus Armbruster
void bdrv_detach_dev(BlockDriverState *bs, void *dev)
1002 fa879d62 Markus Armbruster
/* TODO change to DeviceState *dev when all users are qdevified */
1003 fa879d62 Markus Armbruster
{
1004 fa879d62 Markus Armbruster
    assert(bs->dev == dev);
1005 fa879d62 Markus Armbruster
    bs->dev = NULL;
1006 0e49de52 Markus Armbruster
    bs->dev_ops = NULL;
1007 0e49de52 Markus Armbruster
    bs->dev_opaque = NULL;
1008 29e05f20 Markus Armbruster
    bs->buffer_alignment = 512;
1009 18846dee Markus Armbruster
}
1010 18846dee Markus Armbruster
1011 fa879d62 Markus Armbruster
/* TODO change to return DeviceState * when all users are qdevified */
1012 fa879d62 Markus Armbruster
void *bdrv_get_attached_dev(BlockDriverState *bs)
1013 18846dee Markus Armbruster
{
1014 fa879d62 Markus Armbruster
    return bs->dev;
1015 18846dee Markus Armbruster
}
1016 18846dee Markus Armbruster
1017 0e49de52 Markus Armbruster
void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
1018 0e49de52 Markus Armbruster
                      void *opaque)
1019 0e49de52 Markus Armbruster
{
1020 0e49de52 Markus Armbruster
    bs->dev_ops = ops;
1021 0e49de52 Markus Armbruster
    bs->dev_opaque = opaque;
1022 2c6942fa Markus Armbruster
    if (bdrv_dev_has_removable_media(bs) && bs == bs_snapshots) {
1023 2c6942fa Markus Armbruster
        bs_snapshots = NULL;
1024 2c6942fa Markus Armbruster
    }
1025 0e49de52 Markus Armbruster
}
1026 0e49de52 Markus Armbruster
1027 329c0a48 Luiz Capitulino
void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
1028 329c0a48 Luiz Capitulino
                               BlockQMPEventAction action, int is_read)
1029 329c0a48 Luiz Capitulino
{
1030 329c0a48 Luiz Capitulino
    QObject *data;
1031 329c0a48 Luiz Capitulino
    const char *action_str;
1032 329c0a48 Luiz Capitulino
1033 329c0a48 Luiz Capitulino
    switch (action) {
1034 329c0a48 Luiz Capitulino
    case BDRV_ACTION_REPORT:
1035 329c0a48 Luiz Capitulino
        action_str = "report";
1036 329c0a48 Luiz Capitulino
        break;
1037 329c0a48 Luiz Capitulino
    case BDRV_ACTION_IGNORE:
1038 329c0a48 Luiz Capitulino
        action_str = "ignore";
1039 329c0a48 Luiz Capitulino
        break;
1040 329c0a48 Luiz Capitulino
    case BDRV_ACTION_STOP:
1041 329c0a48 Luiz Capitulino
        action_str = "stop";
1042 329c0a48 Luiz Capitulino
        break;
1043 329c0a48 Luiz Capitulino
    default:
1044 329c0a48 Luiz Capitulino
        abort();
1045 329c0a48 Luiz Capitulino
    }
1046 329c0a48 Luiz Capitulino
1047 329c0a48 Luiz Capitulino
    data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1048 329c0a48 Luiz Capitulino
                              bdrv->device_name,
1049 329c0a48 Luiz Capitulino
                              action_str,
1050 329c0a48 Luiz Capitulino
                              is_read ? "read" : "write");
1051 329c0a48 Luiz Capitulino
    monitor_protocol_event(QEVENT_BLOCK_IO_ERROR, data);
1052 329c0a48 Luiz Capitulino
1053 329c0a48 Luiz Capitulino
    qobject_decref(data);
1054 329c0a48 Luiz Capitulino
}
1055 329c0a48 Luiz Capitulino
1056 6f382ed2 Luiz Capitulino
static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected)
1057 6f382ed2 Luiz Capitulino
{
1058 6f382ed2 Luiz Capitulino
    QObject *data;
1059 6f382ed2 Luiz Capitulino
1060 6f382ed2 Luiz Capitulino
    data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }",
1061 6f382ed2 Luiz Capitulino
                              bdrv_get_device_name(bs), ejected);
1062 6f382ed2 Luiz Capitulino
    monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data);
1063 6f382ed2 Luiz Capitulino
1064 6f382ed2 Luiz Capitulino
    qobject_decref(data);
1065 6f382ed2 Luiz Capitulino
}
1066 6f382ed2 Luiz Capitulino
1067 7d4b4ba5 Markus Armbruster
static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
1068 0e49de52 Markus Armbruster
{
1069 145feb17 Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->change_media_cb) {
1070 6f382ed2 Luiz Capitulino
        bool tray_was_closed = !bdrv_dev_is_tray_open(bs);
1071 7d4b4ba5 Markus Armbruster
        bs->dev_ops->change_media_cb(bs->dev_opaque, load);
1072 6f382ed2 Luiz Capitulino
        if (tray_was_closed) {
1073 6f382ed2 Luiz Capitulino
            /* tray open */
1074 6f382ed2 Luiz Capitulino
            bdrv_emit_qmp_eject_event(bs, true);
1075 6f382ed2 Luiz Capitulino
        }
1076 6f382ed2 Luiz Capitulino
        if (load) {
1077 6f382ed2 Luiz Capitulino
            /* tray close */
1078 6f382ed2 Luiz Capitulino
            bdrv_emit_qmp_eject_event(bs, false);
1079 6f382ed2 Luiz Capitulino
        }
1080 145feb17 Markus Armbruster
    }
1081 145feb17 Markus Armbruster
}
1082 145feb17 Markus Armbruster
1083 2c6942fa Markus Armbruster
bool bdrv_dev_has_removable_media(BlockDriverState *bs)
1084 2c6942fa Markus Armbruster
{
1085 2c6942fa Markus Armbruster
    return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
1086 2c6942fa Markus Armbruster
}
1087 2c6942fa Markus Armbruster
1088 025ccaa7 Paolo Bonzini
void bdrv_dev_eject_request(BlockDriverState *bs, bool force)
1089 025ccaa7 Paolo Bonzini
{
1090 025ccaa7 Paolo Bonzini
    if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
1091 025ccaa7 Paolo Bonzini
        bs->dev_ops->eject_request_cb(bs->dev_opaque, force);
1092 025ccaa7 Paolo Bonzini
    }
1093 025ccaa7 Paolo Bonzini
}
1094 025ccaa7 Paolo Bonzini
1095 e4def80b Markus Armbruster
bool bdrv_dev_is_tray_open(BlockDriverState *bs)
1096 e4def80b Markus Armbruster
{
1097 e4def80b Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->is_tray_open) {
1098 e4def80b Markus Armbruster
        return bs->dev_ops->is_tray_open(bs->dev_opaque);
1099 e4def80b Markus Armbruster
    }
1100 e4def80b Markus Armbruster
    return false;
1101 e4def80b Markus Armbruster
}
1102 e4def80b Markus Armbruster
1103 145feb17 Markus Armbruster
static void bdrv_dev_resize_cb(BlockDriverState *bs)
1104 145feb17 Markus Armbruster
{
1105 145feb17 Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->resize_cb) {
1106 145feb17 Markus Armbruster
        bs->dev_ops->resize_cb(bs->dev_opaque);
1107 0e49de52 Markus Armbruster
    }
1108 0e49de52 Markus Armbruster
}
1109 0e49de52 Markus Armbruster
1110 f107639a Markus Armbruster
bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
1111 f107639a Markus Armbruster
{
1112 f107639a Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
1113 f107639a Markus Armbruster
        return bs->dev_ops->is_medium_locked(bs->dev_opaque);
1114 f107639a Markus Armbruster
    }
1115 f107639a Markus Armbruster
    return false;
1116 f107639a Markus Armbruster
}
1117 f107639a Markus Armbruster
1118 e97fc193 aliguori
/*
1119 e97fc193 aliguori
 * Run consistency checks on an image
1120 e97fc193 aliguori
 *
1121 e076f338 Kevin Wolf
 * Returns 0 if the check could be completed (it doesn't mean that the image is
1122 a1c7273b Stefan Weil
 * free of errors) or -errno when an internal error occurred. The results of the
1123 e076f338 Kevin Wolf
 * check are stored in res.
1124 e97fc193 aliguori
 */
1125 e076f338 Kevin Wolf
int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res)
1126 e97fc193 aliguori
{
1127 e97fc193 aliguori
    if (bs->drv->bdrv_check == NULL) {
1128 e97fc193 aliguori
        return -ENOTSUP;
1129 e97fc193 aliguori
    }
1130 e97fc193 aliguori
1131 e076f338 Kevin Wolf
    memset(res, 0, sizeof(*res));
1132 9ac228e0 Kevin Wolf
    return bs->drv->bdrv_check(bs, res);
1133 e97fc193 aliguori
}
1134 e97fc193 aliguori
1135 8a426614 Kevin Wolf
#define COMMIT_BUF_SECTORS 2048
1136 8a426614 Kevin Wolf
1137 33e3963e bellard
/* commit COW file into the raw image */
1138 33e3963e bellard
int bdrv_commit(BlockDriverState *bs)
1139 33e3963e bellard
{
1140 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1141 ee181196 Kevin Wolf
    BlockDriver *backing_drv;
1142 8a426614 Kevin Wolf
    int64_t sector, total_sectors;
1143 8a426614 Kevin Wolf
    int n, ro, open_flags;
1144 4dca4b63 Naphtali Sprei
    int ret = 0, rw_ret = 0;
1145 8a426614 Kevin Wolf
    uint8_t *buf;
1146 4dca4b63 Naphtali Sprei
    char filename[1024];
1147 4dca4b63 Naphtali Sprei
    BlockDriverState *bs_rw, *bs_ro;
1148 33e3963e bellard
1149 19cb3738 bellard
    if (!drv)
1150 19cb3738 bellard
        return -ENOMEDIUM;
1151 4dca4b63 Naphtali Sprei
    
1152 4dca4b63 Naphtali Sprei
    if (!bs->backing_hd) {
1153 4dca4b63 Naphtali Sprei
        return -ENOTSUP;
1154 33e3963e bellard
    }
1155 33e3963e bellard
1156 4dca4b63 Naphtali Sprei
    if (bs->backing_hd->keep_read_only) {
1157 4dca4b63 Naphtali Sprei
        return -EACCES;
1158 4dca4b63 Naphtali Sprei
    }
1159 ee181196 Kevin Wolf
1160 2d3735d3 Stefan Hajnoczi
    if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
1161 2d3735d3 Stefan Hajnoczi
        return -EBUSY;
1162 2d3735d3 Stefan Hajnoczi
    }
1163 2d3735d3 Stefan Hajnoczi
1164 ee181196 Kevin Wolf
    backing_drv = bs->backing_hd->drv;
1165 4dca4b63 Naphtali Sprei
    ro = bs->backing_hd->read_only;
1166 4dca4b63 Naphtali Sprei
    strncpy(filename, bs->backing_hd->filename, sizeof(filename));
1167 4dca4b63 Naphtali Sprei
    open_flags =  bs->backing_hd->open_flags;
1168 4dca4b63 Naphtali Sprei
1169 4dca4b63 Naphtali Sprei
    if (ro) {
1170 4dca4b63 Naphtali Sprei
        /* re-open as RW */
1171 4dca4b63 Naphtali Sprei
        bdrv_delete(bs->backing_hd);
1172 4dca4b63 Naphtali Sprei
        bs->backing_hd = NULL;
1173 4dca4b63 Naphtali Sprei
        bs_rw = bdrv_new("");
1174 ee181196 Kevin Wolf
        rw_ret = bdrv_open(bs_rw, filename, open_flags | BDRV_O_RDWR,
1175 ee181196 Kevin Wolf
            backing_drv);
1176 4dca4b63 Naphtali Sprei
        if (rw_ret < 0) {
1177 4dca4b63 Naphtali Sprei
            bdrv_delete(bs_rw);
1178 4dca4b63 Naphtali Sprei
            /* try to re-open read-only */
1179 4dca4b63 Naphtali Sprei
            bs_ro = bdrv_new("");
1180 ee181196 Kevin Wolf
            ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
1181 ee181196 Kevin Wolf
                backing_drv);
1182 4dca4b63 Naphtali Sprei
            if (ret < 0) {
1183 4dca4b63 Naphtali Sprei
                bdrv_delete(bs_ro);
1184 4dca4b63 Naphtali Sprei
                /* drive not functional anymore */
1185 4dca4b63 Naphtali Sprei
                bs->drv = NULL;
1186 4dca4b63 Naphtali Sprei
                return ret;
1187 4dca4b63 Naphtali Sprei
            }
1188 4dca4b63 Naphtali Sprei
            bs->backing_hd = bs_ro;
1189 4dca4b63 Naphtali Sprei
            return rw_ret;
1190 4dca4b63 Naphtali Sprei
        }
1191 4dca4b63 Naphtali Sprei
        bs->backing_hd = bs_rw;
1192 ea2384d3 bellard
    }
1193 33e3963e bellard
1194 6ea44308 Jan Kiszka
    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
1195 7267c094 Anthony Liguori
    buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
1196 8a426614 Kevin Wolf
1197 8a426614 Kevin Wolf
    for (sector = 0; sector < total_sectors; sector += n) {
1198 05c4af54 Stefan Hajnoczi
        if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
1199 8a426614 Kevin Wolf
1200 8a426614 Kevin Wolf
            if (bdrv_read(bs, sector, buf, n) != 0) {
1201 8a426614 Kevin Wolf
                ret = -EIO;
1202 8a426614 Kevin Wolf
                goto ro_cleanup;
1203 8a426614 Kevin Wolf
            }
1204 8a426614 Kevin Wolf
1205 8a426614 Kevin Wolf
            if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
1206 8a426614 Kevin Wolf
                ret = -EIO;
1207 8a426614 Kevin Wolf
                goto ro_cleanup;
1208 8a426614 Kevin Wolf
            }
1209 ea2384d3 bellard
        }
1210 33e3963e bellard
    }
1211 95389c86 bellard
1212 1d44952f Christoph Hellwig
    if (drv->bdrv_make_empty) {
1213 1d44952f Christoph Hellwig
        ret = drv->bdrv_make_empty(bs);
1214 1d44952f Christoph Hellwig
        bdrv_flush(bs);
1215 1d44952f Christoph Hellwig
    }
1216 95389c86 bellard
1217 3f5075ae Christoph Hellwig
    /*
1218 3f5075ae Christoph Hellwig
     * Make sure all data we wrote to the backing device is actually
1219 3f5075ae Christoph Hellwig
     * stable on disk.
1220 3f5075ae Christoph Hellwig
     */
1221 3f5075ae Christoph Hellwig
    if (bs->backing_hd)
1222 3f5075ae Christoph Hellwig
        bdrv_flush(bs->backing_hd);
1223 4dca4b63 Naphtali Sprei
1224 4dca4b63 Naphtali Sprei
ro_cleanup:
1225 7267c094 Anthony Liguori
    g_free(buf);
1226 4dca4b63 Naphtali Sprei
1227 4dca4b63 Naphtali Sprei
    if (ro) {
1228 4dca4b63 Naphtali Sprei
        /* re-open as RO */
1229 4dca4b63 Naphtali Sprei
        bdrv_delete(bs->backing_hd);
1230 4dca4b63 Naphtali Sprei
        bs->backing_hd = NULL;
1231 4dca4b63 Naphtali Sprei
        bs_ro = bdrv_new("");
1232 ee181196 Kevin Wolf
        ret = bdrv_open(bs_ro, filename, open_flags & ~BDRV_O_RDWR,
1233 ee181196 Kevin Wolf
            backing_drv);
1234 4dca4b63 Naphtali Sprei
        if (ret < 0) {
1235 4dca4b63 Naphtali Sprei
            bdrv_delete(bs_ro);
1236 4dca4b63 Naphtali Sprei
            /* drive not functional anymore */
1237 4dca4b63 Naphtali Sprei
            bs->drv = NULL;
1238 4dca4b63 Naphtali Sprei
            return ret;
1239 4dca4b63 Naphtali Sprei
        }
1240 4dca4b63 Naphtali Sprei
        bs->backing_hd = bs_ro;
1241 4dca4b63 Naphtali Sprei
        bs->backing_hd->keep_read_only = 0;
1242 4dca4b63 Naphtali Sprei
    }
1243 4dca4b63 Naphtali Sprei
1244 1d44952f Christoph Hellwig
    return ret;
1245 33e3963e bellard
}
1246 33e3963e bellard
1247 6ab4b5ab Markus Armbruster
void bdrv_commit_all(void)
1248 6ab4b5ab Markus Armbruster
{
1249 6ab4b5ab Markus Armbruster
    BlockDriverState *bs;
1250 6ab4b5ab Markus Armbruster
1251 6ab4b5ab Markus Armbruster
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1252 6ab4b5ab Markus Armbruster
        bdrv_commit(bs);
1253 6ab4b5ab Markus Armbruster
    }
1254 6ab4b5ab Markus Armbruster
}
1255 6ab4b5ab Markus Armbruster
1256 dbffbdcf Stefan Hajnoczi
struct BdrvTrackedRequest {
1257 dbffbdcf Stefan Hajnoczi
    BlockDriverState *bs;
1258 dbffbdcf Stefan Hajnoczi
    int64_t sector_num;
1259 dbffbdcf Stefan Hajnoczi
    int nb_sectors;
1260 dbffbdcf Stefan Hajnoczi
    bool is_write;
1261 dbffbdcf Stefan Hajnoczi
    QLIST_ENTRY(BdrvTrackedRequest) list;
1262 5f8b6491 Stefan Hajnoczi
    Coroutine *co; /* owner, used for deadlock detection */
1263 f4658285 Stefan Hajnoczi
    CoQueue wait_queue; /* coroutines blocked on this request */
1264 dbffbdcf Stefan Hajnoczi
};
1265 dbffbdcf Stefan Hajnoczi
1266 dbffbdcf Stefan Hajnoczi
/**
1267 dbffbdcf Stefan Hajnoczi
 * Remove an active request from the tracked requests list
1268 dbffbdcf Stefan Hajnoczi
 *
1269 dbffbdcf Stefan Hajnoczi
 * This function should be called when a tracked request is completing.
1270 dbffbdcf Stefan Hajnoczi
 */
1271 dbffbdcf Stefan Hajnoczi
static void tracked_request_end(BdrvTrackedRequest *req)
1272 dbffbdcf Stefan Hajnoczi
{
1273 dbffbdcf Stefan Hajnoczi
    QLIST_REMOVE(req, list);
1274 f4658285 Stefan Hajnoczi
    qemu_co_queue_restart_all(&req->wait_queue);
1275 dbffbdcf Stefan Hajnoczi
}
1276 dbffbdcf Stefan Hajnoczi
1277 dbffbdcf Stefan Hajnoczi
/**
1278 dbffbdcf Stefan Hajnoczi
 * Add an active request to the tracked requests list
1279 dbffbdcf Stefan Hajnoczi
 */
1280 dbffbdcf Stefan Hajnoczi
static void tracked_request_begin(BdrvTrackedRequest *req,
1281 dbffbdcf Stefan Hajnoczi
                                  BlockDriverState *bs,
1282 dbffbdcf Stefan Hajnoczi
                                  int64_t sector_num,
1283 dbffbdcf Stefan Hajnoczi
                                  int nb_sectors, bool is_write)
1284 dbffbdcf Stefan Hajnoczi
{
1285 dbffbdcf Stefan Hajnoczi
    *req = (BdrvTrackedRequest){
1286 dbffbdcf Stefan Hajnoczi
        .bs = bs,
1287 dbffbdcf Stefan Hajnoczi
        .sector_num = sector_num,
1288 dbffbdcf Stefan Hajnoczi
        .nb_sectors = nb_sectors,
1289 dbffbdcf Stefan Hajnoczi
        .is_write = is_write,
1290 5f8b6491 Stefan Hajnoczi
        .co = qemu_coroutine_self(),
1291 dbffbdcf Stefan Hajnoczi
    };
1292 dbffbdcf Stefan Hajnoczi
1293 f4658285 Stefan Hajnoczi
    qemu_co_queue_init(&req->wait_queue);
1294 f4658285 Stefan Hajnoczi
1295 dbffbdcf Stefan Hajnoczi
    QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
1296 dbffbdcf Stefan Hajnoczi
}
1297 dbffbdcf Stefan Hajnoczi
1298 d83947ac Stefan Hajnoczi
/**
1299 d83947ac Stefan Hajnoczi
 * Round a region to cluster boundaries
1300 d83947ac Stefan Hajnoczi
 */
1301 d83947ac Stefan Hajnoczi
static void round_to_clusters(BlockDriverState *bs,
1302 d83947ac Stefan Hajnoczi
                              int64_t sector_num, int nb_sectors,
1303 d83947ac Stefan Hajnoczi
                              int64_t *cluster_sector_num,
1304 d83947ac Stefan Hajnoczi
                              int *cluster_nb_sectors)
1305 d83947ac Stefan Hajnoczi
{
1306 d83947ac Stefan Hajnoczi
    BlockDriverInfo bdi;
1307 d83947ac Stefan Hajnoczi
1308 d83947ac Stefan Hajnoczi
    if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
1309 d83947ac Stefan Hajnoczi
        *cluster_sector_num = sector_num;
1310 d83947ac Stefan Hajnoczi
        *cluster_nb_sectors = nb_sectors;
1311 d83947ac Stefan Hajnoczi
    } else {
1312 d83947ac Stefan Hajnoczi
        int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE;
1313 d83947ac Stefan Hajnoczi
        *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
1314 d83947ac Stefan Hajnoczi
        *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
1315 d83947ac Stefan Hajnoczi
                                            nb_sectors, c);
1316 d83947ac Stefan Hajnoczi
    }
1317 d83947ac Stefan Hajnoczi
}
1318 d83947ac Stefan Hajnoczi
1319 f4658285 Stefan Hajnoczi
static bool tracked_request_overlaps(BdrvTrackedRequest *req,
1320 f4658285 Stefan Hajnoczi
                                     int64_t sector_num, int nb_sectors) {
1321 d83947ac Stefan Hajnoczi
    /*        aaaa   bbbb */
1322 d83947ac Stefan Hajnoczi
    if (sector_num >= req->sector_num + req->nb_sectors) {
1323 d83947ac Stefan Hajnoczi
        return false;
1324 d83947ac Stefan Hajnoczi
    }
1325 d83947ac Stefan Hajnoczi
    /* bbbb   aaaa        */
1326 d83947ac Stefan Hajnoczi
    if (req->sector_num >= sector_num + nb_sectors) {
1327 d83947ac Stefan Hajnoczi
        return false;
1328 d83947ac Stefan Hajnoczi
    }
1329 d83947ac Stefan Hajnoczi
    return true;
1330 f4658285 Stefan Hajnoczi
}
1331 f4658285 Stefan Hajnoczi
1332 f4658285 Stefan Hajnoczi
static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
1333 f4658285 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors)
1334 f4658285 Stefan Hajnoczi
{
1335 f4658285 Stefan Hajnoczi
    BdrvTrackedRequest *req;
1336 d83947ac Stefan Hajnoczi
    int64_t cluster_sector_num;
1337 d83947ac Stefan Hajnoczi
    int cluster_nb_sectors;
1338 f4658285 Stefan Hajnoczi
    bool retry;
1339 f4658285 Stefan Hajnoczi
1340 d83947ac Stefan Hajnoczi
    /* If we touch the same cluster it counts as an overlap.  This guarantees
1341 d83947ac Stefan Hajnoczi
     * that allocating writes will be serialized and not race with each other
1342 d83947ac Stefan Hajnoczi
     * for the same cluster.  For example, in copy-on-read it ensures that the
1343 d83947ac Stefan Hajnoczi
     * CoR read and write operations are atomic and guest writes cannot
1344 d83947ac Stefan Hajnoczi
     * interleave between them.
1345 d83947ac Stefan Hajnoczi
     */
1346 d83947ac Stefan Hajnoczi
    round_to_clusters(bs, sector_num, nb_sectors,
1347 d83947ac Stefan Hajnoczi
                      &cluster_sector_num, &cluster_nb_sectors);
1348 d83947ac Stefan Hajnoczi
1349 f4658285 Stefan Hajnoczi
    do {
1350 f4658285 Stefan Hajnoczi
        retry = false;
1351 f4658285 Stefan Hajnoczi
        QLIST_FOREACH(req, &bs->tracked_requests, list) {
1352 d83947ac Stefan Hajnoczi
            if (tracked_request_overlaps(req, cluster_sector_num,
1353 d83947ac Stefan Hajnoczi
                                         cluster_nb_sectors)) {
1354 5f8b6491 Stefan Hajnoczi
                /* Hitting this means there was a reentrant request, for
1355 5f8b6491 Stefan Hajnoczi
                 * example, a block driver issuing nested requests.  This must
1356 5f8b6491 Stefan Hajnoczi
                 * never happen since it means deadlock.
1357 5f8b6491 Stefan Hajnoczi
                 */
1358 5f8b6491 Stefan Hajnoczi
                assert(qemu_coroutine_self() != req->co);
1359 5f8b6491 Stefan Hajnoczi
1360 f4658285 Stefan Hajnoczi
                qemu_co_queue_wait(&req->wait_queue);
1361 f4658285 Stefan Hajnoczi
                retry = true;
1362 f4658285 Stefan Hajnoczi
                break;
1363 f4658285 Stefan Hajnoczi
            }
1364 f4658285 Stefan Hajnoczi
        }
1365 f4658285 Stefan Hajnoczi
    } while (retry);
1366 f4658285 Stefan Hajnoczi
}
1367 f4658285 Stefan Hajnoczi
1368 756e6736 Kevin Wolf
/*
1369 756e6736 Kevin Wolf
 * Return values:
1370 756e6736 Kevin Wolf
 * 0        - success
1371 756e6736 Kevin Wolf
 * -EINVAL  - backing format specified, but no file
1372 756e6736 Kevin Wolf
 * -ENOSPC  - can't update the backing file because no space is left in the
1373 756e6736 Kevin Wolf
 *            image file header
1374 756e6736 Kevin Wolf
 * -ENOTSUP - format driver doesn't support changing the backing file
1375 756e6736 Kevin Wolf
 */
1376 756e6736 Kevin Wolf
int bdrv_change_backing_file(BlockDriverState *bs,
1377 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
1378 756e6736 Kevin Wolf
{
1379 756e6736 Kevin Wolf
    BlockDriver *drv = bs->drv;
1380 756e6736 Kevin Wolf
1381 756e6736 Kevin Wolf
    if (drv->bdrv_change_backing_file != NULL) {
1382 756e6736 Kevin Wolf
        return drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
1383 756e6736 Kevin Wolf
    } else {
1384 756e6736 Kevin Wolf
        return -ENOTSUP;
1385 756e6736 Kevin Wolf
    }
1386 756e6736 Kevin Wolf
}
1387 756e6736 Kevin Wolf
1388 71d0770c aliguori
static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
1389 71d0770c aliguori
                                   size_t size)
1390 71d0770c aliguori
{
1391 71d0770c aliguori
    int64_t len;
1392 71d0770c aliguori
1393 71d0770c aliguori
    if (!bdrv_is_inserted(bs))
1394 71d0770c aliguori
        return -ENOMEDIUM;
1395 71d0770c aliguori
1396 71d0770c aliguori
    if (bs->growable)
1397 71d0770c aliguori
        return 0;
1398 71d0770c aliguori
1399 71d0770c aliguori
    len = bdrv_getlength(bs);
1400 71d0770c aliguori
1401 fbb7b4e0 Kevin Wolf
    if (offset < 0)
1402 fbb7b4e0 Kevin Wolf
        return -EIO;
1403 fbb7b4e0 Kevin Wolf
1404 fbb7b4e0 Kevin Wolf
    if ((offset > len) || (len - offset < size))
1405 71d0770c aliguori
        return -EIO;
1406 71d0770c aliguori
1407 71d0770c aliguori
    return 0;
1408 71d0770c aliguori
}
1409 71d0770c aliguori
1410 71d0770c aliguori
static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
1411 71d0770c aliguori
                              int nb_sectors)
1412 71d0770c aliguori
{
1413 eb5a3165 Jes Sorensen
    return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
1414 eb5a3165 Jes Sorensen
                                   nb_sectors * BDRV_SECTOR_SIZE);
1415 71d0770c aliguori
}
1416 71d0770c aliguori
1417 1c9805a3 Stefan Hajnoczi
typedef struct RwCo {
1418 1c9805a3 Stefan Hajnoczi
    BlockDriverState *bs;
1419 1c9805a3 Stefan Hajnoczi
    int64_t sector_num;
1420 1c9805a3 Stefan Hajnoczi
    int nb_sectors;
1421 1c9805a3 Stefan Hajnoczi
    QEMUIOVector *qiov;
1422 1c9805a3 Stefan Hajnoczi
    bool is_write;
1423 1c9805a3 Stefan Hajnoczi
    int ret;
1424 1c9805a3 Stefan Hajnoczi
} RwCo;
1425 1c9805a3 Stefan Hajnoczi
1426 1c9805a3 Stefan Hajnoczi
static void coroutine_fn bdrv_rw_co_entry(void *opaque)
1427 fc01f7e7 bellard
{
1428 1c9805a3 Stefan Hajnoczi
    RwCo *rwco = opaque;
1429 ea2384d3 bellard
1430 1c9805a3 Stefan Hajnoczi
    if (!rwco->is_write) {
1431 1c9805a3 Stefan Hajnoczi
        rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
1432 470c0504 Stefan Hajnoczi
                                     rwco->nb_sectors, rwco->qiov, 0);
1433 1c9805a3 Stefan Hajnoczi
    } else {
1434 1c9805a3 Stefan Hajnoczi
        rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
1435 f08f2dda Stefan Hajnoczi
                                      rwco->nb_sectors, rwco->qiov, 0);
1436 1c9805a3 Stefan Hajnoczi
    }
1437 1c9805a3 Stefan Hajnoczi
}
1438 e7a8a783 Kevin Wolf
1439 1c9805a3 Stefan Hajnoczi
/*
1440 1c9805a3 Stefan Hajnoczi
 * Process a synchronous request using coroutines
1441 1c9805a3 Stefan Hajnoczi
 */
1442 1c9805a3 Stefan Hajnoczi
static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
1443 1c9805a3 Stefan Hajnoczi
                      int nb_sectors, bool is_write)
1444 1c9805a3 Stefan Hajnoczi
{
1445 1c9805a3 Stefan Hajnoczi
    QEMUIOVector qiov;
1446 1c9805a3 Stefan Hajnoczi
    struct iovec iov = {
1447 1c9805a3 Stefan Hajnoczi
        .iov_base = (void *)buf,
1448 1c9805a3 Stefan Hajnoczi
        .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
1449 1c9805a3 Stefan Hajnoczi
    };
1450 1c9805a3 Stefan Hajnoczi
    Coroutine *co;
1451 1c9805a3 Stefan Hajnoczi
    RwCo rwco = {
1452 1c9805a3 Stefan Hajnoczi
        .bs = bs,
1453 1c9805a3 Stefan Hajnoczi
        .sector_num = sector_num,
1454 1c9805a3 Stefan Hajnoczi
        .nb_sectors = nb_sectors,
1455 1c9805a3 Stefan Hajnoczi
        .qiov = &qiov,
1456 1c9805a3 Stefan Hajnoczi
        .is_write = is_write,
1457 1c9805a3 Stefan Hajnoczi
        .ret = NOT_DONE,
1458 1c9805a3 Stefan Hajnoczi
    };
1459 e7a8a783 Kevin Wolf
1460 1c9805a3 Stefan Hajnoczi
    qemu_iovec_init_external(&qiov, &iov, 1);
1461 e7a8a783 Kevin Wolf
1462 1c9805a3 Stefan Hajnoczi
    if (qemu_in_coroutine()) {
1463 1c9805a3 Stefan Hajnoczi
        /* Fast-path if already in coroutine context */
1464 1c9805a3 Stefan Hajnoczi
        bdrv_rw_co_entry(&rwco);
1465 1c9805a3 Stefan Hajnoczi
    } else {
1466 1c9805a3 Stefan Hajnoczi
        co = qemu_coroutine_create(bdrv_rw_co_entry);
1467 1c9805a3 Stefan Hajnoczi
        qemu_coroutine_enter(co, &rwco);
1468 1c9805a3 Stefan Hajnoczi
        while (rwco.ret == NOT_DONE) {
1469 1c9805a3 Stefan Hajnoczi
            qemu_aio_wait();
1470 1c9805a3 Stefan Hajnoczi
        }
1471 1c9805a3 Stefan Hajnoczi
    }
1472 1c9805a3 Stefan Hajnoczi
    return rwco.ret;
1473 1c9805a3 Stefan Hajnoczi
}
1474 b338082b bellard
1475 1c9805a3 Stefan Hajnoczi
/* return < 0 if error. See bdrv_write() for the return codes */
1476 1c9805a3 Stefan Hajnoczi
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
1477 1c9805a3 Stefan Hajnoczi
              uint8_t *buf, int nb_sectors)
1478 1c9805a3 Stefan Hajnoczi
{
1479 1c9805a3 Stefan Hajnoczi
    return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false);
1480 fc01f7e7 bellard
}
1481 fc01f7e7 bellard
1482 7cd1e32a lirans@il.ibm.com
static void set_dirty_bitmap(BlockDriverState *bs, int64_t sector_num,
1483 a55eb92c Jan Kiszka
                             int nb_sectors, int dirty)
1484 7cd1e32a lirans@il.ibm.com
{
1485 7cd1e32a lirans@il.ibm.com
    int64_t start, end;
1486 c6d22830 Jan Kiszka
    unsigned long val, idx, bit;
1487 a55eb92c Jan Kiszka
1488 6ea44308 Jan Kiszka
    start = sector_num / BDRV_SECTORS_PER_DIRTY_CHUNK;
1489 c6d22830 Jan Kiszka
    end = (sector_num + nb_sectors - 1) / BDRV_SECTORS_PER_DIRTY_CHUNK;
1490 a55eb92c Jan Kiszka
1491 a55eb92c Jan Kiszka
    for (; start <= end; start++) {
1492 c6d22830 Jan Kiszka
        idx = start / (sizeof(unsigned long) * 8);
1493 c6d22830 Jan Kiszka
        bit = start % (sizeof(unsigned long) * 8);
1494 c6d22830 Jan Kiszka
        val = bs->dirty_bitmap[idx];
1495 c6d22830 Jan Kiszka
        if (dirty) {
1496 6d59fec1 Marcelo Tosatti
            if (!(val & (1UL << bit))) {
1497 aaa0eb75 Liran Schour
                bs->dirty_count++;
1498 6d59fec1 Marcelo Tosatti
                val |= 1UL << bit;
1499 aaa0eb75 Liran Schour
            }
1500 c6d22830 Jan Kiszka
        } else {
1501 6d59fec1 Marcelo Tosatti
            if (val & (1UL << bit)) {
1502 aaa0eb75 Liran Schour
                bs->dirty_count--;
1503 6d59fec1 Marcelo Tosatti
                val &= ~(1UL << bit);
1504 aaa0eb75 Liran Schour
            }
1505 c6d22830 Jan Kiszka
        }
1506 c6d22830 Jan Kiszka
        bs->dirty_bitmap[idx] = val;
1507 7cd1e32a lirans@il.ibm.com
    }
1508 7cd1e32a lirans@il.ibm.com
}
1509 7cd1e32a lirans@il.ibm.com
1510 5fafdf24 ths
/* Return < 0 if error. Important errors are:
1511 19cb3738 bellard
  -EIO         generic I/O error (may happen for all errors)
1512 19cb3738 bellard
  -ENOMEDIUM   No media inserted.
1513 19cb3738 bellard
  -EINVAL      Invalid sector number or nb_sectors
1514 19cb3738 bellard
  -EACCES      Trying to write a read-only device
1515 19cb3738 bellard
*/
1516 5fafdf24 ths
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
1517 fc01f7e7 bellard
               const uint8_t *buf, int nb_sectors)
1518 fc01f7e7 bellard
{
1519 1c9805a3 Stefan Hajnoczi
    return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true);
1520 83f64091 bellard
}
1521 83f64091 bellard
1522 eda578e5 aliguori
int bdrv_pread(BlockDriverState *bs, int64_t offset,
1523 eda578e5 aliguori
               void *buf, int count1)
1524 83f64091 bellard
{
1525 6ea44308 Jan Kiszka
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1526 83f64091 bellard
    int len, nb_sectors, count;
1527 83f64091 bellard
    int64_t sector_num;
1528 9a8c4cce Kevin Wolf
    int ret;
1529 83f64091 bellard
1530 83f64091 bellard
    count = count1;
1531 83f64091 bellard
    /* first read to align to sector start */
1532 6ea44308 Jan Kiszka
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1533 83f64091 bellard
    if (len > count)
1534 83f64091 bellard
        len = count;
1535 6ea44308 Jan Kiszka
    sector_num = offset >> BDRV_SECTOR_BITS;
1536 83f64091 bellard
    if (len > 0) {
1537 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1538 9a8c4cce Kevin Wolf
            return ret;
1539 6ea44308 Jan Kiszka
        memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
1540 83f64091 bellard
        count -= len;
1541 83f64091 bellard
        if (count == 0)
1542 83f64091 bellard
            return count1;
1543 83f64091 bellard
        sector_num++;
1544 83f64091 bellard
        buf += len;
1545 83f64091 bellard
    }
1546 83f64091 bellard
1547 83f64091 bellard
    /* read the sectors "in place" */
1548 6ea44308 Jan Kiszka
    nb_sectors = count >> BDRV_SECTOR_BITS;
1549 83f64091 bellard
    if (nb_sectors > 0) {
1550 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
1551 9a8c4cce Kevin Wolf
            return ret;
1552 83f64091 bellard
        sector_num += nb_sectors;
1553 6ea44308 Jan Kiszka
        len = nb_sectors << BDRV_SECTOR_BITS;
1554 83f64091 bellard
        buf += len;
1555 83f64091 bellard
        count -= len;
1556 83f64091 bellard
    }
1557 83f64091 bellard
1558 83f64091 bellard
    /* add data from the last sector */
1559 83f64091 bellard
    if (count > 0) {
1560 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1561 9a8c4cce Kevin Wolf
            return ret;
1562 83f64091 bellard
        memcpy(buf, tmp_buf, count);
1563 83f64091 bellard
    }
1564 83f64091 bellard
    return count1;
1565 83f64091 bellard
}
1566 83f64091 bellard
1567 eda578e5 aliguori
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
1568 eda578e5 aliguori
                const void *buf, int count1)
1569 83f64091 bellard
{
1570 6ea44308 Jan Kiszka
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
1571 83f64091 bellard
    int len, nb_sectors, count;
1572 83f64091 bellard
    int64_t sector_num;
1573 9a8c4cce Kevin Wolf
    int ret;
1574 83f64091 bellard
1575 83f64091 bellard
    count = count1;
1576 83f64091 bellard
    /* first write to align to sector start */
1577 6ea44308 Jan Kiszka
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
1578 83f64091 bellard
    if (len > count)
1579 83f64091 bellard
        len = count;
1580 6ea44308 Jan Kiszka
    sector_num = offset >> BDRV_SECTOR_BITS;
1581 83f64091 bellard
    if (len > 0) {
1582 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1583 9a8c4cce Kevin Wolf
            return ret;
1584 6ea44308 Jan Kiszka
        memcpy(tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), buf, len);
1585 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1586 9a8c4cce Kevin Wolf
            return ret;
1587 83f64091 bellard
        count -= len;
1588 83f64091 bellard
        if (count == 0)
1589 83f64091 bellard
            return count1;
1590 83f64091 bellard
        sector_num++;
1591 83f64091 bellard
        buf += len;
1592 83f64091 bellard
    }
1593 83f64091 bellard
1594 83f64091 bellard
    /* write the sectors "in place" */
1595 6ea44308 Jan Kiszka
    nb_sectors = count >> BDRV_SECTOR_BITS;
1596 83f64091 bellard
    if (nb_sectors > 0) {
1597 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, buf, nb_sectors)) < 0)
1598 9a8c4cce Kevin Wolf
            return ret;
1599 83f64091 bellard
        sector_num += nb_sectors;
1600 6ea44308 Jan Kiszka
        len = nb_sectors << BDRV_SECTOR_BITS;
1601 83f64091 bellard
        buf += len;
1602 83f64091 bellard
        count -= len;
1603 83f64091 bellard
    }
1604 83f64091 bellard
1605 83f64091 bellard
    /* add data from the last sector */
1606 83f64091 bellard
    if (count > 0) {
1607 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
1608 9a8c4cce Kevin Wolf
            return ret;
1609 83f64091 bellard
        memcpy(tmp_buf, buf, count);
1610 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
1611 9a8c4cce Kevin Wolf
            return ret;
1612 83f64091 bellard
    }
1613 83f64091 bellard
    return count1;
1614 83f64091 bellard
}
1615 83f64091 bellard
1616 f08145fe Kevin Wolf
/*
1617 f08145fe Kevin Wolf
 * Writes to the file and ensures that no writes are reordered across this
1618 f08145fe Kevin Wolf
 * request (acts as a barrier)
1619 f08145fe Kevin Wolf
 *
1620 f08145fe Kevin Wolf
 * Returns 0 on success, -errno in error cases.
1621 f08145fe Kevin Wolf
 */
1622 f08145fe Kevin Wolf
int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
1623 f08145fe Kevin Wolf
    const void *buf, int count)
1624 f08145fe Kevin Wolf
{
1625 f08145fe Kevin Wolf
    int ret;
1626 f08145fe Kevin Wolf
1627 f08145fe Kevin Wolf
    ret = bdrv_pwrite(bs, offset, buf, count);
1628 f08145fe Kevin Wolf
    if (ret < 0) {
1629 f08145fe Kevin Wolf
        return ret;
1630 f08145fe Kevin Wolf
    }
1631 f08145fe Kevin Wolf
1632 92196b2f Stefan Hajnoczi
    /* No flush needed for cache modes that use O_DSYNC */
1633 92196b2f Stefan Hajnoczi
    if ((bs->open_flags & BDRV_O_CACHE_WB) != 0) {
1634 f08145fe Kevin Wolf
        bdrv_flush(bs);
1635 f08145fe Kevin Wolf
    }
1636 f08145fe Kevin Wolf
1637 f08145fe Kevin Wolf
    return 0;
1638 f08145fe Kevin Wolf
}
1639 f08145fe Kevin Wolf
1640 470c0504 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
1641 ab185921 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1642 ab185921 Stefan Hajnoczi
{
1643 ab185921 Stefan Hajnoczi
    /* Perform I/O through a temporary buffer so that users who scribble over
1644 ab185921 Stefan Hajnoczi
     * their read buffer while the operation is in progress do not end up
1645 ab185921 Stefan Hajnoczi
     * modifying the image file.  This is critical for zero-copy guest I/O
1646 ab185921 Stefan Hajnoczi
     * where anything might happen inside guest memory.
1647 ab185921 Stefan Hajnoczi
     */
1648 ab185921 Stefan Hajnoczi
    void *bounce_buffer;
1649 ab185921 Stefan Hajnoczi
1650 79c053bd Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
1651 ab185921 Stefan Hajnoczi
    struct iovec iov;
1652 ab185921 Stefan Hajnoczi
    QEMUIOVector bounce_qiov;
1653 ab185921 Stefan Hajnoczi
    int64_t cluster_sector_num;
1654 ab185921 Stefan Hajnoczi
    int cluster_nb_sectors;
1655 ab185921 Stefan Hajnoczi
    size_t skip_bytes;
1656 ab185921 Stefan Hajnoczi
    int ret;
1657 ab185921 Stefan Hajnoczi
1658 ab185921 Stefan Hajnoczi
    /* Cover entire cluster so no additional backing file I/O is required when
1659 ab185921 Stefan Hajnoczi
     * allocating cluster in the image file.
1660 ab185921 Stefan Hajnoczi
     */
1661 ab185921 Stefan Hajnoczi
    round_to_clusters(bs, sector_num, nb_sectors,
1662 ab185921 Stefan Hajnoczi
                      &cluster_sector_num, &cluster_nb_sectors);
1663 ab185921 Stefan Hajnoczi
1664 470c0504 Stefan Hajnoczi
    trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors,
1665 470c0504 Stefan Hajnoczi
                                   cluster_sector_num, cluster_nb_sectors);
1666 ab185921 Stefan Hajnoczi
1667 ab185921 Stefan Hajnoczi
    iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE;
1668 ab185921 Stefan Hajnoczi
    iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len);
1669 ab185921 Stefan Hajnoczi
    qemu_iovec_init_external(&bounce_qiov, &iov, 1);
1670 ab185921 Stefan Hajnoczi
1671 79c053bd Stefan Hajnoczi
    ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors,
1672 79c053bd Stefan Hajnoczi
                             &bounce_qiov);
1673 ab185921 Stefan Hajnoczi
    if (ret < 0) {
1674 ab185921 Stefan Hajnoczi
        goto err;
1675 ab185921 Stefan Hajnoczi
    }
1676 ab185921 Stefan Hajnoczi
1677 79c053bd Stefan Hajnoczi
    if (drv->bdrv_co_write_zeroes &&
1678 79c053bd Stefan Hajnoczi
        buffer_is_zero(bounce_buffer, iov.iov_len)) {
1679 79c053bd Stefan Hajnoczi
        ret = drv->bdrv_co_write_zeroes(bs, cluster_sector_num,
1680 79c053bd Stefan Hajnoczi
                                        cluster_nb_sectors);
1681 79c053bd Stefan Hajnoczi
    } else {
1682 79c053bd Stefan Hajnoczi
        ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors,
1683 ab185921 Stefan Hajnoczi
                                  &bounce_qiov);
1684 79c053bd Stefan Hajnoczi
    }
1685 79c053bd Stefan Hajnoczi
1686 ab185921 Stefan Hajnoczi
    if (ret < 0) {
1687 ab185921 Stefan Hajnoczi
        /* It might be okay to ignore write errors for guest requests.  If this
1688 ab185921 Stefan Hajnoczi
         * is a deliberate copy-on-read then we don't want to ignore the error.
1689 ab185921 Stefan Hajnoczi
         * Simply report it in all cases.
1690 ab185921 Stefan Hajnoczi
         */
1691 ab185921 Stefan Hajnoczi
        goto err;
1692 ab185921 Stefan Hajnoczi
    }
1693 ab185921 Stefan Hajnoczi
1694 ab185921 Stefan Hajnoczi
    skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE;
1695 ab185921 Stefan Hajnoczi
    qemu_iovec_from_buffer(qiov, bounce_buffer + skip_bytes,
1696 ab185921 Stefan Hajnoczi
                           nb_sectors * BDRV_SECTOR_SIZE);
1697 ab185921 Stefan Hajnoczi
1698 ab185921 Stefan Hajnoczi
err:
1699 ab185921 Stefan Hajnoczi
    qemu_vfree(bounce_buffer);
1700 ab185921 Stefan Hajnoczi
    return ret;
1701 ab185921 Stefan Hajnoczi
}
1702 ab185921 Stefan Hajnoczi
1703 c5fbe571 Stefan Hajnoczi
/*
1704 c5fbe571 Stefan Hajnoczi
 * Handle a read request in coroutine context
1705 c5fbe571 Stefan Hajnoczi
 */
1706 c5fbe571 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
1707 470c0504 Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
1708 470c0504 Stefan Hajnoczi
    BdrvRequestFlags flags)
1709 da1fa91d Kevin Wolf
{
1710 da1fa91d Kevin Wolf
    BlockDriver *drv = bs->drv;
1711 dbffbdcf Stefan Hajnoczi
    BdrvTrackedRequest req;
1712 dbffbdcf Stefan Hajnoczi
    int ret;
1713 da1fa91d Kevin Wolf
1714 da1fa91d Kevin Wolf
    if (!drv) {
1715 da1fa91d Kevin Wolf
        return -ENOMEDIUM;
1716 da1fa91d Kevin Wolf
    }
1717 da1fa91d Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1718 da1fa91d Kevin Wolf
        return -EIO;
1719 da1fa91d Kevin Wolf
    }
1720 da1fa91d Kevin Wolf
1721 98f90dba Zhi Yong Wu
    /* throttling disk read I/O */
1722 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
1723 98f90dba Zhi Yong Wu
        bdrv_io_limits_intercept(bs, false, nb_sectors);
1724 98f90dba Zhi Yong Wu
    }
1725 98f90dba Zhi Yong Wu
1726 f4658285 Stefan Hajnoczi
    if (bs->copy_on_read) {
1727 470c0504 Stefan Hajnoczi
        flags |= BDRV_REQ_COPY_ON_READ;
1728 470c0504 Stefan Hajnoczi
    }
1729 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
1730 470c0504 Stefan Hajnoczi
        bs->copy_on_read_in_flight++;
1731 470c0504 Stefan Hajnoczi
    }
1732 470c0504 Stefan Hajnoczi
1733 470c0504 Stefan Hajnoczi
    if (bs->copy_on_read_in_flight) {
1734 f4658285 Stefan Hajnoczi
        wait_for_overlapping_requests(bs, sector_num, nb_sectors);
1735 f4658285 Stefan Hajnoczi
    }
1736 f4658285 Stefan Hajnoczi
1737 dbffbdcf Stefan Hajnoczi
    tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
1738 ab185921 Stefan Hajnoczi
1739 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
1740 ab185921 Stefan Hajnoczi
        int pnum;
1741 ab185921 Stefan Hajnoczi
1742 ab185921 Stefan Hajnoczi
        ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum);
1743 ab185921 Stefan Hajnoczi
        if (ret < 0) {
1744 ab185921 Stefan Hajnoczi
            goto out;
1745 ab185921 Stefan Hajnoczi
        }
1746 ab185921 Stefan Hajnoczi
1747 ab185921 Stefan Hajnoczi
        if (!ret || pnum != nb_sectors) {
1748 470c0504 Stefan Hajnoczi
            ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov);
1749 ab185921 Stefan Hajnoczi
            goto out;
1750 ab185921 Stefan Hajnoczi
        }
1751 ab185921 Stefan Hajnoczi
    }
1752 ab185921 Stefan Hajnoczi
1753 dbffbdcf Stefan Hajnoczi
    ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
1754 ab185921 Stefan Hajnoczi
1755 ab185921 Stefan Hajnoczi
out:
1756 dbffbdcf Stefan Hajnoczi
    tracked_request_end(&req);
1757 470c0504 Stefan Hajnoczi
1758 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
1759 470c0504 Stefan Hajnoczi
        bs->copy_on_read_in_flight--;
1760 470c0504 Stefan Hajnoczi
    }
1761 470c0504 Stefan Hajnoczi
1762 dbffbdcf Stefan Hajnoczi
    return ret;
1763 da1fa91d Kevin Wolf
}
1764 da1fa91d Kevin Wolf
1765 c5fbe571 Stefan Hajnoczi
int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
1766 da1fa91d Kevin Wolf
    int nb_sectors, QEMUIOVector *qiov)
1767 da1fa91d Kevin Wolf
{
1768 c5fbe571 Stefan Hajnoczi
    trace_bdrv_co_readv(bs, sector_num, nb_sectors);
1769 da1fa91d Kevin Wolf
1770 470c0504 Stefan Hajnoczi
    return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0);
1771 470c0504 Stefan Hajnoczi
}
1772 470c0504 Stefan Hajnoczi
1773 470c0504 Stefan Hajnoczi
int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
1774 470c0504 Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
1775 470c0504 Stefan Hajnoczi
{
1776 470c0504 Stefan Hajnoczi
    trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors);
1777 470c0504 Stefan Hajnoczi
1778 470c0504 Stefan Hajnoczi
    return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
1779 470c0504 Stefan Hajnoczi
                            BDRV_REQ_COPY_ON_READ);
1780 c5fbe571 Stefan Hajnoczi
}
1781 c5fbe571 Stefan Hajnoczi
1782 f08f2dda Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
1783 f08f2dda Stefan Hajnoczi
    int64_t sector_num, int nb_sectors)
1784 f08f2dda Stefan Hajnoczi
{
1785 f08f2dda Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
1786 f08f2dda Stefan Hajnoczi
    QEMUIOVector qiov;
1787 f08f2dda Stefan Hajnoczi
    struct iovec iov;
1788 f08f2dda Stefan Hajnoczi
    int ret;
1789 f08f2dda Stefan Hajnoczi
1790 f08f2dda Stefan Hajnoczi
    /* First try the efficient write zeroes operation */
1791 f08f2dda Stefan Hajnoczi
    if (drv->bdrv_co_write_zeroes) {
1792 f08f2dda Stefan Hajnoczi
        return drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
1793 f08f2dda Stefan Hajnoczi
    }
1794 f08f2dda Stefan Hajnoczi
1795 f08f2dda Stefan Hajnoczi
    /* Fall back to bounce buffer if write zeroes is unsupported */
1796 f08f2dda Stefan Hajnoczi
    iov.iov_len  = nb_sectors * BDRV_SECTOR_SIZE;
1797 f08f2dda Stefan Hajnoczi
    iov.iov_base = qemu_blockalign(bs, iov.iov_len);
1798 f08f2dda Stefan Hajnoczi
    memset(iov.iov_base, 0, iov.iov_len);
1799 f08f2dda Stefan Hajnoczi
    qemu_iovec_init_external(&qiov, &iov, 1);
1800 f08f2dda Stefan Hajnoczi
1801 f08f2dda Stefan Hajnoczi
    ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov);
1802 f08f2dda Stefan Hajnoczi
1803 f08f2dda Stefan Hajnoczi
    qemu_vfree(iov.iov_base);
1804 f08f2dda Stefan Hajnoczi
    return ret;
1805 f08f2dda Stefan Hajnoczi
}
1806 f08f2dda Stefan Hajnoczi
1807 c5fbe571 Stefan Hajnoczi
/*
1808 c5fbe571 Stefan Hajnoczi
 * Handle a write request in coroutine context
1809 c5fbe571 Stefan Hajnoczi
 */
1810 c5fbe571 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
1811 f08f2dda Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
1812 f08f2dda Stefan Hajnoczi
    BdrvRequestFlags flags)
1813 c5fbe571 Stefan Hajnoczi
{
1814 c5fbe571 Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
1815 dbffbdcf Stefan Hajnoczi
    BdrvTrackedRequest req;
1816 6b7cb247 Stefan Hajnoczi
    int ret;
1817 da1fa91d Kevin Wolf
1818 da1fa91d Kevin Wolf
    if (!bs->drv) {
1819 da1fa91d Kevin Wolf
        return -ENOMEDIUM;
1820 da1fa91d Kevin Wolf
    }
1821 da1fa91d Kevin Wolf
    if (bs->read_only) {
1822 da1fa91d Kevin Wolf
        return -EACCES;
1823 da1fa91d Kevin Wolf
    }
1824 da1fa91d Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors)) {
1825 da1fa91d Kevin Wolf
        return -EIO;
1826 da1fa91d Kevin Wolf
    }
1827 da1fa91d Kevin Wolf
1828 98f90dba Zhi Yong Wu
    /* throttling disk write I/O */
1829 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
1830 98f90dba Zhi Yong Wu
        bdrv_io_limits_intercept(bs, true, nb_sectors);
1831 98f90dba Zhi Yong Wu
    }
1832 98f90dba Zhi Yong Wu
1833 470c0504 Stefan Hajnoczi
    if (bs->copy_on_read_in_flight) {
1834 f4658285 Stefan Hajnoczi
        wait_for_overlapping_requests(bs, sector_num, nb_sectors);
1835 f4658285 Stefan Hajnoczi
    }
1836 f4658285 Stefan Hajnoczi
1837 dbffbdcf Stefan Hajnoczi
    tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
1838 dbffbdcf Stefan Hajnoczi
1839 f08f2dda Stefan Hajnoczi
    if (flags & BDRV_REQ_ZERO_WRITE) {
1840 f08f2dda Stefan Hajnoczi
        ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
1841 f08f2dda Stefan Hajnoczi
    } else {
1842 f08f2dda Stefan Hajnoczi
        ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
1843 f08f2dda Stefan Hajnoczi
    }
1844 6b7cb247 Stefan Hajnoczi
1845 da1fa91d Kevin Wolf
    if (bs->dirty_bitmap) {
1846 da1fa91d Kevin Wolf
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
1847 da1fa91d Kevin Wolf
    }
1848 da1fa91d Kevin Wolf
1849 da1fa91d Kevin Wolf
    if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
1850 da1fa91d Kevin Wolf
        bs->wr_highest_sector = sector_num + nb_sectors - 1;
1851 da1fa91d Kevin Wolf
    }
1852 da1fa91d Kevin Wolf
1853 dbffbdcf Stefan Hajnoczi
    tracked_request_end(&req);
1854 dbffbdcf Stefan Hajnoczi
1855 6b7cb247 Stefan Hajnoczi
    return ret;
1856 da1fa91d Kevin Wolf
}
1857 da1fa91d Kevin Wolf
1858 c5fbe571 Stefan Hajnoczi
int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
1859 c5fbe571 Stefan Hajnoczi
    int nb_sectors, QEMUIOVector *qiov)
1860 c5fbe571 Stefan Hajnoczi
{
1861 c5fbe571 Stefan Hajnoczi
    trace_bdrv_co_writev(bs, sector_num, nb_sectors);
1862 c5fbe571 Stefan Hajnoczi
1863 f08f2dda Stefan Hajnoczi
    return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0);
1864 f08f2dda Stefan Hajnoczi
}
1865 f08f2dda Stefan Hajnoczi
1866 f08f2dda Stefan Hajnoczi
int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
1867 f08f2dda Stefan Hajnoczi
                                      int64_t sector_num, int nb_sectors)
1868 f08f2dda Stefan Hajnoczi
{
1869 f08f2dda Stefan Hajnoczi
    trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
1870 f08f2dda Stefan Hajnoczi
1871 f08f2dda Stefan Hajnoczi
    return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
1872 f08f2dda Stefan Hajnoczi
                             BDRV_REQ_ZERO_WRITE);
1873 c5fbe571 Stefan Hajnoczi
}
1874 c5fbe571 Stefan Hajnoczi
1875 83f64091 bellard
/**
1876 83f64091 bellard
 * Truncate file to 'offset' bytes (needed only for file protocols)
1877 83f64091 bellard
 */
1878 83f64091 bellard
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
1879 83f64091 bellard
{
1880 83f64091 bellard
    BlockDriver *drv = bs->drv;
1881 51762288 Stefan Hajnoczi
    int ret;
1882 83f64091 bellard
    if (!drv)
1883 19cb3738 bellard
        return -ENOMEDIUM;
1884 83f64091 bellard
    if (!drv->bdrv_truncate)
1885 83f64091 bellard
        return -ENOTSUP;
1886 59f2689d Naphtali Sprei
    if (bs->read_only)
1887 59f2689d Naphtali Sprei
        return -EACCES;
1888 8591675f Marcelo Tosatti
    if (bdrv_in_use(bs))
1889 8591675f Marcelo Tosatti
        return -EBUSY;
1890 51762288 Stefan Hajnoczi
    ret = drv->bdrv_truncate(bs, offset);
1891 51762288 Stefan Hajnoczi
    if (ret == 0) {
1892 51762288 Stefan Hajnoczi
        ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
1893 145feb17 Markus Armbruster
        bdrv_dev_resize_cb(bs);
1894 51762288 Stefan Hajnoczi
    }
1895 51762288 Stefan Hajnoczi
    return ret;
1896 83f64091 bellard
}
1897 83f64091 bellard
1898 83f64091 bellard
/**
1899 4a1d5e1f Fam Zheng
 * Length of a allocated file in bytes. Sparse files are counted by actual
1900 4a1d5e1f Fam Zheng
 * allocated space. Return < 0 if error or unknown.
1901 4a1d5e1f Fam Zheng
 */
1902 4a1d5e1f Fam Zheng
int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
1903 4a1d5e1f Fam Zheng
{
1904 4a1d5e1f Fam Zheng
    BlockDriver *drv = bs->drv;
1905 4a1d5e1f Fam Zheng
    if (!drv) {
1906 4a1d5e1f Fam Zheng
        return -ENOMEDIUM;
1907 4a1d5e1f Fam Zheng
    }
1908 4a1d5e1f Fam Zheng
    if (drv->bdrv_get_allocated_file_size) {
1909 4a1d5e1f Fam Zheng
        return drv->bdrv_get_allocated_file_size(bs);
1910 4a1d5e1f Fam Zheng
    }
1911 4a1d5e1f Fam Zheng
    if (bs->file) {
1912 4a1d5e1f Fam Zheng
        return bdrv_get_allocated_file_size(bs->file);
1913 4a1d5e1f Fam Zheng
    }
1914 4a1d5e1f Fam Zheng
    return -ENOTSUP;
1915 4a1d5e1f Fam Zheng
}
1916 4a1d5e1f Fam Zheng
1917 4a1d5e1f Fam Zheng
/**
1918 83f64091 bellard
 * Length of a file in bytes. Return < 0 if error or unknown.
1919 83f64091 bellard
 */
1920 83f64091 bellard
int64_t bdrv_getlength(BlockDriverState *bs)
1921 83f64091 bellard
{
1922 83f64091 bellard
    BlockDriver *drv = bs->drv;
1923 83f64091 bellard
    if (!drv)
1924 19cb3738 bellard
        return -ENOMEDIUM;
1925 51762288 Stefan Hajnoczi
1926 2c6942fa Markus Armbruster
    if (bs->growable || bdrv_dev_has_removable_media(bs)) {
1927 46a4e4e6 Stefan Hajnoczi
        if (drv->bdrv_getlength) {
1928 46a4e4e6 Stefan Hajnoczi
            return drv->bdrv_getlength(bs);
1929 46a4e4e6 Stefan Hajnoczi
        }
1930 83f64091 bellard
    }
1931 46a4e4e6 Stefan Hajnoczi
    return bs->total_sectors * BDRV_SECTOR_SIZE;
1932 fc01f7e7 bellard
}
1933 fc01f7e7 bellard
1934 19cb3738 bellard
/* return 0 as number of sectors if no device present or error */
1935 96b8f136 ths
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
1936 fc01f7e7 bellard
{
1937 19cb3738 bellard
    int64_t length;
1938 19cb3738 bellard
    length = bdrv_getlength(bs);
1939 19cb3738 bellard
    if (length < 0)
1940 19cb3738 bellard
        length = 0;
1941 19cb3738 bellard
    else
1942 6ea44308 Jan Kiszka
        length = length >> BDRV_SECTOR_BITS;
1943 19cb3738 bellard
    *nb_sectors_ptr = length;
1944 fc01f7e7 bellard
}
1945 cf98951b bellard
1946 f3d54fc4 aliguori
struct partition {
1947 f3d54fc4 aliguori
        uint8_t boot_ind;           /* 0x80 - active */
1948 f3d54fc4 aliguori
        uint8_t head;               /* starting head */
1949 f3d54fc4 aliguori
        uint8_t sector;             /* starting sector */
1950 f3d54fc4 aliguori
        uint8_t cyl;                /* starting cylinder */
1951 f3d54fc4 aliguori
        uint8_t sys_ind;            /* What partition type */
1952 f3d54fc4 aliguori
        uint8_t end_head;           /* end head */
1953 f3d54fc4 aliguori
        uint8_t end_sector;         /* end sector */
1954 f3d54fc4 aliguori
        uint8_t end_cyl;            /* end cylinder */
1955 f3d54fc4 aliguori
        uint32_t start_sect;        /* starting sector counting from 0 */
1956 f3d54fc4 aliguori
        uint32_t nr_sects;          /* nr of sectors in partition */
1957 541dc0d4 Stefan Weil
} QEMU_PACKED;
1958 f3d54fc4 aliguori
1959 f3d54fc4 aliguori
/* try to guess the disk logical geometry from the MSDOS partition table. Return 0 if OK, -1 if could not guess */
1960 f3d54fc4 aliguori
static int guess_disk_lchs(BlockDriverState *bs,
1961 f3d54fc4 aliguori
                           int *pcylinders, int *pheads, int *psectors)
1962 f3d54fc4 aliguori
{
1963 eb5a3165 Jes Sorensen
    uint8_t buf[BDRV_SECTOR_SIZE];
1964 f3d54fc4 aliguori
    int ret, i, heads, sectors, cylinders;
1965 f3d54fc4 aliguori
    struct partition *p;
1966 f3d54fc4 aliguori
    uint32_t nr_sects;
1967 a38131b6 blueswir1
    uint64_t nb_sectors;
1968 f3d54fc4 aliguori
1969 f3d54fc4 aliguori
    bdrv_get_geometry(bs, &nb_sectors);
1970 f3d54fc4 aliguori
1971 f3d54fc4 aliguori
    ret = bdrv_read(bs, 0, buf, 1);
1972 f3d54fc4 aliguori
    if (ret < 0)
1973 f3d54fc4 aliguori
        return -1;
1974 f3d54fc4 aliguori
    /* test msdos magic */
1975 f3d54fc4 aliguori
    if (buf[510] != 0x55 || buf[511] != 0xaa)
1976 f3d54fc4 aliguori
        return -1;
1977 f3d54fc4 aliguori
    for(i = 0; i < 4; i++) {
1978 f3d54fc4 aliguori
        p = ((struct partition *)(buf + 0x1be)) + i;
1979 f3d54fc4 aliguori
        nr_sects = le32_to_cpu(p->nr_sects);
1980 f3d54fc4 aliguori
        if (nr_sects && p->end_head) {
1981 f3d54fc4 aliguori
            /* We make the assumption that the partition terminates on
1982 f3d54fc4 aliguori
               a cylinder boundary */
1983 f3d54fc4 aliguori
            heads = p->end_head + 1;
1984 f3d54fc4 aliguori
            sectors = p->end_sector & 63;
1985 f3d54fc4 aliguori
            if (sectors == 0)
1986 f3d54fc4 aliguori
                continue;
1987 f3d54fc4 aliguori
            cylinders = nb_sectors / (heads * sectors);
1988 f3d54fc4 aliguori
            if (cylinders < 1 || cylinders > 16383)
1989 f3d54fc4 aliguori
                continue;
1990 f3d54fc4 aliguori
            *pheads = heads;
1991 f3d54fc4 aliguori
            *psectors = sectors;
1992 f3d54fc4 aliguori
            *pcylinders = cylinders;
1993 f3d54fc4 aliguori
#if 0
1994 f3d54fc4 aliguori
            printf("guessed geometry: LCHS=%d %d %d\n",
1995 f3d54fc4 aliguori
                   cylinders, heads, sectors);
1996 f3d54fc4 aliguori
#endif
1997 f3d54fc4 aliguori
            return 0;
1998 f3d54fc4 aliguori
        }
1999 f3d54fc4 aliguori
    }
2000 f3d54fc4 aliguori
    return -1;
2001 f3d54fc4 aliguori
}
2002 f3d54fc4 aliguori
2003 f3d54fc4 aliguori
void bdrv_guess_geometry(BlockDriverState *bs, int *pcyls, int *pheads, int *psecs)
2004 f3d54fc4 aliguori
{
2005 f3d54fc4 aliguori
    int translation, lba_detected = 0;
2006 f3d54fc4 aliguori
    int cylinders, heads, secs;
2007 a38131b6 blueswir1
    uint64_t nb_sectors;
2008 f3d54fc4 aliguori
2009 f3d54fc4 aliguori
    /* if a geometry hint is available, use it */
2010 f3d54fc4 aliguori
    bdrv_get_geometry(bs, &nb_sectors);
2011 f3d54fc4 aliguori
    bdrv_get_geometry_hint(bs, &cylinders, &heads, &secs);
2012 f3d54fc4 aliguori
    translation = bdrv_get_translation_hint(bs);
2013 f3d54fc4 aliguori
    if (cylinders != 0) {
2014 f3d54fc4 aliguori
        *pcyls = cylinders;
2015 f3d54fc4 aliguori
        *pheads = heads;
2016 f3d54fc4 aliguori
        *psecs = secs;
2017 f3d54fc4 aliguori
    } else {
2018 f3d54fc4 aliguori
        if (guess_disk_lchs(bs, &cylinders, &heads, &secs) == 0) {
2019 f3d54fc4 aliguori
            if (heads > 16) {
2020 f3d54fc4 aliguori
                /* if heads > 16, it means that a BIOS LBA
2021 f3d54fc4 aliguori
                   translation was active, so the default
2022 f3d54fc4 aliguori
                   hardware geometry is OK */
2023 f3d54fc4 aliguori
                lba_detected = 1;
2024 f3d54fc4 aliguori
                goto default_geometry;
2025 f3d54fc4 aliguori
            } else {
2026 f3d54fc4 aliguori
                *pcyls = cylinders;
2027 f3d54fc4 aliguori
                *pheads = heads;
2028 f3d54fc4 aliguori
                *psecs = secs;
2029 f3d54fc4 aliguori
                /* disable any translation to be in sync with
2030 f3d54fc4 aliguori
                   the logical geometry */
2031 f3d54fc4 aliguori
                if (translation == BIOS_ATA_TRANSLATION_AUTO) {
2032 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
2033 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_NONE);
2034 f3d54fc4 aliguori
                }
2035 f3d54fc4 aliguori
            }
2036 f3d54fc4 aliguori
        } else {
2037 f3d54fc4 aliguori
        default_geometry:
2038 f3d54fc4 aliguori
            /* if no geometry, use a standard physical disk geometry */
2039 f3d54fc4 aliguori
            cylinders = nb_sectors / (16 * 63);
2040 f3d54fc4 aliguori
2041 f3d54fc4 aliguori
            if (cylinders > 16383)
2042 f3d54fc4 aliguori
                cylinders = 16383;
2043 f3d54fc4 aliguori
            else if (cylinders < 2)
2044 f3d54fc4 aliguori
                cylinders = 2;
2045 f3d54fc4 aliguori
            *pcyls = cylinders;
2046 f3d54fc4 aliguori
            *pheads = 16;
2047 f3d54fc4 aliguori
            *psecs = 63;
2048 f3d54fc4 aliguori
            if ((lba_detected == 1) && (translation == BIOS_ATA_TRANSLATION_AUTO)) {
2049 f3d54fc4 aliguori
                if ((*pcyls * *pheads) <= 131072) {
2050 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
2051 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_LARGE);
2052 f3d54fc4 aliguori
                } else {
2053 f3d54fc4 aliguori
                    bdrv_set_translation_hint(bs,
2054 f3d54fc4 aliguori
                                              BIOS_ATA_TRANSLATION_LBA);
2055 f3d54fc4 aliguori
                }
2056 f3d54fc4 aliguori
            }
2057 f3d54fc4 aliguori
        }
2058 f3d54fc4 aliguori
        bdrv_set_geometry_hint(bs, *pcyls, *pheads, *psecs);
2059 f3d54fc4 aliguori
    }
2060 f3d54fc4 aliguori
}
2061 f3d54fc4 aliguori
2062 5fafdf24 ths
void bdrv_set_geometry_hint(BlockDriverState *bs,
2063 b338082b bellard
                            int cyls, int heads, int secs)
2064 b338082b bellard
{
2065 b338082b bellard
    bs->cyls = cyls;
2066 b338082b bellard
    bs->heads = heads;
2067 b338082b bellard
    bs->secs = secs;
2068 b338082b bellard
}
2069 b338082b bellard
2070 46d4767d bellard
void bdrv_set_translation_hint(BlockDriverState *bs, int translation)
2071 46d4767d bellard
{
2072 46d4767d bellard
    bs->translation = translation;
2073 46d4767d bellard
}
2074 46d4767d bellard
2075 5fafdf24 ths
void bdrv_get_geometry_hint(BlockDriverState *bs,
2076 b338082b bellard
                            int *pcyls, int *pheads, int *psecs)
2077 b338082b bellard
{
2078 b338082b bellard
    *pcyls = bs->cyls;
2079 b338082b bellard
    *pheads = bs->heads;
2080 b338082b bellard
    *psecs = bs->secs;
2081 b338082b bellard
}
2082 b338082b bellard
2083 0563e191 Zhi Yong Wu
/* throttling disk io limits */
2084 0563e191 Zhi Yong Wu
void bdrv_set_io_limits(BlockDriverState *bs,
2085 0563e191 Zhi Yong Wu
                        BlockIOLimit *io_limits)
2086 0563e191 Zhi Yong Wu
{
2087 0563e191 Zhi Yong Wu
    bs->io_limits = *io_limits;
2088 0563e191 Zhi Yong Wu
    bs->io_limits_enabled = bdrv_io_limits_enabled(bs);
2089 0563e191 Zhi Yong Wu
}
2090 0563e191 Zhi Yong Wu
2091 5bbdbb46 Blue Swirl
/* Recognize floppy formats */
2092 5bbdbb46 Blue Swirl
typedef struct FDFormat {
2093 5bbdbb46 Blue Swirl
    FDriveType drive;
2094 5bbdbb46 Blue Swirl
    uint8_t last_sect;
2095 5bbdbb46 Blue Swirl
    uint8_t max_track;
2096 5bbdbb46 Blue Swirl
    uint8_t max_head;
2097 f8d3d128 Hervรฉ Poussineau
    FDriveRate rate;
2098 5bbdbb46 Blue Swirl
} FDFormat;
2099 5bbdbb46 Blue Swirl
2100 5bbdbb46 Blue Swirl
static const FDFormat fd_formats[] = {
2101 5bbdbb46 Blue Swirl
    /* First entry is default format */
2102 5bbdbb46 Blue Swirl
    /* 1.44 MB 3"1/2 floppy disks */
2103 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 18, 80, 1, FDRIVE_RATE_500K, },
2104 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 20, 80, 1, FDRIVE_RATE_500K, },
2105 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 21, 80, 1, FDRIVE_RATE_500K, },
2106 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 21, 82, 1, FDRIVE_RATE_500K, },
2107 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 21, 83, 1, FDRIVE_RATE_500K, },
2108 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 22, 80, 1, FDRIVE_RATE_500K, },
2109 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 23, 80, 1, FDRIVE_RATE_500K, },
2110 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 24, 80, 1, FDRIVE_RATE_500K, },
2111 5bbdbb46 Blue Swirl
    /* 2.88 MB 3"1/2 floppy disks */
2112 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_288, 36, 80, 1, FDRIVE_RATE_1M, },
2113 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_288, 39, 80, 1, FDRIVE_RATE_1M, },
2114 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_288, 40, 80, 1, FDRIVE_RATE_1M, },
2115 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_288, 44, 80, 1, FDRIVE_RATE_1M, },
2116 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_288, 48, 80, 1, FDRIVE_RATE_1M, },
2117 5bbdbb46 Blue Swirl
    /* 720 kB 3"1/2 floppy disks */
2118 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144,  9, 80, 1, FDRIVE_RATE_250K, },
2119 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 10, 80, 1, FDRIVE_RATE_250K, },
2120 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 10, 82, 1, FDRIVE_RATE_250K, },
2121 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 10, 83, 1, FDRIVE_RATE_250K, },
2122 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 13, 80, 1, FDRIVE_RATE_250K, },
2123 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144, 14, 80, 1, FDRIVE_RATE_250K, },
2124 5bbdbb46 Blue Swirl
    /* 1.2 MB 5"1/4 floppy disks */
2125 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120, 15, 80, 1, FDRIVE_RATE_500K, },
2126 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120, 18, 80, 1, FDRIVE_RATE_500K, },
2127 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120, 18, 82, 1, FDRIVE_RATE_500K, },
2128 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120, 18, 83, 1, FDRIVE_RATE_500K, },
2129 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120, 20, 80, 1, FDRIVE_RATE_500K, },
2130 5bbdbb46 Blue Swirl
    /* 720 kB 5"1/4 floppy disks */
2131 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120,  9, 80, 1, FDRIVE_RATE_250K, },
2132 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120, 11, 80, 1, FDRIVE_RATE_250K, },
2133 5bbdbb46 Blue Swirl
    /* 360 kB 5"1/4 floppy disks */
2134 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120,  9, 40, 1, FDRIVE_RATE_300K, },
2135 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120,  9, 40, 0, FDRIVE_RATE_300K, },
2136 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120, 10, 41, 1, FDRIVE_RATE_300K, },
2137 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120, 10, 42, 1, FDRIVE_RATE_300K, },
2138 5bbdbb46 Blue Swirl
    /* 320 kB 5"1/4 floppy disks */
2139 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120,  8, 40, 1, FDRIVE_RATE_250K, },
2140 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_120,  8, 40, 0, FDRIVE_RATE_250K, },
2141 5bbdbb46 Blue Swirl
    /* 360 kB must match 5"1/4 better than 3"1/2... */
2142 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_144,  9, 80, 0, FDRIVE_RATE_250K, },
2143 5bbdbb46 Blue Swirl
    /* end */
2144 f8d3d128 Hervรฉ Poussineau
    { FDRIVE_DRV_NONE, -1, -1, 0, 0, },
2145 5bbdbb46 Blue Swirl
};
2146 5bbdbb46 Blue Swirl
2147 5bbdbb46 Blue Swirl
void bdrv_get_floppy_geometry_hint(BlockDriverState *bs, int *nb_heads,
2148 5bbdbb46 Blue Swirl
                                   int *max_track, int *last_sect,
2149 f8d3d128 Hervรฉ Poussineau
                                   FDriveType drive_in, FDriveType *drive,
2150 f8d3d128 Hervรฉ Poussineau
                                   FDriveRate *rate)
2151 5bbdbb46 Blue Swirl
{
2152 5bbdbb46 Blue Swirl
    const FDFormat *parse;
2153 5bbdbb46 Blue Swirl
    uint64_t nb_sectors, size;
2154 5bbdbb46 Blue Swirl
    int i, first_match, match;
2155 5bbdbb46 Blue Swirl
2156 5bbdbb46 Blue Swirl
    bdrv_get_geometry_hint(bs, nb_heads, max_track, last_sect);
2157 5bbdbb46 Blue Swirl
    if (*nb_heads != 0 && *max_track != 0 && *last_sect != 0) {
2158 5bbdbb46 Blue Swirl
        /* User defined disk */
2159 f8d3d128 Hervรฉ Poussineau
        *rate = FDRIVE_RATE_500K;
2160 5bbdbb46 Blue Swirl
    } else {
2161 5bbdbb46 Blue Swirl
        bdrv_get_geometry(bs, &nb_sectors);
2162 5bbdbb46 Blue Swirl
        match = -1;
2163 5bbdbb46 Blue Swirl
        first_match = -1;
2164 5bbdbb46 Blue Swirl
        for (i = 0; ; i++) {
2165 5bbdbb46 Blue Swirl
            parse = &fd_formats[i];
2166 5bbdbb46 Blue Swirl
            if (parse->drive == FDRIVE_DRV_NONE) {
2167 5bbdbb46 Blue Swirl
                break;
2168 5bbdbb46 Blue Swirl
            }
2169 5bbdbb46 Blue Swirl
            if (drive_in == parse->drive ||
2170 5bbdbb46 Blue Swirl
                drive_in == FDRIVE_DRV_NONE) {
2171 5bbdbb46 Blue Swirl
                size = (parse->max_head + 1) * parse->max_track *
2172 5bbdbb46 Blue Swirl
                    parse->last_sect;
2173 5bbdbb46 Blue Swirl
                if (nb_sectors == size) {
2174 5bbdbb46 Blue Swirl
                    match = i;
2175 5bbdbb46 Blue Swirl
                    break;
2176 5bbdbb46 Blue Swirl
                }
2177 5bbdbb46 Blue Swirl
                if (first_match == -1) {
2178 5bbdbb46 Blue Swirl
                    first_match = i;
2179 5bbdbb46 Blue Swirl
                }
2180 5bbdbb46 Blue Swirl
            }
2181 5bbdbb46 Blue Swirl
        }
2182 5bbdbb46 Blue Swirl
        if (match == -1) {
2183 5bbdbb46 Blue Swirl
            if (first_match == -1) {
2184 5bbdbb46 Blue Swirl
                match = 1;
2185 5bbdbb46 Blue Swirl
            } else {
2186 5bbdbb46 Blue Swirl
                match = first_match;
2187 5bbdbb46 Blue Swirl
            }
2188 5bbdbb46 Blue Swirl
            parse = &fd_formats[match];
2189 5bbdbb46 Blue Swirl
        }
2190 5bbdbb46 Blue Swirl
        *nb_heads = parse->max_head + 1;
2191 5bbdbb46 Blue Swirl
        *max_track = parse->max_track;
2192 5bbdbb46 Blue Swirl
        *last_sect = parse->last_sect;
2193 5bbdbb46 Blue Swirl
        *drive = parse->drive;
2194 f8d3d128 Hervรฉ Poussineau
        *rate = parse->rate;
2195 5bbdbb46 Blue Swirl
    }
2196 5bbdbb46 Blue Swirl
}
2197 5bbdbb46 Blue Swirl
2198 46d4767d bellard
int bdrv_get_translation_hint(BlockDriverState *bs)
2199 46d4767d bellard
{
2200 46d4767d bellard
    return bs->translation;
2201 46d4767d bellard
}
2202 46d4767d bellard
2203 abd7f68d Markus Armbruster
void bdrv_set_on_error(BlockDriverState *bs, BlockErrorAction on_read_error,
2204 abd7f68d Markus Armbruster
                       BlockErrorAction on_write_error)
2205 abd7f68d Markus Armbruster
{
2206 abd7f68d Markus Armbruster
    bs->on_read_error = on_read_error;
2207 abd7f68d Markus Armbruster
    bs->on_write_error = on_write_error;
2208 abd7f68d Markus Armbruster
}
2209 abd7f68d Markus Armbruster
2210 abd7f68d Markus Armbruster
BlockErrorAction bdrv_get_on_error(BlockDriverState *bs, int is_read)
2211 abd7f68d Markus Armbruster
{
2212 abd7f68d Markus Armbruster
    return is_read ? bs->on_read_error : bs->on_write_error;
2213 abd7f68d Markus Armbruster
}
2214 abd7f68d Markus Armbruster
2215 b338082b bellard
int bdrv_is_read_only(BlockDriverState *bs)
2216 b338082b bellard
{
2217 b338082b bellard
    return bs->read_only;
2218 b338082b bellard
}
2219 b338082b bellard
2220 985a03b0 ths
int bdrv_is_sg(BlockDriverState *bs)
2221 985a03b0 ths
{
2222 985a03b0 ths
    return bs->sg;
2223 985a03b0 ths
}
2224 985a03b0 ths
2225 e900a7b7 Christoph Hellwig
int bdrv_enable_write_cache(BlockDriverState *bs)
2226 e900a7b7 Christoph Hellwig
{
2227 e900a7b7 Christoph Hellwig
    return bs->enable_write_cache;
2228 e900a7b7 Christoph Hellwig
}
2229 e900a7b7 Christoph Hellwig
2230 ea2384d3 bellard
int bdrv_is_encrypted(BlockDriverState *bs)
2231 ea2384d3 bellard
{
2232 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted)
2233 ea2384d3 bellard
        return 1;
2234 ea2384d3 bellard
    return bs->encrypted;
2235 ea2384d3 bellard
}
2236 ea2384d3 bellard
2237 c0f4ce77 aliguori
int bdrv_key_required(BlockDriverState *bs)
2238 c0f4ce77 aliguori
{
2239 c0f4ce77 aliguori
    BlockDriverState *backing_hd = bs->backing_hd;
2240 c0f4ce77 aliguori
2241 c0f4ce77 aliguori
    if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
2242 c0f4ce77 aliguori
        return 1;
2243 c0f4ce77 aliguori
    return (bs->encrypted && !bs->valid_key);
2244 c0f4ce77 aliguori
}
2245 c0f4ce77 aliguori
2246 ea2384d3 bellard
int bdrv_set_key(BlockDriverState *bs, const char *key)
2247 ea2384d3 bellard
{
2248 ea2384d3 bellard
    int ret;
2249 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted) {
2250 ea2384d3 bellard
        ret = bdrv_set_key(bs->backing_hd, key);
2251 ea2384d3 bellard
        if (ret < 0)
2252 ea2384d3 bellard
            return ret;
2253 ea2384d3 bellard
        if (!bs->encrypted)
2254 ea2384d3 bellard
            return 0;
2255 ea2384d3 bellard
    }
2256 fd04a2ae Shahar Havivi
    if (!bs->encrypted) {
2257 fd04a2ae Shahar Havivi
        return -EINVAL;
2258 fd04a2ae Shahar Havivi
    } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2259 fd04a2ae Shahar Havivi
        return -ENOMEDIUM;
2260 fd04a2ae Shahar Havivi
    }
2261 c0f4ce77 aliguori
    ret = bs->drv->bdrv_set_key(bs, key);
2262 bb5fc20f aliguori
    if (ret < 0) {
2263 bb5fc20f aliguori
        bs->valid_key = 0;
2264 bb5fc20f aliguori
    } else if (!bs->valid_key) {
2265 bb5fc20f aliguori
        bs->valid_key = 1;
2266 bb5fc20f aliguori
        /* call the change callback now, we skipped it on open */
2267 7d4b4ba5 Markus Armbruster
        bdrv_dev_change_media_cb(bs, true);
2268 bb5fc20f aliguori
    }
2269 c0f4ce77 aliguori
    return ret;
2270 ea2384d3 bellard
}
2271 ea2384d3 bellard
2272 ea2384d3 bellard
void bdrv_get_format(BlockDriverState *bs, char *buf, int buf_size)
2273 ea2384d3 bellard
{
2274 19cb3738 bellard
    if (!bs->drv) {
2275 ea2384d3 bellard
        buf[0] = '\0';
2276 ea2384d3 bellard
    } else {
2277 ea2384d3 bellard
        pstrcpy(buf, buf_size, bs->drv->format_name);
2278 ea2384d3 bellard
    }
2279 ea2384d3 bellard
}
2280 ea2384d3 bellard
2281 5fafdf24 ths
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
2282 ea2384d3 bellard
                         void *opaque)
2283 ea2384d3 bellard
{
2284 ea2384d3 bellard
    BlockDriver *drv;
2285 ea2384d3 bellard
2286 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv, &bdrv_drivers, list) {
2287 ea2384d3 bellard
        it(opaque, drv->format_name);
2288 ea2384d3 bellard
    }
2289 ea2384d3 bellard
}
2290 ea2384d3 bellard
2291 b338082b bellard
BlockDriverState *bdrv_find(const char *name)
2292 b338082b bellard
{
2293 b338082b bellard
    BlockDriverState *bs;
2294 b338082b bellard
2295 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2296 1b7bdbc1 Stefan Hajnoczi
        if (!strcmp(name, bs->device_name)) {
2297 b338082b bellard
            return bs;
2298 1b7bdbc1 Stefan Hajnoczi
        }
2299 b338082b bellard
    }
2300 b338082b bellard
    return NULL;
2301 b338082b bellard
}
2302 b338082b bellard
2303 2f399b0a Markus Armbruster
BlockDriverState *bdrv_next(BlockDriverState *bs)
2304 2f399b0a Markus Armbruster
{
2305 2f399b0a Markus Armbruster
    if (!bs) {
2306 2f399b0a Markus Armbruster
        return QTAILQ_FIRST(&bdrv_states);
2307 2f399b0a Markus Armbruster
    }
2308 2f399b0a Markus Armbruster
    return QTAILQ_NEXT(bs, list);
2309 2f399b0a Markus Armbruster
}
2310 2f399b0a Markus Armbruster
2311 51de9760 aliguori
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
2312 81d0912d bellard
{
2313 81d0912d bellard
    BlockDriverState *bs;
2314 81d0912d bellard
2315 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2316 51de9760 aliguori
        it(opaque, bs);
2317 81d0912d bellard
    }
2318 81d0912d bellard
}
2319 81d0912d bellard
2320 ea2384d3 bellard
const char *bdrv_get_device_name(BlockDriverState *bs)
2321 ea2384d3 bellard
{
2322 ea2384d3 bellard
    return bs->device_name;
2323 ea2384d3 bellard
}
2324 ea2384d3 bellard
2325 c6ca28d6 aliguori
void bdrv_flush_all(void)
2326 c6ca28d6 aliguori
{
2327 c6ca28d6 aliguori
    BlockDriverState *bs;
2328 c6ca28d6 aliguori
2329 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2330 c602a489 Markus Armbruster
        if (!bdrv_is_read_only(bs) && bdrv_is_inserted(bs)) {
2331 c6ca28d6 aliguori
            bdrv_flush(bs);
2332 1b7bdbc1 Stefan Hajnoczi
        }
2333 1b7bdbc1 Stefan Hajnoczi
    }
2334 c6ca28d6 aliguori
}
2335 c6ca28d6 aliguori
2336 f2feebbd Kevin Wolf
int bdrv_has_zero_init(BlockDriverState *bs)
2337 f2feebbd Kevin Wolf
{
2338 f2feebbd Kevin Wolf
    assert(bs->drv);
2339 f2feebbd Kevin Wolf
2340 336c1c12 Kevin Wolf
    if (bs->drv->bdrv_has_zero_init) {
2341 336c1c12 Kevin Wolf
        return bs->drv->bdrv_has_zero_init(bs);
2342 f2feebbd Kevin Wolf
    }
2343 f2feebbd Kevin Wolf
2344 f2feebbd Kevin Wolf
    return 1;
2345 f2feebbd Kevin Wolf
}
2346 f2feebbd Kevin Wolf
2347 376ae3f1 Stefan Hajnoczi
typedef struct BdrvCoIsAllocatedData {
2348 376ae3f1 Stefan Hajnoczi
    BlockDriverState *bs;
2349 376ae3f1 Stefan Hajnoczi
    int64_t sector_num;
2350 376ae3f1 Stefan Hajnoczi
    int nb_sectors;
2351 376ae3f1 Stefan Hajnoczi
    int *pnum;
2352 376ae3f1 Stefan Hajnoczi
    int ret;
2353 376ae3f1 Stefan Hajnoczi
    bool done;
2354 376ae3f1 Stefan Hajnoczi
} BdrvCoIsAllocatedData;
2355 376ae3f1 Stefan Hajnoczi
2356 f58c7b35 ths
/*
2357 f58c7b35 ths
 * Returns true iff the specified sector is present in the disk image. Drivers
2358 f58c7b35 ths
 * not implementing the functionality are assumed to not support backing files,
2359 f58c7b35 ths
 * hence all their sectors are reported as allocated.
2360 f58c7b35 ths
 *
2361 bd9533e3 Stefan Hajnoczi
 * If 'sector_num' is beyond the end of the disk image the return value is 0
2362 bd9533e3 Stefan Hajnoczi
 * and 'pnum' is set to 0.
2363 bd9533e3 Stefan Hajnoczi
 *
2364 f58c7b35 ths
 * 'pnum' is set to the number of sectors (including and immediately following
2365 f58c7b35 ths
 * the specified sector) that are known to be in the same
2366 f58c7b35 ths
 * allocated/unallocated state.
2367 f58c7b35 ths
 *
2368 bd9533e3 Stefan Hajnoczi
 * 'nb_sectors' is the max value 'pnum' should be set to.  If nb_sectors goes
2369 bd9533e3 Stefan Hajnoczi
 * beyond the end of the disk image it will be clamped.
2370 f58c7b35 ths
 */
2371 060f51c9 Stefan Hajnoczi
int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num,
2372 060f51c9 Stefan Hajnoczi
                                      int nb_sectors, int *pnum)
2373 f58c7b35 ths
{
2374 bd9533e3 Stefan Hajnoczi
    int64_t n;
2375 bd9533e3 Stefan Hajnoczi
2376 bd9533e3 Stefan Hajnoczi
    if (sector_num >= bs->total_sectors) {
2377 bd9533e3 Stefan Hajnoczi
        *pnum = 0;
2378 bd9533e3 Stefan Hajnoczi
        return 0;
2379 bd9533e3 Stefan Hajnoczi
    }
2380 bd9533e3 Stefan Hajnoczi
2381 bd9533e3 Stefan Hajnoczi
    n = bs->total_sectors - sector_num;
2382 bd9533e3 Stefan Hajnoczi
    if (n < nb_sectors) {
2383 bd9533e3 Stefan Hajnoczi
        nb_sectors = n;
2384 bd9533e3 Stefan Hajnoczi
    }
2385 bd9533e3 Stefan Hajnoczi
2386 6aebab14 Stefan Hajnoczi
    if (!bs->drv->bdrv_co_is_allocated) {
2387 bd9533e3 Stefan Hajnoczi
        *pnum = nb_sectors;
2388 f58c7b35 ths
        return 1;
2389 f58c7b35 ths
    }
2390 6aebab14 Stefan Hajnoczi
2391 060f51c9 Stefan Hajnoczi
    return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum);
2392 060f51c9 Stefan Hajnoczi
}
2393 060f51c9 Stefan Hajnoczi
2394 060f51c9 Stefan Hajnoczi
/* Coroutine wrapper for bdrv_is_allocated() */
2395 060f51c9 Stefan Hajnoczi
static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque)
2396 060f51c9 Stefan Hajnoczi
{
2397 060f51c9 Stefan Hajnoczi
    BdrvCoIsAllocatedData *data = opaque;
2398 060f51c9 Stefan Hajnoczi
    BlockDriverState *bs = data->bs;
2399 060f51c9 Stefan Hajnoczi
2400 060f51c9 Stefan Hajnoczi
    data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors,
2401 060f51c9 Stefan Hajnoczi
                                     data->pnum);
2402 060f51c9 Stefan Hajnoczi
    data->done = true;
2403 060f51c9 Stefan Hajnoczi
}
2404 060f51c9 Stefan Hajnoczi
2405 060f51c9 Stefan Hajnoczi
/*
2406 060f51c9 Stefan Hajnoczi
 * Synchronous wrapper around bdrv_co_is_allocated().
2407 060f51c9 Stefan Hajnoczi
 *
2408 060f51c9 Stefan Hajnoczi
 * See bdrv_co_is_allocated() for details.
2409 060f51c9 Stefan Hajnoczi
 */
2410 060f51c9 Stefan Hajnoczi
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
2411 060f51c9 Stefan Hajnoczi
                      int *pnum)
2412 060f51c9 Stefan Hajnoczi
{
2413 6aebab14 Stefan Hajnoczi
    Coroutine *co;
2414 6aebab14 Stefan Hajnoczi
    BdrvCoIsAllocatedData data = {
2415 6aebab14 Stefan Hajnoczi
        .bs = bs,
2416 6aebab14 Stefan Hajnoczi
        .sector_num = sector_num,
2417 6aebab14 Stefan Hajnoczi
        .nb_sectors = nb_sectors,
2418 6aebab14 Stefan Hajnoczi
        .pnum = pnum,
2419 6aebab14 Stefan Hajnoczi
        .done = false,
2420 6aebab14 Stefan Hajnoczi
    };
2421 6aebab14 Stefan Hajnoczi
2422 6aebab14 Stefan Hajnoczi
    co = qemu_coroutine_create(bdrv_is_allocated_co_entry);
2423 6aebab14 Stefan Hajnoczi
    qemu_coroutine_enter(co, &data);
2424 6aebab14 Stefan Hajnoczi
    while (!data.done) {
2425 6aebab14 Stefan Hajnoczi
        qemu_aio_wait();
2426 6aebab14 Stefan Hajnoczi
    }
2427 6aebab14 Stefan Hajnoczi
    return data.ret;
2428 f58c7b35 ths
}
2429 f58c7b35 ths
2430 b2023818 Luiz Capitulino
BlockInfoList *qmp_query_block(Error **errp)
2431 b338082b bellard
{
2432 b2023818 Luiz Capitulino
    BlockInfoList *head = NULL, *cur_item = NULL;
2433 b338082b bellard
    BlockDriverState *bs;
2434 b338082b bellard
2435 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2436 b2023818 Luiz Capitulino
        BlockInfoList *info = g_malloc0(sizeof(*info));
2437 d15e5465 Luiz Capitulino
2438 b2023818 Luiz Capitulino
        info->value = g_malloc0(sizeof(*info->value));
2439 b2023818 Luiz Capitulino
        info->value->device = g_strdup(bs->device_name);
2440 b2023818 Luiz Capitulino
        info->value->type = g_strdup("unknown");
2441 b2023818 Luiz Capitulino
        info->value->locked = bdrv_dev_is_medium_locked(bs);
2442 b2023818 Luiz Capitulino
        info->value->removable = bdrv_dev_has_removable_media(bs);
2443 d15e5465 Luiz Capitulino
2444 e4def80b Markus Armbruster
        if (bdrv_dev_has_removable_media(bs)) {
2445 b2023818 Luiz Capitulino
            info->value->has_tray_open = true;
2446 b2023818 Luiz Capitulino
            info->value->tray_open = bdrv_dev_is_tray_open(bs);
2447 e4def80b Markus Armbruster
        }
2448 f04ef601 Luiz Capitulino
2449 f04ef601 Luiz Capitulino
        if (bdrv_iostatus_is_enabled(bs)) {
2450 b2023818 Luiz Capitulino
            info->value->has_io_status = true;
2451 b2023818 Luiz Capitulino
            info->value->io_status = bs->iostatus;
2452 f04ef601 Luiz Capitulino
        }
2453 f04ef601 Luiz Capitulino
2454 19cb3738 bellard
        if (bs->drv) {
2455 b2023818 Luiz Capitulino
            info->value->has_inserted = true;
2456 b2023818 Luiz Capitulino
            info->value->inserted = g_malloc0(sizeof(*info->value->inserted));
2457 b2023818 Luiz Capitulino
            info->value->inserted->file = g_strdup(bs->filename);
2458 b2023818 Luiz Capitulino
            info->value->inserted->ro = bs->read_only;
2459 b2023818 Luiz Capitulino
            info->value->inserted->drv = g_strdup(bs->drv->format_name);
2460 b2023818 Luiz Capitulino
            info->value->inserted->encrypted = bs->encrypted;
2461 b2023818 Luiz Capitulino
            if (bs->backing_file[0]) {
2462 b2023818 Luiz Capitulino
                info->value->inserted->has_backing_file = true;
2463 b2023818 Luiz Capitulino
                info->value->inserted->backing_file = g_strdup(bs->backing_file);
2464 376253ec aliguori
            }
2465 727f005e Zhi Yong Wu
2466 727f005e Zhi Yong Wu
            if (bs->io_limits_enabled) {
2467 727f005e Zhi Yong Wu
                info->value->inserted->bps =
2468 727f005e Zhi Yong Wu
                               bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
2469 727f005e Zhi Yong Wu
                info->value->inserted->bps_rd =
2470 727f005e Zhi Yong Wu
                               bs->io_limits.bps[BLOCK_IO_LIMIT_READ];
2471 727f005e Zhi Yong Wu
                info->value->inserted->bps_wr =
2472 727f005e Zhi Yong Wu
                               bs->io_limits.bps[BLOCK_IO_LIMIT_WRITE];
2473 727f005e Zhi Yong Wu
                info->value->inserted->iops =
2474 727f005e Zhi Yong Wu
                               bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
2475 727f005e Zhi Yong Wu
                info->value->inserted->iops_rd =
2476 727f005e Zhi Yong Wu
                               bs->io_limits.iops[BLOCK_IO_LIMIT_READ];
2477 727f005e Zhi Yong Wu
                info->value->inserted->iops_wr =
2478 727f005e Zhi Yong Wu
                               bs->io_limits.iops[BLOCK_IO_LIMIT_WRITE];
2479 727f005e Zhi Yong Wu
            }
2480 b2023818 Luiz Capitulino
        }
2481 d15e5465 Luiz Capitulino
2482 b2023818 Luiz Capitulino
        /* XXX: waiting for the qapi to support GSList */
2483 b2023818 Luiz Capitulino
        if (!cur_item) {
2484 b2023818 Luiz Capitulino
            head = cur_item = info;
2485 b2023818 Luiz Capitulino
        } else {
2486 b2023818 Luiz Capitulino
            cur_item->next = info;
2487 b2023818 Luiz Capitulino
            cur_item = info;
2488 b338082b bellard
        }
2489 b338082b bellard
    }
2490 d15e5465 Luiz Capitulino
2491 b2023818 Luiz Capitulino
    return head;
2492 b338082b bellard
}
2493 a36e69dd ths
2494 f11f57e4 Luiz Capitulino
/* Consider exposing this as a full fledged QMP command */
2495 f11f57e4 Luiz Capitulino
static BlockStats *qmp_query_blockstat(const BlockDriverState *bs, Error **errp)
2496 f11f57e4 Luiz Capitulino
{
2497 f11f57e4 Luiz Capitulino
    BlockStats *s;
2498 f11f57e4 Luiz Capitulino
2499 f11f57e4 Luiz Capitulino
    s = g_malloc0(sizeof(*s));
2500 f11f57e4 Luiz Capitulino
2501 f11f57e4 Luiz Capitulino
    if (bs->device_name[0]) {
2502 f11f57e4 Luiz Capitulino
        s->has_device = true;
2503 f11f57e4 Luiz Capitulino
        s->device = g_strdup(bs->device_name);
2504 294cc35f Kevin Wolf
    }
2505 294cc35f Kevin Wolf
2506 f11f57e4 Luiz Capitulino
    s->stats = g_malloc0(sizeof(*s->stats));
2507 f11f57e4 Luiz Capitulino
    s->stats->rd_bytes = bs->nr_bytes[BDRV_ACCT_READ];
2508 f11f57e4 Luiz Capitulino
    s->stats->wr_bytes = bs->nr_bytes[BDRV_ACCT_WRITE];
2509 f11f57e4 Luiz Capitulino
    s->stats->rd_operations = bs->nr_ops[BDRV_ACCT_READ];
2510 f11f57e4 Luiz Capitulino
    s->stats->wr_operations = bs->nr_ops[BDRV_ACCT_WRITE];
2511 f11f57e4 Luiz Capitulino
    s->stats->wr_highest_offset = bs->wr_highest_sector * BDRV_SECTOR_SIZE;
2512 f11f57e4 Luiz Capitulino
    s->stats->flush_operations = bs->nr_ops[BDRV_ACCT_FLUSH];
2513 f11f57e4 Luiz Capitulino
    s->stats->wr_total_time_ns = bs->total_time_ns[BDRV_ACCT_WRITE];
2514 f11f57e4 Luiz Capitulino
    s->stats->rd_total_time_ns = bs->total_time_ns[BDRV_ACCT_READ];
2515 f11f57e4 Luiz Capitulino
    s->stats->flush_total_time_ns = bs->total_time_ns[BDRV_ACCT_FLUSH];
2516 f11f57e4 Luiz Capitulino
2517 294cc35f Kevin Wolf
    if (bs->file) {
2518 f11f57e4 Luiz Capitulino
        s->has_parent = true;
2519 f11f57e4 Luiz Capitulino
        s->parent = qmp_query_blockstat(bs->file, NULL);
2520 294cc35f Kevin Wolf
    }
2521 294cc35f Kevin Wolf
2522 f11f57e4 Luiz Capitulino
    return s;
2523 294cc35f Kevin Wolf
}
2524 294cc35f Kevin Wolf
2525 f11f57e4 Luiz Capitulino
BlockStatsList *qmp_query_blockstats(Error **errp)
2526 218a536a Luiz Capitulino
{
2527 f11f57e4 Luiz Capitulino
    BlockStatsList *head = NULL, *cur_item = NULL;
2528 a36e69dd ths
    BlockDriverState *bs;
2529 a36e69dd ths
2530 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2531 f11f57e4 Luiz Capitulino
        BlockStatsList *info = g_malloc0(sizeof(*info));
2532 f11f57e4 Luiz Capitulino
        info->value = qmp_query_blockstat(bs, NULL);
2533 f11f57e4 Luiz Capitulino
2534 f11f57e4 Luiz Capitulino
        /* XXX: waiting for the qapi to support GSList */
2535 f11f57e4 Luiz Capitulino
        if (!cur_item) {
2536 f11f57e4 Luiz Capitulino
            head = cur_item = info;
2537 f11f57e4 Luiz Capitulino
        } else {
2538 f11f57e4 Luiz Capitulino
            cur_item->next = info;
2539 f11f57e4 Luiz Capitulino
            cur_item = info;
2540 f11f57e4 Luiz Capitulino
        }
2541 a36e69dd ths
    }
2542 218a536a Luiz Capitulino
2543 f11f57e4 Luiz Capitulino
    return head;
2544 a36e69dd ths
}
2545 ea2384d3 bellard
2546 045df330 aliguori
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
2547 045df330 aliguori
{
2548 045df330 aliguori
    if (bs->backing_hd && bs->backing_hd->encrypted)
2549 045df330 aliguori
        return bs->backing_file;
2550 045df330 aliguori
    else if (bs->encrypted)
2551 045df330 aliguori
        return bs->filename;
2552 045df330 aliguori
    else
2553 045df330 aliguori
        return NULL;
2554 045df330 aliguori
}
2555 045df330 aliguori
2556 5fafdf24 ths
void bdrv_get_backing_filename(BlockDriverState *bs,
2557 83f64091 bellard
                               char *filename, int filename_size)
2558 83f64091 bellard
{
2559 3574c608 Kevin Wolf
    pstrcpy(filename, filename_size, bs->backing_file);
2560 83f64091 bellard
}
2561 83f64091 bellard
2562 5fafdf24 ths
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
2563 faea38e7 bellard
                          const uint8_t *buf, int nb_sectors)
2564 faea38e7 bellard
{
2565 faea38e7 bellard
    BlockDriver *drv = bs->drv;
2566 faea38e7 bellard
    if (!drv)
2567 19cb3738 bellard
        return -ENOMEDIUM;
2568 faea38e7 bellard
    if (!drv->bdrv_write_compressed)
2569 faea38e7 bellard
        return -ENOTSUP;
2570 fbb7b4e0 Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors))
2571 fbb7b4e0 Kevin Wolf
        return -EIO;
2572 a55eb92c Jan Kiszka
2573 c6d22830 Jan Kiszka
    if (bs->dirty_bitmap) {
2574 7cd1e32a lirans@il.ibm.com
        set_dirty_bitmap(bs, sector_num, nb_sectors, 1);
2575 7cd1e32a lirans@il.ibm.com
    }
2576 a55eb92c Jan Kiszka
2577 faea38e7 bellard
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
2578 faea38e7 bellard
}
2579 3b46e624 ths
2580 faea38e7 bellard
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
2581 faea38e7 bellard
{
2582 faea38e7 bellard
    BlockDriver *drv = bs->drv;
2583 faea38e7 bellard
    if (!drv)
2584 19cb3738 bellard
        return -ENOMEDIUM;
2585 faea38e7 bellard
    if (!drv->bdrv_get_info)
2586 faea38e7 bellard
        return -ENOTSUP;
2587 faea38e7 bellard
    memset(bdi, 0, sizeof(*bdi));
2588 faea38e7 bellard
    return drv->bdrv_get_info(bs, bdi);
2589 faea38e7 bellard
}
2590 faea38e7 bellard
2591 45566e9c Christoph Hellwig
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
2592 45566e9c Christoph Hellwig
                      int64_t pos, int size)
2593 178e08a5 aliguori
{
2594 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
2595 178e08a5 aliguori
    if (!drv)
2596 178e08a5 aliguori
        return -ENOMEDIUM;
2597 7cdb1f6d MORITA Kazutaka
    if (drv->bdrv_save_vmstate)
2598 7cdb1f6d MORITA Kazutaka
        return drv->bdrv_save_vmstate(bs, buf, pos, size);
2599 7cdb1f6d MORITA Kazutaka
    if (bs->file)
2600 7cdb1f6d MORITA Kazutaka
        return bdrv_save_vmstate(bs->file, buf, pos, size);
2601 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
2602 178e08a5 aliguori
}
2603 178e08a5 aliguori
2604 45566e9c Christoph Hellwig
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
2605 45566e9c Christoph Hellwig
                      int64_t pos, int size)
2606 178e08a5 aliguori
{
2607 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
2608 178e08a5 aliguori
    if (!drv)
2609 178e08a5 aliguori
        return -ENOMEDIUM;
2610 7cdb1f6d MORITA Kazutaka
    if (drv->bdrv_load_vmstate)
2611 7cdb1f6d MORITA Kazutaka
        return drv->bdrv_load_vmstate(bs, buf, pos, size);
2612 7cdb1f6d MORITA Kazutaka
    if (bs->file)
2613 7cdb1f6d MORITA Kazutaka
        return bdrv_load_vmstate(bs->file, buf, pos, size);
2614 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
2615 178e08a5 aliguori
}
2616 178e08a5 aliguori
2617 8b9b0cc2 Kevin Wolf
void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
2618 8b9b0cc2 Kevin Wolf
{
2619 8b9b0cc2 Kevin Wolf
    BlockDriver *drv = bs->drv;
2620 8b9b0cc2 Kevin Wolf
2621 8b9b0cc2 Kevin Wolf
    if (!drv || !drv->bdrv_debug_event) {
2622 8b9b0cc2 Kevin Wolf
        return;
2623 8b9b0cc2 Kevin Wolf
    }
2624 8b9b0cc2 Kevin Wolf
2625 8b9b0cc2 Kevin Wolf
    return drv->bdrv_debug_event(bs, event);
2626 8b9b0cc2 Kevin Wolf
2627 8b9b0cc2 Kevin Wolf
}
2628 8b9b0cc2 Kevin Wolf
2629 faea38e7 bellard
/**************************************************************/
2630 faea38e7 bellard
/* handling of snapshots */
2631 faea38e7 bellard
2632 feeee5ac Miguel Di Ciurcio Filho
int bdrv_can_snapshot(BlockDriverState *bs)
2633 feeee5ac Miguel Di Ciurcio Filho
{
2634 feeee5ac Miguel Di Ciurcio Filho
    BlockDriver *drv = bs->drv;
2635 07b70bfb Markus Armbruster
    if (!drv || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2636 feeee5ac Miguel Di Ciurcio Filho
        return 0;
2637 feeee5ac Miguel Di Ciurcio Filho
    }
2638 feeee5ac Miguel Di Ciurcio Filho
2639 feeee5ac Miguel Di Ciurcio Filho
    if (!drv->bdrv_snapshot_create) {
2640 feeee5ac Miguel Di Ciurcio Filho
        if (bs->file != NULL) {
2641 feeee5ac Miguel Di Ciurcio Filho
            return bdrv_can_snapshot(bs->file);
2642 feeee5ac Miguel Di Ciurcio Filho
        }
2643 feeee5ac Miguel Di Ciurcio Filho
        return 0;
2644 feeee5ac Miguel Di Ciurcio Filho
    }
2645 feeee5ac Miguel Di Ciurcio Filho
2646 feeee5ac Miguel Di Ciurcio Filho
    return 1;
2647 feeee5ac Miguel Di Ciurcio Filho
}
2648 feeee5ac Miguel Di Ciurcio Filho
2649 199630b6 Blue Swirl
int bdrv_is_snapshot(BlockDriverState *bs)
2650 199630b6 Blue Swirl
{
2651 199630b6 Blue Swirl
    return !!(bs->open_flags & BDRV_O_SNAPSHOT);
2652 199630b6 Blue Swirl
}
2653 199630b6 Blue Swirl
2654 f9092b10 Markus Armbruster
BlockDriverState *bdrv_snapshots(void)
2655 f9092b10 Markus Armbruster
{
2656 f9092b10 Markus Armbruster
    BlockDriverState *bs;
2657 f9092b10 Markus Armbruster
2658 3ac906f7 Markus Armbruster
    if (bs_snapshots) {
2659 f9092b10 Markus Armbruster
        return bs_snapshots;
2660 3ac906f7 Markus Armbruster
    }
2661 f9092b10 Markus Armbruster
2662 f9092b10 Markus Armbruster
    bs = NULL;
2663 f9092b10 Markus Armbruster
    while ((bs = bdrv_next(bs))) {
2664 f9092b10 Markus Armbruster
        if (bdrv_can_snapshot(bs)) {
2665 3ac906f7 Markus Armbruster
            bs_snapshots = bs;
2666 3ac906f7 Markus Armbruster
            return bs;
2667 f9092b10 Markus Armbruster
        }
2668 f9092b10 Markus Armbruster
    }
2669 f9092b10 Markus Armbruster
    return NULL;
2670 f9092b10 Markus Armbruster
}
2671 f9092b10 Markus Armbruster
2672 5fafdf24 ths
int bdrv_snapshot_create(BlockDriverState *bs,
2673 faea38e7 bellard
                         QEMUSnapshotInfo *sn_info)
2674 faea38e7 bellard
{
2675 faea38e7 bellard
    BlockDriver *drv = bs->drv;
2676 faea38e7 bellard
    if (!drv)
2677 19cb3738 bellard
        return -ENOMEDIUM;
2678 7cdb1f6d MORITA Kazutaka
    if (drv->bdrv_snapshot_create)
2679 7cdb1f6d MORITA Kazutaka
        return drv->bdrv_snapshot_create(bs, sn_info);
2680 7cdb1f6d MORITA Kazutaka
    if (bs->file)
2681 7cdb1f6d MORITA Kazutaka
        return bdrv_snapshot_create(bs->file, sn_info);
2682 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
2683 faea38e7 bellard
}
2684 faea38e7 bellard
2685 5fafdf24 ths
int bdrv_snapshot_goto(BlockDriverState *bs,
2686 faea38e7 bellard
                       const char *snapshot_id)
2687 faea38e7 bellard
{
2688 faea38e7 bellard
    BlockDriver *drv = bs->drv;
2689 7cdb1f6d MORITA Kazutaka
    int ret, open_ret;
2690 7cdb1f6d MORITA Kazutaka
2691 faea38e7 bellard
    if (!drv)
2692 19cb3738 bellard
        return -ENOMEDIUM;
2693 7cdb1f6d MORITA Kazutaka
    if (drv->bdrv_snapshot_goto)
2694 7cdb1f6d MORITA Kazutaka
        return drv->bdrv_snapshot_goto(bs, snapshot_id);
2695 7cdb1f6d MORITA Kazutaka
2696 7cdb1f6d MORITA Kazutaka
    if (bs->file) {
2697 7cdb1f6d MORITA Kazutaka
        drv->bdrv_close(bs);
2698 7cdb1f6d MORITA Kazutaka
        ret = bdrv_snapshot_goto(bs->file, snapshot_id);
2699 7cdb1f6d MORITA Kazutaka
        open_ret = drv->bdrv_open(bs, bs->open_flags);
2700 7cdb1f6d MORITA Kazutaka
        if (open_ret < 0) {
2701 7cdb1f6d MORITA Kazutaka
            bdrv_delete(bs->file);
2702 7cdb1f6d MORITA Kazutaka
            bs->drv = NULL;
2703 7cdb1f6d MORITA Kazutaka
            return open_ret;
2704 7cdb1f6d MORITA Kazutaka
        }
2705 7cdb1f6d MORITA Kazutaka
        return ret;
2706 7cdb1f6d MORITA Kazutaka
    }
2707 7cdb1f6d MORITA Kazutaka
2708 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
2709 faea38e7 bellard
}
2710 faea38e7 bellard
2711 faea38e7 bellard
int bdrv_snapshot_delete(BlockDriverState *bs, const char *snapshot_id)
2712 faea38e7 bellard
{
2713 faea38e7 bellard
    BlockDriver *drv = bs->drv;
2714 faea38e7 bellard
    if (!drv)
2715 19cb3738 bellard
        return -ENOMEDIUM;
2716 7cdb1f6d MORITA Kazutaka
    if (drv->bdrv_snapshot_delete)
2717 7cdb1f6d MORITA Kazutaka
        return drv->bdrv_snapshot_delete(bs, snapshot_id);
2718 7cdb1f6d MORITA Kazutaka
    if (bs->file)
2719 7cdb1f6d MORITA Kazutaka
        return bdrv_snapshot_delete(bs->file, snapshot_id);
2720 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
2721 faea38e7 bellard
}
2722 faea38e7 bellard
2723 5fafdf24 ths
int bdrv_snapshot_list(BlockDriverState *bs,
2724 faea38e7 bellard
                       QEMUSnapshotInfo **psn_info)
2725 faea38e7 bellard
{
2726 faea38e7 bellard
    BlockDriver *drv = bs->drv;
2727 faea38e7 bellard
    if (!drv)
2728 19cb3738 bellard
        return -ENOMEDIUM;
2729 7cdb1f6d MORITA Kazutaka
    if (drv->bdrv_snapshot_list)
2730 7cdb1f6d MORITA Kazutaka
        return drv->bdrv_snapshot_list(bs, psn_info);
2731 7cdb1f6d MORITA Kazutaka
    if (bs->file)
2732 7cdb1f6d MORITA Kazutaka
        return bdrv_snapshot_list(bs->file, psn_info);
2733 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
2734 faea38e7 bellard
}
2735 faea38e7 bellard
2736 51ef6727 edison
int bdrv_snapshot_load_tmp(BlockDriverState *bs,
2737 51ef6727 edison
        const char *snapshot_name)
2738 51ef6727 edison
{
2739 51ef6727 edison
    BlockDriver *drv = bs->drv;
2740 51ef6727 edison
    if (!drv) {
2741 51ef6727 edison
        return -ENOMEDIUM;
2742 51ef6727 edison
    }
2743 51ef6727 edison
    if (!bs->read_only) {
2744 51ef6727 edison
        return -EINVAL;
2745 51ef6727 edison
    }
2746 51ef6727 edison
    if (drv->bdrv_snapshot_load_tmp) {
2747 51ef6727 edison
        return drv->bdrv_snapshot_load_tmp(bs, snapshot_name);
2748 51ef6727 edison
    }
2749 51ef6727 edison
    return -ENOTSUP;
2750 51ef6727 edison
}
2751 51ef6727 edison
2752 e8a6bb9c Marcelo Tosatti
BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
2753 e8a6bb9c Marcelo Tosatti
        const char *backing_file)
2754 e8a6bb9c Marcelo Tosatti
{
2755 e8a6bb9c Marcelo Tosatti
    if (!bs->drv) {
2756 e8a6bb9c Marcelo Tosatti
        return NULL;
2757 e8a6bb9c Marcelo Tosatti
    }
2758 e8a6bb9c Marcelo Tosatti
2759 e8a6bb9c Marcelo Tosatti
    if (bs->backing_hd) {
2760 e8a6bb9c Marcelo Tosatti
        if (strcmp(bs->backing_file, backing_file) == 0) {
2761 e8a6bb9c Marcelo Tosatti
            return bs->backing_hd;
2762 e8a6bb9c Marcelo Tosatti
        } else {
2763 e8a6bb9c Marcelo Tosatti
            return bdrv_find_backing_image(bs->backing_hd, backing_file);
2764 e8a6bb9c Marcelo Tosatti
        }
2765 e8a6bb9c Marcelo Tosatti
    }
2766 e8a6bb9c Marcelo Tosatti
2767 e8a6bb9c Marcelo Tosatti
    return NULL;
2768 e8a6bb9c Marcelo Tosatti
}
2769 e8a6bb9c Marcelo Tosatti
2770 faea38e7 bellard
#define NB_SUFFIXES 4
2771 faea38e7 bellard
2772 faea38e7 bellard
char *get_human_readable_size(char *buf, int buf_size, int64_t size)
2773 faea38e7 bellard
{
2774 faea38e7 bellard
    static const char suffixes[NB_SUFFIXES] = "KMGT";
2775 faea38e7 bellard
    int64_t base;
2776 faea38e7 bellard
    int i;
2777 faea38e7 bellard
2778 faea38e7 bellard
    if (size <= 999) {
2779 faea38e7 bellard
        snprintf(buf, buf_size, "%" PRId64, size);
2780 faea38e7 bellard
    } else {
2781 faea38e7 bellard
        base = 1024;
2782 faea38e7 bellard
        for(i = 0; i < NB_SUFFIXES; i++) {
2783 faea38e7 bellard
            if (size < (10 * base)) {
2784 5fafdf24 ths
                snprintf(buf, buf_size, "%0.1f%c",
2785 faea38e7 bellard
                         (double)size / base,
2786 faea38e7 bellard
                         suffixes[i]);
2787 faea38e7 bellard
                break;
2788 faea38e7 bellard
            } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
2789 5fafdf24 ths
                snprintf(buf, buf_size, "%" PRId64 "%c",
2790 faea38e7 bellard
                         ((size + (base >> 1)) / base),
2791 faea38e7 bellard
                         suffixes[i]);
2792 faea38e7 bellard
                break;
2793 faea38e7 bellard
            }
2794 faea38e7 bellard
            base = base * 1024;
2795 faea38e7 bellard
        }
2796 faea38e7 bellard
    }
2797 faea38e7 bellard
    return buf;
2798 faea38e7 bellard
}
2799 faea38e7 bellard
2800 faea38e7 bellard
char *bdrv_snapshot_dump(char *buf, int buf_size, QEMUSnapshotInfo *sn)
2801 faea38e7 bellard
{
2802 faea38e7 bellard
    char buf1[128], date_buf[128], clock_buf[128];
2803 3b9f94e1 bellard
#ifdef _WIN32
2804 3b9f94e1 bellard
    struct tm *ptm;
2805 3b9f94e1 bellard
#else
2806 faea38e7 bellard
    struct tm tm;
2807 3b9f94e1 bellard
#endif
2808 faea38e7 bellard
    time_t ti;
2809 faea38e7 bellard
    int64_t secs;
2810 faea38e7 bellard
2811 faea38e7 bellard
    if (!sn) {
2812 5fafdf24 ths
        snprintf(buf, buf_size,
2813 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
2814 faea38e7 bellard
                 "ID", "TAG", "VM SIZE", "DATE", "VM CLOCK");
2815 faea38e7 bellard
    } else {
2816 faea38e7 bellard
        ti = sn->date_sec;
2817 3b9f94e1 bellard
#ifdef _WIN32
2818 3b9f94e1 bellard
        ptm = localtime(&ti);
2819 3b9f94e1 bellard
        strftime(date_buf, sizeof(date_buf),
2820 3b9f94e1 bellard
                 "%Y-%m-%d %H:%M:%S", ptm);
2821 3b9f94e1 bellard
#else
2822 faea38e7 bellard
        localtime_r(&ti, &tm);
2823 faea38e7 bellard
        strftime(date_buf, sizeof(date_buf),
2824 faea38e7 bellard
                 "%Y-%m-%d %H:%M:%S", &tm);
2825 3b9f94e1 bellard
#endif
2826 faea38e7 bellard
        secs = sn->vm_clock_nsec / 1000000000;
2827 faea38e7 bellard
        snprintf(clock_buf, sizeof(clock_buf),
2828 faea38e7 bellard
                 "%02d:%02d:%02d.%03d",
2829 faea38e7 bellard
                 (int)(secs / 3600),
2830 faea38e7 bellard
                 (int)((secs / 60) % 60),
2831 5fafdf24 ths
                 (int)(secs % 60),
2832 faea38e7 bellard
                 (int)((sn->vm_clock_nsec / 1000000) % 1000));
2833 faea38e7 bellard
        snprintf(buf, buf_size,
2834 5fafdf24 ths
                 "%-10s%-20s%7s%20s%15s",
2835 faea38e7 bellard
                 sn->id_str, sn->name,
2836 faea38e7 bellard
                 get_human_readable_size(buf1, sizeof(buf1), sn->vm_state_size),
2837 faea38e7 bellard
                 date_buf,
2838 faea38e7 bellard
                 clock_buf);
2839 faea38e7 bellard
    }
2840 faea38e7 bellard
    return buf;
2841 faea38e7 bellard
}
2842 faea38e7 bellard
2843 ea2384d3 bellard
/**************************************************************/
2844 83f64091 bellard
/* async I/Os */
2845 ea2384d3 bellard
2846 3b69e4b9 aliguori
BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
2847 f141eafe aliguori
                                 QEMUIOVector *qiov, int nb_sectors,
2848 3b69e4b9 aliguori
                                 BlockDriverCompletionFunc *cb, void *opaque)
2849 3b69e4b9 aliguori
{
2850 bbf0a440 Stefan Hajnoczi
    trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
2851 bbf0a440 Stefan Hajnoczi
2852 b2a61371 Stefan Hajnoczi
    return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
2853 8c5873d6 Stefan Hajnoczi
                                 cb, opaque, false);
2854 ea2384d3 bellard
}
2855 ea2384d3 bellard
2856 f141eafe aliguori
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
2857 f141eafe aliguori
                                  QEMUIOVector *qiov, int nb_sectors,
2858 f141eafe aliguori
                                  BlockDriverCompletionFunc *cb, void *opaque)
2859 ea2384d3 bellard
{
2860 bbf0a440 Stefan Hajnoczi
    trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
2861 bbf0a440 Stefan Hajnoczi
2862 1a6e115b Stefan Hajnoczi
    return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
2863 8c5873d6 Stefan Hajnoczi
                                 cb, opaque, true);
2864 83f64091 bellard
}
2865 83f64091 bellard
2866 40b4f539 Kevin Wolf
2867 40b4f539 Kevin Wolf
typedef struct MultiwriteCB {
2868 40b4f539 Kevin Wolf
    int error;
2869 40b4f539 Kevin Wolf
    int num_requests;
2870 40b4f539 Kevin Wolf
    int num_callbacks;
2871 40b4f539 Kevin Wolf
    struct {
2872 40b4f539 Kevin Wolf
        BlockDriverCompletionFunc *cb;
2873 40b4f539 Kevin Wolf
        void *opaque;
2874 40b4f539 Kevin Wolf
        QEMUIOVector *free_qiov;
2875 40b4f539 Kevin Wolf
    } callbacks[];
2876 40b4f539 Kevin Wolf
} MultiwriteCB;
2877 40b4f539 Kevin Wolf
2878 40b4f539 Kevin Wolf
static void multiwrite_user_cb(MultiwriteCB *mcb)
2879 40b4f539 Kevin Wolf
{
2880 40b4f539 Kevin Wolf
    int i;
2881 40b4f539 Kevin Wolf
2882 40b4f539 Kevin Wolf
    for (i = 0; i < mcb->num_callbacks; i++) {
2883 40b4f539 Kevin Wolf
        mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
2884 1e1ea48d Stefan Hajnoczi
        if (mcb->callbacks[i].free_qiov) {
2885 1e1ea48d Stefan Hajnoczi
            qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
2886 1e1ea48d Stefan Hajnoczi
        }
2887 7267c094 Anthony Liguori
        g_free(mcb->callbacks[i].free_qiov);
2888 40b4f539 Kevin Wolf
    }
2889 40b4f539 Kevin Wolf
}
2890 40b4f539 Kevin Wolf
2891 40b4f539 Kevin Wolf
static void multiwrite_cb(void *opaque, int ret)
2892 40b4f539 Kevin Wolf
{
2893 40b4f539 Kevin Wolf
    MultiwriteCB *mcb = opaque;
2894 40b4f539 Kevin Wolf
2895 6d519a5f Stefan Hajnoczi
    trace_multiwrite_cb(mcb, ret);
2896 6d519a5f Stefan Hajnoczi
2897 cb6d3ca0 Kevin Wolf
    if (ret < 0 && !mcb->error) {
2898 40b4f539 Kevin Wolf
        mcb->error = ret;
2899 40b4f539 Kevin Wolf
    }
2900 40b4f539 Kevin Wolf
2901 40b4f539 Kevin Wolf
    mcb->num_requests--;
2902 40b4f539 Kevin Wolf
    if (mcb->num_requests == 0) {
2903 de189a1b Kevin Wolf
        multiwrite_user_cb(mcb);
2904 7267c094 Anthony Liguori
        g_free(mcb);
2905 40b4f539 Kevin Wolf
    }
2906 40b4f539 Kevin Wolf
}
2907 40b4f539 Kevin Wolf
2908 40b4f539 Kevin Wolf
static int multiwrite_req_compare(const void *a, const void *b)
2909 40b4f539 Kevin Wolf
{
2910 77be4366 Christoph Hellwig
    const BlockRequest *req1 = a, *req2 = b;
2911 77be4366 Christoph Hellwig
2912 77be4366 Christoph Hellwig
    /*
2913 77be4366 Christoph Hellwig
     * Note that we can't simply subtract req2->sector from req1->sector
2914 77be4366 Christoph Hellwig
     * here as that could overflow the return value.
2915 77be4366 Christoph Hellwig
     */
2916 77be4366 Christoph Hellwig
    if (req1->sector > req2->sector) {
2917 77be4366 Christoph Hellwig
        return 1;
2918 77be4366 Christoph Hellwig
    } else if (req1->sector < req2->sector) {
2919 77be4366 Christoph Hellwig
        return -1;
2920 77be4366 Christoph Hellwig
    } else {
2921 77be4366 Christoph Hellwig
        return 0;
2922 77be4366 Christoph Hellwig
    }
2923 40b4f539 Kevin Wolf
}
2924 40b4f539 Kevin Wolf
2925 40b4f539 Kevin Wolf
/*
2926 40b4f539 Kevin Wolf
 * Takes a bunch of requests and tries to merge them. Returns the number of
2927 40b4f539 Kevin Wolf
 * requests that remain after merging.
2928 40b4f539 Kevin Wolf
 */
2929 40b4f539 Kevin Wolf
static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
2930 40b4f539 Kevin Wolf
    int num_reqs, MultiwriteCB *mcb)
2931 40b4f539 Kevin Wolf
{
2932 40b4f539 Kevin Wolf
    int i, outidx;
2933 40b4f539 Kevin Wolf
2934 40b4f539 Kevin Wolf
    // Sort requests by start sector
2935 40b4f539 Kevin Wolf
    qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
2936 40b4f539 Kevin Wolf
2937 40b4f539 Kevin Wolf
    // Check if adjacent requests touch the same clusters. If so, combine them,
2938 40b4f539 Kevin Wolf
    // filling up gaps with zero sectors.
2939 40b4f539 Kevin Wolf
    outidx = 0;
2940 40b4f539 Kevin Wolf
    for (i = 1; i < num_reqs; i++) {
2941 40b4f539 Kevin Wolf
        int merge = 0;
2942 40b4f539 Kevin Wolf
        int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
2943 40b4f539 Kevin Wolf
2944 b6a127a1 Paolo Bonzini
        // Handle exactly sequential writes and overlapping writes.
2945 40b4f539 Kevin Wolf
        if (reqs[i].sector <= oldreq_last) {
2946 40b4f539 Kevin Wolf
            merge = 1;
2947 40b4f539 Kevin Wolf
        }
2948 40b4f539 Kevin Wolf
2949 e2a305fb Christoph Hellwig
        if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
2950 e2a305fb Christoph Hellwig
            merge = 0;
2951 e2a305fb Christoph Hellwig
        }
2952 e2a305fb Christoph Hellwig
2953 40b4f539 Kevin Wolf
        if (merge) {
2954 40b4f539 Kevin Wolf
            size_t size;
2955 7267c094 Anthony Liguori
            QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
2956 40b4f539 Kevin Wolf
            qemu_iovec_init(qiov,
2957 40b4f539 Kevin Wolf
                reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
2958 40b4f539 Kevin Wolf
2959 40b4f539 Kevin Wolf
            // Add the first request to the merged one. If the requests are
2960 40b4f539 Kevin Wolf
            // overlapping, drop the last sectors of the first request.
2961 40b4f539 Kevin Wolf
            size = (reqs[i].sector - reqs[outidx].sector) << 9;
2962 40b4f539 Kevin Wolf
            qemu_iovec_concat(qiov, reqs[outidx].qiov, size);
2963 40b4f539 Kevin Wolf
2964 b6a127a1 Paolo Bonzini
            // We should need to add any zeros between the two requests
2965 b6a127a1 Paolo Bonzini
            assert (reqs[i].sector <= oldreq_last);
2966 40b4f539 Kevin Wolf
2967 40b4f539 Kevin Wolf
            // Add the second request
2968 40b4f539 Kevin Wolf
            qemu_iovec_concat(qiov, reqs[i].qiov, reqs[i].qiov->size);
2969 40b4f539 Kevin Wolf
2970 cbf1dff2 Kevin Wolf
            reqs[outidx].nb_sectors = qiov->size >> 9;
2971 40b4f539 Kevin Wolf
            reqs[outidx].qiov = qiov;
2972 40b4f539 Kevin Wolf
2973 40b4f539 Kevin Wolf
            mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
2974 40b4f539 Kevin Wolf
        } else {
2975 40b4f539 Kevin Wolf
            outidx++;
2976 40b4f539 Kevin Wolf
            reqs[outidx].sector     = reqs[i].sector;
2977 40b4f539 Kevin Wolf
            reqs[outidx].nb_sectors = reqs[i].nb_sectors;
2978 40b4f539 Kevin Wolf
            reqs[outidx].qiov       = reqs[i].qiov;
2979 40b4f539 Kevin Wolf
        }
2980 40b4f539 Kevin Wolf
    }
2981 40b4f539 Kevin Wolf
2982 40b4f539 Kevin Wolf
    return outidx + 1;
2983 40b4f539 Kevin Wolf
}
2984 40b4f539 Kevin Wolf
2985 40b4f539 Kevin Wolf
/*
2986 40b4f539 Kevin Wolf
 * Submit multiple AIO write requests at once.
2987 40b4f539 Kevin Wolf
 *
2988 40b4f539 Kevin Wolf
 * On success, the function returns 0 and all requests in the reqs array have
2989 40b4f539 Kevin Wolf
 * been submitted. In error case this function returns -1, and any of the
2990 40b4f539 Kevin Wolf
 * requests may or may not be submitted yet. In particular, this means that the
2991 40b4f539 Kevin Wolf
 * callback will be called for some of the requests, for others it won't. The
2992 40b4f539 Kevin Wolf
 * caller must check the error field of the BlockRequest to wait for the right
2993 40b4f539 Kevin Wolf
 * callbacks (if error != 0, no callback will be called).
2994 40b4f539 Kevin Wolf
 *
2995 40b4f539 Kevin Wolf
 * The implementation may modify the contents of the reqs array, e.g. to merge
2996 40b4f539 Kevin Wolf
 * requests. However, the fields opaque and error are left unmodified as they
2997 40b4f539 Kevin Wolf
 * are used to signal failure for a single request to the caller.
2998 40b4f539 Kevin Wolf
 */
2999 40b4f539 Kevin Wolf
int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
3000 40b4f539 Kevin Wolf
{
3001 40b4f539 Kevin Wolf
    MultiwriteCB *mcb;
3002 40b4f539 Kevin Wolf
    int i;
3003 40b4f539 Kevin Wolf
3004 301db7c2 Ryan Harper
    /* don't submit writes if we don't have a medium */
3005 301db7c2 Ryan Harper
    if (bs->drv == NULL) {
3006 301db7c2 Ryan Harper
        for (i = 0; i < num_reqs; i++) {
3007 301db7c2 Ryan Harper
            reqs[i].error = -ENOMEDIUM;
3008 301db7c2 Ryan Harper
        }
3009 301db7c2 Ryan Harper
        return -1;
3010 301db7c2 Ryan Harper
    }
3011 301db7c2 Ryan Harper
3012 40b4f539 Kevin Wolf
    if (num_reqs == 0) {
3013 40b4f539 Kevin Wolf
        return 0;
3014 40b4f539 Kevin Wolf
    }
3015 40b4f539 Kevin Wolf
3016 40b4f539 Kevin Wolf
    // Create MultiwriteCB structure
3017 7267c094 Anthony Liguori
    mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
3018 40b4f539 Kevin Wolf
    mcb->num_requests = 0;
3019 40b4f539 Kevin Wolf
    mcb->num_callbacks = num_reqs;
3020 40b4f539 Kevin Wolf
3021 40b4f539 Kevin Wolf
    for (i = 0; i < num_reqs; i++) {
3022 40b4f539 Kevin Wolf
        mcb->callbacks[i].cb = reqs[i].cb;
3023 40b4f539 Kevin Wolf
        mcb->callbacks[i].opaque = reqs[i].opaque;
3024 40b4f539 Kevin Wolf
    }
3025 40b4f539 Kevin Wolf
3026 40b4f539 Kevin Wolf
    // Check for mergable requests
3027 40b4f539 Kevin Wolf
    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
3028 40b4f539 Kevin Wolf
3029 6d519a5f Stefan Hajnoczi
    trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
3030 6d519a5f Stefan Hajnoczi
3031 df9309fb Paolo Bonzini
    /* Run the aio requests. */
3032 df9309fb Paolo Bonzini
    mcb->num_requests = num_reqs;
3033 40b4f539 Kevin Wolf
    for (i = 0; i < num_reqs; i++) {
3034 ad54ae80 Paolo Bonzini
        bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
3035 40b4f539 Kevin Wolf
            reqs[i].nb_sectors, multiwrite_cb, mcb);
3036 40b4f539 Kevin Wolf
    }
3037 40b4f539 Kevin Wolf
3038 40b4f539 Kevin Wolf
    return 0;
3039 40b4f539 Kevin Wolf
}
3040 40b4f539 Kevin Wolf
3041 83f64091 bellard
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
3042 83f64091 bellard
{
3043 6bbff9a0 aliguori
    acb->pool->cancel(acb);
3044 83f64091 bellard
}
3045 83f64091 bellard
3046 98f90dba Zhi Yong Wu
/* block I/O throttling */
3047 98f90dba Zhi Yong Wu
static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors,
3048 98f90dba Zhi Yong Wu
                 bool is_write, double elapsed_time, uint64_t *wait)
3049 98f90dba Zhi Yong Wu
{
3050 98f90dba Zhi Yong Wu
    uint64_t bps_limit = 0;
3051 98f90dba Zhi Yong Wu
    double   bytes_limit, bytes_base, bytes_res;
3052 98f90dba Zhi Yong Wu
    double   slice_time, wait_time;
3053 98f90dba Zhi Yong Wu
3054 98f90dba Zhi Yong Wu
    if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
3055 98f90dba Zhi Yong Wu
        bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
3056 98f90dba Zhi Yong Wu
    } else if (bs->io_limits.bps[is_write]) {
3057 98f90dba Zhi Yong Wu
        bps_limit = bs->io_limits.bps[is_write];
3058 98f90dba Zhi Yong Wu
    } else {
3059 98f90dba Zhi Yong Wu
        if (wait) {
3060 98f90dba Zhi Yong Wu
            *wait = 0;
3061 98f90dba Zhi Yong Wu
        }
3062 98f90dba Zhi Yong Wu
3063 98f90dba Zhi Yong Wu
        return false;
3064 98f90dba Zhi Yong Wu
    }
3065 98f90dba Zhi Yong Wu
3066 98f90dba Zhi Yong Wu
    slice_time = bs->slice_end - bs->slice_start;
3067 98f90dba Zhi Yong Wu
    slice_time /= (NANOSECONDS_PER_SECOND);
3068 98f90dba Zhi Yong Wu
    bytes_limit = bps_limit * slice_time;
3069 98f90dba Zhi Yong Wu
    bytes_base  = bs->nr_bytes[is_write] - bs->io_base.bytes[is_write];
3070 98f90dba Zhi Yong Wu
    if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
3071 98f90dba Zhi Yong Wu
        bytes_base += bs->nr_bytes[!is_write] - bs->io_base.bytes[!is_write];
3072 98f90dba Zhi Yong Wu
    }
3073 98f90dba Zhi Yong Wu
3074 98f90dba Zhi Yong Wu
    /* bytes_base: the bytes of data which have been read/written; and
3075 98f90dba Zhi Yong Wu
     *             it is obtained from the history statistic info.
3076 98f90dba Zhi Yong Wu
     * bytes_res: the remaining bytes of data which need to be read/written.
3077 98f90dba Zhi Yong Wu
     * (bytes_base + bytes_res) / bps_limit: used to calcuate
3078 98f90dba Zhi Yong Wu
     *             the total time for completing reading/writting all data.
3079 98f90dba Zhi Yong Wu
     */
3080 98f90dba Zhi Yong Wu
    bytes_res   = (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
3081 98f90dba Zhi Yong Wu
3082 98f90dba Zhi Yong Wu
    if (bytes_base + bytes_res <= bytes_limit) {
3083 98f90dba Zhi Yong Wu
        if (wait) {
3084 98f90dba Zhi Yong Wu
            *wait = 0;
3085 98f90dba Zhi Yong Wu
        }
3086 98f90dba Zhi Yong Wu
3087 98f90dba Zhi Yong Wu
        return false;
3088 98f90dba Zhi Yong Wu
    }
3089 98f90dba Zhi Yong Wu
3090 98f90dba Zhi Yong Wu
    /* Calc approx time to dispatch */
3091 98f90dba Zhi Yong Wu
    wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time;
3092 98f90dba Zhi Yong Wu
3093 98f90dba Zhi Yong Wu
    /* When the I/O rate at runtime exceeds the limits,
3094 98f90dba Zhi Yong Wu
     * bs->slice_end need to be extended in order that the current statistic
3095 98f90dba Zhi Yong Wu
     * info can be kept until the timer fire, so it is increased and tuned
3096 98f90dba Zhi Yong Wu
     * based on the result of experiment.
3097 98f90dba Zhi Yong Wu
     */
3098 98f90dba Zhi Yong Wu
    bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10;
3099 98f90dba Zhi Yong Wu
    bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME;
3100 98f90dba Zhi Yong Wu
    if (wait) {
3101 98f90dba Zhi Yong Wu
        *wait = wait_time * BLOCK_IO_SLICE_TIME * 10;
3102 98f90dba Zhi Yong Wu
    }
3103 98f90dba Zhi Yong Wu
3104 98f90dba Zhi Yong Wu
    return true;
3105 98f90dba Zhi Yong Wu
}
3106 98f90dba Zhi Yong Wu
3107 98f90dba Zhi Yong Wu
static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write,
3108 98f90dba Zhi Yong Wu
                             double elapsed_time, uint64_t *wait)
3109 98f90dba Zhi Yong Wu
{
3110 98f90dba Zhi Yong Wu
    uint64_t iops_limit = 0;
3111 98f90dba Zhi Yong Wu
    double   ios_limit, ios_base;
3112 98f90dba Zhi Yong Wu
    double   slice_time, wait_time;
3113 98f90dba Zhi Yong Wu
3114 98f90dba Zhi Yong Wu
    if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
3115 98f90dba Zhi Yong Wu
        iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
3116 98f90dba Zhi Yong Wu
    } else if (bs->io_limits.iops[is_write]) {
3117 98f90dba Zhi Yong Wu
        iops_limit = bs->io_limits.iops[is_write];
3118 98f90dba Zhi Yong Wu
    } else {
3119 98f90dba Zhi Yong Wu
        if (wait) {
3120 98f90dba Zhi Yong Wu
            *wait = 0;
3121 98f90dba Zhi Yong Wu
        }
3122 98f90dba Zhi Yong Wu
3123 98f90dba Zhi Yong Wu
        return false;
3124 98f90dba Zhi Yong Wu
    }
3125 98f90dba Zhi Yong Wu
3126 98f90dba Zhi Yong Wu
    slice_time = bs->slice_end - bs->slice_start;
3127 98f90dba Zhi Yong Wu
    slice_time /= (NANOSECONDS_PER_SECOND);
3128 98f90dba Zhi Yong Wu
    ios_limit  = iops_limit * slice_time;
3129 98f90dba Zhi Yong Wu
    ios_base   = bs->nr_ops[is_write] - bs->io_base.ios[is_write];
3130 98f90dba Zhi Yong Wu
    if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
3131 98f90dba Zhi Yong Wu
        ios_base += bs->nr_ops[!is_write] - bs->io_base.ios[!is_write];
3132 98f90dba Zhi Yong Wu
    }
3133 98f90dba Zhi Yong Wu
3134 98f90dba Zhi Yong Wu
    if (ios_base + 1 <= ios_limit) {
3135 98f90dba Zhi Yong Wu
        if (wait) {
3136 98f90dba Zhi Yong Wu
            *wait = 0;
3137 98f90dba Zhi Yong Wu
        }
3138 98f90dba Zhi Yong Wu
3139 98f90dba Zhi Yong Wu
        return false;
3140 98f90dba Zhi Yong Wu
    }
3141 98f90dba Zhi Yong Wu
3142 98f90dba Zhi Yong Wu
    /* Calc approx time to dispatch */
3143 98f90dba Zhi Yong Wu
    wait_time = (ios_base + 1) / iops_limit;
3144 98f90dba Zhi Yong Wu
    if (wait_time > elapsed_time) {
3145 98f90dba Zhi Yong Wu
        wait_time = wait_time - elapsed_time;
3146 98f90dba Zhi Yong Wu
    } else {
3147 98f90dba Zhi Yong Wu
        wait_time = 0;
3148 98f90dba Zhi Yong Wu
    }
3149 98f90dba Zhi Yong Wu
3150 98f90dba Zhi Yong Wu
    bs->slice_time = wait_time * BLOCK_IO_SLICE_TIME * 10;
3151 98f90dba Zhi Yong Wu
    bs->slice_end += bs->slice_time - 3 * BLOCK_IO_SLICE_TIME;
3152 98f90dba Zhi Yong Wu
    if (wait) {
3153 98f90dba Zhi Yong Wu
        *wait = wait_time * BLOCK_IO_SLICE_TIME * 10;
3154 98f90dba Zhi Yong Wu
    }
3155 98f90dba Zhi Yong Wu
3156 98f90dba Zhi Yong Wu
    return true;
3157 98f90dba Zhi Yong Wu
}
3158 98f90dba Zhi Yong Wu
3159 98f90dba Zhi Yong Wu
static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors,
3160 98f90dba Zhi Yong Wu
                           bool is_write, int64_t *wait)
3161 98f90dba Zhi Yong Wu
{
3162 98f90dba Zhi Yong Wu
    int64_t  now, max_wait;
3163 98f90dba Zhi Yong Wu
    uint64_t bps_wait = 0, iops_wait = 0;
3164 98f90dba Zhi Yong Wu
    double   elapsed_time;
3165 98f90dba Zhi Yong Wu
    int      bps_ret, iops_ret;
3166 98f90dba Zhi Yong Wu
3167 98f90dba Zhi Yong Wu
    now = qemu_get_clock_ns(vm_clock);
3168 98f90dba Zhi Yong Wu
    if ((bs->slice_start < now)
3169 98f90dba Zhi Yong Wu
        && (bs->slice_end > now)) {
3170 98f90dba Zhi Yong Wu
        bs->slice_end = now + bs->slice_time;
3171 98f90dba Zhi Yong Wu
    } else {
3172 98f90dba Zhi Yong Wu
        bs->slice_time  =  5 * BLOCK_IO_SLICE_TIME;
3173 98f90dba Zhi Yong Wu
        bs->slice_start = now;
3174 98f90dba Zhi Yong Wu
        bs->slice_end   = now + bs->slice_time;
3175 98f90dba Zhi Yong Wu
3176 98f90dba Zhi Yong Wu
        bs->io_base.bytes[is_write]  = bs->nr_bytes[is_write];
3177 98f90dba Zhi Yong Wu
        bs->io_base.bytes[!is_write] = bs->nr_bytes[!is_write];
3178 98f90dba Zhi Yong Wu
3179 98f90dba Zhi Yong Wu
        bs->io_base.ios[is_write]    = bs->nr_ops[is_write];
3180 98f90dba Zhi Yong Wu
        bs->io_base.ios[!is_write]   = bs->nr_ops[!is_write];
3181 98f90dba Zhi Yong Wu
    }
3182 98f90dba Zhi Yong Wu
3183 98f90dba Zhi Yong Wu
    elapsed_time  = now - bs->slice_start;
3184 98f90dba Zhi Yong Wu
    elapsed_time  /= (NANOSECONDS_PER_SECOND);
3185 98f90dba Zhi Yong Wu
3186 98f90dba Zhi Yong Wu
    bps_ret  = bdrv_exceed_bps_limits(bs, nb_sectors,
3187 98f90dba Zhi Yong Wu
                                      is_write, elapsed_time, &bps_wait);
3188 98f90dba Zhi Yong Wu
    iops_ret = bdrv_exceed_iops_limits(bs, is_write,
3189 98f90dba Zhi Yong Wu
                                      elapsed_time, &iops_wait);
3190 98f90dba Zhi Yong Wu
    if (bps_ret || iops_ret) {
3191 98f90dba Zhi Yong Wu
        max_wait = bps_wait > iops_wait ? bps_wait : iops_wait;
3192 98f90dba Zhi Yong Wu
        if (wait) {
3193 98f90dba Zhi Yong Wu
            *wait = max_wait;
3194 98f90dba Zhi Yong Wu
        }
3195 98f90dba Zhi Yong Wu
3196 98f90dba Zhi Yong Wu
        now = qemu_get_clock_ns(vm_clock);
3197 98f90dba Zhi Yong Wu
        if (bs->slice_end < now + max_wait) {
3198 98f90dba Zhi Yong Wu
            bs->slice_end = now + max_wait;
3199 98f90dba Zhi Yong Wu
        }
3200 98f90dba Zhi Yong Wu
3201 98f90dba Zhi Yong Wu
        return true;
3202 98f90dba Zhi Yong Wu
    }
3203 98f90dba Zhi Yong Wu
3204 98f90dba Zhi Yong Wu
    if (wait) {
3205 98f90dba Zhi Yong Wu
        *wait = 0;
3206 98f90dba Zhi Yong Wu
    }
3207 98f90dba Zhi Yong Wu
3208 98f90dba Zhi Yong Wu
    return false;
3209 98f90dba Zhi Yong Wu
}
3210 ce1a14dc pbrook
3211 83f64091 bellard
/**************************************************************/
3212 83f64091 bellard
/* async block device emulation */
3213 83f64091 bellard
3214 c16b5a2c Christoph Hellwig
typedef struct BlockDriverAIOCBSync {
3215 c16b5a2c Christoph Hellwig
    BlockDriverAIOCB common;
3216 c16b5a2c Christoph Hellwig
    QEMUBH *bh;
3217 c16b5a2c Christoph Hellwig
    int ret;
3218 c16b5a2c Christoph Hellwig
    /* vector translation state */
3219 c16b5a2c Christoph Hellwig
    QEMUIOVector *qiov;
3220 c16b5a2c Christoph Hellwig
    uint8_t *bounce;
3221 c16b5a2c Christoph Hellwig
    int is_write;
3222 c16b5a2c Christoph Hellwig
} BlockDriverAIOCBSync;
3223 c16b5a2c Christoph Hellwig
3224 c16b5a2c Christoph Hellwig
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
3225 c16b5a2c Christoph Hellwig
{
3226 b666d239 Kevin Wolf
    BlockDriverAIOCBSync *acb =
3227 b666d239 Kevin Wolf
        container_of(blockacb, BlockDriverAIOCBSync, common);
3228 6a7ad299 Dor Laor
    qemu_bh_delete(acb->bh);
3229 36afc451 Avi Kivity
    acb->bh = NULL;
3230 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
3231 c16b5a2c Christoph Hellwig
}
3232 c16b5a2c Christoph Hellwig
3233 c16b5a2c Christoph Hellwig
static AIOPool bdrv_em_aio_pool = {
3234 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(BlockDriverAIOCBSync),
3235 c16b5a2c Christoph Hellwig
    .cancel             = bdrv_aio_cancel_em,
3236 c16b5a2c Christoph Hellwig
};
3237 c16b5a2c Christoph Hellwig
3238 ce1a14dc pbrook
static void bdrv_aio_bh_cb(void *opaque)
3239 83f64091 bellard
{
3240 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb = opaque;
3241 f141eafe aliguori
3242 f141eafe aliguori
    if (!acb->is_write)
3243 f141eafe aliguori
        qemu_iovec_from_buffer(acb->qiov, acb->bounce, acb->qiov->size);
3244 ceb42de8 aliguori
    qemu_vfree(acb->bounce);
3245 ce1a14dc pbrook
    acb->common.cb(acb->common.opaque, acb->ret);
3246 6a7ad299 Dor Laor
    qemu_bh_delete(acb->bh);
3247 36afc451 Avi Kivity
    acb->bh = NULL;
3248 ce1a14dc pbrook
    qemu_aio_release(acb);
3249 83f64091 bellard
}
3250 beac80cd bellard
3251 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
3252 f141eafe aliguori
                                            int64_t sector_num,
3253 f141eafe aliguori
                                            QEMUIOVector *qiov,
3254 f141eafe aliguori
                                            int nb_sectors,
3255 f141eafe aliguori
                                            BlockDriverCompletionFunc *cb,
3256 f141eafe aliguori
                                            void *opaque,
3257 f141eafe aliguori
                                            int is_write)
3258 f141eafe aliguori
3259 83f64091 bellard
{
3260 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb;
3261 ce1a14dc pbrook
3262 c16b5a2c Christoph Hellwig
    acb = qemu_aio_get(&bdrv_em_aio_pool, bs, cb, opaque);
3263 f141eafe aliguori
    acb->is_write = is_write;
3264 f141eafe aliguori
    acb->qiov = qiov;
3265 e268ca52 aliguori
    acb->bounce = qemu_blockalign(bs, qiov->size);
3266 3f3aace8 Paolo Bonzini
    acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
3267 f141eafe aliguori
3268 f141eafe aliguori
    if (is_write) {
3269 f141eafe aliguori
        qemu_iovec_to_buffer(acb->qiov, acb->bounce);
3270 1ed20acf Stefan Hajnoczi
        acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
3271 f141eafe aliguori
    } else {
3272 1ed20acf Stefan Hajnoczi
        acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
3273 f141eafe aliguori
    }
3274 f141eafe aliguori
3275 ce1a14dc pbrook
    qemu_bh_schedule(acb->bh);
3276 f141eafe aliguori
3277 ce1a14dc pbrook
    return &acb->common;
3278 beac80cd bellard
}
3279 beac80cd bellard
3280 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
3281 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
3282 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
3283 beac80cd bellard
{
3284 f141eafe aliguori
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
3285 f141eafe aliguori
}
3286 83f64091 bellard
3287 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
3288 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
3289 f141eafe aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
3290 f141eafe aliguori
{
3291 f141eafe aliguori
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
3292 beac80cd bellard
}
3293 beac80cd bellard
3294 68485420 Kevin Wolf
3295 68485420 Kevin Wolf
typedef struct BlockDriverAIOCBCoroutine {
3296 68485420 Kevin Wolf
    BlockDriverAIOCB common;
3297 68485420 Kevin Wolf
    BlockRequest req;
3298 68485420 Kevin Wolf
    bool is_write;
3299 68485420 Kevin Wolf
    QEMUBH* bh;
3300 68485420 Kevin Wolf
} BlockDriverAIOCBCoroutine;
3301 68485420 Kevin Wolf
3302 68485420 Kevin Wolf
static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
3303 68485420 Kevin Wolf
{
3304 68485420 Kevin Wolf
    qemu_aio_flush();
3305 68485420 Kevin Wolf
}
3306 68485420 Kevin Wolf
3307 68485420 Kevin Wolf
static AIOPool bdrv_em_co_aio_pool = {
3308 68485420 Kevin Wolf
    .aiocb_size         = sizeof(BlockDriverAIOCBCoroutine),
3309 68485420 Kevin Wolf
    .cancel             = bdrv_aio_co_cancel_em,
3310 68485420 Kevin Wolf
};
3311 68485420 Kevin Wolf
3312 35246a68 Paolo Bonzini
static void bdrv_co_em_bh(void *opaque)
3313 68485420 Kevin Wolf
{
3314 68485420 Kevin Wolf
    BlockDriverAIOCBCoroutine *acb = opaque;
3315 68485420 Kevin Wolf
3316 68485420 Kevin Wolf
    acb->common.cb(acb->common.opaque, acb->req.error);
3317 68485420 Kevin Wolf
    qemu_bh_delete(acb->bh);
3318 68485420 Kevin Wolf
    qemu_aio_release(acb);
3319 68485420 Kevin Wolf
}
3320 68485420 Kevin Wolf
3321 b2a61371 Stefan Hajnoczi
/* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
3322 b2a61371 Stefan Hajnoczi
static void coroutine_fn bdrv_co_do_rw(void *opaque)
3323 b2a61371 Stefan Hajnoczi
{
3324 b2a61371 Stefan Hajnoczi
    BlockDriverAIOCBCoroutine *acb = opaque;
3325 b2a61371 Stefan Hajnoczi
    BlockDriverState *bs = acb->common.bs;
3326 b2a61371 Stefan Hajnoczi
3327 b2a61371 Stefan Hajnoczi
    if (!acb->is_write) {
3328 b2a61371 Stefan Hajnoczi
        acb->req.error = bdrv_co_do_readv(bs, acb->req.sector,
3329 470c0504 Stefan Hajnoczi
            acb->req.nb_sectors, acb->req.qiov, 0);
3330 b2a61371 Stefan Hajnoczi
    } else {
3331 b2a61371 Stefan Hajnoczi
        acb->req.error = bdrv_co_do_writev(bs, acb->req.sector,
3332 f08f2dda Stefan Hajnoczi
            acb->req.nb_sectors, acb->req.qiov, 0);
3333 b2a61371 Stefan Hajnoczi
    }
3334 b2a61371 Stefan Hajnoczi
3335 35246a68 Paolo Bonzini
    acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
3336 b2a61371 Stefan Hajnoczi
    qemu_bh_schedule(acb->bh);
3337 b2a61371 Stefan Hajnoczi
}
3338 b2a61371 Stefan Hajnoczi
3339 68485420 Kevin Wolf
static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
3340 68485420 Kevin Wolf
                                               int64_t sector_num,
3341 68485420 Kevin Wolf
                                               QEMUIOVector *qiov,
3342 68485420 Kevin Wolf
                                               int nb_sectors,
3343 68485420 Kevin Wolf
                                               BlockDriverCompletionFunc *cb,
3344 68485420 Kevin Wolf
                                               void *opaque,
3345 8c5873d6 Stefan Hajnoczi
                                               bool is_write)
3346 68485420 Kevin Wolf
{
3347 68485420 Kevin Wolf
    Coroutine *co;
3348 68485420 Kevin Wolf
    BlockDriverAIOCBCoroutine *acb;
3349 68485420 Kevin Wolf
3350 68485420 Kevin Wolf
    acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
3351 68485420 Kevin Wolf
    acb->req.sector = sector_num;
3352 68485420 Kevin Wolf
    acb->req.nb_sectors = nb_sectors;
3353 68485420 Kevin Wolf
    acb->req.qiov = qiov;
3354 68485420 Kevin Wolf
    acb->is_write = is_write;
3355 68485420 Kevin Wolf
3356 8c5873d6 Stefan Hajnoczi
    co = qemu_coroutine_create(bdrv_co_do_rw);
3357 68485420 Kevin Wolf
    qemu_coroutine_enter(co, acb);
3358 68485420 Kevin Wolf
3359 68485420 Kevin Wolf
    return &acb->common;
3360 68485420 Kevin Wolf
}
3361 68485420 Kevin Wolf
3362 07f07615 Paolo Bonzini
static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
3363 b2e12bc6 Christoph Hellwig
{
3364 07f07615 Paolo Bonzini
    BlockDriverAIOCBCoroutine *acb = opaque;
3365 07f07615 Paolo Bonzini
    BlockDriverState *bs = acb->common.bs;
3366 b2e12bc6 Christoph Hellwig
3367 07f07615 Paolo Bonzini
    acb->req.error = bdrv_co_flush(bs);
3368 07f07615 Paolo Bonzini
    acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
3369 b2e12bc6 Christoph Hellwig
    qemu_bh_schedule(acb->bh);
3370 b2e12bc6 Christoph Hellwig
}
3371 b2e12bc6 Christoph Hellwig
3372 07f07615 Paolo Bonzini
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
3373 016f5cf6 Alexander Graf
        BlockDriverCompletionFunc *cb, void *opaque)
3374 016f5cf6 Alexander Graf
{
3375 07f07615 Paolo Bonzini
    trace_bdrv_aio_flush(bs, opaque);
3376 016f5cf6 Alexander Graf
3377 07f07615 Paolo Bonzini
    Coroutine *co;
3378 07f07615 Paolo Bonzini
    BlockDriverAIOCBCoroutine *acb;
3379 016f5cf6 Alexander Graf
3380 07f07615 Paolo Bonzini
    acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
3381 07f07615 Paolo Bonzini
    co = qemu_coroutine_create(bdrv_aio_flush_co_entry);
3382 07f07615 Paolo Bonzini
    qemu_coroutine_enter(co, acb);
3383 016f5cf6 Alexander Graf
3384 016f5cf6 Alexander Graf
    return &acb->common;
3385 016f5cf6 Alexander Graf
}
3386 016f5cf6 Alexander Graf
3387 4265d620 Paolo Bonzini
static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
3388 4265d620 Paolo Bonzini
{
3389 4265d620 Paolo Bonzini
    BlockDriverAIOCBCoroutine *acb = opaque;
3390 4265d620 Paolo Bonzini
    BlockDriverState *bs = acb->common.bs;
3391 4265d620 Paolo Bonzini
3392 4265d620 Paolo Bonzini
    acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
3393 4265d620 Paolo Bonzini
    acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
3394 4265d620 Paolo Bonzini
    qemu_bh_schedule(acb->bh);
3395 4265d620 Paolo Bonzini
}
3396 4265d620 Paolo Bonzini
3397 4265d620 Paolo Bonzini
BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
3398 4265d620 Paolo Bonzini
        int64_t sector_num, int nb_sectors,
3399 4265d620 Paolo Bonzini
        BlockDriverCompletionFunc *cb, void *opaque)
3400 4265d620 Paolo Bonzini
{
3401 4265d620 Paolo Bonzini
    Coroutine *co;
3402 4265d620 Paolo Bonzini
    BlockDriverAIOCBCoroutine *acb;
3403 4265d620 Paolo Bonzini
3404 4265d620 Paolo Bonzini
    trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
3405 4265d620 Paolo Bonzini
3406 4265d620 Paolo Bonzini
    acb = qemu_aio_get(&bdrv_em_co_aio_pool, bs, cb, opaque);
3407 4265d620 Paolo Bonzini
    acb->req.sector = sector_num;
3408 4265d620 Paolo Bonzini
    acb->req.nb_sectors = nb_sectors;
3409 4265d620 Paolo Bonzini
    co = qemu_coroutine_create(bdrv_aio_discard_co_entry);
3410 4265d620 Paolo Bonzini
    qemu_coroutine_enter(co, acb);
3411 4265d620 Paolo Bonzini
3412 4265d620 Paolo Bonzini
    return &acb->common;
3413 4265d620 Paolo Bonzini
}
3414 4265d620 Paolo Bonzini
3415 ea2384d3 bellard
void bdrv_init(void)
3416 ea2384d3 bellard
{
3417 5efa9d5a Anthony Liguori
    module_call_init(MODULE_INIT_BLOCK);
3418 ea2384d3 bellard
}
3419 ce1a14dc pbrook
3420 eb852011 Markus Armbruster
void bdrv_init_with_whitelist(void)
3421 eb852011 Markus Armbruster
{
3422 eb852011 Markus Armbruster
    use_bdrv_whitelist = 1;
3423 eb852011 Markus Armbruster
    bdrv_init();
3424 eb852011 Markus Armbruster
}
3425 eb852011 Markus Armbruster
3426 c16b5a2c Christoph Hellwig
void *qemu_aio_get(AIOPool *pool, BlockDriverState *bs,
3427 c16b5a2c Christoph Hellwig
                   BlockDriverCompletionFunc *cb, void *opaque)
3428 ce1a14dc pbrook
{
3429 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
3430 ce1a14dc pbrook
3431 6bbff9a0 aliguori
    if (pool->free_aiocb) {
3432 6bbff9a0 aliguori
        acb = pool->free_aiocb;
3433 6bbff9a0 aliguori
        pool->free_aiocb = acb->next;
3434 ce1a14dc pbrook
    } else {
3435 7267c094 Anthony Liguori
        acb = g_malloc0(pool->aiocb_size);
3436 6bbff9a0 aliguori
        acb->pool = pool;
3437 ce1a14dc pbrook
    }
3438 ce1a14dc pbrook
    acb->bs = bs;
3439 ce1a14dc pbrook
    acb->cb = cb;
3440 ce1a14dc pbrook
    acb->opaque = opaque;
3441 ce1a14dc pbrook
    return acb;
3442 ce1a14dc pbrook
}
3443 ce1a14dc pbrook
3444 ce1a14dc pbrook
void qemu_aio_release(void *p)
3445 ce1a14dc pbrook
{
3446 6bbff9a0 aliguori
    BlockDriverAIOCB *acb = (BlockDriverAIOCB *)p;
3447 6bbff9a0 aliguori
    AIOPool *pool = acb->pool;
3448 6bbff9a0 aliguori
    acb->next = pool->free_aiocb;
3449 6bbff9a0 aliguori
    pool->free_aiocb = acb;
3450 ce1a14dc pbrook
}
3451 19cb3738 bellard
3452 19cb3738 bellard
/**************************************************************/
3453 f9f05dc5 Kevin Wolf
/* Coroutine block device emulation */
3454 f9f05dc5 Kevin Wolf
3455 f9f05dc5 Kevin Wolf
typedef struct CoroutineIOCompletion {
3456 f9f05dc5 Kevin Wolf
    Coroutine *coroutine;
3457 f9f05dc5 Kevin Wolf
    int ret;
3458 f9f05dc5 Kevin Wolf
} CoroutineIOCompletion;
3459 f9f05dc5 Kevin Wolf
3460 f9f05dc5 Kevin Wolf
static void bdrv_co_io_em_complete(void *opaque, int ret)
3461 f9f05dc5 Kevin Wolf
{
3462 f9f05dc5 Kevin Wolf
    CoroutineIOCompletion *co = opaque;
3463 f9f05dc5 Kevin Wolf
3464 f9f05dc5 Kevin Wolf
    co->ret = ret;
3465 f9f05dc5 Kevin Wolf
    qemu_coroutine_enter(co->coroutine, NULL);
3466 f9f05dc5 Kevin Wolf
}
3467 f9f05dc5 Kevin Wolf
3468 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
3469 f9f05dc5 Kevin Wolf
                                      int nb_sectors, QEMUIOVector *iov,
3470 f9f05dc5 Kevin Wolf
                                      bool is_write)
3471 f9f05dc5 Kevin Wolf
{
3472 f9f05dc5 Kevin Wolf
    CoroutineIOCompletion co = {
3473 f9f05dc5 Kevin Wolf
        .coroutine = qemu_coroutine_self(),
3474 f9f05dc5 Kevin Wolf
    };
3475 f9f05dc5 Kevin Wolf
    BlockDriverAIOCB *acb;
3476 f9f05dc5 Kevin Wolf
3477 f9f05dc5 Kevin Wolf
    if (is_write) {
3478 a652d160 Stefan Hajnoczi
        acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
3479 a652d160 Stefan Hajnoczi
                                       bdrv_co_io_em_complete, &co);
3480 f9f05dc5 Kevin Wolf
    } else {
3481 a652d160 Stefan Hajnoczi
        acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
3482 a652d160 Stefan Hajnoczi
                                      bdrv_co_io_em_complete, &co);
3483 f9f05dc5 Kevin Wolf
    }
3484 f9f05dc5 Kevin Wolf
3485 59370aaa Stefan Hajnoczi
    trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
3486 f9f05dc5 Kevin Wolf
    if (!acb) {
3487 f9f05dc5 Kevin Wolf
        return -EIO;
3488 f9f05dc5 Kevin Wolf
    }
3489 f9f05dc5 Kevin Wolf
    qemu_coroutine_yield();
3490 f9f05dc5 Kevin Wolf
3491 f9f05dc5 Kevin Wolf
    return co.ret;
3492 f9f05dc5 Kevin Wolf
}
3493 f9f05dc5 Kevin Wolf
3494 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
3495 f9f05dc5 Kevin Wolf
                                         int64_t sector_num, int nb_sectors,
3496 f9f05dc5 Kevin Wolf
                                         QEMUIOVector *iov)
3497 f9f05dc5 Kevin Wolf
{
3498 f9f05dc5 Kevin Wolf
    return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
3499 f9f05dc5 Kevin Wolf
}
3500 f9f05dc5 Kevin Wolf
3501 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
3502 f9f05dc5 Kevin Wolf
                                         int64_t sector_num, int nb_sectors,
3503 f9f05dc5 Kevin Wolf
                                         QEMUIOVector *iov)
3504 f9f05dc5 Kevin Wolf
{
3505 f9f05dc5 Kevin Wolf
    return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
3506 f9f05dc5 Kevin Wolf
}
3507 f9f05dc5 Kevin Wolf
3508 07f07615 Paolo Bonzini
static void coroutine_fn bdrv_flush_co_entry(void *opaque)
3509 e7a8a783 Kevin Wolf
{
3510 07f07615 Paolo Bonzini
    RwCo *rwco = opaque;
3511 07f07615 Paolo Bonzini
3512 07f07615 Paolo Bonzini
    rwco->ret = bdrv_co_flush(rwco->bs);
3513 07f07615 Paolo Bonzini
}
3514 07f07615 Paolo Bonzini
3515 07f07615 Paolo Bonzini
int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
3516 07f07615 Paolo Bonzini
{
3517 eb489bb1 Kevin Wolf
    int ret;
3518 eb489bb1 Kevin Wolf
3519 ca716364 Kevin Wolf
    if (!bs->drv) {
3520 07f07615 Paolo Bonzini
        return 0;
3521 eb489bb1 Kevin Wolf
    }
3522 eb489bb1 Kevin Wolf
3523 ca716364 Kevin Wolf
    /* Write back cached data to the OS even with cache=unsafe */
3524 eb489bb1 Kevin Wolf
    if (bs->drv->bdrv_co_flush_to_os) {
3525 eb489bb1 Kevin Wolf
        ret = bs->drv->bdrv_co_flush_to_os(bs);
3526 eb489bb1 Kevin Wolf
        if (ret < 0) {
3527 eb489bb1 Kevin Wolf
            return ret;
3528 eb489bb1 Kevin Wolf
        }
3529 eb489bb1 Kevin Wolf
    }
3530 eb489bb1 Kevin Wolf
3531 ca716364 Kevin Wolf
    /* But don't actually force it to the disk with cache=unsafe */
3532 ca716364 Kevin Wolf
    if (bs->open_flags & BDRV_O_NO_FLUSH) {
3533 ca716364 Kevin Wolf
        return 0;
3534 ca716364 Kevin Wolf
    }
3535 ca716364 Kevin Wolf
3536 eb489bb1 Kevin Wolf
    if (bs->drv->bdrv_co_flush_to_disk) {
3537 c68b89ac Kevin Wolf
        return bs->drv->bdrv_co_flush_to_disk(bs);
3538 07f07615 Paolo Bonzini
    } else if (bs->drv->bdrv_aio_flush) {
3539 07f07615 Paolo Bonzini
        BlockDriverAIOCB *acb;
3540 07f07615 Paolo Bonzini
        CoroutineIOCompletion co = {
3541 07f07615 Paolo Bonzini
            .coroutine = qemu_coroutine_self(),
3542 07f07615 Paolo Bonzini
        };
3543 07f07615 Paolo Bonzini
3544 07f07615 Paolo Bonzini
        acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
3545 07f07615 Paolo Bonzini
        if (acb == NULL) {
3546 07f07615 Paolo Bonzini
            return -EIO;
3547 07f07615 Paolo Bonzini
        } else {
3548 07f07615 Paolo Bonzini
            qemu_coroutine_yield();
3549 07f07615 Paolo Bonzini
            return co.ret;
3550 07f07615 Paolo Bonzini
        }
3551 07f07615 Paolo Bonzini
    } else {
3552 07f07615 Paolo Bonzini
        /*
3553 07f07615 Paolo Bonzini
         * Some block drivers always operate in either writethrough or unsafe
3554 07f07615 Paolo Bonzini
         * mode and don't support bdrv_flush therefore. Usually qemu doesn't
3555 07f07615 Paolo Bonzini
         * know how the server works (because the behaviour is hardcoded or
3556 07f07615 Paolo Bonzini
         * depends on server-side configuration), so we can't ensure that
3557 07f07615 Paolo Bonzini
         * everything is safe on disk. Returning an error doesn't work because
3558 07f07615 Paolo Bonzini
         * that would break guests even if the server operates in writethrough
3559 07f07615 Paolo Bonzini
         * mode.
3560 07f07615 Paolo Bonzini
         *
3561 07f07615 Paolo Bonzini
         * Let's hope the user knows what he's doing.
3562 07f07615 Paolo Bonzini
         */
3563 07f07615 Paolo Bonzini
        return 0;
3564 07f07615 Paolo Bonzini
    }
3565 07f07615 Paolo Bonzini
}
3566 07f07615 Paolo Bonzini
3567 0f15423c Anthony Liguori
void bdrv_invalidate_cache(BlockDriverState *bs)
3568 0f15423c Anthony Liguori
{
3569 0f15423c Anthony Liguori
    if (bs->drv && bs->drv->bdrv_invalidate_cache) {
3570 0f15423c Anthony Liguori
        bs->drv->bdrv_invalidate_cache(bs);
3571 0f15423c Anthony Liguori
    }
3572 0f15423c Anthony Liguori
}
3573 0f15423c Anthony Liguori
3574 0f15423c Anthony Liguori
void bdrv_invalidate_cache_all(void)
3575 0f15423c Anthony Liguori
{
3576 0f15423c Anthony Liguori
    BlockDriverState *bs;
3577 0f15423c Anthony Liguori
3578 0f15423c Anthony Liguori
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
3579 0f15423c Anthony Liguori
        bdrv_invalidate_cache(bs);
3580 0f15423c Anthony Liguori
    }
3581 0f15423c Anthony Liguori
}
3582 0f15423c Anthony Liguori
3583 07f07615 Paolo Bonzini
int bdrv_flush(BlockDriverState *bs)
3584 07f07615 Paolo Bonzini
{
3585 07f07615 Paolo Bonzini
    Coroutine *co;
3586 07f07615 Paolo Bonzini
    RwCo rwco = {
3587 07f07615 Paolo Bonzini
        .bs = bs,
3588 07f07615 Paolo Bonzini
        .ret = NOT_DONE,
3589 e7a8a783 Kevin Wolf
    };
3590 e7a8a783 Kevin Wolf
3591 07f07615 Paolo Bonzini
    if (qemu_in_coroutine()) {
3592 07f07615 Paolo Bonzini
        /* Fast-path if already in coroutine context */
3593 07f07615 Paolo Bonzini
        bdrv_flush_co_entry(&rwco);
3594 07f07615 Paolo Bonzini
    } else {
3595 07f07615 Paolo Bonzini
        co = qemu_coroutine_create(bdrv_flush_co_entry);
3596 07f07615 Paolo Bonzini
        qemu_coroutine_enter(co, &rwco);
3597 07f07615 Paolo Bonzini
        while (rwco.ret == NOT_DONE) {
3598 07f07615 Paolo Bonzini
            qemu_aio_wait();
3599 07f07615 Paolo Bonzini
        }
3600 e7a8a783 Kevin Wolf
    }
3601 07f07615 Paolo Bonzini
3602 07f07615 Paolo Bonzini
    return rwco.ret;
3603 e7a8a783 Kevin Wolf
}
3604 e7a8a783 Kevin Wolf
3605 4265d620 Paolo Bonzini
static void coroutine_fn bdrv_discard_co_entry(void *opaque)
3606 4265d620 Paolo Bonzini
{
3607 4265d620 Paolo Bonzini
    RwCo *rwco = opaque;
3608 4265d620 Paolo Bonzini
3609 4265d620 Paolo Bonzini
    rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
3610 4265d620 Paolo Bonzini
}
3611 4265d620 Paolo Bonzini
3612 4265d620 Paolo Bonzini
int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
3613 4265d620 Paolo Bonzini
                                 int nb_sectors)
3614 4265d620 Paolo Bonzini
{
3615 4265d620 Paolo Bonzini
    if (!bs->drv) {
3616 4265d620 Paolo Bonzini
        return -ENOMEDIUM;
3617 4265d620 Paolo Bonzini
    } else if (bdrv_check_request(bs, sector_num, nb_sectors)) {
3618 4265d620 Paolo Bonzini
        return -EIO;
3619 4265d620 Paolo Bonzini
    } else if (bs->read_only) {
3620 4265d620 Paolo Bonzini
        return -EROFS;
3621 4265d620 Paolo Bonzini
    } else if (bs->drv->bdrv_co_discard) {
3622 4265d620 Paolo Bonzini
        return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
3623 4265d620 Paolo Bonzini
    } else if (bs->drv->bdrv_aio_discard) {
3624 4265d620 Paolo Bonzini
        BlockDriverAIOCB *acb;
3625 4265d620 Paolo Bonzini
        CoroutineIOCompletion co = {
3626 4265d620 Paolo Bonzini
            .coroutine = qemu_coroutine_self(),
3627 4265d620 Paolo Bonzini
        };
3628 4265d620 Paolo Bonzini
3629 4265d620 Paolo Bonzini
        acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
3630 4265d620 Paolo Bonzini
                                        bdrv_co_io_em_complete, &co);
3631 4265d620 Paolo Bonzini
        if (acb == NULL) {
3632 4265d620 Paolo Bonzini
            return -EIO;
3633 4265d620 Paolo Bonzini
        } else {
3634 4265d620 Paolo Bonzini
            qemu_coroutine_yield();
3635 4265d620 Paolo Bonzini
            return co.ret;
3636 4265d620 Paolo Bonzini
        }
3637 4265d620 Paolo Bonzini
    } else {
3638 4265d620 Paolo Bonzini
        return 0;
3639 4265d620 Paolo Bonzini
    }
3640 4265d620 Paolo Bonzini
}
3641 4265d620 Paolo Bonzini
3642 4265d620 Paolo Bonzini
int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
3643 4265d620 Paolo Bonzini
{
3644 4265d620 Paolo Bonzini
    Coroutine *co;
3645 4265d620 Paolo Bonzini
    RwCo rwco = {
3646 4265d620 Paolo Bonzini
        .bs = bs,
3647 4265d620 Paolo Bonzini
        .sector_num = sector_num,
3648 4265d620 Paolo Bonzini
        .nb_sectors = nb_sectors,
3649 4265d620 Paolo Bonzini
        .ret = NOT_DONE,
3650 4265d620 Paolo Bonzini
    };
3651 4265d620 Paolo Bonzini
3652 4265d620 Paolo Bonzini
    if (qemu_in_coroutine()) {
3653 4265d620 Paolo Bonzini
        /* Fast-path if already in coroutine context */
3654 4265d620 Paolo Bonzini
        bdrv_discard_co_entry(&rwco);
3655 4265d620 Paolo Bonzini
    } else {
3656 4265d620 Paolo Bonzini
        co = qemu_coroutine_create(bdrv_discard_co_entry);
3657 4265d620 Paolo Bonzini
        qemu_coroutine_enter(co, &rwco);
3658 4265d620 Paolo Bonzini
        while (rwco.ret == NOT_DONE) {
3659 4265d620 Paolo Bonzini
            qemu_aio_wait();
3660 4265d620 Paolo Bonzini
        }
3661 4265d620 Paolo Bonzini
    }
3662 4265d620 Paolo Bonzini
3663 4265d620 Paolo Bonzini
    return rwco.ret;
3664 4265d620 Paolo Bonzini
}
3665 4265d620 Paolo Bonzini
3666 f9f05dc5 Kevin Wolf
/**************************************************************/
3667 19cb3738 bellard
/* removable device support */
3668 19cb3738 bellard
3669 19cb3738 bellard
/**
3670 19cb3738 bellard
 * Return TRUE if the media is present
3671 19cb3738 bellard
 */
3672 19cb3738 bellard
int bdrv_is_inserted(BlockDriverState *bs)
3673 19cb3738 bellard
{
3674 19cb3738 bellard
    BlockDriver *drv = bs->drv;
3675 a1aff5bf Markus Armbruster
3676 19cb3738 bellard
    if (!drv)
3677 19cb3738 bellard
        return 0;
3678 19cb3738 bellard
    if (!drv->bdrv_is_inserted)
3679 a1aff5bf Markus Armbruster
        return 1;
3680 a1aff5bf Markus Armbruster
    return drv->bdrv_is_inserted(bs);
3681 19cb3738 bellard
}
3682 19cb3738 bellard
3683 19cb3738 bellard
/**
3684 8e49ca46 Markus Armbruster
 * Return whether the media changed since the last call to this
3685 8e49ca46 Markus Armbruster
 * function, or -ENOTSUP if we don't know.  Most drivers don't know.
3686 19cb3738 bellard
 */
3687 19cb3738 bellard
int bdrv_media_changed(BlockDriverState *bs)
3688 19cb3738 bellard
{
3689 19cb3738 bellard
    BlockDriver *drv = bs->drv;
3690 19cb3738 bellard
3691 8e49ca46 Markus Armbruster
    if (drv && drv->bdrv_media_changed) {
3692 8e49ca46 Markus Armbruster
        return drv->bdrv_media_changed(bs);
3693 8e49ca46 Markus Armbruster
    }
3694 8e49ca46 Markus Armbruster
    return -ENOTSUP;
3695 19cb3738 bellard
}
3696 19cb3738 bellard
3697 19cb3738 bellard
/**
3698 19cb3738 bellard
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
3699 19cb3738 bellard
 */
3700 f36f3949 Luiz Capitulino
void bdrv_eject(BlockDriverState *bs, bool eject_flag)
3701 19cb3738 bellard
{
3702 19cb3738 bellard
    BlockDriver *drv = bs->drv;
3703 19cb3738 bellard
3704 822e1cd1 Markus Armbruster
    if (drv && drv->bdrv_eject) {
3705 822e1cd1 Markus Armbruster
        drv->bdrv_eject(bs, eject_flag);
3706 19cb3738 bellard
    }
3707 6f382ed2 Luiz Capitulino
3708 6f382ed2 Luiz Capitulino
    if (bs->device_name[0] != '\0') {
3709 6f382ed2 Luiz Capitulino
        bdrv_emit_qmp_eject_event(bs, eject_flag);
3710 6f382ed2 Luiz Capitulino
    }
3711 19cb3738 bellard
}
3712 19cb3738 bellard
3713 19cb3738 bellard
/**
3714 19cb3738 bellard
 * Lock or unlock the media (if it is locked, the user won't be able
3715 19cb3738 bellard
 * to eject it manually).
3716 19cb3738 bellard
 */
3717 025e849a Markus Armbruster
void bdrv_lock_medium(BlockDriverState *bs, bool locked)
3718 19cb3738 bellard
{
3719 19cb3738 bellard
    BlockDriver *drv = bs->drv;
3720 19cb3738 bellard
3721 025e849a Markus Armbruster
    trace_bdrv_lock_medium(bs, locked);
3722 b8c6d095 Stefan Hajnoczi
3723 025e849a Markus Armbruster
    if (drv && drv->bdrv_lock_medium) {
3724 025e849a Markus Armbruster
        drv->bdrv_lock_medium(bs, locked);
3725 19cb3738 bellard
    }
3726 19cb3738 bellard
}
3727 985a03b0 ths
3728 985a03b0 ths
/* needed for generic scsi interface */
3729 985a03b0 ths
3730 985a03b0 ths
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
3731 985a03b0 ths
{
3732 985a03b0 ths
    BlockDriver *drv = bs->drv;
3733 985a03b0 ths
3734 985a03b0 ths
    if (drv && drv->bdrv_ioctl)
3735 985a03b0 ths
        return drv->bdrv_ioctl(bs, req, buf);
3736 985a03b0 ths
    return -ENOTSUP;
3737 985a03b0 ths
}
3738 7d780669 aliguori
3739 221f715d aliguori
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
3740 221f715d aliguori
        unsigned long int req, void *buf,
3741 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
3742 7d780669 aliguori
{
3743 221f715d aliguori
    BlockDriver *drv = bs->drv;
3744 7d780669 aliguori
3745 221f715d aliguori
    if (drv && drv->bdrv_aio_ioctl)
3746 221f715d aliguori
        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
3747 221f715d aliguori
    return NULL;
3748 7d780669 aliguori
}
3749 e268ca52 aliguori
3750 7b6f9300 Markus Armbruster
void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
3751 7b6f9300 Markus Armbruster
{
3752 7b6f9300 Markus Armbruster
    bs->buffer_alignment = align;
3753 7b6f9300 Markus Armbruster
}
3754 7cd1e32a lirans@il.ibm.com
3755 e268ca52 aliguori
void *qemu_blockalign(BlockDriverState *bs, size_t size)
3756 e268ca52 aliguori
{
3757 e268ca52 aliguori
    return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
3758 e268ca52 aliguori
}
3759 7cd1e32a lirans@il.ibm.com
3760 7cd1e32a lirans@il.ibm.com
void bdrv_set_dirty_tracking(BlockDriverState *bs, int enable)
3761 7cd1e32a lirans@il.ibm.com
{
3762 7cd1e32a lirans@il.ibm.com
    int64_t bitmap_size;
3763 a55eb92c Jan Kiszka
3764 aaa0eb75 Liran Schour
    bs->dirty_count = 0;
3765 a55eb92c Jan Kiszka
    if (enable) {
3766 c6d22830 Jan Kiszka
        if (!bs->dirty_bitmap) {
3767 c6d22830 Jan Kiszka
            bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS) +
3768 c6d22830 Jan Kiszka
                    BDRV_SECTORS_PER_DIRTY_CHUNK * 8 - 1;
3769 c6d22830 Jan Kiszka
            bitmap_size /= BDRV_SECTORS_PER_DIRTY_CHUNK * 8;
3770 a55eb92c Jan Kiszka
3771 7267c094 Anthony Liguori
            bs->dirty_bitmap = g_malloc0(bitmap_size);
3772 a55eb92c Jan Kiszka
        }
3773 7cd1e32a lirans@il.ibm.com
    } else {
3774 c6d22830 Jan Kiszka
        if (bs->dirty_bitmap) {
3775 7267c094 Anthony Liguori
            g_free(bs->dirty_bitmap);
3776 c6d22830 Jan Kiszka
            bs->dirty_bitmap = NULL;
3777 a55eb92c Jan Kiszka
        }
3778 7cd1e32a lirans@il.ibm.com
    }
3779 7cd1e32a lirans@il.ibm.com
}
3780 7cd1e32a lirans@il.ibm.com
3781 7cd1e32a lirans@il.ibm.com
int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
3782 7cd1e32a lirans@il.ibm.com
{
3783 6ea44308 Jan Kiszka
    int64_t chunk = sector / (int64_t)BDRV_SECTORS_PER_DIRTY_CHUNK;
3784 a55eb92c Jan Kiszka
3785 c6d22830 Jan Kiszka
    if (bs->dirty_bitmap &&
3786 c6d22830 Jan Kiszka
        (sector << BDRV_SECTOR_BITS) < bdrv_getlength(bs)) {
3787 6d59fec1 Marcelo Tosatti
        return !!(bs->dirty_bitmap[chunk / (sizeof(unsigned long) * 8)] &
3788 6d59fec1 Marcelo Tosatti
            (1UL << (chunk % (sizeof(unsigned long) * 8))));
3789 7cd1e32a lirans@il.ibm.com
    } else {
3790 7cd1e32a lirans@il.ibm.com
        return 0;
3791 7cd1e32a lirans@il.ibm.com
    }
3792 7cd1e32a lirans@il.ibm.com
}
3793 7cd1e32a lirans@il.ibm.com
3794 a55eb92c Jan Kiszka
void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
3795 a55eb92c Jan Kiszka
                      int nr_sectors)
3796 7cd1e32a lirans@il.ibm.com
{
3797 7cd1e32a lirans@il.ibm.com
    set_dirty_bitmap(bs, cur_sector, nr_sectors, 0);
3798 7cd1e32a lirans@il.ibm.com
}
3799 aaa0eb75 Liran Schour
3800 aaa0eb75 Liran Schour
int64_t bdrv_get_dirty_count(BlockDriverState *bs)
3801 aaa0eb75 Liran Schour
{
3802 aaa0eb75 Liran Schour
    return bs->dirty_count;
3803 aaa0eb75 Liran Schour
}
3804 f88e1a42 Jes Sorensen
3805 db593f25 Marcelo Tosatti
void bdrv_set_in_use(BlockDriverState *bs, int in_use)
3806 db593f25 Marcelo Tosatti
{
3807 db593f25 Marcelo Tosatti
    assert(bs->in_use != in_use);
3808 db593f25 Marcelo Tosatti
    bs->in_use = in_use;
3809 db593f25 Marcelo Tosatti
}
3810 db593f25 Marcelo Tosatti
3811 db593f25 Marcelo Tosatti
int bdrv_in_use(BlockDriverState *bs)
3812 db593f25 Marcelo Tosatti
{
3813 db593f25 Marcelo Tosatti
    return bs->in_use;
3814 db593f25 Marcelo Tosatti
}
3815 db593f25 Marcelo Tosatti
3816 28a7282a Luiz Capitulino
void bdrv_iostatus_enable(BlockDriverState *bs)
3817 28a7282a Luiz Capitulino
{
3818 d6bf279e Luiz Capitulino
    bs->iostatus_enabled = true;
3819 58e21ef5 Luiz Capitulino
    bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
3820 28a7282a Luiz Capitulino
}
3821 28a7282a Luiz Capitulino
3822 28a7282a Luiz Capitulino
/* The I/O status is only enabled if the drive explicitly
3823 28a7282a Luiz Capitulino
 * enables it _and_ the VM is configured to stop on errors */
3824 28a7282a Luiz Capitulino
bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
3825 28a7282a Luiz Capitulino
{
3826 d6bf279e Luiz Capitulino
    return (bs->iostatus_enabled &&
3827 28a7282a Luiz Capitulino
           (bs->on_write_error == BLOCK_ERR_STOP_ENOSPC ||
3828 28a7282a Luiz Capitulino
            bs->on_write_error == BLOCK_ERR_STOP_ANY    ||
3829 28a7282a Luiz Capitulino
            bs->on_read_error == BLOCK_ERR_STOP_ANY));
3830 28a7282a Luiz Capitulino
}
3831 28a7282a Luiz Capitulino
3832 28a7282a Luiz Capitulino
void bdrv_iostatus_disable(BlockDriverState *bs)
3833 28a7282a Luiz Capitulino
{
3834 d6bf279e Luiz Capitulino
    bs->iostatus_enabled = false;
3835 28a7282a Luiz Capitulino
}
3836 28a7282a Luiz Capitulino
3837 28a7282a Luiz Capitulino
void bdrv_iostatus_reset(BlockDriverState *bs)
3838 28a7282a Luiz Capitulino
{
3839 28a7282a Luiz Capitulino
    if (bdrv_iostatus_is_enabled(bs)) {
3840 58e21ef5 Luiz Capitulino
        bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
3841 28a7282a Luiz Capitulino
    }
3842 28a7282a Luiz Capitulino
}
3843 28a7282a Luiz Capitulino
3844 28a7282a Luiz Capitulino
/* XXX: Today this is set by device models because it makes the implementation
3845 28a7282a Luiz Capitulino
   quite simple. However, the block layer knows about the error, so it's
3846 28a7282a Luiz Capitulino
   possible to implement this without device models being involved */
3847 28a7282a Luiz Capitulino
void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
3848 28a7282a Luiz Capitulino
{
3849 58e21ef5 Luiz Capitulino
    if (bdrv_iostatus_is_enabled(bs) &&
3850 58e21ef5 Luiz Capitulino
        bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
3851 28a7282a Luiz Capitulino
        assert(error >= 0);
3852 58e21ef5 Luiz Capitulino
        bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
3853 58e21ef5 Luiz Capitulino
                                         BLOCK_DEVICE_IO_STATUS_FAILED;
3854 28a7282a Luiz Capitulino
    }
3855 28a7282a Luiz Capitulino
}
3856 28a7282a Luiz Capitulino
3857 a597e79c Christoph Hellwig
void
3858 a597e79c Christoph Hellwig
bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
3859 a597e79c Christoph Hellwig
        enum BlockAcctType type)
3860 a597e79c Christoph Hellwig
{
3861 a597e79c Christoph Hellwig
    assert(type < BDRV_MAX_IOTYPE);
3862 a597e79c Christoph Hellwig
3863 a597e79c Christoph Hellwig
    cookie->bytes = bytes;
3864 c488c7f6 Christoph Hellwig
    cookie->start_time_ns = get_clock();
3865 a597e79c Christoph Hellwig
    cookie->type = type;
3866 a597e79c Christoph Hellwig
}
3867 a597e79c Christoph Hellwig
3868 a597e79c Christoph Hellwig
void
3869 a597e79c Christoph Hellwig
bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
3870 a597e79c Christoph Hellwig
{
3871 a597e79c Christoph Hellwig
    assert(cookie->type < BDRV_MAX_IOTYPE);
3872 a597e79c Christoph Hellwig
3873 a597e79c Christoph Hellwig
    bs->nr_bytes[cookie->type] += cookie->bytes;
3874 a597e79c Christoph Hellwig
    bs->nr_ops[cookie->type]++;
3875 c488c7f6 Christoph Hellwig
    bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
3876 a597e79c Christoph Hellwig
}
3877 a597e79c Christoph Hellwig
3878 f88e1a42 Jes Sorensen
int bdrv_img_create(const char *filename, const char *fmt,
3879 f88e1a42 Jes Sorensen
                    const char *base_filename, const char *base_fmt,
3880 f88e1a42 Jes Sorensen
                    char *options, uint64_t img_size, int flags)
3881 f88e1a42 Jes Sorensen
{
3882 f88e1a42 Jes Sorensen
    QEMUOptionParameter *param = NULL, *create_options = NULL;
3883 d220894e Kevin Wolf
    QEMUOptionParameter *backing_fmt, *backing_file, *size;
3884 f88e1a42 Jes Sorensen
    BlockDriverState *bs = NULL;
3885 f88e1a42 Jes Sorensen
    BlockDriver *drv, *proto_drv;
3886 96df67d1 Stefan Hajnoczi
    BlockDriver *backing_drv = NULL;
3887 f88e1a42 Jes Sorensen
    int ret = 0;
3888 f88e1a42 Jes Sorensen
3889 f88e1a42 Jes Sorensen
    /* Find driver and parse its options */
3890 f88e1a42 Jes Sorensen
    drv = bdrv_find_format(fmt);
3891 f88e1a42 Jes Sorensen
    if (!drv) {
3892 f88e1a42 Jes Sorensen
        error_report("Unknown file format '%s'", fmt);
3893 4f70f249 Jes Sorensen
        ret = -EINVAL;
3894 f88e1a42 Jes Sorensen
        goto out;
3895 f88e1a42 Jes Sorensen
    }
3896 f88e1a42 Jes Sorensen
3897 f88e1a42 Jes Sorensen
    proto_drv = bdrv_find_protocol(filename);
3898 f88e1a42 Jes Sorensen
    if (!proto_drv) {
3899 f88e1a42 Jes Sorensen
        error_report("Unknown protocol '%s'", filename);
3900 4f70f249 Jes Sorensen
        ret = -EINVAL;
3901 f88e1a42 Jes Sorensen
        goto out;
3902 f88e1a42 Jes Sorensen
    }
3903 f88e1a42 Jes Sorensen
3904 f88e1a42 Jes Sorensen
    create_options = append_option_parameters(create_options,
3905 f88e1a42 Jes Sorensen
                                              drv->create_options);
3906 f88e1a42 Jes Sorensen
    create_options = append_option_parameters(create_options,
3907 f88e1a42 Jes Sorensen
                                              proto_drv->create_options);
3908 f88e1a42 Jes Sorensen
3909 f88e1a42 Jes Sorensen
    /* Create parameter list with default values */
3910 f88e1a42 Jes Sorensen
    param = parse_option_parameters("", create_options, param);
3911 f88e1a42 Jes Sorensen
3912 f88e1a42 Jes Sorensen
    set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
3913 f88e1a42 Jes Sorensen
3914 f88e1a42 Jes Sorensen
    /* Parse -o options */
3915 f88e1a42 Jes Sorensen
    if (options) {
3916 f88e1a42 Jes Sorensen
        param = parse_option_parameters(options, create_options, param);
3917 f88e1a42 Jes Sorensen
        if (param == NULL) {
3918 f88e1a42 Jes Sorensen
            error_report("Invalid options for file format '%s'.", fmt);
3919 4f70f249 Jes Sorensen
            ret = -EINVAL;
3920 f88e1a42 Jes Sorensen
            goto out;
3921 f88e1a42 Jes Sorensen
        }
3922 f88e1a42 Jes Sorensen
    }
3923 f88e1a42 Jes Sorensen
3924 f88e1a42 Jes Sorensen
    if (base_filename) {
3925 f88e1a42 Jes Sorensen
        if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
3926 f88e1a42 Jes Sorensen
                                 base_filename)) {
3927 f88e1a42 Jes Sorensen
            error_report("Backing file not supported for file format '%s'",
3928 f88e1a42 Jes Sorensen
                         fmt);
3929 4f70f249 Jes Sorensen
            ret = -EINVAL;
3930 f88e1a42 Jes Sorensen
            goto out;
3931 f88e1a42 Jes Sorensen
        }
3932 f88e1a42 Jes Sorensen
    }
3933 f88e1a42 Jes Sorensen
3934 f88e1a42 Jes Sorensen
    if (base_fmt) {
3935 f88e1a42 Jes Sorensen
        if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
3936 f88e1a42 Jes Sorensen
            error_report("Backing file format not supported for file "
3937 f88e1a42 Jes Sorensen
                         "format '%s'", fmt);
3938 4f70f249 Jes Sorensen
            ret = -EINVAL;
3939 f88e1a42 Jes Sorensen
            goto out;
3940 f88e1a42 Jes Sorensen
        }
3941 f88e1a42 Jes Sorensen
    }
3942 f88e1a42 Jes Sorensen
3943 792da93a Jes Sorensen
    backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
3944 792da93a Jes Sorensen
    if (backing_file && backing_file->value.s) {
3945 792da93a Jes Sorensen
        if (!strcmp(filename, backing_file->value.s)) {
3946 792da93a Jes Sorensen
            error_report("Error: Trying to create an image with the "
3947 792da93a Jes Sorensen
                         "same filename as the backing file");
3948 4f70f249 Jes Sorensen
            ret = -EINVAL;
3949 792da93a Jes Sorensen
            goto out;
3950 792da93a Jes Sorensen
        }
3951 792da93a Jes Sorensen
    }
3952 792da93a Jes Sorensen
3953 f88e1a42 Jes Sorensen
    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
3954 f88e1a42 Jes Sorensen
    if (backing_fmt && backing_fmt->value.s) {
3955 96df67d1 Stefan Hajnoczi
        backing_drv = bdrv_find_format(backing_fmt->value.s);
3956 96df67d1 Stefan Hajnoczi
        if (!backing_drv) {
3957 f88e1a42 Jes Sorensen
            error_report("Unknown backing file format '%s'",
3958 f88e1a42 Jes Sorensen
                         backing_fmt->value.s);
3959 4f70f249 Jes Sorensen
            ret = -EINVAL;
3960 f88e1a42 Jes Sorensen
            goto out;
3961 f88e1a42 Jes Sorensen
        }
3962 f88e1a42 Jes Sorensen
    }
3963 f88e1a42 Jes Sorensen
3964 f88e1a42 Jes Sorensen
    // The size for the image must always be specified, with one exception:
3965 f88e1a42 Jes Sorensen
    // If we are using a backing file, we can obtain the size from there
3966 d220894e Kevin Wolf
    size = get_option_parameter(param, BLOCK_OPT_SIZE);
3967 d220894e Kevin Wolf
    if (size && size->value.n == -1) {
3968 f88e1a42 Jes Sorensen
        if (backing_file && backing_file->value.s) {
3969 f88e1a42 Jes Sorensen
            uint64_t size;
3970 f88e1a42 Jes Sorensen
            char buf[32];
3971 f88e1a42 Jes Sorensen
3972 f88e1a42 Jes Sorensen
            bs = bdrv_new("");
3973 f88e1a42 Jes Sorensen
3974 96df67d1 Stefan Hajnoczi
            ret = bdrv_open(bs, backing_file->value.s, flags, backing_drv);
3975 f88e1a42 Jes Sorensen
            if (ret < 0) {
3976 96df67d1 Stefan Hajnoczi
                error_report("Could not open '%s'", backing_file->value.s);
3977 f88e1a42 Jes Sorensen
                goto out;
3978 f88e1a42 Jes Sorensen
            }
3979 f88e1a42 Jes Sorensen
            bdrv_get_geometry(bs, &size);
3980 f88e1a42 Jes Sorensen
            size *= 512;
3981 f88e1a42 Jes Sorensen
3982 f88e1a42 Jes Sorensen
            snprintf(buf, sizeof(buf), "%" PRId64, size);
3983 f88e1a42 Jes Sorensen
            set_option_parameter(param, BLOCK_OPT_SIZE, buf);
3984 f88e1a42 Jes Sorensen
        } else {
3985 f88e1a42 Jes Sorensen
            error_report("Image creation needs a size parameter");
3986 4f70f249 Jes Sorensen
            ret = -EINVAL;
3987 f88e1a42 Jes Sorensen
            goto out;
3988 f88e1a42 Jes Sorensen
        }
3989 f88e1a42 Jes Sorensen
    }
3990 f88e1a42 Jes Sorensen
3991 f88e1a42 Jes Sorensen
    printf("Formatting '%s', fmt=%s ", filename, fmt);
3992 f88e1a42 Jes Sorensen
    print_option_parameters(param);
3993 f88e1a42 Jes Sorensen
    puts("");
3994 f88e1a42 Jes Sorensen
3995 f88e1a42 Jes Sorensen
    ret = bdrv_create(drv, filename, param);
3996 f88e1a42 Jes Sorensen
3997 f88e1a42 Jes Sorensen
    if (ret < 0) {
3998 f88e1a42 Jes Sorensen
        if (ret == -ENOTSUP) {
3999 f88e1a42 Jes Sorensen
            error_report("Formatting or formatting option not supported for "
4000 f88e1a42 Jes Sorensen
                         "file format '%s'", fmt);
4001 f88e1a42 Jes Sorensen
        } else if (ret == -EFBIG) {
4002 f88e1a42 Jes Sorensen
            error_report("The image size is too large for file format '%s'",
4003 f88e1a42 Jes Sorensen
                         fmt);
4004 f88e1a42 Jes Sorensen
        } else {
4005 f88e1a42 Jes Sorensen
            error_report("%s: error while creating %s: %s", filename, fmt,
4006 f88e1a42 Jes Sorensen
                         strerror(-ret));
4007 f88e1a42 Jes Sorensen
        }
4008 f88e1a42 Jes Sorensen
    }
4009 f88e1a42 Jes Sorensen
4010 f88e1a42 Jes Sorensen
out:
4011 f88e1a42 Jes Sorensen
    free_option_parameters(create_options);
4012 f88e1a42 Jes Sorensen
    free_option_parameters(param);
4013 f88e1a42 Jes Sorensen
4014 f88e1a42 Jes Sorensen
    if (bs) {
4015 f88e1a42 Jes Sorensen
        bdrv_delete(bs);
4016 f88e1a42 Jes Sorensen
    }
4017 4f70f249 Jes Sorensen
4018 4f70f249 Jes Sorensen
    return ret;
4019 f88e1a42 Jes Sorensen
}
4020 eeec61f2 Stefan Hajnoczi
4021 eeec61f2 Stefan Hajnoczi
void *block_job_create(const BlockJobType *job_type, BlockDriverState *bs,
4022 eeec61f2 Stefan Hajnoczi
                       BlockDriverCompletionFunc *cb, void *opaque)
4023 eeec61f2 Stefan Hajnoczi
{
4024 eeec61f2 Stefan Hajnoczi
    BlockJob *job;
4025 eeec61f2 Stefan Hajnoczi
4026 eeec61f2 Stefan Hajnoczi
    if (bs->job || bdrv_in_use(bs)) {
4027 eeec61f2 Stefan Hajnoczi
        return NULL;
4028 eeec61f2 Stefan Hajnoczi
    }
4029 eeec61f2 Stefan Hajnoczi
    bdrv_set_in_use(bs, 1);
4030 eeec61f2 Stefan Hajnoczi
4031 eeec61f2 Stefan Hajnoczi
    job = g_malloc0(job_type->instance_size);
4032 eeec61f2 Stefan Hajnoczi
    job->job_type      = job_type;
4033 eeec61f2 Stefan Hajnoczi
    job->bs            = bs;
4034 eeec61f2 Stefan Hajnoczi
    job->cb            = cb;
4035 eeec61f2 Stefan Hajnoczi
    job->opaque        = opaque;
4036 eeec61f2 Stefan Hajnoczi
    bs->job = job;
4037 eeec61f2 Stefan Hajnoczi
    return job;
4038 eeec61f2 Stefan Hajnoczi
}
4039 eeec61f2 Stefan Hajnoczi
4040 eeec61f2 Stefan Hajnoczi
void block_job_complete(BlockJob *job, int ret)
4041 eeec61f2 Stefan Hajnoczi
{
4042 eeec61f2 Stefan Hajnoczi
    BlockDriverState *bs = job->bs;
4043 eeec61f2 Stefan Hajnoczi
4044 eeec61f2 Stefan Hajnoczi
    assert(bs->job == job);
4045 eeec61f2 Stefan Hajnoczi
    job->cb(job->opaque, ret);
4046 eeec61f2 Stefan Hajnoczi
    bs->job = NULL;
4047 eeec61f2 Stefan Hajnoczi
    g_free(job);
4048 eeec61f2 Stefan Hajnoczi
    bdrv_set_in_use(bs, 0);
4049 eeec61f2 Stefan Hajnoczi
}
4050 eeec61f2 Stefan Hajnoczi
4051 eeec61f2 Stefan Hajnoczi
int block_job_set_speed(BlockJob *job, int64_t value)
4052 eeec61f2 Stefan Hajnoczi
{
4053 eeec61f2 Stefan Hajnoczi
    if (!job->job_type->set_speed) {
4054 eeec61f2 Stefan Hajnoczi
        return -ENOTSUP;
4055 eeec61f2 Stefan Hajnoczi
    }
4056 eeec61f2 Stefan Hajnoczi
    return job->job_type->set_speed(job, value);
4057 eeec61f2 Stefan Hajnoczi
}
4058 eeec61f2 Stefan Hajnoczi
4059 eeec61f2 Stefan Hajnoczi
void block_job_cancel(BlockJob *job)
4060 eeec61f2 Stefan Hajnoczi
{
4061 eeec61f2 Stefan Hajnoczi
    job->cancelled = true;
4062 eeec61f2 Stefan Hajnoczi
}
4063 eeec61f2 Stefan Hajnoczi
4064 eeec61f2 Stefan Hajnoczi
bool block_job_is_cancelled(BlockJob *job)
4065 eeec61f2 Stefan Hajnoczi
{
4066 eeec61f2 Stefan Hajnoczi
    return job->cancelled;
4067 eeec61f2 Stefan Hajnoczi
}