Statistics
| Branch: | Revision:

root / block.c @ 11c7549d

History | View | Annotate | Download (125.3 kB)

1 fc01f7e7 bellard
/*
2 fc01f7e7 bellard
 * QEMU System Emulator block driver
3 5fafdf24 ths
 *
4 fc01f7e7 bellard
 * Copyright (c) 2003 Fabrice Bellard
5 5fafdf24 ths
 *
6 fc01f7e7 bellard
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 fc01f7e7 bellard
 * of this software and associated documentation files (the "Software"), to deal
8 fc01f7e7 bellard
 * in the Software without restriction, including without limitation the rights
9 fc01f7e7 bellard
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 fc01f7e7 bellard
 * copies of the Software, and to permit persons to whom the Software is
11 fc01f7e7 bellard
 * furnished to do so, subject to the following conditions:
12 fc01f7e7 bellard
 *
13 fc01f7e7 bellard
 * The above copyright notice and this permission notice shall be included in
14 fc01f7e7 bellard
 * all copies or substantial portions of the Software.
15 fc01f7e7 bellard
 *
16 fc01f7e7 bellard
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 fc01f7e7 bellard
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 fc01f7e7 bellard
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 fc01f7e7 bellard
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 fc01f7e7 bellard
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 fc01f7e7 bellard
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 fc01f7e7 bellard
 * THE SOFTWARE.
23 fc01f7e7 bellard
 */
24 3990d09a blueswir1
#include "config-host.h"
25 faf07963 pbrook
#include "qemu-common.h"
26 6d519a5f Stefan Hajnoczi
#include "trace.h"
27 83c9089e Paolo Bonzini
#include "monitor/monitor.h"
28 737e150e Paolo Bonzini
#include "block/block_int.h"
29 737e150e Paolo Bonzini
#include "block/blockjob.h"
30 1de7afc9 Paolo Bonzini
#include "qemu/module.h"
31 7b1b5d19 Paolo Bonzini
#include "qapi/qmp/qjson.h"
32 9c17d615 Paolo Bonzini
#include "sysemu/sysemu.h"
33 1de7afc9 Paolo Bonzini
#include "qemu/notify.h"
34 737e150e Paolo Bonzini
#include "block/coroutine.h"
35 b2023818 Luiz Capitulino
#include "qmp-commands.h"
36 1de7afc9 Paolo Bonzini
#include "qemu/timer.h"
37 fc01f7e7 bellard
38 71e72a19 Juan Quintela
#ifdef CONFIG_BSD
39 7674e7bf bellard
#include <sys/types.h>
40 7674e7bf bellard
#include <sys/stat.h>
41 7674e7bf bellard
#include <sys/ioctl.h>
42 72cf2d4f Blue Swirl
#include <sys/queue.h>
43 c5e97233 blueswir1
#ifndef __DragonFly__
44 7674e7bf bellard
#include <sys/disk.h>
45 7674e7bf bellard
#endif
46 c5e97233 blueswir1
#endif
47 7674e7bf bellard
48 49dc768d aliguori
#ifdef _WIN32
49 49dc768d aliguori
#include <windows.h>
50 49dc768d aliguori
#endif
51 49dc768d aliguori
52 1c9805a3 Stefan Hajnoczi
#define NOT_DONE 0x7fffffff /* used while emulated sync operation in progress */
53 1c9805a3 Stefan Hajnoczi
54 470c0504 Stefan Hajnoczi
typedef enum {
55 470c0504 Stefan Hajnoczi
    BDRV_REQ_COPY_ON_READ = 0x1,
56 f08f2dda Stefan Hajnoczi
    BDRV_REQ_ZERO_WRITE   = 0x2,
57 470c0504 Stefan Hajnoczi
} BdrvRequestFlags;
58 470c0504 Stefan Hajnoczi
59 7d4b4ba5 Markus Armbruster
static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load);
60 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
61 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
62 c87c0672 aliguori
        BlockDriverCompletionFunc *cb, void *opaque);
63 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
64 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
65 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque);
66 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
67 f9f05dc5 Kevin Wolf
                                         int64_t sector_num, int nb_sectors,
68 f9f05dc5 Kevin Wolf
                                         QEMUIOVector *iov);
69 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
70 f9f05dc5 Kevin Wolf
                                         int64_t sector_num, int nb_sectors,
71 f9f05dc5 Kevin Wolf
                                         QEMUIOVector *iov);
72 c5fbe571 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
73 470c0504 Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
74 470c0504 Stefan Hajnoczi
    BdrvRequestFlags flags);
75 1c9805a3 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
76 f08f2dda Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
77 f08f2dda Stefan Hajnoczi
    BdrvRequestFlags flags);
78 b2a61371 Stefan Hajnoczi
static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
79 b2a61371 Stefan Hajnoczi
                                               int64_t sector_num,
80 b2a61371 Stefan Hajnoczi
                                               QEMUIOVector *qiov,
81 b2a61371 Stefan Hajnoczi
                                               int nb_sectors,
82 b2a61371 Stefan Hajnoczi
                                               BlockDriverCompletionFunc *cb,
83 b2a61371 Stefan Hajnoczi
                                               void *opaque,
84 8c5873d6 Stefan Hajnoczi
                                               bool is_write);
85 b2a61371 Stefan Hajnoczi
static void coroutine_fn bdrv_co_do_rw(void *opaque);
86 621f0589 Kevin Wolf
static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
87 621f0589 Kevin Wolf
    int64_t sector_num, int nb_sectors);
88 ec530c81 bellard
89 98f90dba Zhi Yong Wu
static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors,
90 98f90dba Zhi Yong Wu
        bool is_write, double elapsed_time, uint64_t *wait);
91 98f90dba Zhi Yong Wu
static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write,
92 98f90dba Zhi Yong Wu
        double elapsed_time, uint64_t *wait);
93 98f90dba Zhi Yong Wu
static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors,
94 98f90dba Zhi Yong Wu
        bool is_write, int64_t *wait);
95 98f90dba Zhi Yong Wu
96 1b7bdbc1 Stefan Hajnoczi
static QTAILQ_HEAD(, BlockDriverState) bdrv_states =
97 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_HEAD_INITIALIZER(bdrv_states);
98 7ee930d0 blueswir1
99 8a22f02a Stefan Hajnoczi
static QLIST_HEAD(, BlockDriver) bdrv_drivers =
100 8a22f02a Stefan Hajnoczi
    QLIST_HEAD_INITIALIZER(bdrv_drivers);
101 ea2384d3 bellard
102 eb852011 Markus Armbruster
/* If non-zero, use only whitelisted block drivers */
103 eb852011 Markus Armbruster
static int use_bdrv_whitelist;
104 eb852011 Markus Armbruster
105 9e0b22f4 Stefan Hajnoczi
#ifdef _WIN32
106 9e0b22f4 Stefan Hajnoczi
static int is_windows_drive_prefix(const char *filename)
107 9e0b22f4 Stefan Hajnoczi
{
108 9e0b22f4 Stefan Hajnoczi
    return (((filename[0] >= 'a' && filename[0] <= 'z') ||
109 9e0b22f4 Stefan Hajnoczi
             (filename[0] >= 'A' && filename[0] <= 'Z')) &&
110 9e0b22f4 Stefan Hajnoczi
            filename[1] == ':');
111 9e0b22f4 Stefan Hajnoczi
}
112 9e0b22f4 Stefan Hajnoczi
113 9e0b22f4 Stefan Hajnoczi
int is_windows_drive(const char *filename)
114 9e0b22f4 Stefan Hajnoczi
{
115 9e0b22f4 Stefan Hajnoczi
    if (is_windows_drive_prefix(filename) &&
116 9e0b22f4 Stefan Hajnoczi
        filename[2] == '\0')
117 9e0b22f4 Stefan Hajnoczi
        return 1;
118 9e0b22f4 Stefan Hajnoczi
    if (strstart(filename, "\\\\.\\", NULL) ||
119 9e0b22f4 Stefan Hajnoczi
        strstart(filename, "//./", NULL))
120 9e0b22f4 Stefan Hajnoczi
        return 1;
121 9e0b22f4 Stefan Hajnoczi
    return 0;
122 9e0b22f4 Stefan Hajnoczi
}
123 9e0b22f4 Stefan Hajnoczi
#endif
124 9e0b22f4 Stefan Hajnoczi
125 0563e191 Zhi Yong Wu
/* throttling disk I/O limits */
126 98f90dba Zhi Yong Wu
void bdrv_io_limits_disable(BlockDriverState *bs)
127 98f90dba Zhi Yong Wu
{
128 98f90dba Zhi Yong Wu
    bs->io_limits_enabled = false;
129 98f90dba Zhi Yong Wu
130 98f90dba Zhi Yong Wu
    while (qemu_co_queue_next(&bs->throttled_reqs));
131 98f90dba Zhi Yong Wu
132 98f90dba Zhi Yong Wu
    if (bs->block_timer) {
133 98f90dba Zhi Yong Wu
        qemu_del_timer(bs->block_timer);
134 98f90dba Zhi Yong Wu
        qemu_free_timer(bs->block_timer);
135 98f90dba Zhi Yong Wu
        bs->block_timer = NULL;
136 98f90dba Zhi Yong Wu
    }
137 98f90dba Zhi Yong Wu
138 98f90dba Zhi Yong Wu
    bs->slice_start = 0;
139 98f90dba Zhi Yong Wu
    bs->slice_end   = 0;
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->io_limits_enabled = true;
154 0563e191 Zhi Yong Wu
}
155 0563e191 Zhi Yong Wu
156 0563e191 Zhi Yong Wu
bool bdrv_io_limits_enabled(BlockDriverState *bs)
157 0563e191 Zhi Yong Wu
{
158 0563e191 Zhi Yong Wu
    BlockIOLimit *io_limits = &bs->io_limits;
159 0563e191 Zhi Yong Wu
    return io_limits->bps[BLOCK_IO_LIMIT_READ]
160 0563e191 Zhi Yong Wu
         || io_limits->bps[BLOCK_IO_LIMIT_WRITE]
161 0563e191 Zhi Yong Wu
         || io_limits->bps[BLOCK_IO_LIMIT_TOTAL]
162 0563e191 Zhi Yong Wu
         || io_limits->iops[BLOCK_IO_LIMIT_READ]
163 0563e191 Zhi Yong Wu
         || io_limits->iops[BLOCK_IO_LIMIT_WRITE]
164 0563e191 Zhi Yong Wu
         || io_limits->iops[BLOCK_IO_LIMIT_TOTAL];
165 0563e191 Zhi Yong Wu
}
166 0563e191 Zhi Yong Wu
167 98f90dba Zhi Yong Wu
static void bdrv_io_limits_intercept(BlockDriverState *bs,
168 98f90dba Zhi Yong Wu
                                     bool is_write, int nb_sectors)
169 98f90dba Zhi Yong Wu
{
170 98f90dba Zhi Yong Wu
    int64_t wait_time = -1;
171 98f90dba Zhi Yong Wu
172 98f90dba Zhi Yong Wu
    if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
173 98f90dba Zhi Yong Wu
        qemu_co_queue_wait(&bs->throttled_reqs);
174 98f90dba Zhi Yong Wu
    }
175 98f90dba Zhi Yong Wu
176 98f90dba Zhi Yong Wu
    /* In fact, we hope to keep each request's timing, in FIFO mode. The next
177 98f90dba Zhi Yong Wu
     * throttled requests will not be dequeued until the current request is
178 98f90dba Zhi Yong Wu
     * allowed to be serviced. So if the current request still exceeds the
179 98f90dba Zhi Yong Wu
     * limits, it will be inserted to the head. All requests followed it will
180 98f90dba Zhi Yong Wu
     * be still in throttled_reqs queue.
181 98f90dba Zhi Yong Wu
     */
182 98f90dba Zhi Yong Wu
183 98f90dba Zhi Yong Wu
    while (bdrv_exceed_io_limits(bs, nb_sectors, is_write, &wait_time)) {
184 98f90dba Zhi Yong Wu
        qemu_mod_timer(bs->block_timer,
185 98f90dba Zhi Yong Wu
                       wait_time + qemu_get_clock_ns(vm_clock));
186 98f90dba Zhi Yong Wu
        qemu_co_queue_wait_insert_head(&bs->throttled_reqs);
187 98f90dba Zhi Yong Wu
    }
188 98f90dba Zhi Yong Wu
189 98f90dba Zhi Yong Wu
    qemu_co_queue_next(&bs->throttled_reqs);
190 98f90dba Zhi Yong Wu
}
191 98f90dba Zhi Yong Wu
192 9e0b22f4 Stefan Hajnoczi
/* check if the path starts with "<protocol>:" */
193 9e0b22f4 Stefan Hajnoczi
static int path_has_protocol(const char *path)
194 9e0b22f4 Stefan Hajnoczi
{
195 947995c0 Paolo Bonzini
    const char *p;
196 947995c0 Paolo Bonzini
197 9e0b22f4 Stefan Hajnoczi
#ifdef _WIN32
198 9e0b22f4 Stefan Hajnoczi
    if (is_windows_drive(path) ||
199 9e0b22f4 Stefan Hajnoczi
        is_windows_drive_prefix(path)) {
200 9e0b22f4 Stefan Hajnoczi
        return 0;
201 9e0b22f4 Stefan Hajnoczi
    }
202 947995c0 Paolo Bonzini
    p = path + strcspn(path, ":/\\");
203 947995c0 Paolo Bonzini
#else
204 947995c0 Paolo Bonzini
    p = path + strcspn(path, ":/");
205 9e0b22f4 Stefan Hajnoczi
#endif
206 9e0b22f4 Stefan Hajnoczi
207 947995c0 Paolo Bonzini
    return *p == ':';
208 9e0b22f4 Stefan Hajnoczi
}
209 9e0b22f4 Stefan Hajnoczi
210 83f64091 bellard
int path_is_absolute(const char *path)
211 3b0d4f61 bellard
{
212 21664424 bellard
#ifdef _WIN32
213 21664424 bellard
    /* specific case for names like: "\\.\d:" */
214 f53f4da9 Paolo Bonzini
    if (is_windows_drive(path) || is_windows_drive_prefix(path)) {
215 21664424 bellard
        return 1;
216 f53f4da9 Paolo Bonzini
    }
217 f53f4da9 Paolo Bonzini
    return (*path == '/' || *path == '\\');
218 3b9f94e1 bellard
#else
219 f53f4da9 Paolo Bonzini
    return (*path == '/');
220 3b9f94e1 bellard
#endif
221 3b0d4f61 bellard
}
222 3b0d4f61 bellard
223 83f64091 bellard
/* if filename is absolute, just copy it to dest. Otherwise, build a
224 83f64091 bellard
   path to it by considering it is relative to base_path. URL are
225 83f64091 bellard
   supported. */
226 83f64091 bellard
void path_combine(char *dest, int dest_size,
227 83f64091 bellard
                  const char *base_path,
228 83f64091 bellard
                  const char *filename)
229 3b0d4f61 bellard
{
230 83f64091 bellard
    const char *p, *p1;
231 83f64091 bellard
    int len;
232 83f64091 bellard
233 83f64091 bellard
    if (dest_size <= 0)
234 83f64091 bellard
        return;
235 83f64091 bellard
    if (path_is_absolute(filename)) {
236 83f64091 bellard
        pstrcpy(dest, dest_size, filename);
237 83f64091 bellard
    } else {
238 83f64091 bellard
        p = strchr(base_path, ':');
239 83f64091 bellard
        if (p)
240 83f64091 bellard
            p++;
241 83f64091 bellard
        else
242 83f64091 bellard
            p = base_path;
243 3b9f94e1 bellard
        p1 = strrchr(base_path, '/');
244 3b9f94e1 bellard
#ifdef _WIN32
245 3b9f94e1 bellard
        {
246 3b9f94e1 bellard
            const char *p2;
247 3b9f94e1 bellard
            p2 = strrchr(base_path, '\\');
248 3b9f94e1 bellard
            if (!p1 || p2 > p1)
249 3b9f94e1 bellard
                p1 = p2;
250 3b9f94e1 bellard
        }
251 3b9f94e1 bellard
#endif
252 83f64091 bellard
        if (p1)
253 83f64091 bellard
            p1++;
254 83f64091 bellard
        else
255 83f64091 bellard
            p1 = base_path;
256 83f64091 bellard
        if (p1 > p)
257 83f64091 bellard
            p = p1;
258 83f64091 bellard
        len = p - base_path;
259 83f64091 bellard
        if (len > dest_size - 1)
260 83f64091 bellard
            len = dest_size - 1;
261 83f64091 bellard
        memcpy(dest, base_path, len);
262 83f64091 bellard
        dest[len] = '\0';
263 83f64091 bellard
        pstrcat(dest, dest_size, filename);
264 3b0d4f61 bellard
    }
265 3b0d4f61 bellard
}
266 3b0d4f61 bellard
267 dc5a1371 Paolo Bonzini
void bdrv_get_full_backing_filename(BlockDriverState *bs, char *dest, size_t sz)
268 dc5a1371 Paolo Bonzini
{
269 dc5a1371 Paolo Bonzini
    if (bs->backing_file[0] == '\0' || path_has_protocol(bs->backing_file)) {
270 dc5a1371 Paolo Bonzini
        pstrcpy(dest, sz, bs->backing_file);
271 dc5a1371 Paolo Bonzini
    } else {
272 dc5a1371 Paolo Bonzini
        path_combine(dest, sz, bs->filename, bs->backing_file);
273 dc5a1371 Paolo Bonzini
    }
274 dc5a1371 Paolo Bonzini
}
275 dc5a1371 Paolo Bonzini
276 5efa9d5a Anthony Liguori
void bdrv_register(BlockDriver *bdrv)
277 ea2384d3 bellard
{
278 8c5873d6 Stefan Hajnoczi
    /* Block drivers without coroutine functions need emulation */
279 8c5873d6 Stefan Hajnoczi
    if (!bdrv->bdrv_co_readv) {
280 f9f05dc5 Kevin Wolf
        bdrv->bdrv_co_readv = bdrv_co_readv_em;
281 f9f05dc5 Kevin Wolf
        bdrv->bdrv_co_writev = bdrv_co_writev_em;
282 f9f05dc5 Kevin Wolf
283 f8c35c1d Stefan Hajnoczi
        /* bdrv_co_readv_em()/brdv_co_writev_em() work in terms of aio, so if
284 f8c35c1d Stefan Hajnoczi
         * the block driver lacks aio we need to emulate that too.
285 f8c35c1d Stefan Hajnoczi
         */
286 f9f05dc5 Kevin Wolf
        if (!bdrv->bdrv_aio_readv) {
287 f9f05dc5 Kevin Wolf
            /* add AIO emulation layer */
288 f9f05dc5 Kevin Wolf
            bdrv->bdrv_aio_readv = bdrv_aio_readv_em;
289 f9f05dc5 Kevin Wolf
            bdrv->bdrv_aio_writev = bdrv_aio_writev_em;
290 f9f05dc5 Kevin Wolf
        }
291 83f64091 bellard
    }
292 b2e12bc6 Christoph Hellwig
293 8a22f02a Stefan Hajnoczi
    QLIST_INSERT_HEAD(&bdrv_drivers, bdrv, list);
294 ea2384d3 bellard
}
295 b338082b bellard
296 b338082b bellard
/* create a new block device (by default it is empty) */
297 b338082b bellard
BlockDriverState *bdrv_new(const char *device_name)
298 b338082b bellard
{
299 1b7bdbc1 Stefan Hajnoczi
    BlockDriverState *bs;
300 b338082b bellard
301 7267c094 Anthony Liguori
    bs = g_malloc0(sizeof(BlockDriverState));
302 b338082b bellard
    pstrcpy(bs->device_name, sizeof(bs->device_name), device_name);
303 ea2384d3 bellard
    if (device_name[0] != '\0') {
304 1b7bdbc1 Stefan Hajnoczi
        QTAILQ_INSERT_TAIL(&bdrv_states, bs, list);
305 ea2384d3 bellard
    }
306 28a7282a Luiz Capitulino
    bdrv_iostatus_disable(bs);
307 d7d512f6 Paolo Bonzini
    notifier_list_init(&bs->close_notifiers);
308 d7d512f6 Paolo Bonzini
309 b338082b bellard
    return bs;
310 b338082b bellard
}
311 b338082b bellard
312 d7d512f6 Paolo Bonzini
void bdrv_add_close_notifier(BlockDriverState *bs, Notifier *notify)
313 d7d512f6 Paolo Bonzini
{
314 d7d512f6 Paolo Bonzini
    notifier_list_add(&bs->close_notifiers, notify);
315 d7d512f6 Paolo Bonzini
}
316 d7d512f6 Paolo Bonzini
317 ea2384d3 bellard
BlockDriver *bdrv_find_format(const char *format_name)
318 ea2384d3 bellard
{
319 ea2384d3 bellard
    BlockDriver *drv1;
320 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
321 8a22f02a Stefan Hajnoczi
        if (!strcmp(drv1->format_name, format_name)) {
322 ea2384d3 bellard
            return drv1;
323 8a22f02a Stefan Hajnoczi
        }
324 ea2384d3 bellard
    }
325 ea2384d3 bellard
    return NULL;
326 ea2384d3 bellard
}
327 ea2384d3 bellard
328 b64ec4e4 Fam Zheng
static int bdrv_is_whitelisted(BlockDriver *drv, bool read_only)
329 eb852011 Markus Armbruster
{
330 b64ec4e4 Fam Zheng
    static const char *whitelist_rw[] = {
331 b64ec4e4 Fam Zheng
        CONFIG_BDRV_RW_WHITELIST
332 b64ec4e4 Fam Zheng
    };
333 b64ec4e4 Fam Zheng
    static const char *whitelist_ro[] = {
334 b64ec4e4 Fam Zheng
        CONFIG_BDRV_RO_WHITELIST
335 eb852011 Markus Armbruster
    };
336 eb852011 Markus Armbruster
    const char **p;
337 eb852011 Markus Armbruster
338 b64ec4e4 Fam Zheng
    if (!whitelist_rw[0] && !whitelist_ro[0]) {
339 eb852011 Markus Armbruster
        return 1;               /* no whitelist, anything goes */
340 b64ec4e4 Fam Zheng
    }
341 eb852011 Markus Armbruster
342 b64ec4e4 Fam Zheng
    for (p = whitelist_rw; *p; p++) {
343 eb852011 Markus Armbruster
        if (!strcmp(drv->format_name, *p)) {
344 eb852011 Markus Armbruster
            return 1;
345 eb852011 Markus Armbruster
        }
346 eb852011 Markus Armbruster
    }
347 b64ec4e4 Fam Zheng
    if (read_only) {
348 b64ec4e4 Fam Zheng
        for (p = whitelist_ro; *p; p++) {
349 b64ec4e4 Fam Zheng
            if (!strcmp(drv->format_name, *p)) {
350 b64ec4e4 Fam Zheng
                return 1;
351 b64ec4e4 Fam Zheng
            }
352 b64ec4e4 Fam Zheng
        }
353 b64ec4e4 Fam Zheng
    }
354 eb852011 Markus Armbruster
    return 0;
355 eb852011 Markus Armbruster
}
356 eb852011 Markus Armbruster
357 b64ec4e4 Fam Zheng
BlockDriver *bdrv_find_whitelisted_format(const char *format_name,
358 b64ec4e4 Fam Zheng
                                          bool read_only)
359 eb852011 Markus Armbruster
{
360 eb852011 Markus Armbruster
    BlockDriver *drv = bdrv_find_format(format_name);
361 b64ec4e4 Fam Zheng
    return drv && bdrv_is_whitelisted(drv, read_only) ? drv : NULL;
362 eb852011 Markus Armbruster
}
363 eb852011 Markus Armbruster
364 5b7e1542 Zhi Yong Wu
typedef struct CreateCo {
365 5b7e1542 Zhi Yong Wu
    BlockDriver *drv;
366 5b7e1542 Zhi Yong Wu
    char *filename;
367 5b7e1542 Zhi Yong Wu
    QEMUOptionParameter *options;
368 5b7e1542 Zhi Yong Wu
    int ret;
369 5b7e1542 Zhi Yong Wu
} CreateCo;
370 5b7e1542 Zhi Yong Wu
371 5b7e1542 Zhi Yong Wu
static void coroutine_fn bdrv_create_co_entry(void *opaque)
372 5b7e1542 Zhi Yong Wu
{
373 5b7e1542 Zhi Yong Wu
    CreateCo *cco = opaque;
374 5b7e1542 Zhi Yong Wu
    assert(cco->drv);
375 5b7e1542 Zhi Yong Wu
376 5b7e1542 Zhi Yong Wu
    cco->ret = cco->drv->bdrv_create(cco->filename, cco->options);
377 5b7e1542 Zhi Yong Wu
}
378 5b7e1542 Zhi Yong Wu
379 0e7e1989 Kevin Wolf
int bdrv_create(BlockDriver *drv, const char* filename,
380 0e7e1989 Kevin Wolf
    QEMUOptionParameter *options)
381 ea2384d3 bellard
{
382 5b7e1542 Zhi Yong Wu
    int ret;
383 5b7e1542 Zhi Yong Wu
384 5b7e1542 Zhi Yong Wu
    Coroutine *co;
385 5b7e1542 Zhi Yong Wu
    CreateCo cco = {
386 5b7e1542 Zhi Yong Wu
        .drv = drv,
387 5b7e1542 Zhi Yong Wu
        .filename = g_strdup(filename),
388 5b7e1542 Zhi Yong Wu
        .options = options,
389 5b7e1542 Zhi Yong Wu
        .ret = NOT_DONE,
390 5b7e1542 Zhi Yong Wu
    };
391 5b7e1542 Zhi Yong Wu
392 5b7e1542 Zhi Yong Wu
    if (!drv->bdrv_create) {
393 80168bff Luiz Capitulino
        ret = -ENOTSUP;
394 80168bff Luiz Capitulino
        goto out;
395 5b7e1542 Zhi Yong Wu
    }
396 5b7e1542 Zhi Yong Wu
397 5b7e1542 Zhi Yong Wu
    if (qemu_in_coroutine()) {
398 5b7e1542 Zhi Yong Wu
        /* Fast-path if already in coroutine context */
399 5b7e1542 Zhi Yong Wu
        bdrv_create_co_entry(&cco);
400 5b7e1542 Zhi Yong Wu
    } else {
401 5b7e1542 Zhi Yong Wu
        co = qemu_coroutine_create(bdrv_create_co_entry);
402 5b7e1542 Zhi Yong Wu
        qemu_coroutine_enter(co, &cco);
403 5b7e1542 Zhi Yong Wu
        while (cco.ret == NOT_DONE) {
404 5b7e1542 Zhi Yong Wu
            qemu_aio_wait();
405 5b7e1542 Zhi Yong Wu
        }
406 5b7e1542 Zhi Yong Wu
    }
407 5b7e1542 Zhi Yong Wu
408 5b7e1542 Zhi Yong Wu
    ret = cco.ret;
409 0e7e1989 Kevin Wolf
410 80168bff Luiz Capitulino
out:
411 80168bff Luiz Capitulino
    g_free(cco.filename);
412 5b7e1542 Zhi Yong Wu
    return ret;
413 ea2384d3 bellard
}
414 ea2384d3 bellard
415 84a12e66 Christoph Hellwig
int bdrv_create_file(const char* filename, QEMUOptionParameter *options)
416 84a12e66 Christoph Hellwig
{
417 84a12e66 Christoph Hellwig
    BlockDriver *drv;
418 84a12e66 Christoph Hellwig
419 b50cbabc MORITA Kazutaka
    drv = bdrv_find_protocol(filename);
420 84a12e66 Christoph Hellwig
    if (drv == NULL) {
421 16905d71 Stefan Hajnoczi
        return -ENOENT;
422 84a12e66 Christoph Hellwig
    }
423 84a12e66 Christoph Hellwig
424 84a12e66 Christoph Hellwig
    return bdrv_create(drv, filename, options);
425 84a12e66 Christoph Hellwig
}
426 84a12e66 Christoph Hellwig
427 eba25057 Jim Meyering
/*
428 eba25057 Jim Meyering
 * Create a uniquely-named empty temporary file.
429 eba25057 Jim Meyering
 * Return 0 upon success, otherwise a negative errno value.
430 eba25057 Jim Meyering
 */
431 eba25057 Jim Meyering
int get_tmp_filename(char *filename, int size)
432 d5249393 bellard
{
433 eba25057 Jim Meyering
#ifdef _WIN32
434 3b9f94e1 bellard
    char temp_dir[MAX_PATH];
435 eba25057 Jim Meyering
    /* GetTempFileName requires that its output buffer (4th param)
436 eba25057 Jim Meyering
       have length MAX_PATH or greater.  */
437 eba25057 Jim Meyering
    assert(size >= MAX_PATH);
438 eba25057 Jim Meyering
    return (GetTempPath(MAX_PATH, temp_dir)
439 eba25057 Jim Meyering
            && GetTempFileName(temp_dir, "qem", 0, filename)
440 eba25057 Jim Meyering
            ? 0 : -GetLastError());
441 d5249393 bellard
#else
442 67b915a5 bellard
    int fd;
443 7ccfb2eb blueswir1
    const char *tmpdir;
444 0badc1ee aurel32
    tmpdir = getenv("TMPDIR");
445 0badc1ee aurel32
    if (!tmpdir)
446 0badc1ee aurel32
        tmpdir = "/tmp";
447 eba25057 Jim Meyering
    if (snprintf(filename, size, "%s/vl.XXXXXX", tmpdir) >= size) {
448 eba25057 Jim Meyering
        return -EOVERFLOW;
449 eba25057 Jim Meyering
    }
450 ea2384d3 bellard
    fd = mkstemp(filename);
451 fe235a06 Dunrong Huang
    if (fd < 0) {
452 fe235a06 Dunrong Huang
        return -errno;
453 fe235a06 Dunrong Huang
    }
454 fe235a06 Dunrong Huang
    if (close(fd) != 0) {
455 fe235a06 Dunrong Huang
        unlink(filename);
456 eba25057 Jim Meyering
        return -errno;
457 eba25057 Jim Meyering
    }
458 eba25057 Jim Meyering
    return 0;
459 d5249393 bellard
#endif
460 eba25057 Jim Meyering
}
461 fc01f7e7 bellard
462 84a12e66 Christoph Hellwig
/*
463 84a12e66 Christoph Hellwig
 * Detect host devices. By convention, /dev/cdrom[N] is always
464 84a12e66 Christoph Hellwig
 * recognized as a host CDROM.
465 84a12e66 Christoph Hellwig
 */
466 84a12e66 Christoph Hellwig
static BlockDriver *find_hdev_driver(const char *filename)
467 84a12e66 Christoph Hellwig
{
468 84a12e66 Christoph Hellwig
    int score_max = 0, score;
469 84a12e66 Christoph Hellwig
    BlockDriver *drv = NULL, *d;
470 84a12e66 Christoph Hellwig
471 84a12e66 Christoph Hellwig
    QLIST_FOREACH(d, &bdrv_drivers, list) {
472 84a12e66 Christoph Hellwig
        if (d->bdrv_probe_device) {
473 84a12e66 Christoph Hellwig
            score = d->bdrv_probe_device(filename);
474 84a12e66 Christoph Hellwig
            if (score > score_max) {
475 84a12e66 Christoph Hellwig
                score_max = score;
476 84a12e66 Christoph Hellwig
                drv = d;
477 84a12e66 Christoph Hellwig
            }
478 84a12e66 Christoph Hellwig
        }
479 84a12e66 Christoph Hellwig
    }
480 84a12e66 Christoph Hellwig
481 84a12e66 Christoph Hellwig
    return drv;
482 84a12e66 Christoph Hellwig
}
483 84a12e66 Christoph Hellwig
484 b50cbabc MORITA Kazutaka
BlockDriver *bdrv_find_protocol(const char *filename)
485 83f64091 bellard
{
486 83f64091 bellard
    BlockDriver *drv1;
487 83f64091 bellard
    char protocol[128];
488 1cec71e3 Anthony Liguori
    int len;
489 83f64091 bellard
    const char *p;
490 19cb3738 bellard
491 66f82cee Kevin Wolf
    /* TODO Drivers without bdrv_file_open must be specified explicitly */
492 66f82cee Kevin Wolf
493 39508e7a Christoph Hellwig
    /*
494 39508e7a Christoph Hellwig
     * XXX(hch): we really should not let host device detection
495 39508e7a Christoph Hellwig
     * override an explicit protocol specification, but moving this
496 39508e7a Christoph Hellwig
     * later breaks access to device names with colons in them.
497 39508e7a Christoph Hellwig
     * Thanks to the brain-dead persistent naming schemes on udev-
498 39508e7a Christoph Hellwig
     * based Linux systems those actually are quite common.
499 39508e7a Christoph Hellwig
     */
500 39508e7a Christoph Hellwig
    drv1 = find_hdev_driver(filename);
501 39508e7a Christoph Hellwig
    if (drv1) {
502 39508e7a Christoph Hellwig
        return drv1;
503 39508e7a Christoph Hellwig
    }
504 39508e7a Christoph Hellwig
505 9e0b22f4 Stefan Hajnoczi
    if (!path_has_protocol(filename)) {
506 39508e7a Christoph Hellwig
        return bdrv_find_format("file");
507 84a12e66 Christoph Hellwig
    }
508 9e0b22f4 Stefan Hajnoczi
    p = strchr(filename, ':');
509 9e0b22f4 Stefan Hajnoczi
    assert(p != NULL);
510 1cec71e3 Anthony Liguori
    len = p - filename;
511 1cec71e3 Anthony Liguori
    if (len > sizeof(protocol) - 1)
512 1cec71e3 Anthony Liguori
        len = sizeof(protocol) - 1;
513 1cec71e3 Anthony Liguori
    memcpy(protocol, filename, len);
514 1cec71e3 Anthony Liguori
    protocol[len] = '\0';
515 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
516 5fafdf24 ths
        if (drv1->protocol_name &&
517 8a22f02a Stefan Hajnoczi
            !strcmp(drv1->protocol_name, protocol)) {
518 83f64091 bellard
            return drv1;
519 8a22f02a Stefan Hajnoczi
        }
520 83f64091 bellard
    }
521 83f64091 bellard
    return NULL;
522 83f64091 bellard
}
523 83f64091 bellard
524 f500a6d3 Kevin Wolf
static int find_image_format(BlockDriverState *bs, const char *filename,
525 f500a6d3 Kevin Wolf
                             BlockDriver **pdrv)
526 f3a5d3f8 Christoph Hellwig
{
527 f500a6d3 Kevin Wolf
    int score, score_max;
528 f3a5d3f8 Christoph Hellwig
    BlockDriver *drv1, *drv;
529 f3a5d3f8 Christoph Hellwig
    uint8_t buf[2048];
530 f500a6d3 Kevin Wolf
    int ret = 0;
531 f8ea0b00 Nicholas Bellinger
532 08a00559 Kevin Wolf
    /* Return the raw BlockDriver * to scsi-generic devices or empty drives */
533 8e895599 Paolo Bonzini
    if (bs->sg || !bdrv_is_inserted(bs) || bdrv_getlength(bs) == 0) {
534 c98ac35d Stefan Weil
        drv = bdrv_find_format("raw");
535 c98ac35d Stefan Weil
        if (!drv) {
536 c98ac35d Stefan Weil
            ret = -ENOENT;
537 c98ac35d Stefan Weil
        }
538 c98ac35d Stefan Weil
        *pdrv = drv;
539 c98ac35d Stefan Weil
        return ret;
540 1a396859 Nicholas A. Bellinger
    }
541 f8ea0b00 Nicholas Bellinger
542 83f64091 bellard
    ret = bdrv_pread(bs, 0, buf, sizeof(buf));
543 83f64091 bellard
    if (ret < 0) {
544 c98ac35d Stefan Weil
        *pdrv = NULL;
545 c98ac35d Stefan Weil
        return ret;
546 83f64091 bellard
    }
547 83f64091 bellard
548 ea2384d3 bellard
    score_max = 0;
549 84a12e66 Christoph Hellwig
    drv = NULL;
550 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv1, &bdrv_drivers, list) {
551 83f64091 bellard
        if (drv1->bdrv_probe) {
552 83f64091 bellard
            score = drv1->bdrv_probe(buf, ret, filename);
553 83f64091 bellard
            if (score > score_max) {
554 83f64091 bellard
                score_max = score;
555 83f64091 bellard
                drv = drv1;
556 83f64091 bellard
            }
557 0849bf08 bellard
        }
558 fc01f7e7 bellard
    }
559 c98ac35d Stefan Weil
    if (!drv) {
560 c98ac35d Stefan Weil
        ret = -ENOENT;
561 c98ac35d Stefan Weil
    }
562 c98ac35d Stefan Weil
    *pdrv = drv;
563 c98ac35d Stefan Weil
    return ret;
564 ea2384d3 bellard
}
565 ea2384d3 bellard
566 51762288 Stefan Hajnoczi
/**
567 51762288 Stefan Hajnoczi
 * Set the current 'total_sectors' value
568 51762288 Stefan Hajnoczi
 */
569 51762288 Stefan Hajnoczi
static int refresh_total_sectors(BlockDriverState *bs, int64_t hint)
570 51762288 Stefan Hajnoczi
{
571 51762288 Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
572 51762288 Stefan Hajnoczi
573 396759ad Nicholas Bellinger
    /* Do not attempt drv->bdrv_getlength() on scsi-generic devices */
574 396759ad Nicholas Bellinger
    if (bs->sg)
575 396759ad Nicholas Bellinger
        return 0;
576 396759ad Nicholas Bellinger
577 51762288 Stefan Hajnoczi
    /* query actual device if possible, otherwise just trust the hint */
578 51762288 Stefan Hajnoczi
    if (drv->bdrv_getlength) {
579 51762288 Stefan Hajnoczi
        int64_t length = drv->bdrv_getlength(bs);
580 51762288 Stefan Hajnoczi
        if (length < 0) {
581 51762288 Stefan Hajnoczi
            return length;
582 51762288 Stefan Hajnoczi
        }
583 51762288 Stefan Hajnoczi
        hint = length >> BDRV_SECTOR_BITS;
584 51762288 Stefan Hajnoczi
    }
585 51762288 Stefan Hajnoczi
586 51762288 Stefan Hajnoczi
    bs->total_sectors = hint;
587 51762288 Stefan Hajnoczi
    return 0;
588 51762288 Stefan Hajnoczi
}
589 51762288 Stefan Hajnoczi
590 c3993cdc Stefan Hajnoczi
/**
591 9e8f1835 Paolo Bonzini
 * Set open flags for a given discard mode
592 9e8f1835 Paolo Bonzini
 *
593 9e8f1835 Paolo Bonzini
 * Return 0 on success, -1 if the discard mode was invalid.
594 9e8f1835 Paolo Bonzini
 */
595 9e8f1835 Paolo Bonzini
int bdrv_parse_discard_flags(const char *mode, int *flags)
596 9e8f1835 Paolo Bonzini
{
597 9e8f1835 Paolo Bonzini
    *flags &= ~BDRV_O_UNMAP;
598 9e8f1835 Paolo Bonzini
599 9e8f1835 Paolo Bonzini
    if (!strcmp(mode, "off") || !strcmp(mode, "ignore")) {
600 9e8f1835 Paolo Bonzini
        /* do nothing */
601 9e8f1835 Paolo Bonzini
    } else if (!strcmp(mode, "on") || !strcmp(mode, "unmap")) {
602 9e8f1835 Paolo Bonzini
        *flags |= BDRV_O_UNMAP;
603 9e8f1835 Paolo Bonzini
    } else {
604 9e8f1835 Paolo Bonzini
        return -1;
605 9e8f1835 Paolo Bonzini
    }
606 9e8f1835 Paolo Bonzini
607 9e8f1835 Paolo Bonzini
    return 0;
608 9e8f1835 Paolo Bonzini
}
609 9e8f1835 Paolo Bonzini
610 9e8f1835 Paolo Bonzini
/**
611 c3993cdc Stefan Hajnoczi
 * Set open flags for a given cache mode
612 c3993cdc Stefan Hajnoczi
 *
613 c3993cdc Stefan Hajnoczi
 * Return 0 on success, -1 if the cache mode was invalid.
614 c3993cdc Stefan Hajnoczi
 */
615 c3993cdc Stefan Hajnoczi
int bdrv_parse_cache_flags(const char *mode, int *flags)
616 c3993cdc Stefan Hajnoczi
{
617 c3993cdc Stefan Hajnoczi
    *flags &= ~BDRV_O_CACHE_MASK;
618 c3993cdc Stefan Hajnoczi
619 c3993cdc Stefan Hajnoczi
    if (!strcmp(mode, "off") || !strcmp(mode, "none")) {
620 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
621 92196b2f Stefan Hajnoczi
    } else if (!strcmp(mode, "directsync")) {
622 92196b2f Stefan Hajnoczi
        *flags |= BDRV_O_NOCACHE;
623 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "writeback")) {
624 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_CACHE_WB;
625 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "unsafe")) {
626 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_CACHE_WB;
627 c3993cdc Stefan Hajnoczi
        *flags |= BDRV_O_NO_FLUSH;
628 c3993cdc Stefan Hajnoczi
    } else if (!strcmp(mode, "writethrough")) {
629 c3993cdc Stefan Hajnoczi
        /* this is the default */
630 c3993cdc Stefan Hajnoczi
    } else {
631 c3993cdc Stefan Hajnoczi
        return -1;
632 c3993cdc Stefan Hajnoczi
    }
633 c3993cdc Stefan Hajnoczi
634 c3993cdc Stefan Hajnoczi
    return 0;
635 c3993cdc Stefan Hajnoczi
}
636 c3993cdc Stefan Hajnoczi
637 53fec9d3 Stefan Hajnoczi
/**
638 53fec9d3 Stefan Hajnoczi
 * The copy-on-read flag is actually a reference count so multiple users may
639 53fec9d3 Stefan Hajnoczi
 * use the feature without worrying about clobbering its previous state.
640 53fec9d3 Stefan Hajnoczi
 * Copy-on-read stays enabled until all users have called to disable it.
641 53fec9d3 Stefan Hajnoczi
 */
642 53fec9d3 Stefan Hajnoczi
void bdrv_enable_copy_on_read(BlockDriverState *bs)
643 53fec9d3 Stefan Hajnoczi
{
644 53fec9d3 Stefan Hajnoczi
    bs->copy_on_read++;
645 53fec9d3 Stefan Hajnoczi
}
646 53fec9d3 Stefan Hajnoczi
647 53fec9d3 Stefan Hajnoczi
void bdrv_disable_copy_on_read(BlockDriverState *bs)
648 53fec9d3 Stefan Hajnoczi
{
649 53fec9d3 Stefan Hajnoczi
    assert(bs->copy_on_read > 0);
650 53fec9d3 Stefan Hajnoczi
    bs->copy_on_read--;
651 53fec9d3 Stefan Hajnoczi
}
652 53fec9d3 Stefan Hajnoczi
653 7b272452 Kevin Wolf
static int bdrv_open_flags(BlockDriverState *bs, int flags)
654 7b272452 Kevin Wolf
{
655 7b272452 Kevin Wolf
    int open_flags = flags | BDRV_O_CACHE_WB;
656 7b272452 Kevin Wolf
657 7b272452 Kevin Wolf
    /*
658 7b272452 Kevin Wolf
     * Clear flags that are internal to the block layer before opening the
659 7b272452 Kevin Wolf
     * image.
660 7b272452 Kevin Wolf
     */
661 7b272452 Kevin Wolf
    open_flags &= ~(BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
662 7b272452 Kevin Wolf
663 7b272452 Kevin Wolf
    /*
664 7b272452 Kevin Wolf
     * Snapshots should be writable.
665 7b272452 Kevin Wolf
     */
666 7b272452 Kevin Wolf
    if (bs->is_temporary) {
667 7b272452 Kevin Wolf
        open_flags |= BDRV_O_RDWR;
668 7b272452 Kevin Wolf
    }
669 7b272452 Kevin Wolf
670 7b272452 Kevin Wolf
    return open_flags;
671 7b272452 Kevin Wolf
}
672 7b272452 Kevin Wolf
673 b6ce07aa Kevin Wolf
/*
674 57915332 Kevin Wolf
 * Common part for opening disk images and files
675 b6ad491a Kevin Wolf
 *
676 b6ad491a Kevin Wolf
 * Removes all processed options from *options.
677 57915332 Kevin Wolf
 */
678 f500a6d3 Kevin Wolf
static int bdrv_open_common(BlockDriverState *bs, BlockDriverState *file,
679 035fccdf Kevin Wolf
    QDict *options, int flags, BlockDriver *drv)
680 57915332 Kevin Wolf
{
681 57915332 Kevin Wolf
    int ret, open_flags;
682 035fccdf Kevin Wolf
    const char *filename;
683 57915332 Kevin Wolf
684 57915332 Kevin Wolf
    assert(drv != NULL);
685 6405875c Paolo Bonzini
    assert(bs->file == NULL);
686 707ff828 Kevin Wolf
    assert(options != NULL && bs->options != options);
687 57915332 Kevin Wolf
688 45673671 Kevin Wolf
    if (file != NULL) {
689 45673671 Kevin Wolf
        filename = file->filename;
690 45673671 Kevin Wolf
    } else {
691 45673671 Kevin Wolf
        filename = qdict_get_try_str(options, "filename");
692 45673671 Kevin Wolf
    }
693 45673671 Kevin Wolf
694 45673671 Kevin Wolf
    trace_bdrv_open_common(bs, filename ?: "", flags, drv->format_name);
695 28dcee10 Stefan Hajnoczi
696 5d186eb0 Kevin Wolf
    /* bdrv_open() with directly using a protocol as drv. This layer is already
697 5d186eb0 Kevin Wolf
     * opened, so assign it to bs (while file becomes a closed BlockDriverState)
698 5d186eb0 Kevin Wolf
     * and return immediately. */
699 5d186eb0 Kevin Wolf
    if (file != NULL && drv->bdrv_file_open) {
700 5d186eb0 Kevin Wolf
        bdrv_swap(file, bs);
701 5d186eb0 Kevin Wolf
        return 0;
702 5d186eb0 Kevin Wolf
    }
703 5d186eb0 Kevin Wolf
704 57915332 Kevin Wolf
    bs->open_flags = flags;
705 57915332 Kevin Wolf
    bs->buffer_alignment = 512;
706 b64ec4e4 Fam Zheng
    open_flags = bdrv_open_flags(bs, flags);
707 b64ec4e4 Fam Zheng
    bs->read_only = !(open_flags & BDRV_O_RDWR);
708 b64ec4e4 Fam Zheng
709 b64ec4e4 Fam Zheng
    if (use_bdrv_whitelist && !bdrv_is_whitelisted(drv, bs->read_only)) {
710 b64ec4e4 Fam Zheng
        return -ENOTSUP;
711 b64ec4e4 Fam Zheng
    }
712 57915332 Kevin Wolf
713 53fec9d3 Stefan Hajnoczi
    assert(bs->copy_on_read == 0); /* bdrv_new() and bdrv_close() make it so */
714 b64ec4e4 Fam Zheng
    if (!bs->read_only && (flags & BDRV_O_COPY_ON_READ)) {
715 53fec9d3 Stefan Hajnoczi
        bdrv_enable_copy_on_read(bs);
716 53fec9d3 Stefan Hajnoczi
    }
717 53fec9d3 Stefan Hajnoczi
718 c2ad1b0c Kevin Wolf
    if (filename != NULL) {
719 c2ad1b0c Kevin Wolf
        pstrcpy(bs->filename, sizeof(bs->filename), filename);
720 c2ad1b0c Kevin Wolf
    } else {
721 c2ad1b0c Kevin Wolf
        bs->filename[0] = '\0';
722 c2ad1b0c Kevin Wolf
    }
723 57915332 Kevin Wolf
724 57915332 Kevin Wolf
    bs->drv = drv;
725 7267c094 Anthony Liguori
    bs->opaque = g_malloc0(drv->instance_size);
726 57915332 Kevin Wolf
727 03f541bd Stefan Hajnoczi
    bs->enable_write_cache = !!(flags & BDRV_O_CACHE_WB);
728 e7c63796 Stefan Hajnoczi
729 66f82cee Kevin Wolf
    /* Open the image, either directly or using a protocol */
730 66f82cee Kevin Wolf
    if (drv->bdrv_file_open) {
731 5d186eb0 Kevin Wolf
        assert(file == NULL);
732 5d186eb0 Kevin Wolf
        assert(drv->bdrv_parse_filename || filename != NULL);
733 56d1b4d2 Kevin Wolf
        ret = drv->bdrv_file_open(bs, options, open_flags);
734 f500a6d3 Kevin Wolf
    } else {
735 2af5ef70 Kevin Wolf
        if (file == NULL) {
736 2af5ef70 Kevin Wolf
            qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't use '%s' as a "
737 2af5ef70 Kevin Wolf
                          "block driver for the protocol level",
738 2af5ef70 Kevin Wolf
                          drv->format_name);
739 2af5ef70 Kevin Wolf
            ret = -EINVAL;
740 2af5ef70 Kevin Wolf
            goto free_and_fail;
741 2af5ef70 Kevin Wolf
        }
742 f500a6d3 Kevin Wolf
        assert(file != NULL);
743 f500a6d3 Kevin Wolf
        bs->file = file;
744 b6ad491a Kevin Wolf
        ret = drv->bdrv_open(bs, options, open_flags);
745 66f82cee Kevin Wolf
    }
746 66f82cee Kevin Wolf
747 57915332 Kevin Wolf
    if (ret < 0) {
748 57915332 Kevin Wolf
        goto free_and_fail;
749 57915332 Kevin Wolf
    }
750 57915332 Kevin Wolf
751 51762288 Stefan Hajnoczi
    ret = refresh_total_sectors(bs, bs->total_sectors);
752 51762288 Stefan Hajnoczi
    if (ret < 0) {
753 51762288 Stefan Hajnoczi
        goto free_and_fail;
754 57915332 Kevin Wolf
    }
755 51762288 Stefan Hajnoczi
756 57915332 Kevin Wolf
#ifndef _WIN32
757 57915332 Kevin Wolf
    if (bs->is_temporary) {
758 c2ad1b0c Kevin Wolf
        assert(filename != NULL);
759 57915332 Kevin Wolf
        unlink(filename);
760 57915332 Kevin Wolf
    }
761 57915332 Kevin Wolf
#endif
762 57915332 Kevin Wolf
    return 0;
763 57915332 Kevin Wolf
764 57915332 Kevin Wolf
free_and_fail:
765 f500a6d3 Kevin Wolf
    bs->file = NULL;
766 7267c094 Anthony Liguori
    g_free(bs->opaque);
767 57915332 Kevin Wolf
    bs->opaque = NULL;
768 57915332 Kevin Wolf
    bs->drv = NULL;
769 57915332 Kevin Wolf
    return ret;
770 57915332 Kevin Wolf
}
771 57915332 Kevin Wolf
772 57915332 Kevin Wolf
/*
773 b6ce07aa Kevin Wolf
 * Opens a file using a protocol (file, host_device, nbd, ...)
774 787e4a85 Kevin Wolf
 *
775 787e4a85 Kevin Wolf
 * options is a QDict of options to pass to the block drivers, or NULL for an
776 787e4a85 Kevin Wolf
 * empty set of options. The reference to the QDict belongs to the block layer
777 787e4a85 Kevin Wolf
 * after the call (even on failure), so if the caller intends to reuse the
778 787e4a85 Kevin Wolf
 * dictionary, it needs to use QINCREF() before calling bdrv_file_open.
779 b6ce07aa Kevin Wolf
 */
780 787e4a85 Kevin Wolf
int bdrv_file_open(BlockDriverState **pbs, const char *filename,
781 787e4a85 Kevin Wolf
                   QDict *options, int flags)
782 ea2384d3 bellard
{
783 83f64091 bellard
    BlockDriverState *bs;
784 6db95603 Christoph Hellwig
    BlockDriver *drv;
785 c2ad1b0c Kevin Wolf
    const char *drvname;
786 83f64091 bellard
    int ret;
787 83f64091 bellard
788 707ff828 Kevin Wolf
    /* NULL means an empty set of options */
789 707ff828 Kevin Wolf
    if (options == NULL) {
790 707ff828 Kevin Wolf
        options = qdict_new();
791 707ff828 Kevin Wolf
    }
792 707ff828 Kevin Wolf
793 83f64091 bellard
    bs = bdrv_new("");
794 707ff828 Kevin Wolf
    bs->options = options;
795 707ff828 Kevin Wolf
    options = qdict_clone_shallow(options);
796 707ff828 Kevin Wolf
797 035fccdf Kevin Wolf
    /* Fetch the file name from the options QDict if necessary */
798 035fccdf Kevin Wolf
    if (!filename) {
799 035fccdf Kevin Wolf
        filename = qdict_get_try_str(options, "filename");
800 035fccdf Kevin Wolf
    } else if (filename && !qdict_haskey(options, "filename")) {
801 035fccdf Kevin Wolf
        qdict_put(options, "filename", qstring_from_str(filename));
802 035fccdf Kevin Wolf
    } else {
803 035fccdf Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Can't specify 'file' and "
804 035fccdf Kevin Wolf
                      "'filename' options at the same time");
805 035fccdf Kevin Wolf
        ret = -EINVAL;
806 035fccdf Kevin Wolf
        goto fail;
807 035fccdf Kevin Wolf
    }
808 035fccdf Kevin Wolf
809 c2ad1b0c Kevin Wolf
    /* Find the right block driver */
810 c2ad1b0c Kevin Wolf
    drvname = qdict_get_try_str(options, "driver");
811 c2ad1b0c Kevin Wolf
    if (drvname) {
812 b64ec4e4 Fam Zheng
        drv = bdrv_find_whitelisted_format(drvname, !(flags & BDRV_O_RDWR));
813 c2ad1b0c Kevin Wolf
        qdict_del(options, "driver");
814 c2ad1b0c Kevin Wolf
    } else if (filename) {
815 c2ad1b0c Kevin Wolf
        drv = bdrv_find_protocol(filename);
816 c2ad1b0c Kevin Wolf
    } else {
817 c2ad1b0c Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
818 c2ad1b0c Kevin Wolf
                      "Must specify either driver or file");
819 c2ad1b0c Kevin Wolf
        drv = NULL;
820 c2ad1b0c Kevin Wolf
    }
821 c2ad1b0c Kevin Wolf
822 c2ad1b0c Kevin Wolf
    if (!drv) {
823 c2ad1b0c Kevin Wolf
        ret = -ENOENT;
824 c2ad1b0c Kevin Wolf
        goto fail;
825 c2ad1b0c Kevin Wolf
    }
826 c2ad1b0c Kevin Wolf
827 c2ad1b0c Kevin Wolf
    /* Parse the filename and open it */
828 c2ad1b0c Kevin Wolf
    if (drv->bdrv_parse_filename && filename) {
829 6963a30d Kevin Wolf
        Error *local_err = NULL;
830 6963a30d Kevin Wolf
        drv->bdrv_parse_filename(filename, options, &local_err);
831 6963a30d Kevin Wolf
        if (error_is_set(&local_err)) {
832 6963a30d Kevin Wolf
            qerror_report_err(local_err);
833 6963a30d Kevin Wolf
            error_free(local_err);
834 6963a30d Kevin Wolf
            ret = -EINVAL;
835 6963a30d Kevin Wolf
            goto fail;
836 6963a30d Kevin Wolf
        }
837 56d1b4d2 Kevin Wolf
        qdict_del(options, "filename");
838 c2ad1b0c Kevin Wolf
    } else if (!drv->bdrv_parse_filename && !filename) {
839 c2ad1b0c Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR,
840 c2ad1b0c Kevin Wolf
                      "The '%s' block driver requires a file name",
841 c2ad1b0c Kevin Wolf
                      drv->format_name);
842 c2ad1b0c Kevin Wolf
        ret = -EINVAL;
843 c2ad1b0c Kevin Wolf
        goto fail;
844 6963a30d Kevin Wolf
    }
845 6963a30d Kevin Wolf
846 035fccdf Kevin Wolf
    ret = bdrv_open_common(bs, NULL, options, flags, drv);
847 83f64091 bellard
    if (ret < 0) {
848 707ff828 Kevin Wolf
        goto fail;
849 707ff828 Kevin Wolf
    }
850 707ff828 Kevin Wolf
851 707ff828 Kevin Wolf
    /* Check if any unknown options were used */
852 707ff828 Kevin Wolf
    if (qdict_size(options) != 0) {
853 707ff828 Kevin Wolf
        const QDictEntry *entry = qdict_first(options);
854 707ff828 Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Block protocol '%s' doesn't "
855 707ff828 Kevin Wolf
                      "support the option '%s'",
856 707ff828 Kevin Wolf
                      drv->format_name, entry->key);
857 707ff828 Kevin Wolf
        ret = -EINVAL;
858 707ff828 Kevin Wolf
        goto fail;
859 3b0d4f61 bellard
    }
860 707ff828 Kevin Wolf
    QDECREF(options);
861 707ff828 Kevin Wolf
862 71d0770c aliguori
    bs->growable = 1;
863 83f64091 bellard
    *pbs = bs;
864 83f64091 bellard
    return 0;
865 707ff828 Kevin Wolf
866 707ff828 Kevin Wolf
fail:
867 707ff828 Kevin Wolf
    QDECREF(options);
868 707ff828 Kevin Wolf
    if (!bs->drv) {
869 707ff828 Kevin Wolf
        QDECREF(bs->options);
870 707ff828 Kevin Wolf
    }
871 707ff828 Kevin Wolf
    bdrv_delete(bs);
872 707ff828 Kevin Wolf
    return ret;
873 83f64091 bellard
}
874 83f64091 bellard
875 31ca6d07 Kevin Wolf
/*
876 31ca6d07 Kevin Wolf
 * Opens the backing file for a BlockDriverState if not yet open
877 31ca6d07 Kevin Wolf
 *
878 31ca6d07 Kevin Wolf
 * options is a QDict of options to pass to the block drivers, or NULL for an
879 31ca6d07 Kevin Wolf
 * empty set of options. The reference to the QDict is transferred to this
880 31ca6d07 Kevin Wolf
 * function (even on failure), so if the caller intends to reuse the dictionary,
881 31ca6d07 Kevin Wolf
 * it needs to use QINCREF() before calling bdrv_file_open.
882 31ca6d07 Kevin Wolf
 */
883 31ca6d07 Kevin Wolf
int bdrv_open_backing_file(BlockDriverState *bs, QDict *options)
884 9156df12 Paolo Bonzini
{
885 9156df12 Paolo Bonzini
    char backing_filename[PATH_MAX];
886 9156df12 Paolo Bonzini
    int back_flags, ret;
887 9156df12 Paolo Bonzini
    BlockDriver *back_drv = NULL;
888 9156df12 Paolo Bonzini
889 9156df12 Paolo Bonzini
    if (bs->backing_hd != NULL) {
890 31ca6d07 Kevin Wolf
        QDECREF(options);
891 9156df12 Paolo Bonzini
        return 0;
892 9156df12 Paolo Bonzini
    }
893 9156df12 Paolo Bonzini
894 31ca6d07 Kevin Wolf
    /* NULL means an empty set of options */
895 31ca6d07 Kevin Wolf
    if (options == NULL) {
896 31ca6d07 Kevin Wolf
        options = qdict_new();
897 31ca6d07 Kevin Wolf
    }
898 31ca6d07 Kevin Wolf
899 9156df12 Paolo Bonzini
    bs->open_flags &= ~BDRV_O_NO_BACKING;
900 1cb6f506 Kevin Wolf
    if (qdict_haskey(options, "file.filename")) {
901 1cb6f506 Kevin Wolf
        backing_filename[0] = '\0';
902 1cb6f506 Kevin Wolf
    } else if (bs->backing_file[0] == '\0' && qdict_size(options) == 0) {
903 31ca6d07 Kevin Wolf
        QDECREF(options);
904 9156df12 Paolo Bonzini
        return 0;
905 9156df12 Paolo Bonzini
    }
906 9156df12 Paolo Bonzini
907 9156df12 Paolo Bonzini
    bs->backing_hd = bdrv_new("");
908 9156df12 Paolo Bonzini
    bdrv_get_full_backing_filename(bs, backing_filename,
909 9156df12 Paolo Bonzini
                                   sizeof(backing_filename));
910 9156df12 Paolo Bonzini
911 9156df12 Paolo Bonzini
    if (bs->backing_format[0] != '\0') {
912 9156df12 Paolo Bonzini
        back_drv = bdrv_find_format(bs->backing_format);
913 9156df12 Paolo Bonzini
    }
914 9156df12 Paolo Bonzini
915 9156df12 Paolo Bonzini
    /* backing files always opened read-only */
916 9156df12 Paolo Bonzini
    back_flags = bs->open_flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT);
917 9156df12 Paolo Bonzini
918 31ca6d07 Kevin Wolf
    ret = bdrv_open(bs->backing_hd,
919 31ca6d07 Kevin Wolf
                    *backing_filename ? backing_filename : NULL, options,
920 de9c0cec Kevin Wolf
                    back_flags, back_drv);
921 9156df12 Paolo Bonzini
    if (ret < 0) {
922 9156df12 Paolo Bonzini
        bdrv_delete(bs->backing_hd);
923 9156df12 Paolo Bonzini
        bs->backing_hd = NULL;
924 9156df12 Paolo Bonzini
        bs->open_flags |= BDRV_O_NO_BACKING;
925 9156df12 Paolo Bonzini
        return ret;
926 9156df12 Paolo Bonzini
    }
927 9156df12 Paolo Bonzini
    return 0;
928 9156df12 Paolo Bonzini
}
929 9156df12 Paolo Bonzini
930 707ff828 Kevin Wolf
static void extract_subqdict(QDict *src, QDict **dst, const char *start)
931 707ff828 Kevin Wolf
{
932 707ff828 Kevin Wolf
    const QDictEntry *entry, *next;
933 707ff828 Kevin Wolf
    const char *p;
934 707ff828 Kevin Wolf
935 707ff828 Kevin Wolf
    *dst = qdict_new();
936 707ff828 Kevin Wolf
    entry = qdict_first(src);
937 707ff828 Kevin Wolf
938 707ff828 Kevin Wolf
    while (entry != NULL) {
939 707ff828 Kevin Wolf
        next = qdict_next(src, entry);
940 707ff828 Kevin Wolf
        if (strstart(entry->key, start, &p)) {
941 707ff828 Kevin Wolf
            qobject_incref(entry->value);
942 707ff828 Kevin Wolf
            qdict_put_obj(*dst, p, entry->value);
943 707ff828 Kevin Wolf
            qdict_del(src, entry->key);
944 707ff828 Kevin Wolf
        }
945 707ff828 Kevin Wolf
        entry = next;
946 707ff828 Kevin Wolf
    }
947 707ff828 Kevin Wolf
}
948 707ff828 Kevin Wolf
949 b6ce07aa Kevin Wolf
/*
950 b6ce07aa Kevin Wolf
 * Opens a disk image (raw, qcow2, vmdk, ...)
951 de9c0cec Kevin Wolf
 *
952 de9c0cec Kevin Wolf
 * options is a QDict of options to pass to the block drivers, or NULL for an
953 de9c0cec Kevin Wolf
 * empty set of options. The reference to the QDict belongs to the block layer
954 de9c0cec Kevin Wolf
 * after the call (even on failure), so if the caller intends to reuse the
955 de9c0cec Kevin Wolf
 * dictionary, it needs to use QINCREF() before calling bdrv_open.
956 b6ce07aa Kevin Wolf
 */
957 de9c0cec Kevin Wolf
int bdrv_open(BlockDriverState *bs, const char *filename, QDict *options,
958 de9c0cec Kevin Wolf
              int flags, BlockDriver *drv)
959 ea2384d3 bellard
{
960 b6ce07aa Kevin Wolf
    int ret;
961 89c9bc3d Stefan Weil
    /* TODO: extra byte is a hack to ensure MAX_PATH space on Windows. */
962 89c9bc3d Stefan Weil
    char tmp_filename[PATH_MAX + 1];
963 f500a6d3 Kevin Wolf
    BlockDriverState *file = NULL;
964 707ff828 Kevin Wolf
    QDict *file_options = NULL;
965 712e7874 bellard
966 de9c0cec Kevin Wolf
    /* NULL means an empty set of options */
967 de9c0cec Kevin Wolf
    if (options == NULL) {
968 de9c0cec Kevin Wolf
        options = qdict_new();
969 de9c0cec Kevin Wolf
    }
970 de9c0cec Kevin Wolf
971 de9c0cec Kevin Wolf
    bs->options = options;
972 b6ad491a Kevin Wolf
    options = qdict_clone_shallow(options);
973 de9c0cec Kevin Wolf
974 de9c0cec Kevin Wolf
    /* For snapshot=on, create a temporary qcow2 overlay */
975 83f64091 bellard
    if (flags & BDRV_O_SNAPSHOT) {
976 ea2384d3 bellard
        BlockDriverState *bs1;
977 ea2384d3 bellard
        int64_t total_size;
978 91a073a9 Kevin Wolf
        BlockDriver *bdrv_qcow2;
979 08b392e1 Kevin Wolf
        QEMUOptionParameter *create_options;
980 b6ce07aa Kevin Wolf
        char backing_filename[PATH_MAX];
981 3b46e624 ths
982 c2ad1b0c Kevin Wolf
        if (qdict_size(options) != 0) {
983 c2ad1b0c Kevin Wolf
            error_report("Can't use snapshot=on with driver-specific options");
984 c2ad1b0c Kevin Wolf
            ret = -EINVAL;
985 c2ad1b0c Kevin Wolf
            goto fail;
986 c2ad1b0c Kevin Wolf
        }
987 c2ad1b0c Kevin Wolf
        assert(filename != NULL);
988 c2ad1b0c Kevin Wolf
989 ea2384d3 bellard
        /* if snapshot, we create a temporary backing file and open it
990 ea2384d3 bellard
           instead of opening 'filename' directly */
991 33e3963e bellard
992 ea2384d3 bellard
        /* if there is a backing file, use it */
993 ea2384d3 bellard
        bs1 = bdrv_new("");
994 de9c0cec Kevin Wolf
        ret = bdrv_open(bs1, filename, NULL, 0, drv);
995 51d7c00c aliguori
        if (ret < 0) {
996 ea2384d3 bellard
            bdrv_delete(bs1);
997 de9c0cec Kevin Wolf
            goto fail;
998 ea2384d3 bellard
        }
999 3e82990b Jes Sorensen
        total_size = bdrv_getlength(bs1) & BDRV_SECTOR_MASK;
1000 7c96d46e aliguori
1001 ea2384d3 bellard
        bdrv_delete(bs1);
1002 3b46e624 ths
1003 eba25057 Jim Meyering
        ret = get_tmp_filename(tmp_filename, sizeof(tmp_filename));
1004 eba25057 Jim Meyering
        if (ret < 0) {
1005 de9c0cec Kevin Wolf
            goto fail;
1006 eba25057 Jim Meyering
        }
1007 7c96d46e aliguori
1008 7c96d46e aliguori
        /* Real path is meaningless for protocols */
1009 4d70655b Stefan Hajnoczi
        if (path_has_protocol(filename)) {
1010 7c96d46e aliguori
            snprintf(backing_filename, sizeof(backing_filename),
1011 7c96d46e aliguori
                     "%s", filename);
1012 de9c0cec Kevin Wolf
        } else if (!realpath(filename, backing_filename)) {
1013 de9c0cec Kevin Wolf
            ret = -errno;
1014 de9c0cec Kevin Wolf
            goto fail;
1015 de9c0cec Kevin Wolf
        }
1016 7c96d46e aliguori
1017 91a073a9 Kevin Wolf
        bdrv_qcow2 = bdrv_find_format("qcow2");
1018 08b392e1 Kevin Wolf
        create_options = parse_option_parameters("", bdrv_qcow2->create_options,
1019 08b392e1 Kevin Wolf
                                                 NULL);
1020 91a073a9 Kevin Wolf
1021 08b392e1 Kevin Wolf
        set_option_parameter_int(create_options, BLOCK_OPT_SIZE, total_size);
1022 08b392e1 Kevin Wolf
        set_option_parameter(create_options, BLOCK_OPT_BACKING_FILE,
1023 08b392e1 Kevin Wolf
                             backing_filename);
1024 91a073a9 Kevin Wolf
        if (drv) {
1025 08b392e1 Kevin Wolf
            set_option_parameter(create_options, BLOCK_OPT_BACKING_FMT,
1026 91a073a9 Kevin Wolf
                drv->format_name);
1027 91a073a9 Kevin Wolf
        }
1028 91a073a9 Kevin Wolf
1029 08b392e1 Kevin Wolf
        ret = bdrv_create(bdrv_qcow2, tmp_filename, create_options);
1030 08b392e1 Kevin Wolf
        free_option_parameters(create_options);
1031 51d7c00c aliguori
        if (ret < 0) {
1032 de9c0cec Kevin Wolf
            goto fail;
1033 ea2384d3 bellard
        }
1034 91a073a9 Kevin Wolf
1035 ea2384d3 bellard
        filename = tmp_filename;
1036 91a073a9 Kevin Wolf
        drv = bdrv_qcow2;
1037 ea2384d3 bellard
        bs->is_temporary = 1;
1038 ea2384d3 bellard
    }
1039 712e7874 bellard
1040 f500a6d3 Kevin Wolf
    /* Open image file without format layer */
1041 f500a6d3 Kevin Wolf
    if (flags & BDRV_O_RDWR) {
1042 f500a6d3 Kevin Wolf
        flags |= BDRV_O_ALLOW_RDWR;
1043 f500a6d3 Kevin Wolf
    }
1044 f500a6d3 Kevin Wolf
1045 707ff828 Kevin Wolf
    extract_subqdict(options, &file_options, "file.");
1046 707ff828 Kevin Wolf
1047 707ff828 Kevin Wolf
    ret = bdrv_file_open(&file, filename, file_options,
1048 707ff828 Kevin Wolf
                         bdrv_open_flags(bs, flags));
1049 f500a6d3 Kevin Wolf
    if (ret < 0) {
1050 de9c0cec Kevin Wolf
        goto fail;
1051 f500a6d3 Kevin Wolf
    }
1052 f500a6d3 Kevin Wolf
1053 b6ce07aa Kevin Wolf
    /* Find the right image format driver */
1054 6db95603 Christoph Hellwig
    if (!drv) {
1055 f500a6d3 Kevin Wolf
        ret = find_image_format(file, filename, &drv);
1056 51d7c00c aliguori
    }
1057 6987307c Christoph Hellwig
1058 51d7c00c aliguori
    if (!drv) {
1059 51d7c00c aliguori
        goto unlink_and_fail;
1060 ea2384d3 bellard
    }
1061 b6ce07aa Kevin Wolf
1062 b6ce07aa Kevin Wolf
    /* Open the image */
1063 035fccdf Kevin Wolf
    ret = bdrv_open_common(bs, file, options, flags, drv);
1064 b6ce07aa Kevin Wolf
    if (ret < 0) {
1065 6987307c Christoph Hellwig
        goto unlink_and_fail;
1066 6987307c Christoph Hellwig
    }
1067 6987307c Christoph Hellwig
1068 f500a6d3 Kevin Wolf
    if (bs->file != file) {
1069 f500a6d3 Kevin Wolf
        bdrv_delete(file);
1070 f500a6d3 Kevin Wolf
        file = NULL;
1071 f500a6d3 Kevin Wolf
    }
1072 f500a6d3 Kevin Wolf
1073 b6ce07aa Kevin Wolf
    /* If there is a backing file, use it */
1074 9156df12 Paolo Bonzini
    if ((flags & BDRV_O_NO_BACKING) == 0) {
1075 31ca6d07 Kevin Wolf
        QDict *backing_options;
1076 31ca6d07 Kevin Wolf
1077 31ca6d07 Kevin Wolf
        extract_subqdict(options, &backing_options, "backing.");
1078 31ca6d07 Kevin Wolf
        ret = bdrv_open_backing_file(bs, backing_options);
1079 b6ce07aa Kevin Wolf
        if (ret < 0) {
1080 b6ad491a Kevin Wolf
            goto close_and_fail;
1081 b6ce07aa Kevin Wolf
        }
1082 b6ce07aa Kevin Wolf
    }
1083 b6ce07aa Kevin Wolf
1084 b6ad491a Kevin Wolf
    /* Check if any unknown options were used */
1085 b6ad491a Kevin Wolf
    if (qdict_size(options) != 0) {
1086 b6ad491a Kevin Wolf
        const QDictEntry *entry = qdict_first(options);
1087 b6ad491a Kevin Wolf
        qerror_report(ERROR_CLASS_GENERIC_ERROR, "Block format '%s' used by "
1088 b6ad491a Kevin Wolf
            "device '%s' doesn't support the option '%s'",
1089 b6ad491a Kevin Wolf
            drv->format_name, bs->device_name, entry->key);
1090 b6ad491a Kevin Wolf
1091 b6ad491a Kevin Wolf
        ret = -EINVAL;
1092 b6ad491a Kevin Wolf
        goto close_and_fail;
1093 b6ad491a Kevin Wolf
    }
1094 b6ad491a Kevin Wolf
    QDECREF(options);
1095 b6ad491a Kevin Wolf
1096 b6ce07aa Kevin Wolf
    if (!bdrv_key_required(bs)) {
1097 7d4b4ba5 Markus Armbruster
        bdrv_dev_change_media_cb(bs, true);
1098 b6ce07aa Kevin Wolf
    }
1099 b6ce07aa Kevin Wolf
1100 98f90dba Zhi Yong Wu
    /* throttling disk I/O limits */
1101 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
1102 98f90dba Zhi Yong Wu
        bdrv_io_limits_enable(bs);
1103 98f90dba Zhi Yong Wu
    }
1104 98f90dba Zhi Yong Wu
1105 b6ce07aa Kevin Wolf
    return 0;
1106 b6ce07aa Kevin Wolf
1107 b6ce07aa Kevin Wolf
unlink_and_fail:
1108 f500a6d3 Kevin Wolf
    if (file != NULL) {
1109 f500a6d3 Kevin Wolf
        bdrv_delete(file);
1110 f500a6d3 Kevin Wolf
    }
1111 b6ce07aa Kevin Wolf
    if (bs->is_temporary) {
1112 b6ce07aa Kevin Wolf
        unlink(filename);
1113 b6ce07aa Kevin Wolf
    }
1114 de9c0cec Kevin Wolf
fail:
1115 de9c0cec Kevin Wolf
    QDECREF(bs->options);
1116 b6ad491a Kevin Wolf
    QDECREF(options);
1117 de9c0cec Kevin Wolf
    bs->options = NULL;
1118 b6ad491a Kevin Wolf
    return ret;
1119 de9c0cec Kevin Wolf
1120 b6ad491a Kevin Wolf
close_and_fail:
1121 b6ad491a Kevin Wolf
    bdrv_close(bs);
1122 b6ad491a Kevin Wolf
    QDECREF(options);
1123 b6ce07aa Kevin Wolf
    return ret;
1124 b6ce07aa Kevin Wolf
}
1125 b6ce07aa Kevin Wolf
1126 e971aa12 Jeff Cody
typedef struct BlockReopenQueueEntry {
1127 e971aa12 Jeff Cody
     bool prepared;
1128 e971aa12 Jeff Cody
     BDRVReopenState state;
1129 e971aa12 Jeff Cody
     QSIMPLEQ_ENTRY(BlockReopenQueueEntry) entry;
1130 e971aa12 Jeff Cody
} BlockReopenQueueEntry;
1131 e971aa12 Jeff Cody
1132 e971aa12 Jeff Cody
/*
1133 e971aa12 Jeff Cody
 * Adds a BlockDriverState to a simple queue for an atomic, transactional
1134 e971aa12 Jeff Cody
 * reopen of multiple devices.
1135 e971aa12 Jeff Cody
 *
1136 e971aa12 Jeff Cody
 * bs_queue can either be an existing BlockReopenQueue that has had QSIMPLE_INIT
1137 e971aa12 Jeff Cody
 * already performed, or alternatively may be NULL a new BlockReopenQueue will
1138 e971aa12 Jeff Cody
 * be created and initialized. This newly created BlockReopenQueue should be
1139 e971aa12 Jeff Cody
 * passed back in for subsequent calls that are intended to be of the same
1140 e971aa12 Jeff Cody
 * atomic 'set'.
1141 e971aa12 Jeff Cody
 *
1142 e971aa12 Jeff Cody
 * bs is the BlockDriverState to add to the reopen queue.
1143 e971aa12 Jeff Cody
 *
1144 e971aa12 Jeff Cody
 * flags contains the open flags for the associated bs
1145 e971aa12 Jeff Cody
 *
1146 e971aa12 Jeff Cody
 * returns a pointer to bs_queue, which is either the newly allocated
1147 e971aa12 Jeff Cody
 * bs_queue, or the existing bs_queue being used.
1148 e971aa12 Jeff Cody
 *
1149 e971aa12 Jeff Cody
 */
1150 e971aa12 Jeff Cody
BlockReopenQueue *bdrv_reopen_queue(BlockReopenQueue *bs_queue,
1151 e971aa12 Jeff Cody
                                    BlockDriverState *bs, int flags)
1152 e971aa12 Jeff Cody
{
1153 e971aa12 Jeff Cody
    assert(bs != NULL);
1154 e971aa12 Jeff Cody
1155 e971aa12 Jeff Cody
    BlockReopenQueueEntry *bs_entry;
1156 e971aa12 Jeff Cody
    if (bs_queue == NULL) {
1157 e971aa12 Jeff Cody
        bs_queue = g_new0(BlockReopenQueue, 1);
1158 e971aa12 Jeff Cody
        QSIMPLEQ_INIT(bs_queue);
1159 e971aa12 Jeff Cody
    }
1160 e971aa12 Jeff Cody
1161 e971aa12 Jeff Cody
    if (bs->file) {
1162 e971aa12 Jeff Cody
        bdrv_reopen_queue(bs_queue, bs->file, flags);
1163 e971aa12 Jeff Cody
    }
1164 e971aa12 Jeff Cody
1165 e971aa12 Jeff Cody
    bs_entry = g_new0(BlockReopenQueueEntry, 1);
1166 e971aa12 Jeff Cody
    QSIMPLEQ_INSERT_TAIL(bs_queue, bs_entry, entry);
1167 e971aa12 Jeff Cody
1168 e971aa12 Jeff Cody
    bs_entry->state.bs = bs;
1169 e971aa12 Jeff Cody
    bs_entry->state.flags = flags;
1170 e971aa12 Jeff Cody
1171 e971aa12 Jeff Cody
    return bs_queue;
1172 e971aa12 Jeff Cody
}
1173 e971aa12 Jeff Cody
1174 e971aa12 Jeff Cody
/*
1175 e971aa12 Jeff Cody
 * Reopen multiple BlockDriverStates atomically & transactionally.
1176 e971aa12 Jeff Cody
 *
1177 e971aa12 Jeff Cody
 * The queue passed in (bs_queue) must have been built up previous
1178 e971aa12 Jeff Cody
 * via bdrv_reopen_queue().
1179 e971aa12 Jeff Cody
 *
1180 e971aa12 Jeff Cody
 * Reopens all BDS specified in the queue, with the appropriate
1181 e971aa12 Jeff Cody
 * flags.  All devices are prepared for reopen, and failure of any
1182 e971aa12 Jeff Cody
 * device will cause all device changes to be abandonded, and intermediate
1183 e971aa12 Jeff Cody
 * data cleaned up.
1184 e971aa12 Jeff Cody
 *
1185 e971aa12 Jeff Cody
 * If all devices prepare successfully, then the changes are committed
1186 e971aa12 Jeff Cody
 * to all devices.
1187 e971aa12 Jeff Cody
 *
1188 e971aa12 Jeff Cody
 */
1189 e971aa12 Jeff Cody
int bdrv_reopen_multiple(BlockReopenQueue *bs_queue, Error **errp)
1190 e971aa12 Jeff Cody
{
1191 e971aa12 Jeff Cody
    int ret = -1;
1192 e971aa12 Jeff Cody
    BlockReopenQueueEntry *bs_entry, *next;
1193 e971aa12 Jeff Cody
    Error *local_err = NULL;
1194 e971aa12 Jeff Cody
1195 e971aa12 Jeff Cody
    assert(bs_queue != NULL);
1196 e971aa12 Jeff Cody
1197 e971aa12 Jeff Cody
    bdrv_drain_all();
1198 e971aa12 Jeff Cody
1199 e971aa12 Jeff Cody
    QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1200 e971aa12 Jeff Cody
        if (bdrv_reopen_prepare(&bs_entry->state, bs_queue, &local_err)) {
1201 e971aa12 Jeff Cody
            error_propagate(errp, local_err);
1202 e971aa12 Jeff Cody
            goto cleanup;
1203 e971aa12 Jeff Cody
        }
1204 e971aa12 Jeff Cody
        bs_entry->prepared = true;
1205 e971aa12 Jeff Cody
    }
1206 e971aa12 Jeff Cody
1207 e971aa12 Jeff Cody
    /* If we reach this point, we have success and just need to apply the
1208 e971aa12 Jeff Cody
     * changes
1209 e971aa12 Jeff Cody
     */
1210 e971aa12 Jeff Cody
    QSIMPLEQ_FOREACH(bs_entry, bs_queue, entry) {
1211 e971aa12 Jeff Cody
        bdrv_reopen_commit(&bs_entry->state);
1212 e971aa12 Jeff Cody
    }
1213 e971aa12 Jeff Cody
1214 e971aa12 Jeff Cody
    ret = 0;
1215 e971aa12 Jeff Cody
1216 e971aa12 Jeff Cody
cleanup:
1217 e971aa12 Jeff Cody
    QSIMPLEQ_FOREACH_SAFE(bs_entry, bs_queue, entry, next) {
1218 e971aa12 Jeff Cody
        if (ret && bs_entry->prepared) {
1219 e971aa12 Jeff Cody
            bdrv_reopen_abort(&bs_entry->state);
1220 e971aa12 Jeff Cody
        }
1221 e971aa12 Jeff Cody
        g_free(bs_entry);
1222 e971aa12 Jeff Cody
    }
1223 e971aa12 Jeff Cody
    g_free(bs_queue);
1224 e971aa12 Jeff Cody
    return ret;
1225 e971aa12 Jeff Cody
}
1226 e971aa12 Jeff Cody
1227 e971aa12 Jeff Cody
1228 e971aa12 Jeff Cody
/* Reopen a single BlockDriverState with the specified flags. */
1229 e971aa12 Jeff Cody
int bdrv_reopen(BlockDriverState *bs, int bdrv_flags, Error **errp)
1230 e971aa12 Jeff Cody
{
1231 e971aa12 Jeff Cody
    int ret = -1;
1232 e971aa12 Jeff Cody
    Error *local_err = NULL;
1233 e971aa12 Jeff Cody
    BlockReopenQueue *queue = bdrv_reopen_queue(NULL, bs, bdrv_flags);
1234 e971aa12 Jeff Cody
1235 e971aa12 Jeff Cody
    ret = bdrv_reopen_multiple(queue, &local_err);
1236 e971aa12 Jeff Cody
    if (local_err != NULL) {
1237 e971aa12 Jeff Cody
        error_propagate(errp, local_err);
1238 e971aa12 Jeff Cody
    }
1239 e971aa12 Jeff Cody
    return ret;
1240 e971aa12 Jeff Cody
}
1241 e971aa12 Jeff Cody
1242 e971aa12 Jeff Cody
1243 e971aa12 Jeff Cody
/*
1244 e971aa12 Jeff Cody
 * Prepares a BlockDriverState for reopen. All changes are staged in the
1245 e971aa12 Jeff Cody
 * 'opaque' field of the BDRVReopenState, which is used and allocated by
1246 e971aa12 Jeff Cody
 * the block driver layer .bdrv_reopen_prepare()
1247 e971aa12 Jeff Cody
 *
1248 e971aa12 Jeff Cody
 * bs is the BlockDriverState to reopen
1249 e971aa12 Jeff Cody
 * flags are the new open flags
1250 e971aa12 Jeff Cody
 * queue is the reopen queue
1251 e971aa12 Jeff Cody
 *
1252 e971aa12 Jeff Cody
 * Returns 0 on success, non-zero on error.  On error errp will be set
1253 e971aa12 Jeff Cody
 * as well.
1254 e971aa12 Jeff Cody
 *
1255 e971aa12 Jeff Cody
 * On failure, bdrv_reopen_abort() will be called to clean up any data.
1256 e971aa12 Jeff Cody
 * It is the responsibility of the caller to then call the abort() or
1257 e971aa12 Jeff Cody
 * commit() for any other BDS that have been left in a prepare() state
1258 e971aa12 Jeff Cody
 *
1259 e971aa12 Jeff Cody
 */
1260 e971aa12 Jeff Cody
int bdrv_reopen_prepare(BDRVReopenState *reopen_state, BlockReopenQueue *queue,
1261 e971aa12 Jeff Cody
                        Error **errp)
1262 e971aa12 Jeff Cody
{
1263 e971aa12 Jeff Cody
    int ret = -1;
1264 e971aa12 Jeff Cody
    Error *local_err = NULL;
1265 e971aa12 Jeff Cody
    BlockDriver *drv;
1266 e971aa12 Jeff Cody
1267 e971aa12 Jeff Cody
    assert(reopen_state != NULL);
1268 e971aa12 Jeff Cody
    assert(reopen_state->bs->drv != NULL);
1269 e971aa12 Jeff Cody
    drv = reopen_state->bs->drv;
1270 e971aa12 Jeff Cody
1271 e971aa12 Jeff Cody
    /* if we are to stay read-only, do not allow permission change
1272 e971aa12 Jeff Cody
     * to r/w */
1273 e971aa12 Jeff Cody
    if (!(reopen_state->bs->open_flags & BDRV_O_ALLOW_RDWR) &&
1274 e971aa12 Jeff Cody
        reopen_state->flags & BDRV_O_RDWR) {
1275 e971aa12 Jeff Cody
        error_set(errp, QERR_DEVICE_IS_READ_ONLY,
1276 e971aa12 Jeff Cody
                  reopen_state->bs->device_name);
1277 e971aa12 Jeff Cody
        goto error;
1278 e971aa12 Jeff Cody
    }
1279 e971aa12 Jeff Cody
1280 e971aa12 Jeff Cody
1281 e971aa12 Jeff Cody
    ret = bdrv_flush(reopen_state->bs);
1282 e971aa12 Jeff Cody
    if (ret) {
1283 e971aa12 Jeff Cody
        error_set(errp, ERROR_CLASS_GENERIC_ERROR, "Error (%s) flushing drive",
1284 e971aa12 Jeff Cody
                  strerror(-ret));
1285 e971aa12 Jeff Cody
        goto error;
1286 e971aa12 Jeff Cody
    }
1287 e971aa12 Jeff Cody
1288 e971aa12 Jeff Cody
    if (drv->bdrv_reopen_prepare) {
1289 e971aa12 Jeff Cody
        ret = drv->bdrv_reopen_prepare(reopen_state, queue, &local_err);
1290 e971aa12 Jeff Cody
        if (ret) {
1291 e971aa12 Jeff Cody
            if (local_err != NULL) {
1292 e971aa12 Jeff Cody
                error_propagate(errp, local_err);
1293 e971aa12 Jeff Cody
            } else {
1294 e971aa12 Jeff Cody
                error_set(errp, QERR_OPEN_FILE_FAILED,
1295 e971aa12 Jeff Cody
                          reopen_state->bs->filename);
1296 e971aa12 Jeff Cody
            }
1297 e971aa12 Jeff Cody
            goto error;
1298 e971aa12 Jeff Cody
        }
1299 e971aa12 Jeff Cody
    } else {
1300 e971aa12 Jeff Cody
        /* It is currently mandatory to have a bdrv_reopen_prepare()
1301 e971aa12 Jeff Cody
         * handler for each supported drv. */
1302 e971aa12 Jeff Cody
        error_set(errp, QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED,
1303 e971aa12 Jeff Cody
                  drv->format_name, reopen_state->bs->device_name,
1304 e971aa12 Jeff Cody
                 "reopening of file");
1305 e971aa12 Jeff Cody
        ret = -1;
1306 e971aa12 Jeff Cody
        goto error;
1307 e971aa12 Jeff Cody
    }
1308 e971aa12 Jeff Cody
1309 e971aa12 Jeff Cody
    ret = 0;
1310 e971aa12 Jeff Cody
1311 e971aa12 Jeff Cody
error:
1312 e971aa12 Jeff Cody
    return ret;
1313 e971aa12 Jeff Cody
}
1314 e971aa12 Jeff Cody
1315 e971aa12 Jeff Cody
/*
1316 e971aa12 Jeff Cody
 * Takes the staged changes for the reopen from bdrv_reopen_prepare(), and
1317 e971aa12 Jeff Cody
 * makes them final by swapping the staging BlockDriverState contents into
1318 e971aa12 Jeff Cody
 * the active BlockDriverState contents.
1319 e971aa12 Jeff Cody
 */
1320 e971aa12 Jeff Cody
void bdrv_reopen_commit(BDRVReopenState *reopen_state)
1321 e971aa12 Jeff Cody
{
1322 e971aa12 Jeff Cody
    BlockDriver *drv;
1323 e971aa12 Jeff Cody
1324 e971aa12 Jeff Cody
    assert(reopen_state != NULL);
1325 e971aa12 Jeff Cody
    drv = reopen_state->bs->drv;
1326 e971aa12 Jeff Cody
    assert(drv != NULL);
1327 e971aa12 Jeff Cody
1328 e971aa12 Jeff Cody
    /* If there are any driver level actions to take */
1329 e971aa12 Jeff Cody
    if (drv->bdrv_reopen_commit) {
1330 e971aa12 Jeff Cody
        drv->bdrv_reopen_commit(reopen_state);
1331 e971aa12 Jeff Cody
    }
1332 e971aa12 Jeff Cody
1333 e971aa12 Jeff Cody
    /* set BDS specific flags now */
1334 e971aa12 Jeff Cody
    reopen_state->bs->open_flags         = reopen_state->flags;
1335 e971aa12 Jeff Cody
    reopen_state->bs->enable_write_cache = !!(reopen_state->flags &
1336 e971aa12 Jeff Cody
                                              BDRV_O_CACHE_WB);
1337 e971aa12 Jeff Cody
    reopen_state->bs->read_only = !(reopen_state->flags & BDRV_O_RDWR);
1338 e971aa12 Jeff Cody
}
1339 e971aa12 Jeff Cody
1340 e971aa12 Jeff Cody
/*
1341 e971aa12 Jeff Cody
 * Abort the reopen, and delete and free the staged changes in
1342 e971aa12 Jeff Cody
 * reopen_state
1343 e971aa12 Jeff Cody
 */
1344 e971aa12 Jeff Cody
void bdrv_reopen_abort(BDRVReopenState *reopen_state)
1345 e971aa12 Jeff Cody
{
1346 e971aa12 Jeff Cody
    BlockDriver *drv;
1347 e971aa12 Jeff Cody
1348 e971aa12 Jeff Cody
    assert(reopen_state != NULL);
1349 e971aa12 Jeff Cody
    drv = reopen_state->bs->drv;
1350 e971aa12 Jeff Cody
    assert(drv != NULL);
1351 e971aa12 Jeff Cody
1352 e971aa12 Jeff Cody
    if (drv->bdrv_reopen_abort) {
1353 e971aa12 Jeff Cody
        drv->bdrv_reopen_abort(reopen_state);
1354 e971aa12 Jeff Cody
    }
1355 e971aa12 Jeff Cody
}
1356 e971aa12 Jeff Cody
1357 e971aa12 Jeff Cody
1358 fc01f7e7 bellard
void bdrv_close(BlockDriverState *bs)
1359 fc01f7e7 bellard
{
1360 80ccf93b Liu Yuan
    bdrv_flush(bs);
1361 3cbc002c Paolo Bonzini
    if (bs->job) {
1362 3cbc002c Paolo Bonzini
        block_job_cancel_sync(bs->job);
1363 3cbc002c Paolo Bonzini
    }
1364 3cbc002c Paolo Bonzini
    bdrv_drain_all();
1365 d7d512f6 Paolo Bonzini
    notifier_list_notify(&bs->close_notifiers, bs);
1366 7094f12f Kevin Wolf
1367 3cbc002c Paolo Bonzini
    if (bs->drv) {
1368 557df6ac Stefan Hajnoczi
        if (bs->backing_hd) {
1369 ea2384d3 bellard
            bdrv_delete(bs->backing_hd);
1370 557df6ac Stefan Hajnoczi
            bs->backing_hd = NULL;
1371 557df6ac Stefan Hajnoczi
        }
1372 ea2384d3 bellard
        bs->drv->bdrv_close(bs);
1373 7267c094 Anthony Liguori
        g_free(bs->opaque);
1374 ea2384d3 bellard
#ifdef _WIN32
1375 ea2384d3 bellard
        if (bs->is_temporary) {
1376 ea2384d3 bellard
            unlink(bs->filename);
1377 ea2384d3 bellard
        }
1378 67b915a5 bellard
#endif
1379 ea2384d3 bellard
        bs->opaque = NULL;
1380 ea2384d3 bellard
        bs->drv = NULL;
1381 53fec9d3 Stefan Hajnoczi
        bs->copy_on_read = 0;
1382 a275fa42 Paolo Bonzini
        bs->backing_file[0] = '\0';
1383 a275fa42 Paolo Bonzini
        bs->backing_format[0] = '\0';
1384 6405875c Paolo Bonzini
        bs->total_sectors = 0;
1385 6405875c Paolo Bonzini
        bs->encrypted = 0;
1386 6405875c Paolo Bonzini
        bs->valid_key = 0;
1387 6405875c Paolo Bonzini
        bs->sg = 0;
1388 6405875c Paolo Bonzini
        bs->growable = 0;
1389 de9c0cec Kevin Wolf
        QDECREF(bs->options);
1390 de9c0cec Kevin Wolf
        bs->options = NULL;
1391 b338082b bellard
1392 66f82cee Kevin Wolf
        if (bs->file != NULL) {
1393 0ac9377d Paolo Bonzini
            bdrv_delete(bs->file);
1394 0ac9377d Paolo Bonzini
            bs->file = NULL;
1395 66f82cee Kevin Wolf
        }
1396 b338082b bellard
    }
1397 98f90dba Zhi Yong Wu
1398 9ca11154 Pavel Hrdina
    bdrv_dev_change_media_cb(bs, false);
1399 9ca11154 Pavel Hrdina
1400 98f90dba Zhi Yong Wu
    /*throttling disk I/O limits*/
1401 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
1402 98f90dba Zhi Yong Wu
        bdrv_io_limits_disable(bs);
1403 98f90dba Zhi Yong Wu
    }
1404 b338082b bellard
}
1405 b338082b bellard
1406 2bc93fed MORITA Kazutaka
void bdrv_close_all(void)
1407 2bc93fed MORITA Kazutaka
{
1408 2bc93fed MORITA Kazutaka
    BlockDriverState *bs;
1409 2bc93fed MORITA Kazutaka
1410 2bc93fed MORITA Kazutaka
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1411 2bc93fed MORITA Kazutaka
        bdrv_close(bs);
1412 2bc93fed MORITA Kazutaka
    }
1413 2bc93fed MORITA Kazutaka
}
1414 2bc93fed MORITA Kazutaka
1415 922453bc Stefan Hajnoczi
/*
1416 922453bc Stefan Hajnoczi
 * Wait for pending requests to complete across all BlockDriverStates
1417 922453bc Stefan Hajnoczi
 *
1418 922453bc Stefan Hajnoczi
 * This function does not flush data to disk, use bdrv_flush_all() for that
1419 922453bc Stefan Hajnoczi
 * after calling this function.
1420 4c355d53 Zhi Yong Wu
 *
1421 4c355d53 Zhi Yong Wu
 * Note that completion of an asynchronous I/O operation can trigger any
1422 4c355d53 Zhi Yong Wu
 * number of other I/O operations on other devices---for example a coroutine
1423 4c355d53 Zhi Yong Wu
 * can be arbitrarily complex and a constant flow of I/O can come until the
1424 4c355d53 Zhi Yong Wu
 * coroutine is complete.  Because of this, it is not possible to have a
1425 4c355d53 Zhi Yong Wu
 * function to drain a single device's I/O queue.
1426 922453bc Stefan Hajnoczi
 */
1427 922453bc Stefan Hajnoczi
void bdrv_drain_all(void)
1428 922453bc Stefan Hajnoczi
{
1429 922453bc Stefan Hajnoczi
    BlockDriverState *bs;
1430 4c355d53 Zhi Yong Wu
    bool busy;
1431 4c355d53 Zhi Yong Wu
1432 4c355d53 Zhi Yong Wu
    do {
1433 4c355d53 Zhi Yong Wu
        busy = qemu_aio_wait();
1434 922453bc Stefan Hajnoczi
1435 4c355d53 Zhi Yong Wu
        /* FIXME: We do not have timer support here, so this is effectively
1436 4c355d53 Zhi Yong Wu
         * a busy wait.
1437 4c355d53 Zhi Yong Wu
         */
1438 4c355d53 Zhi Yong Wu
        QTAILQ_FOREACH(bs, &bdrv_states, list) {
1439 4c355d53 Zhi Yong Wu
            if (!qemu_co_queue_empty(&bs->throttled_reqs)) {
1440 4c355d53 Zhi Yong Wu
                qemu_co_queue_restart_all(&bs->throttled_reqs);
1441 4c355d53 Zhi Yong Wu
                busy = true;
1442 4c355d53 Zhi Yong Wu
            }
1443 4c355d53 Zhi Yong Wu
        }
1444 4c355d53 Zhi Yong Wu
    } while (busy);
1445 922453bc Stefan Hajnoczi
1446 922453bc Stefan Hajnoczi
    /* If requests are still pending there is a bug somewhere */
1447 922453bc Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1448 922453bc Stefan Hajnoczi
        assert(QLIST_EMPTY(&bs->tracked_requests));
1449 922453bc Stefan Hajnoczi
        assert(qemu_co_queue_empty(&bs->throttled_reqs));
1450 922453bc Stefan Hajnoczi
    }
1451 922453bc Stefan Hajnoczi
}
1452 922453bc Stefan Hajnoczi
1453 d22b2f41 Ryan Harper
/* make a BlockDriverState anonymous by removing from bdrv_state list.
1454 d22b2f41 Ryan Harper
   Also, NULL terminate the device_name to prevent double remove */
1455 d22b2f41 Ryan Harper
void bdrv_make_anon(BlockDriverState *bs)
1456 d22b2f41 Ryan Harper
{
1457 d22b2f41 Ryan Harper
    if (bs->device_name[0] != '\0') {
1458 d22b2f41 Ryan Harper
        QTAILQ_REMOVE(&bdrv_states, bs, list);
1459 d22b2f41 Ryan Harper
    }
1460 d22b2f41 Ryan Harper
    bs->device_name[0] = '\0';
1461 d22b2f41 Ryan Harper
}
1462 d22b2f41 Ryan Harper
1463 e023b2e2 Paolo Bonzini
static void bdrv_rebind(BlockDriverState *bs)
1464 e023b2e2 Paolo Bonzini
{
1465 e023b2e2 Paolo Bonzini
    if (bs->drv && bs->drv->bdrv_rebind) {
1466 e023b2e2 Paolo Bonzini
        bs->drv->bdrv_rebind(bs);
1467 e023b2e2 Paolo Bonzini
    }
1468 e023b2e2 Paolo Bonzini
}
1469 e023b2e2 Paolo Bonzini
1470 4ddc07ca Paolo Bonzini
static void bdrv_move_feature_fields(BlockDriverState *bs_dest,
1471 4ddc07ca Paolo Bonzini
                                     BlockDriverState *bs_src)
1472 8802d1fd Jeff Cody
{
1473 4ddc07ca Paolo Bonzini
    /* move some fields that need to stay attached to the device */
1474 4ddc07ca Paolo Bonzini
    bs_dest->open_flags         = bs_src->open_flags;
1475 8802d1fd Jeff Cody
1476 8802d1fd Jeff Cody
    /* dev info */
1477 4ddc07ca Paolo Bonzini
    bs_dest->dev_ops            = bs_src->dev_ops;
1478 4ddc07ca Paolo Bonzini
    bs_dest->dev_opaque         = bs_src->dev_opaque;
1479 4ddc07ca Paolo Bonzini
    bs_dest->dev                = bs_src->dev;
1480 4ddc07ca Paolo Bonzini
    bs_dest->buffer_alignment   = bs_src->buffer_alignment;
1481 4ddc07ca Paolo Bonzini
    bs_dest->copy_on_read       = bs_src->copy_on_read;
1482 8802d1fd Jeff Cody
1483 4ddc07ca Paolo Bonzini
    bs_dest->enable_write_cache = bs_src->enable_write_cache;
1484 c4a248a1 Paolo Bonzini
1485 8802d1fd Jeff Cody
    /* i/o timing parameters */
1486 4ddc07ca Paolo Bonzini
    bs_dest->slice_start        = bs_src->slice_start;
1487 4ddc07ca Paolo Bonzini
    bs_dest->slice_end          = bs_src->slice_end;
1488 5905fbc9 Stefan Hajnoczi
    bs_dest->slice_submitted    = bs_src->slice_submitted;
1489 4ddc07ca Paolo Bonzini
    bs_dest->io_limits          = bs_src->io_limits;
1490 4ddc07ca Paolo Bonzini
    bs_dest->throttled_reqs     = bs_src->throttled_reqs;
1491 4ddc07ca Paolo Bonzini
    bs_dest->block_timer        = bs_src->block_timer;
1492 4ddc07ca Paolo Bonzini
    bs_dest->io_limits_enabled  = bs_src->io_limits_enabled;
1493 8802d1fd Jeff Cody
1494 8802d1fd Jeff Cody
    /* r/w error */
1495 4ddc07ca Paolo Bonzini
    bs_dest->on_read_error      = bs_src->on_read_error;
1496 4ddc07ca Paolo Bonzini
    bs_dest->on_write_error     = bs_src->on_write_error;
1497 8802d1fd Jeff Cody
1498 8802d1fd Jeff Cody
    /* i/o status */
1499 4ddc07ca Paolo Bonzini
    bs_dest->iostatus_enabled   = bs_src->iostatus_enabled;
1500 4ddc07ca Paolo Bonzini
    bs_dest->iostatus           = bs_src->iostatus;
1501 8802d1fd Jeff Cody
1502 a9fc4408 Paolo Bonzini
    /* dirty bitmap */
1503 4ddc07ca Paolo Bonzini
    bs_dest->dirty_bitmap       = bs_src->dirty_bitmap;
1504 a9fc4408 Paolo Bonzini
1505 a9fc4408 Paolo Bonzini
    /* job */
1506 4ddc07ca Paolo Bonzini
    bs_dest->in_use             = bs_src->in_use;
1507 4ddc07ca Paolo Bonzini
    bs_dest->job                = bs_src->job;
1508 a9fc4408 Paolo Bonzini
1509 8802d1fd Jeff Cody
    /* keep the same entry in bdrv_states */
1510 4ddc07ca Paolo Bonzini
    pstrcpy(bs_dest->device_name, sizeof(bs_dest->device_name),
1511 4ddc07ca Paolo Bonzini
            bs_src->device_name);
1512 4ddc07ca Paolo Bonzini
    bs_dest->list = bs_src->list;
1513 4ddc07ca Paolo Bonzini
}
1514 8802d1fd Jeff Cody
1515 4ddc07ca Paolo Bonzini
/*
1516 4ddc07ca Paolo Bonzini
 * Swap bs contents for two image chains while they are live,
1517 4ddc07ca Paolo Bonzini
 * while keeping required fields on the BlockDriverState that is
1518 4ddc07ca Paolo Bonzini
 * actually attached to a device.
1519 4ddc07ca Paolo Bonzini
 *
1520 4ddc07ca Paolo Bonzini
 * This will modify the BlockDriverState fields, and swap contents
1521 4ddc07ca Paolo Bonzini
 * between bs_new and bs_old. Both bs_new and bs_old are modified.
1522 4ddc07ca Paolo Bonzini
 *
1523 4ddc07ca Paolo Bonzini
 * bs_new is required to be anonymous.
1524 4ddc07ca Paolo Bonzini
 *
1525 4ddc07ca Paolo Bonzini
 * This function does not create any image files.
1526 4ddc07ca Paolo Bonzini
 */
1527 4ddc07ca Paolo Bonzini
void bdrv_swap(BlockDriverState *bs_new, BlockDriverState *bs_old)
1528 4ddc07ca Paolo Bonzini
{
1529 4ddc07ca Paolo Bonzini
    BlockDriverState tmp;
1530 f6801b83 Jeff Cody
1531 4ddc07ca Paolo Bonzini
    /* bs_new must be anonymous and shouldn't have anything fancy enabled */
1532 4ddc07ca Paolo Bonzini
    assert(bs_new->device_name[0] == '\0');
1533 4ddc07ca Paolo Bonzini
    assert(bs_new->dirty_bitmap == NULL);
1534 4ddc07ca Paolo Bonzini
    assert(bs_new->job == NULL);
1535 4ddc07ca Paolo Bonzini
    assert(bs_new->dev == NULL);
1536 4ddc07ca Paolo Bonzini
    assert(bs_new->in_use == 0);
1537 4ddc07ca Paolo Bonzini
    assert(bs_new->io_limits_enabled == false);
1538 4ddc07ca Paolo Bonzini
    assert(bs_new->block_timer == NULL);
1539 8802d1fd Jeff Cody
1540 4ddc07ca Paolo Bonzini
    tmp = *bs_new;
1541 4ddc07ca Paolo Bonzini
    *bs_new = *bs_old;
1542 4ddc07ca Paolo Bonzini
    *bs_old = tmp;
1543 a9fc4408 Paolo Bonzini
1544 4ddc07ca Paolo Bonzini
    /* there are some fields that should not be swapped, move them back */
1545 4ddc07ca Paolo Bonzini
    bdrv_move_feature_fields(&tmp, bs_old);
1546 4ddc07ca Paolo Bonzini
    bdrv_move_feature_fields(bs_old, bs_new);
1547 4ddc07ca Paolo Bonzini
    bdrv_move_feature_fields(bs_new, &tmp);
1548 8802d1fd Jeff Cody
1549 4ddc07ca Paolo Bonzini
    /* bs_new shouldn't be in bdrv_states even after the swap!  */
1550 4ddc07ca Paolo Bonzini
    assert(bs_new->device_name[0] == '\0');
1551 4ddc07ca Paolo Bonzini
1552 4ddc07ca Paolo Bonzini
    /* Check a few fields that should remain attached to the device */
1553 4ddc07ca Paolo Bonzini
    assert(bs_new->dev == NULL);
1554 4ddc07ca Paolo Bonzini
    assert(bs_new->job == NULL);
1555 4ddc07ca Paolo Bonzini
    assert(bs_new->in_use == 0);
1556 4ddc07ca Paolo Bonzini
    assert(bs_new->io_limits_enabled == false);
1557 4ddc07ca Paolo Bonzini
    assert(bs_new->block_timer == NULL);
1558 e023b2e2 Paolo Bonzini
1559 e023b2e2 Paolo Bonzini
    bdrv_rebind(bs_new);
1560 4ddc07ca Paolo Bonzini
    bdrv_rebind(bs_old);
1561 4ddc07ca Paolo Bonzini
}
1562 4ddc07ca Paolo Bonzini
1563 4ddc07ca Paolo Bonzini
/*
1564 4ddc07ca Paolo Bonzini
 * Add new bs contents at the top of an image chain while the chain is
1565 4ddc07ca Paolo Bonzini
 * live, while keeping required fields on the top layer.
1566 4ddc07ca Paolo Bonzini
 *
1567 4ddc07ca Paolo Bonzini
 * This will modify the BlockDriverState fields, and swap contents
1568 4ddc07ca Paolo Bonzini
 * between bs_new and bs_top. Both bs_new and bs_top are modified.
1569 4ddc07ca Paolo Bonzini
 *
1570 4ddc07ca Paolo Bonzini
 * bs_new is required to be anonymous.
1571 4ddc07ca Paolo Bonzini
 *
1572 4ddc07ca Paolo Bonzini
 * This function does not create any image files.
1573 4ddc07ca Paolo Bonzini
 */
1574 4ddc07ca Paolo Bonzini
void bdrv_append(BlockDriverState *bs_new, BlockDriverState *bs_top)
1575 4ddc07ca Paolo Bonzini
{
1576 4ddc07ca Paolo Bonzini
    bdrv_swap(bs_new, bs_top);
1577 4ddc07ca Paolo Bonzini
1578 4ddc07ca Paolo Bonzini
    /* The contents of 'tmp' will become bs_top, as we are
1579 4ddc07ca Paolo Bonzini
     * swapping bs_new and bs_top contents. */
1580 4ddc07ca Paolo Bonzini
    bs_top->backing_hd = bs_new;
1581 4ddc07ca Paolo Bonzini
    bs_top->open_flags &= ~BDRV_O_NO_BACKING;
1582 4ddc07ca Paolo Bonzini
    pstrcpy(bs_top->backing_file, sizeof(bs_top->backing_file),
1583 4ddc07ca Paolo Bonzini
            bs_new->filename);
1584 4ddc07ca Paolo Bonzini
    pstrcpy(bs_top->backing_format, sizeof(bs_top->backing_format),
1585 4ddc07ca Paolo Bonzini
            bs_new->drv ? bs_new->drv->format_name : "");
1586 8802d1fd Jeff Cody
}
1587 8802d1fd Jeff Cody
1588 b338082b bellard
void bdrv_delete(BlockDriverState *bs)
1589 b338082b bellard
{
1590 fa879d62 Markus Armbruster
    assert(!bs->dev);
1591 3e914655 Paolo Bonzini
    assert(!bs->job);
1592 3e914655 Paolo Bonzini
    assert(!bs->in_use);
1593 18846dee Markus Armbruster
1594 1b7bdbc1 Stefan Hajnoczi
    /* remove from list, if necessary */
1595 d22b2f41 Ryan Harper
    bdrv_make_anon(bs);
1596 34c6f050 aurel32
1597 b338082b bellard
    bdrv_close(bs);
1598 66f82cee Kevin Wolf
1599 7267c094 Anthony Liguori
    g_free(bs);
1600 fc01f7e7 bellard
}
1601 fc01f7e7 bellard
1602 fa879d62 Markus Armbruster
int bdrv_attach_dev(BlockDriverState *bs, void *dev)
1603 fa879d62 Markus Armbruster
/* TODO change to DeviceState *dev when all users are qdevified */
1604 18846dee Markus Armbruster
{
1605 fa879d62 Markus Armbruster
    if (bs->dev) {
1606 18846dee Markus Armbruster
        return -EBUSY;
1607 18846dee Markus Armbruster
    }
1608 fa879d62 Markus Armbruster
    bs->dev = dev;
1609 28a7282a Luiz Capitulino
    bdrv_iostatus_reset(bs);
1610 18846dee Markus Armbruster
    return 0;
1611 18846dee Markus Armbruster
}
1612 18846dee Markus Armbruster
1613 fa879d62 Markus Armbruster
/* TODO qdevified devices don't use this, remove when devices are qdevified */
1614 fa879d62 Markus Armbruster
void bdrv_attach_dev_nofail(BlockDriverState *bs, void *dev)
1615 18846dee Markus Armbruster
{
1616 fa879d62 Markus Armbruster
    if (bdrv_attach_dev(bs, dev) < 0) {
1617 fa879d62 Markus Armbruster
        abort();
1618 fa879d62 Markus Armbruster
    }
1619 fa879d62 Markus Armbruster
}
1620 fa879d62 Markus Armbruster
1621 fa879d62 Markus Armbruster
void bdrv_detach_dev(BlockDriverState *bs, void *dev)
1622 fa879d62 Markus Armbruster
/* TODO change to DeviceState *dev when all users are qdevified */
1623 fa879d62 Markus Armbruster
{
1624 fa879d62 Markus Armbruster
    assert(bs->dev == dev);
1625 fa879d62 Markus Armbruster
    bs->dev = NULL;
1626 0e49de52 Markus Armbruster
    bs->dev_ops = NULL;
1627 0e49de52 Markus Armbruster
    bs->dev_opaque = NULL;
1628 29e05f20 Markus Armbruster
    bs->buffer_alignment = 512;
1629 18846dee Markus Armbruster
}
1630 18846dee Markus Armbruster
1631 fa879d62 Markus Armbruster
/* TODO change to return DeviceState * when all users are qdevified */
1632 fa879d62 Markus Armbruster
void *bdrv_get_attached_dev(BlockDriverState *bs)
1633 18846dee Markus Armbruster
{
1634 fa879d62 Markus Armbruster
    return bs->dev;
1635 18846dee Markus Armbruster
}
1636 18846dee Markus Armbruster
1637 0e49de52 Markus Armbruster
void bdrv_set_dev_ops(BlockDriverState *bs, const BlockDevOps *ops,
1638 0e49de52 Markus Armbruster
                      void *opaque)
1639 0e49de52 Markus Armbruster
{
1640 0e49de52 Markus Armbruster
    bs->dev_ops = ops;
1641 0e49de52 Markus Armbruster
    bs->dev_opaque = opaque;
1642 0e49de52 Markus Armbruster
}
1643 0e49de52 Markus Armbruster
1644 32c81a4a Paolo Bonzini
void bdrv_emit_qmp_error_event(const BlockDriverState *bdrv,
1645 32c81a4a Paolo Bonzini
                               enum MonitorEvent ev,
1646 32c81a4a Paolo Bonzini
                               BlockErrorAction action, bool is_read)
1647 329c0a48 Luiz Capitulino
{
1648 329c0a48 Luiz Capitulino
    QObject *data;
1649 329c0a48 Luiz Capitulino
    const char *action_str;
1650 329c0a48 Luiz Capitulino
1651 329c0a48 Luiz Capitulino
    switch (action) {
1652 329c0a48 Luiz Capitulino
    case BDRV_ACTION_REPORT:
1653 329c0a48 Luiz Capitulino
        action_str = "report";
1654 329c0a48 Luiz Capitulino
        break;
1655 329c0a48 Luiz Capitulino
    case BDRV_ACTION_IGNORE:
1656 329c0a48 Luiz Capitulino
        action_str = "ignore";
1657 329c0a48 Luiz Capitulino
        break;
1658 329c0a48 Luiz Capitulino
    case BDRV_ACTION_STOP:
1659 329c0a48 Luiz Capitulino
        action_str = "stop";
1660 329c0a48 Luiz Capitulino
        break;
1661 329c0a48 Luiz Capitulino
    default:
1662 329c0a48 Luiz Capitulino
        abort();
1663 329c0a48 Luiz Capitulino
    }
1664 329c0a48 Luiz Capitulino
1665 329c0a48 Luiz Capitulino
    data = qobject_from_jsonf("{ 'device': %s, 'action': %s, 'operation': %s }",
1666 329c0a48 Luiz Capitulino
                              bdrv->device_name,
1667 329c0a48 Luiz Capitulino
                              action_str,
1668 329c0a48 Luiz Capitulino
                              is_read ? "read" : "write");
1669 32c81a4a Paolo Bonzini
    monitor_protocol_event(ev, data);
1670 329c0a48 Luiz Capitulino
1671 329c0a48 Luiz Capitulino
    qobject_decref(data);
1672 329c0a48 Luiz Capitulino
}
1673 329c0a48 Luiz Capitulino
1674 6f382ed2 Luiz Capitulino
static void bdrv_emit_qmp_eject_event(BlockDriverState *bs, bool ejected)
1675 6f382ed2 Luiz Capitulino
{
1676 6f382ed2 Luiz Capitulino
    QObject *data;
1677 6f382ed2 Luiz Capitulino
1678 6f382ed2 Luiz Capitulino
    data = qobject_from_jsonf("{ 'device': %s, 'tray-open': %i }",
1679 6f382ed2 Luiz Capitulino
                              bdrv_get_device_name(bs), ejected);
1680 6f382ed2 Luiz Capitulino
    monitor_protocol_event(QEVENT_DEVICE_TRAY_MOVED, data);
1681 6f382ed2 Luiz Capitulino
1682 6f382ed2 Luiz Capitulino
    qobject_decref(data);
1683 6f382ed2 Luiz Capitulino
}
1684 6f382ed2 Luiz Capitulino
1685 7d4b4ba5 Markus Armbruster
static void bdrv_dev_change_media_cb(BlockDriverState *bs, bool load)
1686 0e49de52 Markus Armbruster
{
1687 145feb17 Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->change_media_cb) {
1688 6f382ed2 Luiz Capitulino
        bool tray_was_closed = !bdrv_dev_is_tray_open(bs);
1689 7d4b4ba5 Markus Armbruster
        bs->dev_ops->change_media_cb(bs->dev_opaque, load);
1690 6f382ed2 Luiz Capitulino
        if (tray_was_closed) {
1691 6f382ed2 Luiz Capitulino
            /* tray open */
1692 6f382ed2 Luiz Capitulino
            bdrv_emit_qmp_eject_event(bs, true);
1693 6f382ed2 Luiz Capitulino
        }
1694 6f382ed2 Luiz Capitulino
        if (load) {
1695 6f382ed2 Luiz Capitulino
            /* tray close */
1696 6f382ed2 Luiz Capitulino
            bdrv_emit_qmp_eject_event(bs, false);
1697 6f382ed2 Luiz Capitulino
        }
1698 145feb17 Markus Armbruster
    }
1699 145feb17 Markus Armbruster
}
1700 145feb17 Markus Armbruster
1701 2c6942fa Markus Armbruster
bool bdrv_dev_has_removable_media(BlockDriverState *bs)
1702 2c6942fa Markus Armbruster
{
1703 2c6942fa Markus Armbruster
    return !bs->dev || (bs->dev_ops && bs->dev_ops->change_media_cb);
1704 2c6942fa Markus Armbruster
}
1705 2c6942fa Markus Armbruster
1706 025ccaa7 Paolo Bonzini
void bdrv_dev_eject_request(BlockDriverState *bs, bool force)
1707 025ccaa7 Paolo Bonzini
{
1708 025ccaa7 Paolo Bonzini
    if (bs->dev_ops && bs->dev_ops->eject_request_cb) {
1709 025ccaa7 Paolo Bonzini
        bs->dev_ops->eject_request_cb(bs->dev_opaque, force);
1710 025ccaa7 Paolo Bonzini
    }
1711 025ccaa7 Paolo Bonzini
}
1712 025ccaa7 Paolo Bonzini
1713 e4def80b Markus Armbruster
bool bdrv_dev_is_tray_open(BlockDriverState *bs)
1714 e4def80b Markus Armbruster
{
1715 e4def80b Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->is_tray_open) {
1716 e4def80b Markus Armbruster
        return bs->dev_ops->is_tray_open(bs->dev_opaque);
1717 e4def80b Markus Armbruster
    }
1718 e4def80b Markus Armbruster
    return false;
1719 e4def80b Markus Armbruster
}
1720 e4def80b Markus Armbruster
1721 145feb17 Markus Armbruster
static void bdrv_dev_resize_cb(BlockDriverState *bs)
1722 145feb17 Markus Armbruster
{
1723 145feb17 Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->resize_cb) {
1724 145feb17 Markus Armbruster
        bs->dev_ops->resize_cb(bs->dev_opaque);
1725 0e49de52 Markus Armbruster
    }
1726 0e49de52 Markus Armbruster
}
1727 0e49de52 Markus Armbruster
1728 f107639a Markus Armbruster
bool bdrv_dev_is_medium_locked(BlockDriverState *bs)
1729 f107639a Markus Armbruster
{
1730 f107639a Markus Armbruster
    if (bs->dev_ops && bs->dev_ops->is_medium_locked) {
1731 f107639a Markus Armbruster
        return bs->dev_ops->is_medium_locked(bs->dev_opaque);
1732 f107639a Markus Armbruster
    }
1733 f107639a Markus Armbruster
    return false;
1734 f107639a Markus Armbruster
}
1735 f107639a Markus Armbruster
1736 e97fc193 aliguori
/*
1737 e97fc193 aliguori
 * Run consistency checks on an image
1738 e97fc193 aliguori
 *
1739 e076f338 Kevin Wolf
 * Returns 0 if the check could be completed (it doesn't mean that the image is
1740 a1c7273b Stefan Weil
 * free of errors) or -errno when an internal error occurred. The results of the
1741 e076f338 Kevin Wolf
 * check are stored in res.
1742 e97fc193 aliguori
 */
1743 4534ff54 Kevin Wolf
int bdrv_check(BlockDriverState *bs, BdrvCheckResult *res, BdrvCheckMode fix)
1744 e97fc193 aliguori
{
1745 e97fc193 aliguori
    if (bs->drv->bdrv_check == NULL) {
1746 e97fc193 aliguori
        return -ENOTSUP;
1747 e97fc193 aliguori
    }
1748 e97fc193 aliguori
1749 e076f338 Kevin Wolf
    memset(res, 0, sizeof(*res));
1750 4534ff54 Kevin Wolf
    return bs->drv->bdrv_check(bs, res, fix);
1751 e97fc193 aliguori
}
1752 e97fc193 aliguori
1753 8a426614 Kevin Wolf
#define COMMIT_BUF_SECTORS 2048
1754 8a426614 Kevin Wolf
1755 33e3963e bellard
/* commit COW file into the raw image */
1756 33e3963e bellard
int bdrv_commit(BlockDriverState *bs)
1757 33e3963e bellard
{
1758 19cb3738 bellard
    BlockDriver *drv = bs->drv;
1759 8a426614 Kevin Wolf
    int64_t sector, total_sectors;
1760 8a426614 Kevin Wolf
    int n, ro, open_flags;
1761 0bce597d Jeff Cody
    int ret = 0;
1762 8a426614 Kevin Wolf
    uint8_t *buf;
1763 c2cba3d9 Jim Meyering
    char filename[PATH_MAX];
1764 33e3963e bellard
1765 19cb3738 bellard
    if (!drv)
1766 19cb3738 bellard
        return -ENOMEDIUM;
1767 4dca4b63 Naphtali Sprei
    
1768 4dca4b63 Naphtali Sprei
    if (!bs->backing_hd) {
1769 4dca4b63 Naphtali Sprei
        return -ENOTSUP;
1770 33e3963e bellard
    }
1771 33e3963e bellard
1772 2d3735d3 Stefan Hajnoczi
    if (bdrv_in_use(bs) || bdrv_in_use(bs->backing_hd)) {
1773 2d3735d3 Stefan Hajnoczi
        return -EBUSY;
1774 2d3735d3 Stefan Hajnoczi
    }
1775 2d3735d3 Stefan Hajnoczi
1776 4dca4b63 Naphtali Sprei
    ro = bs->backing_hd->read_only;
1777 c2cba3d9 Jim Meyering
    /* Use pstrcpy (not strncpy): filename must be NUL-terminated. */
1778 c2cba3d9 Jim Meyering
    pstrcpy(filename, sizeof(filename), bs->backing_hd->filename);
1779 4dca4b63 Naphtali Sprei
    open_flags =  bs->backing_hd->open_flags;
1780 4dca4b63 Naphtali Sprei
1781 4dca4b63 Naphtali Sprei
    if (ro) {
1782 0bce597d Jeff Cody
        if (bdrv_reopen(bs->backing_hd, open_flags | BDRV_O_RDWR, NULL)) {
1783 0bce597d Jeff Cody
            return -EACCES;
1784 4dca4b63 Naphtali Sprei
        }
1785 ea2384d3 bellard
    }
1786 33e3963e bellard
1787 6ea44308 Jan Kiszka
    total_sectors = bdrv_getlength(bs) >> BDRV_SECTOR_BITS;
1788 7267c094 Anthony Liguori
    buf = g_malloc(COMMIT_BUF_SECTORS * BDRV_SECTOR_SIZE);
1789 8a426614 Kevin Wolf
1790 8a426614 Kevin Wolf
    for (sector = 0; sector < total_sectors; sector += n) {
1791 05c4af54 Stefan Hajnoczi
        if (bdrv_is_allocated(bs, sector, COMMIT_BUF_SECTORS, &n)) {
1792 8a426614 Kevin Wolf
1793 8a426614 Kevin Wolf
            if (bdrv_read(bs, sector, buf, n) != 0) {
1794 8a426614 Kevin Wolf
                ret = -EIO;
1795 8a426614 Kevin Wolf
                goto ro_cleanup;
1796 8a426614 Kevin Wolf
            }
1797 8a426614 Kevin Wolf
1798 8a426614 Kevin Wolf
            if (bdrv_write(bs->backing_hd, sector, buf, n) != 0) {
1799 8a426614 Kevin Wolf
                ret = -EIO;
1800 8a426614 Kevin Wolf
                goto ro_cleanup;
1801 8a426614 Kevin Wolf
            }
1802 ea2384d3 bellard
        }
1803 33e3963e bellard
    }
1804 95389c86 bellard
1805 1d44952f Christoph Hellwig
    if (drv->bdrv_make_empty) {
1806 1d44952f Christoph Hellwig
        ret = drv->bdrv_make_empty(bs);
1807 1d44952f Christoph Hellwig
        bdrv_flush(bs);
1808 1d44952f Christoph Hellwig
    }
1809 95389c86 bellard
1810 3f5075ae Christoph Hellwig
    /*
1811 3f5075ae Christoph Hellwig
     * Make sure all data we wrote to the backing device is actually
1812 3f5075ae Christoph Hellwig
     * stable on disk.
1813 3f5075ae Christoph Hellwig
     */
1814 3f5075ae Christoph Hellwig
    if (bs->backing_hd)
1815 3f5075ae Christoph Hellwig
        bdrv_flush(bs->backing_hd);
1816 4dca4b63 Naphtali Sprei
1817 4dca4b63 Naphtali Sprei
ro_cleanup:
1818 7267c094 Anthony Liguori
    g_free(buf);
1819 4dca4b63 Naphtali Sprei
1820 4dca4b63 Naphtali Sprei
    if (ro) {
1821 0bce597d Jeff Cody
        /* ignoring error return here */
1822 0bce597d Jeff Cody
        bdrv_reopen(bs->backing_hd, open_flags & ~BDRV_O_RDWR, NULL);
1823 4dca4b63 Naphtali Sprei
    }
1824 4dca4b63 Naphtali Sprei
1825 1d44952f Christoph Hellwig
    return ret;
1826 33e3963e bellard
}
1827 33e3963e bellard
1828 e8877497 Stefan Hajnoczi
int bdrv_commit_all(void)
1829 6ab4b5ab Markus Armbruster
{
1830 6ab4b5ab Markus Armbruster
    BlockDriverState *bs;
1831 6ab4b5ab Markus Armbruster
1832 6ab4b5ab Markus Armbruster
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
1833 272d2d8e Jeff Cody
        if (bs->drv && bs->backing_hd) {
1834 272d2d8e Jeff Cody
            int ret = bdrv_commit(bs);
1835 272d2d8e Jeff Cody
            if (ret < 0) {
1836 272d2d8e Jeff Cody
                return ret;
1837 272d2d8e Jeff Cody
            }
1838 e8877497 Stefan Hajnoczi
        }
1839 6ab4b5ab Markus Armbruster
    }
1840 e8877497 Stefan Hajnoczi
    return 0;
1841 6ab4b5ab Markus Armbruster
}
1842 6ab4b5ab Markus Armbruster
1843 dbffbdcf Stefan Hajnoczi
struct BdrvTrackedRequest {
1844 dbffbdcf Stefan Hajnoczi
    BlockDriverState *bs;
1845 dbffbdcf Stefan Hajnoczi
    int64_t sector_num;
1846 dbffbdcf Stefan Hajnoczi
    int nb_sectors;
1847 dbffbdcf Stefan Hajnoczi
    bool is_write;
1848 dbffbdcf Stefan Hajnoczi
    QLIST_ENTRY(BdrvTrackedRequest) list;
1849 5f8b6491 Stefan Hajnoczi
    Coroutine *co; /* owner, used for deadlock detection */
1850 f4658285 Stefan Hajnoczi
    CoQueue wait_queue; /* coroutines blocked on this request */
1851 dbffbdcf Stefan Hajnoczi
};
1852 dbffbdcf Stefan Hajnoczi
1853 dbffbdcf Stefan Hajnoczi
/**
1854 dbffbdcf Stefan Hajnoczi
 * Remove an active request from the tracked requests list
1855 dbffbdcf Stefan Hajnoczi
 *
1856 dbffbdcf Stefan Hajnoczi
 * This function should be called when a tracked request is completing.
1857 dbffbdcf Stefan Hajnoczi
 */
1858 dbffbdcf Stefan Hajnoczi
static void tracked_request_end(BdrvTrackedRequest *req)
1859 dbffbdcf Stefan Hajnoczi
{
1860 dbffbdcf Stefan Hajnoczi
    QLIST_REMOVE(req, list);
1861 f4658285 Stefan Hajnoczi
    qemu_co_queue_restart_all(&req->wait_queue);
1862 dbffbdcf Stefan Hajnoczi
}
1863 dbffbdcf Stefan Hajnoczi
1864 dbffbdcf Stefan Hajnoczi
/**
1865 dbffbdcf Stefan Hajnoczi
 * Add an active request to the tracked requests list
1866 dbffbdcf Stefan Hajnoczi
 */
1867 dbffbdcf Stefan Hajnoczi
static void tracked_request_begin(BdrvTrackedRequest *req,
1868 dbffbdcf Stefan Hajnoczi
                                  BlockDriverState *bs,
1869 dbffbdcf Stefan Hajnoczi
                                  int64_t sector_num,
1870 dbffbdcf Stefan Hajnoczi
                                  int nb_sectors, bool is_write)
1871 dbffbdcf Stefan Hajnoczi
{
1872 dbffbdcf Stefan Hajnoczi
    *req = (BdrvTrackedRequest){
1873 dbffbdcf Stefan Hajnoczi
        .bs = bs,
1874 dbffbdcf Stefan Hajnoczi
        .sector_num = sector_num,
1875 dbffbdcf Stefan Hajnoczi
        .nb_sectors = nb_sectors,
1876 dbffbdcf Stefan Hajnoczi
        .is_write = is_write,
1877 5f8b6491 Stefan Hajnoczi
        .co = qemu_coroutine_self(),
1878 dbffbdcf Stefan Hajnoczi
    };
1879 dbffbdcf Stefan Hajnoczi
1880 f4658285 Stefan Hajnoczi
    qemu_co_queue_init(&req->wait_queue);
1881 f4658285 Stefan Hajnoczi
1882 dbffbdcf Stefan Hajnoczi
    QLIST_INSERT_HEAD(&bs->tracked_requests, req, list);
1883 dbffbdcf Stefan Hajnoczi
}
1884 dbffbdcf Stefan Hajnoczi
1885 d83947ac Stefan Hajnoczi
/**
1886 d83947ac Stefan Hajnoczi
 * Round a region to cluster boundaries
1887 d83947ac Stefan Hajnoczi
 */
1888 343bded4 Paolo Bonzini
void bdrv_round_to_clusters(BlockDriverState *bs,
1889 343bded4 Paolo Bonzini
                            int64_t sector_num, int nb_sectors,
1890 343bded4 Paolo Bonzini
                            int64_t *cluster_sector_num,
1891 343bded4 Paolo Bonzini
                            int *cluster_nb_sectors)
1892 d83947ac Stefan Hajnoczi
{
1893 d83947ac Stefan Hajnoczi
    BlockDriverInfo bdi;
1894 d83947ac Stefan Hajnoczi
1895 d83947ac Stefan Hajnoczi
    if (bdrv_get_info(bs, &bdi) < 0 || bdi.cluster_size == 0) {
1896 d83947ac Stefan Hajnoczi
        *cluster_sector_num = sector_num;
1897 d83947ac Stefan Hajnoczi
        *cluster_nb_sectors = nb_sectors;
1898 d83947ac Stefan Hajnoczi
    } else {
1899 d83947ac Stefan Hajnoczi
        int64_t c = bdi.cluster_size / BDRV_SECTOR_SIZE;
1900 d83947ac Stefan Hajnoczi
        *cluster_sector_num = QEMU_ALIGN_DOWN(sector_num, c);
1901 d83947ac Stefan Hajnoczi
        *cluster_nb_sectors = QEMU_ALIGN_UP(sector_num - *cluster_sector_num +
1902 d83947ac Stefan Hajnoczi
                                            nb_sectors, c);
1903 d83947ac Stefan Hajnoczi
    }
1904 d83947ac Stefan Hajnoczi
}
1905 d83947ac Stefan Hajnoczi
1906 f4658285 Stefan Hajnoczi
static bool tracked_request_overlaps(BdrvTrackedRequest *req,
1907 f4658285 Stefan Hajnoczi
                                     int64_t sector_num, int nb_sectors) {
1908 d83947ac Stefan Hajnoczi
    /*        aaaa   bbbb */
1909 d83947ac Stefan Hajnoczi
    if (sector_num >= req->sector_num + req->nb_sectors) {
1910 d83947ac Stefan Hajnoczi
        return false;
1911 d83947ac Stefan Hajnoczi
    }
1912 d83947ac Stefan Hajnoczi
    /* bbbb   aaaa        */
1913 d83947ac Stefan Hajnoczi
    if (req->sector_num >= sector_num + nb_sectors) {
1914 d83947ac Stefan Hajnoczi
        return false;
1915 d83947ac Stefan Hajnoczi
    }
1916 d83947ac Stefan Hajnoczi
    return true;
1917 f4658285 Stefan Hajnoczi
}
1918 f4658285 Stefan Hajnoczi
1919 f4658285 Stefan Hajnoczi
static void coroutine_fn wait_for_overlapping_requests(BlockDriverState *bs,
1920 f4658285 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors)
1921 f4658285 Stefan Hajnoczi
{
1922 f4658285 Stefan Hajnoczi
    BdrvTrackedRequest *req;
1923 d83947ac Stefan Hajnoczi
    int64_t cluster_sector_num;
1924 d83947ac Stefan Hajnoczi
    int cluster_nb_sectors;
1925 f4658285 Stefan Hajnoczi
    bool retry;
1926 f4658285 Stefan Hajnoczi
1927 d83947ac Stefan Hajnoczi
    /* If we touch the same cluster it counts as an overlap.  This guarantees
1928 d83947ac Stefan Hajnoczi
     * that allocating writes will be serialized and not race with each other
1929 d83947ac Stefan Hajnoczi
     * for the same cluster.  For example, in copy-on-read it ensures that the
1930 d83947ac Stefan Hajnoczi
     * CoR read and write operations are atomic and guest writes cannot
1931 d83947ac Stefan Hajnoczi
     * interleave between them.
1932 d83947ac Stefan Hajnoczi
     */
1933 343bded4 Paolo Bonzini
    bdrv_round_to_clusters(bs, sector_num, nb_sectors,
1934 343bded4 Paolo Bonzini
                           &cluster_sector_num, &cluster_nb_sectors);
1935 d83947ac Stefan Hajnoczi
1936 f4658285 Stefan Hajnoczi
    do {
1937 f4658285 Stefan Hajnoczi
        retry = false;
1938 f4658285 Stefan Hajnoczi
        QLIST_FOREACH(req, &bs->tracked_requests, list) {
1939 d83947ac Stefan Hajnoczi
            if (tracked_request_overlaps(req, cluster_sector_num,
1940 d83947ac Stefan Hajnoczi
                                         cluster_nb_sectors)) {
1941 5f8b6491 Stefan Hajnoczi
                /* Hitting this means there was a reentrant request, for
1942 5f8b6491 Stefan Hajnoczi
                 * example, a block driver issuing nested requests.  This must
1943 5f8b6491 Stefan Hajnoczi
                 * never happen since it means deadlock.
1944 5f8b6491 Stefan Hajnoczi
                 */
1945 5f8b6491 Stefan Hajnoczi
                assert(qemu_coroutine_self() != req->co);
1946 5f8b6491 Stefan Hajnoczi
1947 f4658285 Stefan Hajnoczi
                qemu_co_queue_wait(&req->wait_queue);
1948 f4658285 Stefan Hajnoczi
                retry = true;
1949 f4658285 Stefan Hajnoczi
                break;
1950 f4658285 Stefan Hajnoczi
            }
1951 f4658285 Stefan Hajnoczi
        }
1952 f4658285 Stefan Hajnoczi
    } while (retry);
1953 f4658285 Stefan Hajnoczi
}
1954 f4658285 Stefan Hajnoczi
1955 756e6736 Kevin Wolf
/*
1956 756e6736 Kevin Wolf
 * Return values:
1957 756e6736 Kevin Wolf
 * 0        - success
1958 756e6736 Kevin Wolf
 * -EINVAL  - backing format specified, but no file
1959 756e6736 Kevin Wolf
 * -ENOSPC  - can't update the backing file because no space is left in the
1960 756e6736 Kevin Wolf
 *            image file header
1961 756e6736 Kevin Wolf
 * -ENOTSUP - format driver doesn't support changing the backing file
1962 756e6736 Kevin Wolf
 */
1963 756e6736 Kevin Wolf
int bdrv_change_backing_file(BlockDriverState *bs,
1964 756e6736 Kevin Wolf
    const char *backing_file, const char *backing_fmt)
1965 756e6736 Kevin Wolf
{
1966 756e6736 Kevin Wolf
    BlockDriver *drv = bs->drv;
1967 469ef350 Paolo Bonzini
    int ret;
1968 756e6736 Kevin Wolf
1969 5f377794 Paolo Bonzini
    /* Backing file format doesn't make sense without a backing file */
1970 5f377794 Paolo Bonzini
    if (backing_fmt && !backing_file) {
1971 5f377794 Paolo Bonzini
        return -EINVAL;
1972 5f377794 Paolo Bonzini
    }
1973 5f377794 Paolo Bonzini
1974 756e6736 Kevin Wolf
    if (drv->bdrv_change_backing_file != NULL) {
1975 469ef350 Paolo Bonzini
        ret = drv->bdrv_change_backing_file(bs, backing_file, backing_fmt);
1976 756e6736 Kevin Wolf
    } else {
1977 469ef350 Paolo Bonzini
        ret = -ENOTSUP;
1978 756e6736 Kevin Wolf
    }
1979 469ef350 Paolo Bonzini
1980 469ef350 Paolo Bonzini
    if (ret == 0) {
1981 469ef350 Paolo Bonzini
        pstrcpy(bs->backing_file, sizeof(bs->backing_file), backing_file ?: "");
1982 469ef350 Paolo Bonzini
        pstrcpy(bs->backing_format, sizeof(bs->backing_format), backing_fmt ?: "");
1983 469ef350 Paolo Bonzini
    }
1984 469ef350 Paolo Bonzini
    return ret;
1985 756e6736 Kevin Wolf
}
1986 756e6736 Kevin Wolf
1987 6ebdcee2 Jeff Cody
/*
1988 6ebdcee2 Jeff Cody
 * Finds the image layer in the chain that has 'bs' as its backing file.
1989 6ebdcee2 Jeff Cody
 *
1990 6ebdcee2 Jeff Cody
 * active is the current topmost image.
1991 6ebdcee2 Jeff Cody
 *
1992 6ebdcee2 Jeff Cody
 * Returns NULL if bs is not found in active's image chain,
1993 6ebdcee2 Jeff Cody
 * or if active == bs.
1994 6ebdcee2 Jeff Cody
 */
1995 6ebdcee2 Jeff Cody
BlockDriverState *bdrv_find_overlay(BlockDriverState *active,
1996 6ebdcee2 Jeff Cody
                                    BlockDriverState *bs)
1997 6ebdcee2 Jeff Cody
{
1998 6ebdcee2 Jeff Cody
    BlockDriverState *overlay = NULL;
1999 6ebdcee2 Jeff Cody
    BlockDriverState *intermediate;
2000 6ebdcee2 Jeff Cody
2001 6ebdcee2 Jeff Cody
    assert(active != NULL);
2002 6ebdcee2 Jeff Cody
    assert(bs != NULL);
2003 6ebdcee2 Jeff Cody
2004 6ebdcee2 Jeff Cody
    /* if bs is the same as active, then by definition it has no overlay
2005 6ebdcee2 Jeff Cody
     */
2006 6ebdcee2 Jeff Cody
    if (active == bs) {
2007 6ebdcee2 Jeff Cody
        return NULL;
2008 6ebdcee2 Jeff Cody
    }
2009 6ebdcee2 Jeff Cody
2010 6ebdcee2 Jeff Cody
    intermediate = active;
2011 6ebdcee2 Jeff Cody
    while (intermediate->backing_hd) {
2012 6ebdcee2 Jeff Cody
        if (intermediate->backing_hd == bs) {
2013 6ebdcee2 Jeff Cody
            overlay = intermediate;
2014 6ebdcee2 Jeff Cody
            break;
2015 6ebdcee2 Jeff Cody
        }
2016 6ebdcee2 Jeff Cody
        intermediate = intermediate->backing_hd;
2017 6ebdcee2 Jeff Cody
    }
2018 6ebdcee2 Jeff Cody
2019 6ebdcee2 Jeff Cody
    return overlay;
2020 6ebdcee2 Jeff Cody
}
2021 6ebdcee2 Jeff Cody
2022 6ebdcee2 Jeff Cody
typedef struct BlkIntermediateStates {
2023 6ebdcee2 Jeff Cody
    BlockDriverState *bs;
2024 6ebdcee2 Jeff Cody
    QSIMPLEQ_ENTRY(BlkIntermediateStates) entry;
2025 6ebdcee2 Jeff Cody
} BlkIntermediateStates;
2026 6ebdcee2 Jeff Cody
2027 6ebdcee2 Jeff Cody
2028 6ebdcee2 Jeff Cody
/*
2029 6ebdcee2 Jeff Cody
 * Drops images above 'base' up to and including 'top', and sets the image
2030 6ebdcee2 Jeff Cody
 * above 'top' to have base as its backing file.
2031 6ebdcee2 Jeff Cody
 *
2032 6ebdcee2 Jeff Cody
 * Requires that the overlay to 'top' is opened r/w, so that the backing file
2033 6ebdcee2 Jeff Cody
 * information in 'bs' can be properly updated.
2034 6ebdcee2 Jeff Cody
 *
2035 6ebdcee2 Jeff Cody
 * E.g., this will convert the following chain:
2036 6ebdcee2 Jeff Cody
 * bottom <- base <- intermediate <- top <- active
2037 6ebdcee2 Jeff Cody
 *
2038 6ebdcee2 Jeff Cody
 * to
2039 6ebdcee2 Jeff Cody
 *
2040 6ebdcee2 Jeff Cody
 * bottom <- base <- active
2041 6ebdcee2 Jeff Cody
 *
2042 6ebdcee2 Jeff Cody
 * It is allowed for bottom==base, in which case it converts:
2043 6ebdcee2 Jeff Cody
 *
2044 6ebdcee2 Jeff Cody
 * base <- intermediate <- top <- active
2045 6ebdcee2 Jeff Cody
 *
2046 6ebdcee2 Jeff Cody
 * to
2047 6ebdcee2 Jeff Cody
 *
2048 6ebdcee2 Jeff Cody
 * base <- active
2049 6ebdcee2 Jeff Cody
 *
2050 6ebdcee2 Jeff Cody
 * Error conditions:
2051 6ebdcee2 Jeff Cody
 *  if active == top, that is considered an error
2052 6ebdcee2 Jeff Cody
 *
2053 6ebdcee2 Jeff Cody
 */
2054 6ebdcee2 Jeff Cody
int bdrv_drop_intermediate(BlockDriverState *active, BlockDriverState *top,
2055 6ebdcee2 Jeff Cody
                           BlockDriverState *base)
2056 6ebdcee2 Jeff Cody
{
2057 6ebdcee2 Jeff Cody
    BlockDriverState *intermediate;
2058 6ebdcee2 Jeff Cody
    BlockDriverState *base_bs = NULL;
2059 6ebdcee2 Jeff Cody
    BlockDriverState *new_top_bs = NULL;
2060 6ebdcee2 Jeff Cody
    BlkIntermediateStates *intermediate_state, *next;
2061 6ebdcee2 Jeff Cody
    int ret = -EIO;
2062 6ebdcee2 Jeff Cody
2063 6ebdcee2 Jeff Cody
    QSIMPLEQ_HEAD(states_to_delete, BlkIntermediateStates) states_to_delete;
2064 6ebdcee2 Jeff Cody
    QSIMPLEQ_INIT(&states_to_delete);
2065 6ebdcee2 Jeff Cody
2066 6ebdcee2 Jeff Cody
    if (!top->drv || !base->drv) {
2067 6ebdcee2 Jeff Cody
        goto exit;
2068 6ebdcee2 Jeff Cody
    }
2069 6ebdcee2 Jeff Cody
2070 6ebdcee2 Jeff Cody
    new_top_bs = bdrv_find_overlay(active, top);
2071 6ebdcee2 Jeff Cody
2072 6ebdcee2 Jeff Cody
    if (new_top_bs == NULL) {
2073 6ebdcee2 Jeff Cody
        /* we could not find the image above 'top', this is an error */
2074 6ebdcee2 Jeff Cody
        goto exit;
2075 6ebdcee2 Jeff Cody
    }
2076 6ebdcee2 Jeff Cody
2077 6ebdcee2 Jeff Cody
    /* special case of new_top_bs->backing_hd already pointing to base - nothing
2078 6ebdcee2 Jeff Cody
     * to do, no intermediate images */
2079 6ebdcee2 Jeff Cody
    if (new_top_bs->backing_hd == base) {
2080 6ebdcee2 Jeff Cody
        ret = 0;
2081 6ebdcee2 Jeff Cody
        goto exit;
2082 6ebdcee2 Jeff Cody
    }
2083 6ebdcee2 Jeff Cody
2084 6ebdcee2 Jeff Cody
    intermediate = top;
2085 6ebdcee2 Jeff Cody
2086 6ebdcee2 Jeff Cody
    /* now we will go down through the list, and add each BDS we find
2087 6ebdcee2 Jeff Cody
     * into our deletion queue, until we hit the 'base'
2088 6ebdcee2 Jeff Cody
     */
2089 6ebdcee2 Jeff Cody
    while (intermediate) {
2090 6ebdcee2 Jeff Cody
        intermediate_state = g_malloc0(sizeof(BlkIntermediateStates));
2091 6ebdcee2 Jeff Cody
        intermediate_state->bs = intermediate;
2092 6ebdcee2 Jeff Cody
        QSIMPLEQ_INSERT_TAIL(&states_to_delete, intermediate_state, entry);
2093 6ebdcee2 Jeff Cody
2094 6ebdcee2 Jeff Cody
        if (intermediate->backing_hd == base) {
2095 6ebdcee2 Jeff Cody
            base_bs = intermediate->backing_hd;
2096 6ebdcee2 Jeff Cody
            break;
2097 6ebdcee2 Jeff Cody
        }
2098 6ebdcee2 Jeff Cody
        intermediate = intermediate->backing_hd;
2099 6ebdcee2 Jeff Cody
    }
2100 6ebdcee2 Jeff Cody
    if (base_bs == NULL) {
2101 6ebdcee2 Jeff Cody
        /* something went wrong, we did not end at the base. safely
2102 6ebdcee2 Jeff Cody
         * unravel everything, and exit with error */
2103 6ebdcee2 Jeff Cody
        goto exit;
2104 6ebdcee2 Jeff Cody
    }
2105 6ebdcee2 Jeff Cody
2106 6ebdcee2 Jeff Cody
    /* success - we can delete the intermediate states, and link top->base */
2107 6ebdcee2 Jeff Cody
    ret = bdrv_change_backing_file(new_top_bs, base_bs->filename,
2108 6ebdcee2 Jeff Cody
                                   base_bs->drv ? base_bs->drv->format_name : "");
2109 6ebdcee2 Jeff Cody
    if (ret) {
2110 6ebdcee2 Jeff Cody
        goto exit;
2111 6ebdcee2 Jeff Cody
    }
2112 6ebdcee2 Jeff Cody
    new_top_bs->backing_hd = base_bs;
2113 6ebdcee2 Jeff Cody
2114 6ebdcee2 Jeff Cody
2115 6ebdcee2 Jeff Cody
    QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2116 6ebdcee2 Jeff Cody
        /* so that bdrv_close() does not recursively close the chain */
2117 6ebdcee2 Jeff Cody
        intermediate_state->bs->backing_hd = NULL;
2118 6ebdcee2 Jeff Cody
        bdrv_delete(intermediate_state->bs);
2119 6ebdcee2 Jeff Cody
    }
2120 6ebdcee2 Jeff Cody
    ret = 0;
2121 6ebdcee2 Jeff Cody
2122 6ebdcee2 Jeff Cody
exit:
2123 6ebdcee2 Jeff Cody
    QSIMPLEQ_FOREACH_SAFE(intermediate_state, &states_to_delete, entry, next) {
2124 6ebdcee2 Jeff Cody
        g_free(intermediate_state);
2125 6ebdcee2 Jeff Cody
    }
2126 6ebdcee2 Jeff Cody
    return ret;
2127 6ebdcee2 Jeff Cody
}
2128 6ebdcee2 Jeff Cody
2129 6ebdcee2 Jeff Cody
2130 71d0770c aliguori
static int bdrv_check_byte_request(BlockDriverState *bs, int64_t offset,
2131 71d0770c aliguori
                                   size_t size)
2132 71d0770c aliguori
{
2133 71d0770c aliguori
    int64_t len;
2134 71d0770c aliguori
2135 71d0770c aliguori
    if (!bdrv_is_inserted(bs))
2136 71d0770c aliguori
        return -ENOMEDIUM;
2137 71d0770c aliguori
2138 71d0770c aliguori
    if (bs->growable)
2139 71d0770c aliguori
        return 0;
2140 71d0770c aliguori
2141 71d0770c aliguori
    len = bdrv_getlength(bs);
2142 71d0770c aliguori
2143 fbb7b4e0 Kevin Wolf
    if (offset < 0)
2144 fbb7b4e0 Kevin Wolf
        return -EIO;
2145 fbb7b4e0 Kevin Wolf
2146 fbb7b4e0 Kevin Wolf
    if ((offset > len) || (len - offset < size))
2147 71d0770c aliguori
        return -EIO;
2148 71d0770c aliguori
2149 71d0770c aliguori
    return 0;
2150 71d0770c aliguori
}
2151 71d0770c aliguori
2152 71d0770c aliguori
static int bdrv_check_request(BlockDriverState *bs, int64_t sector_num,
2153 71d0770c aliguori
                              int nb_sectors)
2154 71d0770c aliguori
{
2155 eb5a3165 Jes Sorensen
    return bdrv_check_byte_request(bs, sector_num * BDRV_SECTOR_SIZE,
2156 eb5a3165 Jes Sorensen
                                   nb_sectors * BDRV_SECTOR_SIZE);
2157 71d0770c aliguori
}
2158 71d0770c aliguori
2159 1c9805a3 Stefan Hajnoczi
typedef struct RwCo {
2160 1c9805a3 Stefan Hajnoczi
    BlockDriverState *bs;
2161 1c9805a3 Stefan Hajnoczi
    int64_t sector_num;
2162 1c9805a3 Stefan Hajnoczi
    int nb_sectors;
2163 1c9805a3 Stefan Hajnoczi
    QEMUIOVector *qiov;
2164 1c9805a3 Stefan Hajnoczi
    bool is_write;
2165 1c9805a3 Stefan Hajnoczi
    int ret;
2166 1c9805a3 Stefan Hajnoczi
} RwCo;
2167 1c9805a3 Stefan Hajnoczi
2168 1c9805a3 Stefan Hajnoczi
static void coroutine_fn bdrv_rw_co_entry(void *opaque)
2169 fc01f7e7 bellard
{
2170 1c9805a3 Stefan Hajnoczi
    RwCo *rwco = opaque;
2171 ea2384d3 bellard
2172 1c9805a3 Stefan Hajnoczi
    if (!rwco->is_write) {
2173 1c9805a3 Stefan Hajnoczi
        rwco->ret = bdrv_co_do_readv(rwco->bs, rwco->sector_num,
2174 470c0504 Stefan Hajnoczi
                                     rwco->nb_sectors, rwco->qiov, 0);
2175 1c9805a3 Stefan Hajnoczi
    } else {
2176 1c9805a3 Stefan Hajnoczi
        rwco->ret = bdrv_co_do_writev(rwco->bs, rwco->sector_num,
2177 f08f2dda Stefan Hajnoczi
                                      rwco->nb_sectors, rwco->qiov, 0);
2178 1c9805a3 Stefan Hajnoczi
    }
2179 1c9805a3 Stefan Hajnoczi
}
2180 e7a8a783 Kevin Wolf
2181 1c9805a3 Stefan Hajnoczi
/*
2182 8d3b1a2d Kevin Wolf
 * Process a vectored synchronous request using coroutines
2183 1c9805a3 Stefan Hajnoczi
 */
2184 8d3b1a2d Kevin Wolf
static int bdrv_rwv_co(BlockDriverState *bs, int64_t sector_num,
2185 8d3b1a2d Kevin Wolf
                       QEMUIOVector *qiov, bool is_write)
2186 1c9805a3 Stefan Hajnoczi
{
2187 1c9805a3 Stefan Hajnoczi
    Coroutine *co;
2188 1c9805a3 Stefan Hajnoczi
    RwCo rwco = {
2189 1c9805a3 Stefan Hajnoczi
        .bs = bs,
2190 1c9805a3 Stefan Hajnoczi
        .sector_num = sector_num,
2191 8d3b1a2d Kevin Wolf
        .nb_sectors = qiov->size >> BDRV_SECTOR_BITS,
2192 8d3b1a2d Kevin Wolf
        .qiov = qiov,
2193 1c9805a3 Stefan Hajnoczi
        .is_write = is_write,
2194 1c9805a3 Stefan Hajnoczi
        .ret = NOT_DONE,
2195 1c9805a3 Stefan Hajnoczi
    };
2196 8d3b1a2d Kevin Wolf
    assert((qiov->size & (BDRV_SECTOR_SIZE - 1)) == 0);
2197 e7a8a783 Kevin Wolf
2198 498e386c Zhi Yong Wu
    /**
2199 498e386c Zhi Yong Wu
     * In sync call context, when the vcpu is blocked, this throttling timer
2200 498e386c Zhi Yong Wu
     * will not fire; so the I/O throttling function has to be disabled here
2201 498e386c Zhi Yong Wu
     * if it has been enabled.
2202 498e386c Zhi Yong Wu
     */
2203 498e386c Zhi Yong Wu
    if (bs->io_limits_enabled) {
2204 498e386c Zhi Yong Wu
        fprintf(stderr, "Disabling I/O throttling on '%s' due "
2205 498e386c Zhi Yong Wu
                        "to synchronous I/O.\n", bdrv_get_device_name(bs));
2206 498e386c Zhi Yong Wu
        bdrv_io_limits_disable(bs);
2207 498e386c Zhi Yong Wu
    }
2208 498e386c Zhi Yong Wu
2209 1c9805a3 Stefan Hajnoczi
    if (qemu_in_coroutine()) {
2210 1c9805a3 Stefan Hajnoczi
        /* Fast-path if already in coroutine context */
2211 1c9805a3 Stefan Hajnoczi
        bdrv_rw_co_entry(&rwco);
2212 1c9805a3 Stefan Hajnoczi
    } else {
2213 1c9805a3 Stefan Hajnoczi
        co = qemu_coroutine_create(bdrv_rw_co_entry);
2214 1c9805a3 Stefan Hajnoczi
        qemu_coroutine_enter(co, &rwco);
2215 1c9805a3 Stefan Hajnoczi
        while (rwco.ret == NOT_DONE) {
2216 1c9805a3 Stefan Hajnoczi
            qemu_aio_wait();
2217 1c9805a3 Stefan Hajnoczi
        }
2218 1c9805a3 Stefan Hajnoczi
    }
2219 1c9805a3 Stefan Hajnoczi
    return rwco.ret;
2220 1c9805a3 Stefan Hajnoczi
}
2221 b338082b bellard
2222 8d3b1a2d Kevin Wolf
/*
2223 8d3b1a2d Kevin Wolf
 * Process a synchronous request using coroutines
2224 8d3b1a2d Kevin Wolf
 */
2225 8d3b1a2d Kevin Wolf
static int bdrv_rw_co(BlockDriverState *bs, int64_t sector_num, uint8_t *buf,
2226 8d3b1a2d Kevin Wolf
                      int nb_sectors, bool is_write)
2227 8d3b1a2d Kevin Wolf
{
2228 8d3b1a2d Kevin Wolf
    QEMUIOVector qiov;
2229 8d3b1a2d Kevin Wolf
    struct iovec iov = {
2230 8d3b1a2d Kevin Wolf
        .iov_base = (void *)buf,
2231 8d3b1a2d Kevin Wolf
        .iov_len = nb_sectors * BDRV_SECTOR_SIZE,
2232 8d3b1a2d Kevin Wolf
    };
2233 8d3b1a2d Kevin Wolf
2234 8d3b1a2d Kevin Wolf
    qemu_iovec_init_external(&qiov, &iov, 1);
2235 8d3b1a2d Kevin Wolf
    return bdrv_rwv_co(bs, sector_num, &qiov, is_write);
2236 8d3b1a2d Kevin Wolf
}
2237 8d3b1a2d Kevin Wolf
2238 1c9805a3 Stefan Hajnoczi
/* return < 0 if error. See bdrv_write() for the return codes */
2239 1c9805a3 Stefan Hajnoczi
int bdrv_read(BlockDriverState *bs, int64_t sector_num,
2240 1c9805a3 Stefan Hajnoczi
              uint8_t *buf, int nb_sectors)
2241 1c9805a3 Stefan Hajnoczi
{
2242 1c9805a3 Stefan Hajnoczi
    return bdrv_rw_co(bs, sector_num, buf, nb_sectors, false);
2243 fc01f7e7 bellard
}
2244 fc01f7e7 bellard
2245 07d27a44 Markus Armbruster
/* Just like bdrv_read(), but with I/O throttling temporarily disabled */
2246 07d27a44 Markus Armbruster
int bdrv_read_unthrottled(BlockDriverState *bs, int64_t sector_num,
2247 07d27a44 Markus Armbruster
                          uint8_t *buf, int nb_sectors)
2248 07d27a44 Markus Armbruster
{
2249 07d27a44 Markus Armbruster
    bool enabled;
2250 07d27a44 Markus Armbruster
    int ret;
2251 07d27a44 Markus Armbruster
2252 07d27a44 Markus Armbruster
    enabled = bs->io_limits_enabled;
2253 07d27a44 Markus Armbruster
    bs->io_limits_enabled = false;
2254 07d27a44 Markus Armbruster
    ret = bdrv_read(bs, 0, buf, 1);
2255 07d27a44 Markus Armbruster
    bs->io_limits_enabled = enabled;
2256 07d27a44 Markus Armbruster
    return ret;
2257 07d27a44 Markus Armbruster
}
2258 07d27a44 Markus Armbruster
2259 5fafdf24 ths
/* Return < 0 if error. Important errors are:
2260 19cb3738 bellard
  -EIO         generic I/O error (may happen for all errors)
2261 19cb3738 bellard
  -ENOMEDIUM   No media inserted.
2262 19cb3738 bellard
  -EINVAL      Invalid sector number or nb_sectors
2263 19cb3738 bellard
  -EACCES      Trying to write a read-only device
2264 19cb3738 bellard
*/
2265 5fafdf24 ths
int bdrv_write(BlockDriverState *bs, int64_t sector_num,
2266 fc01f7e7 bellard
               const uint8_t *buf, int nb_sectors)
2267 fc01f7e7 bellard
{
2268 1c9805a3 Stefan Hajnoczi
    return bdrv_rw_co(bs, sector_num, (uint8_t *)buf, nb_sectors, true);
2269 83f64091 bellard
}
2270 83f64091 bellard
2271 8d3b1a2d Kevin Wolf
int bdrv_writev(BlockDriverState *bs, int64_t sector_num, QEMUIOVector *qiov)
2272 8d3b1a2d Kevin Wolf
{
2273 8d3b1a2d Kevin Wolf
    return bdrv_rwv_co(bs, sector_num, qiov, true);
2274 8d3b1a2d Kevin Wolf
}
2275 8d3b1a2d Kevin Wolf
2276 eda578e5 aliguori
int bdrv_pread(BlockDriverState *bs, int64_t offset,
2277 eda578e5 aliguori
               void *buf, int count1)
2278 83f64091 bellard
{
2279 6ea44308 Jan Kiszka
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
2280 83f64091 bellard
    int len, nb_sectors, count;
2281 83f64091 bellard
    int64_t sector_num;
2282 9a8c4cce Kevin Wolf
    int ret;
2283 83f64091 bellard
2284 83f64091 bellard
    count = count1;
2285 83f64091 bellard
    /* first read to align to sector start */
2286 6ea44308 Jan Kiszka
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
2287 83f64091 bellard
    if (len > count)
2288 83f64091 bellard
        len = count;
2289 6ea44308 Jan Kiszka
    sector_num = offset >> BDRV_SECTOR_BITS;
2290 83f64091 bellard
    if (len > 0) {
2291 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2292 9a8c4cce Kevin Wolf
            return ret;
2293 6ea44308 Jan Kiszka
        memcpy(buf, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)), len);
2294 83f64091 bellard
        count -= len;
2295 83f64091 bellard
        if (count == 0)
2296 83f64091 bellard
            return count1;
2297 83f64091 bellard
        sector_num++;
2298 83f64091 bellard
        buf += len;
2299 83f64091 bellard
    }
2300 83f64091 bellard
2301 83f64091 bellard
    /* read the sectors "in place" */
2302 6ea44308 Jan Kiszka
    nb_sectors = count >> BDRV_SECTOR_BITS;
2303 83f64091 bellard
    if (nb_sectors > 0) {
2304 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, buf, nb_sectors)) < 0)
2305 9a8c4cce Kevin Wolf
            return ret;
2306 83f64091 bellard
        sector_num += nb_sectors;
2307 6ea44308 Jan Kiszka
        len = nb_sectors << BDRV_SECTOR_BITS;
2308 83f64091 bellard
        buf += len;
2309 83f64091 bellard
        count -= len;
2310 83f64091 bellard
    }
2311 83f64091 bellard
2312 83f64091 bellard
    /* add data from the last sector */
2313 83f64091 bellard
    if (count > 0) {
2314 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2315 9a8c4cce Kevin Wolf
            return ret;
2316 83f64091 bellard
        memcpy(buf, tmp_buf, count);
2317 83f64091 bellard
    }
2318 83f64091 bellard
    return count1;
2319 83f64091 bellard
}
2320 83f64091 bellard
2321 8d3b1a2d Kevin Wolf
int bdrv_pwritev(BlockDriverState *bs, int64_t offset, QEMUIOVector *qiov)
2322 83f64091 bellard
{
2323 6ea44308 Jan Kiszka
    uint8_t tmp_buf[BDRV_SECTOR_SIZE];
2324 83f64091 bellard
    int len, nb_sectors, count;
2325 83f64091 bellard
    int64_t sector_num;
2326 9a8c4cce Kevin Wolf
    int ret;
2327 83f64091 bellard
2328 8d3b1a2d Kevin Wolf
    count = qiov->size;
2329 8d3b1a2d Kevin Wolf
2330 83f64091 bellard
    /* first write to align to sector start */
2331 6ea44308 Jan Kiszka
    len = (BDRV_SECTOR_SIZE - offset) & (BDRV_SECTOR_SIZE - 1);
2332 83f64091 bellard
    if (len > count)
2333 83f64091 bellard
        len = count;
2334 6ea44308 Jan Kiszka
    sector_num = offset >> BDRV_SECTOR_BITS;
2335 83f64091 bellard
    if (len > 0) {
2336 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2337 9a8c4cce Kevin Wolf
            return ret;
2338 8d3b1a2d Kevin Wolf
        qemu_iovec_to_buf(qiov, 0, tmp_buf + (offset & (BDRV_SECTOR_SIZE - 1)),
2339 8d3b1a2d Kevin Wolf
                          len);
2340 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
2341 9a8c4cce Kevin Wolf
            return ret;
2342 83f64091 bellard
        count -= len;
2343 83f64091 bellard
        if (count == 0)
2344 8d3b1a2d Kevin Wolf
            return qiov->size;
2345 83f64091 bellard
        sector_num++;
2346 83f64091 bellard
    }
2347 83f64091 bellard
2348 83f64091 bellard
    /* write the sectors "in place" */
2349 6ea44308 Jan Kiszka
    nb_sectors = count >> BDRV_SECTOR_BITS;
2350 83f64091 bellard
    if (nb_sectors > 0) {
2351 8d3b1a2d Kevin Wolf
        QEMUIOVector qiov_inplace;
2352 8d3b1a2d Kevin Wolf
2353 8d3b1a2d Kevin Wolf
        qemu_iovec_init(&qiov_inplace, qiov->niov);
2354 8d3b1a2d Kevin Wolf
        qemu_iovec_concat(&qiov_inplace, qiov, len,
2355 8d3b1a2d Kevin Wolf
                          nb_sectors << BDRV_SECTOR_BITS);
2356 8d3b1a2d Kevin Wolf
        ret = bdrv_writev(bs, sector_num, &qiov_inplace);
2357 8d3b1a2d Kevin Wolf
        qemu_iovec_destroy(&qiov_inplace);
2358 8d3b1a2d Kevin Wolf
        if (ret < 0) {
2359 9a8c4cce Kevin Wolf
            return ret;
2360 8d3b1a2d Kevin Wolf
        }
2361 8d3b1a2d Kevin Wolf
2362 83f64091 bellard
        sector_num += nb_sectors;
2363 6ea44308 Jan Kiszka
        len = nb_sectors << BDRV_SECTOR_BITS;
2364 83f64091 bellard
        count -= len;
2365 83f64091 bellard
    }
2366 83f64091 bellard
2367 83f64091 bellard
    /* add data from the last sector */
2368 83f64091 bellard
    if (count > 0) {
2369 9a8c4cce Kevin Wolf
        if ((ret = bdrv_read(bs, sector_num, tmp_buf, 1)) < 0)
2370 9a8c4cce Kevin Wolf
            return ret;
2371 8d3b1a2d Kevin Wolf
        qemu_iovec_to_buf(qiov, qiov->size - count, tmp_buf, count);
2372 9a8c4cce Kevin Wolf
        if ((ret = bdrv_write(bs, sector_num, tmp_buf, 1)) < 0)
2373 9a8c4cce Kevin Wolf
            return ret;
2374 83f64091 bellard
    }
2375 8d3b1a2d Kevin Wolf
    return qiov->size;
2376 8d3b1a2d Kevin Wolf
}
2377 8d3b1a2d Kevin Wolf
2378 8d3b1a2d Kevin Wolf
int bdrv_pwrite(BlockDriverState *bs, int64_t offset,
2379 8d3b1a2d Kevin Wolf
                const void *buf, int count1)
2380 8d3b1a2d Kevin Wolf
{
2381 8d3b1a2d Kevin Wolf
    QEMUIOVector qiov;
2382 8d3b1a2d Kevin Wolf
    struct iovec iov = {
2383 8d3b1a2d Kevin Wolf
        .iov_base   = (void *) buf,
2384 8d3b1a2d Kevin Wolf
        .iov_len    = count1,
2385 8d3b1a2d Kevin Wolf
    };
2386 8d3b1a2d Kevin Wolf
2387 8d3b1a2d Kevin Wolf
    qemu_iovec_init_external(&qiov, &iov, 1);
2388 8d3b1a2d Kevin Wolf
    return bdrv_pwritev(bs, offset, &qiov);
2389 83f64091 bellard
}
2390 83f64091 bellard
2391 f08145fe Kevin Wolf
/*
2392 f08145fe Kevin Wolf
 * Writes to the file and ensures that no writes are reordered across this
2393 f08145fe Kevin Wolf
 * request (acts as a barrier)
2394 f08145fe Kevin Wolf
 *
2395 f08145fe Kevin Wolf
 * Returns 0 on success, -errno in error cases.
2396 f08145fe Kevin Wolf
 */
2397 f08145fe Kevin Wolf
int bdrv_pwrite_sync(BlockDriverState *bs, int64_t offset,
2398 f08145fe Kevin Wolf
    const void *buf, int count)
2399 f08145fe Kevin Wolf
{
2400 f08145fe Kevin Wolf
    int ret;
2401 f08145fe Kevin Wolf
2402 f08145fe Kevin Wolf
    ret = bdrv_pwrite(bs, offset, buf, count);
2403 f08145fe Kevin Wolf
    if (ret < 0) {
2404 f08145fe Kevin Wolf
        return ret;
2405 f08145fe Kevin Wolf
    }
2406 f08145fe Kevin Wolf
2407 f05fa4ad Paolo Bonzini
    /* No flush needed for cache modes that already do it */
2408 f05fa4ad Paolo Bonzini
    if (bs->enable_write_cache) {
2409 f08145fe Kevin Wolf
        bdrv_flush(bs);
2410 f08145fe Kevin Wolf
    }
2411 f08145fe Kevin Wolf
2412 f08145fe Kevin Wolf
    return 0;
2413 f08145fe Kevin Wolf
}
2414 f08145fe Kevin Wolf
2415 470c0504 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_copy_on_readv(BlockDriverState *bs,
2416 ab185921 Stefan Hajnoczi
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
2417 ab185921 Stefan Hajnoczi
{
2418 ab185921 Stefan Hajnoczi
    /* Perform I/O through a temporary buffer so that users who scribble over
2419 ab185921 Stefan Hajnoczi
     * their read buffer while the operation is in progress do not end up
2420 ab185921 Stefan Hajnoczi
     * modifying the image file.  This is critical for zero-copy guest I/O
2421 ab185921 Stefan Hajnoczi
     * where anything might happen inside guest memory.
2422 ab185921 Stefan Hajnoczi
     */
2423 ab185921 Stefan Hajnoczi
    void *bounce_buffer;
2424 ab185921 Stefan Hajnoczi
2425 79c053bd Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
2426 ab185921 Stefan Hajnoczi
    struct iovec iov;
2427 ab185921 Stefan Hajnoczi
    QEMUIOVector bounce_qiov;
2428 ab185921 Stefan Hajnoczi
    int64_t cluster_sector_num;
2429 ab185921 Stefan Hajnoczi
    int cluster_nb_sectors;
2430 ab185921 Stefan Hajnoczi
    size_t skip_bytes;
2431 ab185921 Stefan Hajnoczi
    int ret;
2432 ab185921 Stefan Hajnoczi
2433 ab185921 Stefan Hajnoczi
    /* Cover entire cluster so no additional backing file I/O is required when
2434 ab185921 Stefan Hajnoczi
     * allocating cluster in the image file.
2435 ab185921 Stefan Hajnoczi
     */
2436 343bded4 Paolo Bonzini
    bdrv_round_to_clusters(bs, sector_num, nb_sectors,
2437 343bded4 Paolo Bonzini
                           &cluster_sector_num, &cluster_nb_sectors);
2438 ab185921 Stefan Hajnoczi
2439 470c0504 Stefan Hajnoczi
    trace_bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors,
2440 470c0504 Stefan Hajnoczi
                                   cluster_sector_num, cluster_nb_sectors);
2441 ab185921 Stefan Hajnoczi
2442 ab185921 Stefan Hajnoczi
    iov.iov_len = cluster_nb_sectors * BDRV_SECTOR_SIZE;
2443 ab185921 Stefan Hajnoczi
    iov.iov_base = bounce_buffer = qemu_blockalign(bs, iov.iov_len);
2444 ab185921 Stefan Hajnoczi
    qemu_iovec_init_external(&bounce_qiov, &iov, 1);
2445 ab185921 Stefan Hajnoczi
2446 79c053bd Stefan Hajnoczi
    ret = drv->bdrv_co_readv(bs, cluster_sector_num, cluster_nb_sectors,
2447 79c053bd Stefan Hajnoczi
                             &bounce_qiov);
2448 ab185921 Stefan Hajnoczi
    if (ret < 0) {
2449 ab185921 Stefan Hajnoczi
        goto err;
2450 ab185921 Stefan Hajnoczi
    }
2451 ab185921 Stefan Hajnoczi
2452 79c053bd Stefan Hajnoczi
    if (drv->bdrv_co_write_zeroes &&
2453 79c053bd Stefan Hajnoczi
        buffer_is_zero(bounce_buffer, iov.iov_len)) {
2454 621f0589 Kevin Wolf
        ret = bdrv_co_do_write_zeroes(bs, cluster_sector_num,
2455 621f0589 Kevin Wolf
                                      cluster_nb_sectors);
2456 79c053bd Stefan Hajnoczi
    } else {
2457 f05fa4ad Paolo Bonzini
        /* This does not change the data on the disk, it is not necessary
2458 f05fa4ad Paolo Bonzini
         * to flush even in cache=writethrough mode.
2459 f05fa4ad Paolo Bonzini
         */
2460 79c053bd Stefan Hajnoczi
        ret = drv->bdrv_co_writev(bs, cluster_sector_num, cluster_nb_sectors,
2461 ab185921 Stefan Hajnoczi
                                  &bounce_qiov);
2462 79c053bd Stefan Hajnoczi
    }
2463 79c053bd Stefan Hajnoczi
2464 ab185921 Stefan Hajnoczi
    if (ret < 0) {
2465 ab185921 Stefan Hajnoczi
        /* It might be okay to ignore write errors for guest requests.  If this
2466 ab185921 Stefan Hajnoczi
         * is a deliberate copy-on-read then we don't want to ignore the error.
2467 ab185921 Stefan Hajnoczi
         * Simply report it in all cases.
2468 ab185921 Stefan Hajnoczi
         */
2469 ab185921 Stefan Hajnoczi
        goto err;
2470 ab185921 Stefan Hajnoczi
    }
2471 ab185921 Stefan Hajnoczi
2472 ab185921 Stefan Hajnoczi
    skip_bytes = (sector_num - cluster_sector_num) * BDRV_SECTOR_SIZE;
2473 03396148 Michael Tokarev
    qemu_iovec_from_buf(qiov, 0, bounce_buffer + skip_bytes,
2474 03396148 Michael Tokarev
                        nb_sectors * BDRV_SECTOR_SIZE);
2475 ab185921 Stefan Hajnoczi
2476 ab185921 Stefan Hajnoczi
err:
2477 ab185921 Stefan Hajnoczi
    qemu_vfree(bounce_buffer);
2478 ab185921 Stefan Hajnoczi
    return ret;
2479 ab185921 Stefan Hajnoczi
}
2480 ab185921 Stefan Hajnoczi
2481 c5fbe571 Stefan Hajnoczi
/*
2482 c5fbe571 Stefan Hajnoczi
 * Handle a read request in coroutine context
2483 c5fbe571 Stefan Hajnoczi
 */
2484 c5fbe571 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_readv(BlockDriverState *bs,
2485 470c0504 Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
2486 470c0504 Stefan Hajnoczi
    BdrvRequestFlags flags)
2487 da1fa91d Kevin Wolf
{
2488 da1fa91d Kevin Wolf
    BlockDriver *drv = bs->drv;
2489 dbffbdcf Stefan Hajnoczi
    BdrvTrackedRequest req;
2490 dbffbdcf Stefan Hajnoczi
    int ret;
2491 da1fa91d Kevin Wolf
2492 da1fa91d Kevin Wolf
    if (!drv) {
2493 da1fa91d Kevin Wolf
        return -ENOMEDIUM;
2494 da1fa91d Kevin Wolf
    }
2495 da1fa91d Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2496 da1fa91d Kevin Wolf
        return -EIO;
2497 da1fa91d Kevin Wolf
    }
2498 da1fa91d Kevin Wolf
2499 98f90dba Zhi Yong Wu
    /* throttling disk read I/O */
2500 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
2501 98f90dba Zhi Yong Wu
        bdrv_io_limits_intercept(bs, false, nb_sectors);
2502 98f90dba Zhi Yong Wu
    }
2503 98f90dba Zhi Yong Wu
2504 f4658285 Stefan Hajnoczi
    if (bs->copy_on_read) {
2505 470c0504 Stefan Hajnoczi
        flags |= BDRV_REQ_COPY_ON_READ;
2506 470c0504 Stefan Hajnoczi
    }
2507 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
2508 470c0504 Stefan Hajnoczi
        bs->copy_on_read_in_flight++;
2509 470c0504 Stefan Hajnoczi
    }
2510 470c0504 Stefan Hajnoczi
2511 470c0504 Stefan Hajnoczi
    if (bs->copy_on_read_in_flight) {
2512 f4658285 Stefan Hajnoczi
        wait_for_overlapping_requests(bs, sector_num, nb_sectors);
2513 f4658285 Stefan Hajnoczi
    }
2514 f4658285 Stefan Hajnoczi
2515 dbffbdcf Stefan Hajnoczi
    tracked_request_begin(&req, bs, sector_num, nb_sectors, false);
2516 ab185921 Stefan Hajnoczi
2517 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
2518 ab185921 Stefan Hajnoczi
        int pnum;
2519 ab185921 Stefan Hajnoczi
2520 ab185921 Stefan Hajnoczi
        ret = bdrv_co_is_allocated(bs, sector_num, nb_sectors, &pnum);
2521 ab185921 Stefan Hajnoczi
        if (ret < 0) {
2522 ab185921 Stefan Hajnoczi
            goto out;
2523 ab185921 Stefan Hajnoczi
        }
2524 ab185921 Stefan Hajnoczi
2525 ab185921 Stefan Hajnoczi
        if (!ret || pnum != nb_sectors) {
2526 470c0504 Stefan Hajnoczi
            ret = bdrv_co_do_copy_on_readv(bs, sector_num, nb_sectors, qiov);
2527 ab185921 Stefan Hajnoczi
            goto out;
2528 ab185921 Stefan Hajnoczi
        }
2529 ab185921 Stefan Hajnoczi
    }
2530 ab185921 Stefan Hajnoczi
2531 dbffbdcf Stefan Hajnoczi
    ret = drv->bdrv_co_readv(bs, sector_num, nb_sectors, qiov);
2532 ab185921 Stefan Hajnoczi
2533 ab185921 Stefan Hajnoczi
out:
2534 dbffbdcf Stefan Hajnoczi
    tracked_request_end(&req);
2535 470c0504 Stefan Hajnoczi
2536 470c0504 Stefan Hajnoczi
    if (flags & BDRV_REQ_COPY_ON_READ) {
2537 470c0504 Stefan Hajnoczi
        bs->copy_on_read_in_flight--;
2538 470c0504 Stefan Hajnoczi
    }
2539 470c0504 Stefan Hajnoczi
2540 dbffbdcf Stefan Hajnoczi
    return ret;
2541 da1fa91d Kevin Wolf
}
2542 da1fa91d Kevin Wolf
2543 c5fbe571 Stefan Hajnoczi
int coroutine_fn bdrv_co_readv(BlockDriverState *bs, int64_t sector_num,
2544 da1fa91d Kevin Wolf
    int nb_sectors, QEMUIOVector *qiov)
2545 da1fa91d Kevin Wolf
{
2546 c5fbe571 Stefan Hajnoczi
    trace_bdrv_co_readv(bs, sector_num, nb_sectors);
2547 da1fa91d Kevin Wolf
2548 470c0504 Stefan Hajnoczi
    return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov, 0);
2549 470c0504 Stefan Hajnoczi
}
2550 470c0504 Stefan Hajnoczi
2551 470c0504 Stefan Hajnoczi
int coroutine_fn bdrv_co_copy_on_readv(BlockDriverState *bs,
2552 470c0504 Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov)
2553 470c0504 Stefan Hajnoczi
{
2554 470c0504 Stefan Hajnoczi
    trace_bdrv_co_copy_on_readv(bs, sector_num, nb_sectors);
2555 470c0504 Stefan Hajnoczi
2556 470c0504 Stefan Hajnoczi
    return bdrv_co_do_readv(bs, sector_num, nb_sectors, qiov,
2557 470c0504 Stefan Hajnoczi
                            BDRV_REQ_COPY_ON_READ);
2558 c5fbe571 Stefan Hajnoczi
}
2559 c5fbe571 Stefan Hajnoczi
2560 f08f2dda Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_write_zeroes(BlockDriverState *bs,
2561 f08f2dda Stefan Hajnoczi
    int64_t sector_num, int nb_sectors)
2562 f08f2dda Stefan Hajnoczi
{
2563 f08f2dda Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
2564 f08f2dda Stefan Hajnoczi
    QEMUIOVector qiov;
2565 f08f2dda Stefan Hajnoczi
    struct iovec iov;
2566 f08f2dda Stefan Hajnoczi
    int ret;
2567 f08f2dda Stefan Hajnoczi
2568 621f0589 Kevin Wolf
    /* TODO Emulate only part of misaligned requests instead of letting block
2569 621f0589 Kevin Wolf
     * drivers return -ENOTSUP and emulate everything */
2570 621f0589 Kevin Wolf
2571 f08f2dda Stefan Hajnoczi
    /* First try the efficient write zeroes operation */
2572 f08f2dda Stefan Hajnoczi
    if (drv->bdrv_co_write_zeroes) {
2573 621f0589 Kevin Wolf
        ret = drv->bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
2574 621f0589 Kevin Wolf
        if (ret != -ENOTSUP) {
2575 621f0589 Kevin Wolf
            return ret;
2576 621f0589 Kevin Wolf
        }
2577 f08f2dda Stefan Hajnoczi
    }
2578 f08f2dda Stefan Hajnoczi
2579 f08f2dda Stefan Hajnoczi
    /* Fall back to bounce buffer if write zeroes is unsupported */
2580 f08f2dda Stefan Hajnoczi
    iov.iov_len  = nb_sectors * BDRV_SECTOR_SIZE;
2581 f08f2dda Stefan Hajnoczi
    iov.iov_base = qemu_blockalign(bs, iov.iov_len);
2582 f08f2dda Stefan Hajnoczi
    memset(iov.iov_base, 0, iov.iov_len);
2583 f08f2dda Stefan Hajnoczi
    qemu_iovec_init_external(&qiov, &iov, 1);
2584 f08f2dda Stefan Hajnoczi
2585 f08f2dda Stefan Hajnoczi
    ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, &qiov);
2586 f08f2dda Stefan Hajnoczi
2587 f08f2dda Stefan Hajnoczi
    qemu_vfree(iov.iov_base);
2588 f08f2dda Stefan Hajnoczi
    return ret;
2589 f08f2dda Stefan Hajnoczi
}
2590 f08f2dda Stefan Hajnoczi
2591 c5fbe571 Stefan Hajnoczi
/*
2592 c5fbe571 Stefan Hajnoczi
 * Handle a write request in coroutine context
2593 c5fbe571 Stefan Hajnoczi
 */
2594 c5fbe571 Stefan Hajnoczi
static int coroutine_fn bdrv_co_do_writev(BlockDriverState *bs,
2595 f08f2dda Stefan Hajnoczi
    int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
2596 f08f2dda Stefan Hajnoczi
    BdrvRequestFlags flags)
2597 c5fbe571 Stefan Hajnoczi
{
2598 c5fbe571 Stefan Hajnoczi
    BlockDriver *drv = bs->drv;
2599 dbffbdcf Stefan Hajnoczi
    BdrvTrackedRequest req;
2600 6b7cb247 Stefan Hajnoczi
    int ret;
2601 da1fa91d Kevin Wolf
2602 da1fa91d Kevin Wolf
    if (!bs->drv) {
2603 da1fa91d Kevin Wolf
        return -ENOMEDIUM;
2604 da1fa91d Kevin Wolf
    }
2605 da1fa91d Kevin Wolf
    if (bs->read_only) {
2606 da1fa91d Kevin Wolf
        return -EACCES;
2607 da1fa91d Kevin Wolf
    }
2608 da1fa91d Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors)) {
2609 da1fa91d Kevin Wolf
        return -EIO;
2610 da1fa91d Kevin Wolf
    }
2611 da1fa91d Kevin Wolf
2612 98f90dba Zhi Yong Wu
    /* throttling disk write I/O */
2613 98f90dba Zhi Yong Wu
    if (bs->io_limits_enabled) {
2614 98f90dba Zhi Yong Wu
        bdrv_io_limits_intercept(bs, true, nb_sectors);
2615 98f90dba Zhi Yong Wu
    }
2616 98f90dba Zhi Yong Wu
2617 470c0504 Stefan Hajnoczi
    if (bs->copy_on_read_in_flight) {
2618 f4658285 Stefan Hajnoczi
        wait_for_overlapping_requests(bs, sector_num, nb_sectors);
2619 f4658285 Stefan Hajnoczi
    }
2620 f4658285 Stefan Hajnoczi
2621 dbffbdcf Stefan Hajnoczi
    tracked_request_begin(&req, bs, sector_num, nb_sectors, true);
2622 dbffbdcf Stefan Hajnoczi
2623 f08f2dda Stefan Hajnoczi
    if (flags & BDRV_REQ_ZERO_WRITE) {
2624 f08f2dda Stefan Hajnoczi
        ret = bdrv_co_do_write_zeroes(bs, sector_num, nb_sectors);
2625 f08f2dda Stefan Hajnoczi
    } else {
2626 f08f2dda Stefan Hajnoczi
        ret = drv->bdrv_co_writev(bs, sector_num, nb_sectors, qiov);
2627 f08f2dda Stefan Hajnoczi
    }
2628 6b7cb247 Stefan Hajnoczi
2629 f05fa4ad Paolo Bonzini
    if (ret == 0 && !bs->enable_write_cache) {
2630 f05fa4ad Paolo Bonzini
        ret = bdrv_co_flush(bs);
2631 f05fa4ad Paolo Bonzini
    }
2632 f05fa4ad Paolo Bonzini
2633 da1fa91d Kevin Wolf
    if (bs->dirty_bitmap) {
2634 1755da16 Paolo Bonzini
        bdrv_set_dirty(bs, sector_num, nb_sectors);
2635 da1fa91d Kevin Wolf
    }
2636 da1fa91d Kevin Wolf
2637 da1fa91d Kevin Wolf
    if (bs->wr_highest_sector < sector_num + nb_sectors - 1) {
2638 da1fa91d Kevin Wolf
        bs->wr_highest_sector = sector_num + nb_sectors - 1;
2639 da1fa91d Kevin Wolf
    }
2640 da1fa91d Kevin Wolf
2641 dbffbdcf Stefan Hajnoczi
    tracked_request_end(&req);
2642 dbffbdcf Stefan Hajnoczi
2643 6b7cb247 Stefan Hajnoczi
    return ret;
2644 da1fa91d Kevin Wolf
}
2645 da1fa91d Kevin Wolf
2646 c5fbe571 Stefan Hajnoczi
int coroutine_fn bdrv_co_writev(BlockDriverState *bs, int64_t sector_num,
2647 c5fbe571 Stefan Hajnoczi
    int nb_sectors, QEMUIOVector *qiov)
2648 c5fbe571 Stefan Hajnoczi
{
2649 c5fbe571 Stefan Hajnoczi
    trace_bdrv_co_writev(bs, sector_num, nb_sectors);
2650 c5fbe571 Stefan Hajnoczi
2651 f08f2dda Stefan Hajnoczi
    return bdrv_co_do_writev(bs, sector_num, nb_sectors, qiov, 0);
2652 f08f2dda Stefan Hajnoczi
}
2653 f08f2dda Stefan Hajnoczi
2654 f08f2dda Stefan Hajnoczi
int coroutine_fn bdrv_co_write_zeroes(BlockDriverState *bs,
2655 f08f2dda Stefan Hajnoczi
                                      int64_t sector_num, int nb_sectors)
2656 f08f2dda Stefan Hajnoczi
{
2657 f08f2dda Stefan Hajnoczi
    trace_bdrv_co_write_zeroes(bs, sector_num, nb_sectors);
2658 f08f2dda Stefan Hajnoczi
2659 f08f2dda Stefan Hajnoczi
    return bdrv_co_do_writev(bs, sector_num, nb_sectors, NULL,
2660 f08f2dda Stefan Hajnoczi
                             BDRV_REQ_ZERO_WRITE);
2661 c5fbe571 Stefan Hajnoczi
}
2662 c5fbe571 Stefan Hajnoczi
2663 83f64091 bellard
/**
2664 83f64091 bellard
 * Truncate file to 'offset' bytes (needed only for file protocols)
2665 83f64091 bellard
 */
2666 83f64091 bellard
int bdrv_truncate(BlockDriverState *bs, int64_t offset)
2667 83f64091 bellard
{
2668 83f64091 bellard
    BlockDriver *drv = bs->drv;
2669 51762288 Stefan Hajnoczi
    int ret;
2670 83f64091 bellard
    if (!drv)
2671 19cb3738 bellard
        return -ENOMEDIUM;
2672 83f64091 bellard
    if (!drv->bdrv_truncate)
2673 83f64091 bellard
        return -ENOTSUP;
2674 59f2689d Naphtali Sprei
    if (bs->read_only)
2675 59f2689d Naphtali Sprei
        return -EACCES;
2676 8591675f Marcelo Tosatti
    if (bdrv_in_use(bs))
2677 8591675f Marcelo Tosatti
        return -EBUSY;
2678 51762288 Stefan Hajnoczi
    ret = drv->bdrv_truncate(bs, offset);
2679 51762288 Stefan Hajnoczi
    if (ret == 0) {
2680 51762288 Stefan Hajnoczi
        ret = refresh_total_sectors(bs, offset >> BDRV_SECTOR_BITS);
2681 145feb17 Markus Armbruster
        bdrv_dev_resize_cb(bs);
2682 51762288 Stefan Hajnoczi
    }
2683 51762288 Stefan Hajnoczi
    return ret;
2684 83f64091 bellard
}
2685 83f64091 bellard
2686 83f64091 bellard
/**
2687 4a1d5e1f Fam Zheng
 * Length of a allocated file in bytes. Sparse files are counted by actual
2688 4a1d5e1f Fam Zheng
 * allocated space. Return < 0 if error or unknown.
2689 4a1d5e1f Fam Zheng
 */
2690 4a1d5e1f Fam Zheng
int64_t bdrv_get_allocated_file_size(BlockDriverState *bs)
2691 4a1d5e1f Fam Zheng
{
2692 4a1d5e1f Fam Zheng
    BlockDriver *drv = bs->drv;
2693 4a1d5e1f Fam Zheng
    if (!drv) {
2694 4a1d5e1f Fam Zheng
        return -ENOMEDIUM;
2695 4a1d5e1f Fam Zheng
    }
2696 4a1d5e1f Fam Zheng
    if (drv->bdrv_get_allocated_file_size) {
2697 4a1d5e1f Fam Zheng
        return drv->bdrv_get_allocated_file_size(bs);
2698 4a1d5e1f Fam Zheng
    }
2699 4a1d5e1f Fam Zheng
    if (bs->file) {
2700 4a1d5e1f Fam Zheng
        return bdrv_get_allocated_file_size(bs->file);
2701 4a1d5e1f Fam Zheng
    }
2702 4a1d5e1f Fam Zheng
    return -ENOTSUP;
2703 4a1d5e1f Fam Zheng
}
2704 4a1d5e1f Fam Zheng
2705 4a1d5e1f Fam Zheng
/**
2706 83f64091 bellard
 * Length of a file in bytes. Return < 0 if error or unknown.
2707 83f64091 bellard
 */
2708 83f64091 bellard
int64_t bdrv_getlength(BlockDriverState *bs)
2709 83f64091 bellard
{
2710 83f64091 bellard
    BlockDriver *drv = bs->drv;
2711 83f64091 bellard
    if (!drv)
2712 19cb3738 bellard
        return -ENOMEDIUM;
2713 51762288 Stefan Hajnoczi
2714 2c6942fa Markus Armbruster
    if (bs->growable || bdrv_dev_has_removable_media(bs)) {
2715 46a4e4e6 Stefan Hajnoczi
        if (drv->bdrv_getlength) {
2716 46a4e4e6 Stefan Hajnoczi
            return drv->bdrv_getlength(bs);
2717 46a4e4e6 Stefan Hajnoczi
        }
2718 83f64091 bellard
    }
2719 46a4e4e6 Stefan Hajnoczi
    return bs->total_sectors * BDRV_SECTOR_SIZE;
2720 fc01f7e7 bellard
}
2721 fc01f7e7 bellard
2722 19cb3738 bellard
/* return 0 as number of sectors if no device present or error */
2723 96b8f136 ths
void bdrv_get_geometry(BlockDriverState *bs, uint64_t *nb_sectors_ptr)
2724 fc01f7e7 bellard
{
2725 19cb3738 bellard
    int64_t length;
2726 19cb3738 bellard
    length = bdrv_getlength(bs);
2727 19cb3738 bellard
    if (length < 0)
2728 19cb3738 bellard
        length = 0;
2729 19cb3738 bellard
    else
2730 6ea44308 Jan Kiszka
        length = length >> BDRV_SECTOR_BITS;
2731 19cb3738 bellard
    *nb_sectors_ptr = length;
2732 fc01f7e7 bellard
}
2733 cf98951b bellard
2734 0563e191 Zhi Yong Wu
/* throttling disk io limits */
2735 0563e191 Zhi Yong Wu
void bdrv_set_io_limits(BlockDriverState *bs,
2736 0563e191 Zhi Yong Wu
                        BlockIOLimit *io_limits)
2737 0563e191 Zhi Yong Wu
{
2738 0563e191 Zhi Yong Wu
    bs->io_limits = *io_limits;
2739 0563e191 Zhi Yong Wu
    bs->io_limits_enabled = bdrv_io_limits_enabled(bs);
2740 0563e191 Zhi Yong Wu
}
2741 0563e191 Zhi Yong Wu
2742 ff06f5f3 Paolo Bonzini
void bdrv_set_on_error(BlockDriverState *bs, BlockdevOnError on_read_error,
2743 ff06f5f3 Paolo Bonzini
                       BlockdevOnError on_write_error)
2744 abd7f68d Markus Armbruster
{
2745 abd7f68d Markus Armbruster
    bs->on_read_error = on_read_error;
2746 abd7f68d Markus Armbruster
    bs->on_write_error = on_write_error;
2747 abd7f68d Markus Armbruster
}
2748 abd7f68d Markus Armbruster
2749 1ceee0d5 Paolo Bonzini
BlockdevOnError bdrv_get_on_error(BlockDriverState *bs, bool is_read)
2750 abd7f68d Markus Armbruster
{
2751 abd7f68d Markus Armbruster
    return is_read ? bs->on_read_error : bs->on_write_error;
2752 abd7f68d Markus Armbruster
}
2753 abd7f68d Markus Armbruster
2754 3e1caa5f Paolo Bonzini
BlockErrorAction bdrv_get_error_action(BlockDriverState *bs, bool is_read, int error)
2755 3e1caa5f Paolo Bonzini
{
2756 3e1caa5f Paolo Bonzini
    BlockdevOnError on_err = is_read ? bs->on_read_error : bs->on_write_error;
2757 3e1caa5f Paolo Bonzini
2758 3e1caa5f Paolo Bonzini
    switch (on_err) {
2759 3e1caa5f Paolo Bonzini
    case BLOCKDEV_ON_ERROR_ENOSPC:
2760 3e1caa5f Paolo Bonzini
        return (error == ENOSPC) ? BDRV_ACTION_STOP : BDRV_ACTION_REPORT;
2761 3e1caa5f Paolo Bonzini
    case BLOCKDEV_ON_ERROR_STOP:
2762 3e1caa5f Paolo Bonzini
        return BDRV_ACTION_STOP;
2763 3e1caa5f Paolo Bonzini
    case BLOCKDEV_ON_ERROR_REPORT:
2764 3e1caa5f Paolo Bonzini
        return BDRV_ACTION_REPORT;
2765 3e1caa5f Paolo Bonzini
    case BLOCKDEV_ON_ERROR_IGNORE:
2766 3e1caa5f Paolo Bonzini
        return BDRV_ACTION_IGNORE;
2767 3e1caa5f Paolo Bonzini
    default:
2768 3e1caa5f Paolo Bonzini
        abort();
2769 3e1caa5f Paolo Bonzini
    }
2770 3e1caa5f Paolo Bonzini
}
2771 3e1caa5f Paolo Bonzini
2772 3e1caa5f Paolo Bonzini
/* This is done by device models because, while the block layer knows
2773 3e1caa5f Paolo Bonzini
 * about the error, it does not know whether an operation comes from
2774 3e1caa5f Paolo Bonzini
 * the device or the block layer (from a job, for example).
2775 3e1caa5f Paolo Bonzini
 */
2776 3e1caa5f Paolo Bonzini
void bdrv_error_action(BlockDriverState *bs, BlockErrorAction action,
2777 3e1caa5f Paolo Bonzini
                       bool is_read, int error)
2778 3e1caa5f Paolo Bonzini
{
2779 3e1caa5f Paolo Bonzini
    assert(error >= 0);
2780 32c81a4a Paolo Bonzini
    bdrv_emit_qmp_error_event(bs, QEVENT_BLOCK_IO_ERROR, action, is_read);
2781 3e1caa5f Paolo Bonzini
    if (action == BDRV_ACTION_STOP) {
2782 3e1caa5f Paolo Bonzini
        vm_stop(RUN_STATE_IO_ERROR);
2783 3e1caa5f Paolo Bonzini
        bdrv_iostatus_set_err(bs, error);
2784 3e1caa5f Paolo Bonzini
    }
2785 3e1caa5f Paolo Bonzini
}
2786 3e1caa5f Paolo Bonzini
2787 b338082b bellard
int bdrv_is_read_only(BlockDriverState *bs)
2788 b338082b bellard
{
2789 b338082b bellard
    return bs->read_only;
2790 b338082b bellard
}
2791 b338082b bellard
2792 985a03b0 ths
int bdrv_is_sg(BlockDriverState *bs)
2793 985a03b0 ths
{
2794 985a03b0 ths
    return bs->sg;
2795 985a03b0 ths
}
2796 985a03b0 ths
2797 e900a7b7 Christoph Hellwig
int bdrv_enable_write_cache(BlockDriverState *bs)
2798 e900a7b7 Christoph Hellwig
{
2799 e900a7b7 Christoph Hellwig
    return bs->enable_write_cache;
2800 e900a7b7 Christoph Hellwig
}
2801 e900a7b7 Christoph Hellwig
2802 425b0148 Paolo Bonzini
void bdrv_set_enable_write_cache(BlockDriverState *bs, bool wce)
2803 425b0148 Paolo Bonzini
{
2804 425b0148 Paolo Bonzini
    bs->enable_write_cache = wce;
2805 55b110f2 Jeff Cody
2806 55b110f2 Jeff Cody
    /* so a reopen() will preserve wce */
2807 55b110f2 Jeff Cody
    if (wce) {
2808 55b110f2 Jeff Cody
        bs->open_flags |= BDRV_O_CACHE_WB;
2809 55b110f2 Jeff Cody
    } else {
2810 55b110f2 Jeff Cody
        bs->open_flags &= ~BDRV_O_CACHE_WB;
2811 55b110f2 Jeff Cody
    }
2812 425b0148 Paolo Bonzini
}
2813 425b0148 Paolo Bonzini
2814 ea2384d3 bellard
int bdrv_is_encrypted(BlockDriverState *bs)
2815 ea2384d3 bellard
{
2816 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted)
2817 ea2384d3 bellard
        return 1;
2818 ea2384d3 bellard
    return bs->encrypted;
2819 ea2384d3 bellard
}
2820 ea2384d3 bellard
2821 c0f4ce77 aliguori
int bdrv_key_required(BlockDriverState *bs)
2822 c0f4ce77 aliguori
{
2823 c0f4ce77 aliguori
    BlockDriverState *backing_hd = bs->backing_hd;
2824 c0f4ce77 aliguori
2825 c0f4ce77 aliguori
    if (backing_hd && backing_hd->encrypted && !backing_hd->valid_key)
2826 c0f4ce77 aliguori
        return 1;
2827 c0f4ce77 aliguori
    return (bs->encrypted && !bs->valid_key);
2828 c0f4ce77 aliguori
}
2829 c0f4ce77 aliguori
2830 ea2384d3 bellard
int bdrv_set_key(BlockDriverState *bs, const char *key)
2831 ea2384d3 bellard
{
2832 ea2384d3 bellard
    int ret;
2833 ea2384d3 bellard
    if (bs->backing_hd && bs->backing_hd->encrypted) {
2834 ea2384d3 bellard
        ret = bdrv_set_key(bs->backing_hd, key);
2835 ea2384d3 bellard
        if (ret < 0)
2836 ea2384d3 bellard
            return ret;
2837 ea2384d3 bellard
        if (!bs->encrypted)
2838 ea2384d3 bellard
            return 0;
2839 ea2384d3 bellard
    }
2840 fd04a2ae Shahar Havivi
    if (!bs->encrypted) {
2841 fd04a2ae Shahar Havivi
        return -EINVAL;
2842 fd04a2ae Shahar Havivi
    } else if (!bs->drv || !bs->drv->bdrv_set_key) {
2843 fd04a2ae Shahar Havivi
        return -ENOMEDIUM;
2844 fd04a2ae Shahar Havivi
    }
2845 c0f4ce77 aliguori
    ret = bs->drv->bdrv_set_key(bs, key);
2846 bb5fc20f aliguori
    if (ret < 0) {
2847 bb5fc20f aliguori
        bs->valid_key = 0;
2848 bb5fc20f aliguori
    } else if (!bs->valid_key) {
2849 bb5fc20f aliguori
        bs->valid_key = 1;
2850 bb5fc20f aliguori
        /* call the change callback now, we skipped it on open */
2851 7d4b4ba5 Markus Armbruster
        bdrv_dev_change_media_cb(bs, true);
2852 bb5fc20f aliguori
    }
2853 c0f4ce77 aliguori
    return ret;
2854 ea2384d3 bellard
}
2855 ea2384d3 bellard
2856 f8d6bba1 Markus Armbruster
const char *bdrv_get_format_name(BlockDriverState *bs)
2857 ea2384d3 bellard
{
2858 f8d6bba1 Markus Armbruster
    return bs->drv ? bs->drv->format_name : NULL;
2859 ea2384d3 bellard
}
2860 ea2384d3 bellard
2861 5fafdf24 ths
void bdrv_iterate_format(void (*it)(void *opaque, const char *name),
2862 ea2384d3 bellard
                         void *opaque)
2863 ea2384d3 bellard
{
2864 ea2384d3 bellard
    BlockDriver *drv;
2865 ea2384d3 bellard
2866 8a22f02a Stefan Hajnoczi
    QLIST_FOREACH(drv, &bdrv_drivers, list) {
2867 ea2384d3 bellard
        it(opaque, drv->format_name);
2868 ea2384d3 bellard
    }
2869 ea2384d3 bellard
}
2870 ea2384d3 bellard
2871 b338082b bellard
BlockDriverState *bdrv_find(const char *name)
2872 b338082b bellard
{
2873 b338082b bellard
    BlockDriverState *bs;
2874 b338082b bellard
2875 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2876 1b7bdbc1 Stefan Hajnoczi
        if (!strcmp(name, bs->device_name)) {
2877 b338082b bellard
            return bs;
2878 1b7bdbc1 Stefan Hajnoczi
        }
2879 b338082b bellard
    }
2880 b338082b bellard
    return NULL;
2881 b338082b bellard
}
2882 b338082b bellard
2883 2f399b0a Markus Armbruster
BlockDriverState *bdrv_next(BlockDriverState *bs)
2884 2f399b0a Markus Armbruster
{
2885 2f399b0a Markus Armbruster
    if (!bs) {
2886 2f399b0a Markus Armbruster
        return QTAILQ_FIRST(&bdrv_states);
2887 2f399b0a Markus Armbruster
    }
2888 2f399b0a Markus Armbruster
    return QTAILQ_NEXT(bs, list);
2889 2f399b0a Markus Armbruster
}
2890 2f399b0a Markus Armbruster
2891 51de9760 aliguori
void bdrv_iterate(void (*it)(void *opaque, BlockDriverState *bs), void *opaque)
2892 81d0912d bellard
{
2893 81d0912d bellard
    BlockDriverState *bs;
2894 81d0912d bellard
2895 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2896 51de9760 aliguori
        it(opaque, bs);
2897 81d0912d bellard
    }
2898 81d0912d bellard
}
2899 81d0912d bellard
2900 ea2384d3 bellard
const char *bdrv_get_device_name(BlockDriverState *bs)
2901 ea2384d3 bellard
{
2902 ea2384d3 bellard
    return bs->device_name;
2903 ea2384d3 bellard
}
2904 ea2384d3 bellard
2905 c8433287 Markus Armbruster
int bdrv_get_flags(BlockDriverState *bs)
2906 c8433287 Markus Armbruster
{
2907 c8433287 Markus Armbruster
    return bs->open_flags;
2908 c8433287 Markus Armbruster
}
2909 c8433287 Markus Armbruster
2910 c6ca28d6 aliguori
void bdrv_flush_all(void)
2911 c6ca28d6 aliguori
{
2912 c6ca28d6 aliguori
    BlockDriverState *bs;
2913 c6ca28d6 aliguori
2914 1b7bdbc1 Stefan Hajnoczi
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
2915 29cdb251 Paolo Bonzini
        bdrv_flush(bs);
2916 1b7bdbc1 Stefan Hajnoczi
    }
2917 c6ca28d6 aliguori
}
2918 c6ca28d6 aliguori
2919 f2feebbd Kevin Wolf
int bdrv_has_zero_init(BlockDriverState *bs)
2920 f2feebbd Kevin Wolf
{
2921 f2feebbd Kevin Wolf
    assert(bs->drv);
2922 f2feebbd Kevin Wolf
2923 336c1c12 Kevin Wolf
    if (bs->drv->bdrv_has_zero_init) {
2924 336c1c12 Kevin Wolf
        return bs->drv->bdrv_has_zero_init(bs);
2925 f2feebbd Kevin Wolf
    }
2926 f2feebbd Kevin Wolf
2927 f2feebbd Kevin Wolf
    return 1;
2928 f2feebbd Kevin Wolf
}
2929 f2feebbd Kevin Wolf
2930 376ae3f1 Stefan Hajnoczi
typedef struct BdrvCoIsAllocatedData {
2931 376ae3f1 Stefan Hajnoczi
    BlockDriverState *bs;
2932 b35b2bba Miroslav Rezanina
    BlockDriverState *base;
2933 376ae3f1 Stefan Hajnoczi
    int64_t sector_num;
2934 376ae3f1 Stefan Hajnoczi
    int nb_sectors;
2935 376ae3f1 Stefan Hajnoczi
    int *pnum;
2936 376ae3f1 Stefan Hajnoczi
    int ret;
2937 376ae3f1 Stefan Hajnoczi
    bool done;
2938 376ae3f1 Stefan Hajnoczi
} BdrvCoIsAllocatedData;
2939 376ae3f1 Stefan Hajnoczi
2940 f58c7b35 ths
/*
2941 f58c7b35 ths
 * Returns true iff the specified sector is present in the disk image. Drivers
2942 f58c7b35 ths
 * not implementing the functionality are assumed to not support backing files,
2943 f58c7b35 ths
 * hence all their sectors are reported as allocated.
2944 f58c7b35 ths
 *
2945 bd9533e3 Stefan Hajnoczi
 * If 'sector_num' is beyond the end of the disk image the return value is 0
2946 bd9533e3 Stefan Hajnoczi
 * and 'pnum' is set to 0.
2947 bd9533e3 Stefan Hajnoczi
 *
2948 f58c7b35 ths
 * 'pnum' is set to the number of sectors (including and immediately following
2949 f58c7b35 ths
 * the specified sector) that are known to be in the same
2950 f58c7b35 ths
 * allocated/unallocated state.
2951 f58c7b35 ths
 *
2952 bd9533e3 Stefan Hajnoczi
 * 'nb_sectors' is the max value 'pnum' should be set to.  If nb_sectors goes
2953 bd9533e3 Stefan Hajnoczi
 * beyond the end of the disk image it will be clamped.
2954 f58c7b35 ths
 */
2955 060f51c9 Stefan Hajnoczi
int coroutine_fn bdrv_co_is_allocated(BlockDriverState *bs, int64_t sector_num,
2956 060f51c9 Stefan Hajnoczi
                                      int nb_sectors, int *pnum)
2957 f58c7b35 ths
{
2958 bd9533e3 Stefan Hajnoczi
    int64_t n;
2959 bd9533e3 Stefan Hajnoczi
2960 bd9533e3 Stefan Hajnoczi
    if (sector_num >= bs->total_sectors) {
2961 bd9533e3 Stefan Hajnoczi
        *pnum = 0;
2962 bd9533e3 Stefan Hajnoczi
        return 0;
2963 bd9533e3 Stefan Hajnoczi
    }
2964 bd9533e3 Stefan Hajnoczi
2965 bd9533e3 Stefan Hajnoczi
    n = bs->total_sectors - sector_num;
2966 bd9533e3 Stefan Hajnoczi
    if (n < nb_sectors) {
2967 bd9533e3 Stefan Hajnoczi
        nb_sectors = n;
2968 bd9533e3 Stefan Hajnoczi
    }
2969 bd9533e3 Stefan Hajnoczi
2970 6aebab14 Stefan Hajnoczi
    if (!bs->drv->bdrv_co_is_allocated) {
2971 bd9533e3 Stefan Hajnoczi
        *pnum = nb_sectors;
2972 f58c7b35 ths
        return 1;
2973 f58c7b35 ths
    }
2974 6aebab14 Stefan Hajnoczi
2975 060f51c9 Stefan Hajnoczi
    return bs->drv->bdrv_co_is_allocated(bs, sector_num, nb_sectors, pnum);
2976 060f51c9 Stefan Hajnoczi
}
2977 060f51c9 Stefan Hajnoczi
2978 060f51c9 Stefan Hajnoczi
/* Coroutine wrapper for bdrv_is_allocated() */
2979 060f51c9 Stefan Hajnoczi
static void coroutine_fn bdrv_is_allocated_co_entry(void *opaque)
2980 060f51c9 Stefan Hajnoczi
{
2981 060f51c9 Stefan Hajnoczi
    BdrvCoIsAllocatedData *data = opaque;
2982 060f51c9 Stefan Hajnoczi
    BlockDriverState *bs = data->bs;
2983 060f51c9 Stefan Hajnoczi
2984 060f51c9 Stefan Hajnoczi
    data->ret = bdrv_co_is_allocated(bs, data->sector_num, data->nb_sectors,
2985 060f51c9 Stefan Hajnoczi
                                     data->pnum);
2986 060f51c9 Stefan Hajnoczi
    data->done = true;
2987 060f51c9 Stefan Hajnoczi
}
2988 060f51c9 Stefan Hajnoczi
2989 060f51c9 Stefan Hajnoczi
/*
2990 060f51c9 Stefan Hajnoczi
 * Synchronous wrapper around bdrv_co_is_allocated().
2991 060f51c9 Stefan Hajnoczi
 *
2992 060f51c9 Stefan Hajnoczi
 * See bdrv_co_is_allocated() for details.
2993 060f51c9 Stefan Hajnoczi
 */
2994 060f51c9 Stefan Hajnoczi
int bdrv_is_allocated(BlockDriverState *bs, int64_t sector_num, int nb_sectors,
2995 060f51c9 Stefan Hajnoczi
                      int *pnum)
2996 060f51c9 Stefan Hajnoczi
{
2997 6aebab14 Stefan Hajnoczi
    Coroutine *co;
2998 6aebab14 Stefan Hajnoczi
    BdrvCoIsAllocatedData data = {
2999 6aebab14 Stefan Hajnoczi
        .bs = bs,
3000 6aebab14 Stefan Hajnoczi
        .sector_num = sector_num,
3001 6aebab14 Stefan Hajnoczi
        .nb_sectors = nb_sectors,
3002 6aebab14 Stefan Hajnoczi
        .pnum = pnum,
3003 6aebab14 Stefan Hajnoczi
        .done = false,
3004 6aebab14 Stefan Hajnoczi
    };
3005 6aebab14 Stefan Hajnoczi
3006 6aebab14 Stefan Hajnoczi
    co = qemu_coroutine_create(bdrv_is_allocated_co_entry);
3007 6aebab14 Stefan Hajnoczi
    qemu_coroutine_enter(co, &data);
3008 6aebab14 Stefan Hajnoczi
    while (!data.done) {
3009 6aebab14 Stefan Hajnoczi
        qemu_aio_wait();
3010 6aebab14 Stefan Hajnoczi
    }
3011 6aebab14 Stefan Hajnoczi
    return data.ret;
3012 f58c7b35 ths
}
3013 f58c7b35 ths
3014 188a7bbf Paolo Bonzini
/*
3015 188a7bbf Paolo Bonzini
 * Given an image chain: ... -> [BASE] -> [INTER1] -> [INTER2] -> [TOP]
3016 188a7bbf Paolo Bonzini
 *
3017 188a7bbf Paolo Bonzini
 * Return true if the given sector is allocated in any image between
3018 188a7bbf Paolo Bonzini
 * BASE and TOP (inclusive).  BASE can be NULL to check if the given
3019 188a7bbf Paolo Bonzini
 * sector is allocated in any image of the chain.  Return false otherwise.
3020 188a7bbf Paolo Bonzini
 *
3021 188a7bbf Paolo Bonzini
 * 'pnum' is set to the number of sectors (including and immediately following
3022 188a7bbf Paolo Bonzini
 *  the specified sector) that are known to be in the same
3023 188a7bbf Paolo Bonzini
 *  allocated/unallocated state.
3024 188a7bbf Paolo Bonzini
 *
3025 188a7bbf Paolo Bonzini
 */
3026 188a7bbf Paolo Bonzini
int coroutine_fn bdrv_co_is_allocated_above(BlockDriverState *top,
3027 188a7bbf Paolo Bonzini
                                            BlockDriverState *base,
3028 188a7bbf Paolo Bonzini
                                            int64_t sector_num,
3029 188a7bbf Paolo Bonzini
                                            int nb_sectors, int *pnum)
3030 188a7bbf Paolo Bonzini
{
3031 188a7bbf Paolo Bonzini
    BlockDriverState *intermediate;
3032 188a7bbf Paolo Bonzini
    int ret, n = nb_sectors;
3033 188a7bbf Paolo Bonzini
3034 188a7bbf Paolo Bonzini
    intermediate = top;
3035 188a7bbf Paolo Bonzini
    while (intermediate && intermediate != base) {
3036 188a7bbf Paolo Bonzini
        int pnum_inter;
3037 188a7bbf Paolo Bonzini
        ret = bdrv_co_is_allocated(intermediate, sector_num, nb_sectors,
3038 188a7bbf Paolo Bonzini
                                   &pnum_inter);
3039 188a7bbf Paolo Bonzini
        if (ret < 0) {
3040 188a7bbf Paolo Bonzini
            return ret;
3041 188a7bbf Paolo Bonzini
        } else if (ret) {
3042 188a7bbf Paolo Bonzini
            *pnum = pnum_inter;
3043 188a7bbf Paolo Bonzini
            return 1;
3044 188a7bbf Paolo Bonzini
        }
3045 188a7bbf Paolo Bonzini
3046 188a7bbf Paolo Bonzini
        /*
3047 188a7bbf Paolo Bonzini
         * [sector_num, nb_sectors] is unallocated on top but intermediate
3048 188a7bbf Paolo Bonzini
         * might have
3049 188a7bbf Paolo Bonzini
         *
3050 188a7bbf Paolo Bonzini
         * [sector_num+x, nr_sectors] allocated.
3051 188a7bbf Paolo Bonzini
         */
3052 63ba17d3 Vishvananda Ishaya
        if (n > pnum_inter &&
3053 63ba17d3 Vishvananda Ishaya
            (intermediate == top ||
3054 63ba17d3 Vishvananda Ishaya
             sector_num + pnum_inter < intermediate->total_sectors)) {
3055 188a7bbf Paolo Bonzini
            n = pnum_inter;
3056 188a7bbf Paolo Bonzini
        }
3057 188a7bbf Paolo Bonzini
3058 188a7bbf Paolo Bonzini
        intermediate = intermediate->backing_hd;
3059 188a7bbf Paolo Bonzini
    }
3060 188a7bbf Paolo Bonzini
3061 188a7bbf Paolo Bonzini
    *pnum = n;
3062 188a7bbf Paolo Bonzini
    return 0;
3063 188a7bbf Paolo Bonzini
}
3064 188a7bbf Paolo Bonzini
3065 b35b2bba Miroslav Rezanina
/* Coroutine wrapper for bdrv_is_allocated_above() */
3066 b35b2bba Miroslav Rezanina
static void coroutine_fn bdrv_is_allocated_above_co_entry(void *opaque)
3067 b35b2bba Miroslav Rezanina
{
3068 b35b2bba Miroslav Rezanina
    BdrvCoIsAllocatedData *data = opaque;
3069 b35b2bba Miroslav Rezanina
    BlockDriverState *top = data->bs;
3070 b35b2bba Miroslav Rezanina
    BlockDriverState *base = data->base;
3071 b35b2bba Miroslav Rezanina
3072 b35b2bba Miroslav Rezanina
    data->ret = bdrv_co_is_allocated_above(top, base, data->sector_num,
3073 b35b2bba Miroslav Rezanina
                                           data->nb_sectors, data->pnum);
3074 b35b2bba Miroslav Rezanina
    data->done = true;
3075 b35b2bba Miroslav Rezanina
}
3076 b35b2bba Miroslav Rezanina
3077 b35b2bba Miroslav Rezanina
/*
3078 b35b2bba Miroslav Rezanina
 * Synchronous wrapper around bdrv_co_is_allocated_above().
3079 b35b2bba Miroslav Rezanina
 *
3080 b35b2bba Miroslav Rezanina
 * See bdrv_co_is_allocated_above() for details.
3081 b35b2bba Miroslav Rezanina
 */
3082 b35b2bba Miroslav Rezanina
int bdrv_is_allocated_above(BlockDriverState *top, BlockDriverState *base,
3083 b35b2bba Miroslav Rezanina
                            int64_t sector_num, int nb_sectors, int *pnum)
3084 b35b2bba Miroslav Rezanina
{
3085 b35b2bba Miroslav Rezanina
    Coroutine *co;
3086 b35b2bba Miroslav Rezanina
    BdrvCoIsAllocatedData data = {
3087 b35b2bba Miroslav Rezanina
        .bs = top,
3088 b35b2bba Miroslav Rezanina
        .base = base,
3089 b35b2bba Miroslav Rezanina
        .sector_num = sector_num,
3090 b35b2bba Miroslav Rezanina
        .nb_sectors = nb_sectors,
3091 b35b2bba Miroslav Rezanina
        .pnum = pnum,
3092 b35b2bba Miroslav Rezanina
        .done = false,
3093 b35b2bba Miroslav Rezanina
    };
3094 b35b2bba Miroslav Rezanina
3095 b35b2bba Miroslav Rezanina
    co = qemu_coroutine_create(bdrv_is_allocated_above_co_entry);
3096 b35b2bba Miroslav Rezanina
    qemu_coroutine_enter(co, &data);
3097 b35b2bba Miroslav Rezanina
    while (!data.done) {
3098 b35b2bba Miroslav Rezanina
        qemu_aio_wait();
3099 b35b2bba Miroslav Rezanina
    }
3100 b35b2bba Miroslav Rezanina
    return data.ret;
3101 b35b2bba Miroslav Rezanina
}
3102 b35b2bba Miroslav Rezanina
3103 045df330 aliguori
const char *bdrv_get_encrypted_filename(BlockDriverState *bs)
3104 045df330 aliguori
{
3105 045df330 aliguori
    if (bs->backing_hd && bs->backing_hd->encrypted)
3106 045df330 aliguori
        return bs->backing_file;
3107 045df330 aliguori
    else if (bs->encrypted)
3108 045df330 aliguori
        return bs->filename;
3109 045df330 aliguori
    else
3110 045df330 aliguori
        return NULL;
3111 045df330 aliguori
}
3112 045df330 aliguori
3113 5fafdf24 ths
void bdrv_get_backing_filename(BlockDriverState *bs,
3114 83f64091 bellard
                               char *filename, int filename_size)
3115 83f64091 bellard
{
3116 3574c608 Kevin Wolf
    pstrcpy(filename, filename_size, bs->backing_file);
3117 83f64091 bellard
}
3118 83f64091 bellard
3119 5fafdf24 ths
int bdrv_write_compressed(BlockDriverState *bs, int64_t sector_num,
3120 faea38e7 bellard
                          const uint8_t *buf, int nb_sectors)
3121 faea38e7 bellard
{
3122 faea38e7 bellard
    BlockDriver *drv = bs->drv;
3123 faea38e7 bellard
    if (!drv)
3124 19cb3738 bellard
        return -ENOMEDIUM;
3125 faea38e7 bellard
    if (!drv->bdrv_write_compressed)
3126 faea38e7 bellard
        return -ENOTSUP;
3127 fbb7b4e0 Kevin Wolf
    if (bdrv_check_request(bs, sector_num, nb_sectors))
3128 fbb7b4e0 Kevin Wolf
        return -EIO;
3129 a55eb92c Jan Kiszka
3130 1755da16 Paolo Bonzini
    assert(!bs->dirty_bitmap);
3131 a55eb92c Jan Kiszka
3132 faea38e7 bellard
    return drv->bdrv_write_compressed(bs, sector_num, buf, nb_sectors);
3133 faea38e7 bellard
}
3134 3b46e624 ths
3135 faea38e7 bellard
int bdrv_get_info(BlockDriverState *bs, BlockDriverInfo *bdi)
3136 faea38e7 bellard
{
3137 faea38e7 bellard
    BlockDriver *drv = bs->drv;
3138 faea38e7 bellard
    if (!drv)
3139 19cb3738 bellard
        return -ENOMEDIUM;
3140 faea38e7 bellard
    if (!drv->bdrv_get_info)
3141 faea38e7 bellard
        return -ENOTSUP;
3142 faea38e7 bellard
    memset(bdi, 0, sizeof(*bdi));
3143 faea38e7 bellard
    return drv->bdrv_get_info(bs, bdi);
3144 faea38e7 bellard
}
3145 faea38e7 bellard
3146 45566e9c Christoph Hellwig
int bdrv_save_vmstate(BlockDriverState *bs, const uint8_t *buf,
3147 45566e9c Christoph Hellwig
                      int64_t pos, int size)
3148 178e08a5 aliguori
{
3149 cf8074b3 Kevin Wolf
    QEMUIOVector qiov;
3150 cf8074b3 Kevin Wolf
    struct iovec iov = {
3151 cf8074b3 Kevin Wolf
        .iov_base   = (void *) buf,
3152 cf8074b3 Kevin Wolf
        .iov_len    = size,
3153 cf8074b3 Kevin Wolf
    };
3154 cf8074b3 Kevin Wolf
3155 cf8074b3 Kevin Wolf
    qemu_iovec_init_external(&qiov, &iov, 1);
3156 cf8074b3 Kevin Wolf
    return bdrv_writev_vmstate(bs, &qiov, pos);
3157 cf8074b3 Kevin Wolf
}
3158 cf8074b3 Kevin Wolf
3159 cf8074b3 Kevin Wolf
int bdrv_writev_vmstate(BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos)
3160 cf8074b3 Kevin Wolf
{
3161 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
3162 cf8074b3 Kevin Wolf
3163 cf8074b3 Kevin Wolf
    if (!drv) {
3164 178e08a5 aliguori
        return -ENOMEDIUM;
3165 cf8074b3 Kevin Wolf
    } else if (drv->bdrv_save_vmstate) {
3166 cf8074b3 Kevin Wolf
        return drv->bdrv_save_vmstate(bs, qiov, pos);
3167 cf8074b3 Kevin Wolf
    } else if (bs->file) {
3168 cf8074b3 Kevin Wolf
        return bdrv_writev_vmstate(bs->file, qiov, pos);
3169 cf8074b3 Kevin Wolf
    }
3170 cf8074b3 Kevin Wolf
3171 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
3172 178e08a5 aliguori
}
3173 178e08a5 aliguori
3174 45566e9c Christoph Hellwig
int bdrv_load_vmstate(BlockDriverState *bs, uint8_t *buf,
3175 45566e9c Christoph Hellwig
                      int64_t pos, int size)
3176 178e08a5 aliguori
{
3177 178e08a5 aliguori
    BlockDriver *drv = bs->drv;
3178 178e08a5 aliguori
    if (!drv)
3179 178e08a5 aliguori
        return -ENOMEDIUM;
3180 7cdb1f6d MORITA Kazutaka
    if (drv->bdrv_load_vmstate)
3181 7cdb1f6d MORITA Kazutaka
        return drv->bdrv_load_vmstate(bs, buf, pos, size);
3182 7cdb1f6d MORITA Kazutaka
    if (bs->file)
3183 7cdb1f6d MORITA Kazutaka
        return bdrv_load_vmstate(bs->file, buf, pos, size);
3184 7cdb1f6d MORITA Kazutaka
    return -ENOTSUP;
3185 178e08a5 aliguori
}
3186 178e08a5 aliguori
3187 8b9b0cc2 Kevin Wolf
void bdrv_debug_event(BlockDriverState *bs, BlkDebugEvent event)
3188 8b9b0cc2 Kevin Wolf
{
3189 8b9b0cc2 Kevin Wolf
    BlockDriver *drv = bs->drv;
3190 8b9b0cc2 Kevin Wolf
3191 8b9b0cc2 Kevin Wolf
    if (!drv || !drv->bdrv_debug_event) {
3192 8b9b0cc2 Kevin Wolf
        return;
3193 8b9b0cc2 Kevin Wolf
    }
3194 8b9b0cc2 Kevin Wolf
3195 0ed8b6f6 Blue Swirl
    drv->bdrv_debug_event(bs, event);
3196 41c695c7 Kevin Wolf
}
3197 41c695c7 Kevin Wolf
3198 41c695c7 Kevin Wolf
int bdrv_debug_breakpoint(BlockDriverState *bs, const char *event,
3199 41c695c7 Kevin Wolf
                          const char *tag)
3200 41c695c7 Kevin Wolf
{
3201 41c695c7 Kevin Wolf
    while (bs && bs->drv && !bs->drv->bdrv_debug_breakpoint) {
3202 41c695c7 Kevin Wolf
        bs = bs->file;
3203 41c695c7 Kevin Wolf
    }
3204 41c695c7 Kevin Wolf
3205 41c695c7 Kevin Wolf
    if (bs && bs->drv && bs->drv->bdrv_debug_breakpoint) {
3206 41c695c7 Kevin Wolf
        return bs->drv->bdrv_debug_breakpoint(bs, event, tag);
3207 41c695c7 Kevin Wolf
    }
3208 41c695c7 Kevin Wolf
3209 41c695c7 Kevin Wolf
    return -ENOTSUP;
3210 41c695c7 Kevin Wolf
}
3211 41c695c7 Kevin Wolf
3212 41c695c7 Kevin Wolf
int bdrv_debug_resume(BlockDriverState *bs, const char *tag)
3213 41c695c7 Kevin Wolf
{
3214 41c695c7 Kevin Wolf
    while (bs && bs->drv && !bs->drv->bdrv_debug_resume) {
3215 41c695c7 Kevin Wolf
        bs = bs->file;
3216 41c695c7 Kevin Wolf
    }
3217 8b9b0cc2 Kevin Wolf
3218 41c695c7 Kevin Wolf
    if (bs && bs->drv && bs->drv->bdrv_debug_resume) {
3219 41c695c7 Kevin Wolf
        return bs->drv->bdrv_debug_resume(bs, tag);
3220 41c695c7 Kevin Wolf
    }
3221 41c695c7 Kevin Wolf
3222 41c695c7 Kevin Wolf
    return -ENOTSUP;
3223 41c695c7 Kevin Wolf
}
3224 41c695c7 Kevin Wolf
3225 41c695c7 Kevin Wolf
bool bdrv_debug_is_suspended(BlockDriverState *bs, const char *tag)
3226 41c695c7 Kevin Wolf
{
3227 41c695c7 Kevin Wolf
    while (bs && bs->drv && !bs->drv->bdrv_debug_is_suspended) {
3228 41c695c7 Kevin Wolf
        bs = bs->file;
3229 41c695c7 Kevin Wolf
    }
3230 41c695c7 Kevin Wolf
3231 41c695c7 Kevin Wolf
    if (bs && bs->drv && bs->drv->bdrv_debug_is_suspended) {
3232 41c695c7 Kevin Wolf
        return bs->drv->bdrv_debug_is_suspended(bs, tag);
3233 41c695c7 Kevin Wolf
    }
3234 41c695c7 Kevin Wolf
3235 41c695c7 Kevin Wolf
    return false;
3236 8b9b0cc2 Kevin Wolf
}
3237 8b9b0cc2 Kevin Wolf
3238 199630b6 Blue Swirl
int bdrv_is_snapshot(BlockDriverState *bs)
3239 199630b6 Blue Swirl
{
3240 199630b6 Blue Swirl
    return !!(bs->open_flags & BDRV_O_SNAPSHOT);
3241 199630b6 Blue Swirl
}
3242 199630b6 Blue Swirl
3243 b1b1d783 Jeff Cody
/* backing_file can either be relative, or absolute, or a protocol.  If it is
3244 b1b1d783 Jeff Cody
 * relative, it must be relative to the chain.  So, passing in bs->filename
3245 b1b1d783 Jeff Cody
 * from a BDS as backing_file should not be done, as that may be relative to
3246 b1b1d783 Jeff Cody
 * the CWD rather than the chain. */
3247 e8a6bb9c Marcelo Tosatti
BlockDriverState *bdrv_find_backing_image(BlockDriverState *bs,
3248 e8a6bb9c Marcelo Tosatti
        const char *backing_file)
3249 e8a6bb9c Marcelo Tosatti
{
3250 b1b1d783 Jeff Cody
    char *filename_full = NULL;
3251 b1b1d783 Jeff Cody
    char *backing_file_full = NULL;
3252 b1b1d783 Jeff Cody
    char *filename_tmp = NULL;
3253 b1b1d783 Jeff Cody
    int is_protocol = 0;
3254 b1b1d783 Jeff Cody
    BlockDriverState *curr_bs = NULL;
3255 b1b1d783 Jeff Cody
    BlockDriverState *retval = NULL;
3256 b1b1d783 Jeff Cody
3257 b1b1d783 Jeff Cody
    if (!bs || !bs->drv || !backing_file) {
3258 e8a6bb9c Marcelo Tosatti
        return NULL;
3259 e8a6bb9c Marcelo Tosatti
    }
3260 e8a6bb9c Marcelo Tosatti
3261 b1b1d783 Jeff Cody
    filename_full     = g_malloc(PATH_MAX);
3262 b1b1d783 Jeff Cody
    backing_file_full = g_malloc(PATH_MAX);
3263 b1b1d783 Jeff Cody
    filename_tmp      = g_malloc(PATH_MAX);
3264 b1b1d783 Jeff Cody
3265 b1b1d783 Jeff Cody
    is_protocol = path_has_protocol(backing_file);
3266 b1b1d783 Jeff Cody
3267 b1b1d783 Jeff Cody
    for (curr_bs = bs; curr_bs->backing_hd; curr_bs = curr_bs->backing_hd) {
3268 b1b1d783 Jeff Cody
3269 b1b1d783 Jeff Cody
        /* If either of the filename paths is actually a protocol, then
3270 b1b1d783 Jeff Cody
         * compare unmodified paths; otherwise make paths relative */
3271 b1b1d783 Jeff Cody
        if (is_protocol || path_has_protocol(curr_bs->backing_file)) {
3272 b1b1d783 Jeff Cody
            if (strcmp(backing_file, curr_bs->backing_file) == 0) {
3273 b1b1d783 Jeff Cody
                retval = curr_bs->backing_hd;
3274 b1b1d783 Jeff Cody
                break;
3275 b1b1d783 Jeff Cody
            }
3276 e8a6bb9c Marcelo Tosatti
        } else {
3277 b1b1d783 Jeff Cody
            /* If not an absolute filename path, make it relative to the current
3278 b1b1d783 Jeff Cody
             * image's filename path */
3279 b1b1d783 Jeff Cody
            path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3280 b1b1d783 Jeff Cody
                         backing_file);
3281 b1b1d783 Jeff Cody
3282 b1b1d783 Jeff Cody
            /* We are going to compare absolute pathnames */
3283 b1b1d783 Jeff Cody
            if (!realpath(filename_tmp, filename_full)) {
3284 b1b1d783 Jeff Cody
                continue;
3285 b1b1d783 Jeff Cody
            }
3286 b1b1d783 Jeff Cody
3287 b1b1d783 Jeff Cody
            /* We need to make sure the backing filename we are comparing against
3288 b1b1d783 Jeff Cody
             * is relative to the current image filename (or absolute) */
3289 b1b1d783 Jeff Cody
            path_combine(filename_tmp, PATH_MAX, curr_bs->filename,
3290 b1b1d783 Jeff Cody
                         curr_bs->backing_file);
3291 b1b1d783 Jeff Cody
3292 b1b1d783 Jeff Cody
            if (!realpath(filename_tmp, backing_file_full)) {
3293 b1b1d783 Jeff Cody
                continue;
3294 b1b1d783 Jeff Cody
            }
3295 b1b1d783 Jeff Cody
3296 b1b1d783 Jeff Cody
            if (strcmp(backing_file_full, filename_full) == 0) {
3297 b1b1d783 Jeff Cody
                retval = curr_bs->backing_hd;
3298 b1b1d783 Jeff Cody
                break;
3299 b1b1d783 Jeff Cody
            }
3300 e8a6bb9c Marcelo Tosatti
        }
3301 e8a6bb9c Marcelo Tosatti
    }
3302 e8a6bb9c Marcelo Tosatti
3303 b1b1d783 Jeff Cody
    g_free(filename_full);
3304 b1b1d783 Jeff Cody
    g_free(backing_file_full);
3305 b1b1d783 Jeff Cody
    g_free(filename_tmp);
3306 b1b1d783 Jeff Cody
    return retval;
3307 e8a6bb9c Marcelo Tosatti
}
3308 e8a6bb9c Marcelo Tosatti
3309 f198fd1c Benoรฎt Canet
int bdrv_get_backing_file_depth(BlockDriverState *bs)
3310 f198fd1c Benoรฎt Canet
{
3311 f198fd1c Benoรฎt Canet
    if (!bs->drv) {
3312 f198fd1c Benoรฎt Canet
        return 0;
3313 f198fd1c Benoรฎt Canet
    }
3314 f198fd1c Benoรฎt Canet
3315 f198fd1c Benoรฎt Canet
    if (!bs->backing_hd) {
3316 f198fd1c Benoรฎt Canet
        return 0;
3317 f198fd1c Benoรฎt Canet
    }
3318 f198fd1c Benoรฎt Canet
3319 f198fd1c Benoรฎt Canet
    return 1 + bdrv_get_backing_file_depth(bs->backing_hd);
3320 f198fd1c Benoรฎt Canet
}
3321 f198fd1c Benoรฎt Canet
3322 79fac568 Jeff Cody
BlockDriverState *bdrv_find_base(BlockDriverState *bs)
3323 79fac568 Jeff Cody
{
3324 79fac568 Jeff Cody
    BlockDriverState *curr_bs = NULL;
3325 79fac568 Jeff Cody
3326 79fac568 Jeff Cody
    if (!bs) {
3327 79fac568 Jeff Cody
        return NULL;
3328 79fac568 Jeff Cody
    }
3329 79fac568 Jeff Cody
3330 79fac568 Jeff Cody
    curr_bs = bs;
3331 79fac568 Jeff Cody
3332 79fac568 Jeff Cody
    while (curr_bs->backing_hd) {
3333 79fac568 Jeff Cody
        curr_bs = curr_bs->backing_hd;
3334 79fac568 Jeff Cody
    }
3335 79fac568 Jeff Cody
    return curr_bs;
3336 79fac568 Jeff Cody
}
3337 79fac568 Jeff Cody
3338 ea2384d3 bellard
/**************************************************************/
3339 83f64091 bellard
/* async I/Os */
3340 ea2384d3 bellard
3341 3b69e4b9 aliguori
BlockDriverAIOCB *bdrv_aio_readv(BlockDriverState *bs, int64_t sector_num,
3342 f141eafe aliguori
                                 QEMUIOVector *qiov, int nb_sectors,
3343 3b69e4b9 aliguori
                                 BlockDriverCompletionFunc *cb, void *opaque)
3344 3b69e4b9 aliguori
{
3345 bbf0a440 Stefan Hajnoczi
    trace_bdrv_aio_readv(bs, sector_num, nb_sectors, opaque);
3346 bbf0a440 Stefan Hajnoczi
3347 b2a61371 Stefan Hajnoczi
    return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
3348 8c5873d6 Stefan Hajnoczi
                                 cb, opaque, false);
3349 ea2384d3 bellard
}
3350 ea2384d3 bellard
3351 f141eafe aliguori
BlockDriverAIOCB *bdrv_aio_writev(BlockDriverState *bs, int64_t sector_num,
3352 f141eafe aliguori
                                  QEMUIOVector *qiov, int nb_sectors,
3353 f141eafe aliguori
                                  BlockDriverCompletionFunc *cb, void *opaque)
3354 ea2384d3 bellard
{
3355 bbf0a440 Stefan Hajnoczi
    trace_bdrv_aio_writev(bs, sector_num, nb_sectors, opaque);
3356 bbf0a440 Stefan Hajnoczi
3357 1a6e115b Stefan Hajnoczi
    return bdrv_co_aio_rw_vector(bs, sector_num, qiov, nb_sectors,
3358 8c5873d6 Stefan Hajnoczi
                                 cb, opaque, true);
3359 83f64091 bellard
}
3360 83f64091 bellard
3361 40b4f539 Kevin Wolf
3362 40b4f539 Kevin Wolf
typedef struct MultiwriteCB {
3363 40b4f539 Kevin Wolf
    int error;
3364 40b4f539 Kevin Wolf
    int num_requests;
3365 40b4f539 Kevin Wolf
    int num_callbacks;
3366 40b4f539 Kevin Wolf
    struct {
3367 40b4f539 Kevin Wolf
        BlockDriverCompletionFunc *cb;
3368 40b4f539 Kevin Wolf
        void *opaque;
3369 40b4f539 Kevin Wolf
        QEMUIOVector *free_qiov;
3370 40b4f539 Kevin Wolf
    } callbacks[];
3371 40b4f539 Kevin Wolf
} MultiwriteCB;
3372 40b4f539 Kevin Wolf
3373 40b4f539 Kevin Wolf
static void multiwrite_user_cb(MultiwriteCB *mcb)
3374 40b4f539 Kevin Wolf
{
3375 40b4f539 Kevin Wolf
    int i;
3376 40b4f539 Kevin Wolf
3377 40b4f539 Kevin Wolf
    for (i = 0; i < mcb->num_callbacks; i++) {
3378 40b4f539 Kevin Wolf
        mcb->callbacks[i].cb(mcb->callbacks[i].opaque, mcb->error);
3379 1e1ea48d Stefan Hajnoczi
        if (mcb->callbacks[i].free_qiov) {
3380 1e1ea48d Stefan Hajnoczi
            qemu_iovec_destroy(mcb->callbacks[i].free_qiov);
3381 1e1ea48d Stefan Hajnoczi
        }
3382 7267c094 Anthony Liguori
        g_free(mcb->callbacks[i].free_qiov);
3383 40b4f539 Kevin Wolf
    }
3384 40b4f539 Kevin Wolf
}
3385 40b4f539 Kevin Wolf
3386 40b4f539 Kevin Wolf
static void multiwrite_cb(void *opaque, int ret)
3387 40b4f539 Kevin Wolf
{
3388 40b4f539 Kevin Wolf
    MultiwriteCB *mcb = opaque;
3389 40b4f539 Kevin Wolf
3390 6d519a5f Stefan Hajnoczi
    trace_multiwrite_cb(mcb, ret);
3391 6d519a5f Stefan Hajnoczi
3392 cb6d3ca0 Kevin Wolf
    if (ret < 0 && !mcb->error) {
3393 40b4f539 Kevin Wolf
        mcb->error = ret;
3394 40b4f539 Kevin Wolf
    }
3395 40b4f539 Kevin Wolf
3396 40b4f539 Kevin Wolf
    mcb->num_requests--;
3397 40b4f539 Kevin Wolf
    if (mcb->num_requests == 0) {
3398 de189a1b Kevin Wolf
        multiwrite_user_cb(mcb);
3399 7267c094 Anthony Liguori
        g_free(mcb);
3400 40b4f539 Kevin Wolf
    }
3401 40b4f539 Kevin Wolf
}
3402 40b4f539 Kevin Wolf
3403 40b4f539 Kevin Wolf
static int multiwrite_req_compare(const void *a, const void *b)
3404 40b4f539 Kevin Wolf
{
3405 77be4366 Christoph Hellwig
    const BlockRequest *req1 = a, *req2 = b;
3406 77be4366 Christoph Hellwig
3407 77be4366 Christoph Hellwig
    /*
3408 77be4366 Christoph Hellwig
     * Note that we can't simply subtract req2->sector from req1->sector
3409 77be4366 Christoph Hellwig
     * here as that could overflow the return value.
3410 77be4366 Christoph Hellwig
     */
3411 77be4366 Christoph Hellwig
    if (req1->sector > req2->sector) {
3412 77be4366 Christoph Hellwig
        return 1;
3413 77be4366 Christoph Hellwig
    } else if (req1->sector < req2->sector) {
3414 77be4366 Christoph Hellwig
        return -1;
3415 77be4366 Christoph Hellwig
    } else {
3416 77be4366 Christoph Hellwig
        return 0;
3417 77be4366 Christoph Hellwig
    }
3418 40b4f539 Kevin Wolf
}
3419 40b4f539 Kevin Wolf
3420 40b4f539 Kevin Wolf
/*
3421 40b4f539 Kevin Wolf
 * Takes a bunch of requests and tries to merge them. Returns the number of
3422 40b4f539 Kevin Wolf
 * requests that remain after merging.
3423 40b4f539 Kevin Wolf
 */
3424 40b4f539 Kevin Wolf
static int multiwrite_merge(BlockDriverState *bs, BlockRequest *reqs,
3425 40b4f539 Kevin Wolf
    int num_reqs, MultiwriteCB *mcb)
3426 40b4f539 Kevin Wolf
{
3427 40b4f539 Kevin Wolf
    int i, outidx;
3428 40b4f539 Kevin Wolf
3429 40b4f539 Kevin Wolf
    // Sort requests by start sector
3430 40b4f539 Kevin Wolf
    qsort(reqs, num_reqs, sizeof(*reqs), &multiwrite_req_compare);
3431 40b4f539 Kevin Wolf
3432 40b4f539 Kevin Wolf
    // Check if adjacent requests touch the same clusters. If so, combine them,
3433 40b4f539 Kevin Wolf
    // filling up gaps with zero sectors.
3434 40b4f539 Kevin Wolf
    outidx = 0;
3435 40b4f539 Kevin Wolf
    for (i = 1; i < num_reqs; i++) {
3436 40b4f539 Kevin Wolf
        int merge = 0;
3437 40b4f539 Kevin Wolf
        int64_t oldreq_last = reqs[outidx].sector + reqs[outidx].nb_sectors;
3438 40b4f539 Kevin Wolf
3439 b6a127a1 Paolo Bonzini
        // Handle exactly sequential writes and overlapping writes.
3440 40b4f539 Kevin Wolf
        if (reqs[i].sector <= oldreq_last) {
3441 40b4f539 Kevin Wolf
            merge = 1;
3442 40b4f539 Kevin Wolf
        }
3443 40b4f539 Kevin Wolf
3444 e2a305fb Christoph Hellwig
        if (reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1 > IOV_MAX) {
3445 e2a305fb Christoph Hellwig
            merge = 0;
3446 e2a305fb Christoph Hellwig
        }
3447 e2a305fb Christoph Hellwig
3448 40b4f539 Kevin Wolf
        if (merge) {
3449 40b4f539 Kevin Wolf
            size_t size;
3450 7267c094 Anthony Liguori
            QEMUIOVector *qiov = g_malloc0(sizeof(*qiov));
3451 40b4f539 Kevin Wolf
            qemu_iovec_init(qiov,
3452 40b4f539 Kevin Wolf
                reqs[outidx].qiov->niov + reqs[i].qiov->niov + 1);
3453 40b4f539 Kevin Wolf
3454 40b4f539 Kevin Wolf
            // Add the first request to the merged one. If the requests are
3455 40b4f539 Kevin Wolf
            // overlapping, drop the last sectors of the first request.
3456 40b4f539 Kevin Wolf
            size = (reqs[i].sector - reqs[outidx].sector) << 9;
3457 1b093c48 Michael Tokarev
            qemu_iovec_concat(qiov, reqs[outidx].qiov, 0, size);
3458 40b4f539 Kevin Wolf
3459 b6a127a1 Paolo Bonzini
            // We should need to add any zeros between the two requests
3460 b6a127a1 Paolo Bonzini
            assert (reqs[i].sector <= oldreq_last);
3461 40b4f539 Kevin Wolf
3462 40b4f539 Kevin Wolf
            // Add the second request
3463 1b093c48 Michael Tokarev
            qemu_iovec_concat(qiov, reqs[i].qiov, 0, reqs[i].qiov->size);
3464 40b4f539 Kevin Wolf
3465 cbf1dff2 Kevin Wolf
            reqs[outidx].nb_sectors = qiov->size >> 9;
3466 40b4f539 Kevin Wolf
            reqs[outidx].qiov = qiov;
3467 40b4f539 Kevin Wolf
3468 40b4f539 Kevin Wolf
            mcb->callbacks[i].free_qiov = reqs[outidx].qiov;
3469 40b4f539 Kevin Wolf
        } else {
3470 40b4f539 Kevin Wolf
            outidx++;
3471 40b4f539 Kevin Wolf
            reqs[outidx].sector     = reqs[i].sector;
3472 40b4f539 Kevin Wolf
            reqs[outidx].nb_sectors = reqs[i].nb_sectors;
3473 40b4f539 Kevin Wolf
            reqs[outidx].qiov       = reqs[i].qiov;
3474 40b4f539 Kevin Wolf
        }
3475 40b4f539 Kevin Wolf
    }
3476 40b4f539 Kevin Wolf
3477 40b4f539 Kevin Wolf
    return outidx + 1;
3478 40b4f539 Kevin Wolf
}
3479 40b4f539 Kevin Wolf
3480 40b4f539 Kevin Wolf
/*
3481 40b4f539 Kevin Wolf
 * Submit multiple AIO write requests at once.
3482 40b4f539 Kevin Wolf
 *
3483 40b4f539 Kevin Wolf
 * On success, the function returns 0 and all requests in the reqs array have
3484 40b4f539 Kevin Wolf
 * been submitted. In error case this function returns -1, and any of the
3485 40b4f539 Kevin Wolf
 * requests may or may not be submitted yet. In particular, this means that the
3486 40b4f539 Kevin Wolf
 * callback will be called for some of the requests, for others it won't. The
3487 40b4f539 Kevin Wolf
 * caller must check the error field of the BlockRequest to wait for the right
3488 40b4f539 Kevin Wolf
 * callbacks (if error != 0, no callback will be called).
3489 40b4f539 Kevin Wolf
 *
3490 40b4f539 Kevin Wolf
 * The implementation may modify the contents of the reqs array, e.g. to merge
3491 40b4f539 Kevin Wolf
 * requests. However, the fields opaque and error are left unmodified as they
3492 40b4f539 Kevin Wolf
 * are used to signal failure for a single request to the caller.
3493 40b4f539 Kevin Wolf
 */
3494 40b4f539 Kevin Wolf
int bdrv_aio_multiwrite(BlockDriverState *bs, BlockRequest *reqs, int num_reqs)
3495 40b4f539 Kevin Wolf
{
3496 40b4f539 Kevin Wolf
    MultiwriteCB *mcb;
3497 40b4f539 Kevin Wolf
    int i;
3498 40b4f539 Kevin Wolf
3499 301db7c2 Ryan Harper
    /* don't submit writes if we don't have a medium */
3500 301db7c2 Ryan Harper
    if (bs->drv == NULL) {
3501 301db7c2 Ryan Harper
        for (i = 0; i < num_reqs; i++) {
3502 301db7c2 Ryan Harper
            reqs[i].error = -ENOMEDIUM;
3503 301db7c2 Ryan Harper
        }
3504 301db7c2 Ryan Harper
        return -1;
3505 301db7c2 Ryan Harper
    }
3506 301db7c2 Ryan Harper
3507 40b4f539 Kevin Wolf
    if (num_reqs == 0) {
3508 40b4f539 Kevin Wolf
        return 0;
3509 40b4f539 Kevin Wolf
    }
3510 40b4f539 Kevin Wolf
3511 40b4f539 Kevin Wolf
    // Create MultiwriteCB structure
3512 7267c094 Anthony Liguori
    mcb = g_malloc0(sizeof(*mcb) + num_reqs * sizeof(*mcb->callbacks));
3513 40b4f539 Kevin Wolf
    mcb->num_requests = 0;
3514 40b4f539 Kevin Wolf
    mcb->num_callbacks = num_reqs;
3515 40b4f539 Kevin Wolf
3516 40b4f539 Kevin Wolf
    for (i = 0; i < num_reqs; i++) {
3517 40b4f539 Kevin Wolf
        mcb->callbacks[i].cb = reqs[i].cb;
3518 40b4f539 Kevin Wolf
        mcb->callbacks[i].opaque = reqs[i].opaque;
3519 40b4f539 Kevin Wolf
    }
3520 40b4f539 Kevin Wolf
3521 40b4f539 Kevin Wolf
    // Check for mergable requests
3522 40b4f539 Kevin Wolf
    num_reqs = multiwrite_merge(bs, reqs, num_reqs, mcb);
3523 40b4f539 Kevin Wolf
3524 6d519a5f Stefan Hajnoczi
    trace_bdrv_aio_multiwrite(mcb, mcb->num_callbacks, num_reqs);
3525 6d519a5f Stefan Hajnoczi
3526 df9309fb Paolo Bonzini
    /* Run the aio requests. */
3527 df9309fb Paolo Bonzini
    mcb->num_requests = num_reqs;
3528 40b4f539 Kevin Wolf
    for (i = 0; i < num_reqs; i++) {
3529 ad54ae80 Paolo Bonzini
        bdrv_aio_writev(bs, reqs[i].sector, reqs[i].qiov,
3530 40b4f539 Kevin Wolf
            reqs[i].nb_sectors, multiwrite_cb, mcb);
3531 40b4f539 Kevin Wolf
    }
3532 40b4f539 Kevin Wolf
3533 40b4f539 Kevin Wolf
    return 0;
3534 40b4f539 Kevin Wolf
}
3535 40b4f539 Kevin Wolf
3536 83f64091 bellard
void bdrv_aio_cancel(BlockDriverAIOCB *acb)
3537 83f64091 bellard
{
3538 d7331bed Stefan Hajnoczi
    acb->aiocb_info->cancel(acb);
3539 83f64091 bellard
}
3540 83f64091 bellard
3541 98f90dba Zhi Yong Wu
/* block I/O throttling */
3542 98f90dba Zhi Yong Wu
static bool bdrv_exceed_bps_limits(BlockDriverState *bs, int nb_sectors,
3543 98f90dba Zhi Yong Wu
                 bool is_write, double elapsed_time, uint64_t *wait)
3544 98f90dba Zhi Yong Wu
{
3545 98f90dba Zhi Yong Wu
    uint64_t bps_limit = 0;
3546 ae29d6c6 Stefan Hajnoczi
    uint64_t extension;
3547 98f90dba Zhi Yong Wu
    double   bytes_limit, bytes_base, bytes_res;
3548 98f90dba Zhi Yong Wu
    double   slice_time, wait_time;
3549 98f90dba Zhi Yong Wu
3550 98f90dba Zhi Yong Wu
    if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
3551 98f90dba Zhi Yong Wu
        bps_limit = bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL];
3552 98f90dba Zhi Yong Wu
    } else if (bs->io_limits.bps[is_write]) {
3553 98f90dba Zhi Yong Wu
        bps_limit = bs->io_limits.bps[is_write];
3554 98f90dba Zhi Yong Wu
    } else {
3555 98f90dba Zhi Yong Wu
        if (wait) {
3556 98f90dba Zhi Yong Wu
            *wait = 0;
3557 98f90dba Zhi Yong Wu
        }
3558 98f90dba Zhi Yong Wu
3559 98f90dba Zhi Yong Wu
        return false;
3560 98f90dba Zhi Yong Wu
    }
3561 98f90dba Zhi Yong Wu
3562 98f90dba Zhi Yong Wu
    slice_time = bs->slice_end - bs->slice_start;
3563 98f90dba Zhi Yong Wu
    slice_time /= (NANOSECONDS_PER_SECOND);
3564 98f90dba Zhi Yong Wu
    bytes_limit = bps_limit * slice_time;
3565 5905fbc9 Stefan Hajnoczi
    bytes_base  = bs->slice_submitted.bytes[is_write];
3566 98f90dba Zhi Yong Wu
    if (bs->io_limits.bps[BLOCK_IO_LIMIT_TOTAL]) {
3567 5905fbc9 Stefan Hajnoczi
        bytes_base += bs->slice_submitted.bytes[!is_write];
3568 98f90dba Zhi Yong Wu
    }
3569 98f90dba Zhi Yong Wu
3570 98f90dba Zhi Yong Wu
    /* bytes_base: the bytes of data which have been read/written; and
3571 98f90dba Zhi Yong Wu
     *             it is obtained from the history statistic info.
3572 98f90dba Zhi Yong Wu
     * bytes_res: the remaining bytes of data which need to be read/written.
3573 98f90dba Zhi Yong Wu
     * (bytes_base + bytes_res) / bps_limit: used to calcuate
3574 98f90dba Zhi Yong Wu
     *             the total time for completing reading/writting all data.
3575 98f90dba Zhi Yong Wu
     */
3576 98f90dba Zhi Yong Wu
    bytes_res   = (unsigned) nb_sectors * BDRV_SECTOR_SIZE;
3577 98f90dba Zhi Yong Wu
3578 98f90dba Zhi Yong Wu
    if (bytes_base + bytes_res <= bytes_limit) {
3579 98f90dba Zhi Yong Wu
        if (wait) {
3580 98f90dba Zhi Yong Wu
            *wait = 0;
3581 98f90dba Zhi Yong Wu
        }
3582 98f90dba Zhi Yong Wu
3583 98f90dba Zhi Yong Wu
        return false;
3584 98f90dba Zhi Yong Wu
    }
3585 98f90dba Zhi Yong Wu
3586 98f90dba Zhi Yong Wu
    /* Calc approx time to dispatch */
3587 98f90dba Zhi Yong Wu
    wait_time = (bytes_base + bytes_res) / bps_limit - elapsed_time;
3588 98f90dba Zhi Yong Wu
3589 98f90dba Zhi Yong Wu
    /* When the I/O rate at runtime exceeds the limits,
3590 98f90dba Zhi Yong Wu
     * bs->slice_end need to be extended in order that the current statistic
3591 98f90dba Zhi Yong Wu
     * info can be kept until the timer fire, so it is increased and tuned
3592 98f90dba Zhi Yong Wu
     * based on the result of experiment.
3593 98f90dba Zhi Yong Wu
     */
3594 ae29d6c6 Stefan Hajnoczi
    extension = wait_time * NANOSECONDS_PER_SECOND;
3595 ae29d6c6 Stefan Hajnoczi
    extension = DIV_ROUND_UP(extension, BLOCK_IO_SLICE_TIME) *
3596 ae29d6c6 Stefan Hajnoczi
                BLOCK_IO_SLICE_TIME;
3597 ae29d6c6 Stefan Hajnoczi
    bs->slice_end += extension;
3598 98f90dba Zhi Yong Wu
    if (wait) {
3599 0775437f Stefan Hajnoczi
        *wait = wait_time * NANOSECONDS_PER_SECOND;
3600 98f90dba Zhi Yong Wu
    }
3601 98f90dba Zhi Yong Wu
3602 98f90dba Zhi Yong Wu
    return true;
3603 98f90dba Zhi Yong Wu
}
3604 98f90dba Zhi Yong Wu
3605 98f90dba Zhi Yong Wu
static bool bdrv_exceed_iops_limits(BlockDriverState *bs, bool is_write,
3606 98f90dba Zhi Yong Wu
                             double elapsed_time, uint64_t *wait)
3607 98f90dba Zhi Yong Wu
{
3608 98f90dba Zhi Yong Wu
    uint64_t iops_limit = 0;
3609 98f90dba Zhi Yong Wu
    double   ios_limit, ios_base;
3610 98f90dba Zhi Yong Wu
    double   slice_time, wait_time;
3611 98f90dba Zhi Yong Wu
3612 98f90dba Zhi Yong Wu
    if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
3613 98f90dba Zhi Yong Wu
        iops_limit = bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL];
3614 98f90dba Zhi Yong Wu
    } else if (bs->io_limits.iops[is_write]) {
3615 98f90dba Zhi Yong Wu
        iops_limit = bs->io_limits.iops[is_write];
3616 98f90dba Zhi Yong Wu
    } else {
3617 98f90dba Zhi Yong Wu
        if (wait) {
3618 98f90dba Zhi Yong Wu
            *wait = 0;
3619 98f90dba Zhi Yong Wu
        }
3620 98f90dba Zhi Yong Wu
3621 98f90dba Zhi Yong Wu
        return false;
3622 98f90dba Zhi Yong Wu
    }
3623 98f90dba Zhi Yong Wu
3624 98f90dba Zhi Yong Wu
    slice_time = bs->slice_end - bs->slice_start;
3625 98f90dba Zhi Yong Wu
    slice_time /= (NANOSECONDS_PER_SECOND);
3626 98f90dba Zhi Yong Wu
    ios_limit  = iops_limit * slice_time;
3627 5905fbc9 Stefan Hajnoczi
    ios_base   = bs->slice_submitted.ios[is_write];
3628 98f90dba Zhi Yong Wu
    if (bs->io_limits.iops[BLOCK_IO_LIMIT_TOTAL]) {
3629 5905fbc9 Stefan Hajnoczi
        ios_base += bs->slice_submitted.ios[!is_write];
3630 98f90dba Zhi Yong Wu
    }
3631 98f90dba Zhi Yong Wu
3632 98f90dba Zhi Yong Wu
    if (ios_base + 1 <= ios_limit) {
3633 98f90dba Zhi Yong Wu
        if (wait) {
3634 98f90dba Zhi Yong Wu
            *wait = 0;
3635 98f90dba Zhi Yong Wu
        }
3636 98f90dba Zhi Yong Wu
3637 98f90dba Zhi Yong Wu
        return false;
3638 98f90dba Zhi Yong Wu
    }
3639 98f90dba Zhi Yong Wu
3640 0775437f Stefan Hajnoczi
    /* Calc approx time to dispatch, in seconds */
3641 98f90dba Zhi Yong Wu
    wait_time = (ios_base + 1) / iops_limit;
3642 98f90dba Zhi Yong Wu
    if (wait_time > elapsed_time) {
3643 98f90dba Zhi Yong Wu
        wait_time = wait_time - elapsed_time;
3644 98f90dba Zhi Yong Wu
    } else {
3645 98f90dba Zhi Yong Wu
        wait_time = 0;
3646 98f90dba Zhi Yong Wu
    }
3647 98f90dba Zhi Yong Wu
3648 ae29d6c6 Stefan Hajnoczi
    /* Exceeded current slice, extend it by another slice time */
3649 ae29d6c6 Stefan Hajnoczi
    bs->slice_end += BLOCK_IO_SLICE_TIME;
3650 98f90dba Zhi Yong Wu
    if (wait) {
3651 0775437f Stefan Hajnoczi
        *wait = wait_time * NANOSECONDS_PER_SECOND;
3652 98f90dba Zhi Yong Wu
    }
3653 98f90dba Zhi Yong Wu
3654 98f90dba Zhi Yong Wu
    return true;
3655 98f90dba Zhi Yong Wu
}
3656 98f90dba Zhi Yong Wu
3657 98f90dba Zhi Yong Wu
static bool bdrv_exceed_io_limits(BlockDriverState *bs, int nb_sectors,
3658 98f90dba Zhi Yong Wu
                           bool is_write, int64_t *wait)
3659 98f90dba Zhi Yong Wu
{
3660 98f90dba Zhi Yong Wu
    int64_t  now, max_wait;
3661 98f90dba Zhi Yong Wu
    uint64_t bps_wait = 0, iops_wait = 0;
3662 98f90dba Zhi Yong Wu
    double   elapsed_time;
3663 98f90dba Zhi Yong Wu
    int      bps_ret, iops_ret;
3664 98f90dba Zhi Yong Wu
3665 98f90dba Zhi Yong Wu
    now = qemu_get_clock_ns(vm_clock);
3666 e660fb8b Stefan Hajnoczi
    if (now > bs->slice_end) {
3667 98f90dba Zhi Yong Wu
        bs->slice_start = now;
3668 ae29d6c6 Stefan Hajnoczi
        bs->slice_end   = now + BLOCK_IO_SLICE_TIME;
3669 5905fbc9 Stefan Hajnoczi
        memset(&bs->slice_submitted, 0, sizeof(bs->slice_submitted));
3670 98f90dba Zhi Yong Wu
    }
3671 98f90dba Zhi Yong Wu
3672 98f90dba Zhi Yong Wu
    elapsed_time  = now - bs->slice_start;
3673 98f90dba Zhi Yong Wu
    elapsed_time  /= (NANOSECONDS_PER_SECOND);
3674 98f90dba Zhi Yong Wu
3675 98f90dba Zhi Yong Wu
    bps_ret  = bdrv_exceed_bps_limits(bs, nb_sectors,
3676 98f90dba Zhi Yong Wu
                                      is_write, elapsed_time, &bps_wait);
3677 98f90dba Zhi Yong Wu
    iops_ret = bdrv_exceed_iops_limits(bs, is_write,
3678 98f90dba Zhi Yong Wu
                                      elapsed_time, &iops_wait);
3679 98f90dba Zhi Yong Wu
    if (bps_ret || iops_ret) {
3680 98f90dba Zhi Yong Wu
        max_wait = bps_wait > iops_wait ? bps_wait : iops_wait;
3681 98f90dba Zhi Yong Wu
        if (wait) {
3682 98f90dba Zhi Yong Wu
            *wait = max_wait;
3683 98f90dba Zhi Yong Wu
        }
3684 98f90dba Zhi Yong Wu
3685 98f90dba Zhi Yong Wu
        now = qemu_get_clock_ns(vm_clock);
3686 98f90dba Zhi Yong Wu
        if (bs->slice_end < now + max_wait) {
3687 98f90dba Zhi Yong Wu
            bs->slice_end = now + max_wait;
3688 98f90dba Zhi Yong Wu
        }
3689 98f90dba Zhi Yong Wu
3690 98f90dba Zhi Yong Wu
        return true;
3691 98f90dba Zhi Yong Wu
    }
3692 98f90dba Zhi Yong Wu
3693 98f90dba Zhi Yong Wu
    if (wait) {
3694 98f90dba Zhi Yong Wu
        *wait = 0;
3695 98f90dba Zhi Yong Wu
    }
3696 98f90dba Zhi Yong Wu
3697 5905fbc9 Stefan Hajnoczi
    bs->slice_submitted.bytes[is_write] += (int64_t)nb_sectors *
3698 5905fbc9 Stefan Hajnoczi
                                           BDRV_SECTOR_SIZE;
3699 5905fbc9 Stefan Hajnoczi
    bs->slice_submitted.ios[is_write]++;
3700 5905fbc9 Stefan Hajnoczi
3701 98f90dba Zhi Yong Wu
    return false;
3702 98f90dba Zhi Yong Wu
}
3703 ce1a14dc pbrook
3704 83f64091 bellard
/**************************************************************/
3705 83f64091 bellard
/* async block device emulation */
3706 83f64091 bellard
3707 c16b5a2c Christoph Hellwig
typedef struct BlockDriverAIOCBSync {
3708 c16b5a2c Christoph Hellwig
    BlockDriverAIOCB common;
3709 c16b5a2c Christoph Hellwig
    QEMUBH *bh;
3710 c16b5a2c Christoph Hellwig
    int ret;
3711 c16b5a2c Christoph Hellwig
    /* vector translation state */
3712 c16b5a2c Christoph Hellwig
    QEMUIOVector *qiov;
3713 c16b5a2c Christoph Hellwig
    uint8_t *bounce;
3714 c16b5a2c Christoph Hellwig
    int is_write;
3715 c16b5a2c Christoph Hellwig
} BlockDriverAIOCBSync;
3716 c16b5a2c Christoph Hellwig
3717 c16b5a2c Christoph Hellwig
static void bdrv_aio_cancel_em(BlockDriverAIOCB *blockacb)
3718 c16b5a2c Christoph Hellwig
{
3719 b666d239 Kevin Wolf
    BlockDriverAIOCBSync *acb =
3720 b666d239 Kevin Wolf
        container_of(blockacb, BlockDriverAIOCBSync, common);
3721 6a7ad299 Dor Laor
    qemu_bh_delete(acb->bh);
3722 36afc451 Avi Kivity
    acb->bh = NULL;
3723 c16b5a2c Christoph Hellwig
    qemu_aio_release(acb);
3724 c16b5a2c Christoph Hellwig
}
3725 c16b5a2c Christoph Hellwig
3726 d7331bed Stefan Hajnoczi
static const AIOCBInfo bdrv_em_aiocb_info = {
3727 c16b5a2c Christoph Hellwig
    .aiocb_size         = sizeof(BlockDriverAIOCBSync),
3728 c16b5a2c Christoph Hellwig
    .cancel             = bdrv_aio_cancel_em,
3729 c16b5a2c Christoph Hellwig
};
3730 c16b5a2c Christoph Hellwig
3731 ce1a14dc pbrook
static void bdrv_aio_bh_cb(void *opaque)
3732 83f64091 bellard
{
3733 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb = opaque;
3734 f141eafe aliguori
3735 f141eafe aliguori
    if (!acb->is_write)
3736 03396148 Michael Tokarev
        qemu_iovec_from_buf(acb->qiov, 0, acb->bounce, acb->qiov->size);
3737 ceb42de8 aliguori
    qemu_vfree(acb->bounce);
3738 ce1a14dc pbrook
    acb->common.cb(acb->common.opaque, acb->ret);
3739 6a7ad299 Dor Laor
    qemu_bh_delete(acb->bh);
3740 36afc451 Avi Kivity
    acb->bh = NULL;
3741 ce1a14dc pbrook
    qemu_aio_release(acb);
3742 83f64091 bellard
}
3743 beac80cd bellard
3744 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_rw_vector(BlockDriverState *bs,
3745 f141eafe aliguori
                                            int64_t sector_num,
3746 f141eafe aliguori
                                            QEMUIOVector *qiov,
3747 f141eafe aliguori
                                            int nb_sectors,
3748 f141eafe aliguori
                                            BlockDriverCompletionFunc *cb,
3749 f141eafe aliguori
                                            void *opaque,
3750 f141eafe aliguori
                                            int is_write)
3751 f141eafe aliguori
3752 83f64091 bellard
{
3753 ce1a14dc pbrook
    BlockDriverAIOCBSync *acb;
3754 ce1a14dc pbrook
3755 d7331bed Stefan Hajnoczi
    acb = qemu_aio_get(&bdrv_em_aiocb_info, bs, cb, opaque);
3756 f141eafe aliguori
    acb->is_write = is_write;
3757 f141eafe aliguori
    acb->qiov = qiov;
3758 e268ca52 aliguori
    acb->bounce = qemu_blockalign(bs, qiov->size);
3759 3f3aace8 Paolo Bonzini
    acb->bh = qemu_bh_new(bdrv_aio_bh_cb, acb);
3760 f141eafe aliguori
3761 f141eafe aliguori
    if (is_write) {
3762 d5e6b161 Michael Tokarev
        qemu_iovec_to_buf(acb->qiov, 0, acb->bounce, qiov->size);
3763 1ed20acf Stefan Hajnoczi
        acb->ret = bs->drv->bdrv_write(bs, sector_num, acb->bounce, nb_sectors);
3764 f141eafe aliguori
    } else {
3765 1ed20acf Stefan Hajnoczi
        acb->ret = bs->drv->bdrv_read(bs, sector_num, acb->bounce, nb_sectors);
3766 f141eafe aliguori
    }
3767 f141eafe aliguori
3768 ce1a14dc pbrook
    qemu_bh_schedule(acb->bh);
3769 f141eafe aliguori
3770 ce1a14dc pbrook
    return &acb->common;
3771 beac80cd bellard
}
3772 beac80cd bellard
3773 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_readv_em(BlockDriverState *bs,
3774 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
3775 ce1a14dc pbrook
        BlockDriverCompletionFunc *cb, void *opaque)
3776 beac80cd bellard
{
3777 f141eafe aliguori
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 0);
3778 f141eafe aliguori
}
3779 83f64091 bellard
3780 f141eafe aliguori
static BlockDriverAIOCB *bdrv_aio_writev_em(BlockDriverState *bs,
3781 f141eafe aliguori
        int64_t sector_num, QEMUIOVector *qiov, int nb_sectors,
3782 f141eafe aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
3783 f141eafe aliguori
{
3784 f141eafe aliguori
    return bdrv_aio_rw_vector(bs, sector_num, qiov, nb_sectors, cb, opaque, 1);
3785 beac80cd bellard
}
3786 beac80cd bellard
3787 68485420 Kevin Wolf
3788 68485420 Kevin Wolf
typedef struct BlockDriverAIOCBCoroutine {
3789 68485420 Kevin Wolf
    BlockDriverAIOCB common;
3790 68485420 Kevin Wolf
    BlockRequest req;
3791 68485420 Kevin Wolf
    bool is_write;
3792 d318aea9 Kevin Wolf
    bool *done;
3793 68485420 Kevin Wolf
    QEMUBH* bh;
3794 68485420 Kevin Wolf
} BlockDriverAIOCBCoroutine;
3795 68485420 Kevin Wolf
3796 68485420 Kevin Wolf
static void bdrv_aio_co_cancel_em(BlockDriverAIOCB *blockacb)
3797 68485420 Kevin Wolf
{
3798 d318aea9 Kevin Wolf
    BlockDriverAIOCBCoroutine *acb =
3799 d318aea9 Kevin Wolf
        container_of(blockacb, BlockDriverAIOCBCoroutine, common);
3800 d318aea9 Kevin Wolf
    bool done = false;
3801 d318aea9 Kevin Wolf
3802 d318aea9 Kevin Wolf
    acb->done = &done;
3803 d318aea9 Kevin Wolf
    while (!done) {
3804 d318aea9 Kevin Wolf
        qemu_aio_wait();
3805 d318aea9 Kevin Wolf
    }
3806 68485420 Kevin Wolf
}
3807 68485420 Kevin Wolf
3808 d7331bed Stefan Hajnoczi
static const AIOCBInfo bdrv_em_co_aiocb_info = {
3809 68485420 Kevin Wolf
    .aiocb_size         = sizeof(BlockDriverAIOCBCoroutine),
3810 68485420 Kevin Wolf
    .cancel             = bdrv_aio_co_cancel_em,
3811 68485420 Kevin Wolf
};
3812 68485420 Kevin Wolf
3813 35246a68 Paolo Bonzini
static void bdrv_co_em_bh(void *opaque)
3814 68485420 Kevin Wolf
{
3815 68485420 Kevin Wolf
    BlockDriverAIOCBCoroutine *acb = opaque;
3816 68485420 Kevin Wolf
3817 68485420 Kevin Wolf
    acb->common.cb(acb->common.opaque, acb->req.error);
3818 d318aea9 Kevin Wolf
3819 d318aea9 Kevin Wolf
    if (acb->done) {
3820 d318aea9 Kevin Wolf
        *acb->done = true;
3821 d318aea9 Kevin Wolf
    }
3822 d318aea9 Kevin Wolf
3823 68485420 Kevin Wolf
    qemu_bh_delete(acb->bh);
3824 68485420 Kevin Wolf
    qemu_aio_release(acb);
3825 68485420 Kevin Wolf
}
3826 68485420 Kevin Wolf
3827 b2a61371 Stefan Hajnoczi
/* Invoke bdrv_co_do_readv/bdrv_co_do_writev */
3828 b2a61371 Stefan Hajnoczi
static void coroutine_fn bdrv_co_do_rw(void *opaque)
3829 b2a61371 Stefan Hajnoczi
{
3830 b2a61371 Stefan Hajnoczi
    BlockDriverAIOCBCoroutine *acb = opaque;
3831 b2a61371 Stefan Hajnoczi
    BlockDriverState *bs = acb->common.bs;
3832 b2a61371 Stefan Hajnoczi
3833 b2a61371 Stefan Hajnoczi
    if (!acb->is_write) {
3834 b2a61371 Stefan Hajnoczi
        acb->req.error = bdrv_co_do_readv(bs, acb->req.sector,
3835 470c0504 Stefan Hajnoczi
            acb->req.nb_sectors, acb->req.qiov, 0);
3836 b2a61371 Stefan Hajnoczi
    } else {
3837 b2a61371 Stefan Hajnoczi
        acb->req.error = bdrv_co_do_writev(bs, acb->req.sector,
3838 f08f2dda Stefan Hajnoczi
            acb->req.nb_sectors, acb->req.qiov, 0);
3839 b2a61371 Stefan Hajnoczi
    }
3840 b2a61371 Stefan Hajnoczi
3841 35246a68 Paolo Bonzini
    acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
3842 b2a61371 Stefan Hajnoczi
    qemu_bh_schedule(acb->bh);
3843 b2a61371 Stefan Hajnoczi
}
3844 b2a61371 Stefan Hajnoczi
3845 68485420 Kevin Wolf
static BlockDriverAIOCB *bdrv_co_aio_rw_vector(BlockDriverState *bs,
3846 68485420 Kevin Wolf
                                               int64_t sector_num,
3847 68485420 Kevin Wolf
                                               QEMUIOVector *qiov,
3848 68485420 Kevin Wolf
                                               int nb_sectors,
3849 68485420 Kevin Wolf
                                               BlockDriverCompletionFunc *cb,
3850 68485420 Kevin Wolf
                                               void *opaque,
3851 8c5873d6 Stefan Hajnoczi
                                               bool is_write)
3852 68485420 Kevin Wolf
{
3853 68485420 Kevin Wolf
    Coroutine *co;
3854 68485420 Kevin Wolf
    BlockDriverAIOCBCoroutine *acb;
3855 68485420 Kevin Wolf
3856 d7331bed Stefan Hajnoczi
    acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
3857 68485420 Kevin Wolf
    acb->req.sector = sector_num;
3858 68485420 Kevin Wolf
    acb->req.nb_sectors = nb_sectors;
3859 68485420 Kevin Wolf
    acb->req.qiov = qiov;
3860 68485420 Kevin Wolf
    acb->is_write = is_write;
3861 d318aea9 Kevin Wolf
    acb->done = NULL;
3862 68485420 Kevin Wolf
3863 8c5873d6 Stefan Hajnoczi
    co = qemu_coroutine_create(bdrv_co_do_rw);
3864 68485420 Kevin Wolf
    qemu_coroutine_enter(co, acb);
3865 68485420 Kevin Wolf
3866 68485420 Kevin Wolf
    return &acb->common;
3867 68485420 Kevin Wolf
}
3868 68485420 Kevin Wolf
3869 07f07615 Paolo Bonzini
static void coroutine_fn bdrv_aio_flush_co_entry(void *opaque)
3870 b2e12bc6 Christoph Hellwig
{
3871 07f07615 Paolo Bonzini
    BlockDriverAIOCBCoroutine *acb = opaque;
3872 07f07615 Paolo Bonzini
    BlockDriverState *bs = acb->common.bs;
3873 b2e12bc6 Christoph Hellwig
3874 07f07615 Paolo Bonzini
    acb->req.error = bdrv_co_flush(bs);
3875 07f07615 Paolo Bonzini
    acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
3876 b2e12bc6 Christoph Hellwig
    qemu_bh_schedule(acb->bh);
3877 b2e12bc6 Christoph Hellwig
}
3878 b2e12bc6 Christoph Hellwig
3879 07f07615 Paolo Bonzini
BlockDriverAIOCB *bdrv_aio_flush(BlockDriverState *bs,
3880 016f5cf6 Alexander Graf
        BlockDriverCompletionFunc *cb, void *opaque)
3881 016f5cf6 Alexander Graf
{
3882 07f07615 Paolo Bonzini
    trace_bdrv_aio_flush(bs, opaque);
3883 016f5cf6 Alexander Graf
3884 07f07615 Paolo Bonzini
    Coroutine *co;
3885 07f07615 Paolo Bonzini
    BlockDriverAIOCBCoroutine *acb;
3886 016f5cf6 Alexander Graf
3887 d7331bed Stefan Hajnoczi
    acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
3888 d318aea9 Kevin Wolf
    acb->done = NULL;
3889 d318aea9 Kevin Wolf
3890 07f07615 Paolo Bonzini
    co = qemu_coroutine_create(bdrv_aio_flush_co_entry);
3891 07f07615 Paolo Bonzini
    qemu_coroutine_enter(co, acb);
3892 016f5cf6 Alexander Graf
3893 016f5cf6 Alexander Graf
    return &acb->common;
3894 016f5cf6 Alexander Graf
}
3895 016f5cf6 Alexander Graf
3896 4265d620 Paolo Bonzini
static void coroutine_fn bdrv_aio_discard_co_entry(void *opaque)
3897 4265d620 Paolo Bonzini
{
3898 4265d620 Paolo Bonzini
    BlockDriverAIOCBCoroutine *acb = opaque;
3899 4265d620 Paolo Bonzini
    BlockDriverState *bs = acb->common.bs;
3900 4265d620 Paolo Bonzini
3901 4265d620 Paolo Bonzini
    acb->req.error = bdrv_co_discard(bs, acb->req.sector, acb->req.nb_sectors);
3902 4265d620 Paolo Bonzini
    acb->bh = qemu_bh_new(bdrv_co_em_bh, acb);
3903 4265d620 Paolo Bonzini
    qemu_bh_schedule(acb->bh);
3904 4265d620 Paolo Bonzini
}
3905 4265d620 Paolo Bonzini
3906 4265d620 Paolo Bonzini
BlockDriverAIOCB *bdrv_aio_discard(BlockDriverState *bs,
3907 4265d620 Paolo Bonzini
        int64_t sector_num, int nb_sectors,
3908 4265d620 Paolo Bonzini
        BlockDriverCompletionFunc *cb, void *opaque)
3909 4265d620 Paolo Bonzini
{
3910 4265d620 Paolo Bonzini
    Coroutine *co;
3911 4265d620 Paolo Bonzini
    BlockDriverAIOCBCoroutine *acb;
3912 4265d620 Paolo Bonzini
3913 4265d620 Paolo Bonzini
    trace_bdrv_aio_discard(bs, sector_num, nb_sectors, opaque);
3914 4265d620 Paolo Bonzini
3915 d7331bed Stefan Hajnoczi
    acb = qemu_aio_get(&bdrv_em_co_aiocb_info, bs, cb, opaque);
3916 4265d620 Paolo Bonzini
    acb->req.sector = sector_num;
3917 4265d620 Paolo Bonzini
    acb->req.nb_sectors = nb_sectors;
3918 d318aea9 Kevin Wolf
    acb->done = NULL;
3919 4265d620 Paolo Bonzini
    co = qemu_coroutine_create(bdrv_aio_discard_co_entry);
3920 4265d620 Paolo Bonzini
    qemu_coroutine_enter(co, acb);
3921 4265d620 Paolo Bonzini
3922 4265d620 Paolo Bonzini
    return &acb->common;
3923 4265d620 Paolo Bonzini
}
3924 4265d620 Paolo Bonzini
3925 ea2384d3 bellard
void bdrv_init(void)
3926 ea2384d3 bellard
{
3927 5efa9d5a Anthony Liguori
    module_call_init(MODULE_INIT_BLOCK);
3928 ea2384d3 bellard
}
3929 ce1a14dc pbrook
3930 eb852011 Markus Armbruster
void bdrv_init_with_whitelist(void)
3931 eb852011 Markus Armbruster
{
3932 eb852011 Markus Armbruster
    use_bdrv_whitelist = 1;
3933 eb852011 Markus Armbruster
    bdrv_init();
3934 eb852011 Markus Armbruster
}
3935 eb852011 Markus Armbruster
3936 d7331bed Stefan Hajnoczi
void *qemu_aio_get(const AIOCBInfo *aiocb_info, BlockDriverState *bs,
3937 c16b5a2c Christoph Hellwig
                   BlockDriverCompletionFunc *cb, void *opaque)
3938 ce1a14dc pbrook
{
3939 ce1a14dc pbrook
    BlockDriverAIOCB *acb;
3940 ce1a14dc pbrook
3941 d7331bed Stefan Hajnoczi
    acb = g_slice_alloc(aiocb_info->aiocb_size);
3942 d7331bed Stefan Hajnoczi
    acb->aiocb_info = aiocb_info;
3943 ce1a14dc pbrook
    acb->bs = bs;
3944 ce1a14dc pbrook
    acb->cb = cb;
3945 ce1a14dc pbrook
    acb->opaque = opaque;
3946 ce1a14dc pbrook
    return acb;
3947 ce1a14dc pbrook
}
3948 ce1a14dc pbrook
3949 ce1a14dc pbrook
void qemu_aio_release(void *p)
3950 ce1a14dc pbrook
{
3951 d37c975f Stefan Hajnoczi
    BlockDriverAIOCB *acb = p;
3952 d7331bed Stefan Hajnoczi
    g_slice_free1(acb->aiocb_info->aiocb_size, acb);
3953 ce1a14dc pbrook
}
3954 19cb3738 bellard
3955 19cb3738 bellard
/**************************************************************/
3956 f9f05dc5 Kevin Wolf
/* Coroutine block device emulation */
3957 f9f05dc5 Kevin Wolf
3958 f9f05dc5 Kevin Wolf
typedef struct CoroutineIOCompletion {
3959 f9f05dc5 Kevin Wolf
    Coroutine *coroutine;
3960 f9f05dc5 Kevin Wolf
    int ret;
3961 f9f05dc5 Kevin Wolf
} CoroutineIOCompletion;
3962 f9f05dc5 Kevin Wolf
3963 f9f05dc5 Kevin Wolf
static void bdrv_co_io_em_complete(void *opaque, int ret)
3964 f9f05dc5 Kevin Wolf
{
3965 f9f05dc5 Kevin Wolf
    CoroutineIOCompletion *co = opaque;
3966 f9f05dc5 Kevin Wolf
3967 f9f05dc5 Kevin Wolf
    co->ret = ret;
3968 f9f05dc5 Kevin Wolf
    qemu_coroutine_enter(co->coroutine, NULL);
3969 f9f05dc5 Kevin Wolf
}
3970 f9f05dc5 Kevin Wolf
3971 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_io_em(BlockDriverState *bs, int64_t sector_num,
3972 f9f05dc5 Kevin Wolf
                                      int nb_sectors, QEMUIOVector *iov,
3973 f9f05dc5 Kevin Wolf
                                      bool is_write)
3974 f9f05dc5 Kevin Wolf
{
3975 f9f05dc5 Kevin Wolf
    CoroutineIOCompletion co = {
3976 f9f05dc5 Kevin Wolf
        .coroutine = qemu_coroutine_self(),
3977 f9f05dc5 Kevin Wolf
    };
3978 f9f05dc5 Kevin Wolf
    BlockDriverAIOCB *acb;
3979 f9f05dc5 Kevin Wolf
3980 f9f05dc5 Kevin Wolf
    if (is_write) {
3981 a652d160 Stefan Hajnoczi
        acb = bs->drv->bdrv_aio_writev(bs, sector_num, iov, nb_sectors,
3982 a652d160 Stefan Hajnoczi
                                       bdrv_co_io_em_complete, &co);
3983 f9f05dc5 Kevin Wolf
    } else {
3984 a652d160 Stefan Hajnoczi
        acb = bs->drv->bdrv_aio_readv(bs, sector_num, iov, nb_sectors,
3985 a652d160 Stefan Hajnoczi
                                      bdrv_co_io_em_complete, &co);
3986 f9f05dc5 Kevin Wolf
    }
3987 f9f05dc5 Kevin Wolf
3988 59370aaa Stefan Hajnoczi
    trace_bdrv_co_io_em(bs, sector_num, nb_sectors, is_write, acb);
3989 f9f05dc5 Kevin Wolf
    if (!acb) {
3990 f9f05dc5 Kevin Wolf
        return -EIO;
3991 f9f05dc5 Kevin Wolf
    }
3992 f9f05dc5 Kevin Wolf
    qemu_coroutine_yield();
3993 f9f05dc5 Kevin Wolf
3994 f9f05dc5 Kevin Wolf
    return co.ret;
3995 f9f05dc5 Kevin Wolf
}
3996 f9f05dc5 Kevin Wolf
3997 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_readv_em(BlockDriverState *bs,
3998 f9f05dc5 Kevin Wolf
                                         int64_t sector_num, int nb_sectors,
3999 f9f05dc5 Kevin Wolf
                                         QEMUIOVector *iov)
4000 f9f05dc5 Kevin Wolf
{
4001 f9f05dc5 Kevin Wolf
    return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, false);
4002 f9f05dc5 Kevin Wolf
}
4003 f9f05dc5 Kevin Wolf
4004 f9f05dc5 Kevin Wolf
static int coroutine_fn bdrv_co_writev_em(BlockDriverState *bs,
4005 f9f05dc5 Kevin Wolf
                                         int64_t sector_num, int nb_sectors,
4006 f9f05dc5 Kevin Wolf
                                         QEMUIOVector *iov)
4007 f9f05dc5 Kevin Wolf
{
4008 f9f05dc5 Kevin Wolf
    return bdrv_co_io_em(bs, sector_num, nb_sectors, iov, true);
4009 f9f05dc5 Kevin Wolf
}
4010 f9f05dc5 Kevin Wolf
4011 07f07615 Paolo Bonzini
static void coroutine_fn bdrv_flush_co_entry(void *opaque)
4012 e7a8a783 Kevin Wolf
{
4013 07f07615 Paolo Bonzini
    RwCo *rwco = opaque;
4014 07f07615 Paolo Bonzini
4015 07f07615 Paolo Bonzini
    rwco->ret = bdrv_co_flush(rwco->bs);
4016 07f07615 Paolo Bonzini
}
4017 07f07615 Paolo Bonzini
4018 07f07615 Paolo Bonzini
int coroutine_fn bdrv_co_flush(BlockDriverState *bs)
4019 07f07615 Paolo Bonzini
{
4020 eb489bb1 Kevin Wolf
    int ret;
4021 eb489bb1 Kevin Wolf
4022 29cdb251 Paolo Bonzini
    if (!bs || !bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
4023 07f07615 Paolo Bonzini
        return 0;
4024 eb489bb1 Kevin Wolf
    }
4025 eb489bb1 Kevin Wolf
4026 ca716364 Kevin Wolf
    /* Write back cached data to the OS even with cache=unsafe */
4027 eb489bb1 Kevin Wolf
    if (bs->drv->bdrv_co_flush_to_os) {
4028 eb489bb1 Kevin Wolf
        ret = bs->drv->bdrv_co_flush_to_os(bs);
4029 eb489bb1 Kevin Wolf
        if (ret < 0) {
4030 eb489bb1 Kevin Wolf
            return ret;
4031 eb489bb1 Kevin Wolf
        }
4032 eb489bb1 Kevin Wolf
    }
4033 eb489bb1 Kevin Wolf
4034 ca716364 Kevin Wolf
    /* But don't actually force it to the disk with cache=unsafe */
4035 ca716364 Kevin Wolf
    if (bs->open_flags & BDRV_O_NO_FLUSH) {
4036 d4c82329 Kevin Wolf
        goto flush_parent;
4037 ca716364 Kevin Wolf
    }
4038 ca716364 Kevin Wolf
4039 eb489bb1 Kevin Wolf
    if (bs->drv->bdrv_co_flush_to_disk) {
4040 29cdb251 Paolo Bonzini
        ret = bs->drv->bdrv_co_flush_to_disk(bs);
4041 07f07615 Paolo Bonzini
    } else if (bs->drv->bdrv_aio_flush) {
4042 07f07615 Paolo Bonzini
        BlockDriverAIOCB *acb;
4043 07f07615 Paolo Bonzini
        CoroutineIOCompletion co = {
4044 07f07615 Paolo Bonzini
            .coroutine = qemu_coroutine_self(),
4045 07f07615 Paolo Bonzini
        };
4046 07f07615 Paolo Bonzini
4047 07f07615 Paolo Bonzini
        acb = bs->drv->bdrv_aio_flush(bs, bdrv_co_io_em_complete, &co);
4048 07f07615 Paolo Bonzini
        if (acb == NULL) {
4049 29cdb251 Paolo Bonzini
            ret = -EIO;
4050 07f07615 Paolo Bonzini
        } else {
4051 07f07615 Paolo Bonzini
            qemu_coroutine_yield();
4052 29cdb251 Paolo Bonzini
            ret = co.ret;
4053 07f07615 Paolo Bonzini
        }
4054 07f07615 Paolo Bonzini
    } else {
4055 07f07615 Paolo Bonzini
        /*
4056 07f07615 Paolo Bonzini
         * Some block drivers always operate in either writethrough or unsafe
4057 07f07615 Paolo Bonzini
         * mode and don't support bdrv_flush therefore. Usually qemu doesn't
4058 07f07615 Paolo Bonzini
         * know how the server works (because the behaviour is hardcoded or
4059 07f07615 Paolo Bonzini
         * depends on server-side configuration), so we can't ensure that
4060 07f07615 Paolo Bonzini
         * everything is safe on disk. Returning an error doesn't work because
4061 07f07615 Paolo Bonzini
         * that would break guests even if the server operates in writethrough
4062 07f07615 Paolo Bonzini
         * mode.
4063 07f07615 Paolo Bonzini
         *
4064 07f07615 Paolo Bonzini
         * Let's hope the user knows what he's doing.
4065 07f07615 Paolo Bonzini
         */
4066 29cdb251 Paolo Bonzini
        ret = 0;
4067 07f07615 Paolo Bonzini
    }
4068 29cdb251 Paolo Bonzini
    if (ret < 0) {
4069 29cdb251 Paolo Bonzini
        return ret;
4070 29cdb251 Paolo Bonzini
    }
4071 29cdb251 Paolo Bonzini
4072 29cdb251 Paolo Bonzini
    /* Now flush the underlying protocol.  It will also have BDRV_O_NO_FLUSH
4073 29cdb251 Paolo Bonzini
     * in the case of cache=unsafe, so there are no useless flushes.
4074 29cdb251 Paolo Bonzini
     */
4075 d4c82329 Kevin Wolf
flush_parent:
4076 29cdb251 Paolo Bonzini
    return bdrv_co_flush(bs->file);
4077 07f07615 Paolo Bonzini
}
4078 07f07615 Paolo Bonzini
4079 0f15423c Anthony Liguori
void bdrv_invalidate_cache(BlockDriverState *bs)
4080 0f15423c Anthony Liguori
{
4081 0f15423c Anthony Liguori
    if (bs->drv && bs->drv->bdrv_invalidate_cache) {
4082 0f15423c Anthony Liguori
        bs->drv->bdrv_invalidate_cache(bs);
4083 0f15423c Anthony Liguori
    }
4084 0f15423c Anthony Liguori
}
4085 0f15423c Anthony Liguori
4086 0f15423c Anthony Liguori
void bdrv_invalidate_cache_all(void)
4087 0f15423c Anthony Liguori
{
4088 0f15423c Anthony Liguori
    BlockDriverState *bs;
4089 0f15423c Anthony Liguori
4090 0f15423c Anthony Liguori
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
4091 0f15423c Anthony Liguori
        bdrv_invalidate_cache(bs);
4092 0f15423c Anthony Liguori
    }
4093 0f15423c Anthony Liguori
}
4094 0f15423c Anthony Liguori
4095 07789269 Benoรฎt Canet
void bdrv_clear_incoming_migration_all(void)
4096 07789269 Benoรฎt Canet
{
4097 07789269 Benoรฎt Canet
    BlockDriverState *bs;
4098 07789269 Benoรฎt Canet
4099 07789269 Benoรฎt Canet
    QTAILQ_FOREACH(bs, &bdrv_states, list) {
4100 07789269 Benoรฎt Canet
        bs->open_flags = bs->open_flags & ~(BDRV_O_INCOMING);
4101 07789269 Benoรฎt Canet
    }
4102 07789269 Benoรฎt Canet
}
4103 07789269 Benoรฎt Canet
4104 07f07615 Paolo Bonzini
int bdrv_flush(BlockDriverState *bs)
4105 07f07615 Paolo Bonzini
{
4106 07f07615 Paolo Bonzini
    Coroutine *co;
4107 07f07615 Paolo Bonzini
    RwCo rwco = {
4108 07f07615 Paolo Bonzini
        .bs = bs,
4109 07f07615 Paolo Bonzini
        .ret = NOT_DONE,
4110 e7a8a783 Kevin Wolf
    };
4111 e7a8a783 Kevin Wolf
4112 07f07615 Paolo Bonzini
    if (qemu_in_coroutine()) {
4113 07f07615 Paolo Bonzini
        /* Fast-path if already in coroutine context */
4114 07f07615 Paolo Bonzini
        bdrv_flush_co_entry(&rwco);
4115 07f07615 Paolo Bonzini
    } else {
4116 07f07615 Paolo Bonzini
        co = qemu_coroutine_create(bdrv_flush_co_entry);
4117 07f07615 Paolo Bonzini
        qemu_coroutine_enter(co, &rwco);
4118 07f07615 Paolo Bonzini
        while (rwco.ret == NOT_DONE) {
4119 07f07615 Paolo Bonzini
            qemu_aio_wait();
4120 07f07615 Paolo Bonzini
        }
4121 e7a8a783 Kevin Wolf
    }
4122 07f07615 Paolo Bonzini
4123 07f07615 Paolo Bonzini
    return rwco.ret;
4124 e7a8a783 Kevin Wolf
}
4125 e7a8a783 Kevin Wolf
4126 4265d620 Paolo Bonzini
static void coroutine_fn bdrv_discard_co_entry(void *opaque)
4127 4265d620 Paolo Bonzini
{
4128 4265d620 Paolo Bonzini
    RwCo *rwco = opaque;
4129 4265d620 Paolo Bonzini
4130 4265d620 Paolo Bonzini
    rwco->ret = bdrv_co_discard(rwco->bs, rwco->sector_num, rwco->nb_sectors);
4131 4265d620 Paolo Bonzini
}
4132 4265d620 Paolo Bonzini
4133 4265d620 Paolo Bonzini
int coroutine_fn bdrv_co_discard(BlockDriverState *bs, int64_t sector_num,
4134 4265d620 Paolo Bonzini
                                 int nb_sectors)
4135 4265d620 Paolo Bonzini
{
4136 4265d620 Paolo Bonzini
    if (!bs->drv) {
4137 4265d620 Paolo Bonzini
        return -ENOMEDIUM;
4138 4265d620 Paolo Bonzini
    } else if (bdrv_check_request(bs, sector_num, nb_sectors)) {
4139 4265d620 Paolo Bonzini
        return -EIO;
4140 4265d620 Paolo Bonzini
    } else if (bs->read_only) {
4141 4265d620 Paolo Bonzini
        return -EROFS;
4142 df702c9b Paolo Bonzini
    }
4143 df702c9b Paolo Bonzini
4144 df702c9b Paolo Bonzini
    if (bs->dirty_bitmap) {
4145 8f0720ec Paolo Bonzini
        bdrv_reset_dirty(bs, sector_num, nb_sectors);
4146 df702c9b Paolo Bonzini
    }
4147 df702c9b Paolo Bonzini
4148 9e8f1835 Paolo Bonzini
    /* Do nothing if disabled.  */
4149 9e8f1835 Paolo Bonzini
    if (!(bs->open_flags & BDRV_O_UNMAP)) {
4150 9e8f1835 Paolo Bonzini
        return 0;
4151 9e8f1835 Paolo Bonzini
    }
4152 9e8f1835 Paolo Bonzini
4153 df702c9b Paolo Bonzini
    if (bs->drv->bdrv_co_discard) {
4154 4265d620 Paolo Bonzini
        return bs->drv->bdrv_co_discard(bs, sector_num, nb_sectors);
4155 4265d620 Paolo Bonzini
    } else if (bs->drv->bdrv_aio_discard) {
4156 4265d620 Paolo Bonzini
        BlockDriverAIOCB *acb;
4157 4265d620 Paolo Bonzini
        CoroutineIOCompletion co = {
4158 4265d620 Paolo Bonzini
            .coroutine = qemu_coroutine_self(),
4159 4265d620 Paolo Bonzini
        };
4160 4265d620 Paolo Bonzini
4161 4265d620 Paolo Bonzini
        acb = bs->drv->bdrv_aio_discard(bs, sector_num, nb_sectors,
4162 4265d620 Paolo Bonzini
                                        bdrv_co_io_em_complete, &co);
4163 4265d620 Paolo Bonzini
        if (acb == NULL) {
4164 4265d620 Paolo Bonzini
            return -EIO;
4165 4265d620 Paolo Bonzini
        } else {
4166 4265d620 Paolo Bonzini
            qemu_coroutine_yield();
4167 4265d620 Paolo Bonzini
            return co.ret;
4168 4265d620 Paolo Bonzini
        }
4169 4265d620 Paolo Bonzini
    } else {
4170 4265d620 Paolo Bonzini
        return 0;
4171 4265d620 Paolo Bonzini
    }
4172 4265d620 Paolo Bonzini
}
4173 4265d620 Paolo Bonzini
4174 4265d620 Paolo Bonzini
int bdrv_discard(BlockDriverState *bs, int64_t sector_num, int nb_sectors)
4175 4265d620 Paolo Bonzini
{
4176 4265d620 Paolo Bonzini
    Coroutine *co;
4177 4265d620 Paolo Bonzini
    RwCo rwco = {
4178 4265d620 Paolo Bonzini
        .bs = bs,
4179 4265d620 Paolo Bonzini
        .sector_num = sector_num,
4180 4265d620 Paolo Bonzini
        .nb_sectors = nb_sectors,
4181 4265d620 Paolo Bonzini
        .ret = NOT_DONE,
4182 4265d620 Paolo Bonzini
    };
4183 4265d620 Paolo Bonzini
4184 4265d620 Paolo Bonzini
    if (qemu_in_coroutine()) {
4185 4265d620 Paolo Bonzini
        /* Fast-path if already in coroutine context */
4186 4265d620 Paolo Bonzini
        bdrv_discard_co_entry(&rwco);
4187 4265d620 Paolo Bonzini
    } else {
4188 4265d620 Paolo Bonzini
        co = qemu_coroutine_create(bdrv_discard_co_entry);
4189 4265d620 Paolo Bonzini
        qemu_coroutine_enter(co, &rwco);
4190 4265d620 Paolo Bonzini
        while (rwco.ret == NOT_DONE) {
4191 4265d620 Paolo Bonzini
            qemu_aio_wait();
4192 4265d620 Paolo Bonzini
        }
4193 4265d620 Paolo Bonzini
    }
4194 4265d620 Paolo Bonzini
4195 4265d620 Paolo Bonzini
    return rwco.ret;
4196 4265d620 Paolo Bonzini
}
4197 4265d620 Paolo Bonzini
4198 f9f05dc5 Kevin Wolf
/**************************************************************/
4199 19cb3738 bellard
/* removable device support */
4200 19cb3738 bellard
4201 19cb3738 bellard
/**
4202 19cb3738 bellard
 * Return TRUE if the media is present
4203 19cb3738 bellard
 */
4204 19cb3738 bellard
int bdrv_is_inserted(BlockDriverState *bs)
4205 19cb3738 bellard
{
4206 19cb3738 bellard
    BlockDriver *drv = bs->drv;
4207 a1aff5bf Markus Armbruster
4208 19cb3738 bellard
    if (!drv)
4209 19cb3738 bellard
        return 0;
4210 19cb3738 bellard
    if (!drv->bdrv_is_inserted)
4211 a1aff5bf Markus Armbruster
        return 1;
4212 a1aff5bf Markus Armbruster
    return drv->bdrv_is_inserted(bs);
4213 19cb3738 bellard
}
4214 19cb3738 bellard
4215 19cb3738 bellard
/**
4216 8e49ca46 Markus Armbruster
 * Return whether the media changed since the last call to this
4217 8e49ca46 Markus Armbruster
 * function, or -ENOTSUP if we don't know.  Most drivers don't know.
4218 19cb3738 bellard
 */
4219 19cb3738 bellard
int bdrv_media_changed(BlockDriverState *bs)
4220 19cb3738 bellard
{
4221 19cb3738 bellard
    BlockDriver *drv = bs->drv;
4222 19cb3738 bellard
4223 8e49ca46 Markus Armbruster
    if (drv && drv->bdrv_media_changed) {
4224 8e49ca46 Markus Armbruster
        return drv->bdrv_media_changed(bs);
4225 8e49ca46 Markus Armbruster
    }
4226 8e49ca46 Markus Armbruster
    return -ENOTSUP;
4227 19cb3738 bellard
}
4228 19cb3738 bellard
4229 19cb3738 bellard
/**
4230 19cb3738 bellard
 * If eject_flag is TRUE, eject the media. Otherwise, close the tray
4231 19cb3738 bellard
 */
4232 f36f3949 Luiz Capitulino
void bdrv_eject(BlockDriverState *bs, bool eject_flag)
4233 19cb3738 bellard
{
4234 19cb3738 bellard
    BlockDriver *drv = bs->drv;
4235 19cb3738 bellard
4236 822e1cd1 Markus Armbruster
    if (drv && drv->bdrv_eject) {
4237 822e1cd1 Markus Armbruster
        drv->bdrv_eject(bs, eject_flag);
4238 19cb3738 bellard
    }
4239 6f382ed2 Luiz Capitulino
4240 6f382ed2 Luiz Capitulino
    if (bs->device_name[0] != '\0') {
4241 6f382ed2 Luiz Capitulino
        bdrv_emit_qmp_eject_event(bs, eject_flag);
4242 6f382ed2 Luiz Capitulino
    }
4243 19cb3738 bellard
}
4244 19cb3738 bellard
4245 19cb3738 bellard
/**
4246 19cb3738 bellard
 * Lock or unlock the media (if it is locked, the user won't be able
4247 19cb3738 bellard
 * to eject it manually).
4248 19cb3738 bellard
 */
4249 025e849a Markus Armbruster
void bdrv_lock_medium(BlockDriverState *bs, bool locked)
4250 19cb3738 bellard
{
4251 19cb3738 bellard
    BlockDriver *drv = bs->drv;
4252 19cb3738 bellard
4253 025e849a Markus Armbruster
    trace_bdrv_lock_medium(bs, locked);
4254 b8c6d095 Stefan Hajnoczi
4255 025e849a Markus Armbruster
    if (drv && drv->bdrv_lock_medium) {
4256 025e849a Markus Armbruster
        drv->bdrv_lock_medium(bs, locked);
4257 19cb3738 bellard
    }
4258 19cb3738 bellard
}
4259 985a03b0 ths
4260 985a03b0 ths
/* needed for generic scsi interface */
4261 985a03b0 ths
4262 985a03b0 ths
int bdrv_ioctl(BlockDriverState *bs, unsigned long int req, void *buf)
4263 985a03b0 ths
{
4264 985a03b0 ths
    BlockDriver *drv = bs->drv;
4265 985a03b0 ths
4266 985a03b0 ths
    if (drv && drv->bdrv_ioctl)
4267 985a03b0 ths
        return drv->bdrv_ioctl(bs, req, buf);
4268 985a03b0 ths
    return -ENOTSUP;
4269 985a03b0 ths
}
4270 7d780669 aliguori
4271 221f715d aliguori
BlockDriverAIOCB *bdrv_aio_ioctl(BlockDriverState *bs,
4272 221f715d aliguori
        unsigned long int req, void *buf,
4273 221f715d aliguori
        BlockDriverCompletionFunc *cb, void *opaque)
4274 7d780669 aliguori
{
4275 221f715d aliguori
    BlockDriver *drv = bs->drv;
4276 7d780669 aliguori
4277 221f715d aliguori
    if (drv && drv->bdrv_aio_ioctl)
4278 221f715d aliguori
        return drv->bdrv_aio_ioctl(bs, req, buf, cb, opaque);
4279 221f715d aliguori
    return NULL;
4280 7d780669 aliguori
}
4281 e268ca52 aliguori
4282 7b6f9300 Markus Armbruster
void bdrv_set_buffer_alignment(BlockDriverState *bs, int align)
4283 7b6f9300 Markus Armbruster
{
4284 7b6f9300 Markus Armbruster
    bs->buffer_alignment = align;
4285 7b6f9300 Markus Armbruster
}
4286 7cd1e32a lirans@il.ibm.com
4287 e268ca52 aliguori
void *qemu_blockalign(BlockDriverState *bs, size_t size)
4288 e268ca52 aliguori
{
4289 e268ca52 aliguori
    return qemu_memalign((bs && bs->buffer_alignment) ? bs->buffer_alignment : 512, size);
4290 e268ca52 aliguori
}
4291 7cd1e32a lirans@il.ibm.com
4292 c53b1c51 Stefan Hajnoczi
/*
4293 c53b1c51 Stefan Hajnoczi
 * Check if all memory in this vector is sector aligned.
4294 c53b1c51 Stefan Hajnoczi
 */
4295 c53b1c51 Stefan Hajnoczi
bool bdrv_qiov_is_aligned(BlockDriverState *bs, QEMUIOVector *qiov)
4296 c53b1c51 Stefan Hajnoczi
{
4297 c53b1c51 Stefan Hajnoczi
    int i;
4298 c53b1c51 Stefan Hajnoczi
4299 c53b1c51 Stefan Hajnoczi
    for (i = 0; i < qiov->niov; i++) {
4300 c53b1c51 Stefan Hajnoczi
        if ((uintptr_t) qiov->iov[i].iov_base % bs->buffer_alignment) {
4301 c53b1c51 Stefan Hajnoczi
            return false;
4302 c53b1c51 Stefan Hajnoczi
        }
4303 c53b1c51 Stefan Hajnoczi
    }
4304 c53b1c51 Stefan Hajnoczi
4305 c53b1c51 Stefan Hajnoczi
    return true;
4306 c53b1c51 Stefan Hajnoczi
}
4307 c53b1c51 Stefan Hajnoczi
4308 50717e94 Paolo Bonzini
void bdrv_set_dirty_tracking(BlockDriverState *bs, int granularity)
4309 7cd1e32a lirans@il.ibm.com
{
4310 7cd1e32a lirans@il.ibm.com
    int64_t bitmap_size;
4311 a55eb92c Jan Kiszka
4312 50717e94 Paolo Bonzini
    assert((granularity & (granularity - 1)) == 0);
4313 50717e94 Paolo Bonzini
4314 50717e94 Paolo Bonzini
    if (granularity) {
4315 50717e94 Paolo Bonzini
        granularity >>= BDRV_SECTOR_BITS;
4316 50717e94 Paolo Bonzini
        assert(!bs->dirty_bitmap);
4317 50717e94 Paolo Bonzini
        bitmap_size = (bdrv_getlength(bs) >> BDRV_SECTOR_BITS);
4318 50717e94 Paolo Bonzini
        bs->dirty_bitmap = hbitmap_alloc(bitmap_size, ffs(granularity) - 1);
4319 7cd1e32a lirans@il.ibm.com
    } else {
4320 c6d22830 Jan Kiszka
        if (bs->dirty_bitmap) {
4321 8f0720ec Paolo Bonzini
            hbitmap_free(bs->dirty_bitmap);
4322 c6d22830 Jan Kiszka
            bs->dirty_bitmap = NULL;
4323 a55eb92c Jan Kiszka
        }
4324 7cd1e32a lirans@il.ibm.com
    }
4325 7cd1e32a lirans@il.ibm.com
}
4326 7cd1e32a lirans@il.ibm.com
4327 7cd1e32a lirans@il.ibm.com
int bdrv_get_dirty(BlockDriverState *bs, int64_t sector)
4328 7cd1e32a lirans@il.ibm.com
{
4329 8f0720ec Paolo Bonzini
    if (bs->dirty_bitmap) {
4330 8f0720ec Paolo Bonzini
        return hbitmap_get(bs->dirty_bitmap, sector);
4331 7cd1e32a lirans@il.ibm.com
    } else {
4332 7cd1e32a lirans@il.ibm.com
        return 0;
4333 7cd1e32a lirans@il.ibm.com
    }
4334 7cd1e32a lirans@il.ibm.com
}
4335 7cd1e32a lirans@il.ibm.com
4336 8f0720ec Paolo Bonzini
void bdrv_dirty_iter_init(BlockDriverState *bs, HBitmapIter *hbi)
4337 1755da16 Paolo Bonzini
{
4338 8f0720ec Paolo Bonzini
    hbitmap_iter_init(hbi, bs->dirty_bitmap, 0);
4339 1755da16 Paolo Bonzini
}
4340 1755da16 Paolo Bonzini
4341 1755da16 Paolo Bonzini
void bdrv_set_dirty(BlockDriverState *bs, int64_t cur_sector,
4342 1755da16 Paolo Bonzini
                    int nr_sectors)
4343 1755da16 Paolo Bonzini
{
4344 8f0720ec Paolo Bonzini
    hbitmap_set(bs->dirty_bitmap, cur_sector, nr_sectors);
4345 1755da16 Paolo Bonzini
}
4346 1755da16 Paolo Bonzini
4347 a55eb92c Jan Kiszka
void bdrv_reset_dirty(BlockDriverState *bs, int64_t cur_sector,
4348 a55eb92c Jan Kiszka
                      int nr_sectors)
4349 7cd1e32a lirans@il.ibm.com
{
4350 8f0720ec Paolo Bonzini
    hbitmap_reset(bs->dirty_bitmap, cur_sector, nr_sectors);
4351 7cd1e32a lirans@il.ibm.com
}
4352 aaa0eb75 Liran Schour
4353 aaa0eb75 Liran Schour
int64_t bdrv_get_dirty_count(BlockDriverState *bs)
4354 aaa0eb75 Liran Schour
{
4355 8f0720ec Paolo Bonzini
    if (bs->dirty_bitmap) {
4356 acc906c6 Paolo Bonzini
        return hbitmap_count(bs->dirty_bitmap);
4357 8f0720ec Paolo Bonzini
    } else {
4358 8f0720ec Paolo Bonzini
        return 0;
4359 8f0720ec Paolo Bonzini
    }
4360 aaa0eb75 Liran Schour
}
4361 f88e1a42 Jes Sorensen
4362 db593f25 Marcelo Tosatti
void bdrv_set_in_use(BlockDriverState *bs, int in_use)
4363 db593f25 Marcelo Tosatti
{
4364 db593f25 Marcelo Tosatti
    assert(bs->in_use != in_use);
4365 db593f25 Marcelo Tosatti
    bs->in_use = in_use;
4366 db593f25 Marcelo Tosatti
}
4367 db593f25 Marcelo Tosatti
4368 db593f25 Marcelo Tosatti
int bdrv_in_use(BlockDriverState *bs)
4369 db593f25 Marcelo Tosatti
{
4370 db593f25 Marcelo Tosatti
    return bs->in_use;
4371 db593f25 Marcelo Tosatti
}
4372 db593f25 Marcelo Tosatti
4373 28a7282a Luiz Capitulino
void bdrv_iostatus_enable(BlockDriverState *bs)
4374 28a7282a Luiz Capitulino
{
4375 d6bf279e Luiz Capitulino
    bs->iostatus_enabled = true;
4376 58e21ef5 Luiz Capitulino
    bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
4377 28a7282a Luiz Capitulino
}
4378 28a7282a Luiz Capitulino
4379 28a7282a Luiz Capitulino
/* The I/O status is only enabled if the drive explicitly
4380 28a7282a Luiz Capitulino
 * enables it _and_ the VM is configured to stop on errors */
4381 28a7282a Luiz Capitulino
bool bdrv_iostatus_is_enabled(const BlockDriverState *bs)
4382 28a7282a Luiz Capitulino
{
4383 d6bf279e Luiz Capitulino
    return (bs->iostatus_enabled &&
4384 92aa5c6d Paolo Bonzini
           (bs->on_write_error == BLOCKDEV_ON_ERROR_ENOSPC ||
4385 92aa5c6d Paolo Bonzini
            bs->on_write_error == BLOCKDEV_ON_ERROR_STOP   ||
4386 92aa5c6d Paolo Bonzini
            bs->on_read_error == BLOCKDEV_ON_ERROR_STOP));
4387 28a7282a Luiz Capitulino
}
4388 28a7282a Luiz Capitulino
4389 28a7282a Luiz Capitulino
void bdrv_iostatus_disable(BlockDriverState *bs)
4390 28a7282a Luiz Capitulino
{
4391 d6bf279e Luiz Capitulino
    bs->iostatus_enabled = false;
4392 28a7282a Luiz Capitulino
}
4393 28a7282a Luiz Capitulino
4394 28a7282a Luiz Capitulino
void bdrv_iostatus_reset(BlockDriverState *bs)
4395 28a7282a Luiz Capitulino
{
4396 28a7282a Luiz Capitulino
    if (bdrv_iostatus_is_enabled(bs)) {
4397 58e21ef5 Luiz Capitulino
        bs->iostatus = BLOCK_DEVICE_IO_STATUS_OK;
4398 3bd293c3 Paolo Bonzini
        if (bs->job) {
4399 3bd293c3 Paolo Bonzini
            block_job_iostatus_reset(bs->job);
4400 3bd293c3 Paolo Bonzini
        }
4401 28a7282a Luiz Capitulino
    }
4402 28a7282a Luiz Capitulino
}
4403 28a7282a Luiz Capitulino
4404 28a7282a Luiz Capitulino
void bdrv_iostatus_set_err(BlockDriverState *bs, int error)
4405 28a7282a Luiz Capitulino
{
4406 3e1caa5f Paolo Bonzini
    assert(bdrv_iostatus_is_enabled(bs));
4407 3e1caa5f Paolo Bonzini
    if (bs->iostatus == BLOCK_DEVICE_IO_STATUS_OK) {
4408 58e21ef5 Luiz Capitulino
        bs->iostatus = error == ENOSPC ? BLOCK_DEVICE_IO_STATUS_NOSPACE :
4409 58e21ef5 Luiz Capitulino
                                         BLOCK_DEVICE_IO_STATUS_FAILED;
4410 28a7282a Luiz Capitulino
    }
4411 28a7282a Luiz Capitulino
}
4412 28a7282a Luiz Capitulino
4413 a597e79c Christoph Hellwig
void
4414 a597e79c Christoph Hellwig
bdrv_acct_start(BlockDriverState *bs, BlockAcctCookie *cookie, int64_t bytes,
4415 a597e79c Christoph Hellwig
        enum BlockAcctType type)
4416 a597e79c Christoph Hellwig
{
4417 a597e79c Christoph Hellwig
    assert(type < BDRV_MAX_IOTYPE);
4418 a597e79c Christoph Hellwig
4419 a597e79c Christoph Hellwig
    cookie->bytes = bytes;
4420 c488c7f6 Christoph Hellwig
    cookie->start_time_ns = get_clock();
4421 a597e79c Christoph Hellwig
    cookie->type = type;
4422 a597e79c Christoph Hellwig
}
4423 a597e79c Christoph Hellwig
4424 a597e79c Christoph Hellwig
void
4425 a597e79c Christoph Hellwig
bdrv_acct_done(BlockDriverState *bs, BlockAcctCookie *cookie)
4426 a597e79c Christoph Hellwig
{
4427 a597e79c Christoph Hellwig
    assert(cookie->type < BDRV_MAX_IOTYPE);
4428 a597e79c Christoph Hellwig
4429 a597e79c Christoph Hellwig
    bs->nr_bytes[cookie->type] += cookie->bytes;
4430 a597e79c Christoph Hellwig
    bs->nr_ops[cookie->type]++;
4431 c488c7f6 Christoph Hellwig
    bs->total_time_ns[cookie->type] += get_clock() - cookie->start_time_ns;
4432 a597e79c Christoph Hellwig
}
4433 a597e79c Christoph Hellwig
4434 d92ada22 Luiz Capitulino
void bdrv_img_create(const char *filename, const char *fmt,
4435 d92ada22 Luiz Capitulino
                     const char *base_filename, const char *base_fmt,
4436 f382d43a Miroslav Rezanina
                     char *options, uint64_t img_size, int flags,
4437 f382d43a Miroslav Rezanina
                     Error **errp, bool quiet)
4438 f88e1a42 Jes Sorensen
{
4439 f88e1a42 Jes Sorensen
    QEMUOptionParameter *param = NULL, *create_options = NULL;
4440 d220894e Kevin Wolf
    QEMUOptionParameter *backing_fmt, *backing_file, *size;
4441 f88e1a42 Jes Sorensen
    BlockDriverState *bs = NULL;
4442 f88e1a42 Jes Sorensen
    BlockDriver *drv, *proto_drv;
4443 96df67d1 Stefan Hajnoczi
    BlockDriver *backing_drv = NULL;
4444 f88e1a42 Jes Sorensen
    int ret = 0;
4445 f88e1a42 Jes Sorensen
4446 f88e1a42 Jes Sorensen
    /* Find driver and parse its options */
4447 f88e1a42 Jes Sorensen
    drv = bdrv_find_format(fmt);
4448 f88e1a42 Jes Sorensen
    if (!drv) {
4449 71c79813 Luiz Capitulino
        error_setg(errp, "Unknown file format '%s'", fmt);
4450 d92ada22 Luiz Capitulino
        return;
4451 f88e1a42 Jes Sorensen
    }
4452 f88e1a42 Jes Sorensen
4453 f88e1a42 Jes Sorensen
    proto_drv = bdrv_find_protocol(filename);
4454 f88e1a42 Jes Sorensen
    if (!proto_drv) {
4455 71c79813 Luiz Capitulino
        error_setg(errp, "Unknown protocol '%s'", filename);
4456 d92ada22 Luiz Capitulino
        return;
4457 f88e1a42 Jes Sorensen
    }
4458 f88e1a42 Jes Sorensen
4459 f88e1a42 Jes Sorensen
    create_options = append_option_parameters(create_options,
4460 f88e1a42 Jes Sorensen
                                              drv->create_options);
4461 f88e1a42 Jes Sorensen
    create_options = append_option_parameters(create_options,
4462 f88e1a42 Jes Sorensen
                                              proto_drv->create_options);
4463 f88e1a42 Jes Sorensen
4464 f88e1a42 Jes Sorensen
    /* Create parameter list with default values */
4465 f88e1a42 Jes Sorensen
    param = parse_option_parameters("", create_options, param);
4466 f88e1a42 Jes Sorensen
4467 f88e1a42 Jes Sorensen
    set_option_parameter_int(param, BLOCK_OPT_SIZE, img_size);
4468 f88e1a42 Jes Sorensen
4469 f88e1a42 Jes Sorensen
    /* Parse -o options */
4470 f88e1a42 Jes Sorensen
    if (options) {
4471 f88e1a42 Jes Sorensen
        param = parse_option_parameters(options, create_options, param);
4472 f88e1a42 Jes Sorensen
        if (param == NULL) {
4473 71c79813 Luiz Capitulino
            error_setg(errp, "Invalid options for file format '%s'.", fmt);
4474 f88e1a42 Jes Sorensen
            goto out;
4475 f88e1a42 Jes Sorensen
        }
4476 f88e1a42 Jes Sorensen
    }
4477 f88e1a42 Jes Sorensen
4478 f88e1a42 Jes Sorensen
    if (base_filename) {
4479 f88e1a42 Jes Sorensen
        if (set_option_parameter(param, BLOCK_OPT_BACKING_FILE,
4480 f88e1a42 Jes Sorensen
                                 base_filename)) {
4481 71c79813 Luiz Capitulino
            error_setg(errp, "Backing file not supported for file format '%s'",
4482 71c79813 Luiz Capitulino
                       fmt);
4483 f88e1a42 Jes Sorensen
            goto out;
4484 f88e1a42 Jes Sorensen
        }
4485 f88e1a42 Jes Sorensen
    }
4486 f88e1a42 Jes Sorensen
4487 f88e1a42 Jes Sorensen
    if (base_fmt) {
4488 f88e1a42 Jes Sorensen
        if (set_option_parameter(param, BLOCK_OPT_BACKING_FMT, base_fmt)) {
4489 71c79813 Luiz Capitulino
            error_setg(errp, "Backing file format not supported for file "
4490 71c79813 Luiz Capitulino
                             "format '%s'", fmt);
4491 f88e1a42 Jes Sorensen
            goto out;
4492 f88e1a42 Jes Sorensen
        }
4493 f88e1a42 Jes Sorensen
    }
4494 f88e1a42 Jes Sorensen
4495 792da93a Jes Sorensen
    backing_file = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
4496 792da93a Jes Sorensen
    if (backing_file && backing_file->value.s) {
4497 792da93a Jes Sorensen
        if (!strcmp(filename, backing_file->value.s)) {
4498 71c79813 Luiz Capitulino
            error_setg(errp, "Error: Trying to create an image with the "
4499 71c79813 Luiz Capitulino
                             "same filename as the backing file");
4500 792da93a Jes Sorensen
            goto out;
4501 792da93a Jes Sorensen
        }
4502 792da93a Jes Sorensen
    }
4503 792da93a Jes Sorensen
4504 f88e1a42 Jes Sorensen
    backing_fmt = get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
4505 f88e1a42 Jes Sorensen
    if (backing_fmt && backing_fmt->value.s) {
4506 96df67d1 Stefan Hajnoczi
        backing_drv = bdrv_find_format(backing_fmt->value.s);
4507 96df67d1 Stefan Hajnoczi
        if (!backing_drv) {
4508 71c79813 Luiz Capitulino
            error_setg(errp, "Unknown backing file format '%s'",
4509 71c79813 Luiz Capitulino
                       backing_fmt->value.s);
4510 f88e1a42 Jes Sorensen
            goto out;
4511 f88e1a42 Jes Sorensen
        }
4512 f88e1a42 Jes Sorensen
    }
4513 f88e1a42 Jes Sorensen
4514 f88e1a42 Jes Sorensen
    // The size for the image must always be specified, with one exception:
4515 f88e1a42 Jes Sorensen
    // If we are using a backing file, we can obtain the size from there
4516 d220894e Kevin Wolf
    size = get_option_parameter(param, BLOCK_OPT_SIZE);
4517 d220894e Kevin Wolf
    if (size && size->value.n == -1) {
4518 f88e1a42 Jes Sorensen
        if (backing_file && backing_file->value.s) {
4519 f88e1a42 Jes Sorensen
            uint64_t size;
4520 f88e1a42 Jes Sorensen
            char buf[32];
4521 63090dac Paolo Bonzini
            int back_flags;
4522 63090dac Paolo Bonzini
4523 63090dac Paolo Bonzini
            /* backing files always opened read-only */
4524 63090dac Paolo Bonzini
            back_flags =
4525 63090dac Paolo Bonzini
                flags & ~(BDRV_O_RDWR | BDRV_O_SNAPSHOT | BDRV_O_NO_BACKING);
4526 f88e1a42 Jes Sorensen
4527 f88e1a42 Jes Sorensen
            bs = bdrv_new("");
4528 f88e1a42 Jes Sorensen
4529 de9c0cec Kevin Wolf
            ret = bdrv_open(bs, backing_file->value.s, NULL, back_flags,
4530 de9c0cec Kevin Wolf
                            backing_drv);
4531 f88e1a42 Jes Sorensen
            if (ret < 0) {
4532 71c79813 Luiz Capitulino
                error_setg_errno(errp, -ret, "Could not open '%s'",
4533 71c79813 Luiz Capitulino
                                 backing_file->value.s);
4534 f88e1a42 Jes Sorensen
                goto out;
4535 f88e1a42 Jes Sorensen
            }
4536 f88e1a42 Jes Sorensen
            bdrv_get_geometry(bs, &size);
4537 f88e1a42 Jes Sorensen
            size *= 512;
4538 f88e1a42 Jes Sorensen
4539 f88e1a42 Jes Sorensen
            snprintf(buf, sizeof(buf), "%" PRId64, size);
4540 f88e1a42 Jes Sorensen
            set_option_parameter(param, BLOCK_OPT_SIZE, buf);
4541 f88e1a42 Jes Sorensen
        } else {
4542 71c79813 Luiz Capitulino
            error_setg(errp, "Image creation needs a size parameter");
4543 f88e1a42 Jes Sorensen
            goto out;
4544 f88e1a42 Jes Sorensen
        }
4545 f88e1a42 Jes Sorensen
    }
4546 f88e1a42 Jes Sorensen
4547 f382d43a Miroslav Rezanina
    if (!quiet) {
4548 f382d43a Miroslav Rezanina
        printf("Formatting '%s', fmt=%s ", filename, fmt);
4549 f382d43a Miroslav Rezanina
        print_option_parameters(param);
4550 f382d43a Miroslav Rezanina
        puts("");
4551 f382d43a Miroslav Rezanina
    }
4552 f88e1a42 Jes Sorensen
    ret = bdrv_create(drv, filename, param);
4553 f88e1a42 Jes Sorensen
    if (ret < 0) {
4554 f88e1a42 Jes Sorensen
        if (ret == -ENOTSUP) {
4555 71c79813 Luiz Capitulino
            error_setg(errp,"Formatting or formatting option not supported for "
4556 71c79813 Luiz Capitulino
                            "file format '%s'", fmt);
4557 f88e1a42 Jes Sorensen
        } else if (ret == -EFBIG) {
4558 f3f4d2c0 Kevin Wolf
            const char *cluster_size_hint = "";
4559 f3f4d2c0 Kevin Wolf
            if (get_option_parameter(create_options, BLOCK_OPT_CLUSTER_SIZE)) {
4560 f3f4d2c0 Kevin Wolf
                cluster_size_hint = " (try using a larger cluster size)";
4561 f3f4d2c0 Kevin Wolf
            }
4562 f3f4d2c0 Kevin Wolf
            error_setg(errp, "The image size is too large for file format '%s'%s",
4563 f3f4d2c0 Kevin Wolf
                       fmt, cluster_size_hint);
4564 f88e1a42 Jes Sorensen
        } else {
4565 71c79813 Luiz Capitulino
            error_setg(errp, "%s: error while creating %s: %s", filename, fmt,
4566 71c79813 Luiz Capitulino
                       strerror(-ret));
4567 f88e1a42 Jes Sorensen
        }
4568 f88e1a42 Jes Sorensen
    }
4569 f88e1a42 Jes Sorensen
4570 f88e1a42 Jes Sorensen
out:
4571 f88e1a42 Jes Sorensen
    free_option_parameters(create_options);
4572 f88e1a42 Jes Sorensen
    free_option_parameters(param);
4573 f88e1a42 Jes Sorensen
4574 f88e1a42 Jes Sorensen
    if (bs) {
4575 f88e1a42 Jes Sorensen
        bdrv_delete(bs);
4576 f88e1a42 Jes Sorensen
    }
4577 f88e1a42 Jes Sorensen
}
4578 85d126f3 Stefan Hajnoczi
4579 85d126f3 Stefan Hajnoczi
AioContext *bdrv_get_aio_context(BlockDriverState *bs)
4580 85d126f3 Stefan Hajnoczi
{
4581 85d126f3 Stefan Hajnoczi
    /* Currently BlockDriverState always uses the main loop AioContext */
4582 85d126f3 Stefan Hajnoczi
    return qemu_get_aio_context();
4583 85d126f3 Stefan Hajnoczi
}